productcustomerprice.class.php 33 KB

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