29require
'../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
31require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
32require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
44$langs->loadLangs(array(
'companies',
'bills',
'products',
'margins'));
48$action =
GETPOST(
'action',
'aZ09');
49$confirm =
GETPOST(
'confirm',
'alpha');
50$TSelectedCats =
GETPOST(
'categories',
'array');
57$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
58$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
60if (empty($page) || $page == -1) {
63$offset = $limit * $page;
68 $sortfield =
"f.datef";
76$startdate = $enddate =
'';
86$hookmanager->initHooks(array(
'marginproductlist'));
89$fieldvalue = (!empty($id) ?
$id : (!empty($ref) ? $ref :
''));
90$fieldtype = (!empty($ref) ?
'ref' :
'rowid');
91if (!empty($user->socid)) {
92 $socid = $user->socid;
94$result =
restrictedArea($user,
'produit|service', $fieldvalue,
'product&product',
'',
'', $fieldtype);
95if (!$user->hasRight(
'margins',
'liretous')) {
104$product_static =
new Product($db);
105$invoicestatic =
new Facture($db);
107$form =
new Form($db);
109llxHeader(
'', $langs->trans(
"Margins").
' - '.$langs->trans(
"Products"),
'',
'', 0, 0,
'',
'',
'',
'mod-margin page-productmargins');
111$text = $langs->trans(
"Margins");
117$titre = $langs->trans(
"Margins");
120print
'<form method="post" name="sel" action="'.$_SERVER[
'PHP_SELF'].
'">';
121print
'<input type="hidden" name="token" value="'.newToken().
'">';
125print
'<table class="border centpercent">';
128print
'<tr><td class="titlefield">'.$langs->trans(
'ProductOrService').
'</td>';
129print
'<td class="maxwidthonsmartphone" colspan="4">';
130print
img_picto(
'',
'product',
'class="pictofixedwidth"').$form->select_produits(($id > 0 ? $id :
''),
'id',
'', 20, 0, 1, 2,
'', 1, array(), 0, 1, 0,
'', 0,
'', null, 1);
134$TCats = $form->select_all_categories(
'product', 0,
'', 64, 0, 3);
137print
'<td class="titlefield">'.$langs->trans(
'Category').
'</td>';
138print
'<td class="maxwidthonsmartphone" colspan="4">';
139print
img_picto(
'',
'category',
'class="pictofixedwidth"').$form->multiselectarray(
'categories', $TCats, $TSelectedCats, 0, 0,
'quatrevingtpercent widthcentpercentminusx');
145print
'<td class="titlefield">'.$langs->trans(
'DateStart').
' ('.$langs->trans(
"DateValidation").
')</td>';
147print $form->selectDate($startdate,
'startdate', 0, 0, 1,
"sel", 1, 1);
149print
'<td>'.$langs->trans(
'DateEnd').
' ('.$langs->trans(
"DateValidation").
')</td>';
151print $form->selectDate($enddate,
'enddate', 0, 0, 1,
"sel", 1, 1);
153print
'<td style="text-align: center;">';
154print
'<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans(
'Refresh')).
'" />';
161print
'<table class="border centpercent">';
164print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="4">';
165print
'<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol(
$conf->currency).
'</span>';
170 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="4">';
171 print
'<span id="marginRate"></span>';
177 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="4">';
178 print
'<span id="markRate"></span>';
190$sql =
"SELECT p.label, p.rowid, p.fk_product_type, p.ref, p.entity as pentity,";
192 $sql .=
" d.fk_product,";
195 $sql .=
" f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut as statut,";
197$sql .=
" SUM(d.total_ht) as selling_price,";
198$sql .=
" SUM(d.qty) as product_qty,";
201$sql .=
" SUM(".$db->ifsql(
'(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))',
'-1 * d.qty * d.buy_price_ht * (d.situation_percent / 100)',
'd.qty * d.buy_price_ht * (d.situation_percent / 100)').
") as buying_price,";
202$sql .=
" SUM(".$db->ifsql(
'(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))',
'-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";
204$sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
205$sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
206$sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
207$sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = d.fk_product";
208if (!empty($TSelectedCats)) {
209 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_product as cp ON cp.fk_product=p.rowid';
211$sql .=
" WHERE f.fk_soc = s.rowid";
212$sql .=
' AND f.entity IN ('.getEntity(
'invoice').
')';
213$sql .=
" AND f.fk_statut NOT IN (".$db->sanitize(implode(
', ', $invoice_status_except_list)).
")";
214$sql .=
" AND d.fk_facture = f.rowid";
216 $sql .=
" AND d.fk_product =".((int) $id);
218if (!empty($TSelectedCats)) {
219 $sql .=
' AND cp.fk_categorie IN ('.$db->sanitize(implode(
',', $TSelectedCats)).
')';
221if (!empty($startdate)) {
222 $sql .=
" AND f.datef >= '".$db->idate($startdate).
"'";
224if (!empty($enddate)) {
225 $sql .=
" AND f.datef <= '".$db->idate($enddate).
"'";
227$sql .=
" AND d.buy_price_ht IS NOT NULL";
231 $sql .=
" AND d.buy_price_ht <> 0";
234 $sql .=
" GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity, d.fk_product, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut";
236 $sql .=
" GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity";
238$sql .= $db->order($sortfield, $sortorder);
242$param =
'&id='.((int) $id);
244 $param .=
'&startdateyear='.GETPOSTINT(
'startdateyear');
245 $param .=
'&startdatemonth='.GETPOSTINT(
'startdatemonth');
246 $param .=
'&startdateday='.GETPOSTINT(
'startdateday');
249 $param .=
'&enddateyear='.GETPOSTINT(
'enddateyear');
250 $param .=
'&enddatemonth='.GETPOSTINT(
'enddatemonth');
251 $param .=
'&enddateday='.GETPOSTINT(
'enddateday');
253$listofcateg =
GETPOST(
'categories',
'array:int');
254if (is_array($listofcateg)) {
255 foreach ($listofcateg as $val) {
256 $param .=
'&categories[]='.$val;
263dol_syslog(
'margin::productMargins.php', LOG_DEBUG);
264$result = $db->query($sql);
266 $num = $db->num_rows($result);
270 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"], $param, $sortfield, $sortorder,
'', $num, $num,
'', 0,
'',
'', 0, 1);
274 $labelcostprice =
'BuyingPrice';
276 $labelcostprice =
'CostPrice';
282 print
'<div class="div-table-responsive">';
283 print
'<table class="tagtable noborder nobottomiftotal liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
285 print
'<tr class="liste_titre">';
288 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'', $sortfield, $sortorder,
'center ');
290 print_liste_field_titre(
"ProductService", $_SERVER[
"PHP_SELF"],
"p.ref",
"", $param,
'', $sortfield, $sortorder);
292 print_liste_field_titre(
"Qty", $_SERVER[
"PHP_SELF"],
"product_qty",
"", $param,
'', $sortfield, $sortorder,
'center ');
293 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
294 print_liste_field_titre($labelcostprice, $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
295 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'', $sortfield, $sortorder,
'right ');
297 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
300 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
310 $objp = $db->fetch_object($result);
311 $qty = $objp->product_qty;
312 $pa = $objp->buying_price;
313 $pv = $objp->selling_price;
314 $marge = $objp->marge;
317 $marginRate = (100 * $marge / $pa);
320 $marginRate = -$marginRate;
327 $markRate = (100 * $marge / $pv);
330 $markRate = -$markRate;
336 print
'<tr class="oddeven">';
339 $invoicestatic->id = $objp->facid;
340 $invoicestatic->ref = $objp->ref;
341 print $invoicestatic->getNomUrl(1);
343 print
"<td class=\"center\">";
347 if ($objp->rowid > 0) {
348 $product_static->type = $objp->fk_product_type;
349 $product_static->id = $objp->rowid;
350 $product_static->ref = $objp->ref;
351 $product_static->label = $objp->label;
352 $product_static->entity = $objp->pentity;
353 $text = $product_static->getNomUrl(1);
354 print $text .=
' - '.$objp->label;
356 print
img_object(
'',
'product').
' '.$langs->trans(
"NotPredefinedProducts");
361 print
'<td class="center">'.$qty.
'</td>';
362 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pv,
'MT')).
'</span></td>';
363 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pa,
'MT')).
'</span></td>';
364 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($marge,
'MT')).
'</span></td>';
366 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
369 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
374 $cumul_achat += $objp->buying_price;
375 $cumul_vente += $objp->selling_price;
376 $cumul_qty += $objp->product_qty;
382 $totalMargin = $cumul_vente - $cumul_achat;
384 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
385 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
387 print
'<tr class="liste_total">';
389 print
'<td colspan=2>';
393 print $langs->trans(
'TotalMargin').
'</td>';
394 print
'<td class="center">'.$cumul_qty.
'</td>';
395 print
'<td class="nowrap right">'.price(
price2num($cumul_vente,
'MT')).
'</td>';
396 print
'<td class="nowrap right">'.price(
price2num($cumul_achat,
'MT')).
'</td>';
397 print
'<td class="nowrap right">'.price(
price2num($totalMargin,
'MT')).
'</td>';
399 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
402 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
415<script type="text/javascript">
416$(document).ready(function() {
417 console.log("Init some values");
418 $("#totalMargin").html("'.price(
price2num($totalMargin,
'MT')).
'");
419 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
420 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Class to manage invoices.
const STATUS_DRAFT
Draft status.
const STATUS_ABANDONED
Classified abandoned and no payment done.
Class to manage products or services.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
marges_prepare_head()
Return array of tabs to used on pages for third parties cards.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.