dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
31{
35 public $db;
36
40 public $error = '';
41
42
48 public function __construct($db)
49 {
50 $this->db = $db;
51 }
52
53
54
63 public function getMarginInfosArray($object, $force_price = false)
64 {
65 global $conf, $db;
66
67 // Default returned array
68 $marginInfos = array(
69 'pa_products' => 0,
70 'pv_products' => 0,
71 'margin_on_products' => 0,
72 'margin_rate_products' => '',
73 'mark_rate_products' => '',
74 'pa_services' => 0,
75 'pv_services' => 0,
76 'margin_on_services' => 0,
77 'margin_rate_services' => '',
78 'mark_rate_services' => '',
79 'pa_total' => 0,
80 'pv_total' => 0,
81 'total_margin' => 0,
82 'total_margin_rate' => '',
83 'total_mark_rate' => ''
84 );
85
86 foreach ($object->lines as $line) {
87 if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price) {
88 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
89 $product = new ProductFournisseur($this->db);
90 if ($product->fetch_product_fournisseur_price($line->fk_fournprice)) {
91 $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
92 }
93 }
94
95 // 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.
96 //if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
97 if ((!isset($line->pa_ht)) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && getDolGlobalInt('ForceBuyingPriceIfNull') > 0)) {
98 $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
99 }
100
101 $pv = $line->total_ht;
102 // We chose to have line->pa_ht always positive in database, so we guess the correct sign
103 // @phan-suppress-next-line PhanUndeclaredConstantOfClass
104 $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);
105 if (getDolGlobalInt('INVOICE_USE_SITUATION') == 1) { // Special case for old situation mode
106 // @phan-suppress-next-line PhanUndeclaredConstantOfClass
107 if (($object->element == 'facture' && $object->type == $object::TYPE_SITUATION)
108 // @phan-suppress-next-line PhanUndeclaredConstantOfClass
109 || ($object->element == 'facture' && $object->type == $object::TYPE_CREDIT_NOTE && getDolGlobalInt('INVOICE_USE_SITUATION_CREDIT_NOTE') && $object->situation_counter > 0)) {
110 // We need a compensation relative to $line->situation_percent
111 $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
112 } else {
113 $pa = $line->qty * $pa_ht;
114 }
115 } else {
116 $pa = $line->qty * $pa_ht;
117 }
118
119 // calcul des marges
120 if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
121 if (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '1') { // remise globale considérée comme produit
122 $marginInfos['pa_products'] += $pa;
123 $marginInfos['pv_products'] += $pv;
124 $marginInfos['pa_total'] += $pa;
125 $marginInfos['pv_total'] += $pv;
126 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
127 //if ($pv < 0)
128 //{
129 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
130 //}
131 //else
132 $marginInfos['margin_on_products'] += $pv - $pa;
133 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '2') { // remise globale considérée comme service
134 $marginInfos['pa_services'] += $pa;
135 $marginInfos['pv_services'] += $pv;
136 $marginInfos['pa_total'] += $pa;
137 $marginInfos['pv_total'] += $pv;
138 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
139 //if ($pv < 0)
140 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
141 //else
142 $marginInfos['margin_on_services'] += $pv - $pa;
143 } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '3') { // remise globale prise en compte uniqt sur total
144 $marginInfos['pa_total'] += $pa;
145 $marginInfos['pv_total'] += $pv;
146 }
147 } else {
148 $type = $line->product_type ? $line->product_type : $line->fk_product_type;
149 if ($type == 0) { // product
150 $marginInfos['pa_products'] += $pa;
151 $marginInfos['pv_products'] += $pv;
152 $marginInfos['pa_total'] += $pa;
153 $marginInfos['pv_total'] += $pv;
154 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
155 //if ($pv < 0)
156 //{
157 // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
158 //}
159 //else
160 //{
161 $marginInfos['margin_on_products'] += $pv - $pa;
162 //}
163 } elseif ($type == 1) { // service
164 $marginInfos['pa_services'] += $pa;
165 $marginInfos['pv_services'] += $pv;
166 $marginInfos['pa_total'] += $pa;
167 $marginInfos['pv_total'] += $pv;
168 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
169 //if ($pv < 0)
170 // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
171 //else
172 $marginInfos['margin_on_services'] += $pv - $pa;
173 }
174 }
175 }
176 if ($marginInfos['pa_products'] > 0) {
177 $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
178 }
179 if ($marginInfos['pv_products'] > 0) {
180 $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
181 }
182
183 if ($marginInfos['pa_services'] > 0) {
184 $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
185 }
186 if ($marginInfos['pv_services'] > 0) {
187 $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
188 }
189
190 // if credit note, margin = -1 * (abs(selling_price) - buying_price)
191 //if ($marginInfos['pv_total'] < 0)
192 // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
193 //else
194 $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
195 if ($marginInfos['pa_total'] > 0) {
196 $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
197 }
198 if ($marginInfos['pv_total'] > 0) {
199 $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
200 }
201
202 return $marginInfos;
203 }
204
212 public function displayMarginInfos($object, $force_price = false)
213 {
214 global $langs, $user, $hookmanager;
215 global $action;
216
217 if (!empty($user->socid)) {
218 return;
219 }
220
221 if (!$user->hasRight('margins', 'liretous')) {
222 return;
223 }
224
225 $marginInfo = $this->getMarginInfosArray($object, $force_price);
226
227 print '<!-- displayMarginInfos() - Show margin table -->' . "\n";
228
229 $parameters = array('marginInfo' => &$marginInfo);
230 $reshook = $hookmanager->executeHooks('displayMarginInfos', $parameters, $object, $action);
231 if ($reshook < 0) {
232 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
233 } elseif (empty($reshook)) {
234 $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ?? ''; // Clean cookie
235
236 $buttonToShowHideMargin = '<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? '' : 'hideobject') . '">';
237 $buttonToShowHideMargin .= img_picto($langs->trans("ShowMarginInfos"), 'switch_off', '', 0, 0, 0, '', 'size15x');
238 $buttonToShowHideMargin .= '</span>';
239 $buttonToShowHideMargin .= '<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? 'hideobject' : '') . '">';
240 $buttonToShowHideMargin .= img_picto($langs->trans("Hide"), 'switch_on_grey', '', 0, 0, 0, '', 'size15x opacitymedium');
241 $buttonToShowHideMargin .= '</span>';
242
243 $buttonToShowHideMargin .= '<script nonce="'.getNonce().'">$(document).ready(function() {';
244 $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"); });';
245 $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"); });';
246 if (!empty($hidemargininfos)) {
247 $buttonToShowHideMargin .= 'console.log("hide the margin info"); $(".margininfo").hide();';
248 }
249 $buttonToShowHideMargin .= '});</script>';
250
251 print '<div class="div-table-responsive-no-min">';
252
253 print '<table class="noborder margintable centpercent" id="margintable">';
254 print '<tr class="liste_titre">';
255 print '<td class="liste_titre">' . $langs->trans('Margins') . ' ' . $buttonToShowHideMargin . '</td>';
256 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('SellingPrice') . '</td>';
257 if (getDolGlobalString('MARGIN_TYPE') == "1") {
258 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('BuyingPrice') . '</td>';
259 } else {
260 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('CostPrice') . '</td>';
261 }
262 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('Margin') . '</td>';
263 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
264 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarginRate') . '</td>';
265 }
266 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
267 print '<td class="liste_titre right margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">' . $langs->trans('MarkRate') . '</td>';
268 }
269 print '</tr>';
270
271 if (isModEnabled("product")) {
272 //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
273 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
274 print '<td>' . $langs->trans('MarginOnProducts') . '</td>';
275 print '<td class="right">' . price($marginInfo['pv_products']) . '</td>';
276 print '<td class="right">' . price($marginInfo['pa_products']) . '</td>';
277 print '<td class="right">' . price($marginInfo['margin_on_products']) . '</td>';
278 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
279 print '<td class="right">' . (($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
280 }
281 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
282 print '<td class="right">' . (($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], 0, '', 0, 0, 2) . '%') . '</td>';
283 }
284 print '</tr>';
285 }
286
287 if (isModEnabled("service")) {
288 print '<tr class="oddeven margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
289 print '<td>' . $langs->trans('MarginOnServices') . '</td>';
290 print '<td class="right">' . price($marginInfo['pv_services']) . '</td>';
291 print '<td class="right">' . price($marginInfo['pa_services']) . '</td>';
292 print '<td class="right">' . price($marginInfo['margin_on_services']) . '</td>';
293 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
294 print '<td class="right">' . (($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
295 }
296 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
297 print '<td class="right">' . (($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], 0, '', 0, 0, 2) . '%') . '</td>';
298 }
299 print '</tr>';
300 }
301
302 if (isModEnabled("product") && isModEnabled("service")) {
303 print '<tr class="liste_total margininfo'.(empty($_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']) ? '' : ' hideobject').'">';
304 print '<td>' . $langs->trans('TotalMargin') . '</td>';
305 print '<td class="right">' . price($marginInfo['pv_total']) . '</td>';
306 print '<td class="right">' . price($marginInfo['pa_total']) . '</td>';
307 print '<td class="right">' . price($marginInfo['total_margin']) . '</td>';
308 if (getDolGlobalString('DISPLAY_MARGIN_RATES')) {
309 print '<td class="right">' . (($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
310 }
311 if (getDolGlobalString('DISPLAY_MARK_RATES')) {
312 print '<td class="right">' . (($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], 0, '', 0, 0, 2) . '%') . '</td>';
313 }
314 print '</tr>';
315 }
316 print $hookmanager->resPrint;
317 print '</table>';
318 print '</div>';
319 } elseif ($reshook > 0) {
320 print $hookmanager->resPrint;
321 }
322 }
323}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.