dolibarr  9.0.0
facturation_verif.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2008 Jeremie Ollivier <jeremie.o@laposte.net>
3  * Copyright (C) 2008-2010 Laurent Destailleur <eldy@uers.sourceforge.net>
4  * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php';
28 require_once DOL_DOCUMENT_ROOT.'/cashdesk/class/Facturation.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 
32 $action = GETPOST('action','alpha');
33 
34 $obj_facturation = unserialize($_SESSION['serObjFacturation']);
35 unset ($_SESSION['serObjFacturation']);
36 
37 
38 switch($action)
39 {
40  default:
41  if ( $_POST['hdnSource'] != 'NULL' )
42  {
43  $sql = "SELECT p.rowid, p.ref, p.price, p.tva_tx, p.default_vat_code, p.recuperableonly";
44  if (! empty($conf->stock->enabled) && !empty($conf_fkentrepot)) $sql.= ", ps.reel";
45  $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
46  if (! empty($conf->stock->enabled) && !empty($conf_fkentrepot)) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps ON p.rowid = ps.fk_product AND ps.fk_entrepot = ".$conf_fkentrepot;
47  $sql.= " WHERE p.entity IN (".getEntity('product').")";
48 
49  // Recuperation des donnees en fonction de la source (liste deroulante ou champ texte) ...
50  if ( $_POST['hdnSource'] == 'LISTE' )
51  {
52  $sql.= " AND p.rowid = ".$_POST['selProduit'];
53  }
54  else if ( $_POST['hdnSource'] == 'REF' )
55  {
56  $sql.= " AND p.ref = '".$_POST['txtRef']."'";
57  }
58 
59  $result = $db->query($sql);
60  if ($result)
61  {
62  // ... et enregistrement dans l'objet
63  if ( $db->num_rows($result) )
64  {
65  $ret=array();
66  $tab = $db->fetch_array($result);
67  foreach ( $tab as $key => $value )
68  {
69  $ret[$key] = $value;
70  }
71  // Here $ret['tva_tx'] is vat rate of product but we want to not use the one into table but found by function
72 
73  $productid = $ret['rowid'];
74  $product = new Product($db);
75  $product->fetch($productid);
76  $prod = $product;
77 
78  $thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
79  $societe = new Societe($db);
80  $societe->fetch($thirdpartyid);
81 
82  // Update if prices fields are defined
83  $tva_tx = get_default_tva($mysoc, $societe, $product->id);
84  $tva_npr = get_default_npr($mysoc, $societe, $product->id);
85  if (empty($tva_tx)) $tva_npr=0;
86 
87  $pu_ht = $prod->price;
88  $pu_ttc = $prod->price_ttc;
89  $price_min = $prod->price_min;
90  $price_base_type = $prod->price_base_type;
91 
92  // multiprix
93  if (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($societe->price_level))
94  {
95  $pu_ht = $prod->multiprices[$societe->price_level];
96  $pu_ttc = $prod->multiprices_ttc[$societe->price_level];
97  $price_min = $prod->multiprices_min[$societe->price_level];
98  $price_base_type = $prod->multiprices_base_type[$societe->price_level];
99  if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
100  {
101  if (isset($prod->multiprices_tva_tx[$societe->price_level])) $tva_tx=$prod->multiprices_tva_tx[$societe->price_level];
102  if (isset($prod->multiprices_recuperableonly[$societe->price_level])) $tva_npr=$prod->multiprices_recuperableonly[$societe->price_level];
103  }
104  }
105  elseif (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
106  {
107  require_once DOL_DOCUMENT_ROOT . '/product/class/productcustomerprice.class.php';
108 
109  $prodcustprice = new Productcustomerprice($db);
110 
111  $filter = array('t.fk_product' => $prod->id,'t.fk_soc' => $societe->id);
112 
113  $result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
114  if ($result >= 0)
115  {
116  if (count($prodcustprice->lines) > 0)
117  {
118  $pu_ht = price($prodcustprice->lines[0]->price);
119  $pu_ttc = price($prodcustprice->lines[0]->price_ttc);
120  $price_base_type = $prodcustprice->lines[0]->price_base_type;
121  $tva_tx = $prodcustprice->lines[0]->tva_tx;
122  if ($prodcustprice->lines[0]->default_vat_code && ! preg_match('/\(.*\)/', $tva_tx)) $tva_tx.= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
123  $tva_npr = $prodcustprice->lines[0]->recuperableonly;
124  if (empty($tva_tx)) $tva_npr=0;
125  }
126  }
127  else
128  {
129  setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
130  }
131  }
132 
133  $tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
134  $tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
135 
136  // if price ht is forced (ie: calculated by margin rate and cost price). TODO Why this ?
137  if (! empty($price_ht)) {
138  $pu_ht = price2num($price_ht, 'MU');
139  $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
140  }
141  // On reevalue prix selon taux tva car taux tva transaction peut etre different
142  // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
143  elseif ($tmpvat != $tmpprodvat) {
144  if ($price_base_type != 'HT') {
145  $pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
146  } else {
147  $pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
148  }
149  }
150 
151  $obj_facturation->id($ret['rowid']);
152  $obj_facturation->ref($ret['ref']);
153  $obj_facturation->stock($ret['reel']);
154  //$obj_facturation->prix($ret['price']);
155  $obj_facturation->prix($pu_ht);
156 
157 
158  $vatrate = $tva_tx;
159  $obj_facturation->vatrate = $vatrate; // Save vat rate (full text vat with code)
160 
161  // Definition du filtre pour n'afficher que le produit concerne
162  if ( $_POST['hdnSource'] == 'LISTE' )
163  {
164  $filtre = $ret['ref'];
165  }
166  else if ( $_POST['hdnSource'] == 'REF' )
167  {
168  $filtre = $_POST['txtRef'];
169  }
170 
171  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.$filtre;
172  }
173  else
174  {
175  $obj_facturation->raz();
176 
177  if ( $_POST['hdnSource'] == 'REF' )
178  {
179  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.$_POST['txtRef'];
180  }
181  else
182  {
183  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
184  }
185  }
186  }
187  else
188  {
189  dol_print_error($db);
190  }
191  }
192  else
193  {
194  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
195  }
196 
197  break;
198 
199  case 'change_thirdparty': // We have clicked on button "Modify" a thirdparty
200  $newthirdpartyid = GETPOST('CASHDESK_ID_THIRDPARTY','int');
201  if ($newthirdpartyid > 0)
202  {
203  $_SESSION["CASHDESK_ID_THIRDPARTY"] = $newthirdpartyid;
204  }
205 
206  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
207  break;
208 
209  case 'ajout_article': // We have clicked on button "Add product"
210 
211  if (! empty($obj_facturation->id)) // A product was previously selected and stored in session, so we can add it
212  {
213  dol_syslog("facturation_verif save vat ".$_POST['selTva']);
214  $obj_facturation->qte($_POST['txtQte']);
215  $obj_facturation->tva($_POST['selTva']); // id of vat. Saved so we can use it for next product
216  $obj_facturation->remisePercent($_POST['txtRemise']);
217  $obj_facturation->ajoutArticle(); // This add an entry into $_SESSION['poscart']
218  // We update prixTotalTtc
219  }
220 
221  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
222  break;
223 
224  case 'suppr_article':
225  $obj_facturation->supprArticle($_GET['suppr_id']);
226 
227  $redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
228  break;
229 }
230 
231 // We saved object obj_facturation
232 $_SESSION['serObjFacturation'] = serialize($obj_facturation);
233 //var_dump($_SESSION['serObjFacturation']);
234 header('Location: '.$redirection);
235 exit;
236 
File of class to manage predefined price products or services by customer.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
Class to manage products or services.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Fonction qui renvoie si tva doit etre tva percue recuperable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
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...
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) Si ...
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...