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