dolibarr  20.0.0-beta
margins.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
3  * Copyright (C) 2014-2015 Marcos GarcĂ­a <marcosgdf@gmail.com>
4  * Copyright (C) 2016 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
33 {
34  global $langs, $conf;
35 
36  $h = 0;
37  $head = array();
38 
39  $head[$h][0] = DOL_URL_ROOT."/margin/admin/margin.php";
40  $head[$h][1] = $langs->trans("Parameters");
41  $head[$h][2] = 'parameters';
42  $h++;
43 
44  // Show more tabs from modules
45  // Entries must be declared in modules descriptor with line
46  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
47  // $this->tabs = array('entity:-tabname); to remove a tab
48  complete_head_from_modules($conf, $langs, null, $head, $h, 'margesadmin');
49 
50  complete_head_from_modules($conf, $langs, null, $head, $h, 'margesadmin', 'remove');
51 
52  return $head;
53 }
54 
61 {
62  global $langs, $conf, $user;
63  $langs->load("margins");
64 
65  $h = 0;
66  $head = array();
67 
68  if ($user->hasRight('produit', 'lire')) {
69  $head[$h][0] = DOL_URL_ROOT."/margin/productMargins.php";
70  $head[$h][1] = $langs->trans("ProductMargins");
71  $head[$h][2] = 'productMargins';
72  $h++;
73  }
74 
75  if ($user->hasRight('societe', 'lire')) {
76  $head[$h][0] = DOL_URL_ROOT."/margin/customerMargins.php";
77  $head[$h][1] = $langs->trans("CustomerMargins");
78  $head[$h][2] = 'customerMargins';
79  $h++;
80  }
81 
82  if ($user->hasRight('margins', 'read', 'all')) {
83  $title = 'UserMargins';
84  } else {
85  $title = 'SalesRepresentativeMargins';
86  }
87 
88  $head[$h][0] = DOL_URL_ROOT."/margin/agentMargins.php";
89  $head[$h][1] = $langs->trans($title);
90  $head[$h][2] = 'agentMargins';
91 
92 
93  if ($user->hasRight('margins', 'creer')) {
94  $h++;
95  $head[$h][0] = DOL_URL_ROOT."/margin/checkMargins.php";
96  $head[$h][1] = $langs->trans('CheckMargins');
97  $head[$h][2] = 'checkMargins';
98  }
99 
100  complete_head_from_modules($conf, $langs, null, $head, $h, 'margins');
101 
102  complete_head_from_modules($conf, $langs, null, $head, $h, 'margins', 'remove');
103 
104  return $head;
105 }
106 
119 function getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
120 {
121  global $db, $conf;
122 
123  $marge_tx_ret = '';
124  $marque_tx_ret = '';
125 
126  if ($fk_pa > 0 && empty($pa_ht)) {
127  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
128  $product = new ProductFournisseur($db);
129  if ($product->fetch_product_fournisseur_price($fk_pa)) {
130  $pa_ht_ret = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
131  } else {
132  $pa_ht_ret = $pa_ht;
133  }
134  } else {
135  $pa_ht_ret = $pa_ht;
136  }
137 
138  // Calculate selling unit price including line discount
139  // We don't use calculate_price, because this function is dedicated to calculation of total with accuracy of total. We need an accuracy of a unit price.
140  // Also we must not apply rounding on non decimal rule defined by option MAIN_ROUNDING_RULE_TOT
141  $pu_ht_remise = $pv_ht * (1 - ($remise_percent / 100));
142  $pu_ht_remise = price2num($pu_ht_remise, 'MU');
143 
144  // calcul marge
145  if ($pu_ht_remise < 0) {
146  $marge = -1 * (abs((float) $pu_ht_remise) - $pa_ht_ret);
147  } else {
148  $marge = (float) $pu_ht_remise - $pa_ht_ret;
149  }
150 
151  // calcul taux marge
152  if ($pa_ht_ret != 0) {
153  $marge_tx_ret = (100 * $marge) / $pa_ht_ret;
154  }
155  // calcul taux marque
156  if ($pu_ht_remise != 0) {
157  $marque_tx_ret = (100 * $marge) / (float) $pu_ht_remise;
158  }
159 
160  return array($pa_ht_ret, $marge_tx_ret, $marque_tx_ret);
161 }
Class to manage predefined suppliers products.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
marges_admin_prepare_head()
Define head array for tabs of marges tools setup pages.
Definition: margins.lib.php:32
marges_prepare_head()
Return array of tabs to used on pages for third parties cards.
Definition: margins.lib.php:60
div float
Buy price without taxes.
Definition: style.css.php:960