25 require
'../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
27 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
32 $langs->loadLangs(array(
'companies',
'bills',
'products',
'margins'));
35 $socid =
GETPOST(
'socid',
'int');
36 $TSelectedProducts =
GETPOST(
'products',
'array');
37 $TSelectedCats =
GETPOST(
'categories',
'array');
39 if (!empty($user->socid)) {
40 $socid = $user->socid;
47 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
48 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
49 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
51 if (empty($page) || $page == -1) {
54 $offset = $limit * $page;
55 $pageprev = $page - 1;
56 $pagenext = $page + 1;
64 $startdate = $enddate =
'';
65 if (
GETPOST(
'startdatemonth')) {
74 $hookmanager->initHooks(array(
'margincustomerlist'));
81 $companystatic =
new Societe($db);
82 $invoicestatic =
new Facture($db);
86 llxHeader(
'', $langs->trans(
"Margins").
' - '.$langs->trans(
"Clients"));
88 $text = $langs->trans(
"Margins");
93 $titre = $langs->trans(
"Margins");
97 print
'<form method="post" name="sel" action="'.$_SERVER[
'PHP_SELF'].
'">';
98 print
'<input type="hidden" name="token" value="'.newToken().
'">';
102 print
'<table class="border centpercent">';
110 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
111 print
'<td class="maxwidthonsmartphone" colspan="4">';
112 print
img_picto(
'',
'company').$form->select_company($socid,
'socid',
'(client=1 OR client=3)', 1, 0, 0);
120 $sortfield =
"f.datef";
124 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
125 print
'<td class="maxwidthonsmartphone" colspan="4">';
126 print
img_picto(
'',
'company').$form->select_company(
null,
'socid',
'(client=1 OR client=3)', 1, 0, 0);
130 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
131 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
137 $sortfield =
"f.datef";
140 $sortfield =
"s.nom";
146 $TRes =
$form->select_produits_list(
'',
'',
'',
'', 0,
'', 1, 2, 1, 0,
'', 1);
148 $TProducts = array();
149 foreach ($TRes as $prod) {
150 $TProducts[$prod[
'key']] = $prod[
'label'];
153 print
'<tr><td class="titlefield">'.$langs->trans(
'ProductOrService').
'</td>';
154 print
'<td class="maxwidthonsmartpone" colspan="4">';
155 print
img_picto(
'',
'product').$form->multiselectarray(
'products', $TProducts, $TSelectedProducts, 0, 0,
'minwidth500');
159 $TCats =
$form->select_all_categories(0, array(),
'', 64, 0, 1);
162 print
'<td class="titlefield">'.$langs->trans(
'Category').
'</td>';
163 print
'<td class="maxwidthonsmartphone" colspan="4">';
164 print
img_picto(
'',
'category').$form->multiselectarray(
'categories', $TCats, $TSelectedCats, 0, 0,
'quatrevingtpercent widthcentpercentminusx');
169 print
'<td>'.$langs->trans(
'DateStart').
' ('.$langs->trans(
"DateValidation").
')</td>';
171 print
$form->selectDate($startdate,
'startdate',
'',
'', 1,
"sel", 1, 1);
173 print
'<td>'.$langs->trans(
'DateEnd').
' ('.$langs->trans(
"DateValidation").
')</td>';
175 print
$form->selectDate($enddate,
'enddate',
'',
'', 1,
"sel", 1, 1);
177 print
'<td style="text-align: center;">';
178 print
'<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans(
'Refresh')).
'" />';
185 print
'<table class="border centpercent">';
188 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="4">';
189 print
'<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol($conf->currency).
'</span>';
193 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
194 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="4">';
195 print
'<span id="marginRate"></span>';
200 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
201 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="4">';
202 print
'<span id="markRate"></span>';
215 $sql .=
" s.rowid as socid, s.nom as name, s.code_client, s.client,";
217 $sql .=
" f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut as statut,";
219 $sql .=
" sum(d.total_ht) as selling_price,";
221 $sql .=
" sum(".$db->ifsql(
'd.total_ht < 0',
'd.qty * d.buy_price_ht * -1 * (d.situation_percent / 100)',
'd.qty * d.buy_price_ht * (d.situation_percent / 100)').
") as buying_price,";
222 $sql .=
" sum(".$db->ifsql(
'd.total_ht < 0',
'-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100)))',
'd.total_ht - (d.buy_price_ht * d.qty * (d.situation_percent / 100))').
") as marge";
223 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
224 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
225 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
226 if (!empty($TSelectedCats)) {
227 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_product as cp ON cp.fk_product=d.fk_product';
230 if (empty($user->rights->societe->client->voir) && !$socid) {
231 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
233 $sql .=
" WHERE f.fk_soc = s.rowid";
235 $sql .=
' AND s.rowid = '.((int) $socid);
237 if (empty($user->rights->societe->client->voir) && !$socid) {
238 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
240 $sql .=
" AND f.fk_statut NOT IN (".$db->sanitize(implode(
', ', $invoice_status_except_list)).
")";
241 $sql .=
' AND s.entity IN ('.getEntity(
'societe').
')';
242 $sql .=
' AND f.entity IN ('.getEntity(
'invoice').
')';
243 $sql .=
" AND d.fk_facture = f.rowid";
244 $sql .=
" AND (d.product_type = 0 OR d.product_type = 1)";
245 if (!empty($TSelectedProducts)) {
246 $sql .=
' AND d.fk_product IN ('.$db->sanitize(implode(
',', $TSelectedProducts)).
')';
248 if (!empty($TSelectedCats)) {
249 $sql .=
' AND cp.fk_categorie IN ('.$db->sanitize(implode(
',', $TSelectedCats)).
')';
251 if (!empty($startdate)) {
252 $sql .=
" AND f.datef >= '".$db->idate($startdate).
"'";
254 if (!empty($enddate)) {
255 $sql .=
" AND f.datef <= '".$db->idate($enddate).
"'";
257 $sql .=
" AND d.buy_price_ht IS NOT NULL";
260 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
261 $sql .=
" AND d.buy_price_ht <> 0";
264 $sql .=
" GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut";
266 $sql .=
" GROUP BY s.rowid, s.nom, s.code_client, s.client";
268 $sql .= $db->order($sortfield, $sortorder);
272 $param =
'&socid='.((int) $socid);
273 if (
GETPOST(
'startdatemonth',
'int')) {
274 $param .=
'&startdateyear='.GETPOST(
'startdateyear',
'int');
275 $param .=
'&startdatemonth='.GETPOST(
'startdatemonth',
'int');
276 $param .=
'&startdateday='.GETPOST(
'startdateday',
'int');
278 if (
GETPOST(
'enddatemonth',
'int')) {
279 $param .=
'&enddateyear='.GETPOST(
'enddateyear',
'int');
280 $param .=
'&enddatemonth='.GETPOST(
'enddatemonth',
'int');
281 $param .=
'&enddateday='.GETPOST(
'enddateday',
'int');
283 $listofproducts =
GETPOST(
'products',
'array:int');
284 if (is_array($listofproducts)) {
285 foreach ($listofproducts as $val) {
286 $param .=
'&products[]='.$val;
289 $listofcateg =
GETPOST(
'categories',
'array:int');
290 if (is_array($listofcateg)) {
291 foreach ($listofcateg as $val) {
292 $param .=
'&categories[]='.$val;
296 dol_syslog(
'margin::customerMargins.php', LOG_DEBUG);
297 $result = $db->query($sql);
299 $num = $db->num_rows($result);
302 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"],
"", $sortfield, $sortorder,
'', $num, $num,
'', 0,
'',
'', 0, 1);
304 if ($conf->global->MARGIN_TYPE ==
"1") {
305 $labelcostprice =
'BuyingPrice';
307 $labelcostprice =
'CostPrice';
313 print
'<div class="div-table-responsive">';
314 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
316 print
'<tr class="liste_titre">';
317 if (!empty($client)) {
319 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'align="center"', $sortfield, $sortorder);
323 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'align="right"', $sortfield, $sortorder);
324 print_liste_field_titre($labelcostprice, $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'align="right"', $sortfield, $sortorder);
325 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'align="right"', $sortfield, $sortorder);
326 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
327 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
329 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
330 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
339 $objp = $db->fetch_object($result);
341 $pa = $objp->buying_price;
342 $pv = $objp->selling_price;
343 $marge = $objp->marge;
346 $marginRate = ($pa != 0) ?-1 * (100 * $marge / $pa) :
'';
347 $markRate = ($pv != 0) ?-1 * (100 * $marge / $pv) :
'';
349 $marginRate = ($pa != 0) ? (100 * $marge / $pa) :
'';
350 $markRate = ($pv != 0) ? (100 * $marge / $pv) :
'';
353 print
'<tr class="oddeven">';
356 $invoicestatic->id = $objp->facid;
357 $invoicestatic->ref = $objp->ref;
358 print $invoicestatic->getNomUrl(1);
360 print
'<td class="center">';
363 $companystatic->id = $objp->socid;
364 $companystatic->name = $objp->name;
365 $companystatic->client = $objp->client;
366 print
'<td>'.$companystatic->getNomUrl(1,
'margin').
'</td>';
369 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pv,
'MT')).
'</span></td>';
370 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pa,
'MT')).
'</span></td>';
371 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($marge,
'MT')).
'</span></td>';
372 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
373 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
375 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
376 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
381 $cumul_achat += $objp->buying_price;
382 $cumul_vente += $objp->selling_price;
388 $totalMargin = $cumul_vente - $cumul_achat;
396 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
397 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
400 print
'<tr class="liste_total">';
402 print
'<td colspan=2>';
406 print $langs->trans(
'TotalMargin').
"</td>";
407 print
'<td class="nowrap right">'.price(
price2num($cumul_vente,
'MT')).
'</td>';
408 print
'<td class="nowrap right">'.price(
price2num($cumul_achat,
'MT')).
'</td>';
409 print
'<td class="nowrap right">'.price(
price2num($totalMargin,
'MT')).
'</td>';
410 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
411 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
413 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
414 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
425 print
'<script type="text/javascript">
426 $(document).ready(function() {
427 console.log("Init some values");
428 $("#totalMargin").html("'.price(
price2num($totalMargin,
'MT')).
'");
429 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
430 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");