24 require
'../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
26 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
29 $langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
33 $action =
GETPOST(
'action',
'aZ09');
34 $confirm =
GETPOST(
'confirm',
'alpha');
37 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref :
''));
38 $fieldtype = (!empty($ref) ?
'ref' :
'rowid');
39 if (!empty($user->socid)) $socid = $user->socid;
40 $result =
restrictedArea($user,
'produit|service', $fieldvalue,
'product&product',
'',
'', $fieldtype);
45 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
46 $sortfield =
GETPOST(
"sortfield",
'alpha');
47 $sortorder =
GETPOST(
"sortorder",
'alpha');
49 if (empty($page) || $page == -1) { $page = 0; }
50 $offset = $limit * $page;
51 $pageprev = $page - 1;
52 $pagenext = $page + 1;
53 if (!$sortorder) $sortorder =
"DESC";
54 if (!$sortfield) $sortfield =
"f.datef";
61 $invoicestatic =
new Facture($db);
63 $form =
new Form($db);
65 if ($id > 0 || !empty($ref))
67 $result = $object->fetch($id, $ref);
69 $title = $langs->trans(
'ProductServiceCard');
71 $shortlabel =
dol_trunc($object->label, 16);
74 $title = $langs->trans(
'Product').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
75 $helpurl =
'EN:Module_Products|FR:Module_Produits|ES:Módulo_Productos';
79 $title = $langs->trans(
'Service').
" ".$shortlabel.
" - ".$langs->trans(
'Card');
80 $helpurl =
'EN:Module_Services_En|FR:Module_Services|ES:Módulo_Servicios';
91 $titre = $langs->trans(
"CardProduct".$object->type);
95 $linkback =
'<a href="'.DOL_URL_ROOT.
'/product/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
97 dol_banner_tab($object,
'ref', $linkback, ($user->socid ? 0 : 1),
'ref');
100 print '<div class="fichecenter">';
102 print '<div class="underbanner clearboth"></div>';
103 print '<table class="border tableforfield" width="100%">';
106 print '<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="3">';
107 print '<span id="totalMargin"></span>';
111 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
112 print '<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="3">';
113 print '<span id="marginRate"></span>';
118 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
119 print '<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="3">';
120 print '<span id="markRate"></span>';
127 print '<div style="clear:both"></div>';
132 if ($user->rights->facture->lire) {
133 $sql =
"SELECT s.nom as name, s.rowid as socid, s.code_client,";
134 $sql .=
" f.rowid as facid, f.ref, f.total as total_ht,";
135 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
136 if (!$user->rights->societe->client->voir && !$socid) $sql .=
" sc.fk_soc, sc.fk_user,";
137 $sql .=
" sum(d.total_ht) as selling_price,";
138 $sql .=
" ".$db->ifsql(
'f.type = 2', -1, 1).
" * sum(d.qty) as qty,";
139 $sql .=
" ".$db->ifsql(
'f.type = 2', -1, 1).
" * sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
140 $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";
141 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
142 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
143 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
144 if (!$user->rights->societe->client->voir && !$socid) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
145 $sql .=
" WHERE f.fk_soc = s.rowid";
146 $sql .=
" AND f.fk_statut > 0";
147 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
148 $sql .=
" AND d.fk_facture = f.rowid";
149 $sql .=
" AND d.fk_product =".$object->id;
150 if (!$user->rights->societe->client->voir && !$socid) $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id;
151 if (!empty($socid)) $sql .=
" AND f.fk_soc = $socid";
152 $sql .=
" AND d.buy_price_ht IS NOT NULL";
153 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .=
" AND d.buy_price_ht <> 0";
154 $sql .=
" GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total, f.datef, f.paye, f.fk_statut, f.type";
155 if (!$user->rights->societe->client->voir && !$socid) $sql .=
", sc.fk_soc, sc.fk_user";
156 $sql .= $db->order($sortfield, $sortorder);
159 dol_syslog(
'margin:tabs:productMargins.php', LOG_DEBUG);
160 $result = $db->query($sql);
162 $num = $db->num_rows($result);
164 print_barre_liste($langs->trans(
"MarginDetails"), $page,
$_SERVER[
"PHP_SELF"],
"&id=$object->id", $sortfield, $sortorder,
'', 0, 0,
'');
168 print '<div class="div-table-responsive">';
169 print '<table class="noborder centpercent">';
171 print '<tr class="liste_titre">';
172 print_liste_field_titre(
"Invoice", $_SERVER[
"PHP_SELF"],
"f.ref",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
173 print_liste_field_titre(
"Company", $_SERVER[
"PHP_SELF"],
"s.nom",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
174 print_liste_field_titre(
"CustomerCode", $_SERVER[
"PHP_SELF"],
"s.code_client",
"",
"&id=".$object->id,
'', $sortfield, $sortorder);
175 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'left ');
176 print_liste_field_titre(
"SellingPrice", $_SERVER[
"PHP_SELF"],
"selling_price",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
177 print_liste_field_titre(
"BuyingPrice", $_SERVER[
"PHP_SELF"],
"buying_price",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
178 print_liste_field_titre(
"Qty", $_SERVER[
"PHP_SELF"],
"d.qty",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
179 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
180 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
181 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
182 if (!empty($conf->global->DISPLAY_MARK_RATES))
183 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
184 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"",
"&id=".$object->id,
'', $sortfield, $sortorder,
'right ');
193 $objp = $db->fetch_object($result);
195 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
196 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
198 print '<tr class="oddeven">';
200 $invoicestatic->id = $objp->facid;
201 $invoicestatic->ref = $objp->ref;
202 print $invoicestatic->getNomUrl(1);
204 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>';
205 print "<td>".$objp->code_client.
"</td>\n";
206 print "<td class=\"center\">";
208 print "<td class=\"right\">".price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
209 print "<td class=\"right\">".price(
price2num($objp->buying_price,
'MT')).
"</td>\n";
210 print "<td class=\"right\">".price(
price2num($objp->qty,
'MT')).
"</td>\n";
211 print "<td class=\"right\">".price(
price2num($objp->marge,
'MT')).
"</td>\n";
212 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
213 print "<td class=\"right\">".(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
214 if (!empty($conf->global->DISPLAY_MARK_RATES))
215 print "<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
216 print '<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
219 $cumul_achat += $objp->buying_price;
220 $cumul_vente += $objp->selling_price;
221 $cumul_qty += $objp->qty;
227 $totalMargin = $cumul_vente - $cumul_achat;
228 if ($totalMargin < 0)
230 $marginRate = ($cumul_achat != 0) ?-1 * (100 * $totalMargin / $cumul_achat) :
'';
231 $markRate = ($cumul_vente != 0) ?-1 * (100 * $totalMargin / $cumul_vente) :
'';
233 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
234 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
236 print '<tr class="liste_total">';
237 print '<td colspan=4>'.$langs->trans(
'TotalMargin').
"</td>";
238 print '<td class="right">'.price(
price2num($cumul_vente,
'MT')).
"</td>\n";
239 print '<td class="right">'.price(
price2num($cumul_achat,
'MT')).
"</td>\n";
240 print '<td class="right">'.price(
price2num($cumul_qty,
'MT')).
"</td>\n";
241 print '<td class="right">'.price(
price2num($totalMargin,
'MT')).
"</td>\n";
242 if (!empty($conf->global->DISPLAY_MARGIN_RATES))
243 print '<td class="right">'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
244 if (!empty($conf->global->DISPLAY_MARK_RATES))
245 print "<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
246 print '<td class="right"> </td>';
261 <script type="text/javascript">
262 $(document).ready(function() {
263 $("#totalMargin").html("'.
price(
price2num($totalMargin,
'MT')).
'");
264 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
265 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage products or services.
const TYPE_SERVICE
Service.
const TYPE_PRODUCT
Regular product.
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...
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.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=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 Calling this function terminate execution ...
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
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.
Class to manage invoices.
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.
product_prepare_head($object)
Prepare array with list of tabs.