dolibarr  13.0.2
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  {
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($this->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->total_ht;
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  if ($object->element == 'facture' && $object->type == $object::TYPE_SITUATION) {
102  $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
103  } else {
104  $pa = $line->qty * $pa_ht;
105  }
106 
107  // calcul des marges
108  if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
109  if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
110  $marginInfos['pa_products'] += $pa;
111  $marginInfos['pv_products'] += $pv;
112  $marginInfos['pa_total'] += $pa;
113  $marginInfos['pv_total'] += $pv;
114  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
115  //if ($pv < 0)
116  //{
117  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
118  //}
119  //else
120  $marginInfos['margin_on_products'] += $pv - $pa;
121  } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
122  $marginInfos['pa_services'] += $pa;
123  $marginInfos['pv_services'] += $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  // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
129  //else
130  $marginInfos['margin_on_services'] += $pv - $pa;
131  } elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
132  $marginInfos['pa_total'] += $pa;
133  $marginInfos['pv_total'] += $pv;
134  }
135  } else {
136  $type = $line->product_type ? $line->product_type : $line->fk_product_type;
137  if ($type == 0) { // product
138  $marginInfos['pa_products'] += $pa;
139  $marginInfos['pv_products'] += $pv;
140  $marginInfos['pa_total'] += $pa;
141  $marginInfos['pv_total'] += $pv;
142  // if credit note, margin = -1 * (abs(selling_price) - buying_price)
143  //if ($pv < 0)
144  //{
145  // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
146  //}
147  //else
148  //{
149  $marginInfos['margin_on_products'] += $pv - $pa;
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  public function displayMarginInfos($object, $force_price = false)
195  {
196  global $langs, $conf, $user;
197 
198  if (!empty($user->socid)) return;
199 
200  if (!$user->rights->margins->liretous) return;
201 
202  $marginInfo = $this->getMarginInfosArray($object, $force_price);
203 
204  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
205  {
206  print $langs->trans('ShowMarginInfos').' : ';
207  $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']); // Clean cookie
208  print '<span id="showMarginInfos" class="linkobject '.(!empty($hidemargininfos) ? '' : 'hideobject').'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</span>';
209  print '<span id="hideMarginInfos" class="linkobject '.(!empty($hidemargininfos) ? 'hideobject' : '').'">'.img_picto($langs->trans("Enabled"), 'switch_on').'</span>';
210 
211  print '<script>$(document).ready(function() {
212  $("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");})});
213  $("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");})});
214  });</script>';
215  if (!empty($hidemargininfos)) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
216  }
217 
218  print '<div class="div-table-responsive-no-min">';
219  print '<!-- Margin table -->'."\n";
220 
221  print '<table class="noborder margintable centpercent">';
222  print '<tr class="liste_titre">';
223  print '<td class="liste_titre">'.$langs->trans('Margins').'</td>';
224  print '<td class="liste_titre right">'.$langs->trans('SellingPrice').'</td>';
225  if ($conf->global->MARGIN_TYPE == "1")
226  print '<td class="liste_titre right">'.$langs->trans('BuyingPrice').'</td>';
227  else print '<td class="liste_titre right">'.$langs->trans('CostPrice').'</td>';
228  print '<td class="liste_titre right">'.$langs->trans('Margin').'</td>';
229  if (!empty($conf->global->DISPLAY_MARGIN_RATES))
230  print '<td class="liste_titre right">'.$langs->trans('MarginRate').'</td>';
231  if (!empty($conf->global->DISPLAY_MARK_RATES))
232  print '<td class="liste_titre right">'.$langs->trans('MarkRate').'</td>';
233  print '</tr>';
234 
235  if (!empty($conf->product->enabled))
236  {
237  //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
238  print '<tr class="oddeven">';
239  print '<td>'.$langs->trans('MarginOnProducts').'</td>';
240  print '<td class="right">'.price($marginInfo['pv_products']).'</td>';
241  print '<td class="right">'.price($marginInfo['pa_products']).'</td>';
242  print '<td class="right">'.price($marginInfo['margin_on_products']).'</td>';
243  if (!empty($conf->global->DISPLAY_MARGIN_RATES))
244  print '<td class="right">'.(($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], null, null, null, null, 2).'%').'</td>';
245  if (!empty($conf->global->DISPLAY_MARK_RATES))
246  print '<td class="right">'.(($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], null, null, null, null, 2).'%').'</td>';
247  print '</tr>';
248  }
249 
250  if (!empty($conf->service->enabled))
251  {
252  print '<tr class="oddeven">';
253  print '<td>'.$langs->trans('MarginOnServices').'</td>';
254  print '<td class="right">'.price($marginInfo['pv_services']).'</td>';
255  print '<td class="right">'.price($marginInfo['pa_services']).'</td>';
256  print '<td class="right">'.price($marginInfo['margin_on_services']).'</td>';
257  if (!empty($conf->global->DISPLAY_MARGIN_RATES))
258  print '<td class="right">'.(($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], null, null, null, null, 2).'%').'</td>';
259  if (!empty($conf->global->DISPLAY_MARK_RATES))
260  print '<td class="right">'.(($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], null, null, null, null, 2).'%').'</td>';
261  print '</tr>';
262  }
263 
264  if (!empty($conf->product->enabled) && !empty($conf->service->enabled))
265  {
266  print '<tr class="liste_total">';
267  print '<td>'.$langs->trans('TotalMargin').'</td>';
268  print '<td class="right">'.price($marginInfo['pv_total']).'</td>';
269  print '<td class="right">'.price($marginInfo['pa_total']).'</td>';
270  print '<td class="right">'.price($marginInfo['total_margin']).'</td>';
271  if (!empty($conf->global->DISPLAY_MARGIN_RATES))
272  print '<td class="right">'.(($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], null, null, null, null, 2).'%').'</td>';
273  if (!empty($conf->global->DISPLAY_MARK_RATES))
274  print '<td class="right">'.(($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], null, null, null, null, 2).'%').'</td>';
275  print '</tr>';
276  }
277  print '</table>';
278  print '</div>';
279  }
280 }
__construct($db)
Constructor.
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.
$conf db
API class for accounts.
Definition: inc.php:54
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...
Classe permettant la generation de composants html autre Only common components are here...
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
getMarginInfosArray($object, $force_price=false)
get array with margin information from lines of object TODO Move this in common class.
print
Draft customers invoices.
Definition: index.php:89
Class to manage predefined suppliers products.