dolibarr  16.0.5
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) && $conf->global->ForceBuyingPriceIfNull > 0)) {
97  $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
98  }
99 
100  $pv = $line->total_ht;
101  $pa_ht = ($pv < 0 ? -$line->pa_ht : $line->pa_ht); // We choosed to have line->pa_ht always positive in database, so we guess the correct sign
102  if (($object->element == 'facture' && $object->type == $object::TYPE_SITUATION)
103  || ($object->element == 'facture' && $object->type == $object::TYPE_CREDIT_NOTE && $conf->global->INVOICE_USE_SITUATION_CREDIT_NOTE && $object->situation_counter > 0)) {
104  $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
105  } else {
106  $pa = $line->qty * $pa_ht;
107  }
108 
109  // calcul des marges
110  if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
111  if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
112  $marginInfos['pa_products'] += $pa;
113  $marginInfos['pv_products'] += $pv;
114  $marginInfos['pa_total'] += $pa;
115  $marginInfos['pv_total'] += $pv;
116  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
117  //if ($pv < 0)
118  //{
119  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
120  //}
121  //else
122  $marginInfos['margin_on_products'] += $pv - $pa;
123  } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
124  $marginInfos['pa_services'] += $pa;
125  $marginInfos['pv_services'] += $pv;
126  $marginInfos['pa_total'] += $pa;
127  $marginInfos['pv_total'] += $pv;
128  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
129  //if ($pv < 0)
130  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
131  //else
132  $marginInfos['margin_on_services'] += $pv - $pa;
133  } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
134  $marginInfos['pa_total'] += $pa;
135  $marginInfos['pv_total'] += $pv;
136  }
137  } else {
138  $type = $line->product_type ? $line->product_type : $line->fk_product_type;
139  if ($type == 0) { // product
140  $marginInfos['pa_products'] += $pa;
141  $marginInfos['pv_products'] += $pv;
142  $marginInfos['pa_total'] += $pa;
143  $marginInfos['pv_total'] += $pv;
144  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
145  //if ($pv < 0)
146  //{
147  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
148  //}
149  //else
150  //{
151  $marginInfos['margin_on_products'] += $pv - $pa;
152  //}
153  } elseif ($type == 1) { // service
154  $marginInfos['pa_services'] += $pa;
155  $marginInfos['pv_services'] += $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  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
161  //else
162  $marginInfos['margin_on_services'] += $pv - $pa;
163  }
164  }
165  }
166  if ($marginInfos['pa_products'] > 0) {
167  $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
168  }
169  if ($marginInfos['pv_products'] > 0) {
170  $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
171  }
172 
173  if ($marginInfos['pa_services'] > 0) {
174  $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
175  }
176  if ($marginInfos['pv_services'] > 0) {
177  $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
178  }
179 
180  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
181  //if ($marginInfos['pv_total'] < 0)
182  // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
183  //else
184  $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
185  if ($marginInfos['pa_total'] > 0) {
186  $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
187  }
188  if ($marginInfos['pv_total'] > 0) {
189  $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
190  }
191 
192  return $marginInfos;
193  }
194 
202  public function displayMarginInfos($object, $force_price = false)
203  {
204  global $langs, $conf, $user, $hookmanager;
205 
206  if (!empty($user->socid)) {
207  return;
208  }
209 
210  if (empty($user->rights->margins->liretous)) {
211  return;
212  }
213 
214  $marginInfo = $this->getMarginInfosArray($object, $force_price);
215 
216  $parameters=array('marginInfo'=>&$marginInfo);
217  $reshook = $hookmanager->executeHooks('displayMarginInfos', $parameters, $object, $action);
218  if ($reshook < 0) {
219  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
220  } elseif (empty($reshook)) {
221  if (!empty($conf->global->MARGIN_ADD_SHOWHIDE_BUTTON)) { // TODO Warning this feature rely on an external js file that may be removed. Using native js function document.cookie should be better
222  print $langs->trans('ShowMarginInfos') . ' : ';
223  $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']); // Clean cookie
224  print '<span id="showMarginInfos" class="linkobject ' . (!empty($hidemargininfos) ? '' : 'hideobject') . '">' . img_picto($langs->trans("Disabled"), 'switch_off') . '</span>';
225  print '<span id="hideMarginInfos" class="linkobject ' . (!empty($hidemargininfos) ? 'hideobject' : '') . '">' . img_picto($langs->trans("Enabled"), 'switch_on') . '</span>';
226 
227  print '<script>$(document).ready(function() {
228  $("span#showMarginInfos").click(function() { $.getScript( "' . dol_buildpath('/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js', 1) . '", function( data, textStatus, jqxhr ) { $.cookie("DOLUSER_MARGININFO_HIDE_SHOW", 0); $(".margininfos").show(); $("span#showMarginInfos").addClass("hideobject"); $("span#hideMarginInfos").removeClass("hideobject");})});
229  $("span#hideMarginInfos").click(function() { $.getScript( "' . dol_buildpath('/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js', 1) . '", function( data, textStatus, jqxhr ) { $.cookie("DOLUSER_MARGININFO_HIDE_SHOW", 1); $(".margininfos").hide(); $("span#hideMarginInfos").addClass("hideobject"); $("span#showMarginInfos").removeClass("hideobject");})});
230  });</script>';
231  if (!empty($hidemargininfos)) {
232  print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
233  }
234  }
235 
236  print '<div class="div-table-responsive-no-min">';
237  print '<!-- Margin table -->' . "\n";
238 
239  print '<table class="noborder margintable centpercent">';
240  print '<tr class="liste_titre">';
241  print '<td class="liste_titre">' . $langs->trans('Margins') . '</td>';
242  print '<td class="liste_titre right">' . $langs->trans('SellingPrice') . '</td>';
243  if ($conf->global->MARGIN_TYPE == "1") {
244  print '<td class="liste_titre right">' . $langs->trans('BuyingPrice') . '</td>';
245  } else {
246  print '<td class="liste_titre right">' . $langs->trans('CostPrice') . '</td>';
247  }
248  print '<td class="liste_titre right">' . $langs->trans('Margin') . '</td>';
249  if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
250  print '<td class="liste_titre right">' . $langs->trans('MarginRate') . '</td>';
251  }
252  if (!empty($conf->global->DISPLAY_MARK_RATES)) {
253  print '<td class="liste_titre right">' . $langs->trans('MarkRate') . '</td>';
254  }
255  print '</tr>';
256 
257  if (!empty($conf->product->enabled)) {
258  //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
259  print '<tr class="oddeven">';
260  print '<td>' . $langs->trans('MarginOnProducts') . '</td>';
261  print '<td class="right">' . price($marginInfo['pv_products']) . '</td>';
262  print '<td class="right">' . price($marginInfo['pa_products']) . '</td>';
263  print '<td class="right">' . price($marginInfo['margin_on_products']) . '</td>';
264  if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
265  print '<td class="right">' . (($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], null, null, null, null, 2) . '%') . '</td>';
266  }
267  if (!empty($conf->global->DISPLAY_MARK_RATES)) {
268  print '<td class="right">' . (($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], null, null, null, null, 2) . '%') . '</td>';
269  }
270  print '</tr>';
271  }
272 
273  if (!empty($conf->service->enabled)) {
274  print '<tr class="oddeven">';
275  print '<td>' . $langs->trans('MarginOnServices') . '</td>';
276  print '<td class="right">' . price($marginInfo['pv_services']) . '</td>';
277  print '<td class="right">' . price($marginInfo['pa_services']) . '</td>';
278  print '<td class="right">' . price($marginInfo['margin_on_services']) . '</td>';
279  if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
280  print '<td class="right">' . (($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], null, null, null, null, 2) . '%') . '</td>';
281  }
282  if (!empty($conf->global->DISPLAY_MARK_RATES)) {
283  print '<td class="right">' . (($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], null, null, null, null, 2) . '%') . '</td>';
284  }
285  print '</tr>';
286  }
287 
288  if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
289  print '<tr class="liste_total">';
290  print '<td>' . $langs->trans('TotalMargin') . '</td>';
291  print '<td class="right">' . price($marginInfo['pv_total']) . '</td>';
292  print '<td class="right">' . price($marginInfo['pa_total']) . '</td>';
293  print '<td class="right">' . price($marginInfo['total_margin']) . '</td>';
294  if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
295  print '<td class="right">' . (($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], null, null, null, null, 2) . '%') . '</td>';
296  }
297  if (!empty($conf->global->DISPLAY_MARK_RATES)) {
298  print '<td class="right">' . (($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], null, null, null, null, 2) . '%') . '</td>';
299  }
300  print '</tr>';
301  }
302  print $hookmanager->resPrint;
303  print '</table>';
304  print '</div>';
305  } elseif ($reshook > 0) {
306  print $hookmanager->resPrint;
307  }
308  }
309 }
db
$conf db
API class for accounts.
Definition: inc.php:41
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:41
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
FormMargin
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formmargin.class.php:29
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
FormMargin\displayMarginInfos
displayMarginInfos($object, $force_price=false)
Show the array with all margin infos.
Definition: html.formmargin.class.php:202
FormMargin\__construct
__construct($db)
Constructor.
Definition: html.formmargin.class.php:47
FormMargin\getMarginInfosArray
getMarginInfosArray($object, $force_price=false)
get array with margin information from lines of object TODO Move this in common class.
Definition: html.formmargin.class.php:62
price
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.
Definition: functions.lib.php:5541
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137