24 require
'../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
26 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
29 $langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
32 $socid =
GETPOST(
'socid',
'int');
33 if (!empty($user->socid)) $socid = $user->socid;
37 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
38 $sortfield =
GETPOST(
"sortfield",
'alpha');
39 $sortorder =
GETPOST(
"sortorder",
'alpha');
41 if (empty($page) || $page == -1) { $page = 0; }
42 $offset = $limit * $page;
43 $pageprev = $page - 1;
44 $pagenext = $page + 1;
45 if (!$sortorder) $sortorder =
"DESC";
46 if (!$sortfield) $sortfield =
"f.datef";
49 if ($socid > 0) $object->fetch($socid);
52 $hookmanager->initHooks(array(
'thirdpartymargins',
'globalcard'));
59 $parameters = array(
'id'=>$socid);
60 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $object, $action);
61 if ($reshook < 0)
setEventMessages($hookmanager->error, $hookmanager->errors,
'errors');
69 $invoicestatic =
new Facture($db);
70 $form =
new Form($db);
72 $title = $langs->trans(
"ThirdParty").
' - '.$langs->trans(
"Margins");
73 if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match(
'/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) $title = $object->name.
' - '.$langs->trans(
"Files");
74 $help_url =
'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
80 $object->fetch($socid);
90 $linkback =
'<a href="'.DOL_URL_ROOT.
'/societe/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
92 dol_banner_tab($object,
'socid', $linkback, ($user->socid ? 0 : 1),
'rowid',
'nom');
94 print '<div class="fichecenter">';
96 print '<div class="underbanner clearboth"></div>';
97 print '<table class="border tableforfield" width="100%">';
101 print '<tr><td class="titlefield">';
102 print $langs->trans(
'CustomerCode').
'</td><td colspan="3">';
103 print $object->code_client;
104 $tmpcheck = $object->check_codeclient();
105 if ($tmpcheck != 0 && $tmpcheck != -5) {
106 print ' <font class="error">('.$langs->trans(
"WrongCustomerCode").
')</font>';
111 if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) && $object->fournisseur && !empty($user->rights->fournisseur->lire))
113 print '<tr><td class="titlefield">';
114 print $langs->trans(
'SupplierCode').
'</td><td colspan="3">';
115 print $object->code_fournisseur;
116 $tmpcheck = $object->check_codefournisseur();
117 if ($tmpcheck != 0 && $tmpcheck != -5) {
118 print ' <font class="error">('.$langs->trans(
"WrongSupplierCode").
')</font>';
124 print '<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="3">';
125 print '<span id="totalMargin"></span>';
129 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
130 print '<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="3">';
131 print '<span id="marginRate"></span>';
136 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
137 print '<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="3">';
138 print '<span id="markRate"></span>';
145 print '<div style="clear:both"></div>';
151 $sql =
"SELECT distinct s.nom, s.rowid as socid, s.code_client,";
152 $sql .=
" f.rowid as facid, f.ref, f.total as total_ht,";
153 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
154 $sql .=
" sum(d.total_ht) as selling_price,";
155 $sql .=
" sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
156 $sql .=
" sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge";
157 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
158 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
159 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
160 $sql .=
" WHERE f.fk_soc = s.rowid";
161 $sql .=
" AND f.fk_statut > 0";
162 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
163 $sql .=
" AND d.fk_facture = f.rowid";
164 $sql .=
" AND f.fk_soc = $socid";
165 $sql .=
" AND d.buy_price_ht IS NOT NULL";
166 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .=
" AND d.buy_price_ht <> 0";
167 $sql .=
" GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total, f.datef, f.paye, f.fk_statut, f.type";
168 $sql .= $db->order($sortfield, $sortorder);
172 dol_syslog(
'margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
173 $result = $db->query($sql);
176 $num = $db->num_rows($result);
178 print_barre_liste($langs->trans(
"MarginDetails"), $page,
$_SERVER[
"PHP_SELF"],
"&socid=".$object->id, $sortfield, $sortorder,
'', $num, $num,
'');
181 print '<div class="div-table-responsive">';
182 print "<table class=\"noborder\" width=\"100%\">";
184 print '<tr class="liste_titre">';
185 print_liste_field_titre(
"Invoice", $_SERVER[
"PHP_SELF"],
"f.ref",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder);
186 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'center ');
187 print_liste_field_titre(
"SoldAmount", $_SERVER[
"PHP_SELF"],
"selling_price",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
188 print_liste_field_titre(
"PurchasedAmount", $_SERVER[
"PHP_SELF"],
"buying_price",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
189 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
190 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
191 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
192 if (!empty($conf->global->DISPLAY_MARK_RATES))
193 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
194 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
204 $objp = $db->fetch_object($result);
206 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
207 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
214 print '<tr class="oddeven">';
216 $invoicestatic->id = $objp->facid;
217 $invoicestatic->ref = $objp->ref;
218 print $invoicestatic->getNomUrl(1);
220 print "<td class=\"center\">";
222 print "<td class=\"right\">".price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
223 print "<td class=\"right\">".price(
price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price,
'MT')).
"</td>\n";
224 print "<td class=\"right\">".$sign.price(
price2num($objp->marge,
'MT')).
"</td>\n";
225 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
226 print "<td class=\"right\">".(($marginRate ===
'') ?
'n/a' : $sign.price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
227 if (!empty($conf->global->DISPLAY_MARK_RATES))
228 print "<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
229 print '<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
232 $cumul_vente += $objp->selling_price;
233 $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
239 $totalMargin = $cumul_vente - $cumul_achat;
240 if ($totalMargin < 0)
242 $marginRate = ($cumul_achat != 0) ?-1 * (100 * $totalMargin / $cumul_achat) :
'';
243 $markRate = ($cumul_vente != 0) ?-1 * (100 * $totalMargin / $cumul_vente) :
'';
245 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
246 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
250 print '<tr class="liste_total">';
251 print '<td colspan=2>'.$langs->trans(
'TotalMargin').
"</td>";
252 print "<td class=\"right\">".price(
price2num($cumul_vente,
'MT')).
"</td>\n";
253 print "<td class=\"right\">".price(
price2num($cumul_achat,
'MT')).
"</td>\n";
254 print "<td class=\"right\">".price(
price2num($totalMargin,
'MT')).
"</td>\n";
255 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
256 print "<td class=\"right\">".(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
257 if (!empty($conf->global->DISPLAY_MARK_RATES))
258 print "<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
259 print '<td class="right"> </td>';
275 <script type="text/javascript">
276 $(document).ready(function() {
277 $("#totalMargin").html("'.
price(
price2num($totalMargin,
'MT')).
'");
278 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
279 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
const TYPE_CREDIT_NOTE
Credit note invoice.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
print_barre_liste($titre, $page, $file, $options= '', $sortfield= '', $sortorder= '', $morehtmlcenter= '', $num=-1, $totalnboflines= '', $picto= 'generic', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow= '')
Print a title with navigation controls for pagination.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
societe_prepare_head(Societe $object)
Return array of tabs to used on pages for third parties cards.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Class to manage invoices.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.