dolibarr  7.0.0-beta
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 {
31  var $db;
32  var $error;
33 
34 
40  function __construct($db)
41  {
42  $this->db = $db;
43 
44  return 1;
45  }
46 
47 
48 
57  function getMarginInfosArray($object, $force_price=false)
58  {
59  global $conf, $db;
60 
61  // Default returned array
62  $marginInfos = array(
63  'pa_products' => 0,
64  'pv_products' => 0,
65  'margin_on_products' => 0,
66  'margin_rate_products' => '',
67  'mark_rate_products' => '',
68  'pa_services' => 0,
69  'pv_services' => 0,
70  'margin_on_services' => 0,
71  'margin_rate_services' => '',
72  'mark_rate_services' => '',
73  'pa_total' => 0,
74  'pv_total' => 0,
75  'total_margin' => 0,
76  'total_margin_rate' => '',
77  'total_mark_rate' => ''
78  );
79 
80  foreach($object->lines as $line)
81  {
82  if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price)
83  {
84  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
85  $product = new ProductFournisseur($db);
86  if ($product->fetch_product_fournisseur_price($line->fk_fournprice))
87  $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
88  if (isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
89  $line->pa_ht += $product->fourn_unitcharges;
90  }
91  // si prix d'achat non renseigné et devrait l'être, alors prix achat = prix vente
92  if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)) {
93  $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
94  }
95 
96  $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
97  $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
98  $pa = $line->qty * $pa_ht;
99 
100  // calcul des marges
101  if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
102  if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
103  $marginInfos['pa_products'] += $pa;
104  $marginInfos['pv_products'] += $pv;
105  $marginInfos['pa_total'] += $pa;
106  $marginInfos['pv_total'] += $pv;
107  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
108  //if ($pv < 0)
109  //{
110  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
111  //}
112  //else
113  $marginInfos['margin_on_products'] += $pv - $pa;
114  }
115  elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
116  $marginInfos['pa_services'] += $pa;
117  $marginInfos['pv_services'] += $pv;
118  $marginInfos['pa_total'] += $pa;
119  $marginInfos['pv_total'] += $pv;
120  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
121  //if ($pv < 0)
122  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
123  //else
124  $marginInfos['margin_on_services'] += $pv - $pa;
125  }
126  elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
127  $marginInfos['pa_total'] += $pa;
128  $marginInfos['pv_total'] += $pv;
129  }
130  }
131  else {
132  $type=$line->product_type?$line->product_type:$line->fk_product_type;
133  if ($type == 0) { // product
134  $marginInfos['pa_products'] += $pa;
135  $marginInfos['pv_products'] += $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  //{
141  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
142  //}
143  //else
144  //{
145  $marginInfos['margin_on_products'] += $pv - $pa;
146  //}
147  }
148  elseif ($type == 1) { // service
149  $marginInfos['pa_services'] += $pa;
150  $marginInfos['pv_services'] += $pv;
151  $marginInfos['pa_total'] += $pa;
152  $marginInfos['pv_total'] += $pv;
153  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
154  //if ($pv < 0)
155  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
156  //else
157  $marginInfos['margin_on_services'] += $pv - $pa;
158  }
159  }
160  }
161  if ($marginInfos['pa_products'] > 0)
162  $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
163  if ($marginInfos['pv_products'] > 0)
164  $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
165 
166  if ($marginInfos['pa_services'] > 0)
167  $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
168  if ($marginInfos['pv_services'] > 0)
169  $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
170 
171  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
172  //if ($marginInfos['pv_total'] < 0)
173  // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
174  //else
175  $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
176  if ($marginInfos['pa_total'] > 0)
177  $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
178  if ($marginInfos['pv_total'] > 0)
179  $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
180 
181  return $marginInfos;
182  }
183 
191  function displayMarginInfos($object, $force_price=false)
192  {
193  global $langs, $conf, $user;
194 
195  if (! empty($user->societe_id)) return;
196 
197  if (! $user->rights->margins->liretous) return;
198 
199  $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT, $conf->global->MAIN_MAX_DECIMALS_TOT);
200 
201  $marginInfo = $this->getMarginInfosArray($object, $force_price);
202 
203  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
204  {
205  print $langs->trans('ShowMarginInfos').' : ';
206  $hidemargininfos = $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW'];
207  print '<span id="showMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'':'hideobject').'">'.img_picto($langs->trans("Disabled"),'switch_off').'</span>';
208  print '<span id="hideMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'hideobject':'').'">'.img_picto($langs->trans("Enabled"),'switch_on').'</span>';
209 
210  print '<script>$(document).ready(function() {
211  $("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");})});
212  $("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");})});
213  });</script>';
214  if (!empty($hidemargininfos)) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
215  }
216 
217  print '<!-- Margin table -->'."\n";
218  print '<table class="noborder margintable centpercent">';
219  print '<tr class="liste_titre">';
220  print '<td class="liste_titre">'.$langs->trans('Margins').'</td>';
221  print '<td class="liste_titre" align="right">'.$langs->trans('SellingPrice').'</td>';
222  if ($conf->global->MARGIN_TYPE == "1")
223  print '<td class="liste_titre" align="right">'.$langs->trans('BuyingPrice').'</td>';
224  else
225  print '<td class="liste_titre" align="right">'.$langs->trans('CostPrice').'</td>';
226  print '<td class="liste_titre" align="right">'.$langs->trans('Margin').'</td>';
227  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
228  print '<td class="liste_titre" align="right">'.$langs->trans('MarginRate').'</td>';
229  if (! empty($conf->global->DISPLAY_MARK_RATES))
230  print '<td class="liste_titre" align="right">'.$langs->trans('MarkRate').'</td>';
231  print '</tr>';
232 
233  if (! empty($conf->product->enabled))
234  {
235  //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
236  print '<tr class="oddeven">';
237  print '<td>'.$langs->trans('MarginOnProducts').'</td>';
238  print '<td align="right">'.price($marginInfo['pv_products'], null, null, null, null, $rounding).'</td>';
239  print '<td align="right">'.price($marginInfo['pa_products'], null, null, null, null, $rounding).'</td>';
240  print '<td align="right">'.price($marginInfo['margin_on_products'], null, null, null, null, $rounding).'</td>';
241  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
242  print '<td align="right">'.(($marginInfo['margin_rate_products'] == '')?'':price($marginInfo['margin_rate_products'], null, null, null, null, $rounding).'%').'</td>';
243  if (! empty($conf->global->DISPLAY_MARK_RATES))
244  print '<td align="right">'.(($marginInfo['mark_rate_products'] == '')?'':price($marginInfo['mark_rate_products'], null, null, null, null, $rounding).'%').'</td>';
245  print '</tr>';
246  }
247 
248  if (! empty($conf->service->enabled))
249  {
250  print '<tr class="oddeven">';
251  print '<td>'.$langs->trans('MarginOnServices').'</td>';
252  print '<td align="right">'.price($marginInfo['pv_services'], null, null, null, null, $rounding).'</td>';
253  print '<td align="right">'.price($marginInfo['pa_services'], null, null, null, null, $rounding).'</td>';
254  print '<td align="right">'.price($marginInfo['margin_on_services'], null, null, null, null, $rounding).'</td>';
255  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
256  print '<td align="right">'.(($marginInfo['margin_rate_services'] == '')?'':price($marginInfo['margin_rate_services'], null, null, null, null, $rounding).'%').'</td>';
257  if (! empty($conf->global->DISPLAY_MARK_RATES))
258  print '<td align="right">'.(($marginInfo['mark_rate_services'] == '')?'':price($marginInfo['mark_rate_services'], null, null, null, null, $rounding).'%').'</td>';
259  print '</tr>';
260  }
261 
262  if (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
263  {
264  print '<tr class="liste_total">';
265  print '<td>'.$langs->trans('TotalMargin').'</td>';
266  print '<td align="right">'.price($marginInfo['pv_total'], null, null, null, null, $rounding).'</td>';
267  print '<td align="right">'.price($marginInfo['pa_total'], null, null, null, null, $rounding).'</td>';
268  print '<td align="right">'.price($marginInfo['total_margin'], null, null, null, null, $rounding).'</td>';
269  if (! empty($conf->global->DISPLAY_MARGIN_RATES))
270  print '<td align="right">'.(($marginInfo['total_margin_rate'] == '')?'':price($marginInfo['total_margin_rate'], null, null, null, null, $rounding).'%').'</td>';
271  if (! empty($conf->global->DISPLAY_MARK_RATES))
272  print '<td align="right">'.(($marginInfo['total_mark_rate'] == '')?'':price($marginInfo['total_mark_rate'], null, null, null, null, $rounding).'%').'</td>';
273  print '</tr>';
274  }
275  print '</table>';
276  }
277 
278 }
279 
__construct($db)
Constructor.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
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...
print
Draft customers invoices.
Definition: index.php:91
Class to manage predefined suppliers products.