facturation_verif.php 8.5 KB

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