fournisseur.product.class.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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-2014 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  8. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  9. * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
  10. * Copyright (C) 2020 Pierre Ardoin <mapiolca@me.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/fourn/class/fournisseur.product.class.php
  27. * \ingroup produit
  28. * \brief File of class to manage predefined suppliers products
  29. */
  30. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/product/class/productfournisseurprice.class.php';
  33. /**
  34. * Class to manage predefined suppliers products
  35. */
  36. class ProductFournisseur extends Product
  37. {
  38. /**
  39. * @var DoliDB Database handler.
  40. */
  41. public $db;
  42. /**
  43. * @var string Error code (or message)
  44. */
  45. public $error = '';
  46. public $product_fourn_price_id; // id of ligne product-supplier
  47. /**
  48. * @var int ID
  49. */
  50. public $id;
  51. /**
  52. * @deprecated
  53. * @see $ref_supplier
  54. */
  55. public $fourn_ref;
  56. public $delivery_time_days;
  57. public $ref_supplier; // ref supplier (can be set by get_buyprice)
  58. public $desc_supplier;
  59. public $vatrate_supplier; // default vat rate for this supplier/qty/product (can be set by get_buyprice)
  60. public $product_id;
  61. public $product_ref;
  62. public $fourn_id; //supplier id
  63. public $fourn_qty; // quantity for price (can be set by get_buyprice)
  64. public $fourn_pu; // unit price for quantity (can be set by get_buyprice)
  65. public $fourn_price; // price for quantity
  66. public $fourn_remise_percent; // discount for quantity (percent)
  67. public $fourn_remise; // discount for quantity (amount)
  68. public $product_fourn_id; // product-supplier id
  69. public $product_fourn_entity;
  70. /**
  71. * @var int ID user_id - user who created/updated supplier price
  72. */
  73. public $user_id;
  74. /**
  75. * @var int ID availability delay - visible/used if option FOURN_PRODUCT_AVAILABILITY is on (duplicate information compared to delivery delay)
  76. */
  77. public $fk_availability;
  78. public $fourn_unitprice;
  79. public $fourn_unitprice_with_discount; // not saved into database
  80. public $fourn_tva_tx;
  81. public $fourn_tva_npr;
  82. /**
  83. * @var int ID
  84. */
  85. public $fk_supplier_price_expression;
  86. public $supplier_reputation; // reputation of supplier
  87. public $reputations = array(); // list of available supplier reputations
  88. // Multicurreny
  89. public $fourn_multicurrency_id;
  90. public $fourn_multicurrency_code;
  91. public $fourn_multicurrency_tx;
  92. public $fourn_multicurrency_price;
  93. public $fourn_multicurrency_unitprice;
  94. /**
  95. * @deprecated
  96. * @see $supplier_barcode
  97. */
  98. public $fourn_barcode;
  99. /**
  100. * @var string $supplier_barcode - Supplier barcode
  101. */
  102. public $supplier_barcode;
  103. /**
  104. * @deprecated
  105. * @see $supplier_fk_barcode_type
  106. */
  107. public $fourn_fk_barcode_type;
  108. /**
  109. * @var string $supplier_fk_barcode_type - Supplier barcode type
  110. */
  111. public $supplier_fk_barcode_type;
  112. public $packaging;
  113. /**
  114. * Constructor
  115. *
  116. * @param DoliDB $db Database handler
  117. */
  118. public function __construct($db)
  119. {
  120. global $langs;
  121. $this->db = $db;
  122. $langs->load("suppliers");
  123. $this->reputations = array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'), 'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
  124. }
  125. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  126. /**
  127. * Remove all prices for this couple supplier-product
  128. *
  129. * @param int $id_fourn Supplier Id
  130. * @return int < 0 if error, > 0 if ok
  131. */
  132. public function remove_fournisseur($id_fourn)
  133. {
  134. // phpcs:enable
  135. $ok = 1;
  136. $this->db->begin();
  137. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  138. $sql .= " WHERE fk_product = ".((int) $this->id)." AND fk_soc = ".((int) $id_fourn);
  139. dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
  140. $resql2 = $this->db->query($sql);
  141. if (!$resql2) {
  142. $this->error = $this->db->lasterror();
  143. $ok = 0;
  144. }
  145. if ($ok) {
  146. $this->db->commit();
  147. return 1;
  148. } else {
  149. $this->db->rollback();
  150. return -1;
  151. }
  152. }
  153. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  154. /**
  155. * Remove a price for a couple supplier-product
  156. *
  157. * @param int $rowid Line id of price
  158. * @return int <0 if KO, >0 if OK
  159. */
  160. public function remove_product_fournisseur_price($rowid)
  161. {
  162. // phpcs:enable
  163. global $conf, $user;
  164. $error = 0;
  165. $this->db->begin();
  166. // Call trigger
  167. $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE', $user);
  168. if ($result < 0) {
  169. $error++;
  170. }
  171. // End call triggers
  172. if (empty($error)) {
  173. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  174. $sql .= " WHERE rowid = ".((int) $rowid);
  175. dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
  176. $resql = $this->db->query($sql);
  177. if (!$resql) {
  178. $this->error = $this->db->lasterror();
  179. $error++;
  180. }
  181. }
  182. if (empty($error)) {
  183. $this->db->commit();
  184. return 1;
  185. } else {
  186. $this->db->rollback();
  187. return -1;
  188. }
  189. }
  190. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  191. /**
  192. * Modify the purchase price for a supplier
  193. *
  194. * @param float $qty Min quantity for which price is valid
  195. * @param float $buyprice Purchase price for the quantity min
  196. * @param User $user Object user user made changes
  197. * @param string $price_base_type HT or TTC
  198. * @param Societe $fourn Supplier
  199. * @param int $availability Product availability
  200. * @param string $ref_fourn Supplier ref
  201. * @param float $tva_tx New VAT Rate (For example 8.5. Should not be a string)
  202. * @param string $charges costs affering to product
  203. * @param float $remise_percent Discount regarding qty (percent)
  204. * @param float $remise Discount regarding qty (amount)
  205. * @param int $newnpr Set NPR or not
  206. * @param int $delivery_time_days Delay in days for delivery (max). May be '' if not defined.
  207. * @param string $supplier_reputation Reputation with this product to the defined supplier (empty, FAVORITE, DONOTORDER)
  208. * @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
  209. * @param string $newdefaultvatcode Default vat code
  210. * @param float $multicurrency_buyprice Purchase price for the quantity min in currency
  211. * @param string $multicurrency_price_base_type HT or TTC in currency
  212. * @param float $multicurrency_tx Rate currency
  213. * @param string $multicurrency_code Currency code
  214. * @param string $desc_fourn Custom description for product_fourn_price
  215. * @param string $barcode Barcode
  216. * @param int $fk_barcode_type Barcode type
  217. * @param array $options Extrafields of product fourn price
  218. * @return int <0 if KO, >=0 if OK
  219. */
  220. public function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges = 0, $remise_percent = 0, $remise = 0, $newnpr = 0, $delivery_time_days = 0, $supplier_reputation = '', $localtaxes_array = array(), $newdefaultvatcode = '', $multicurrency_buyprice = 0, $multicurrency_price_base_type = 'HT', $multicurrency_tx = 1, $multicurrency_code = '', $desc_fourn = '', $barcode = '', $fk_barcode_type = '', $options = array())
  221. {
  222. // phpcs:enable
  223. global $conf, $langs;
  224. //global $mysoc;
  225. // Clean parameter
  226. if (empty($qty)) {
  227. $qty = 0;
  228. }
  229. if (empty($buyprice)) {
  230. $buyprice = 0;
  231. }
  232. if (empty($charges)) {
  233. $charges = 0;
  234. }
  235. if (empty($availability)) {
  236. $availability = 0;
  237. }
  238. if (empty($remise_percent)) {
  239. $remise_percent = 0;
  240. }
  241. if (empty($supplier_reputation) || $supplier_reputation == -1) {
  242. $supplier_reputation = '';
  243. }
  244. if ($delivery_time_days != '' && !is_numeric($delivery_time_days)) {
  245. $delivery_time_days = '';
  246. }
  247. if ($price_base_type == 'TTC') {
  248. $ttx = $tva_tx;
  249. $buyprice = $buyprice / (1 + ($ttx / 100));
  250. }
  251. // Multicurrency
  252. $multicurrency_unitBuyPrice = null;
  253. $fk_multicurrency = null;
  254. if (isModEnabled("multicurrency")) {
  255. if (empty($multicurrency_tx)) {
  256. $multicurrency_tx = 1;
  257. }
  258. if (empty($multicurrency_buyprice)) {
  259. $multicurrency_buyprice = 0;
  260. }
  261. if ($multicurrency_price_base_type == 'TTC') {
  262. $ttx = $tva_tx;
  263. $multicurrency_buyprice = $multicurrency_buyprice / (1 + ($ttx / 100));
  264. }
  265. $multicurrency_buyprice = price2num($multicurrency_buyprice, 'MU');
  266. $multicurrency_unitBuyPrice = price2num($multicurrency_buyprice / $qty, 'MU');
  267. $buyprice = $multicurrency_buyprice / $multicurrency_tx;
  268. $fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $multicurrency_code);
  269. }
  270. $buyprice = price2num($buyprice, 'MU');
  271. $charges = price2num($charges, 'MU');
  272. $qty = price2num($qty, 'MS');
  273. $unitBuyPrice = price2num($buyprice / $qty, 'MU');
  274. // We can have a puchase ref that need to buy 100 min for a given price and with a packaging of 50.
  275. //$packaging = price2num(((empty($this->packaging) || $this->packaging < $qty) ? $qty : $this->packaging), 'MS');
  276. $packaging = price2num((empty($this->packaging) ? $qty : $this->packaging), 'MS');
  277. $error = 0;
  278. $now = dol_now();
  279. $newvat = $tva_tx;
  280. if (count($localtaxes_array) > 0) {
  281. $localtaxtype1 = $localtaxes_array['0'];
  282. $localtax1 = $localtaxes_array['1'];
  283. $localtaxtype2 = $localtaxes_array['2'];
  284. $localtax2 = $localtaxes_array['3'];
  285. } else { // old method. deprecated because ot can't retrieve type
  286. $localtaxtype1 = '0';
  287. $localtax1 = get_localtax($newvat, 1);
  288. $localtaxtype2 = '0';
  289. $localtax2 = get_localtax($newvat, 2);
  290. }
  291. if (empty($localtax1)) {
  292. $localtax1 = 0; // If = '' then = 0
  293. }
  294. if (empty($localtax2)) {
  295. $localtax2 = 0; // If = '' then = 0
  296. }
  297. // Check parameters
  298. if ($buyprice != '' && !is_numeric($buyprice)) {
  299. }
  300. $this->db->begin();
  301. if ($this->product_fourn_price_id > 0) {
  302. // check if price already logged, if not first log current price
  303. $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id);
  304. if (is_array($logPrices) && count($logPrices) == 0) {
  305. $currentPfp = new self($this->db);
  306. $result = $currentPfp->fetch_product_fournisseur_price($this->product_fourn_price_id);
  307. if ($result > 0 && $currentPfp->fourn_price != 0) {
  308. $currentPfpUser = new User($this->db);
  309. $result = $currentPfpUser->fetch($currentPfp->user_id);
  310. if ($result > 0) {
  311. $currentPfp->logPrice(
  312. $currentPfpUser,
  313. $currentPfp->date_creation,
  314. $currentPfp->fourn_price,
  315. $currentPfp->fourn_qty,
  316. $currentPfp->fourn_multicurrency_price,
  317. $currentPfp->fourn_multicurrency_unitprice,
  318. $currentPfp->fourn_multicurrency_tx,
  319. $currentPfp->fourn_multicurrency_id,
  320. $currentPfp->fourn_multicurrency_code
  321. );
  322. }
  323. }
  324. }
  325. $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
  326. $sql .= " SET fk_user = ".((int) $user->id)." ,";
  327. $sql .= " ref_fourn = '".$this->db->escape($ref_fourn)."',";
  328. $sql .= " desc_fourn = '".$this->db->escape($desc_fourn)."',";
  329. $sql .= " price = ".((float) $buyprice).",";
  330. $sql .= " quantity = ".((float) $qty).",";
  331. $sql .= " remise_percent = ".((float) $remise_percent).",";
  332. $sql .= " remise = ".((float) $remise).",";
  333. $sql .= " unitprice = ".((float) $unitBuyPrice).",";
  334. $sql .= " fk_availability = ".((int) $availability).",";
  335. $sql .= " multicurrency_price = ".(isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
  336. $sql .= " multicurrency_unitprice = ".(isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
  337. $sql .= " multicurrency_tx = ".(isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
  338. $sql .= " fk_multicurrency = ".(isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
  339. $sql .= " multicurrency_code = ".(isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
  340. $sql .= " entity = ".$conf->entity.",";
  341. $sql .= " tva_tx = ".price2num($tva_tx).",";
  342. // TODO Add localtax1 and localtax2
  343. //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
  344. //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
  345. //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$this->db->escape($localtaxtype1)."'":"'0'").",";
  346. //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$this->db->escape($localtaxtype2)."'":"'0'").",";
  347. $sql .= " default_vat_code=".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
  348. $sql .= " info_bits = ".((int) $newnpr).",";
  349. $sql .= " charges = ".((float) $charges).","; // deprecated
  350. $sql .= " delivery_time_days = ".($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
  351. $sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
  352. $sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
  353. $sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
  354. if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
  355. $sql .= ", packaging = ".(empty($packaging) ? 1 : $packaging);
  356. }
  357. $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
  358. if (!$error) {
  359. if (!empty($options) && is_array($options)) {
  360. $productfournisseurprice = new ProductFournisseurPrice($this->db);
  361. $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
  362. if ($res > 0) {
  363. foreach ($options as $key=>$value) {
  364. $productfournisseurprice->array_options[$key] = $value;
  365. }
  366. $res = $productfournisseurprice->update($user);
  367. if ($res < 0) {
  368. $this->error = $productfournisseurprice->error;
  369. $this->errors = $productfournisseurprice->errors;
  370. $error++;
  371. }
  372. }
  373. }
  374. }
  375. // TODO Add price_base_type and price_ttc
  376. dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
  377. $resql = $this->db->query($sql);
  378. if ($resql) {
  379. // Call trigger
  380. $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_MODIFY', $user);
  381. if ($result < 0) {
  382. $error++;
  383. }
  384. // End call triggers
  385. if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
  386. $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
  387. if ($result < 0) {
  388. $error++;
  389. }
  390. }
  391. if (empty($error)) {
  392. $this->db->commit();
  393. return $this->product_fourn_price_id;
  394. } else {
  395. $this->db->rollback();
  396. return -1;
  397. }
  398. } else {
  399. $this->error = $this->db->error()." sql=".$sql;
  400. $this->db->rollback();
  401. return -2;
  402. }
  403. } else {
  404. dol_syslog(get_class($this).'::update_buyprice without knowing id of line, so we delete from company, quantity and supplier_ref and insert again', LOG_DEBUG);
  405. // Delete price for this quantity
  406. $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
  407. $sql .= " WHERE fk_soc = ".((int) $fourn->id)." AND ref_fourn = '".$this->db->escape($ref_fourn)."' AND quantity = ".((float) $qty)." AND entity = ".((int) $conf->entity);
  408. $resql = $this->db->query($sql);
  409. if ($resql) {
  410. // Add price for this quantity to supplier
  411. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
  412. $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
  413. $sql .= "datec, fk_product, fk_soc, ref_fourn, desc_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, fk_availability, default_vat_code, info_bits, entity, delivery_time_days, supplier_reputation, barcode, fk_barcode_type";
  414. if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
  415. $sql .= ", packaging";
  416. }
  417. $sql .= ") values(";
  418. $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
  419. $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
  420. $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
  421. $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
  422. $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
  423. $sql .= " '".$this->db->idate($now)."',";
  424. $sql .= " ".((int) $this->id).",";
  425. $sql .= " ".((int) $fourn->id).",";
  426. $sql .= " '".$this->db->escape($ref_fourn)."',";
  427. $sql .= " '".$this->db->escape($desc_fourn)."',";
  428. $sql .= " ".((int) $user->id).",";
  429. $sql .= " ".price2num($buyprice).",";
  430. $sql .= " ".((float) $qty).",";
  431. $sql .= " ".((float) $remise_percent).",";
  432. $sql .= " ".((float) $remise).",";
  433. $sql .= " ".price2num($unitBuyPrice).",";
  434. $sql .= " ".price2num($tva_tx).",";
  435. $sql .= " ".price2num($charges).",";
  436. $sql .= " ".((int) $availability).",";
  437. $sql .= " ".($newdefaultvatcode ? "'".$this->db->escape($newdefaultvatcode)."'" : "null").",";
  438. $sql .= " ".((int) $newnpr).",";
  439. $sql .= $conf->entity.",";
  440. $sql .= ($delivery_time_days != '' ? ((int) $delivery_time_days) : 'null').",";
  441. $sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
  442. $sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
  443. $sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
  444. if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
  445. $sql .= ", ".(empty($this->packaging) ? '1' : "'".$this->db->escape($this->packaging)."'");
  446. }
  447. $sql .= ")";
  448. $this->product_fourn_price_id = 0;
  449. $resql = $this->db->query($sql);
  450. if ($resql) {
  451. $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
  452. } else {
  453. $this->error = $this->db->lasterror();
  454. $error++;
  455. }
  456. if (!$error) {
  457. if (!empty($options) && is_array($options)) {
  458. $productfournisseurprice = new ProductFournisseurPrice($this->db);
  459. $res = $productfournisseurprice->fetch($this->product_fourn_price_id);
  460. if ($res > 0) {
  461. foreach ($options as $key=>$value) {
  462. $productfournisseurprice->array_options[$key] = $value;
  463. }
  464. $res = $productfournisseurprice->update($user);
  465. if ($res < 0) {
  466. $this->error = $productfournisseurprice->error;
  467. $this->errors = $productfournisseurprice->errors;
  468. $error++;
  469. }
  470. }
  471. }
  472. }
  473. if (!$error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
  474. // Add record into log table
  475. // $this->product_fourn_price_id must be set
  476. $result = $this->logPrice($user, $now, $buyprice, $qty, $multicurrency_buyprice, $multicurrency_unitBuyPrice, $multicurrency_tx, $fk_multicurrency, $multicurrency_code);
  477. if ($result < 0) {
  478. $error++;
  479. }
  480. }
  481. if (!$error) {
  482. // Call trigger
  483. $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
  484. if ($result < 0) {
  485. $error++;
  486. }
  487. // End call triggers
  488. if (empty($error)) {
  489. $this->db->commit();
  490. return $this->product_fourn_price_id;
  491. } else {
  492. $this->db->rollback();
  493. return -1;
  494. }
  495. } else {
  496. $this->error = $this->db->lasterror()." sql=".$sql;
  497. $this->db->rollback();
  498. return -2;
  499. }
  500. } else {
  501. $this->error = $this->db->lasterror()." sql=".$sql;
  502. $this->db->rollback();
  503. return -1;
  504. }
  505. }
  506. }
  507. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  508. /**
  509. * Loads the price information of a provider
  510. *
  511. * @param int $rowid Line id
  512. * @param int $ignore_expression Ignores the math expression for calculating price and uses the db value instead
  513. * @return int < 0 if KO, 0 if OK but not found, > 0 if OK
  514. */
  515. public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
  516. {
  517. // phpcs:enable
  518. global $conf;
  519. $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.default_vat_code, pfp.info_bits as fourn_tva_npr, pfp.fk_availability,";
  520. $sql .= " pfp.fk_soc, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product, pfp.charges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,";
  521. $sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
  522. $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
  523. $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging,";
  524. $sql .= " p.ref as product_ref, p.tosell as status, p.tobuy as status_buy";
  525. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p";
  526. $sql .= " WHERE pfp.rowid = ".(int) $rowid;
  527. $sql .= " AND pfp.fk_product = p.rowid";
  528. dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
  529. $resql = $this->db->query($sql);
  530. if ($resql) {
  531. $obj = $this->db->fetch_object($resql);
  532. if ($obj) {
  533. $this->product_fourn_price_id = $rowid;
  534. $this->id = $obj->fk_product;
  535. $this->fk_product = $obj->fk_product;
  536. $this->product_id = $obj->fk_product;
  537. $this->product_ref = $obj->product_ref;
  538. $this->status = $obj->status;
  539. $this->status_buy = $obj->status_buy;
  540. $this->fourn_id = $obj->fk_soc;
  541. $this->fourn_ref = $obj->ref_fourn; // deprecated
  542. $this->ref_supplier = $obj->ref_fourn;
  543. $this->desc_supplier = $obj->desc_fourn;
  544. $this->fourn_price = $obj->price;
  545. $this->fourn_charges = $obj->charges; // deprecated
  546. $this->fourn_qty = $obj->quantity;
  547. $this->fourn_remise_percent = $obj->remise_percent;
  548. $this->fourn_remise = $obj->remise;
  549. $this->fourn_unitprice = $obj->unitprice;
  550. $this->fourn_tva_tx = $obj->tva_tx;
  551. $this->fourn_tva_npr = $obj->fourn_tva_npr;
  552. // Add also localtaxes
  553. $this->fk_availability = $obj->fk_availability;
  554. $this->delivery_time_days = $obj->delivery_time_days;
  555. $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
  556. $this->supplier_reputation = $obj->supplier_reputation;
  557. $this->default_vat_code = $obj->default_vat_code;
  558. $this->user_id = $obj->fk_user;
  559. $this->date_creation = $this->db->jdate($obj->datec);
  560. $this->fourn_multicurrency_price = $obj->multicurrency_price;
  561. $this->fourn_multicurrency_unitprice = $obj->multicurrency_unitprice;
  562. $this->fourn_multicurrency_tx = $obj->multicurrency_tx;
  563. $this->fourn_multicurrency_id = $obj->fk_multicurrency;
  564. $this->fourn_multicurrency_code = $obj->multicurrency_code;
  565. if (isModEnabled('barcode')) {
  566. $this->fourn_barcode = $obj->barcode; // deprecated
  567. $this->fourn_fk_barcode_type = $obj->fk_barcode_type; // deprecated
  568. $this->supplier_barcode = $obj->barcode;
  569. $this->supplier_fk_barcode_type = $obj->fk_barcode_type;
  570. }
  571. $this->packaging = $obj->packaging;
  572. if (isModEnabled('dynamicprices') && empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) {
  573. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  574. $priceparser = new PriceParser($this->db);
  575. $price_result = $priceparser->parseProductSupplier($this);
  576. if ($price_result >= 0) {
  577. $this->fourn_price = $price_result;
  578. //recalculation of unitprice, as probably the price changed...
  579. if ($this->fourn_qty != 0) {
  580. $this->fourn_unitprice = price2num($this->fourn_price / $this->fourn_qty, 'MU');
  581. } else {
  582. $this->fourn_unitprice = "";
  583. }
  584. }
  585. }
  586. return 1;
  587. } else {
  588. return 0;
  589. }
  590. } else {
  591. $this->error = $this->db->lasterror();
  592. return -1;
  593. }
  594. }
  595. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  596. /**
  597. * List all supplier prices of a product
  598. *
  599. * @param int $prodid Id of product
  600. * @param string $sortfield Sort field
  601. * @param string $sortorder Sort order
  602. * @param int $limit Limit
  603. * @param int $offset Offset
  604. * @param int $socid Filter on a third party id
  605. * @return array Array of ProductFournisseur with new properties to define supplier price
  606. * @see find_min_price_product_fournisseur()
  607. */
  608. public function list_product_fournisseur_price($prodid, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0, $socid = 0)
  609. {
  610. // phpcs:enable
  611. global $conf;
  612. $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id, p.ref as product_ref, p.tosell as status, p.tobuy as status_buy, ";
  613. $sql .= " pfp.rowid as product_fourn_pri_id, pfp.entity, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_product as product_fourn_id, pfp.fk_supplier_price_expression,";
  614. $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.info_bits, pfp.delivery_time_days, pfp.supplier_reputation,";
  615. $sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
  616. $sql .= " pfp.barcode, pfp.fk_barcode_type, pfp.packaging";
  617. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."societe as s";
  618. $sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
  619. $sql .= " AND pfp.fk_soc = s.rowid AND pfp.fk_product = p.rowid";
  620. $sql .= ($socid > 0 ? ' AND pfp.fk_soc = '.((int) $socid) : '');
  621. $sql .= " AND s.status = 1"; // only enabled company selected
  622. $sql .= " AND pfp.fk_product = ".((int) $prodid);
  623. if (empty($sortfield)) {
  624. $sql .= " ORDER BY s.nom, pfp.quantity, pfp.price";
  625. } else {
  626. $sql .= $this->db->order($sortfield, $sortorder);
  627. }
  628. $sql .= $this->db->plimit($limit, $offset);
  629. dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
  630. $resql = $this->db->query($sql);
  631. if ($resql) {
  632. $retarray = array();
  633. while ($record = $this->db->fetch_array($resql)) {
  634. //define base attribute
  635. $prodfourn = new ProductFournisseur($this->db);
  636. $prodfourn->product_ref = $record["product_ref"];
  637. $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
  638. $prodfourn->status = $record["status"];
  639. $prodfourn->status_buy = $record["status_buy"];
  640. $prodfourn->product_fourn_id = $record["product_fourn_id"];
  641. $prodfourn->product_fourn_entity = $record["entity"];
  642. $prodfourn->ref_supplier = $record["ref_fourn"];
  643. $prodfourn->fourn_ref = $record["ref_fourn"];
  644. $prodfourn->desc_supplier = $record["desc_fourn"];
  645. $prodfourn->fourn_price = $record["price"];
  646. $prodfourn->fourn_qty = $record["quantity"];
  647. $prodfourn->fourn_remise_percent = $record["remise_percent"];
  648. $prodfourn->fourn_remise = $record["remise"];
  649. $prodfourn->fourn_unitprice = $record["unitprice"];
  650. $prodfourn->fourn_charges = $record["charges"]; // deprecated
  651. $prodfourn->fourn_tva_tx = $record["tva_tx"];
  652. $prodfourn->fourn_id = $record["fourn_id"];
  653. $prodfourn->fourn_name = $record["supplier_name"];
  654. $prodfourn->fk_availability = $record["fk_availability"];
  655. $prodfourn->delivery_time_days = $record["delivery_time_days"];
  656. $prodfourn->id = $prodid;
  657. $prodfourn->fourn_tva_npr = $record["info_bits"];
  658. $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
  659. $prodfourn->supplier_reputation = $record["supplier_reputation"];
  660. $prodfourn->fourn_date_creation = $this->db->jdate($record['datec']);
  661. $prodfourn->fourn_date_modification = $this->db->jdate($record['tms']);
  662. $prodfourn->fourn_multicurrency_price = $record["multicurrency_price"];
  663. $prodfourn->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
  664. $prodfourn->fourn_multicurrency_tx = $record["multicurrency_tx"];
  665. $prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
  666. $prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];
  667. $prodfourn->packaging = $record["packaging"];
  668. if (isModEnabled('barcode')) {
  669. $prodfourn->supplier_barcode = $record["barcode"];
  670. $prodfourn->supplier_fk_barcode_type = $record["fk_barcode_type"];
  671. }
  672. if (isModEnabled('dynamicprices') && !empty($prodfourn->fk_supplier_price_expression)) {
  673. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  674. $priceparser = new PriceParser($this->db);
  675. $price_result = $priceparser->parseProductSupplier($prodfourn);
  676. if ($price_result >= 0) {
  677. $prodfourn->fourn_price = $price_result;
  678. $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
  679. }
  680. }
  681. if (!isset($prodfourn->fourn_unitprice)) {
  682. if ($prodfourn->fourn_qty != 0) {
  683. $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price / $prodfourn->fourn_qty, 'MU');
  684. } else {
  685. $prodfourn->fourn_unitprice = "";
  686. }
  687. }
  688. $retarray[] = $prodfourn;
  689. }
  690. $this->db->free($resql);
  691. return $retarray;
  692. } else {
  693. $this->error = $this->db->error();
  694. return -1;
  695. }
  696. }
  697. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  698. /**
  699. * Load properties for minimum price
  700. *
  701. * @param int $prodid Product id
  702. * @param int $qty Minimum quantity
  703. * @param int $socid get min price for specific supplier
  704. * @return int <0 if KO, 0=Not found of no product id provided, >0 if OK
  705. * @see list_product_fournisseur_price()
  706. */
  707. public function find_min_price_product_fournisseur($prodid, $qty = 0, $socid = 0)
  708. {
  709. // phpcs:enable
  710. global $conf;
  711. if (empty($prodid)) {
  712. dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
  713. return 0;
  714. }
  715. $this->product_fourn_price_id = '';
  716. $this->product_fourn_id = '';
  717. $this->fourn_ref = '';
  718. $this->fourn_price = '';
  719. $this->fourn_qty = '';
  720. $this->fourn_remise_percent = '';
  721. $this->fourn_remise = '';
  722. $this->fourn_unitprice = '';
  723. $this->fourn_id = '';
  724. $this->fourn_name = '';
  725. $this->delivery_time_days = '';
  726. $this->id = '';
  727. $this->fourn_multicurrency_price = '';
  728. $this->fourn_multicurrency_unitprice = '';
  729. $this->fourn_multicurrency_tx = '';
  730. $this->fourn_multicurrency_id = '';
  731. $this->fourn_multicurrency_code = '';
  732. $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
  733. $sql .= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
  734. $sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges,";
  735. $sql .= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
  736. $sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
  737. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
  738. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  739. $sql .= " AND pfp.entity IN (".getEntity('productsupplierprice').")";
  740. $sql .= " AND pfp.fk_product = ".((int) $prodid);
  741. $sql .= " AND pfp.fk_soc = s.rowid";
  742. $sql .= " AND s.status = 1"; // only enabled society
  743. if ($qty > 0) {
  744. $sql .= " AND pfp.quantity <= ".((float) $qty);
  745. }
  746. if ($socid > 0) {
  747. $sql .= ' AND pfp.fk_soc = '.((int) $socid);
  748. }
  749. dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
  750. $resql = $this->db->query($sql);
  751. if ($resql) {
  752. $record_array = array();
  753. //Store each record to array for later search of min
  754. while ($record = $this->db->fetch_array($resql)) {
  755. $record_array[] = $record;
  756. }
  757. if (count($record_array) == 0) {
  758. $this->db->free($resql);
  759. return 0;
  760. } else {
  761. $min = -1;
  762. foreach ($record_array as $record) {
  763. $fourn_price = $record["price"];
  764. // calculate unit price for quantity 1
  765. $fourn_unitprice = $record["unitprice"];
  766. $fourn_unitprice_with_discount = $record["unitprice"] * (1 - $record["remise_percent"] / 100);
  767. if (isModEnabled('dynamicprices') && !empty($record["fk_supplier_price_expression"])) {
  768. $prod_supplier = new ProductFournisseur($this->db);
  769. $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
  770. $prod_supplier->id = $prodid;
  771. $prod_supplier->fourn_qty = $record["quantity"];
  772. $prod_supplier->fourn_tva_tx = $record["tva_tx"];
  773. $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
  774. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  775. $priceparser = new PriceParser($this->db);
  776. $price_result = $priceparser->parseProductSupplier($prod_supplier);
  777. if ($price_result >= 0) {
  778. $fourn_price = price2num($price_result, 'MU');
  779. if ($record["quantity"] != 0) {
  780. $fourn_unitprice = price2num($fourn_price / $record["quantity"], 'MU');
  781. } else {
  782. $fourn_unitprice = $fourn_price;
  783. }
  784. $fourn_unitprice_with_discount = $fourn_unitprice * (1 - $record["remise_percent"] / 100);
  785. }
  786. }
  787. if ($fourn_unitprice < $min || $min == -1) {
  788. $this->product_fourn_price_id = $record["product_fourn_price_id"];
  789. $this->ref_supplier = $record["ref_fourn"];
  790. $this->ref_fourn = $record["ref_fourn"]; // deprecated
  791. $this->fourn_ref = $record["ref_fourn"]; // deprecated
  792. $this->fourn_price = $fourn_price;
  793. $this->fourn_qty = $record["quantity"];
  794. $this->fourn_remise_percent = $record["remise_percent"];
  795. $this->fourn_remise = $record["remise"];
  796. $this->fourn_unitprice = $fourn_unitprice;
  797. $this->fourn_unitprice_with_discount = $fourn_unitprice_with_discount;
  798. $this->fourn_charges = $record["charges"]; // deprecated
  799. $this->fourn_tva_tx = $record["tva_tx"];
  800. $this->fourn_id = $record["fourn_id"];
  801. $this->fourn_name = $record["supplier_name"];
  802. $this->delivery_time_days = $record["delivery_time_days"];
  803. $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
  804. $this->id = $prodid;
  805. $this->fourn_multicurrency_price = $record["multicurrency_price"];
  806. $this->fourn_multicurrency_unitprice = $record["multicurrency_unitprice"];
  807. $this->fourn_multicurrency_tx = $record["multicurrency_tx"];
  808. $this->fourn_multicurrency_id = $record["fk_multicurrency"];
  809. $this->fourn_multicurrency_code = $record["multicurrency_code"];
  810. $min = $fourn_unitprice;
  811. }
  812. }
  813. }
  814. $this->db->free($resql);
  815. return 1;
  816. } else {
  817. $this->error = $this->db->error();
  818. return -1;
  819. }
  820. }
  821. /**
  822. * Sets the supplier price expression
  823. *
  824. * @param int $expression_id Expression
  825. * @return int <0 if KO, >0 if OK
  826. */
  827. public function setSupplierPriceExpression($expression_id)
  828. {
  829. global $conf;
  830. // Clean parameters
  831. $this->db->begin();
  832. $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
  833. $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
  834. $sql .= " SET fk_supplier_price_expression = ".((int) $expression_id);
  835. $sql .= " WHERE rowid = ".((int) $this->product_fourn_price_id);
  836. dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
  837. $resql = $this->db->query($sql);
  838. if ($resql) {
  839. $this->db->commit();
  840. return 1;
  841. } else {
  842. $this->error = $this->db->error()." sql=".$sql;
  843. $this->db->rollback();
  844. return -1;
  845. }
  846. }
  847. /**
  848. * Display supplier of product
  849. *
  850. * @param int $withpicto Add picto
  851. * @param string $option Target of link ('', 'customer', 'prospect', 'supplier')
  852. * @param int $maxlen Max length of name
  853. * @param integer $notooltip 1=Disable tooltip
  854. * @return string String with supplier price
  855. * TODO Remove this method. Use getNomUrl directly.
  856. */
  857. public function getSocNomUrl($withpicto = 0, $option = 'supplier', $maxlen = 0, $notooltip = 0)
  858. {
  859. $thirdparty = new Fournisseur($this->db);
  860. $thirdparty->fetch($this->fourn_id);
  861. return $thirdparty->getNomUrl($withpicto, $option, $maxlen, $notooltip);
  862. }
  863. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  864. /**
  865. * Display price of product
  866. *
  867. * @param int $showunitprice Show "Unit price" into output string
  868. * @param int $showsuptitle Show "Supplier" into output string
  869. * @param int $maxlen Max length of name
  870. * @param integer $notooltip 1=Disable tooltip
  871. * @param array $productFournList list of ProductFournisseur objects
  872. * to display in table format.
  873. * @return string String with supplier price
  874. */
  875. public function display_price_product_fournisseur($showunitprice = 1, $showsuptitle = 1, $maxlen = 0, $notooltip = 0, $productFournList = array())
  876. {
  877. // phpcs:enable
  878. global $conf, $langs;
  879. $out = '';
  880. $langs->load("suppliers");
  881. if (count($productFournList) > 0) {
  882. $out .= '<table class="nobordernopadding" width="100%">';
  883. $out .= '<tr><td class="liste_titre right">'.($showunitprice ? $langs->trans("Price").' '.$langs->trans("HT") : '').'</td>';
  884. $out .= '<td class="liste_titre right">'.($showunitprice ? $langs->trans("QtyMin") : '').'</td>';
  885. $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
  886. $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
  887. foreach ($productFournList as $productFourn) {
  888. $out .= '<tr><td class="right">'.($showunitprice ?price($productFourn->fourn_unitprice * (1 - $productFourn->fourn_remise_percent / 100) - $productFourn->fourn_remise) : '').'</td>';
  889. $out .= '<td class="right">'.($showunitprice ? $productFourn->fourn_qty : '').'</td>';
  890. $out .= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
  891. $out .= '<td>'.$productFourn->fourn_ref.'<td></tr>';
  892. }
  893. $out .= '</table>';
  894. } else {
  895. $out = ($showunitprice ? price($this->fourn_unitprice * (1 - $this->fourn_remise_percent / 100) + $this->fourn_remise, 0, $langs, 1, -1, -1, $conf->currency).' '.$langs->trans("HT").' &nbsp; <span class="opacitymedium">(</span>' : '');
  896. $out .= ($showsuptitle ? '<span class="opacitymedium">'.$langs->trans("Supplier").'</span>: ' : '').$this->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).' / <span class="opacitymedium">'.$langs->trans("SupplierRef").'</span>: '.$this->ref_supplier;
  897. $out .= ($showunitprice ? '<span class="opacitymedium">)</span>' : '');
  898. }
  899. return $out;
  900. }
  901. /**
  902. * Function used to replace a thirdparty id with another one.
  903. *
  904. * @param DoliDB $db Database handler
  905. * @param int $origin_id Old thirdparty id
  906. * @param int $dest_id New thirdparty id
  907. * @return bool
  908. */
  909. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  910. {
  911. $tables = array(
  912. 'product_fournisseur_price'
  913. );
  914. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  915. }
  916. /**
  917. * Function used to replace a product id with another one.
  918. *
  919. * @param DoliDB $db Database handler
  920. * @param int $origin_id Old product id
  921. * @param int $dest_id New product id
  922. * @return bool
  923. */
  924. public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
  925. {
  926. $tables = array(
  927. 'product_fournisseur_price'
  928. );
  929. return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
  930. }
  931. /**
  932. * List supplier prices log of a supplier price
  933. *
  934. * @param int $product_fourn_price_id Id of supplier price
  935. * @param string $sortfield Sort field
  936. * @param string $sortorder Sort order
  937. * @param int $limit Limit
  938. * @param int $offset Offset
  939. * @return array Array of Log prices
  940. */
  941. public function listProductFournisseurPriceLog($product_fourn_price_id, $sortfield = '', $sortorder = '', $limit = 0, $offset = 0)
  942. {
  943. $sql = "SELECT";
  944. $sql .= " u.lastname,";
  945. $sql .= " pfpl.rowid, pfp.ref_fourn as supplier_ref, pfpl.datec,";
  946. $sql .= " pfpl.price, pfpl.quantity,";
  947. $sql .= " pfpl.fk_multicurrency, pfpl.multicurrency_code, pfpl.multicurrency_tx, pfpl.multicurrency_price, pfpl.multicurrency_unitprice";
  948. $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price_log as pfpl,";
  949. $sql .= " ".MAIN_DB_PREFIX."product_fournisseur_price as pfp,";
  950. $sql .= " ".MAIN_DB_PREFIX."user as u";
  951. $sql .= " WHERE pfp.entity IN (".getEntity('productprice').")";
  952. $sql .= " AND pfpl.fk_user = u.rowid";
  953. $sql .= " AND pfp.rowid = pfpl.fk_product_fournisseur";
  954. $sql .= " AND pfpl.fk_product_fournisseur = ".((int) $product_fourn_price_id);
  955. if (empty($sortfield)) {
  956. $sql .= " ORDER BY pfpl.datec";
  957. } else {
  958. $sql .= $this->db->order($sortfield, $sortorder);
  959. }
  960. $sql .= $this->db->plimit($limit, $offset);
  961. dol_syslog(get_class($this)."::list_product_fournisseur_price_log", LOG_DEBUG);
  962. $resql = $this->db->query($sql);
  963. if ($resql) {
  964. $retarray = array();
  965. while ($obj = $this->db->fetch_object($resql)) {
  966. $tmparray = array();
  967. $tmparray['rowid'] = $obj->rowid;
  968. $tmparray['supplier_ref'] = $obj->supplier_ref;
  969. $tmparray['datec'] = $this->db->jdate($obj->datec);
  970. $tmparray['lastname'] = $obj->lastname;
  971. $tmparray['price'] = $obj->price;
  972. $tmparray['quantity'] = $obj->quantity;
  973. $tmparray['fk_multicurrency'] = $obj->fk_multicurrency;
  974. $tmparray['multicurrency_code'] = $obj->multicurrency_code;
  975. $tmparray['multicurrency_tx'] = $obj->multicurrency_tx;
  976. $tmparray['multicurrency_price'] = $obj->multicurrency_price;
  977. $tmparray['multicurrency_unitprice'] = $obj->multicurrency_unitprice;
  978. $retarray[] = $tmparray;
  979. }
  980. $this->db->free($resql);
  981. return $retarray;
  982. } else {
  983. $this->error = $this->db->error();
  984. return -1;
  985. }
  986. }
  987. /**
  988. * Display log price of product supplier price
  989. *
  990. * @param array $productFournLogList list of ProductFournisseur price log objects
  991. * to display in table format.
  992. * @return string HTML String with supplier price
  993. */
  994. public function displayPriceProductFournisseurLog($productFournLogList = array())
  995. {
  996. global $conf, $langs;
  997. $out = '';
  998. $langs->load("suppliers");
  999. if (count($productFournLogList) > 0) {
  1000. $out .= '<table class="noborder centpercent">';
  1001. $out .= '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Date").'</td>';
  1002. $out .= '<td class="liste_titre right">'.$langs->trans("Price").'</td>';
  1003. //$out .= '<td class="liste_titre right">'.$langs->trans("QtyMin").'</td>';
  1004. $out .= '<td class="liste_titre">'.$langs->trans("User").'</td></tr>';
  1005. foreach ($productFournLogList as $productFournLog) {
  1006. $out .= '<tr><td>'.dol_print_date($productFournLog['datec'], 'dayhour', 'tzuser').'</td>';
  1007. $out .= '<td class="right">'.price($productFournLog['price'], 0, $langs, 1, -1, -1, $conf->currency);
  1008. if ($productFournLog['multicurrency_code'] != $conf->currency) {
  1009. $out .= ' ('.price($productFournLog['multicurrency_price'], 0, $langs, 1, -1, -1, $productFournLog['multicurrency_code']).')';
  1010. }
  1011. $out .= '</td>';
  1012. //$out.= '<td class="right">'.$productFournLog['quantity'].'</td>';
  1013. $out .= '<td>'.$productFournLog['lastname'].'</td></tr>';
  1014. }
  1015. $out .= '</table>';
  1016. }
  1017. return $out;
  1018. }
  1019. /**
  1020. * Return a link to the object card (with optionaly the picto).
  1021. * Used getNomUrl of ProductFournisseur if a specific supplier ref is loaded. Otherwise use Product->getNomUrl().
  1022. *
  1023. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  1024. * @param string $option On what the link point to ('nolink', ...)
  1025. * @param int $maxlength Maxlength of ref
  1026. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  1027. * @param int $notooltip No tooltip
  1028. * @param string $morecss ''=Add more css on link
  1029. * @param int $add_label 0=Default, 1=Add label into string, >1=Add first chars into string
  1030. * @param string $sep ' - '=Separator between ref and label if option 'add_label' is set
  1031. * @return string String with URL
  1032. */
  1033. public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $save_lastsearch_value = -1, $notooltip = 0, $morecss = '', $add_label = 0, $sep = ' - ')
  1034. {
  1035. global $db, $conf, $langs, $hookmanager;
  1036. if (!empty($conf->dol_no_mouse_hover)) {
  1037. $notooltip = 1; // Force disable tooltips
  1038. }
  1039. $result = '';
  1040. $label = '';
  1041. $newref = $this->ref;
  1042. if ($maxlength) {
  1043. $newref = dol_trunc($newref, $maxlength, 'middle');
  1044. }
  1045. if (!empty($this->entity)) {
  1046. $tmpphoto = $this->show_photos('product', $conf->product->multidir_output[$this->entity], 1, 1, 0, 0, 0, 80);
  1047. if ($this->nbphoto > 0) {
  1048. $label .= '<div class="photointooltip">';
  1049. $label .= $tmpphoto;
  1050. $label .= '</div><div style="clear: both;"></div>';
  1051. }
  1052. }
  1053. if ($this->type == Product::TYPE_PRODUCT) {
  1054. $label .= img_picto('', 'product').' <u class="paddingrightonly">'.$langs->trans("Product").'</u>';
  1055. } elseif ($this->type == Product::TYPE_SERVICE) {
  1056. $label .= img_picto('', 'service').' <u class="paddingrightonly">'.$langs->trans("Service").'</u>';
  1057. }
  1058. if (isset($this->status) && isset($this->status_buy)) {
  1059. $label .= ' '.$this->getLibStatut(5, 0);
  1060. $label .= ' '.$this->getLibStatut(5, 1);
  1061. }
  1062. if (!empty($this->ref)) {
  1063. $label .= '<br><b>'.$langs->trans('ProductRef').':</b> '.($this->ref ? $this->ref : $this->product_ref);
  1064. }
  1065. if (!empty($this->label)) {
  1066. $label .= '<br><b>'.$langs->trans('ProductLabel').':</b> '.$this->label;
  1067. }
  1068. $label .= '<br><b>'.$langs->trans('RefSupplier').':</b> '.$this->ref_supplier;
  1069. if ($this->type == Product::TYPE_PRODUCT || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  1070. if (isModEnabled('productbatch')) {
  1071. $langs->load("productbatch");
  1072. $label .= "<br><b>".$langs->trans("ManageLotSerial").'</b>: '.$this->getLibStatut(0, 2);
  1073. }
  1074. }
  1075. if (isModEnabled('barcode')) {
  1076. $label .= '<br><b>'.$langs->trans('BarCode').':</b> '.$this->barcode;
  1077. }
  1078. if ($this->type == Product::TYPE_PRODUCT) {
  1079. if ($this->weight) {
  1080. $label .= "<br><b>".$langs->trans("Weight").'</b>: '.$this->weight.' '.measuringUnitString(0, "weight", $this->weight_units);
  1081. }
  1082. $labelsize = "";
  1083. if ($this->length) {
  1084. $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Length").'</b>: '.$this->length.' '.measuringUnitString(0, 'size', $this->length_units);
  1085. }
  1086. if ($this->width) {
  1087. $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Width").'</b>: '.$this->width.' '.measuringUnitString(0, 'size', $this->width_units);
  1088. }
  1089. if ($this->height) {
  1090. $labelsize .= ($labelsize ? " - " : "")."<b>".$langs->trans("Height").'</b>: '.$this->height.' '.measuringUnitString(0, 'size', $this->height_units);
  1091. }
  1092. if ($labelsize) {
  1093. $label .= "<br>".$labelsize;
  1094. }
  1095. $labelsurfacevolume = "";
  1096. if ($this->surface) {
  1097. $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Surface").'</b>: '.$this->surface.' '.measuringUnitString(0, 'surface', $this->surface_units);
  1098. }
  1099. if ($this->volume) {
  1100. $labelsurfacevolume .= ($labelsurfacevolume ? " - " : "")."<b>".$langs->trans("Volume").'</b>: '.$this->volume.' '.measuringUnitString(0, 'volume', $this->volume_units);
  1101. }
  1102. if ($labelsurfacevolume) {
  1103. $label .= "<br>".$labelsurfacevolume;
  1104. }
  1105. }
  1106. if (isModEnabled('accounting') && $this->status) {
  1107. include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  1108. $label .= '<br><b>'.$langs->trans('ProductAccountancySellCode').':</b> '.length_accountg($this->accountancy_code_sell);
  1109. $label .= '<br><b>'.$langs->trans('ProductAccountancySellIntraCode').':</b> '.length_accountg($this->accountancy_code_sell_intra);
  1110. $label .= '<br><b>'.$langs->trans('ProductAccountancySellExportCode').':</b> '.length_accountg($this->accountancy_code_sell_export);
  1111. }
  1112. if (isModEnabled('accounting') && $this->status_buy) {
  1113. include_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  1114. $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyCode').':</b> '.length_accountg($this->accountancy_code_buy);
  1115. $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyIntraCode').':</b> '.length_accountg($this->accountancy_code_buy_intra);
  1116. $label .= '<br><b>'.$langs->trans('ProductAccountancyBuyExportCode').':</b> '.length_accountg($this->accountancy_code_buy_export);
  1117. }
  1118. $logPrices = $this->listProductFournisseurPriceLog($this->product_fourn_price_id, 'pfpl.datec', 'DESC'); // set sort order here
  1119. if (is_array($logPrices) && count($logPrices) > 0) {
  1120. $label .= '<br><br>';
  1121. $label .= '<u>'.$langs->trans("History").'</u>';
  1122. $label .= $this->displayPriceProductFournisseurLog($logPrices);
  1123. }
  1124. $url = dol_buildpath('/product/fournisseurs.php', 1).'?id='.$this->id.'&action=add_price&token='.newToken().'&socid='.$this->fourn_id.'&rowid='.$this->product_fourn_price_id;
  1125. if ($option != 'nolink') {
  1126. // Add param to save lastsearch_values or not
  1127. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  1128. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  1129. $add_save_lastsearch_values = 1;
  1130. }
  1131. if ($add_save_lastsearch_values) {
  1132. $url .= '&save_lastsearch_values=1';
  1133. }
  1134. }
  1135. $linkclose = '';
  1136. if (empty($notooltip)) {
  1137. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  1138. $label = $langs->trans("SupplierRef");
  1139. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  1140. }
  1141. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  1142. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  1143. } else {
  1144. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  1145. }
  1146. $linkstart = '<a href="'.$url.'"';
  1147. $linkstart .= $linkclose.'>';
  1148. $linkend = '</a>';
  1149. $result .= $linkstart;
  1150. if ($withpicto) {
  1151. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  1152. }
  1153. if ($withpicto != 2) {
  1154. $result .= $newref.($this->ref_supplier ? ' ('.$this->ref_supplier.')' : '');
  1155. }
  1156. $result .= $linkend;
  1157. if ($withpicto != 2) {
  1158. $result .= (($add_label && $this->label) ? $sep.dol_trunc($this->label, ($add_label > 1 ? $add_label : 0)) : '');
  1159. }
  1160. global $action;
  1161. $hookmanager->initHooks(array($this->element . 'dao'));
  1162. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  1163. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1164. if ($reshook > 0) {
  1165. $result = $hookmanager->resPrint;
  1166. } else {
  1167. $result .= $hookmanager->resPrint;
  1168. }
  1169. return $result;
  1170. }
  1171. /**
  1172. * Private function to log price history
  1173. *
  1174. * @param User $user Object user who adds/changes price
  1175. * @param integer $datec date create
  1176. * @param float $buyprice price for qty
  1177. * @param float $qty qty for price
  1178. * @param float $multicurrency_buyprice Purchase price for the quantity min in currency
  1179. * @param float $multicurrency_unitBuyPrice Unit Purchase price in currency
  1180. * @param float $multicurrency_tx Rate currency
  1181. * @param int $fk_multicurrency key multi currency
  1182. * @param string $multicurrency_code Currency code
  1183. *
  1184. * @return int < 0 NOK > 0 OK
  1185. */
  1186. private function logPrice($user, $datec, $buyprice, $qty, $multicurrency_buyprice = null, $multicurrency_unitBuyPrice = null, $multicurrency_tx = null, $fk_multicurrency = null, $multicurrency_code = null)
  1187. {
  1188. // Add record into log table
  1189. $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
  1190. $sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
  1191. $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
  1192. $sql .= "values(";
  1193. $sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
  1194. $sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
  1195. $sql .= (isset($multicurrency_tx) ? "'".$this->db->escape($multicurrency_tx)."'" : '1').",";
  1196. $sql .= (isset($fk_multicurrency) ? "'".$this->db->escape($fk_multicurrency)."'" : 'null').",";
  1197. $sql .= (isset($multicurrency_code) ? "'".$this->db->escape($multicurrency_code)."'" : 'null').",";
  1198. $sql .= "'".$this->db->idate($datec)."',";
  1199. $sql .= " ".((int) $this->product_fourn_price_id).",";
  1200. $sql .= " ".$user->id.",";
  1201. $sql .= " ".price2num($buyprice).",";
  1202. $sql .= " ".price2num($qty);
  1203. $sql .= ")";
  1204. $resql = $this->db->query($sql);
  1205. if (!$resql) {
  1206. return -1;
  1207. } else {
  1208. return 1;
  1209. }
  1210. }
  1211. }