25require
'../../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
27require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
30$langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
34$action =
GETPOST(
'action',
'aZ09');
35$confirm =
GETPOST(
'confirm',
'alpha');
38$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref :
''));
39$fieldtype = (!empty($ref) ?
'ref' :
'rowid');
40if (!empty($user->socid)) {
41 $socid = $user->socid;
46$limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
47$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
48$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
49$page = GETPOSTISSET(
'pageplusone') ? (
GETPOST(
'pageplusone') - 1) :
GETPOST(
"page",
'int');
50if (empty($page) || $page == -1) {
53$offset = $limit * $page;
60 $sortfield =
"f.datef";
63$result =
restrictedArea($user,
'produit|service', $fieldvalue,
'product&product',
'',
'', $fieldtype);
65if (!$user->hasRight(
'margins',
'liretous')) {
74$invoicestatic =
new Facture($db);
80if ($id > 0 || !empty($ref)) {
81 $result = $object->fetch($id, $ref);
83 $title = $langs->trans(
'ProductServiceCard');
85 $shortlabel =
dol_trunc($object->label, 16);
87 $title = $langs->trans(
'Product').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
88 $help_url =
'EN:Module_Products|FR:Module_Produits|ES:Módulo_Productos';
91 $title = $langs->trans(
'Service').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
92 $help_url =
'EN:Module_Services_En|FR:Module_Services|ES:Módulo_Servicios';
100 $titre = $langs->trans(
"CardProduct".$object->type);
104 $linkback =
'<a href="'.DOL_URL_ROOT.
'/product/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
106 dol_banner_tab($object,
'ref', $linkback, ($user->socid ? 0 : 1),
'ref');
109 print
'<div class="fichecenter">';
111 print
'<div class="underbanner clearboth"></div>';
112 print
'<table class="border tableforfield centpercent">';
115 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td>';
116 print
'<span id="totalMargin" class="amount"></span>';
121 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td>';
122 print
'<span id="marginRate"></span>';
128 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td>';
129 print
'<span id="markRate"></span>';
136 print
'<div class="clearboth"></div>';
141 if ($user->hasRight(
"facture",
"read")) {
142 $sql =
"SELECT s.nom as name, s.rowid as socid, s.code_client,";
143 $sql .=
" f.rowid as facid, f.ref, f.total_ht,";
144 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
145 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
146 $sql .=
" sc.fk_soc, sc.fk_user,";
148 $sql .=
" sum(d.total_ht) as selling_price,";
149 $sql .=
" ".$db->ifsql(
'f.type = 2', -1, 1).
" * sum(d.qty) as qty,";
150 $sql .=
" ".$db->ifsql(
'f.type = 2', -1, 1).
" * sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
151 $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";
152 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
153 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
154 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
155 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
156 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
158 $sql .=
" WHERE f.fk_soc = s.rowid";
159 $sql .=
" AND f.fk_statut > 0";
160 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
161 $sql .=
" AND d.fk_facture = f.rowid";
162 $sql .=
" AND d.fk_product = ".((int) $object->id);
163 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
164 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
166 if (!empty($socid)) {
167 $sql .=
" AND f.fk_soc = ".((int) $socid);
169 $sql .=
" AND d.buy_price_ht IS NOT NULL";
173 $sql .=
" AND d.buy_price_ht <> 0";
175 $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";
176 if (!$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
177 $sql .=
", sc.fk_soc, sc.fk_user";
179 $sql .= $db->order($sortfield, $sortorder);
182 dol_syslog(
'margin:tabs:productMargins.php', LOG_DEBUG);
183 $result = $db->query($sql);
185 $num = $db->num_rows($result);
187 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"],
"&id=$object->id", $sortfield, $sortorder,
'', 0, 0,
'');
191 print
'<div class="div-table-responsive">';
192 print
'<table class="noborder centpercent">';
194 print
'<tr class="liste_titre">';
195 print_liste_field_titre(
"Invoice", $_SERVER[
"PHP_SELF"],
"f.ref",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
196 print_liste_field_titre(
"Company", $_SERVER[
"PHP_SELF"],
"s.nom",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
197 print_liste_field_titre(
"CustomerCode", $_SERVER[
"PHP_SELF"],
"s.code_client",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
198 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'left ');
199 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
200 print_liste_field_titre(
"BuyingPrice", $_SERVER[
"PHP_SELF"],
"buying_price",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
201 print_liste_field_titre(
"Qty", $_SERVER[
"PHP_SELF"],
"qty",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
202 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
204 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
207 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
209 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
218 $objp = $db->fetch_object($result);
220 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
221 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
223 print
'<tr class="oddeven">';
225 $invoicestatic->id = $objp->facid;
226 $invoicestatic->ref = $objp->ref;
227 print $invoicestatic->getNomUrl(1);
229 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>';
230 print
"<td>".$objp->code_client.
"</td>\n";
231 print
'<td class="center">';
233 print
'<td class="right amount">'.price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
234 print
'<td class="right amount">'.price(
price2num($objp->buying_price,
'MT')).
"</td>\n";
235 print
'<td class="right">'.price(
price2num($objp->qty,
'MT')).
"</td>\n";
236 print
'<td class="right amount">'.price(
price2num($objp->marge,
'MT')).
"</td>\n";
238 print
"<td class=\"right\">".(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
241 print
"<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
243 print
'<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
246 $cumul_achat += $objp->buying_price;
247 $cumul_vente += $objp->selling_price;
248 $cumul_qty += $objp->qty;
254 $totalMargin = $cumul_vente - $cumul_achat;
255 if ($totalMargin < 0) {
256 $marginRate = ($cumul_achat != 0) ? -1 * (100 * $totalMargin / $cumul_achat) :
'';
257 $markRate = ($cumul_vente != 0) ? -1 * (100 * $totalMargin / $cumul_vente) :
'';
259 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
260 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
262 print
'<tr class="liste_total">';
263 print
'<td colspan=4>'.$langs->trans(
'TotalMargin').
"</td>";
264 print
'<td class="right amount">'.price(
price2num($cumul_vente,
'MT')).
"</td>\n";
265 print
'<td class="right amount">'.price(
price2num($cumul_achat,
'MT')).
"</td>\n";
266 print
'<td class="right">'.price(
price2num($cumul_qty,
'MT')).
"</td>\n";
267 print
'<td class="right amount">'.price(
price2num($totalMargin,
'MT')).
"</td>\n";
269 print
'<td class="right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
272 print
"<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
274 print
'<td class="right"> </td>';
289 <script type="text/javascript">
290 $(document).ready(function() {
291 $("#totalMargin").html("'.
price(
price2num($totalMargin,
'MT')).
'");
292 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
293 $("#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.
Class to manage products or services.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
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_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).
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
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 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.