27 require
'../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
34 $langs->loadLangs(array(
'companies',
'bills',
'products',
'margins'));
38 $action =
GETPOST(
'action',
'aZ09');
39 $confirm =
GETPOST(
'confirm',
'alpha');
40 $TSelectedCats =
GETPOST(
'categories',
'array');
46 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
47 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
48 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
50 if (empty($page) || $page == -1) {
53 $offset = $limit * $page;
54 $pageprev = $page - 1;
55 $pagenext = $page + 1;
58 $sortfield =
"f.datef";
66 $startdate = $enddate =
'';
67 if (
GETPOST(
'startdatemonth')) {
76 $hookmanager->initHooks(array(
'marginproductlist'));
79 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref :
''));
80 $fieldtype = (!empty($ref) ?
'ref' :
'rowid');
81 if (!empty($user->socid)) {
82 $socid = $user->socid;
84 $result =
restrictedArea($user,
'produit|service', $fieldvalue,
'product&product',
'',
'', $fieldtype);
85 if (empty($user->rights->margins->liretous)) {
94 $product_static =
new Product($db);
95 $invoicestatic =
new Facture($db);
99 llxHeader(
'', $langs->trans(
"Margins").
' - '.$langs->trans(
"Products"));
101 $text = $langs->trans(
"Margins");
107 $titre = $langs->trans(
"Margins");
110 print
'<form method="post" name="sel" action="'.$_SERVER[
'PHP_SELF'].
'">';
111 print
'<input type="hidden" name="token" value="'.newToken().
'">';
115 print
'<table class="border centpercent">';
118 print
'<tr><td class="titlefield">'.$langs->trans(
'ProductOrService').
'</td>';
119 print
'<td class="maxwidthonsmartphone" colspan="4">';
120 print
img_picto(
'',
'product').$form->select_produits(($id > 0 ? $id :
''),
'id',
'', 20, 0, 1, 2,
'', 1, array(), 0,
'All', 0,
'', 0,
'',
null, 1);
124 $TCats =
$form->select_all_categories(
'product', array(),
'', 64, 0, 1);
127 print
'<td class="titlefield">'.$langs->trans(
'Category').
'</td>';
128 print
'<td class="maxwidthonsmartphone" colspan="4">';
129 print
img_picto(
'',
'category').$form->multiselectarray(
'categories', $TCats, $TSelectedCats, 0, 0,
'quatrevingtpercent widthcentpercentminusx');
135 print
'<td class="titlefield">'.$langs->trans(
'DateStart').
' ('.$langs->trans(
"DateValidation").
')</td>';
137 print
$form->selectDate($startdate,
'startdate',
'',
'', 1,
"sel", 1, 1);
139 print
'<td>'.$langs->trans(
'DateEnd').
' ('.$langs->trans(
"DateValidation").
')</td>';
141 print
$form->selectDate($enddate,
'enddate',
'',
'', 1,
"sel", 1, 1);
143 print
'<td style="text-align: center;">';
144 print
'<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans(
'Refresh')).
'" />';
151 print
'<table class="border centpercent">';
154 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="4">';
155 print
'<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol($conf->currency).
'</span>';
159 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
160 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="4">';
161 print
'<span id="marginRate"></span>';
166 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
167 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="4">';
168 print
'<span id="markRate"></span>';
180 $sql =
"SELECT p.label, p.rowid, p.fk_product_type, p.ref, p.entity as pentity,";
182 $sql .=
" d.fk_product,";
185 $sql .=
" f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut as statut,";
187 $sql .=
" SUM(d.total_ht) as selling_price,";
189 $sql .=
" SUM(d.qty) as product_qty,";
190 $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,";
191 $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";
192 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
193 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
194 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
195 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = d.fk_product";
196 if (!empty($TSelectedCats)) {
197 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_product as cp ON cp.fk_product=p.rowid';
199 $sql .=
" WHERE f.fk_soc = s.rowid";
200 $sql .=
' AND f.entity IN ('.getEntity(
'invoice').
')';
201 $sql .=
" AND f.fk_statut NOT IN (".$db->sanitize(implode(
', ', $invoice_status_except_list)).
")";
202 $sql .=
" AND d.fk_facture = f.rowid";
204 $sql .=
" AND d.fk_product =".((int) $id);
206 if (!empty($TSelectedCats)) {
207 $sql .=
' AND cp.fk_categorie IN ('.$db->sanitize(implode(
',', $TSelectedCats)).
')';
209 if (!empty($startdate)) {
210 $sql .=
" AND f.datef >= '".$db->idate($startdate).
"'";
212 if (!empty($enddate)) {
213 $sql .=
" AND f.datef <= '".$db->idate($enddate).
"'";
215 $sql .=
" AND d.buy_price_ht IS NOT NULL";
218 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
219 $sql .=
" AND d.buy_price_ht <> 0";
222 $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";
224 $sql .=
" GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity";
226 $sql .= $db->order($sortfield, $sortorder);
230 $param =
'&id='.((int) $id);
231 if (
GETPOST(
'startdatemonth',
'int')) {
232 $param .=
'&startdateyear='.GETPOST(
'startdateyear',
'int');
233 $param .=
'&startdatemonth='.GETPOST(
'startdatemonth',
'int');
234 $param .=
'&startdateday='.GETPOST(
'startdateday',
'int');
236 if (
GETPOST(
'enddatemonth',
'int')) {
237 $param .=
'&enddateyear='.GETPOST(
'enddateyear',
'int');
238 $param .=
'&enddatemonth='.GETPOST(
'enddatemonth',
'int');
239 $param .=
'&enddateday='.GETPOST(
'enddateday',
'int');
241 $listofcateg =
GETPOST(
'categories',
'array:int');
242 if (is_array($listofcateg)) {
243 foreach ($listofcateg as $val) {
244 $param .=
'&categories[]='.$val;
248 dol_syslog(
'margin::productMargins.php', LOG_DEBUG);
249 $result = $db->query($sql);
251 $num = $db->num_rows($result);
254 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"], $param, $sortfield, $sortorder,
'', $num, $num,
'', 0,
'',
'', 0, 1);
257 if ($conf->global->MARGIN_TYPE ==
"1") {
258 $labelcostprice =
'BuyingPrice';
260 $labelcostprice =
'CostPrice';
266 print
'<div class="div-table-responsive">';
267 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
269 print
'<tr class="liste_titre">';
272 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'', $sortfield, $sortorder,
'center ');
274 print_liste_field_titre(
"ProductService", $_SERVER[
"PHP_SELF"],
"p.ref",
"", $param,
'', $sortfield, $sortorder);
276 print_liste_field_titre(
"Qty", $_SERVER[
"PHP_SELF"],
"product_qty",
"", $param,
'', $sortfield, $sortorder,
'center ');
277 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
278 print_liste_field_titre($labelcostprice, $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
279 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'', $sortfield, $sortorder,
'right ');
280 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
281 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
283 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
284 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
294 $objp = $db->fetch_object($result);
295 $qty = $objp->product_qty;
296 $pa = $objp->buying_price;
297 $pv = $objp->selling_price;
298 $marge = $objp->marge;
301 $marginRate = ($pa != 0) ?-1 * (100 * $marge / $pa) :
'';
302 $markRate = ($pv != 0) ?-1 * (100 * $marge / $pv) :
'';
304 $marginRate = ($pa != 0) ? (100 * $marge / $pa) :
'';
305 $markRate = ($pv != 0) ? (100 * $marge / $pv) :
'';
308 print
'<tr class="oddeven">';
311 $invoicestatic->id = $objp->facid;
312 $invoicestatic->ref = $objp->ref;
313 print $invoicestatic->getNomUrl(1);
315 print
"<td class=\"center\">";
319 if ($objp->rowid > 0) {
320 $product_static->type = $objp->fk_product_type;
321 $product_static->id = $objp->rowid;
322 $product_static->ref = $objp->ref;
323 $product_static->label = $objp->label;
324 $product_static->entity = $objp->pentity;
325 $text = $product_static->getNomUrl(1);
326 print $text .=
' - '.$objp->label;
328 print
img_object(
'',
'product').
' '.$langs->trans(
"NotPredefinedProducts");
333 print
'<td class="center">'.$qty.
'</td>';
334 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pv,
'MT')).
'</span></td>';
335 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pa,
'MT')).
'</span></td>';
336 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($marge,
'MT')).
'</span></td>';
337 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
338 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
340 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
341 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
346 $cumul_achat += $objp->buying_price;
347 $cumul_vente += $objp->selling_price;
348 $cumul_qty += $objp->product_qty;
354 $totalMargin = $cumul_vente - $cumul_achat;
356 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
357 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
359 print
'<tr class="liste_total">';
361 print
'<td colspan=2>';
365 print $langs->trans(
'TotalMargin').
'</td>';
366 print
'<td class="center">'.$cumul_qty.
'</td>';
367 print
'<td class="nowrap right">'.price(
price2num($cumul_vente,
'MT')).
'</td>';
368 print
'<td class="nowrap right">'.price(
price2num($cumul_achat,
'MT')).
'</td>';
369 print
'<td class="nowrap right">'.price(
price2num($totalMargin,
'MT')).
'</td>';
370 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
371 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
373 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
374 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
387 <script type="text/javascript">
388 $(document).ready(function() {
389 console.log("Init some values");
390 $("#totalMargin").html("'.price(
price2num($totalMargin,
'MT')).
'");
391 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
392 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
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.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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 from a POST of a form.
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.
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.