28require
'../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
31require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
32require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
43$langs->loadLangs(array(
'companies',
'bills',
'products',
'margins'));
47$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
48$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
50if (empty($page) || $page == -1) {
53$offset = $limit * $page;
63$startdate = $enddate =
'';
73$hookmanager->initHooks(array(
'margincustomerlist'));
77$TSelectedProducts =
GETPOST(
'products',
'array');
78$TSelectedCats =
GETPOST(
'categories',
'array');
80if (!empty($user->socid)) {
81 $socid = $user->socid;
91$companystatic =
new Societe($db);
92$invoicestatic =
new Facture($db);
96llxHeader(
'', $langs->trans(
"Margins").
' - '.$langs->trans(
"Clients"),
'',
'', 0, 0,
'',
'',
'',
'mod-margin page-customermargins');
98$text = $langs->trans(
"Margins");
104$titre = $langs->trans(
"Margins");
108print
'<form method="post" name="sel" action="'.$_SERVER[
'PHP_SELF'].
'">';
109print
'<input type="hidden" name="token" value="'.newToken().
'">';
113print
'<table class="border centpercent">';
121 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
122 print
'<td class="maxwidthonsmartphone" colspan="4">';
123 $filter =
'(client:IN:1,3)';
124 print
img_picto(
'',
'company').$form->select_company($socid,
'socid', $filter, 1, 0, 0);
132 $sortfield =
"f.datef";
136 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
137 print
'<td class="maxwidthonsmartphone" colspan="4">';
138 print
img_picto(
'',
'company').$form->select_company(
null,
'socid',
'((client:=:1) OR (client:=:3))', 1, 0, 0);
142$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
143$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
149 $sortfield =
"f.datef";
152 $sortfield =
"s.nom";
158$TRes = $form->select_produits_list(
'',
'',
'',
'', 0,
'', 1, 2, 1, 0,
'', 1);
161foreach ($TRes as $prod) {
162 $TProducts[$prod[
'key']] = $prod[
'label'];
165print
'<tr><td class="titlefield">'.$langs->trans(
'ProductOrService').
'</td>';
166print
'<td class="maxwidthonsmartphone" colspan="4">';
167print
img_picto(
'',
'product').$form->multiselectarray(
'products', $TProducts, $TSelectedProducts, 0, 0,
'minwidth500');
171$TCats = $form->select_all_categories(
'product', array(),
'', 64, 0, 3);
174print
'<td class="titlefield">'.$langs->trans(
'Category').
'</td>';
175print
'<td class="maxwidthonsmartphone" colspan="4">';
176print
img_picto(
'',
'category',
'class="pictofixedwidth"').$form->multiselectarray(
'categories', $TCats, $TSelectedCats, 0, 0,
'quatrevingtpercent widthcentpercentminusx');
181print
'<td>'.$langs->trans(
'DateStart').
' ('.$langs->trans(
"DateValidation").
')</td>';
183print $form->selectDate($startdate,
'startdate', 0, 0, 1,
"sel", 1, 1);
185print
'<td>'.$langs->trans(
'DateEnd').
' ('.$langs->trans(
"DateValidation").
')</td>';
187print $form->selectDate($enddate,
'enddate', 0, 0, 1,
"sel", 1, 1);
189print
'<td style="text-align: center;">';
190print
'<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans(
'Refresh')).
'" />';
197print
'<table class="border centpercent">';
200print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="4">';
201print
'<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol(
$conf->currency).
'</span>';
206 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="4">';
207 print
'<span id="marginRate"></span>';
213 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="4">';
214 print
'<span id="markRate"></span>';
227$sql .=
" s.rowid as socid, s.nom as name, s.code_client, s.client,";
229 $sql .=
" f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.type, f.fk_statut as statut,";
231$sql .=
" sum(d.total_ht) as selling_price,";
234$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,";
235$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";
237$sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
238$sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
239$sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
240if (!empty($TSelectedCats)) {
241 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_product as cp ON cp.fk_product=d.fk_product';
244if (!$user->hasRight(
'societe',
'client',
'voir')) {
245 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
247$sql .=
" WHERE f.fk_soc = s.rowid";
249 $sql .=
' AND s.rowid = '.((int) $socid);
251if (!$user->hasRight(
'societe',
'client',
'voir')) {
252 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
254$sql .=
" AND f.fk_statut NOT IN (".$db->sanitize(implode(
', ', $invoice_status_except_list)).
")";
255$sql .=
' AND s.entity IN ('.getEntity(
'societe').
')';
256$sql .=
' AND f.entity IN ('.getEntity(
'invoice').
')';
257$sql .=
" AND d.fk_facture = f.rowid";
258$sql .=
" AND (d.product_type = 0 OR d.product_type = 1)";
259if (!empty($TSelectedProducts)) {
260 $sql .=
' AND d.fk_product IN ('.$db->sanitize(implode(
',', $TSelectedProducts)).
')';
262if (!empty($TSelectedCats)) {
263 $sql .=
' AND cp.fk_categorie IN ('.$db->sanitize(implode(
',', $TSelectedCats)).
')';
265if (!empty($startdate)) {
266 $sql .=
" AND f.datef >= '".$db->idate($startdate).
"'";
268if (!empty($enddate)) {
269 $sql .=
" AND f.datef <= '".$db->idate($enddate).
"'";
271$sql .=
" AND d.buy_price_ht IS NOT NULL";
275 $sql .=
" AND d.buy_price_ht <> 0";
278 $sql .=
" GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.type, f.fk_statut";
280 $sql .=
" GROUP BY s.rowid, s.nom, s.code_client, s.client";
282$sql .= $db->order($sortfield, $sortorder);
286$param =
'&socid='.((int) $socid);
288 $param .=
'&startdateyear='.GETPOSTINT(
'startdateyear');
289 $param .=
'&startdatemonth='.GETPOSTINT(
'startdatemonth');
290 $param .=
'&startdateday='.GETPOSTINT(
'startdateday');
293 $param .=
'&enddateyear='.GETPOSTINT(
'enddateyear');
294 $param .=
'&enddatemonth='.GETPOSTINT(
'enddatemonth');
295 $param .=
'&enddateday='.GETPOSTINT(
'enddateday');
297$listofproducts =
GETPOST(
'products',
'array:int');
298if (is_array($listofproducts)) {
299 foreach ($listofproducts as $val) {
300 $param .=
'&products[]='.$val;
303$listofcateg =
GETPOST(
'categories',
'array:int');
304if (is_array($listofcateg)) {
305 foreach ($listofcateg as $val) {
306 $param .=
'&categories[]='.$val;
313dol_syslog(
'margin::customerMargins.php', LOG_DEBUG);
314$result = $db->query($sql);
316 $num = $db->num_rows($result);
320 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"],
"", $sortfield, $sortorder,
'', $num, $num,
'', 0,
'',
'', 0, 1);
323 $labelcostprice =
'BuyingPrice';
325 $labelcostprice =
'CostPrice';
331 print
'<div class="div-table-responsive">';
332 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
334 print
'<tr class="liste_titre">';
335 if (!empty($client)) {
337 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'align="center"', $sortfield, $sortorder);
341 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'align="right"', $sortfield, $sortorder);
342 print_liste_field_titre($labelcostprice, $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'align="right"', $sortfield, $sortorder);
343 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'align="right"', $sortfield, $sortorder);
345 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
348 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
357 $objp = $db->fetch_object($result);
359 $pa = $objp->buying_price;
360 $pv = $objp->selling_price;
361 $marge = $objp->marge;
364 $marginRate = ($pa != 0) ? -1 * (100 * $marge / $pa) :
'';
365 $markRate = ($pv != 0) ? -1 * (100 * $marge / $pv) :
'';
367 $marginRate = ($pa != 0) ? (100 * $marge / $pa) :
'';
368 $markRate = ($pv != 0) ? (100 * $marge / $pv) :
'';
371 print
'<tr class="oddeven">';
373 $invoicestatic->id = $objp->facid;
374 $invoicestatic->ref = $objp->ref;
375 $invoicestatic->statut = $objp->statut;
376 $invoicestatic->type = $objp->type;
379 print $invoicestatic->getNomUrl(1);
381 print
'<td class="center">';
384 $companystatic->id = $objp->socid;
385 $companystatic->name = $objp->name;
386 $companystatic->client = $objp->client;
388 print
'<td>'.$companystatic->getNomUrl(1,
'margin').
'</td>';
391 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pv,
'MT')).
'</span></td>';
392 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pa,
'MT')).
'</span></td>';
393 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($marge,
'MT')).
'</span></td>';
395 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
398 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
403 $cumul_achat += $objp->buying_price;
404 $cumul_vente += $objp->selling_price;
410 $totalMargin = $cumul_vente - $cumul_achat;
418 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
419 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
422 print
'<tr class="liste_total">';
424 print
'<td colspan="2">';
428 print $langs->trans(
'TotalMargin').
"</td>";
429 print
'<td class="nowrap right">'.price(
price2num($cumul_vente,
'MT')).
'</td>';
430 print
'<td class="nowrap right">'.price(
price2num($cumul_achat,
'MT')).
'</td>';
431 print
'<td class="nowrap right">'.price(
price2num($totalMargin,
'MT')).
'</td>';
433 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
436 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
447print
'<script type="text/javascript">
448$(document).ready(function() {
449 console.log("Init some values");
450 $("#totalMargin").html("'.price(
price2num($totalMargin,
'MT')).
'");
451 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
452 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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 third parties objects (customers, suppliers, prospects...)
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_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)
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)
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.
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).
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.
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.