dolibarr 21.0.0-beta
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 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 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 = $line->total_ht;
103
104 // $line->pa_ht is always positive in database, so we guess the correct sign
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 if (getDolGlobalInt('INVOICE_USE_SITUATION') == 1) { // Special case for old situation mode
111 '@phan-var-force Facture $object';
112 if (($object->element == 'facture' && $object->type == $object::TYPE_SITUATION)
113 || ($object->element == 'facture' && $object->type == $object::TYPE_CREDIT_NOTE && getDolGlobalInt('INVOICE_USE_SITUATION_CREDIT_NOTE') && $object->situation_counter > 0)) {
114 // We need a compensation relative to $line->situation_percent
115 $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
116 } else {
117 $pa = $line->qty * $pa_ht;
118 }
119 } else {
120 $pa = $line->qty * $pa_ht;
121 }
122
123 // calcul des marges
124 if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
125 if (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '1') { // remise globale considérée comme produit
126 $marginInfos['pa_products'] += $pa;
127 $marginInfos['pv_products'] += $pv;
128 $marginInfos['pa_total'] += $pa;
129 $marginInfos['pv_total'] += $pv;
130 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
131 //if ($pv < 0)
132 //{
133 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
134 //}
135 //else
136 $marginInfos['margin_on_products'] += $pv - $pa;
137 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '2') { // remise globale considérée comme service
138 $marginInfos['pa_services'] += $pa;
139 $marginInfos['pv_services'] += $pv;
140 $marginInfos['pa_total'] += $pa;
141 $marginInfos['pv_total'] += $pv;
142 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
143 //if ($pv < 0)
144 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
145 //else
146 $marginInfos['margin_on_services'] += $pv - $pa;
147 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '3') { // remise globale prise en compte uniqt sur total
148 $marginInfos['pa_total'] += $pa;
149 $marginInfos['pv_total'] += $pv;
150 }
151 } else {
152 $type = $line->product_type ? $line->product_type : $line->fk_product_type;
153 if ($type == 0) { // product
154 $marginInfos['pa_products'] += $pa;
155 $marginInfos['pv_products'] += $pv;
156 $marginInfos['pa_total'] += $pa;
157 $marginInfos['pv_total'] += $pv;
158 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
159 //if ($pv < 0)
160 //{
161 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
162 //}
163 //else
164 //{
165 $marginInfos['margin_on_products'] += $pv - $pa;
166 //}
167 } elseif ($type == 1) { // service
168 $marginInfos['pa_services'] += $pa;
169 $marginInfos['pv_services'] += $pv;
170 $marginInfos['pa_total'] += $pa;
171 $marginInfos['pv_total'] += $pv;
172 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
173 //if ($pv < 0)
174 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
175 //else
176 $marginInfos['margin_on_services'] += $pv - $pa;
177 }
178 }
179 }
180 if ($marginInfos['pa_products'] > 0) {
181 $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
182 }
183 if ($marginInfos['pv_products'] > 0) {
184 $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
185 }
186
187 if ($marginInfos['pa_services'] > 0) {
188 $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
189 }
190 if ($marginInfos['pv_services'] > 0) {
191 $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
192 }
193
194 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
195 //if ($marginInfos['pv_total'] < 0)
196 // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
197 //else
198 $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
199 if ($marginInfos['pa_total'] > 0) {
200 $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
201 }
202 if ($marginInfos['pv_total'] > 0) {
203 $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
204 }
205
206 return $marginInfos;
207 }
208
216 public function displayMarginInfos($object, $force_price = false)
217 {
218 global $langs, $user, $hookmanager;
219 global $action;
220
221 if (!empty($user->socid)) {
222 return;
223 }
224
225 if (!$user->hasRight('margins', 'liretous')) {
226 return;
227 }
228
229 $marginInfo = $this->getMarginInfosArray($object, $force_price);
230
231 print '<!-- displayMarginInfos() - Show margin table -->' . "\n";
232
233 $parameters = array('marginInfo' => &$marginInfo);
234 $reshook = $hookmanager->executeHooks('displayMarginInfos', $parameters, $object, $action);
235 if ($reshook < 0) {
236 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
237 } elseif (empty($reshook)) {
238 $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW'] ?? ''); // Clean cookie
239
240 $buttonToShowHideMargin = '<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? '' : 'hideobject') . '">';
241 $buttonToShowHideMargin .= img_picto($langs->trans("ShowMarginInfos"), 'switch_off', '', 0, 0, 0, '', 'size15x');
242 $buttonToShowHideMargin .= '</span>';
243 $buttonToShowHideMargin .= '<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? 'hideobject' : '') . '">';
244 $buttonToShowHideMargin .= img_picto($langs->trans("Hide"), 'switch_on_grey', '', 0, 0, 0, '', 'size15x opacitymedium');
245 $buttonToShowHideMargin .= '</span>';
246
247 $buttonToShowHideMargin .= '<script nonce="'.getNonce().'">$(document).ready(function() {';
248 $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"); });';
249 $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"); });';
250 if (!empty($hidemargininfos)) {
251 $buttonToShowHideMargin .= 'console.log("hide the margin info"); $(".margininfo").hide();';
252 }
253 $buttonToShowHideMargin .= '});</script>';
254
255 print '<div class="div-table-responsive-no-min">';
256
257 print '<table class="noborder margintable centpercent" id="margintable">';
258 print '<tr class="liste_titre">';
259 print '<td class="liste_titre">' . $langs->trans('Margins') . ' ' . $buttonToShowHideMargin . '</td>';
260 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('SellingPrice') . '</td>';
261 if (getDolGlobalString('MARGIN_TYPE') == "1") {
262 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('BuyingPrice') . '</td>';
263 } else {
264 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('CostPrice') . '</td>';
265 }
266 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('Margin') . '</td>';
267 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
268 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarginRate') . '</td>';
269 }
270 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
271 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarkRate') . '</td>';
272 }
273 print '</tr>';
274
275 if (isModEnabled("product")) {
276 //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
277 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
278 print '<td>' . $langs->trans('MarginOnProducts') . '</td>';
279 print '<td class="right">' . price($marginInfo['pv_products']) . '</td>';
280 print '<td class="right">' . price($marginInfo['pa_products']) . '</td>';
281 print '<td class="right">' . price($marginInfo['margin_on_products']) . '</td>';
282 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
283 print '<td class="right">' . (($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
284 }
285 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
286 print '<td class="right">' . (($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
287 }
288 print '</tr>';
289 }
290
291 if (isModEnabled("service")) {
292 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
293 print '<td>' . $langs->trans('MarginOnServices') . '</td>';
294 print '<td class="right">' . price($marginInfo['pv_services']) . '</td>';
295 print '<td class="right">' . price($marginInfo['pa_services']) . '</td>';
296 print '<td class="right">' . price($marginInfo['margin_on_services']) . '</td>';
297 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
298 print '<td class="right">' . (($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
299 }
300 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
301 print '<td class="right">' . (($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
302 }
303 print '</tr>';
304 }
305
306 if (isModEnabled("product") && isModEnabled("service")) {
307 print '<tr class="liste_total margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
308 print '<td>' . $langs->trans('TotalMargin') . '</td>';
309 print '<td class="right">' . price($marginInfo['pv_total']) . '</td>';
310 print '<td class="right">' . price($marginInfo['pa_total']) . '</td>';
311 print '<td class="right">' . price($marginInfo['total_margin']) . '</td>';
312 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
313 print '<td class="right">' . (($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
314 }
315 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
316 print '<td class="right">' . (($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
317 }
318 print '</tr>';
319 }
320 print $hookmanager->resPrint;
321 print '</table>';
322 print '</div>';
323 } elseif ($reshook > 0) {
324 print $hookmanager->resPrint;
325 }
326 }
327}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class permettant la generation de composants html autre Only common components are here.
displayMarginInfos($object, $force_price=false)
Show the array with all margin infos.
getMarginInfosArray($object, $force_price=false)
get array with margin information from lines of object TODO Move this in common class.
__construct($db)
Constructor.
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)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79