27require
'../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
29require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
40$langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
44$action =
GETPOST(
'action',
'aZ09');
45$confirm =
GETPOST(
'confirm',
'alpha');
48$fieldvalue = (!empty($id) ?
$id : (!empty($ref) ? $ref :
''));
49$fieldtype = (!empty($ref) ?
'ref' :
'rowid');
50if (!empty($user->socid)) {
51 $socid = $user->socid;
57$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
58$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
60if (empty($page) || $page < 0 ||
GETPOST(
'button_search',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha')) {
64$offset = $limit * $page;
71 $sortfield =
"f.datef";
74$hookmanager->initHooks(array(
'tabproductmarginlist'));
76$result =
restrictedArea($user,
'produit|service', $fieldvalue,
'product&product',
'',
'', $fieldtype);
78if (!$user->hasRight(
'margins',
'liretous')) {
82$search_invoice_date_start =
'';
83$search_invoice_date_end =
'';
84if (
GETPOSTINT(
'search_invoice_date_start_month')) {
87if (
GETPOSTINT(
'search_invoice_date_end_month')) {
92if (
GETPOST(
'button_removefilter_x',
'alpha') ||
GETPOST(
'button_removefilter.x',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha')) {
93 $search_invoice_date_start =
'';
94 $search_invoice_date_end =
'';
98if (empty($search_invoice_date_start) && empty($search_invoice_date_end) && !GETPOSTISSET(
'restore_lastsearch_values')) {
99 $query =
"SELECT date_start, date_end";
100 $query .=
" FROM ".MAIN_DB_PREFIX.
"accounting_fiscalyear";
101 $query .=
" WHERE date_start < '".$db->idate(
dol_now()).
"' and date_end > '".$db->idate(
dol_now()).
"' limit 1";
102 $res = $db->query($query);
104 if ($res && $db->num_rows($res) > 0) {
105 $fiscalYear = $db->fetch_object($res);
106 $search_invoice_date_start = strtotime($fiscalYear->date_start);
107 $search_invoice_date_end = strtotime($fiscalYear->date_end);
114 $year_end = $year_start + 1;
115 $month_end = $month_start - 1;
116 if ($month_end < 1) {
120 $search_invoice_date_start =
dol_mktime(0, 0, 0, $month_start, 1, $year_start);
130$invoicestatic =
new Facture($db);
132$form =
new Form($db);
136if ($id > 0 || !empty($ref)) {
137 $result =
$object->fetch($id, $ref);
139 $title = $langs->trans(
'ProductServiceCard');
143 $title = $langs->trans(
'Product').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
144 $help_url =
'EN:Module_Products|FR:Module_Produits|ES:Módulo_Productos';
147 $title = $langs->trans(
'Service').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
148 $help_url =
'EN:Module_Services_En|FR:Module_Services|ES:Módulo_Servicios';
151 llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-margin page-tabs_productmargins');
153 $param =
"&id=".$object->id;
154 if ($limit > 0 && $limit != $conf->liste_limit) {
155 $param .=
'&limit='.((int) $limit);
157 if ($search_invoice_date_start) {
158 $param .=
'&search_invoice_date_start_day='.dol_print_date($search_invoice_date_start,
'%d').
'&search_invoice_date_start_month='.
dol_print_date($search_invoice_date_start,
'%m').
'&search_invoice_date_start_year='.
dol_print_date($search_invoice_date_start,
'%Y');
160 if ($search_invoice_date_end) {
161 $param .=
'&search_invoice_date_end_day='.dol_print_date($search_invoice_date_end,
'%d').
'&search_invoice_date_end_month='.
dol_print_date($search_invoice_date_end,
'%m').
'&search_invoice_date_end_year='.
dol_print_date($search_invoice_date_end,
'%Y');
167 $titre = $langs->trans(
"CardProduct".
$object->type);
171 $linkback =
'<a href="'.DOL_URL_ROOT.
'/product/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
172 $object->next_prev_filter =
"(te.fk_product_type:=:".((int)
$object->type).
")";
174 dol_banner_tab($object,
'ref', $linkback, ($user->socid ? 0 : 1),
'ref');
177 print
'<div class="fichecenter">';
179 print
'<div class="underbanner clearboth"></div>';
180 print
'<table class="border tableforfield centpercent">';
183 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td>';
184 print
'<span id="totalMargin" class="amount"></span>';
189 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td>';
190 print
'<span id="marginRate"></span>';
196 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td>';
197 print
'<span id="markRate"></span>';
204 print
'<div class="clearboth"></div>';
209 if ($user->hasRight(
"facture",
"read")) {
210 $sql =
"SELECT s.nom as name, s.rowid as socid, s.code_client,";
211 $sql .=
" f.rowid as facid, f.ref, f.total_ht,";
212 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
213 if (!$user->hasRight(
'societe',
'client',
'voir')) {
214 $sql .=
" sc.fk_soc, sc.fk_user,";
216 $sql .=
" sum(d.total_ht) as selling_price,";
217 $sql .=
" ".$db->ifsql(
'f.type = 2',
'-1',
'1').
" * sum(d.qty) as qty,";
218 $sql .=
" ".$db->ifsql(
'f.type = 2',
'-1',
'1').
" * sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
219 $sql .=
" ".$db->ifsql(
'f.type = 2',
'-1',
'1').
" * sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge";
220 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
221 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
222 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
223 if (!$user->hasRight(
'societe',
'client',
'voir')) {
224 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
226 $sql .=
" WHERE f.fk_soc = s.rowid";
227 $sql .=
" AND f.fk_statut > 0";
228 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
229 $sql .=
" AND d.fk_facture = f.rowid";
230 $sql .=
" AND d.fk_product = ".((int)
$object->id);
231 if (!$user->hasRight(
'societe',
'client',
'voir')) {
232 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
234 if (!empty($socid)) {
235 $sql .=
" AND f.fk_soc = ".((int) $socid);
237 $sql .=
" AND d.buy_price_ht IS NOT NULL";
241 $sql .=
" AND d.buy_price_ht <> 0";
243 if (!empty($search_invoice_date_start)) {
244 $sql .=
" AND f.datef >= '".$db->idate($search_invoice_date_start).
"'";
246 if (!empty($search_invoice_date_end)) {
247 $sql .=
" AND f.datef <= '".$db->idate($search_invoice_date_end).
"'";
249 $sql .=
" GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut, f.type";
250 if (!$user->hasRight(
'societe',
'client',
'voir')) {
251 $sql .=
", sc.fk_soc, sc.fk_user";
256 $sql .= $db->order($sortfield, $sortorder);
258 $sql .= $db->plimit($limit + 1, $offset);
260 dol_syslog(
'margin:tabs:productMargins.php', LOG_DEBUG);
261 $result = $db->query($sql);
263 $num = $db->num_rows($result);
265 print
'<form method="post" action="'.$_SERVER [
'PHP_SELF'].
'?id='.
$id.
'" name="search_form">'.
"\n";
266 print
'<input type="hidden" name="token" value="'.newToken().
'">';
267 if (!empty($sortfield)) {
268 print
'<input type="hidden" name="sortfield" value="'.$sortfield.
'"/>';
270 if (!empty($sortorder)) {
271 print
'<input type="hidden" name="sortorder" value="'.$sortorder.
'"/>';
274 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"], $param, $sortfield, $sortorder,
'', $num,
'',
'');
278 $parameters = array();
279 $reshook = $hookmanager->executeHooks(
'printFieldPreListTitle', $parameters, $object, $action);
280 if (empty($reshook)) {
281 $moreforfilter .= $hookmanager->resPrint;
283 $moreforfilter = $hookmanager->resPrint;
286 if (!empty($moreforfilter)) {
287 print
'<div class="liste_titre liste_titre_bydiv centpercent">';
288 print $moreforfilter;
292 $selectedfields =
'';
296 print
'<div class="div-table-responsive">';
297 print
'<table class="noborder centpercent">';
301 print
'<tr class="liste_titre liste_titre_filter">';
304 print
'<th class="liste_titre center maxwidthsearch">';
305 $searchpicto = $form->showFilterButtons(
'left');
311 print
'<th class="liste_titre">';
315 print
'<th class="liste_titre">';
319 print
'<th class="liste_titre">';
323 print
'<th class="liste_titre center">';
324 print
'<div class="nowrapfordate">';
325 print $form->selectDate($search_invoice_date_start ?: -1,
'search_invoice_date_start_', 0, 0, 1,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'', $langs->trans(
'From'));
327 print
'<div class="nowrapfordate">';
328 print $form->selectDate($search_invoice_date_end ?: -1,
'search_invoice_date_end_', 0, 0, 1,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'', $langs->trans(
'to'));
333 print
'<th class="liste_titre">';
337 print
'<th class="liste_titre">';
341 print
'<th class="liste_titre">';
345 print
'<th class="liste_titre">';
350 print
'<th class="liste_titre">';
356 print
'<th class="liste_titre">';
361 print
'<th class="liste_titre">';
366 print
'<th class="liste_titre center maxwidthsearch">';
367 $searchpicto = $form->showFilterButtons();
374 print
'<tr class="liste_titre">';
377 print_liste_field_titre($selectedfields, $_SERVER[
"PHP_SELF"],
"",
'', $param,
'', $sortfield, $sortorder,
'maxwidthsearch center ');
381 print_liste_field_titre(
"CustomerCode", $_SERVER[
"PHP_SELF"],
"s.code_client",
"", $param,
'', $sortfield, $sortorder);
382 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'', $sortfield, $sortorder,
'center ');
383 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
384 print_liste_field_titre(
"BuyingPrice", $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
386 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'', $sortfield, $sortorder,
'right ');
388 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
391 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
393 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"", $param,
'', $sortfield, $sortorder,
'right ');
396 print_liste_field_titre($selectedfields, $_SERVER[
"PHP_SELF"],
"",
'', $param,
'', $sortfield, $sortorder,
'maxwidthsearch center ');
405 $imaxinloop = ($limit ? min($num, $limit) : $num);
406 while ($i < $imaxinloop) {
407 $objp = $db->fetch_object($result);
409 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
410 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
412 print
'<tr class="oddeven">';
415 print
'<td class="nowrap center">';
420 $invoicestatic->id = $objp->facid;
421 $invoicestatic->ref = $objp->ref;
422 print $invoicestatic->getNomUrl(1);
424 print
'<td><a href="'.DOL_URL_ROOT.
'/comm/card.php?socid='.$objp->socid.
'">'.
img_object($langs->trans(
"ShowCompany"),
"company").
' '.
dol_trunc($objp->name, 44).
'</a></td>';
425 print
"<td>".$objp->code_client.
"</td>\n";
426 print
'<td class="center">';
428 print
'<td class="right amount">'.price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
429 print
'<td class="right amount">'.price(
price2num($objp->buying_price,
'MT')).
"</td>\n";
430 print
'<td class="right">'.price(
price2num($objp->qty,
'MT')).
"</td>\n";
431 print
'<td class="right amount">'.price(
price2num($objp->marge,
'MT')).
"</td>\n";
433 print
"<td class=\"right\">".(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
436 print
"<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
438 print
'<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
442 print
'<td class="nowrap center">';
447 $cumul_achat += $objp->buying_price;
448 $cumul_vente += $objp->selling_price;
449 $cumul_qty += $objp->qty;
455 $totalMargin = $cumul_vente - $cumul_achat;
456 if ($totalMargin < 0) {
457 $marginRate = ($cumul_achat != 0) ? -1 * (100 * $totalMargin / $cumul_achat) :
'';
458 $markRate = ($cumul_vente != 0) ? -1 * (100 * $totalMargin / $cumul_vente) :
'';
460 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
461 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
463 print
'<tr class="liste_total">';
468 print
'<td colspan="'.$colspan.
'">'.$langs->trans(
'TotalMargin').
"</td>";
469 print
'<td class="right amount">'.price(
price2num($cumul_vente,
'MT')).
"</td>\n";
470 print
'<td class="right amount">'.price(
price2num($cumul_achat,
'MT')).
"</td>\n";
471 print
'<td class="right">'.price(
price2num($cumul_qty,
'MT')).
"</td>\n";
472 print
'<td class="right amount">'.price(
price2num($totalMargin,
'MT')).
"</td>\n";
474 print
'<td class="right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
477 print
'<td class="right">'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
479 print
'<td class="right"> </td>';
482 print
'<td class="center">';
500 <script type="text/javascript">
501 $(document).ready(function() {
503 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
504 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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.
Class to manage products or services.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
dol_now($mode='gmt')
Return date for now.
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.
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.
getDolCurrency()
Return the main currency ('EUR', 'USD', ...)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
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.
product_prepare_head($object)
Prepare array with list of tabs.
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.