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
37if (empty($user->rights->cashdesk->run)) {
38	accessforbidden();
39}
40
41
42/*
43 * View
44 */
45
46switch ($action) {
47	default:
48		if (GETPOST('hdnSource') != 'NULL') {
49			$sql = "SELECT p.rowid, p.ref, p.price, p.tva_tx, p.default_vat_code, p.recuperableonly";
50			if (!empty($conf->stock->enabled) && !empty($conf_fkentrepot)) {
51				$sql .= ", ps.reel";
52			}
53			$sql .= " FROM ".MAIN_DB_PREFIX."product as p";
54			if (!empty($conf->stock->enabled) && !empty($conf_fkentrepot)) {
55				$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps ON p.rowid = ps.fk_product AND ps.fk_entrepot = ".((int) $conf_fkentrepot);
56			}
57			$sql .= " WHERE p.entity IN (".getEntity('product').")";
58
59			// Recuperation des donnees en fonction de la source (liste deroulante ou champ texte) ...
60			if ($_POST['hdnSource'] == 'LISTE') {
61				$sql .= " AND p.rowid = ".((int) GETPOST('selProduit', 'int'));
62			} elseif ($_POST['hdnSource'] == 'REF') {
63				$sql .= " AND p.ref = '".$db->escape(GETPOST('txtRef', 'alpha'))."'";
64			}
65
66			$result = $db->query($sql);
67			if ($result) {
68				// ... et enregistrement dans l'objet
69				if ($db->num_rows($result)) {
70					$ret = array();
71					$tab = $db->fetch_array($result);
72					foreach ($tab as $key => $value) {
73						$ret[$key] = $value;
74					}
75					// Here $ret['tva_tx'] is vat rate of product but we want to not use the one into table but found by function
76
77					$productid = $ret['rowid'];
78					$product = new Product($db);
79					$product->fetch($productid);
80					$prod = $product;
81
82					$thirdpartyid = $_SESSION['CASHDESK_ID_THIRDPARTY'];
83					$societe = new Societe($db);
84					$societe->fetch($thirdpartyid);
85
86					// Update if prices fields are defined
87					$tva_tx = get_default_tva($mysoc, $societe, $product->id);
88					$tva_npr = get_default_npr($mysoc, $societe, $product->id);
89					if (empty($tva_tx)) {
90						$tva_npr = 0;
91					}
92
93					$pu_ht = $prod->price;
94					$pu_ttc = $prod->price_ttc;
95					$price_min = $prod->price_min;
96					$price_base_type = $prod->price_base_type;
97
98					// multiprix
99					if (!empty($conf->global->PRODUIT_MULTIPRICES) && !empty($societe->price_level)) {
100						$pu_ht = $prod->multiprices[$societe->price_level];
101						$pu_ttc = $prod->multiprices_ttc[$societe->price_level];
102						$price_min = $prod->multiprices_min[$societe->price_level];
103						$price_base_type = $prod->multiprices_base_type[$societe->price_level];
104						if (!empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {  // using this option is a bug. kept for backward compatibility
105							if (isset($prod->multiprices_tva_tx[$societe->price_level])) {
106								$tva_tx = $prod->multiprices_tva_tx[$societe->price_level];
107							}
108							if (isset($prod->multiprices_recuperableonly[$societe->price_level])) {
109								$tva_npr = $prod->multiprices_recuperableonly[$societe->price_level];
110							}
111						}
112					} elseif (!empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
113						require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
114
115						$prodcustprice = new Productcustomerprice($db);
116
117						$filter = array('t.fk_product' => $prod->id, 't.fk_soc' => $societe->id);
118
119						$result = $prodcustprice->fetch_all('', '', 0, 0, $filter);
120						if ($result >= 0) {
121							if (count($prodcustprice->lines) > 0) {
122								$pu_ht = price($prodcustprice->lines[0]->price);
123								$pu_ttc = price($prodcustprice->lines[0]->price_ttc);
124								$price_base_type = $prodcustprice->lines[0]->price_base_type;
125								$tva_tx = $prodcustprice->lines[0]->tva_tx;
126								if ($prodcustprice->lines[0]->default_vat_code && !preg_match('/\(.*\)/', $tva_tx)) {
127									$tva_tx .= ' ('.$prodcustprice->lines[0]->default_vat_code.')';
128								}
129								$tva_npr = $prodcustprice->lines[0]->recuperableonly;
130								if (empty($tva_tx)) {
131									$tva_npr = 0;
132								}
133							}
134						} else {
135							setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
136						}
137					}
138
139					$tmpvat = price2num(preg_replace('/\s*\(.*\)/', '', $tva_tx));
140					$tmpprodvat = price2num(preg_replace('/\s*\(.*\)/', '', $prod->tva_tx));
141
142					// if price ht is forced (ie: calculated by margin rate and cost price). TODO Why this ?
143					if (!empty($price_ht)) {
144						$pu_ht = price2num($price_ht, 'MU');
145						$pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
146					} elseif ($tmpvat != $tmpprodvat) {
147						// On reevalue prix selon taux tva car taux tva transaction peut etre different
148						// de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur).
149						if ($price_base_type != 'HT') {
150							$pu_ht = price2num($pu_ttc / (1 + ($tmpvat / 100)), 'MU');
151						} else {
152							$pu_ttc = price2num($pu_ht * (1 + ($tmpvat / 100)), 'MU');
153						}
154					}
155
156					$obj_facturation->id($ret['rowid']);
157					$obj_facturation->ref($ret['ref']);
158					$obj_facturation->stock($ret['reel']);
159					//$obj_facturation->prix($ret['price']);
160					$obj_facturation->prix($pu_ht);
161
162
163					$vatrate = $tva_tx;
164					$obj_facturation->vatrate = $vatrate; // Save vat rate (full text vat with code)
165
166					// Definition du filtre pour n'afficher que le produit concerne
167					if (GETPOST('hdnSource') == 'LISTE') {
168						$filtre = $ret['ref'];
169					} elseif (GETPOST('hdnSource') == 'REF') {
170						$filtre = GETPOST('txtRef');
171					}
172
173					$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.urlencode($filtre);
174				} else {
175					$obj_facturation->raz();
176
177					if (GETPOST('hdnSource') == 'REF') {
178						$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation&filtre='.urlencode(GETPOST('txtRef'));
179					} else {
180						$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
181					}
182				}
183			} else {
184				dol_print_error($db);
185			}
186		} else {
187			$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
188		}
189
190		break;
191
192	case 'change_thirdparty':	// We have clicked on button "Modify" a thirdparty
193		$newthirdpartyid = GETPOST('CASHDESK_ID_THIRDPARTY', 'int');
194		if ($newthirdpartyid > 0) {
195			$_SESSION["CASHDESK_ID_THIRDPARTY"] = $newthirdpartyid;
196		}
197
198		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
199		break;
200
201	case 'ajout_article':
202		if (!empty($obj_facturation->id)) {	// A product was previously selected and stored in session, so we can add it
203			dol_syslog("facturation_verif save vat ".GETPOST('selTva'));
204			$obj_facturation->qte(GETPOST('txtQte'));
205			$obj_facturation->tva(GETPOST('selTva')); // id of vat. Saved so we can use it for next product
206			$obj_facturation->remisePercent(GETPOST('txtRemise'));
207			$obj_facturation->ajoutArticle(); // This add an entry into $_SESSION['poscart']
208			// We update prixTotalTtc
209		}
210
211		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
212		break;
213
214	case 'suppr_article':
215		$obj_facturation->supprArticle(GETPOST('suppr_id'));
216
217		$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menutpl=facturation';
218		break;
219}
220
221// We saved object obj_facturation
222$_SESSION['serObjFacturation'] = serialize($obj_facturation);
223//var_dump($_SESSION['serObjFacturation']);
224header('Location: '.$redirection);
225exit;
226