PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/fourn/class/fournisseur.product.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 503 lines | 331 code | 53 blank | 119 comment | 25 complexity | ad82630c33fb99b621d0ab06d01adb6a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/fourn/class/fournisseur.product.class.php
  23. * \ingroup produit
  24. * \brief File of class to manage predefined suppliers products
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  28. /**
  29. * \class ProductFournisseur
  30. * \brief Class to manage predefined suppliers products
  31. */
  32. class ProductFournisseur extends Product
  33. {
  34. var $db;
  35. var $error;
  36. var $product_fourn_price_id; // id of ligne product-supplier
  37. var $id; // product id
  38. var $fourn_ref; // ref supplier
  39. var $fourn_qty; // quantity for price
  40. var $fourn_price; // price for quantity
  41. var $fourn_remise_percent; // discount for quantity (percent)
  42. var $fourn_remise; // discount for quantity (amount)
  43. var $product_fourn_id; // supplier id
  44. var $fk_availability; // availability delay
  45. var $fourn_unitprice;
  46. /**
  47. * Constructor
  48. *
  49. * @param DoliDB $db Database handler
  50. */
  51. function __construct($db = '')
  52. {
  53. $this->db = $db;
  54. }
  55. /**
  56. * Remove all prices for this couple supplier-product
  57. *
  58. * @param int $id_fourn Supplier Id
  59. * @return int < 0 if error, > 0 if ok
  60. */
  61. function remove_fournisseur($id_fourn)
  62. {
  63. $ok=1;
  64. $this->db->begin();
  65. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  66. $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn;
  67. dol_syslog(get_class($this)."::remove_fournisseur sql=".$sql);
  68. $resql2=$this->db->query($sql);
  69. if (! $resql2)
  70. {
  71. $this->error=$this->db->lasterror();
  72. dol_syslog(get_class($this)."::remove_fournisseur ".$this->error, LOG_ERR);
  73. $ok=0;
  74. }
  75. if ($ok)
  76. {
  77. $this->db->commit();
  78. return 1;
  79. }
  80. else
  81. {
  82. $this->db->rollback();
  83. return -1;
  84. }
  85. }
  86. /**
  87. * Remove a price for a couple supplier-product
  88. *
  89. * @param int $rowid Line id of price
  90. * @return int <0 if KO, >0 if OK
  91. */
  92. function remove_product_fournisseur_price($rowid)
  93. {
  94. global $conf;
  95. $this->db->begin();
  96. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  97. $sql.= " WHERE rowid = ".$rowid;
  98. dol_syslog(get_class($this)."::remove_product_fournisseur_price sql=".$sql);
  99. $resql = $this->db->query($sql);
  100. if ($resql)
  101. {
  102. $this->db->commit();
  103. return 1;
  104. }
  105. else
  106. {
  107. $this->error=$this->db->lasterror();
  108. dol_syslog(get_class($this)."::remove_product_fournisseur_price ".$this->error,LOG_ERR);
  109. $this->db->rollback();
  110. return -1;
  111. }
  112. }
  113. /**
  114. * Modify the purchase price for a supplier
  115. *
  116. * @param int $qty Min quantity for which price is valid
  117. * @param float $buyprice Purchase price for the quantity min
  118. * @param User $user Object user user made changes
  119. * @param string $price_base_type HT or TTC
  120. * @param Societe $fourn Supplier
  121. * @param int $availability Product availability
  122. * @param string $ref_fourn Supplier ref
  123. * @param float $tva_tx VAT rate
  124. * @param string $charges costs affering to product
  125. * @param float $remise_percent Discount regarding qty (percent)
  126. * @param float $remise Discount regarding qty (amount)
  127. * @return int >0 if KO, >0 if OK
  128. */
  129. function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0)
  130. {
  131. global $conf,$mysoc;
  132. // Clean parameter
  133. if (empty($qty)) $qty=0;
  134. if (empty($buyprice)) $buyprice=0;
  135. if (empty($charges)) $charges=0;
  136. if (empty($availability)) $availability=0;
  137. if (empty($remise_percent)) $remise_percent=0;
  138. if ($price_base_type == 'TTC')
  139. {
  140. //$ttx = get_default_tva($fourn,$mysoc,$this->id); // We must use the VAT rate defined by user and not calculate it
  141. $ttx = $tva_tx;
  142. $buyprice = $buyprice/(1+($ttx/100));
  143. }
  144. $buyprice=price2num($buyprice,'MU');
  145. $charges=price2num($charges,'MU');
  146. $qty=price2num($qty);
  147. $error=0;
  148. $unitBuyPrice = price2num($buyprice/$qty,'MU');
  149. $unitCharges = price2num($charges/$qty,'MU');
  150. $now=dol_now();
  151. $this->db->begin();
  152. if ($this->product_fourn_price_id)
  153. {
  154. $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
  155. $sql.= " SET fk_user = " . $user->id." ,";
  156. $sql.= " price = ".price2num($buyprice).",";
  157. $sql.= " quantity = ".$qty.",";
  158. $sql.= " remise_percent = ".$remise_percent.",";
  159. $sql.= " remise = ".$remise.",";
  160. $sql.= " unitprice = ".$unitBuyPrice.",";
  161. $sql.= " unitcharges = ".$unitCharges.",";
  162. $sql.= " tva_tx = ".$tva_tx.",";
  163. $sql.= " fk_availability = ".$availability.",";
  164. $sql.= " entity = ".$conf->entity.",";
  165. $sql.= " charges = ".($charges != ''?price2num($charges):"null");
  166. $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
  167. // TODO Add price_base_type and price_ttc
  168. dol_syslog(get_class($this).'::update_buyprice sql='.$sql);
  169. $resql = $this->db->query($sql);
  170. if ($resql)
  171. {
  172. $this->db->commit();
  173. return 0;
  174. }
  175. else
  176. {
  177. $this->error=$this->db->error()." sql=".$sql;
  178. $this->db->rollback();
  179. return -2;
  180. }
  181. }
  182. else
  183. {
  184. // Delete price for this quantity
  185. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  186. $sql.= " WHERE fk_soc = ".$fourn->id." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".$qty." AND entity = ".$conf->entity;
  187. dol_syslog(get_class($this).'::update_buyprice sql='.$sql);
  188. $resql=$this->db->query($sql);
  189. if ($resql)
  190. {
  191. // Add price for this quantity to supplier
  192. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
  193. $sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, fk_availability, entity)";
  194. $sql.= " values('".$this->db->idate($now)."',";
  195. $sql.= " ".$this->id.",";
  196. $sql.= " ".$fourn->id.",";
  197. $sql.= " '".$this->db->escape($ref_fourn)."',";
  198. $sql.= " ".$user->id.",";
  199. $sql.= " ".price2num($buyprice).",";
  200. $sql.= " ".$qty.",";
  201. $sql.= " ".$remise_percent.",";
  202. $sql.= " ".$remise.",";
  203. $sql.= " ".$unitBuyPrice.",";
  204. $sql.= " ".$tva_tx.",";
  205. $sql.= " ".$availability.",";
  206. $sql.= $conf->entity;
  207. $sql.=")";
  208. dol_syslog(get_class($this)."::update_buyprice sql=".$sql);
  209. if (! $this->db->query($sql))
  210. {
  211. $error++;
  212. }
  213. /*if (! $error)
  214. {
  215. // Ajoute modif dans table log
  216. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
  217. $sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)";
  218. $sql.= "values('".$this->db->idate($now)."',";
  219. $sql.= " ".$this->product_fourn_id.",";
  220. $sql.= " ".$user->id.",";
  221. $sql.= " ".price2num($buyprice).",";
  222. $sql.= " ".$qty;
  223. $sql.=")";
  224. $resql=$this->db->query($sql);
  225. if (! $resql)
  226. {
  227. $error++;
  228. }
  229. }
  230. */
  231. if (! $error)
  232. {
  233. $this->db->commit();
  234. return 0;
  235. }
  236. else
  237. {
  238. $this->error=$this->db->error()." sql=".$sql;
  239. $this->db->rollback();
  240. return -2;
  241. }
  242. }
  243. else
  244. {
  245. $this->error=$this->db->error()." sql=".$sql;
  246. $this->db->rollback();
  247. return -1;
  248. }
  249. }
  250. }
  251. /**
  252. * Loads the price information of a provider
  253. *
  254. * @param int $rowid Line id
  255. * @return int < 0 if KO, 0 if OK but not found, > 0 if OK
  256. */
  257. function fetch_product_fournisseur_price($rowid)
  258. {
  259. $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability,";
  260. $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges";
  261. $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
  262. $sql.= " WHERE pfp.rowid = ".$rowid;
  263. dol_syslog(get_class($this)."::fetch_product_fournisseur_price sql=".$sql, LOG_DEBUG);
  264. $resql = $this->db->query($sql);
  265. if ($resql)
  266. {
  267. $obj = $this->db->fetch_object($resql);
  268. if ($obj)
  269. {
  270. $this->product_fourn_price_id = $rowid;
  271. $this->fourn_ref = $obj->ref_fourn;
  272. $this->fourn_price = $obj->price;
  273. $this->fourn_charges = $obj->charges;
  274. $this->fourn_qty = $obj->quantity;
  275. $this->fourn_remise_percent = $obj->remise_percent;
  276. $this->fourn_remise = $obj->remise;
  277. $this->fourn_unitprice = $obj->unitprice;
  278. $this->fourn_unitcharges = $obj->unitcharges;
  279. $this->tva_tx = $obj->tva_tx;
  280. $this->product_id = $obj->fk_product; // deprecated
  281. $this->fk_product = $obj->fk_product;
  282. $this->fk_availability = $obj->fk_availability;
  283. return 1;
  284. }
  285. else
  286. {
  287. return 0;
  288. }
  289. }
  290. else
  291. {
  292. $this->error=$this->db->error();
  293. dol_syslog(get_class($this)."::fetch_product_fournisseur_price error=".$this->error, LOG_ERR);
  294. return -1;
  295. }
  296. }
  297. /**
  298. * List all supplier prices of a product
  299. *
  300. * @param int $prodid Id of product
  301. * @param string $sortfield Sort field
  302. * @param string $sortorder Sort order
  303. * @return array Array of Products with new properties to define supplier price
  304. */
  305. function list_product_fournisseur_price($prodid, $sortfield='', $sortorder='')
  306. {
  307. global $conf;
  308. $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
  309. $sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn, pfp.fk_product as product_fourn_id,";
  310. $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.unitcharges";
  311. $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
  312. $sql.= ", ".MAIN_DB_PREFIX."societe as s";
  313. $sql.= " WHERE pfp.entity IN (".getEntity('product', 1).")";
  314. $sql.= " AND pfp.fk_soc = s.rowid";
  315. $sql.= " AND pfp.fk_product = ".$prodid;
  316. if (empty($sortfield)) $sql.= " ORDER BY s.nom, pfp.quantity, pfp.price";
  317. else $sql.= $this->db->order($sortfield,$sortorder);
  318. dol_syslog(get_class($this)."::list_product_fournisseur_price sql=".$sql, LOG_DEBUG);
  319. $resql = $this->db->query($sql);
  320. if ($resql)
  321. {
  322. $retarray = array();
  323. while ($record = $this->db->fetch_array($resql))
  324. {
  325. //define base attribute
  326. $prodfourn = new ProductFournisseur($this->db);
  327. $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
  328. $prodfourn->product_fourn_id = $record["product_fourn_id"];
  329. $prodfourn->fourn_ref = $record["ref_fourn"];
  330. $prodfourn->fourn_price = $record["price"];
  331. $prodfourn->fourn_qty = $record["quantity"];
  332. $prodfourn->fourn_remise_percent = $record["remise_percent"];
  333. $prodfourn->fourn_remise = $record["remise"];
  334. $prodfourn->fourn_unitprice = $record["unitprice"];
  335. $prodfourn->fourn_charges = $record["charges"];
  336. $prodfourn->fourn_unitcharges = $record["unitcharges"];
  337. $prodfourn->fourn_tva_tx = $record["tva_tx"];
  338. $prodfourn->fourn_id = $record["fourn_id"];
  339. $prodfourn->fourn_name = $record["supplier_name"];
  340. $prodfourn->fk_availability = $record["fk_availability"];
  341. $prodfourn->id = $prodid;
  342. if (!isset($prodfourn->fourn_unitprice))
  343. {
  344. if ($prodfourn->fourn_qty!=0)
  345. {
  346. $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price/$prodfourn->fourn_qty,'MU');
  347. }
  348. else
  349. {
  350. $prodfourn->fourn_unitprice="";
  351. }
  352. }
  353. $retarray[]=$prodfourn;
  354. }
  355. $this->db->free($resql);
  356. return $retarray;
  357. }
  358. else
  359. {
  360. $this->error=$this->db->error();
  361. dol_syslog(get_class($this)."::list_product_fournisseur_price error=".$this->error, LOG_ERR);
  362. return -1;
  363. }
  364. }
  365. /**
  366. * Load properties for minimum price
  367. *
  368. * @param int $prodid Product id
  369. * @return int <0 if KO, >0 if OK
  370. */
  371. function find_min_price_product_fournisseur($prodid)
  372. {
  373. global $conf;
  374. $this->product_fourn_price_id = '';
  375. $this->product_fourn_id = '';
  376. $this->fourn_ref = '';
  377. $this->fourn_price = '';
  378. $this->fourn_qty = '';
  379. $this->fourn_remise_percent = '';
  380. $this->fourn_remise = '';
  381. $this->fourn_unitprice = '';
  382. $this->fourn_id = '';
  383. $this->fourn_name = '';
  384. $this->id = '';
  385. $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
  386. $sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
  387. $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges, pfp.unitcharges";
  388. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
  389. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  390. $sql.= " AND pfp.fk_product = ".$prodid;
  391. $sql.= " AND pfp.fk_soc = s.rowid";
  392. $sql.= " ORDER BY pfp.unitprice";
  393. $sql.= $this->db->plimit(1);
  394. dol_syslog(get_class($this)."::find_min_price_product_fournisseur sql=".$sql, LOG_DEBUG);
  395. $resql = $this->db->query($sql);
  396. if ($resql)
  397. {
  398. $record = $this->db->fetch_array($resql);
  399. $this->product_fourn_price_id = $record["product_fourn_price_id"];
  400. $this->fourn_ref = $record["ref_fourn"];
  401. $this->fourn_price = $record["price"];
  402. $this->fourn_qty = $record["quantity"];
  403. $this->fourn_remise_percent = $record["remise_percent"];
  404. $this->fourn_remise = $record["remise"];
  405. $this->fourn_unitprice = $record["unitprice"];
  406. $this->fourn_charges = $record["charges"];
  407. $this->fourn_unitcharges = $record["unitcharges"];
  408. $this->fourn_tva_tx = $record["tva_tx"];
  409. $this->fourn_id = $record["fourn_id"];
  410. $this->fourn_name = $record["supplier_name"];
  411. $this->id = $prodid;
  412. $this->db->free($resql);
  413. return 1;
  414. }
  415. else
  416. {
  417. $this->error=$this->db->error();
  418. dol_syslog(get_class($this)."::find_min_price_product_fournisseur error=".$this->error, LOG_ERR);
  419. return -1;
  420. }
  421. }
  422. /**
  423. * Display supplier of product
  424. *
  425. * @param int $withpicto Add picto
  426. * @param string $option Target of link ('', 'customer', 'prospect', 'supplier')
  427. * @return string String with supplier price
  428. * TODO Remove this method. Use getNomUrl directly.
  429. */
  430. function getSocNomUrl($withpicto=0,$option='supplier')
  431. {
  432. $cust = new Fournisseur($this->db);
  433. $cust->fetch($this->fourn_id);
  434. return $cust->getNomUrl($withpicto,$option);
  435. }
  436. /**
  437. * Display price of product
  438. *
  439. * @return string String with supplier price
  440. */
  441. function display_price_product_fournisseur()
  442. {
  443. global $langs;
  444. $langs->load("suppliers");
  445. $out=price($this->fourn_unitprice).' '.$langs->trans("HT").' &nbsp; ('.$langs->trans("Supplier").': '.$this->getSocNomUrl(1).' / '.$langs->trans("SupplierRef").': '.$this->fourn_ref.')';
  446. return $out;
  447. }
  448. }
  449. ?>