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 <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *	\file       htdocs/cashdesk/facturation_verif.php
22 *	\ingroup    cashdesk
23 *	\brief      facturation_verif.php
24 */
25
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php';
28require_once DOL_DOCUMENT_ROOT.'/cashdesk/class/Facturation.class.php';
29require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31
32$action = GETPOST('action', 'aZ09');
33
34$obj_facturation = unserialize($_SESSION['serObjFacturation']);
35unset($_SESSION['serObjFacturation']);
36
37
38switch ($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 = ".((int) GETPOST('selProduit', 'int'));
53			} elseif ($_POST['hdnSource'] == 'REF')
54			{
55				$sql .= " AND p.ref = '".$db->escape(GETPOST('txtRef', 'alpha'))."'";
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					} elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES))
104					{
105						require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
106
107						$prodcustprice = new Productcustomerprice($db);
108
109						$filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $societe->id);
110
111						$result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
112						if ($result >= 0)
113						{
114							if (count($prodcustprice->lines) > 0)
115							{
116								$pu_ht = price($prodcustprice->lines[0]->price);
117								$pu_ttc = price($prodcustprice->lines[0]->price_ttc);
118								$price_base_type = $prodcustprice->lines[0]->price_base_type;
119								$tva_tx = $prodcustprice->lines[0]->tva_tx;
120								if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) $tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
121								$tva_npr = $prodcustprice->lines[0]->recuperableonly;
122								if (empty($tva_tx)) $tva_npr = 0;
123							}
124						} else {
125							setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
126						}
127					}
128
129					$tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
130					$tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
131
132					// if price ht is forced (ie: calculated by margin rate and cost price). TODO Why this ?
133					if (!empty($price_ht)) {
134						$pu_ht = price2num($price_ht, 'MU');
135						$pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
136					}
137					// On reevalue prix selon taux tva car taux tva transaction peut etre different
138					// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
139					elseif ($tmpvat != $tmpprodvat) {
140						if ($price_base_type != 'HT') {
141							$pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
142						} else {
143							$pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
144						}
145					}
146
147					$obj_facturation->id($ret['rowid']);
148					$obj_facturation->ref($ret['ref']);
149					$obj_facturation->stock($ret['reel']);
150					//$obj_facturation->prix($ret['price']);
151					$obj_facturation->prix($pu_ht);
152
153
154					$vatrate = $tva_tx;
155					$obj_facturation->vatrate = $vatrate; // Save vat rate (full text vat with code)
156
157					// Definition du filtre pour n'afficher que le produit concerne
158					if ($_POST['hdnSource'] == 'LISTE')
159					{
160						$filtre = $ret['ref'];
161					} elseif ($_POST['hdnSource'] == 'REF')
162					{
163						$filtre = $_POST['txtRef'];
164					}
165
166					$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.$filtre;
167				} else {
168					$obj_facturation->raz();
169
170					if ($_POST['hdnSource'] == 'REF')
171					{
172						$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.$_POST['txtRef'];
173					} else {
174						$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
175					}
176				}
177			} else {
178				dol_print_error($db);
179			}
180		} else {
181			$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
182		}
183
184		break;
185
186	case 'change_thirdparty':	// We have clicked on button "Modify" a thirdparty
187		$newthirdpartyid = GETPOST('CASHDESK_ID_THIRDPARTY', 'int');
188		if ($newthirdpartyid > 0)
189		{
190			$_SESSION["CASHDESK_ID_THIRDPARTY"] = $newthirdpartyid;
191		}
192
193		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
194		break;
195
196	case 'ajout_article':	// We have clicked on button "Add product"
197
198		if (!empty($obj_facturation->id))	// A product was previously selected and stored in session, so we can add it
199		{
200			dol_syslog("facturation_verif save vat ".$_POST['selTva']);
201			$obj_facturation->qte($_POST['txtQte']);
202			$obj_facturation->tva($_POST['selTva']); // id of vat. Saved so we can use it for next product
203			$obj_facturation->remisePercent($_POST['txtRemise']);
204			$obj_facturation->ajoutArticle(); // This add an entry into $_SESSION['poscart']
205			// We update prixTotalTtc
206		}
207
208		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
209		break;
210
211	case 'suppr_article':
212		$obj_facturation->supprArticle($_GET['suppr_id']);
213
214		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
215		break;
216}
217
218// We saved object obj_facturation
219$_SESSION['serObjFacturation'] = serialize($obj_facturation);
220//var_dump($_SESSION['serObjFacturation']);
221header('Location: '.$redirection);
222exit;
223