123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083 |
- <?php
- /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- /**
- * \file htdocs/product/class/productcustomerprice.class.php
- * \ingroup produit
- * \brief File of class to manage predefined price products or services by customer
- */
- require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
- /**
- * File of class to manage predefined price products or services by customer
- */
- class ProductCustomerPrice extends CommonObject
- {
- /**
- * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
- */
- public $fields = array(
- '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),
- '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'),
- 'ref_customer' => array('type'=>'varchar(128)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1,),
- 'datec' => array('type'=>'datetime', 'label'=>'AppliedPricesFrom', 'enabled'=>1, 'visible'=>1, 'position'=>500, 'notnull'=>1,),
- 'price_base_type' => array('type' => 'varchar(255)', 'label' => 'PriceBase', 'enabled' => 1, 'visible' => 1, 'position' => 11, 'notnull' => -1, 'comment' => 'Price Base Type'),
- 'tva_tx' => array('type' => 'decimal(20,6)', 'label' => 'VAT', 'enabled' => 1, 'visible' => 1, 'position' => 12, 'notnull' => -1, 'comment' => 'TVA Tax Rate'),
- 'price' => array('type' => 'decimal(20,6)', 'label' => 'HT', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price HT'),
- 'price_ttc' => array('type' => 'decimal(20,6)', 'label' => 'TTC', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price TTC'),
- 'price_min' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceHT', 'enabled' => 1, 'visible' => 1, 'position' => 9, 'notnull' => -1, 'comment' => 'Minimum Price'),
- 'price_min_ttc' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceTTC', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => -1, 'comment' => 'Minimum Price TTC'),
- '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'),
- );
- /**
- * @var string ID to identify managed object
- */
- public $element = 'product_customer_price';
- /**
- * @var string Name of table without prefix where object is stored
- */
- public $table_element = 'product_customer_price';
- /**
- * @var int Entity
- */
- public $entity;
- public $datec = '';
- public $tms = '';
- /**
- * @var int ID
- */
- public $fk_product;
- /**
- * @var int Thirdparty ID
- */
- public $fk_soc;
- /**
- * @var string Customer reference
- */
- public $ref_customer;
- public $price;
- public $price_ttc;
- public $price_min;
- public $price_min_ttc;
- public $price_base_type;
- public $default_vat_code;
- public $tva_tx;
- public $recuperableonly;
- public $localtax1_type;
- public $localtax1_tx;
- public $localtax2_type;
- public $localtax2_tx;
- /**
- * @var int User ID
- */
- public $fk_user;
- /**
- * @var PriceByCustomerLine[]
- */
- public $lines = array();
- /**
- * Constructor
- *
- * @param DoliDb $db handler
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * Create object into database
- *
- * @param User $user that creates
- * @param int $notrigger triggers after, 1=disable triggers
- * @param int $forceupdateaffiliate update price on each soc child
- * @return int Return integer <0 if KO, Id of created object if OK
- */
- public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
- {
- global $conf, $langs;
- $error = 0;
- // Clean parameters
- if (isset($this->entity)) {
- $this->entity = trim($this->entity);
- }
- if (isset($this->fk_product)) {
- $this->fk_product = trim($this->fk_product);
- }
- if (isset($this->fk_soc)) {
- $this->fk_soc = trim($this->fk_soc);
- }
- if (isset($this->ref_customer)) {
- $this->ref_customer = trim($this->ref_customer);
- }
- if (isset($this->price)) {
- $this->price = trim($this->price);
- }
- if (isset($this->price_ttc)) {
- $this->price_ttc = trim($this->price_ttc);
- }
- if (isset($this->price_min)) {
- $this->price_min = trim($this->price_min);
- }
- if (isset($this->price_min_ttc)) {
- $this->price_min_ttc = trim($this->price_min_ttc);
- }
- if (isset($this->price_base_type)) {
- $this->price_base_type = trim($this->price_base_type);
- }
- if (isset($this->tva_tx)) {
- $this->tva_tx = (float) $this->tva_tx;
- }
- if (isset($this->recuperableonly)) {
- $this->recuperableonly = trim($this->recuperableonly);
- }
- if (isset($this->localtax1_tx)) {
- $this->localtax1_tx = trim($this->localtax1_tx);
- }
- if (isset($this->localtax2_tx)) {
- $this->localtax2_tx = trim($this->localtax2_tx);
- }
- if (isset($this->fk_user)) {
- $this->fk_user = trim($this->fk_user);
- }
- if (isset($this->import_key)) {
- $this->import_key = trim($this->import_key);
- }
- // Check parameters
- // Put here code to add control on parameters values
- if ($this->price != '' || $this->price == 0) {
- if ($this->price_base_type == 'TTC') {
- $this->price_ttc = price2num($this->price, 'MU');
- $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
- $this->price = price2num($this->price, 'MU');
- if ($this->price_min != '' || $this->price_min == 0) {
- $this->price_min_ttc = price2num($this->price_min, 'MU');
- $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
- $this->price_min = price2num($this->price_min, 'MU');
- } else {
- $this->price_min = 0;
- $this->price_min_ttc = 0;
- }
- } else {
- $this->price = price2num($this->price, 'MU');
- $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
- $this->price_ttc = price2num($this->price_ttc, 'MU');
- if ($this->price_min != '' || $this->price_min == 0) {
- $this->price_min = price2num($this->price_min, 'MU');
- $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
- $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
- // print 'X'.$newminprice.'-'.$price_min;
- } else {
- $this->price_min = 0;
- $this->price_min_ttc = 0;
- }
- }
- }
- // Insert request
- $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
- $sql .= "entity,";
- $sql .= "datec,";
- $sql .= "fk_product,";
- $sql .= "fk_soc,";
- $sql .= 'ref_customer,';
- $sql .= "price,";
- $sql .= "price_ttc,";
- $sql .= "price_min,";
- $sql .= "price_min_ttc,";
- $sql .= "price_base_type,";
- $sql .= "default_vat_code,";
- $sql .= "tva_tx,";
- $sql .= "recuperableonly,";
- $sql .= "localtax1_type,";
- $sql .= "localtax1_tx,";
- $sql .= "localtax2_type,";
- $sql .= "localtax2_tx,";
- $sql .= "fk_user,";
- $sql .= "import_key";
- $sql .= ") VALUES (";
- $sql .= " ".((int) $conf->entity).",";
- $sql .= " '".$this->db->idate(dol_now())."',";
- $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
- $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
- $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
- $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
- $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
- $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
- $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
- $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
- $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
- $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
- $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
- $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
- $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
- $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
- $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
- $sql .= " ".((int) $user->id).",";
- $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
- $sql .= ")";
- $this->db->begin();
- dol_syslog(get_class($this)."::create", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- $this->errors [] = "Error ".$this->db->lasterror();
- }
- if (!$error) {
- $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
- if (!$notrigger) {
- $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
- if ($result < 0) {
- $error++;
- }
- }
- }
- if (!$error) {
- $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
- if ($result < 0) {
- $error++;
- }
- }
- // Commit or rollback
- if ($error) {
- foreach ($this->errors as $errmsg) {
- dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
- $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
- }
- $this->db->rollback();
- return -1 * $error;
- } else {
- $this->db->commit();
- return $this->id;
- }
- }
- /**
- * Load object in memory from the database
- *
- * @param int $id ID of customer price
- * @return int Return integer <0 if KO, 0 if not found, >0 if OK
- */
- public function fetch($id)
- {
- global $langs;
- $sql = "SELECT";
- $sql .= " t.rowid,";
- $sql .= " t.entity,";
- $sql .= " t.datec,";
- $sql .= " t.tms,";
- $sql .= " t.fk_product,";
- $sql .= " t.fk_soc,";
- $sql .= " t.ref_customer,";
- $sql .= " t.price,";
- $sql .= " t.price_ttc,";
- $sql .= " t.price_min,";
- $sql .= " t.price_min_ttc,";
- $sql .= " t.price_base_type,";
- $sql .= " t.default_vat_code,";
- $sql .= " t.tva_tx,";
- $sql .= " t.recuperableonly,";
- $sql .= " t.localtax1_tx,";
- $sql .= " t.localtax2_tx,";
- $sql .= " t.fk_user,";
- $sql .= " t.import_key";
- $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
- $sql .= " WHERE t.rowid = ".((int) $id);
- dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- if ($this->db->num_rows($resql)) {
- $obj = $this->db->fetch_object($resql);
- $this->id = $obj->rowid;
- $this->entity = $obj->entity;
- $this->datec = $this->db->jdate($obj->datec);
- $this->tms = $this->db->jdate($obj->tms);
- $this->fk_product = $obj->fk_product;
- $this->fk_soc = $obj->fk_soc;
- $this->ref_customer = $obj->ref_customer;
- $this->price = $obj->price;
- $this->price_ttc = $obj->price_ttc;
- $this->price_min = $obj->price_min;
- $this->price_min_ttc = $obj->price_min_ttc;
- $this->price_base_type = $obj->price_base_type;
- $this->default_vat_code = $obj->default_vat_code;
- $this->tva_tx = $obj->tva_tx;
- $this->recuperableonly = $obj->recuperableonly;
- $this->localtax1_tx = $obj->localtax1_tx;
- $this->localtax2_tx = $obj->localtax2_tx;
- $this->fk_user = $obj->fk_user;
- $this->import_key = $obj->import_key;
- $this->db->free($resql);
- return 1;
- } else {
- $this->db->free($resql);
- return 0;
- }
- } else {
- $this->error = "Error ".$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Load all customer prices in memory from database
- *
- * @param string $sortorder order
- * @param string $sortfield field
- * @param int $limit page
- * @param int $offset offset
- * @param array $filter Filter for select
- * @return int Return integer <0 if KO, >0 if OK
- * @since dolibarr v17
- */
- public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
- {
- if (empty($sortfield)) {
- $sortfield = "t.rowid";
- }
- if (empty($sortorder)) {
- $sortorder = "DESC";
- }
- $sql = "SELECT";
- $sql .= " t.rowid,";
- $sql .= " t.entity,";
- $sql .= " t.datec,";
- $sql .= " t.tms,";
- $sql .= " t.fk_product,";
- $sql .= " t.fk_soc,";
- $sql .= " t.ref_customer,";
- $sql .= " t.price,";
- $sql .= " t.price_ttc,";
- $sql .= " t.price_min,";
- $sql .= " t.price_min_ttc,";
- $sql .= " t.price_base_type,";
- $sql .= " t.default_vat_code,";
- $sql .= " t.tva_tx,";
- $sql .= " t.recuperableonly,";
- $sql .= " t.localtax1_tx,";
- $sql .= " t.localtax2_tx,";
- $sql .= " t.localtax1_type,";
- $sql .= " t.localtax2_type,";
- $sql .= " t.fk_user,";
- $sql .= " t.import_key,";
- $sql .= " soc.nom as socname,";
- $sql .= " prod.ref as prodref";
- $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
- $sql .= " ".$this->db->prefix()."product as prod,";
- $sql .= " ".$this->db->prefix()."societe as soc";
- $sql .= " WHERE soc.rowid=t.fk_soc ";
- $sql .= " AND prod.rowid=t.fk_product ";
- $sql .= " AND prod.entity IN (".getEntity('product').")";
- $sql .= " AND t.entity IN (".getEntity('productprice').")";
- // Manage filter
- if (count($filter) > 0) {
- foreach ($filter as $key => $value) {
- if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
- $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
- } elseif ($key == 'soc.nom') {
- $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
- } elseif ($key == 'prod.ref' || $key == 'prod.label') {
- $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
- } elseif ($key == 't.price' || $key == 't.price_ttc') {
- $sql .= " AND ".$key." LIKE '%".price2num($value)."%'";
- } else {
- $sql .= " AND ".$key." = ".((int) $value);
- }
- }
- }
- $sql .= $this->db->order($sortfield, $sortorder);
- if (!empty($limit)) {
- $sql .= $this->db->plimit($limit + 1, $offset);
- }
- dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $this->lines = array();
- $num = $this->db->num_rows($resql);
- while ($obj = $this->db->fetch_object($resql)) {
- $line = new PriceByCustomerLine();
- $line->id = $obj->rowid;
- $line->entity = $obj->entity;
- $line->datec = $this->db->jdate($obj->datec);
- $line->tms = $this->db->jdate($obj->tms);
- $line->fk_product = $obj->fk_product;
- $line->fk_soc = $obj->fk_soc;
- $line->ref_customer = $obj->ref_customer;
- $line->price = $obj->price;
- $line->price_ttc = $obj->price_ttc;
- $line->price_min = $obj->price_min;
- $line->price_min_ttc = $obj->price_min_ttc;
- $line->price_base_type = $obj->price_base_type;
- $line->default_vat_code = $obj->default_vat_code;
- $line->tva_tx = $obj->tva_tx;
- $line->recuperableonly = $obj->recuperableonly;
- $line->localtax1_tx = $obj->localtax1_tx;
- $line->localtax2_tx = $obj->localtax2_tx;
- $line->localtax1_type = $obj->localtax1_type;
- $line->localtax2_type = $obj->localtax2_type;
- $line->fk_user = $obj->fk_user;
- $line->import_key = $obj->import_key;
- $line->socname = $obj->socname;
- $line->prodref = $obj->prodref;
- $this->lines[] = $line;
- }
- $this->db->free($resql);
- return $num;
- } else {
- $this->error = "Error ".$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Load all objects in memory from database
- *
- * @param string $sortorder order
- * @param string $sortfield field
- * @param int $limit page
- * @param int $offset offset
- * @param array $filter Filter for sql request
- * @return int Return integer <0 if KO, >0 if OK
- */
- public function fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter = array())
- {
- if (!empty($sortfield)) {
- $sortfield = "t.rowid";
- }
- if (!empty($sortorder)) {
- $sortorder = "DESC";
- }
- $sql = "SELECT";
- $sql .= " t.rowid,";
- $sql .= " t.entity,";
- $sql .= " t.datec,";
- $sql .= " t.fk_product,";
- $sql .= " t.fk_soc,";
- $sql .= " t.ref_customer,";
- $sql .= " t.price,";
- $sql .= " t.price_ttc,";
- $sql .= " t.price_min,";
- $sql .= " t.price_min_ttc,";
- $sql .= " t.price_base_type,";
- $sql .= " t.default_vat_code,";
- $sql .= " t.tva_tx,";
- $sql .= " t.recuperableonly,";
- $sql .= " t.localtax1_tx,";
- $sql .= " t.localtax2_tx,";
- $sql .= " t.fk_user,";
- $sql .= " t.import_key,";
- $sql .= " soc.nom as socname,";
- $sql .= " prod.ref as prodref";
- $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
- $sql .= " ,".$this->db->prefix()."product as prod";
- $sql .= " ,".$this->db->prefix()."societe as soc";
- $sql .= " WHERE soc.rowid=t.fk_soc";
- $sql .= " AND prod.rowid=t.fk_product ";
- $sql .= " AND prod.entity IN (".getEntity('product').")";
- $sql .= " AND t.entity IN (".getEntity('productprice').")";
- // Manage filter
- if (count($filter) > 0) {
- foreach ($filter as $key => $value) {
- if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
- $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
- } elseif ($key == 'soc.nom') {
- $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
- } else {
- $sql .= " AND ".$key." = ".((int) $value);
- }
- }
- }
- $sql .= $this->db->order($sortfield, $sortorder);
- if (!empty($limit)) {
- $sql .= $this->db->plimit($limit + 1, $offset);
- }
- dol_syslog(get_class($this)."::fetchAllLog", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $this->lines = array();
- $num = $this->db->num_rows($resql);
- while ($obj = $this->db->fetch_object($resql)) {
- $line = new PriceByCustomerLine();
- $line->id = $obj->rowid;
- $line->entity = $obj->entity;
- $line->datec = $this->db->jdate($obj->datec);
- $line->tms = $this->db->jdate($obj->tms);
- $line->fk_product = $obj->fk_product;
- $line->fk_soc = $obj->fk_soc;
- $line->ref_customer = $obj->ref_customer;
- $line->price = $obj->price;
- $line->price_ttc = $obj->price_ttc;
- $line->price_min = $obj->price_min;
- $line->price_min_ttc = $obj->price_min_ttc;
- $line->price_base_type = $obj->price_base_type;
- $line->default_vat_code = $obj->default_vat_code;
- $line->tva_tx = $obj->tva_tx;
- $line->recuperableonly = $obj->recuperableonly;
- $line->localtax1_tx = $obj->localtax1_tx;
- $line->localtax2_tx = $obj->localtax2_tx;
- $line->fk_user = $obj->fk_user;
- $line->import_key = $obj->import_key;
- $line->socname = $obj->socname;
- $line->prodref = $obj->prodref;
- $this->lines [] = $line;
- }
- $this->db->free($resql);
- return $num;
- } else {
- $this->error = "Error ".$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Update object into database
- *
- * @param User $user that modifies
- * @param int $notrigger triggers after, 1=disable triggers
- * @param int $forceupdateaffiliate update price on each soc child
- * @return int Return integer <0 if KO, >0 if OK
- */
- public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
- {
- global $conf, $langs;
- $error = 0;
- // Clean parameters
- if (isset($this->entity)) {
- $this->entity = trim($this->entity);
- }
- if (isset($this->fk_product)) {
- $this->fk_product = trim($this->fk_product);
- }
- if (isset($this->fk_soc)) {
- $this->fk_soc = trim($this->fk_soc);
- }
- if (isset($this->ref_customer)) {
- $this->ref_customer = trim($this->ref_customer);
- }
- if (isset($this->price)) {
- $this->price = trim($this->price);
- }
- if (isset($this->price_ttc)) {
- $this->price_ttc = trim($this->price_ttc);
- }
- if (isset($this->price_min)) {
- $this->price_min = trim($this->price_min);
- }
- if (isset($this->price_min_ttc)) {
- $this->price_min_ttc = trim($this->price_min_ttc);
- }
- if (isset($this->price_base_type)) {
- $this->price_base_type = trim($this->price_base_type);
- }
- if (isset($this->tva_tx)) {
- $this->tva_tx = (float) $this->tva_tx;
- }
- if (isset($this->recuperableonly)) {
- $this->recuperableonly = trim($this->recuperableonly);
- }
- if (isset($this->localtax1_tx)) {
- $this->localtax1_tx = trim($this->localtax1_tx);
- }
- if (isset($this->localtax2_tx)) {
- $this->localtax2_tx = trim($this->localtax2_tx);
- }
- if (isset($this->fk_user)) {
- $this->fk_user = trim($this->fk_user);
- }
- if (isset($this->import_key)) {
- $this->import_key = trim($this->import_key);
- }
- // Check parameters
- // Put here code to add a control on parameters values
- if ($this->price != '' || $this->price == 0) {
- if ($this->price_base_type == 'TTC') {
- $this->price_ttc = price2num($this->price, 'MU');
- $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
- $this->price = price2num($this->price, 'MU');
- if ($this->price_min != '' || $this->price_min == 0) {
- $this->price_min_ttc = price2num($this->price_min, 'MU');
- $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
- $this->price_min = price2num($this->price_min, 'MU');
- } else {
- $this->price_min = 0;
- $this->price_min_ttc = 0;
- }
- } else {
- $this->price = price2num($this->price, 'MU');
- $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
- $this->price_ttc = price2num($this->price_ttc, 'MU');
- if ($this->price_min != '' || $this->price_min == 0) {
- $this->price_min = price2num($this->price_min, 'MU');
- $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
- $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
- // print 'X'.$newminprice.'-'.$price_min;
- } else {
- $this->price_min = 0;
- $this->price_min_ttc = 0;
- }
- }
- }
- // Do a copy of current record into log table
- // Insert request
- $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
- $sql .= "entity,";
- $sql .= "datec,";
- $sql .= "fk_product,";
- $sql .= "fk_soc,";
- $sql .= "ref_customer,";
- $sql .= "price,";
- $sql .= "price_ttc,";
- $sql .= "price_min,";
- $sql .= "price_min_ttc,";
- $sql .= "price_base_type,";
- $sql .= "default_vat_code,";
- $sql .= "tva_tx,";
- $sql .= "recuperableonly,";
- $sql .= "localtax1_tx,";
- $sql .= "localtax2_tx,";
- $sql .= "localtax1_type,";
- $sql .= "localtax2_type,";
- $sql .= "fk_user,";
- $sql .= "import_key";
- $sql .= ") ";
- $sql .= "SELECT";
- $sql .= " t.entity,";
- $sql .= " t.datec,";
- $sql .= " t.fk_product,";
- $sql .= " t.fk_soc,";
- $sql .= " t.ref_customer,";
- $sql .= " t.price,";
- $sql .= " t.price_ttc,";
- $sql .= " t.price_min,";
- $sql .= " t.price_min_ttc,";
- $sql .= " t.price_base_type,";
- $sql .= " t.default_vat_code,";
- $sql .= " t.tva_tx,";
- $sql .= " t.recuperableonly,";
- $sql .= " t.localtax1_tx,";
- $sql .= " t.localtax2_tx,";
- $sql .= " t.localtax1_type,";
- $sql .= " t.localtax2_type,";
- $sql .= " t.fk_user,";
- $sql .= " t.import_key";
- $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
- $sql .= " WHERE t.rowid = ".((int) $this->id);
- $this->db->begin();
- dol_syslog(get_class($this)."::update", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- $this->errors [] = "Error ".$this->db->lasterror();
- }
- // Update request
- $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
- $sql .= " entity=".$conf->entity.",";
- $sql .= " datec='".$this->db->idate(dol_now())."',";
- $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
- $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
- $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
- $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
- $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
- $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
- $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
- $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
- $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
- $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
- $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
- $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
- $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
- $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
- $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
- $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
- $sql .= " fk_user=".$user->id.",";
- $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
- $sql .= " WHERE rowid=".((int) $this->id);
- dol_syslog(get_class($this)."::update", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- $this->errors [] = "Error ".$this->db->lasterror();
- }
- if (!$error && !$notrigger) {
- // Call trigger
- $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- }
- if (!$error) {
- $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
- if ($result < 0) {
- $error++;
- }
- }
- // Commit or rollback
- if ($error) {
- foreach ($this->errors as $errmsg) {
- dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
- $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
- }
- $this->db->rollback();
- return -1 * $error;
- } else {
- $this->db->commit();
- return 1;
- }
- }
- /**
- * Force update price on child companies so child company has same prices than parent.
- *
- * @param User $user User that modifies
- * @param int $forceupdateaffiliate update price on each soc child
- * @return int Return integer <0 if KO, 0 = action disabled, >0 if OK
- */
- public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
- {
- global $conf;
- if (getDolGlobalString('PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES')) {
- return 0;
- }
- $error = 0;
- // Find all susidiaries
- $sql = "SELECT s.rowid";
- $sql .= " FROM ".$this->db->prefix()."societe as s";
- $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
- $sql .= " AND s.entity IN (".getEntity('societe').")";
- dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $this->lines = array();
- $num = $this->db->num_rows($resql);
- while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
- // find if there is an existing line for the product and the subsidiaries
- $prodsocprice = new ProductCustomerPrice($this->db);
- $filter = array(
- 't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
- );
- $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
- if ($result < 0) {
- $error++;
- $this->error = $prodsocprice->error;
- } else {
- // There is one line
- if (count($prodsocprice->lines) > 0) {
- // If force update => Update
- if (!empty($forceupdateaffiliate)) {
- $prodsocpriceupd = new ProductCustomerPrice($this->db);
- $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
- $prodsocpriceupd->price = $this->price;
- $prodsocpriceupd->price_min = $this->price_min;
- $prodsocpriceupd->price_base_type = $this->price_base_type;
- $prodsocpriceupd->tva_tx = $this->tva_tx;
- $prodsocpriceupd->recuperableonly = $this->recuperableonly;
- $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
- if ($resultupd < 0) {
- $error++;
- $this->error = $prodsocpriceupd->error;
- }
- }
- } else {
- // If line do not exits then create it
- $prodsocpricenew = new ProductCustomerPrice($this->db);
- $prodsocpricenew->fk_soc = $obj->rowid;
- $prodsocpricenew->ref_customer = $obj->ref_customer;
- $prodsocpricenew->fk_product = $this->fk_product;
- $prodsocpricenew->price = $this->price;
- $prodsocpricenew->price_min = $this->price_min;
- $prodsocpricenew->price_base_type = $this->price_base_type;
- $prodsocpricenew->tva_tx = $this->tva_tx;
- $prodsocpricenew->recuperableonly = $this->recuperableonly;
- $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
- if ($resultupd < 0) {
- $error++;
- $this->error = $prodsocpricenew->error;
- }
- }
- }
- }
- $this->db->free($resql);
- if (empty($error)) {
- return 1;
- } else {
- return -1;
- }
- } else {
- $this->error = "Error ".$this->db->lasterror();
- return -1;
- }
- }
- /**
- * Delete object in database
- *
- * @param User $user that deletes
- * @param int $notrigger triggers after, 1=disable triggers
- * @return int Return integer <0 if KO, >0 if OK
- */
- public function delete($user, $notrigger = 0)
- {
- global $conf, $langs;
- $error = 0;
- $this->db->begin();
- if (!$notrigger) {
- $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
- if ($result < 0) {
- $error++;
- }
- }
- if (!$error) {
- $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
- $sql .= " WHERE rowid=".((int) $this->id);
- dol_syslog(get_class($this)."::delete", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $error++;
- $this->errors [] = "Error ".$this->db->lasterror();
- }
- }
- // Commit or rollback
- if ($error) {
- foreach ($this->errors as $errmsg) {
- dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
- $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
- }
- $this->db->rollback();
- return -1 * $error;
- } else {
- $this->db->commit();
- return 1;
- }
- }
- /**
- * Load an object from its id and create a new one in database
- *
- * @param User $user User making the clone
- * @param int $fromid ID of object to clone
- * @return int id of clone
- */
- public function createFromClone(User $user, $fromid)
- {
- $error = 0;
- $object = new ProductCustomerPrice($this->db);
- $this->db->begin();
- // Load source object
- $object->fetch($fromid);
- $object->id = 0;
- $object->statut = 0;
- // Clear fields
- // ...
- // Create clone
- $object->context['createfromclone'] = 'createfromclone';
- $result = $object->create($user);
- // Other options
- if ($result < 0) {
- $this->error = $object->error;
- $this->errors = array_merge($this->errors, $object->errors);
- $error++;
- }
- if (!$error) {
- }
- unset($object->context['createfromclone']);
- // End
- if (!$error) {
- $this->db->commit();
- return $object->id;
- } else {
- $this->db->rollback();
- return -1;
- }
- }
- /**
- * Initialise object with example values
- * Id must be 0 if object instance is a specimen
- *
- * @return void
- */
- public function initAsSpecimen()
- {
- $this->id = 0;
- $this->entity = '';
- $this->datec = '';
- $this->tms = '';
- $this->fk_product = '';
- $this->fk_soc = '';
- $this->ref_customer = '';
- $this->price = '';
- $this->price_ttc = '';
- $this->price_min = '';
- $this->price_min_ttc = '';
- $this->price_base_type = '';
- $this->default_vat_code = '';
- $this->tva_tx = '';
- $this->recuperableonly = '';
- $this->localtax1_tx = '';
- $this->localtax2_tx = '';
- $this->fk_user = '';
- $this->import_key = '';
- }
- }
- /**
- * File of class to manage predefined price products or services by customer lines
- */
- class PriceByCustomerLine
- {
- /**
- * @var int ID
- */
- public $id;
- /**
- * @var int Entity
- */
- public $entity;
- public $datec = '';
- public $tms = '';
- /**
- * @var int ID
- */
- public $fk_product;
- /**
- * @var string Customer reference
- */
- public $ref_customer;
- /**
- * @var int Thirdparty ID
- */
- public $fk_soc;
- public $price;
- public $price_ttc;
- public $price_min;
- public $price_min_ttc;
- public $price_base_type;
- public $default_vat_code;
- public $tva_tx;
- public $recuperableonly;
- public $localtax1_tx;
- public $localtax2_tx;
- /**
- * @var int User ID
- */
- public $fk_user;
- public $import_key;
- public $socname;
- public $prodref;
- }
|