dolibarr  9.0.0
html.formmargin.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2015 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 <http://www.gnu.org/licenses/>.
16  */
17 
30 {
34  public $db;
35 
39  public $error='';
40 
41 
47  function __construct($db)
48  {
49  $this->db = $db;
50  }
51 
52 
53 
62  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  {
87  if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price)
88  {
89  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
90  $product = new ProductFournisseur($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  // si prix d'achat non renseigné et devrait l'être, alors prix achat = prix vente
95  if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)) {
96  $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
97  }
98 
99  $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
100  $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
101  $pa = $line->qty * $pa_ht;
102 
103  // calcul des marges
104  if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
105  if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
106  $marginInfos['pa_products'] += $pa;
107  $marginInfos['pv_products'] += $pv;
108  $marginInfos['pa_total'] += $pa;
109  $marginInfos['pv_total'] += $pv;
110  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
111  //if ($pv < 0)
112  //{
113  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
114  //}
115  //else
116  $marginInfos['margin_on_products'] += $pv - $pa;
117  }
118  elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
119  $marginInfos['pa_services'] += $pa;
120  $marginInfos['pv_services'] += $pv;
121  $marginInfos['pa_total'] += $pa;
122  $marginInfos['pv_total'] += $pv;
123  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
124  //if ($pv < 0)
125  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
126  //else
127  $marginInfos['margin_on_services'] += $pv - $pa;
128  }
129  elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
130  $marginInfos['pa_total'] += $pa;
131  $marginInfos['pv_total'] += $pv;
132  }
133  }
134  else {
135  $type=$line->product_type?$line->product_type:$line->fk_product_type;
136  if ($type == 0) { // product
137  $marginInfos['pa_products'] += $pa;
138  $marginInfos['pv_products'] += $pv;
139  $marginInfos['pa_total'] += $pa;
140  $marginInfos['pv_total'] += $pv;
141  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
142  //if ($pv < 0)
143  //{
144  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
145  //}
146  //else
147  //{
148  $marginInfos['margin_on_products'] += $pv - $pa;
149  //}
150  }
151  elseif ($type == 1) { // service
152  $marginInfos['pa_services'] += $pa;
153  $marginInfos['pv_services'] += $pv;
154  $marginInfos['pa_total'] += $pa;
155  $marginInfos['pv_total'] += $pv;
156  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
157  //if ($pv < 0)
158  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
159  //else
160  $marginInfos['margin_on_services'] += $pv - $pa;
161  }
162  }
163  }
164  if ($marginInfos['pa_products'] > 0)
165  $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
166  if ($marginInfos['pv_products'] > 0)
167  $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
168 
169  if ($marginInfos['pa_services'] > 0)
170  $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
171  if ($marginInfos['pv_services'] > 0)
172  $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
173 
174  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
175  //if ($marginInfos['pv_total'] < 0)
176  // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
177  //else
178  $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
179  if ($marginInfos['pa_total'] > 0)
180  $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
181  if ($marginInfos['pv_total'] > 0)
182  $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
183 
184  return $marginInfos;
185  }
186 
194  function displayMarginInfos($object, $force_price=false)
195  {
196  global $langs, $conf, $user;
197 
198  if (! empty($user->societe_id)) return;
199 
200  if (! $user->rights->margins->liretous) return;
201 
202  $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT, $conf->global->MAIN_MAX_DECIMALS_TOT);
203 
204  $marginInfo = $this->getMarginInfosArray($object, $force_price);
205 
206  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
207  {
208  print $langs->trans('ShowMarginInfos').' : ';
209  $hidemargininfos = $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW'];
210  print '<span id="showMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'':'hideobject').'">'.img_picto($langs->trans("Disabled"),'switch_off').'</span>';
211  print '<span id="hideMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'hideobject':'').'">'.img_picto($langs->trans("Enabled"),'switch_on').'</span>';
212 
213  print '<script>$(document).ready(function() {
214  $("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");})});
215  $("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");})});
216  });</script>';
217  if (!empty($hidemargininfos)) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
218  }
219 
220  print '<div class="div-table-responsive-no-min">';
221  print '<!-- Margin table -->'."\n";
222 
223  print '<table class="noborder margintable centpercent">';
224  print '<tr class="liste_titre">';
225  print '<td class="liste_titre">'.$langs->trans('Margins').'</td>';
226  print '<td class="liste_titre" align="right">'.$langs->trans('SellingPrice').'</td>';
227  if ($conf->global->MARGIN_TYPE == "1")
228  print '<td class="liste_titre" align="right">'.$langs->trans('BuyingPrice').'</td>';
229  else
230  print '<td class="liste_titre" align="right">'.$langs->trans('CostPrice').'</td>';
231  print '<td class="liste_titre" align="right">'.$langs->trans('Margin').'</td>';
232  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
233  print '<td class="liste_titre" align="right">'.$langs->trans('MarginRate').'</td>';
234  if (! empty($conf->global->DISPLAY_MARK_RATES))
235  print '<td class="liste_titre" align="right">'.$langs->trans('MarkRate').'</td>';
236  print '</tr>';
237 
238  if (! empty($conf->product->enabled))
239  {
240  //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
241  print '<tr class="oddeven">';
242  print '<td>'.$langs->trans('MarginOnProducts').'</td>';
243  print '<td align="right">'.price($marginInfo['pv_products'], null, null, null, null, $rounding).'</td>';
244  print '<td align="right">'.price($marginInfo['pa_products'], null, null, null, null, $rounding).'</td>';
245  print '<td align="right">'.price($marginInfo['margin_on_products'], null, null, null, null, $rounding).'</td>';
246  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
247  print '<td align="right">'.(($marginInfo['margin_rate_products'] == '')?'':price($marginInfo['margin_rate_products'], null, null, null, null, $rounding).'%').'</td>';
248  if (! empty($conf->global->DISPLAY_MARK_RATES))
249  print '<td align="right">'.(($marginInfo['mark_rate_products'] == '')?'':price($marginInfo['mark_rate_products'], null, null, null, null, $rounding).'%').'</td>';
250  print '</tr>';
251  }
252 
253  if (! empty($conf->service->enabled))
254  {
255  print '<tr class="oddeven">';
256  print '<td>'.$langs->trans('MarginOnServices').'</td>';
257  print '<td align="right">'.price($marginInfo['pv_services'], null, null, null, null, $rounding).'</td>';
258  print '<td align="right">'.price($marginInfo['pa_services'], null, null, null, null, $rounding).'</td>';
259  print '<td align="right">'.price($marginInfo['margin_on_services'], null, null, null, null, $rounding).'</td>';
260  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
261  print '<td align="right">'.(($marginInfo['margin_rate_services'] == '')?'':price($marginInfo['margin_rate_services'], null, null, null, null, $rounding).'%').'</td>';
262  if (! empty($conf->global->DISPLAY_MARK_RATES))
263  print '<td align="right">'.(($marginInfo['mark_rate_services'] == '')?'':price($marginInfo['mark_rate_services'], null, null, null, null, $rounding).'%').'</td>';
264  print '</tr>';
265  }
266 
267  if (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
268  {
269  print '<tr class="liste_total">';
270  print '<td>'.$langs->trans('TotalMargin').'</td>';
271  print '<td align="right">'.price($marginInfo['pv_total'], null, null, null, null, $rounding).'</td>';
272  print '<td align="right">'.price($marginInfo['pa_total'], null, null, null, null, $rounding).'</td>';
273  print '<td align="right">'.price($marginInfo['total_margin'], null, null, null, null, $rounding).'</td>';
274  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
275  print '<td align="right">'.(($marginInfo['total_margin_rate'] == '')?'':price($marginInfo['total_margin_rate'], null, null, null, null, $rounding).'%').'</td>';
276  if (! empty($conf->global->DISPLAY_MARK_RATES))
277  print '<td align="right">'.(($marginInfo['total_mark_rate'] == '')?'':price($marginInfo['total_mark_rate'], null, null, null, null, $rounding).'%').'</td>';
278  print '</tr>';
279  }
280  print '</table>';
281  print '</div>';
282  }
283 }
284 
__construct($db)
Constructor.
print
Draft customers invoices.
Definition: index.php:91
displayMarginInfos($object, $force_price=false)
Show the array with all margin infos.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Classe permettant la generation de composants html autre Only common components are here...
getMarginInfosArray($object, $force_price=false)
get array with margin information from lines of object TODO Move this in common class.
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...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Class to manage predefined suppliers products.