70 'margin_on_products' => 0,
71 'margin_rate_products' =>
'',
72 'mark_rate_products' =>
'',
75 'margin_on_services' => 0,
76 'margin_rate_services' =>
'',
77 'mark_rate_services' =>
'',
81 'total_margin_rate' =>
'',
82 'total_mark_rate' =>
''
85 foreach ($object->lines as $line) {
86 if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price) {
87 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
89 if ($product->fetch_product_fournisseur_price($line->fk_fournprice)) {
90 $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
96 if ((!isset($line->pa_ht)) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
97 $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
100 $pv = $line->total_ht;
102 $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);
104 if (($object->element ==
'facture' && $object->type == $object::TYPE_SITUATION)
105 || ($object->element ==
'facture' && $object->type == $object::TYPE_CREDIT_NOTE &&
getDolGlobalInt(
'INVOICE_USE_SITUATION_CREDIT_NOTE') && $object->situation_counter > 0)) {
107 $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
109 $pa = $line->qty * $pa_ht;
112 $pa = $line->qty * $pa_ht;
116 if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) {
118 $marginInfos[
'pa_products'] += $pa;
119 $marginInfos[
'pv_products'] += $pv;
120 $marginInfos[
'pa_total'] += $pa;
121 $marginInfos[
'pv_total'] += $pv;
128 $marginInfos[
'margin_on_products'] += $pv - $pa;
130 $marginInfos[
'pa_services'] += $pa;
131 $marginInfos[
'pv_services'] += $pv;
132 $marginInfos[
'pa_total'] += $pa;
133 $marginInfos[
'pv_total'] += $pv;
138 $marginInfos[
'margin_on_services'] += $pv - $pa;
140 $marginInfos[
'pa_total'] += $pa;
141 $marginInfos[
'pv_total'] += $pv;
144 $type = $line->product_type ? $line->product_type : $line->fk_product_type;
146 $marginInfos[
'pa_products'] += $pa;
147 $marginInfos[
'pv_products'] += $pv;
148 $marginInfos[
'pa_total'] += $pa;
149 $marginInfos[
'pv_total'] += $pv;
157 $marginInfos[
'margin_on_products'] += $pv - $pa;
159 } elseif ($type == 1) {
160 $marginInfos[
'pa_services'] += $pa;
161 $marginInfos[
'pv_services'] += $pv;
162 $marginInfos[
'pa_total'] += $pa;
163 $marginInfos[
'pv_total'] += $pv;
168 $marginInfos[
'margin_on_services'] += $pv - $pa;
172 if ($marginInfos[
'pa_products'] > 0) {
173 $marginInfos[
'margin_rate_products'] = 100 * $marginInfos[
'margin_on_products'] / $marginInfos[
'pa_products'];
175 if ($marginInfos[
'pv_products'] > 0) {
176 $marginInfos[
'mark_rate_products'] = 100 * $marginInfos[
'margin_on_products'] / $marginInfos[
'pv_products'];
179 if ($marginInfos[
'pa_services'] > 0) {
180 $marginInfos[
'margin_rate_services'] = 100 * $marginInfos[
'margin_on_services'] / $marginInfos[
'pa_services'];
182 if ($marginInfos[
'pv_services'] > 0) {
183 $marginInfos[
'mark_rate_services'] = 100 * $marginInfos[
'margin_on_services'] / $marginInfos[
'pv_services'];
190 $marginInfos[
'total_margin'] = $marginInfos[
'pv_total'] - $marginInfos[
'pa_total'];
191 if ($marginInfos[
'pa_total'] > 0) {
192 $marginInfos[
'total_margin_rate'] = 100 * $marginInfos[
'total_margin'] / $marginInfos[
'pa_total'];
194 if ($marginInfos[
'pv_total'] > 0) {
195 $marginInfos[
'total_mark_rate'] = 100 * $marginInfos[
'total_margin'] / $marginInfos[
'pv_total'];
210 global $langs, $conf, $user, $hookmanager;
212 if (!empty($user->socid)) {
216 if (empty($user->rights->margins->liretous)) {
222 $parameters=array(
'marginInfo' => &$marginInfo);
223 $reshook = $hookmanager->executeHooks(
'displayMarginInfos', $parameters, $object, $action);
226 } elseif (empty($reshook)) {
227 if (!empty($conf->global->MARGIN_ADD_SHOWHIDE_BUTTON)) {
228 print $langs->trans(
'ShowMarginInfos') .
' ';
229 $hidemargininfos = preg_replace(
'/[^a-zA-Z0-9_\-]/',
'', $_COOKIE[
'DOLUSER_MARGININFO_HIDE_SHOW']);
230 print
'<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ?
'' :
'hideobject') .
'">' .
img_picto($langs->trans(
"Disabled"),
'switch_off') .
'</span>';
231 print
'<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ?
'hideobject' :
'') .
'">' .
img_picto($langs->trans(
"Enabled"),
'switch_on') .
'</span>';
233 print
'<script nonce="'.getNonce().
'">$(document).ready(function() {';
234 print
'$("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=/ "; $(".margintable").show(); $("span#showMarginInfos").addClass("hideobject"); $("span#hideMarginInfos").removeClass("hideobject"); });';
235 print
'$("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=/ "; $(".margintable").hide(); $("span#hideMarginInfos").addClass("hideobject"); $("span#showMarginInfos").removeClass("hideobject"); });';
236 if (!empty($hidemargininfos)) {
237 print
'console.log("hide the margin info"); $("#margintable").hide();';
239 print
'});</script>';
242 print
'<!-- displayMarginInfos() - Show margin table -->' .
"\n";
243 print
'<div class="div-table-responsive-no-min">';
245 print
'<table class="noborder margintable centpercent" id="margintable">';
246 print
'<tr class="liste_titre">';
247 print
'<td class="liste_titre">' . $langs->trans(
'Margins') .
'</td>';
248 print
'<td class="liste_titre right">' . $langs->trans(
'SellingPrice') .
'</td>';
249 if ($conf->global->MARGIN_TYPE ==
"1") {
250 print
'<td class="liste_titre right">' . $langs->trans(
'BuyingPrice') .
'</td>';
252 print
'<td class="liste_titre right">' . $langs->trans(
'CostPrice') .
'</td>';
254 print
'<td class="liste_titre right">' . $langs->trans(
'Margin') .
'</td>';
255 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
256 print
'<td class="liste_titre right">' . $langs->trans(
'MarginRate') .
'</td>';
258 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
259 print
'<td class="liste_titre right">' . $langs->trans(
'MarkRate') .
'</td>';
263 if (isModEnabled(
"product")) {
265 print
'<tr class="oddeven">';
266 print
'<td>' . $langs->trans(
'MarginOnProducts') .
'</td>';
267 print
'<td class="right">' .
price($marginInfo[
'pv_products']) .
'</td>';
268 print
'<td class="right">' .
price($marginInfo[
'pa_products']) .
'</td>';
269 print
'<td class="right">' .
price($marginInfo[
'margin_on_products']) .
'</td>';
270 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
271 print
'<td class="right">' . (($marginInfo[
'margin_rate_products'] ==
'') ?
'' :
price($marginInfo[
'margin_rate_products'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
273 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
274 print
'<td class="right">' . (($marginInfo[
'mark_rate_products'] ==
'') ?
'' :
price($marginInfo[
'mark_rate_products'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
279 if (isModEnabled(
"service")) {
280 print
'<tr class="oddeven">';
281 print
'<td>' . $langs->trans(
'MarginOnServices') .
'</td>';
282 print
'<td class="right">' .
price($marginInfo[
'pv_services']) .
'</td>';
283 print
'<td class="right">' .
price($marginInfo[
'pa_services']) .
'</td>';
284 print
'<td class="right">' .
price($marginInfo[
'margin_on_services']) .
'</td>';
285 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
286 print
'<td class="right">' . (($marginInfo[
'margin_rate_services'] ==
'') ?
'' :
price($marginInfo[
'margin_rate_services'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
288 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
289 print
'<td class="right">' . (($marginInfo[
'mark_rate_services'] ==
'') ?
'' :
price($marginInfo[
'mark_rate_services'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
294 if (isModEnabled(
"product") && isModEnabled(
"service")) {
295 print
'<tr class="liste_total">';
296 print
'<td>' . $langs->trans(
'TotalMargin') .
'</td>';
297 print
'<td class="right">' .
price($marginInfo[
'pv_total']) .
'</td>';
298 print
'<td class="right">' .
price($marginInfo[
'pa_total']) .
'</td>';
299 print
'<td class="right">' .
price($marginInfo[
'total_margin']) .
'</td>';
300 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
301 print
'<td class="right">' . (($marginInfo[
'total_margin_rate'] ==
'') ?
'' :
price($marginInfo[
'total_margin_rate'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
303 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
304 print
'<td class="right">' . (($marginInfo[
'total_mark_rate'] ==
'') ?
'' :
price($marginInfo[
'total_mark_rate'],
null,
null,
null,
null, 2) .
'%') .
'</td>';
308 print $hookmanager->resPrint;
311 } elseif ($reshook > 0) {
312 print $hookmanager->resPrint;