productcustomerprice.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/product/class/productcustomerprice.class.php
  20. * \ingroup produit
  21. * \brief File of class to manage predefined price products or services by customer
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. /**
  25. * File of class to manage predefined price products or services by customer
  26. */
  27. class Productcustomerprice extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'product_customer_price';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'product_customer_price';
  37. /**
  38. * @var int Entity
  39. */
  40. public $entity;
  41. public $datec = '';
  42. public $tms = '';
  43. /**
  44. * @var int ID
  45. */
  46. public $fk_product;
  47. /**
  48. * @var int Thirdparty ID
  49. */
  50. public $fk_soc;
  51. /**
  52. * @var string Customer reference
  53. */
  54. public $ref_customer;
  55. public $price;
  56. public $price_ttc;
  57. public $price_min;
  58. public $price_min_ttc;
  59. public $price_base_type;
  60. public $tva_tx;
  61. public $recuperableonly;
  62. public $localtax1_type;
  63. public $localtax1_tx;
  64. public $localtax2_type;
  65. public $localtax2_tx;
  66. /**
  67. * @var int User ID
  68. */
  69. public $fk_user;
  70. public $lines = array();
  71. /**
  72. * Constructor
  73. *
  74. * @param DoliDb $db handler
  75. */
  76. public function __construct($db)
  77. {
  78. $this->db = $db;
  79. }
  80. /**
  81. * Create object into database
  82. *
  83. * @param User $user that creates
  84. * @param int $notrigger triggers after, 1=disable triggers
  85. * @param int $forceupdateaffiliate update price on each soc child
  86. * @return int <0 if KO, Id of created object if OK
  87. */
  88. public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
  89. {
  90. global $conf, $langs;
  91. $error = 0;
  92. // Clean parameters
  93. if (isset($this->entity)) {
  94. $this->entity = trim($this->entity);
  95. }
  96. if (isset($this->fk_product)) {
  97. $this->fk_product = trim($this->fk_product);
  98. }
  99. if (isset($this->fk_soc)) {
  100. $this->fk_soc = trim($this->fk_soc);
  101. }
  102. if (isset($this->ref_customer)) {
  103. $this->ref_customer = trim($this->ref_customer);
  104. }
  105. if (isset($this->price)) {
  106. $this->price = trim($this->price);
  107. }
  108. if (isset($this->price_ttc)) {
  109. $this->price_ttc = trim($this->price_ttc);
  110. }
  111. if (isset($this->price_min)) {
  112. $this->price_min = trim($this->price_min);
  113. }
  114. if (isset($this->price_min_ttc)) {
  115. $this->price_min_ttc = trim($this->price_min_ttc);
  116. }
  117. if (isset($this->price_base_type)) {
  118. $this->price_base_type = trim($this->price_base_type);
  119. }
  120. if (isset($this->tva_tx)) {
  121. $this->tva_tx = (float) $this->tva_tx;
  122. }
  123. if (isset($this->recuperableonly)) {
  124. $this->recuperableonly = trim($this->recuperableonly);
  125. }
  126. if (isset($this->localtax1_tx)) {
  127. $this->localtax1_tx = trim($this->localtax1_tx);
  128. }
  129. if (isset($this->localtax2_tx)) {
  130. $this->localtax2_tx = trim($this->localtax2_tx);
  131. }
  132. if (isset($this->fk_user)) {
  133. $this->fk_user = trim($this->fk_user);
  134. }
  135. if (isset($this->import_key)) {
  136. $this->import_key = trim($this->import_key);
  137. }
  138. // Check parameters
  139. // Put here code to add control on parameters values
  140. if ($this->price != '' || $this->price == 0) {
  141. if ($this->price_base_type == 'TTC') {
  142. $this->price_ttc = price2num($this->price, 'MU');
  143. $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
  144. $this->price = price2num($this->price, 'MU');
  145. if ($this->price_min != '' || $this->price_min == 0) {
  146. $this->price_min_ttc = price2num($this->price_min, 'MU');
  147. $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
  148. $this->price_min = price2num($this->price_min, 'MU');
  149. } else {
  150. $this->price_min = 0;
  151. $this->price_min_ttc = 0;
  152. }
  153. } else {
  154. $this->price = price2num($this->price, 'MU');
  155. $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
  156. $this->price_ttc = price2num($this->price_ttc, 'MU');
  157. if ($this->price_min != '' || $this->price_min == 0) {
  158. $this->price_min = price2num($this->price_min, 'MU');
  159. $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
  160. $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
  161. // print 'X'.$newminprice.'-'.$price_min;
  162. } else {
  163. $this->price_min = 0;
  164. $this->price_min_ttc = 0;
  165. }
  166. }
  167. }
  168. // Insert request
  169. $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
  170. $sql .= "entity,";
  171. $sql .= "datec,";
  172. $sql .= "fk_product,";
  173. $sql .= "fk_soc,";
  174. $sql .= 'ref_customer,';
  175. $sql .= "price,";
  176. $sql .= "price_ttc,";
  177. $sql .= "price_min,";
  178. $sql .= "price_min_ttc,";
  179. $sql .= "price_base_type,";
  180. $sql .= "default_vat_code,";
  181. $sql .= "tva_tx,";
  182. $sql .= "recuperableonly,";
  183. $sql .= "localtax1_type,";
  184. $sql .= "localtax1_tx,";
  185. $sql .= "localtax2_type,";
  186. $sql .= "localtax2_tx,";
  187. $sql .= "fk_user,";
  188. $sql .= "import_key";
  189. $sql .= ") VALUES (";
  190. $sql .= " ".((int) $conf->entity).",";
  191. $sql .= " '".$this->db->idate(dol_now())."',";
  192. $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
  193. $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
  194. $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
  195. $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
  196. $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
  197. $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
  198. $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
  199. $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
  200. $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
  201. $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
  202. $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
  203. $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
  204. $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
  205. $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
  206. $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
  207. $sql .= " ".((int) $user->id).",";
  208. $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'")."";
  209. $sql .= ")";
  210. $this->db->begin();
  211. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  212. $resql = $this->db->query($sql);
  213. if (!$resql) {
  214. $error++;
  215. $this->errors [] = "Error ".$this->db->lasterror();
  216. }
  217. if (!$error) {
  218. $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
  219. if (!$notrigger) {
  220. $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
  221. if ($result < 0) {
  222. $error++;
  223. }
  224. }
  225. }
  226. if (!$error) {
  227. $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
  228. if ($result < 0) {
  229. $error++;
  230. }
  231. }
  232. // Commit or rollback
  233. if ($error) {
  234. foreach ($this->errors as $errmsg) {
  235. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  236. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  237. }
  238. $this->db->rollback();
  239. return -1 * $error;
  240. } else {
  241. $this->db->commit();
  242. return $this->id;
  243. }
  244. }
  245. /**
  246. * Load object in memory from the database
  247. *
  248. * @param int $id ID of customer price
  249. * @return int <0 if KO, 0 if not found, >0 if OK
  250. */
  251. public function fetch($id)
  252. {
  253. global $langs;
  254. $sql = "SELECT";
  255. $sql .= " t.rowid,";
  256. $sql .= " t.entity,";
  257. $sql .= " t.datec,";
  258. $sql .= " t.tms,";
  259. $sql .= " t.fk_product,";
  260. $sql .= " t.fk_soc,";
  261. $sql .= " t.ref_customer,";
  262. $sql .= " t.price,";
  263. $sql .= " t.price_ttc,";
  264. $sql .= " t.price_min,";
  265. $sql .= " t.price_min_ttc,";
  266. $sql .= " t.price_base_type,";
  267. $sql .= " t.default_vat_code,";
  268. $sql .= " t.tva_tx,";
  269. $sql .= " t.recuperableonly,";
  270. $sql .= " t.localtax1_tx,";
  271. $sql .= " t.localtax2_tx,";
  272. $sql .= " t.fk_user,";
  273. $sql .= " t.import_key";
  274. $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
  275. $sql .= " WHERE t.rowid = ".((int) $id);
  276. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  277. $resql = $this->db->query($sql);
  278. if ($resql) {
  279. if ($this->db->num_rows($resql)) {
  280. $obj = $this->db->fetch_object($resql);
  281. $this->id = $obj->rowid;
  282. $this->entity = $obj->entity;
  283. $this->datec = $this->db->jdate($obj->datec);
  284. $this->tms = $this->db->jdate($obj->tms);
  285. $this->fk_product = $obj->fk_product;
  286. $this->fk_soc = $obj->fk_soc;
  287. $this->ref_customer = $obj->ref_customer;
  288. $this->price = $obj->price;
  289. $this->price_ttc = $obj->price_ttc;
  290. $this->price_min = $obj->price_min;
  291. $this->price_min_ttc = $obj->price_min_ttc;
  292. $this->price_base_type = $obj->price_base_type;
  293. $this->default_vat_code = $obj->default_vat_code;
  294. $this->tva_tx = $obj->tva_tx;
  295. $this->recuperableonly = $obj->recuperableonly;
  296. $this->localtax1_tx = $obj->localtax1_tx;
  297. $this->localtax2_tx = $obj->localtax2_tx;
  298. $this->fk_user = $obj->fk_user;
  299. $this->import_key = $obj->import_key;
  300. $this->db->free($resql);
  301. return 1;
  302. } else {
  303. $this->db->free($resql);
  304. return 0;
  305. }
  306. } else {
  307. $this->error = "Error ".$this->db->lasterror();
  308. return -1;
  309. }
  310. }
  311. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  312. /**
  313. * Load all customer prices in memory from database
  314. *
  315. * @param string $sortorder order
  316. * @param string $sortfield field
  317. * @param int $limit page
  318. * @param int $offset offset
  319. * @param array $filter Filter for select
  320. * @deprecated since dolibarr v17 use fetchAll
  321. * @return int <0 if KO, >0 if OK
  322. */
  323. public function fetch_all($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
  324. {
  325. // phpcs:enable
  326. dol_syslog(get_class($this)."::fetch_all is deprecated, use fetchAll instead", LOG_NOTICE);
  327. return $this->fetchAll($sortorder, $sortfield, $limit, $offset, $filter);
  328. }
  329. /**
  330. * Load all customer prices in memory from database
  331. *
  332. * @param string $sortorder order
  333. * @param string $sortfield field
  334. * @param int $limit page
  335. * @param int $offset offset
  336. * @param array $filter Filter for select
  337. * @return int <0 if KO, >0 if OK
  338. * @since dolibarr v17
  339. */
  340. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
  341. {
  342. global $langs;
  343. if (empty($sortfield)) {
  344. $sortfield = "t.rowid";
  345. }
  346. if (empty($sortorder)) {
  347. $sortorder = "DESC";
  348. }
  349. $sql = "SELECT";
  350. $sql .= " t.rowid,";
  351. $sql .= " t.entity,";
  352. $sql .= " t.datec,";
  353. $sql .= " t.tms,";
  354. $sql .= " t.fk_product,";
  355. $sql .= " t.fk_soc,";
  356. $sql .= " t.ref_customer,";
  357. $sql .= " t.price,";
  358. $sql .= " t.price_ttc,";
  359. $sql .= " t.price_min,";
  360. $sql .= " t.price_min_ttc,";
  361. $sql .= " t.price_base_type,";
  362. $sql .= " t.default_vat_code,";
  363. $sql .= " t.tva_tx,";
  364. $sql .= " t.recuperableonly,";
  365. $sql .= " t.localtax1_tx,";
  366. $sql .= " t.localtax2_tx,";
  367. $sql .= " t.localtax1_type,";
  368. $sql .= " t.localtax2_type,";
  369. $sql .= " t.fk_user,";
  370. $sql .= " t.import_key,";
  371. $sql .= " soc.nom as socname,";
  372. $sql .= " prod.ref as prodref";
  373. $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
  374. $sql .= " ".$this->db->prefix()."product as prod,";
  375. $sql .= " ".$this->db->prefix()."societe as soc";
  376. $sql .= " WHERE soc.rowid=t.fk_soc ";
  377. $sql .= " AND prod.rowid=t.fk_product ";
  378. $sql .= " AND prod.entity IN (".getEntity('product').")";
  379. $sql .= " AND t.entity IN (".getEntity('productprice').")";
  380. // Manage filter
  381. if (count($filter) > 0) {
  382. foreach ($filter as $key => $value) {
  383. if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
  384. $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
  385. } elseif ($key == 'soc.nom') {
  386. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  387. } elseif ($key == 'prod.ref' || $key == 'prod.label') {
  388. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  389. } elseif ($key == 't.price' || $key == 't.price_ttc') {
  390. $sql .= " AND ".$key." LIKE '%".price2num($value)."%'";
  391. } else {
  392. $sql .= " AND ".$key." = ".((int) $value);
  393. }
  394. }
  395. }
  396. $sql .= $this->db->order($sortfield, $sortorder);
  397. if (!empty($limit)) {
  398. $sql .= $this->db->plimit($limit + 1, $offset);
  399. }
  400. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  401. $resql = $this->db->query($sql);
  402. if ($resql) {
  403. $this->lines = array();
  404. $num = $this->db->num_rows($resql);
  405. while ($obj = $this->db->fetch_object($resql)) {
  406. $line = new PriceByCustomerLine();
  407. $line->id = $obj->rowid;
  408. $line->entity = $obj->entity;
  409. $line->datec = $this->db->jdate($obj->datec);
  410. $line->tms = $this->db->jdate($obj->tms);
  411. $line->fk_product = $obj->fk_product;
  412. $line->fk_soc = $obj->fk_soc;
  413. $line->ref_customer = $obj->ref_customer;
  414. $line->price = $obj->price;
  415. $line->price_ttc = $obj->price_ttc;
  416. $line->price_min = $obj->price_min;
  417. $line->price_min_ttc = $obj->price_min_ttc;
  418. $line->price_base_type = $obj->price_base_type;
  419. $line->default_vat_code = $obj->default_vat_code;
  420. $line->tva_tx = $obj->tva_tx;
  421. $line->recuperableonly = $obj->recuperableonly;
  422. $line->localtax1_tx = $obj->localtax1_tx;
  423. $line->localtax2_tx = $obj->localtax2_tx;
  424. $line->localtax1_type = $obj->localtax1_type;
  425. $line->localtax2_type = $obj->localtax2_type;
  426. $line->fk_user = $obj->fk_user;
  427. $line->import_key = $obj->import_key;
  428. $line->socname = $obj->socname;
  429. $line->prodref = $obj->prodref;
  430. $this->lines[] = $line;
  431. }
  432. $this->db->free($resql);
  433. return $num;
  434. } else {
  435. $this->error = "Error ".$this->db->lasterror();
  436. return -1;
  437. }
  438. }
  439. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  440. /**
  441. * Load all objects in memory from database
  442. *
  443. * @param string $sortorder order
  444. * @param string $sortfield field
  445. * @param int $limit page
  446. * @param int $offset offset
  447. * @param array $filter Filter for sql request
  448. * @return int <0 if KO, >0 if OK
  449. */
  450. public function fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter = array())
  451. {
  452. // phpcs:enable
  453. global $langs;
  454. if (!empty($sortfield)) {
  455. $sortfield = "t.rowid";
  456. }
  457. if (!empty($sortorder)) {
  458. $sortorder = "DESC";
  459. }
  460. $sql = "SELECT";
  461. $sql .= " t.rowid,";
  462. $sql .= " t.entity,";
  463. $sql .= " t.datec,";
  464. $sql .= " t.fk_product,";
  465. $sql .= " t.fk_soc,";
  466. $sql .= " t.ref_customer,";
  467. $sql .= " t.price,";
  468. $sql .= " t.price_ttc,";
  469. $sql .= " t.price_min,";
  470. $sql .= " t.price_min_ttc,";
  471. $sql .= " t.price_base_type,";
  472. $sql .= " t.default_vat_code,";
  473. $sql .= " t.tva_tx,";
  474. $sql .= " t.recuperableonly,";
  475. $sql .= " t.localtax1_tx,";
  476. $sql .= " t.localtax2_tx,";
  477. $sql .= " t.fk_user,";
  478. $sql .= " t.import_key,";
  479. $sql .= " soc.nom as socname,";
  480. $sql .= " prod.ref as prodref";
  481. $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
  482. $sql .= " ,".$this->db->prefix()."product as prod";
  483. $sql .= " ,".$this->db->prefix()."societe as soc";
  484. $sql .= " WHERE soc.rowid=t.fk_soc";
  485. $sql .= " AND prod.rowid=t.fk_product ";
  486. $sql .= " AND prod.entity IN (".getEntity('product').")";
  487. $sql .= " AND t.entity IN (".getEntity('productprice').")";
  488. // Manage filter
  489. if (count($filter) > 0) {
  490. foreach ($filter as $key => $value) {
  491. if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
  492. $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
  493. } elseif ($key == 'soc.nom') {
  494. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  495. } else {
  496. $sql .= " AND ".$key." = ".((int) $value);
  497. }
  498. }
  499. }
  500. $sql .= $this->db->order($sortfield, $sortorder);
  501. if (!empty($limit)) {
  502. $sql .= $this->db->plimit($limit + 1, $offset);
  503. }
  504. dol_syslog(get_class($this)."::fetch_all_log", LOG_DEBUG);
  505. $resql = $this->db->query($sql);
  506. if ($resql) {
  507. $this->lines = array();
  508. $num = $this->db->num_rows($resql);
  509. while ($obj = $this->db->fetch_object($resql)) {
  510. $line = new PriceByCustomerLine();
  511. $line->id = $obj->rowid;
  512. $line->entity = $obj->entity;
  513. $line->datec = $this->db->jdate($obj->datec);
  514. $line->tms = $this->db->jdate($obj->tms);
  515. $line->fk_product = $obj->fk_product;
  516. $line->fk_soc = $obj->fk_soc;
  517. $line->ref_customer = $obj->ref_customer;
  518. $line->price = $obj->price;
  519. $line->price_ttc = $obj->price_ttc;
  520. $line->price_min = $obj->price_min;
  521. $line->price_min_ttc = $obj->price_min_ttc;
  522. $line->price_base_type = $obj->price_base_type;
  523. $line->default_vat_code = $obj->default_vat_code;
  524. $line->tva_tx = $obj->tva_tx;
  525. $line->recuperableonly = $obj->recuperableonly;
  526. $line->localtax1_tx = $obj->localtax1_tx;
  527. $line->localtax2_tx = $obj->localtax2_tx;
  528. $line->fk_user = $obj->fk_user;
  529. $line->import_key = $obj->import_key;
  530. $line->socname = $obj->socname;
  531. $line->prodref = $obj->prodref;
  532. $this->lines [] = $line;
  533. }
  534. $this->db->free($resql);
  535. return $num;
  536. } else {
  537. $this->error = "Error ".$this->db->lasterror();
  538. return -1;
  539. }
  540. }
  541. /**
  542. * Update object into database
  543. *
  544. * @param User $user that modifies
  545. * @param int $notrigger triggers after, 1=disable triggers
  546. * @param int $forceupdateaffiliate update price on each soc child
  547. * @return int <0 if KO, >0 if OK
  548. */
  549. public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
  550. {
  551. global $conf, $langs;
  552. $error = 0;
  553. // Clean parameters
  554. if (isset($this->entity)) {
  555. $this->entity = trim($this->entity);
  556. }
  557. if (isset($this->fk_product)) {
  558. $this->fk_product = trim($this->fk_product);
  559. }
  560. if (isset($this->fk_soc)) {
  561. $this->fk_soc = trim($this->fk_soc);
  562. }
  563. if (isset($this->ref_customer)) {
  564. $this->ref_customer = trim($this->ref_customer);
  565. }
  566. if (isset($this->price)) {
  567. $this->price = trim($this->price);
  568. }
  569. if (isset($this->price_ttc)) {
  570. $this->price_ttc = trim($this->price_ttc);
  571. }
  572. if (isset($this->price_min)) {
  573. $this->price_min = trim($this->price_min);
  574. }
  575. if (isset($this->price_min_ttc)) {
  576. $this->price_min_ttc = trim($this->price_min_ttc);
  577. }
  578. if (isset($this->price_base_type)) {
  579. $this->price_base_type = trim($this->price_base_type);
  580. }
  581. if (isset($this->tva_tx)) {
  582. $this->tva_tx = (float) $this->tva_tx;
  583. }
  584. if (isset($this->recuperableonly)) {
  585. $this->recuperableonly = trim($this->recuperableonly);
  586. }
  587. if (isset($this->localtax1_tx)) {
  588. $this->localtax1_tx = trim($this->localtax1_tx);
  589. }
  590. if (isset($this->localtax2_tx)) {
  591. $this->localtax2_tx = trim($this->localtax2_tx);
  592. }
  593. if (isset($this->fk_user)) {
  594. $this->fk_user = trim($this->fk_user);
  595. }
  596. if (isset($this->import_key)) {
  597. $this->import_key = trim($this->import_key);
  598. }
  599. // Check parameters
  600. // Put here code to add a control on parameters values
  601. if ($this->price != '' || $this->price == 0) {
  602. if ($this->price_base_type == 'TTC') {
  603. $this->price_ttc = price2num($this->price, 'MU');
  604. $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
  605. $this->price = price2num($this->price, 'MU');
  606. if ($this->price_min != '' || $this->price_min == 0) {
  607. $this->price_min_ttc = price2num($this->price_min, 'MU');
  608. $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
  609. $this->price_min = price2num($this->price_min, 'MU');
  610. } else {
  611. $this->price_min = 0;
  612. $this->price_min_ttc = 0;
  613. }
  614. } else {
  615. $this->price = price2num($this->price, 'MU');
  616. $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
  617. $this->price_ttc = price2num($this->price_ttc, 'MU');
  618. if ($this->price_min != '' || $this->price_min == 0) {
  619. $this->price_min = price2num($this->price_min, 'MU');
  620. $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
  621. $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
  622. // print 'X'.$newminprice.'-'.$price_min;
  623. } else {
  624. $this->price_min = 0;
  625. $this->price_min_ttc = 0;
  626. }
  627. }
  628. }
  629. // Do a copy of current record into log table
  630. // Insert request
  631. $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
  632. $sql .= "entity,";
  633. $sql .= "datec,";
  634. $sql .= "fk_product,";
  635. $sql .= "fk_soc,";
  636. $sql .= "ref_customer,";
  637. $sql .= "price,";
  638. $sql .= "price_ttc,";
  639. $sql .= "price_min,";
  640. $sql .= "price_min_ttc,";
  641. $sql .= "price_base_type,";
  642. $sql .= "default_vat_code,";
  643. $sql .= "tva_tx,";
  644. $sql .= "recuperableonly,";
  645. $sql .= "localtax1_tx,";
  646. $sql .= "localtax2_tx,";
  647. $sql .= "localtax1_type,";
  648. $sql .= "localtax2_type,";
  649. $sql .= "fk_user,";
  650. $sql .= "import_key";
  651. $sql .= ") ";
  652. $sql .= "SELECT";
  653. $sql .= " t.entity,";
  654. $sql .= " t.datec,";
  655. $sql .= " t.fk_product,";
  656. $sql .= " t.fk_soc,";
  657. $sql .= " t.ref_customer,";
  658. $sql .= " t.price,";
  659. $sql .= " t.price_ttc,";
  660. $sql .= " t.price_min,";
  661. $sql .= " t.price_min_ttc,";
  662. $sql .= " t.price_base_type,";
  663. $sql .= " t.default_vat_code,";
  664. $sql .= " t.tva_tx,";
  665. $sql .= " t.recuperableonly,";
  666. $sql .= " t.localtax1_tx,";
  667. $sql .= " t.localtax2_tx,";
  668. $sql .= " t.localtax1_type,";
  669. $sql .= " t.localtax2_type,";
  670. $sql .= " t.fk_user,";
  671. $sql .= " t.import_key";
  672. $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
  673. $sql .= " WHERE t.rowid = ".((int) $this->id);
  674. $this->db->begin();
  675. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  676. $resql = $this->db->query($sql);
  677. if (!$resql) {
  678. $error++;
  679. $this->errors [] = "Error ".$this->db->lasterror();
  680. }
  681. // Update request
  682. $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
  683. $sql .= " entity=".$conf->entity.",";
  684. $sql .= " datec='".$this->db->idate(dol_now())."',";
  685. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  686. $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
  687. $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
  688. $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
  689. $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
  690. $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
  691. $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
  692. $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
  693. $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
  694. $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
  695. $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
  696. $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
  697. $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
  698. $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
  699. $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
  700. $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
  701. $sql .= " fk_user=".$user->id.",";
  702. $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
  703. $sql .= " WHERE rowid=".((int) $this->id);
  704. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  705. $resql = $this->db->query($sql);
  706. if (!$resql) {
  707. $error++;
  708. $this->errors [] = "Error ".$this->db->lasterror();
  709. }
  710. if (!$error && !$notrigger) {
  711. // Call trigger
  712. $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
  713. if ($result < 0) {
  714. $error++;
  715. }
  716. // End call triggers
  717. }
  718. if (!$error) {
  719. $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
  720. if ($result < 0) {
  721. $error++;
  722. }
  723. }
  724. // Commit or rollback
  725. if ($error) {
  726. foreach ($this->errors as $errmsg) {
  727. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  728. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  729. }
  730. $this->db->rollback();
  731. return -1 * $error;
  732. } else {
  733. $this->db->commit();
  734. return 1;
  735. }
  736. }
  737. /**
  738. * Force update price on child companies so child company has same prices than parent.
  739. *
  740. * @param User $user User that modifies
  741. * @param int $forceupdateaffiliate update price on each soc child
  742. * @return int <0 if KO, 0 = action disabled, >0 if OK
  743. */
  744. public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
  745. {
  746. global $conf;
  747. if (!empty($conf->global->PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES)) {
  748. return 0;
  749. }
  750. $error = 0;
  751. // Find all susidiaries
  752. $sql = "SELECT s.rowid";
  753. $sql .= " FROM ".$this->db->prefix()."societe as s";
  754. $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
  755. $sql .= " AND s.entity IN (".getEntity('societe').")";
  756. dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
  757. $resql = $this->db->query($sql);
  758. if ($resql) {
  759. $this->lines = array();
  760. $num = $this->db->num_rows($resql);
  761. while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
  762. // find if there is an existing line for the product and the subsidiaries
  763. $prodsocprice = new Productcustomerprice($this->db);
  764. $filter = array(
  765. 't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
  766. );
  767. $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
  768. if ($result < 0) {
  769. $error++;
  770. $this->error = $prodsocprice->error;
  771. } else {
  772. // There is one line
  773. if (count($prodsocprice->lines) > 0) {
  774. // If force update => Update
  775. if (!empty($forceupdateaffiliate)) {
  776. $prodsocpriceupd = new Productcustomerprice($this->db);
  777. $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
  778. $prodsocpriceupd->price = $this->price;
  779. $prodsocpriceupd->price_min = $this->price_min;
  780. $prodsocpriceupd->price_base_type = $this->price_base_type;
  781. $prodsocpriceupd->tva_tx = $this->tva_tx;
  782. $prodsocpriceupd->recuperableonly = $this->recuperableonly;
  783. $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
  784. if ($result < 0) {
  785. $error++;
  786. $this->error = $prodsocpriceupd->error;
  787. }
  788. }
  789. } else {
  790. // If line do not exits then create it
  791. $prodsocpricenew = new Productcustomerprice($this->db);
  792. $prodsocpricenew->fk_soc = $obj->rowid;
  793. $prodsocpricenew->ref_customer = $obj->ref_customer;
  794. $prodsocpricenew->fk_product = $this->fk_product;
  795. $prodsocpricenew->price = $this->price;
  796. $prodsocpricenew->price_min = $this->price_min;
  797. $prodsocpricenew->price_base_type = $this->price_base_type;
  798. $prodsocpricenew->tva_tx = $this->tva_tx;
  799. $prodsocpricenew->recuperableonly = $this->recuperableonly;
  800. $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
  801. if ($result < 0) {
  802. $error++;
  803. $this->error = $prodsocpricenew->error;
  804. }
  805. }
  806. }
  807. }
  808. $this->db->free($resql);
  809. if (empty($error)) {
  810. return 1;
  811. } else {
  812. return -1;
  813. }
  814. } else {
  815. $this->error = "Error ".$this->db->lasterror();
  816. return -1;
  817. }
  818. }
  819. /**
  820. * Delete object in database
  821. *
  822. * @param User $user that deletes
  823. * @param int $notrigger triggers after, 1=disable triggers
  824. * @return int <0 if KO, >0 if OK
  825. */
  826. public function delete($user, $notrigger = 0)
  827. {
  828. global $conf, $langs;
  829. $error = 0;
  830. $this->db->begin();
  831. if (!$error && !$notrigger) {
  832. $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
  833. if ($result < 0) {
  834. $error++;
  835. }
  836. }
  837. if (!$error) {
  838. $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
  839. $sql .= " WHERE rowid=".((int) $this->id);
  840. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  841. $resql = $this->db->query($sql);
  842. if (!$resql) {
  843. $error++;
  844. $this->errors [] = "Error ".$this->db->lasterror();
  845. }
  846. }
  847. // Commit or rollback
  848. if ($error) {
  849. foreach ($this->errors as $errmsg) {
  850. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  851. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  852. }
  853. $this->db->rollback();
  854. return -1 * $error;
  855. } else {
  856. $this->db->commit();
  857. return 1;
  858. }
  859. }
  860. /**
  861. * Load an object from its id and create a new one in database
  862. *
  863. * @param User $user User making the clone
  864. * @param int $fromid ID of object to clone
  865. * @return int id of clone
  866. */
  867. public function createFromClone(User $user, $fromid)
  868. {
  869. $error = 0;
  870. $object = new Productcustomerprice($this->db);
  871. $this->db->begin();
  872. // Load source object
  873. $object->fetch($fromid);
  874. $object->id = 0;
  875. $object->statut = 0;
  876. // Clear fields
  877. // ...
  878. // Create clone
  879. $object->context['createfromclone'] = 'createfromclone';
  880. $result = $object->create($user);
  881. // Other options
  882. if ($result < 0) {
  883. $this->error = $object->error;
  884. $this->errors = array_merge($this->errors, $object->errors);
  885. $error++;
  886. }
  887. if (!$error) {
  888. }
  889. unset($object->context['createfromclone']);
  890. // End
  891. if (!$error) {
  892. $this->db->commit();
  893. return $object->id;
  894. } else {
  895. $this->db->rollback();
  896. return -1;
  897. }
  898. }
  899. /**
  900. * Initialise object with example values
  901. * Id must be 0 if object instance is a specimen
  902. *
  903. * @return void
  904. */
  905. public function initAsSpecimen()
  906. {
  907. $this->id = 0;
  908. $this->entity = '';
  909. $this->datec = '';
  910. $this->tms = '';
  911. $this->fk_product = '';
  912. $this->fk_soc = '';
  913. $this->ref_customer = '';
  914. $this->price = '';
  915. $this->price_ttc = '';
  916. $this->price_min = '';
  917. $this->price_min_ttc = '';
  918. $this->price_base_type = '';
  919. $this->default_vat_code = '';
  920. $this->tva_tx = '';
  921. $this->recuperableonly = '';
  922. $this->localtax1_tx = '';
  923. $this->localtax2_tx = '';
  924. $this->fk_user = '';
  925. $this->import_key = '';
  926. }
  927. }
  928. /**
  929. * File of class to manage predefined price products or services by customer lines
  930. */
  931. class PriceByCustomerLine
  932. {
  933. /**
  934. * @var int ID
  935. */
  936. public $id;
  937. /**
  938. * @var int Entity
  939. */
  940. public $entity;
  941. public $datec = '';
  942. public $tms = '';
  943. /**
  944. * @var int ID
  945. */
  946. public $fk_product;
  947. /**
  948. * @var string Customer reference
  949. */
  950. public $ref_customer;
  951. /**
  952. * @var int Thirdparty ID
  953. */
  954. public $fk_soc;
  955. public $price;
  956. public $price_ttc;
  957. public $price_min;
  958. public $price_min_ttc;
  959. public $price_base_type;
  960. public $default_vat_code;
  961. public $tva_tx;
  962. public $recuperableonly;
  963. public $localtax1_tx;
  964. public $localtax2_tx;
  965. /**
  966. * @var int User ID
  967. */
  968. public $fk_user;
  969. public $import_key;
  970. public $socname;
  971. public $prodref;
  972. }