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