64 public function getMarginInfosArray(
$object, $force_price =
false)
72 'margin_on_products' => 0,
73 'margin_rate_products' =>
'',
74 'mark_rate_products' =>
'',
77 'margin_on_services' => 0,
78 'margin_rate_services' =>
'',
79 'mark_rate_services' =>
'',
83 'total_margin_rate' =>
'',
84 'total_mark_rate' =>
''
87 foreach (
$object->lines as $line) {
88 if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price) {
89 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
91 if ($product->fetch_product_fournisseur_price($line->fk_fournprice)) {
92 $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
98 if ((!isset($line->pa_ht)) && $line->subprice > 0 && (isset(
$conf->global->ForceBuyingPriceIfNull) &&
getDolGlobalInt(
'ForceBuyingPriceIfNull') > 0)) {
99 $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
102 $pv = (float) $line->total_ht;
106 '@phan-var-force Facture|FactureFournisseur $object';
107 $pa_ht = (($pv < 0 || ($pv == 0 && in_array(
$object->element, array(
'facture',
'facture_fourn')) &&
$object->type == $object::TYPE_CREDIT_NOTE)) ? -$line->pa_ht : $line->pa_ht);
108 '@phan-var-force CommonObject $object';
111 '@phan-var-force Facture $object';
113 if ((
$object->element ==
'facture' &&
$object->type == $object::TYPE_SITUATION)
117 $prevPercent = $line->get_prev_progress(
$object->id);
118 $deltaPercent = $line->situation_percent - $prevPercent;
119 if ($line->situation_percent > 0) {
120 $pv *= ($deltaPercent / $line->situation_percent);
122 $pa = $line->qty * $pa_ht * ($deltaPercent / 100);
124 $pa = $line->qty * $pa_ht;
127 $pa = $line->qty * $pa_ht;
131 if (isset($line->fk_remise_except) && isset(
$conf->global->MARGIN_METHODE_FOR_DISCOUNT)) {
133 $marginInfos[
'pa_products'] += $pa;
134 $marginInfos[
'pv_products'] += $pv;
135 $marginInfos[
'pa_total'] += $pa;
136 $marginInfos[
'pv_total'] += $pv;
143 $marginInfos[
'margin_on_products'] += $pv - $pa;
145 $marginInfos[
'pa_services'] += $pa;
146 $marginInfos[
'pv_services'] += $pv;
147 $marginInfos[
'pa_total'] += $pa;
148 $marginInfos[
'pv_total'] += $pv;
153 $marginInfos[
'margin_on_services'] += $pv - $pa;
155 $marginInfos[
'pa_total'] += $pa;
156 $marginInfos[
'pv_total'] += $pv;
159 $type = $line->product_type ? $line->product_type : $line->fk_product_type;
161 $marginInfos[
'pa_products'] += $pa;
162 $marginInfos[
'pv_products'] += $pv;
163 $marginInfos[
'pa_total'] += $pa;
164 $marginInfos[
'pv_total'] += $pv;
172 $marginInfos[
'margin_on_products'] += $pv - $pa;
174 } elseif ($type == 1) {
175 $marginInfos[
'pa_services'] += $pa;
176 $marginInfos[
'pv_services'] += $pv;
177 $marginInfos[
'pa_total'] += $pa;
178 $marginInfos[
'pv_total'] += $pv;
183 $marginInfos[
'margin_on_services'] += $pv - $pa;
187 if ($marginInfos[
'pa_products'] > 0) {
188 $marginInfos[
'margin_rate_products'] = 100 * $marginInfos[
'margin_on_products'] / $marginInfos[
'pa_products'];
190 if ($marginInfos[
'pv_products'] > 0) {
191 $marginInfos[
'mark_rate_products'] = 100 * $marginInfos[
'margin_on_products'] / $marginInfos[
'pv_products'];
194 if ($marginInfos[
'pa_services'] > 0) {
195 $marginInfos[
'margin_rate_services'] = 100 * $marginInfos[
'margin_on_services'] / $marginInfos[
'pa_services'];
197 if ($marginInfos[
'pv_services'] > 0) {
198 $marginInfos[
'mark_rate_services'] = 100 * $marginInfos[
'margin_on_services'] / $marginInfos[
'pv_services'];
205 $marginInfos[
'total_margin'] = $marginInfos[
'pv_total'] - $marginInfos[
'pa_total'];
206 if ($marginInfos[
'pa_total'] > 0) {
207 $marginInfos[
'total_margin_rate'] = 100 * $marginInfos[
'total_margin'] / $marginInfos[
'pa_total'];
209 if ($marginInfos[
'pv_total'] > 0) {
210 $marginInfos[
'total_mark_rate'] = 100 * $marginInfos[
'total_margin'] / $marginInfos[
'pv_total'];
225 global $langs, $user, $hookmanager;
228 if (!empty($user->socid)) {
232 if (!$user->hasRight(
'margins',
'liretous')) {
236 $marginInfo = $this->getMarginInfosArray(
$object, $force_price);
238 print
'<!-- displayMarginInfos() - Show margin table -->' .
"\n";
240 $parameters = array(
'marginInfo' => &$marginInfo);
241 $reshook = $hookmanager->executeHooks(
'displayMarginInfos', $parameters,
$object, $action);
244 } elseif (empty($reshook)) {
245 $hidemargininfos = preg_replace(
'/[^a-zA-Z0-9_\-]/',
'', $_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW'] ??
'');
247 $buttonToShowHideMargin =
'<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ?
'' :
'hideobject') .
'">';
248 $buttonToShowHideMargin .=
img_picto($langs->trans(
"ShowMarginInfos"),
'switch_off',
'', 0, 0, 0,
'',
'size15x');
249 $buttonToShowHideMargin .=
'</span>';
250 $buttonToShowHideMargin .=
'<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ?
'hideobject' :
'') .
'">';
251 $buttonToShowHideMargin .=
img_picto($langs->trans(
"Hide"),
'switch_on_grey',
'', 0, 0, 0,
'',
'size15x opacitymedium');
252 $buttonToShowHideMargin .=
'</span>';
254 $buttonToShowHideMargin .=
'<script nonce="'.getNonce().
'">$(document).ready(function() {';
255 $buttonToShowHideMargin .=
'$("span#showMarginInfos").click(function() { console.log("click on showMargininfos"); date = new Date(); date.setTime(date.getTime()+(30*86400000)); document.cookie = "DOLUSER_MARGININFO_HIDE_SHOW=0; expires=" + date.toGMTString() + "; path=/ "; $(".margininfo").show(); $("span#showMarginInfos").addClass("hideobject"); $("span#hideMarginInfos").removeClass("hideobject"); });';
256 $buttonToShowHideMargin .=
'$("span#hideMarginInfos").click(function() { console.log("click on hideMarginInfos"); date = new Date(); date.setTime(date.getTime()+(30*86400000)); document.cookie = "DOLUSER_MARGININFO_HIDE_SHOW=1; expires=" + date.toGMTString() + "; path=/ "; $(".margininfo").hide(); $("span#hideMarginInfos").addClass("hideobject"); $("span#showMarginInfos").removeClass("hideobject"); });';
257 if (!empty($hidemargininfos)) {
258 $buttonToShowHideMargin .=
'console.log("hide the margin info"); $(".margininfo").hide();';
260 $buttonToShowHideMargin .=
'});</script>';
262 print
'<div class="div-table-responsive-no-min">';
264 print
'<table class="noborder margintable centpercent" id="margintable">';
265 print
'<tr class="liste_titre">';
266 print
'<td class="liste_titre">' . $langs->trans(
'Margins') .
' ' . $buttonToShowHideMargin .
'</td>';
267 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'SellingPrice') .
'</td>';
269 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'BuyingPrice') .
'</td>';
271 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'CostPrice') .
'</td>';
273 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'Margin') .
'</td>';
275 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'MarginRate') .
'</td>';
278 print
'<td class="liste_titre right margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">' . $langs->trans(
'MarkRate') .
'</td>';
284 print
'<tr class="oddeven margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">';
285 print
'<td>' . $langs->trans(
'MarginOnProducts') .
'</td>';
286 print
'<td class="right">' .
price($marginInfo[
'pv_products']) .
'</td>';
287 print
'<td class="right">' .
price($marginInfo[
'pa_products']) .
'</td>';
288 print
'<td class="right">' .
price($marginInfo[
'margin_on_products']) .
'</td>';
290 print
'<td class="right">' . (($marginInfo[
'margin_rate_products'] ==
'') ?
'' :
price($marginInfo[
'margin_rate_products'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
293 print
'<td class="right">' . (($marginInfo[
'mark_rate_products'] ==
'') ?
'' :
price($marginInfo[
'mark_rate_products'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
299 print
'<tr class="oddeven margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">';
300 print
'<td>' . $langs->trans(
'MarginOnServices') .
'</td>';
301 print
'<td class="right">' .
price($marginInfo[
'pv_services']) .
'</td>';
302 print
'<td class="right">' .
price($marginInfo[
'pa_services']) .
'</td>';
303 print
'<td class="right">' .
price($marginInfo[
'margin_on_services']) .
'</td>';
305 print
'<td class="right">' . (($marginInfo[
'margin_rate_services'] ==
'') ?
'' :
price($marginInfo[
'margin_rate_services'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
308 print
'<td class="right">' . (($marginInfo[
'mark_rate_services'] ==
'') ?
'' :
price($marginInfo[
'mark_rate_services'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
314 print
'<tr class="liste_total margininfo'.(empty($_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']) ?
'' :
' hideobject').
'">';
315 print
'<td>' . $langs->trans(
'TotalMargin') .
'</td>';
316 print
'<td class="right">' .
price($marginInfo[
'pv_total']) .
'</td>';
317 print
'<td class="right">' .
price($marginInfo[
'pa_total']) .
'</td>';
318 print
'<td class="right">' .
price($marginInfo[
'total_margin']) .
'</td>';
320 print
'<td class="right">' . (($marginInfo[
'total_margin_rate'] ==
'') ?
'' :
price($marginInfo[
'total_margin_rate'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
323 print
'<td class="right">' . (($marginInfo[
'total_mark_rate'] ==
'') ?
'' :
price($marginInfo[
'total_mark_rate'], 0,
'', 0, 0, 2) .
'%') .
'</td>';
327 print $hookmanager->resPrint;
330 } elseif ($reshook > 0) {
331 print $hookmanager->resPrint;
if(! $sortfield) if(! $sortorder) $object
Class to manage predefined suppliers products.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.