26 require
'../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
28 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
33 $langs->loadLangs(array(
'companies',
'bills',
'products',
'margins'));
36 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
37 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
38 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
40 if (empty($page) || $page == -1) {
43 $offset = $limit * $page;
44 $pageprev = $page - 1;
45 $pagenext = $page + 1;
53 $startdate = $enddate =
'';
54 if (
GETPOST(
'startdatemonth')) {
63 $hookmanager->initHooks(array(
'margincustomerlist'));
66 $socid =
GETPOST(
'socid',
'int');
67 $TSelectedProducts =
GETPOST(
'products',
'array');
68 $TSelectedCats =
GETPOST(
'categories',
'array');
70 if (!empty($user->socid)) {
71 $socid = $user->socid;
81 $companystatic =
new Societe($db);
82 $invoicestatic =
new Facture($db);
86 llxHeader(
'', $langs->trans(
"Margins").
' - '.$langs->trans(
"Clients"));
88 $text = $langs->trans(
"Margins");
94 $titre = $langs->trans(
"Margins");
98 print
'<form method="post" name="sel" action="'.$_SERVER[
'PHP_SELF'].
'">';
99 print
'<input type="hidden" name="token" value="'.newToken().
'">';
103 print
'<table class="border centpercent">';
111 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
112 print
'<td class="maxwidthonsmartphone" colspan="4">';
113 print
img_picto(
'',
'company').$form->select_company($socid,
'socid',
'(client=1 OR client=3)', 1, 0, 0);
121 $sortfield =
"f.datef";
125 print
'<tr><td class="titlefield">'.$langs->trans(
'ThirdPartyName').
'</td>';
126 print
'<td class="maxwidthonsmartphone" colspan="4">';
127 print
img_picto(
'',
'company').$form->select_company(
null,
'socid',
'(client=1 OR client=3)', 1, 0, 0);
131 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
132 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
138 $sortfield =
"f.datef";
141 $sortfield =
"s.nom";
147 $TRes =
$form->select_produits_list(
'',
'',
'',
'', 0,
'', 1, 2, 1, 0,
'', 1);
149 $TProducts = array();
150 foreach ($TRes as $prod) {
151 $TProducts[$prod[
'key']] = $prod[
'label'];
154 print
'<tr><td class="titlefield">'.$langs->trans(
'ProductOrService').
'</td>';
155 print
'<td class="maxwidthonsmartpone" colspan="4">';
156 print
img_picto(
'',
'product').$form->multiselectarray(
'products', $TProducts, $TSelectedProducts, 0, 0,
'minwidth500');
160 $TCats =
$form->select_all_categories(
'product', array(),
'', 64, 0, 1);
163 print
'<td class="titlefield">'.$langs->trans(
'Category').
'</td>';
164 print
'<td class="maxwidthonsmartphone" colspan="4">';
165 print
img_picto(
'',
'category').$form->multiselectarray(
'categories', $TCats, $TSelectedCats, 0, 0,
'quatrevingtpercent widthcentpercentminusx');
170 print
'<td>'.$langs->trans(
'DateStart').
' ('.$langs->trans(
"DateValidation").
')</td>';
172 print
$form->selectDate($startdate,
'startdate',
'',
'', 1,
"sel", 1, 1);
174 print
'<td>'.$langs->trans(
'DateEnd').
' ('.$langs->trans(
"DateValidation").
')</td>';
176 print
$form->selectDate($enddate,
'enddate',
'',
'', 1,
"sel", 1, 1);
178 print
'<td style="text-align: center;">';
179 print
'<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans(
'Refresh')).
'" />';
186 print
'<table class="border centpercent">';
189 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="4">';
190 print
'<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol($conf->currency).
'</span>';
194 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
195 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="4">';
196 print
'<span id="marginRate"></span>';
201 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
202 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="4">';
203 print
'<span id="markRate"></span>';
216 $sql .=
" s.rowid as socid, s.nom as name, s.code_client, s.client,";
218 $sql .=
" f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut as statut,";
220 $sql .=
" sum(d.total_ht) as selling_price,";
222 $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,";
223 $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";
224 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
225 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
226 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
227 if (!empty($TSelectedCats)) {
228 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'categorie_product as cp ON cp.fk_product=d.fk_product';
231 if (empty($user->rights->societe->client->voir) && !$socid) {
232 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
234 $sql .=
" WHERE f.fk_soc = s.rowid";
236 $sql .=
' AND s.rowid = '.((int) $socid);
238 if (empty($user->rights->societe->client->voir) && !$socid) {
239 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
241 $sql .=
" AND f.fk_statut NOT IN (".$db->sanitize(implode(
', ', $invoice_status_except_list)).
")";
242 $sql .=
' AND s.entity IN ('.getEntity(
'societe').
')';
243 $sql .=
' AND f.entity IN ('.getEntity(
'invoice').
')';
244 $sql .=
" AND d.fk_facture = f.rowid";
245 $sql .=
" AND (d.product_type = 0 OR d.product_type = 1)";
246 if (!empty($TSelectedProducts)) {
247 $sql .=
' AND d.fk_product IN ('.$db->sanitize(implode(
',', $TSelectedProducts)).
')';
249 if (!empty($TSelectedCats)) {
250 $sql .=
' AND cp.fk_categorie IN ('.$db->sanitize(implode(
',', $TSelectedCats)).
')';
252 if (!empty($startdate)) {
253 $sql .=
" AND f.datef >= '".$db->idate($startdate).
"'";
255 if (!empty($enddate)) {
256 $sql .=
" AND f.datef <= '".$db->idate($enddate).
"'";
258 $sql .=
" AND d.buy_price_ht IS NOT NULL";
261 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
262 $sql .=
" AND d.buy_price_ht <> 0";
265 $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";
267 $sql .=
" GROUP BY s.rowid, s.nom, s.code_client, s.client";
269 $sql .= $db->order($sortfield, $sortorder);
273 $param =
'&socid='.((int) $socid);
274 if (
GETPOST(
'startdatemonth',
'int')) {
275 $param .=
'&startdateyear='.GETPOST(
'startdateyear',
'int');
276 $param .=
'&startdatemonth='.GETPOST(
'startdatemonth',
'int');
277 $param .=
'&startdateday='.GETPOST(
'startdateday',
'int');
279 if (
GETPOST(
'enddatemonth',
'int')) {
280 $param .=
'&enddateyear='.GETPOST(
'enddateyear',
'int');
281 $param .=
'&enddatemonth='.GETPOST(
'enddatemonth',
'int');
282 $param .=
'&enddateday='.GETPOST(
'enddateday',
'int');
284 $listofproducts =
GETPOST(
'products',
'array:int');
285 if (is_array($listofproducts)) {
286 foreach ($listofproducts as $val) {
287 $param .=
'&products[]='.$val;
290 $listofcateg =
GETPOST(
'categories',
'array:int');
291 if (is_array($listofcateg)) {
292 foreach ($listofcateg as $val) {
293 $param .=
'&categories[]='.$val;
297 dol_syslog(
'margin::customerMargins.php', LOG_DEBUG);
298 $result = $db->query($sql);
300 $num = $db->num_rows($result);
303 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"],
"", $sortfield, $sortorder,
'', $num, $num,
'', 0,
'',
'', 0, 1);
305 if ($conf->global->MARGIN_TYPE ==
"1") {
306 $labelcostprice =
'BuyingPrice';
308 $labelcostprice =
'CostPrice';
314 print
'<div class="div-table-responsive">';
315 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
317 print
'<tr class="liste_titre">';
318 if (!empty($client)) {
320 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'align="center"', $sortfield, $sortorder);
324 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'align="right"', $sortfield, $sortorder);
325 print_liste_field_titre($labelcostprice, $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'align="right"', $sortfield, $sortorder);
326 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'align="right"', $sortfield, $sortorder);
327 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
328 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
330 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
331 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'align="right"', $sortfield, $sortorder);
340 $objp = $db->fetch_object($result);
342 $pa = $objp->buying_price;
343 $pv = $objp->selling_price;
344 $marge = $objp->marge;
347 $marginRate = ($pa != 0) ?-1 * (100 * $marge / $pa) :
'';
348 $markRate = ($pv != 0) ?-1 * (100 * $marge / $pv) :
'';
350 $marginRate = ($pa != 0) ? (100 * $marge / $pa) :
'';
351 $markRate = ($pv != 0) ? (100 * $marge / $pv) :
'';
354 print
'<tr class="oddeven">';
357 $invoicestatic->id = $objp->facid;
358 $invoicestatic->ref = $objp->ref;
359 print $invoicestatic->getNomUrl(1);
361 print
'<td class="center">';
364 $companystatic->id = $objp->socid;
365 $companystatic->name = $objp->name;
366 $companystatic->client = $objp->client;
367 print
'<td>'.$companystatic->getNomUrl(1,
'margin').
'</td>';
370 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pv,
'MT')).
'</span></td>';
371 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($pa,
'MT')).
'</span></td>';
372 print
'<td class="nowrap right"><span class="amount">'.price(
price2num($marge,
'MT')).
'</span></td>';
373 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
374 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
376 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
377 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
382 $cumul_achat += $objp->buying_price;
383 $cumul_vente += $objp->selling_price;
389 $totalMargin = $cumul_vente - $cumul_achat;
397 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
398 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
401 print
'<tr class="liste_total">';
403 print
'<td colspan=2>';
407 print $langs->trans(
'TotalMargin').
"</td>";
408 print
'<td class="nowrap right">'.price(
price2num($cumul_vente,
'MT')).
'</td>';
409 print
'<td class="nowrap right">'.price(
price2num($cumul_achat,
'MT')).
'</td>';
410 print
'<td class="nowrap right">'.price(
price2num($totalMargin,
'MT')).
'</td>';
411 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
412 print
'<td class="nowrap right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'</td>';
414 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
415 print
'<td class="nowrap right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'</td>';
426 print
'<script type="text/javascript">
427 $(document).ready(function() {
428 console.log("Init some values");
429 $("#totalMargin").html("'.price(
price2num($totalMargin,
'MT')).
'");
430 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
431 $("#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 third parties objects (customers, suppliers, prospects...)
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...
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.