dolibarr 25.0.0-alpha
html.formmargin.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2015-2019 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
32{
36 public $db;
37
41 public $error = '';
42
43
49 public function __construct($db)
50 {
51 $this->db = $db;
52 }
53
54
55
64 public function getMarginInfosArray($object, $force_price = false)
65 {
66 global $conf, $db;
67
68 // Default returned array
69 $marginInfos = array(
70 'pa_products' => 0,
71 'pv_products' => 0,
72 'margin_on_products' => 0,
73 'margin_rate_products' => '',
74 'mark_rate_products' => '',
75 'pa_services' => 0,
76 'pv_services' => 0,
77 'margin_on_services' => 0,
78 'margin_rate_services' => '',
79 'mark_rate_services' => '',
80 'pa_total' => 0,
81 'pv_total' => 0,
82 'total_margin' => 0,
83 'total_margin_rate' => '',
84 'total_mark_rate' => ''
85 );
86
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';
90 $product = new ProductFournisseur($this->db);
91 if ($product->fetch_product_fournisseur_price($line->fk_fournprice)) {
92 $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
93 }
94 }
95
96 // If buy price is not defined (null), we will use the sell price. If defined to 0 (it means it was forced to 0 during insert, for example for a free to get product), we must still use 0.
97 //if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
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));
100 }
101
102 $pv = (float) $line->total_ht;
103
104 // $line->pa_ht is always positive in database, so we guess the correct sign for $pa_ht
105
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';
109
110 $pa = $line->qty * $pa_ht;
111
112 if (getDolGlobalInt('INVOICE_USE_SITUATION') && $object->element == 'facture') {
113 '@phan-var-force Facture $object';
115 if ($object->type == $object::TYPE_SITUATION || ($object->type == $object::TYPE_CREDIT_NOTE && getDolGlobalInt('INVOICE_USE_SITUATION_CREDIT_NOTE') && $object->situation_counter > 0)) {
116 if (getDolGlobalInt('INVOICE_USE_SITUATION_CREDIT_NOTE') == 1) {
117 // We need only the delta between this situation and the previous one to avoid cumulating margins across situation invoices
118 $prevPercent = $line->get_prev_progress($object->id);
119 $deltaPercent = $line->situation_percent - $prevPercent;
120 if ($line->situation_percent > 0) {
121 $pv *= ($deltaPercent / $line->situation_percent);
122 }
123 // the sign is carried by $pa_ht, so never by the ratio
124 $pa *= abs($deltaPercent / 100);
125 } else {
126 // the sign is carried by $pa_ht, so never by the ratio
127 $pa *= abs($line->situation_percent) / 100;
128 }
129 }
130 }
131
132 // margin calculation
133 if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
134 if (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '1') { // global discount treated as product
135 $marginInfos['pa_products'] += $pa;
136 $marginInfos['pv_products'] += $pv;
137 $marginInfos['pa_total'] += $pa;
138 $marginInfos['pv_total'] += $pv;
139 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
140 //if ($pv < 0)
141 //{
142 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
143 //}
144 //else
145 $marginInfos['margin_on_products'] += $pv - $pa;
146 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '2') { // global discount treated as service
147 $marginInfos['pa_services'] += $pa;
148 $marginInfos['pv_services'] += $pv;
149 $marginInfos['pa_total'] += $pa;
150 $marginInfos['pv_total'] += $pv;
151 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
152 //if ($pv < 0)
153 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
154 //else
155 $marginInfos['margin_on_services'] += $pv - $pa;
156 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '3') { // remise globale prise en compte uniqt sur total
157 $marginInfos['pa_total'] += $pa;
158 $marginInfos['pv_total'] += $pv;
159 }
160 } else {
161 $type = $line->product_type ? $line->product_type : $line->fk_product_type;
162 if ($type == 0) { // product
163 $marginInfos['pa_products'] += $pa;
164 $marginInfos['pv_products'] += $pv;
165 $marginInfos['pa_total'] += $pa;
166 $marginInfos['pv_total'] += $pv;
167 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
168 //if ($pv < 0)
169 //{
170 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
171 //}
172 //else
173 //{
174 $marginInfos['margin_on_products'] += $pv - $pa;
175 //}
176 } elseif ($type == 1) { // service
177 $marginInfos['pa_services'] += $pa;
178 $marginInfos['pv_services'] += $pv;
179 $marginInfos['pa_total'] += $pa;
180 $marginInfos['pv_total'] += $pv;
181 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
182 //if ($pv < 0)
183 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
184 //else
185 $marginInfos['margin_on_services'] += $pv - $pa;
186 }
187 }
188 }
189 if ($marginInfos['pa_products'] > 0) {
190 $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
191 }
192 if ($marginInfos['pv_products'] > 0) {
193 $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
194 }
195
196 if ($marginInfos['pa_services'] > 0) {
197 $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
198 }
199 if ($marginInfos['pv_services'] > 0) {
200 $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
201 }
202
203 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
204 //if ($marginInfos['pv_total'] < 0)
205 // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
206 //else
207 $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
208 if ($marginInfos['pa_total'] > 0) {
209 $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
210 }
211 if ($marginInfos['pv_total'] > 0) {
212 $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
213 }
214
215 return $marginInfos;
216 }
217
225 public function displayMarginInfos($object, $force_price = false)
226 {
227 global $langs, $user, $hookmanager;
228 global $action;
229
230 if (!empty($user->socid)) {
231 return;
232 }
233
234 if (!$user->hasRight('margins', 'liretous')) {
235 return;
236 }
237
238 $marginInfo = $this->getMarginInfosArray($object, $force_price);
239
240 print '<!-- displayMarginInfos() - Show margin table -->' . "\n";
241
242 $parameters = array('marginInfo' => &$marginInfo);
243 $reshook = $hookmanager->executeHooks('displayMarginInfos', $parameters, $object, $action);
244 if ($reshook < 0) {
245 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
246 } elseif (empty($reshook)) {
247 $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW'] ?? ''); // Clean cookie
248
249 $buttonToShowHideMargin = '<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? '' : 'hideobject') . '">';
250 $buttonToShowHideMargin .= img_picto($langs->trans("ShowMarginInfos"), 'switch_off', '', 0, 0, 0, '', 'size15x');
251 $buttonToShowHideMargin .= '</span>';
252 $buttonToShowHideMargin .= '<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? 'hideobject' : '') . '">';
253 $buttonToShowHideMargin .= img_picto($langs->trans("Hide"), 'switch_on_grey', '', 0, 0, 0, '', 'size15x opacitymedium');
254 $buttonToShowHideMargin .= '</span>';
255
256 $buttonToShowHideMargin .= '<script nonce="'.getNonce().'">$(document).ready(function() {';
257 $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"); });';
258 $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"); });';
259 if (!empty($hidemargininfos)) {
260 $buttonToShowHideMargin .= 'console.log("hide the margin info"); $(".margininfo").hide();';
261 }
262 $buttonToShowHideMargin .= '});</script>';
263
264 print '<div class="div-table-responsive-no-min">';
265
266 print '<table class="noborder margintable centpercent" id="margintable">';
267 print '<tr class="liste_titre">';
268 print '<td class="liste_titre">' . $langs->trans('Margins') . ' ' . $buttonToShowHideMargin . '</td>';
269 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('SellingPrice') . '</td>';
270 if (getDolGlobalString('MARGIN_TYPE') == "1") {
271 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('BuyingPrice') . '</td>';
272 } else {
273 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('CostPrice') . '</td>';
274 }
275 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('Margin') . '</td>';
276 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
277 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarginRate') . '</td>';
278 }
279 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
280 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarkRate') . '</td>';
281 }
282 print '</tr>';
283
284 if (isModEnabled("product")) {
285 //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
286 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
287 print '<td>' . $langs->trans('MarginOnProducts') . '</td>';
288 print '<td class="right">' . price($marginInfo['pv_products']) . '</td>';
289 print '<td class="right">' . price($marginInfo['pa_products']) . '</td>';
290 print '<td class="right">' . price($marginInfo['margin_on_products']) . '</td>';
291 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
292 print '<td class="right">' . (($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
293 }
294 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
295 print '<td class="right">' . (($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
296 }
297 print '</tr>';
298 }
299
300 if (isModEnabled("service")) {
301 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
302 print '<td>' . $langs->trans('MarginOnServices') . '</td>';
303 print '<td class="right">' . price($marginInfo['pv_services']) . '</td>';
304 print '<td class="right">' . price($marginInfo['pa_services']) . '</td>';
305 print '<td class="right">' . price($marginInfo['margin_on_services']) . '</td>';
306 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
307 print '<td class="right">' . (($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
308 }
309 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
310 print '<td class="right">' . (($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
311 }
312 print '</tr>';
313 }
314
315 if (isModEnabled("product") && isModEnabled("service")) {
316 print '<tr class="liste_total margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
317 print '<td>' . $langs->trans('TotalMargin') . '</td>';
318 print '<td class="right">' . price($marginInfo['pv_total']) . '</td>';
319 print '<td class="right">' . price($marginInfo['pa_total']) . '</td>';
320 print '<td class="right">' . price($marginInfo['total_margin']) . '</td>';
321 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
322 print '<td class="right">' . (($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
323 }
324 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
325 print '<td class="right">' . (($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
326 }
327 print '</tr>';
328 }
329 print $hookmanager->resPrint;
330 print '</table>';
331 print '</div>';
332 } elseif ($reshook > 0) {
333 print $hookmanager->resPrint;
334 }
335 }
336}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage "other" html components Only common components are here.
displayMarginInfos($object, $force_price=false)
Show the array with all margin infos.
__construct($db)
Constructor.
Class to manage predefined suppliers products.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.