--- /tmp/dsg/dolibarr/htdocs/commande/class/github_19.0.3_api_orders.class.php +++ /tmp/dsg/dolibarr/htdocs/commande/class/client_api_orders.class.php @@ -31,25 +31,32 @@ - /** - * @var array $FIELDS Mandatory fields, checked when create and update object - */ - public static $FIELDS = array( - 'socid', - 'date' - ); - - /** - * @var Commande $commande {@type Commande} - */ - public $commande; - - /** - * Constructor - */ - public function __construct() - { - global $db, $conf; - $this->db = $db; - $this->commande = new Commande($this->db); - } - - /** - * Get properties of an order object by id + + /** + * @var array $FIELDS Mandatory fields, checked when create and update object + */ + static $FIELDS = array( + 'socid', + 'date' + ); + + /** + * @var Commande $commande {@type Commande} + */ + public $commande; + + /** + * Constructor + */ + public function __construct() + { + global $db, $conf; + $this->db = $db; + $this->commande = new Commande($this->db); + } + + /** + * Get properties of an order object by id + * + * Return an array with order informations + * + * @param int $id ID of order + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information @@ -57,55 +64,49 @@ - * Return an array with order informations - * - * @param int $id ID of order - * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id - * @return array|mixed data without useless information - * - * @throws RestException - */ - public function get($id, $contact_list = 1) - { - return $this->_fetch($id, '', '', $contact_list); - } - - /** - * Get properties of an order object by ref - * - * Return an array with order informations - * - * @param string $ref Ref of object - * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id - * @return array|mixed data without useless information - * - * @url GET ref/{ref} - * - * @throws RestException - */ - public function getByRef($ref, $contact_list = 1) - { - return $this->_fetch('', $ref, '', $contact_list); - } - - /** - * Get properties of an order object by ref_ext - * - * Return an array with order informations - * - * @param string $ref_ext External reference of object - * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id - * @return array|mixed data without useless information - * - * @url GET ref_ext/{ref_ext} - * - * @throws RestException - */ - public function getByRefExt($ref_ext, $contact_list = 1) - { - return $this->_fetch('', '', $ref_ext, $contact_list); - } - - /** - * Get properties of an order object - * - * Return an array with order informations - * - * @param int $id ID of order + * @throws RestException + */ + public function get($id, $contact_list = 1) + { + return $this->_fetch($id, '', '', $contact_list); + } + + /** + * Get properties of an order object by ref + * + * Return an array with order informations + * + * @param string $ref Ref of object + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @url GET ref/{ref} + * + * @throws RestException + */ + public function getByRef($ref, $contact_list = 1) + { + return $this->_fetch('', $ref, '', $contact_list); + } + + /** + * Get properties of an order object by ref_ext + * + * Return an array with order informations + * + * @param string $ref_ext External reference of object + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @url GET ref_ext/{ref_ext} + * + * @throws RestException + */ + public function getByRefExt($ref_ext, $contact_list = 1) + { + return $this->_fetch('', '', $ref_ext, $contact_list); + } + + /** + * Get properties of an order object + * + * Return an array with order informations + * + * @param int $id ID of order @@ -114,8 +115,167 @@ - * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id - * @return Object Object with cleaned properties - * - * @throws RestException - */ - private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1) - { - if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) { + * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id + * @return array|mixed data without useless information + * + * @throws RestException + */ + private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1) + { + if (!DolibarrApiAccess::$user->rights->commande->lire) { + throw new RestException(401); + } + + $result = $this->commande->fetch($id, $ref, $ref_ext); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + // Add external contacts ids + $this->commande->contacts_ids = $this->commande->liste_contact(-1, 'external', $contact_list); + $this->commande->fetchObjectLinked(); + return $this->_cleanObjectDatas($this->commande); + } + + /** + * List orders + * + * Get a list of orders + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit for list + * @param int $page Page number + * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" + * @return array Array of order objects + * + * @throws RestException 404 Not found + * @throws RestException 503 Error + */ + public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') + { + global $db, $conf; + + $obj_ret = array(); + + // case of external user, $thirdparty_ids param is ignored and replaced by user's socid + $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; + + // If the internal user must only see his customers, force searching by him + $search_sale = 0; + if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; + + $sql = "SELECT t.rowid"; + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) + $sql .= " FROM ".MAIN_DB_PREFIX."commande as t"; + + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale + + $sql .= ' WHERE t.entity IN ('.getEntity('commande').')'; + if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc"; + if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")"; + if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale + // Insert sale filter + if ($search_sale > 0) + { + $sql .= " AND sc.fk_user = ".$search_sale; + } + // Add sql filters + if ($sqlfilters) + { + if (!DolibarrApi::_checkFilters($sqlfilters)) + { + throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); + } + $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; + $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + + $sql .= $db->order($sortfield, $sortorder); + if ($limit) { + if ($page < 0) + { + $page = 0; + } + $offset = $limit * $page; + + $sql .= $db->plimit($limit + 1, $offset); + } + + dol_syslog("API Rest request"); + $result = $db->query($sql); + + if ($result) + { + $num = $db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + $i = 0; + while ($i < $min) + { + $obj = $db->fetch_object($result); + $commande_static = new Commande($db); + if ($commande_static->fetch($obj->rowid)) { + // Add external contacts ids + $commande_static->contacts_ids = $commande_static->liste_contact(-1, 'external', 1); + $obj_ret[] = $this->_cleanObjectDatas($commande_static); + } + $i++; + } + } + else { + throw new RestException(503, 'Error when retrieve commande list : '.$db->lasterror()); + } + if (!count($obj_ret)) { + throw new RestException(404, 'No order found'); + } + return $obj_ret; + } + + /** + * Create a sale order + * + * Exemple: { "socid": 2, "date": 1595196000, "type": 0, "lines": [{ "fk_product": 2, "qty": 1 }] } + * + * @param array $request_data Request data + * @return int ID of order + */ + public function post($request_data = null) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401, "Insuffisant rights"); + } + // Check mandatory fields + $result = $this->_validate($request_data); + + foreach ($request_data as $field => $value) { + $this->commande->$field = $value; + } + /*if (isset($request_data["lines"])) { + $lines = array(); + foreach ($request_data["lines"] as $line) { + array_push($lines, (object) $line); + } + $this->commande->lines = $lines; + }*/ + + if ($this->commande->create(DolibarrApiAccess::$user) < 0) { + throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors)); + } + + return $this->commande->id; + } + + /** + * Get lines of an order + * + * @param int $id Id of order + * + * @url GET {id}/lines + * + * @return int + */ + public function getLines($id) + { + if (!DolibarrApiAccess::$user->rights->commande->lire) { @@ -125,4 +285,4 @@ - $result = $this->commande->fetch($id, $ref, $ref_ext); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -132,37 +292,22 @@ - } - - // Add external contacts ids - $tmparray = $this->commande->liste_contact(-1, 'external', $contact_list); - if (is_array($tmparray)) { - $this->commande->contacts_ids = $tmparray; - } - $this->commande->fetchObjectLinked(); - - // Add online_payment_url, cf #20477 - require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; - $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref); - - return $this->_cleanObjectDatas($this->commande); - } - - /** - * List orders - * - * Get a list of orders - * - * @param string $sortfield Sort field - * @param string $sortorder Sort order - * @param int $limit Limit for list - * @param int $page Page number - * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} - * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" - * @param string $sqlfilterlines Other criteria to filter answers separated by a comma. Syntax example "(tl.fk_product:=:'17') and (tl.price:<:'250')" - * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names - * @return array Array of order objects - * - * @throws RestException 404 Not found - * @throws RestException 503 Error - */ - public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '', $properties = '') - { - if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) { + } + $this->commande->getLinesArray(); + $result = array(); + foreach ($this->commande->lines as $line) { + array_push($result, $this->_cleanObjectDatas($line)); + } + return $result; + } + + /** + * Add a line to given order + * + * @param int $id Id of order to update + * @param array $request_data OrderLine data + * + * @url POST {id}/lines + * + * @return int + */ + public function postLine($id, $request_data = null) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -172,153 +317,4 @@ - $obj_ret = array(); - - // case of external user, $thirdparty_ids param is ignored and replaced by user's socid - $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; - - // If the internal user must only see his customers, force searching by him - $search_sale = 0; - if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) { - $search_sale = DolibarrApiAccess::$user->id; - } - - $sql = "SELECT t.rowid"; - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) - } - $sql .= " FROM ".MAIN_DB_PREFIX."commande AS t LEFT JOIN ".MAIN_DB_PREFIX."commande_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields - - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale - } - - $sql .= ' WHERE t.entity IN ('.getEntity('commande').')'; - if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) { - $sql .= " AND t.fk_soc = sc.fk_soc"; - } - if ($socids) { - $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")"; - } - if ($search_sale > 0) { - $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale - } - // Insert sale filter - if ($search_sale > 0) { - $sql .= " AND sc.fk_user = ".((int) $search_sale); - } - // Add sql filters - if ($sqlfilters) { - $errormessage = ''; - $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); - if ($errormessage) { - throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); - } - } - // Add sql filters for lines - if ($sqlfilterlines) { - $errormessage = ''; - $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid"; - $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage); - $sql .= ")"; - if ($errormessage) { - throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage); - } - } - $sql .= $this->db->order($sortfield, $sortorder); - if ($limit) { - if ($page < 0) { - $page = 0; - } - $offset = $limit * $page; - - $sql .= $this->db->plimit($limit + 1, $offset); - } - - dol_syslog("API Rest request"); - $result = $this->db->query($sql); - - if ($result) { - $num = $this->db->num_rows($result); - $min = min($num, ($limit <= 0 ? $num : $limit)); - $i = 0; - while ($i < $min) { - $obj = $this->db->fetch_object($result); - $commande_static = new Commande($this->db); - if ($commande_static->fetch($obj->rowid)) { - // Add external contacts ids - $tmparray = $commande_static->liste_contact(-1, 'external', 1); - if (is_array($tmparray)) { - $commande_static->contacts_ids = $tmparray; - } - // Add online_payment_url, cf #20477 - require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; - $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref); - - $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($commande_static), $properties); - } - $i++; - } - } else { - throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror()); - } - - return $obj_ret; - } - - /** - * Create a sale order - * - * Exemple: { "socid": 2, "date": 1595196000, "type": 0, "lines": [{ "fk_product": 2, "qty": 1 }] } - * - * @param array $request_data Request data - * @return int ID of order - */ - public function post($request_data = null) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401, "Insuffisant rights"); - } - // Check mandatory fields - $result = $this->_validate($request_data); - - foreach ($request_data as $field => $value) { - if ($field === 'caller') { - // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller - $this->commande->context['caller'] = $request_data['caller']; - continue; - } - - $this->commande->$field = $value; - } - /*if (isset($request_data["lines"])) { - $lines = array(); - foreach ($request_data["lines"] as $line) { - array_push($lines, (object) $line); - } - $this->commande->lines = $lines; - }*/ - - if ($this->commande->create(DolibarrApiAccess::$user) < 0) { - throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors)); - } - - return ((int) $this->commande->id); - } - - /** - * Get lines of an order - * - * @param int $id Id of order - * - * @url GET {id}/lines - * - * @return array - */ - public function getLines($id) - { - if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) { - throw new RestException(401); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -328,22 +324,52 @@ - } - $this->commande->getLinesArray(); - $result = array(); - foreach ($this->commande->lines as $line) { - array_push($result, $this->_cleanObjectDatas($line)); - } - return $result; - } - - /** - * Add a line to given order - * - * @param int $id Id of order to update - * @param array $request_data OrderLine data - * - * @url POST {id}/lines - * - * @return int - */ - public function postLine($id, $request_data = null) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + } + $request_data = (object) $request_data; + $updateRes = $this->commande->addline( + $request_data->desc, + $request_data->subprice, + $request_data->qty, + $request_data->tva_tx, + $request_data->localtax1_tx, + $request_data->localtax2_tx, + $request_data->fk_product, + $request_data->remise_percent, + $request_data->info_bits, + $request_data->fk_remise_except, + 'HT', + 0, + $request_data->date_start, + $request_data->date_end, + $request_data->product_type, + $request_data->rang, + $request_data->special_code, + $request_data->fk_parent_line, + $request_data->fk_fournprice, + $request_data->pa_ht, + $request_data->label, + $request_data->array_options, + $request_data->fk_unit, + $request_data->origin, + $request_data->origin_id, + $request_data->multicurrency_subprice + ); + + if ($updateRes > 0) { + return $updateRes; + } else { + throw new RestException(400, $this->commande->error); + } + } + + /** + * Update a line to given order + * + * @param int $id Id of order to update + * @param int $lineid Id of line to update + * @param array $request_data OrderLine data + * + * @url PUT {id}/lines/{lineid} + * + * @return array|bool + */ + public function putLine($id, $lineid, $request_data = null) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -353,4 +379,4 @@ - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -360,2 +386 @@ - } - + } @@ -363,54 +388,50 @@ - - $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); - $request_data->label = sanitizeVal($request_data->label); - - $updateRes = $this->commande->addline( - $request_data->desc, - $request_data->subprice, - $request_data->qty, - $request_data->tva_tx, - $request_data->localtax1_tx, - $request_data->localtax2_tx, - $request_data->fk_product, - $request_data->remise_percent, - $request_data->info_bits, - $request_data->fk_remise_except, - $request_data->price_base_type ? $request_data->price_base_type : 'HT', - $request_data->subprice, - $request_data->date_start, - $request_data->date_end, - $request_data->product_type, - $request_data->rang, - $request_data->special_code, - $request_data->fk_parent_line, - $request_data->fk_fournprice, - $request_data->pa_ht, - $request_data->label, - $request_data->array_options, - $request_data->fk_unit, - $request_data->origin, - $request_data->origin_id, - $request_data->multicurrency_subprice, - $request_data->ref_ext - ); - - if ($updateRes > 0) { - return $updateRes; - } else { - throw new RestException(400, $this->commande->error); - } - } - - /** - * Update a line to given order - * - * @param int $id Id of order to update - * @param int $lineid Id of line to update - * @param array $request_data OrderLine data - * @return Object|false Object with cleaned properties - * - * @url PUT {id}/lines/{lineid} - */ - public function putLine($id, $lineid, $request_data = null) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + $updateRes = $this->commande->updateline( + $lineid, + $request_data->desc, + $request_data->subprice, + $request_data->qty, + $request_data->remise_percent, + $request_data->tva_tx, + $request_data->localtax1_tx, + $request_data->localtax2_tx, + 'HT', + $request_data->info_bits, + $request_data->date_start, + $request_data->date_end, + $request_data->product_type, + $request_data->fk_parent_line, + 0, + $request_data->fk_fournprice, + $request_data->pa_ht, + $request_data->label, + $request_data->special_code, + $request_data->array_options, + $request_data->fk_unit, + $request_data->multicurrency_subprice + ); + + if ($updateRes > 0) { + $result = $this->get($id); + unset($result->line); + return $this->_cleanObjectDatas($result); + } + return false; + } + + /** + * Delete a line to given order + * + * + * @param int $id Id of order to update + * @param int $lineid Id of line to delete + * + * @url DELETE {id}/lines/{lineid} + * + * @return int + * + * @throws RestException 401 + * @throws RestException 404 + */ + public function deleteLine($id, $lineid) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -420,4 +441,4 @@ - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -427,112 +448,13 @@ - } - - $request_data = (object) $request_data; - - $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml'); - $request_data->label = sanitizeVal($request_data->label); - - $updateRes = $this->commande->updateline( - $lineid, - $request_data->desc, - $request_data->subprice, - $request_data->qty, - $request_data->remise_percent, - $request_data->tva_tx, - $request_data->localtax1_tx, - $request_data->localtax2_tx, - $request_data->price_base_type ? $request_data->price_base_type : 'HT', - $request_data->info_bits, - $request_data->date_start, - $request_data->date_end, - $request_data->product_type, - $request_data->fk_parent_line, - 0, - $request_data->fk_fournprice, - $request_data->pa_ht, - $request_data->label, - $request_data->special_code, - $request_data->array_options, - $request_data->fk_unit, - $request_data->multicurrency_subprice, - 0, - $request_data->ref_ext, - $request_data->rang - ); - - if ($updateRes > 0) { - $result = $this->get($id); - unset($result->line); - return $this->_cleanObjectDatas($result); - } - return false; - } - - /** - * Delete a line of a given order - * - * @param int $id Id of order to update - * @param int $lineid Id of line to delete - * @return Object Object with cleaned properties - * - * @url DELETE {id}/lines/{lineid} - * - * @throws RestException 401 - * @throws RestException 404 - */ - public function deleteLine($id, $lineid) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id); - if ($updateRes > 0) { - return $this->get($id); - } else { - throw new RestException(405, $this->commande->error); - } - } - - /** - * Get contacts of given order - * - * Return an array with contact informations - * - * @param int $id ID of order - * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER) - * @return Object Object with cleaned properties - * - * @url GET {id}/contacts - * - * @throws RestException - */ - public function getContacts($id, $type = '') - { - if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) { - throw new RestException(401); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $contacts = $this->commande->liste_contact(-1, 'external', 0, $type); - - return $this->_cleanObjectDatas($contacts); - } - - /** + } + + // TODO Check the lineid $lineid is a line of ojbect + + $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid); + if ($updateRes > 0) { + return $this->get($id); + } else { + throw new RestException(405, $this->commande->error); + } + } + + /** @@ -544 +465,0 @@ - * @return array @@ -548,2 +469,4 @@ - * @throws RestException 401 - * @throws RestException 404 + * @return int + * + * @throws RestException 401 + * @throws RestException 404 @@ -551,3 +474,3 @@ - public function postContact($id, $contactid, $type) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + public function postContact($id, $contactid, $type) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -557,4 +480,4 @@ - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -566,20 +489,11 @@ - $result = $this->commande->add_contact($contactid, $type, 'external'); - - if ($result < 0) { - throw new RestException(500, 'Error when added the contact'); - } - - if ($result == 0) { - throw new RestException(304, 'contact already added'); - } - - return array( - 'success' => array( - 'code' => 200, - 'message' => 'Contact linked to the order' - ) - ); - } - - /** - * Unlink a contact type of given order + $result = $this->commande->add_contact($contactid, $type, 'external'); + + if (!$result) { + throw new RestException(500, 'Error when added the contact'); + } + + return $this->commande; + } + + /** + * Delete a contact type of given order @@ -588,2 +502 @@ - * @param int $contactid Id of contact - * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER). + * @param int $rowid Row key of the contact in the array contact_ids. @@ -591,187 +504 @@ - * @url DELETE {id}/contact/{contactid}/{type} - * - * @return array - * - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 500 System error - */ - public function deleteContact($id, $contactid, $type) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $contacts = $this->commande->liste_contact(); - - foreach ($contacts as $contact) { - if ($contact['id'] == $contactid && $contact['code'] == $type) { - $result = $this->commande->delete_contact($contact['rowid']); - - if (!$result) { - throw new RestException(500, 'Error when deleted the contact'); - } - } - } - - return array( - 'success' => array( - 'code' => 200, - 'message' => 'Contact unlinked from order' - ) - ); - } - - /** - * Update order general fields (won't touch lines of order) - * - * @param int $id Id of order to update - * @param array $request_data Datas - * @return Object Object with cleaned properties - */ - public function put($id, $request_data = null) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - foreach ($request_data as $field => $value) { - if ($field == 'id') { - continue; - } - if ($field === 'caller') { - // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller - $this->commande->context['caller'] = $request_data['caller']; - continue; - } - - $this->commande->$field = $value; - } - - // Update availability - if (!empty($this->commande->availability_id)) { - if ($this->commande->availability($this->commande->availability_id) < 0) { - throw new RestException(400, 'Error while updating availability'); - } - } - - if ($this->commande->update(DolibarrApiAccess::$user) > 0) { - return $this->get($id); - } else { - throw new RestException(500, $this->commande->error); - } - } - - /** - * Delete order - * - * @param int $id Order ID - * @return array - */ - public function delete($id) - { - if (!DolibarrApiAccess::$user->rights->commande->supprimer) { - throw new RestException(401); - } - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - if (!$this->commande->delete(DolibarrApiAccess::$user)) { - throw new RestException(500, 'Error when deleting order : '.$this->commande->error); - } - - return array( - 'success' => array( - 'code' => 200, - 'message' => 'Order deleted' - ) - ); - } - - /** - * Validate an order - * - * If you get a bad value for param notrigger check, provide this in body - * { - * "idwarehouse": 0, - * "notrigger": 0 - * } - * - * @param int $id Order ID - * @param int $idwarehouse Warehouse ID - * @param int $notrigger 1=Does not execute triggers, 0= execute triggers - * @return Object Object with cleaned properties - * - * @url POST {id}/validate - * - * @throws RestException 304 - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 500 System error - * - */ - public function validate($id, $idwarehouse = 0, $notrigger = 0) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes) - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger); - if ($result == 0) { - throw new RestException(304, 'Error nothing done. May be object is already validated'); - } - if ($result < 0) { - throw new RestException(500, 'Error when validating Order: '.$this->commande->error); - } - $result = $this->commande->fetch($id); - - $this->commande->fetchObjectLinked(); - - //fix #20477 : add online_payment_url - require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; - $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref); - - return $this->_cleanObjectDatas($this->commande); - } - - /** - * Tag the order as validated (opened) - * - * Function used when order is reopend after being closed. - * - * @param int $id Id of the order - * - * @url POST {id}/reopen + * @url DELETE {id}/contact/{rowid} @@ -781,5 +508,3 @@ - * @throws RestException 304 - * @throws RestException 400 - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 405 + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 500 @@ -787,3 +512,3 @@ - public function reopen($id) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + public function deleteContact($id, $rowid) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -792,53 +517,5 @@ - if (empty($id)) { - throw new RestException(400, 'Order ID is mandatory'); - } - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - $result = $this->commande->set_reopen(DolibarrApiAccess::$user); - if ($result < 0) { - throw new RestException(405, $this->commande->error); - } elseif ($result == 0) { - throw new RestException(304); - } - - return $result; - } - - /** - * Classify the order as invoiced. Could be also called setbilled - * - * @param int $id Id of the order - * @return Object Object with cleaned properties - * - * @url POST {id}/setinvoiced - * - * @throws RestException 400 - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 405 - */ - public function setinvoiced($id) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - if (empty($id)) { - throw new RestException(400, 'Order ID is mandatory'); - } - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - $result = $this->commande->classifyBilled(DolibarrApiAccess::$user); - if ($result < 0) { - throw new RestException(400, $this->commande->error); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -850,17 +527,20 @@ - $this->commande->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->commande); - } - - /** - * Close an order (Classify it as "Delivered") - * - * @param int $id Order ID - * @param int $notrigger Disabled triggers - * @return Object Object with cleaned properties - * - * @url POST {id}/close - */ - public function close($id, $notrigger = 0) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + $result = $this->commande->delete_contact($rowid); + + if (!$result) { + throw new RestException(500, 'Error when deleted the contact'); + } + + return $this->commande; + } + + /** + * Update order general fields (won't touch lines of order) + * + * @param int $id Id of order to update + * @param array $request_data Datas + * + * @return int + */ + public function put($id, $request_data = null) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -869,4 +549,5 @@ - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -877,13 +558,36 @@ - - $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger); - if ($result == 0) { - throw new RestException(304, 'Error nothing done. May be object is already closed'); - } - if ($result < 0) { - throw new RestException(500, 'Error when closing Order: '.$this->commande->error); - } - - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + foreach ($request_data as $field => $value) { + if ($field == 'id') continue; + $this->commande->$field = $value; + } + + // Update availability + if (!empty($this->commande->availability_id)) { + if ($this->commande->availability($this->commande->availability_id) < 0) + throw new RestException(400, 'Error while updating availability'); + } + + if ($this->commande->update(DolibarrApiAccess::$user) > 0) + { + return $this->get($id); + } + else + { + throw new RestException(500, $this->commande->error); + } + } + + /** + * Delete order + * + * @param int $id Order ID + * @return array + */ + public function delete($id) + { + if (!DolibarrApiAccess::$user->rights->commande->supprimer) { + throw new RestException(401); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -895,17 +599,37 @@ - $this->commande->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->commande); - } - - /** - * Set an order to draft - * - * @param int $id Order ID - * @param int $idwarehouse Warehouse ID to use for stock change (Used only if option STOCK_CALCULATE_ON_VALIDATE_ORDER is on) - * @return Object Object with cleaned properties - * - * @url POST {id}/settodraft - */ - public function settodraft($id, $idwarehouse = -1) - { - if (!DolibarrApiAccess::$user->rights->commande->creer) { + if (!$this->commande->delete(DolibarrApiAccess::$user)) { + throw new RestException(500, 'Error when deleting order : '.$this->commande->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Order deleted' + ) + ); + } + + /** + * Validate an order + * + * If you get a bad value for param notrigger check, provide this in body + * { + * "idwarehouse": 0, + * "notrigger": 0 + * } + * + * @param int $id Order ID + * @param int $idwarehouse Warehouse ID + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers + * + * @url POST {id}/validate + * + * @throws RestException 304 + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 500 + * + * @return array + */ + public function validate($id, $idwarehouse = 0, $notrigger = 0) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { @@ -914,4 +638,4 @@ - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } @@ -923 +647 @@ - $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse); + $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger); @@ -925 +649 @@ - throw new RestException(304, 'Nothing done. May be object is already closed'); + throw new RestException(304, 'Error nothing done. May be object is already validated'); @@ -928,2 +652,181 @@ - throw new RestException(500, 'Error when closing Order: '.$this->commande->error); - } + throw new RestException(500, 'Error when validating Order: '.$this->commande->error); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->commande); + } + + /** + * Tag the order as validated (opened) + * + * Function used when order is reopend after being closed. + * + * @param int $id Id of the order + * + * @url POST {id}/reopen + * + * @return int + * + * @throws RestException 304 + * @throws RestException 400 + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 405 + */ + public function reopen($id) + { + + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + if (empty($id)) { + throw new RestException(400, 'Order ID is mandatory'); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + $result = $this->commande->set_reopen(DolibarrApiAccess::$user); + if ($result < 0) { + throw new RestException(405, $this->commande->error); + }elseif ($result == 0) { + throw new RestException(304); + } + + return $result; + } + + /** + * Classify the order as invoiced. Could be also called setbilled + * + * @param int $id Id of the order + * + * @url POST {id}/setinvoiced + * + * @return int + * + * @throws RestException 400 + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 405 + */ + public function setinvoiced($id) + { + + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + if (empty($id)) { + throw new RestException(400, 'Order ID is mandatory'); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + $result = $this->commande->classifyBilled(DolibarrApiAccess::$user); + if ($result < 0) { + throw new RestException(400, $this->commande->error); + } + + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->commande); + } + + /** + * Close an order (Classify it as "Delivered") + * + * @param int $id Order ID + * @param int $notrigger Disabled triggers + * + * @url POST {id}/close + * + * @return int + */ + public function close($id, $notrigger = 0) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already closed'); + } + if ($result < 0) { + throw new RestException(500, 'Error when closing Order: '.$this->commande->error); + } + + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->commande); + } + + /** + * Set an order to draft + * + * @param int $id Order ID + * @param int $idwarehouse Warehouse ID to use for stock change (Used only if option STOCK_CALCULATE_ON_VALIDATE_ORDER is on) + * + * @url POST {id}/settodraft + * + * @return array + */ + public function settodraft($id, $idwarehouse = -1) + { + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse); + if ($result == 0) { + throw new RestException(304, 'Nothing done. May be object is already closed'); + } + if ($result < 0) { + throw new RestException(500, 'Error when closing Order: '.$this->commande->error); + } @@ -932,194 +835,98 @@ - if (!$result) { - throw new RestException(404, 'Order not found'); - } - - if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - $this->commande->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->commande); - } - - - /** - * Create an order using an existing proposal. - * - * @param int $proposalid Id of the proposal - * @return Object Object with cleaned properties - * - * @url POST /createfromproposal/{proposalid} - * - * @throws RestException 400 - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 405 - */ - public function createOrderFromProposal($proposalid) - { - require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; - - if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) { - throw new RestException(401); - } - if (!DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } - if (empty($proposalid)) { - throw new RestException(400, 'Proposal ID is mandatory'); - } - - $propal = new Propal($this->db); - $result = $propal->fetch($proposalid); - if (!$result) { - throw new RestException(404, 'Proposal not found'); - } - - $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user); - if ($result < 0) { - throw new RestException(405, $this->commande->error); - } - $this->commande->fetchObjectLinked(); - - return $this->_cleanObjectDatas($this->commande); - } - - /** - * Get the shipments of an order - * - * @param int $id Id of the order - * - * @url GET {id}/shipment - * - * @return array - * - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 500 System error - */ - public function getOrderShipments($id) - { - require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; - if (!DolibarrApiAccess::$user->rights->expedition->lire) { - throw new RestException(401); - } - $obj_ret = array(); - $sql = "SELECT e.rowid"; - $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e"; - $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet"; - $sql .= " ON e.rowid = edet.fk_expedition"; - $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet"; - $sql .= " ON edet.fk_origin_line = cdet.rowid"; - $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c"; - $sql .= " ON cdet.fk_commande = c.rowid"; - $sql .= " WHERE c.rowid = ".((int) $id); - $sql .= " GROUP BY e.rowid"; - $sql .= $this->db->order("e.rowid", "ASC"); - - dol_syslog("API Rest request"); - $result = $this->db->query($sql); - - if ($result) { - $num = $this->db->num_rows($result); - if ($num <= 0) { - throw new RestException(404, 'Shipments not found '); - } - $i = 0; - while ($i < $num) { - $obj = $this->db->fetch_object($result); - $shipment_static = new Expedition($this->db); - if ($shipment_static->fetch($obj->rowid)) { - $obj_ret[] = $this->_cleanObjectDatas($shipment_static); - } - $i++; - } - } else { - throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror()); - } - return $obj_ret; - } - - /** - * Create the shipment of an order - * - * @param int $id Id of the order - * @param int $warehouse_id Id of a warehouse - * - * @url POST {id}/shipment/{warehouse_id} - * - * @return int - * - * @throws RestException 401 - * @throws RestException 404 - * @throws RestException 500 System error - */ - public function createOrderShipment($id, $warehouse_id) - { - require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; - if (!DolibarrApiAccess::$user->rights->expedition->creer) { - throw new RestException(401); - } - if ($warehouse_id <= 0) { - throw new RestException(404, 'Warehouse not found'); - } - $result = $this->commande->fetch($id); - if (!$result) { - throw new RestException(404, 'Order not found'); - } - $shipment = new Expedition($this->db); - $shipment->socid = $this->commande->socid; - $shipment->origin_id = $this->commande->id; - $result = $shipment->create(DolibarrApiAccess::$user); - if ($result <= 0) { - throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror()); - } - foreach ($this->commande->lines as $line) { - $result = $shipment->create_line($warehouse_id, $line->id, $line->qty); - if ($result <= 0) { - throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror()); - } - } - return $shipment->id; - } - - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore - /** - * Clean sensible object datas - * - * @param Object $object Object to clean - * @return Object Object with cleaned properties - */ - protected function _cleanObjectDatas($object) - { - // phpcs:enable - $object = parent::_cleanObjectDatas($object); - - unset($object->note); - unset($object->address); - unset($object->barcode_type); - unset($object->barcode_type_code); - unset($object->barcode_type_label); - unset($object->barcode_type_coder); - - return $object; - } - - /** - * Validate fields before create or update object - * - * @param array $data Array with data to verify - * @return array - * @throws RestException - */ - private function _validate($data) - { - $commande = array(); - foreach (Orders::$FIELDS as $field) { - if (!isset($data[$field])) { - throw new RestException(400, $field." field missing"); - } - $commande[$field] = $data[$field]; - } - return $commande; - } + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->commande->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->commande); + } + + + /** + * Create an order using an existing proposal. + * + * + * @param int $proposalid Id of the proposal + * + * @url POST /createfromproposal/{proposalid} + * + * @return int + * @throws RestException 400 + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 405 + */ + public function createOrderFromProposal($proposalid) + { + + require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; + + if (!DolibarrApiAccess::$user->rights->propal->lire) { + throw new RestException(401); + } + if (!DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } + if (empty($proposalid)) { + throw new RestException(400, 'Proposal ID is mandatory'); + } + + $propal = new Propal($this->db); + $result = $propal->fetch($proposalid); + if (!$result) { + throw new RestException(404, 'Proposal not found'); + } + + $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user); + if ($result < 0) { + throw new RestException(405, $this->commande->error); + } + $this->commande->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->commande); + } + + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore + /** + * Clean sensible object datas + * + * @param object $object Object to clean + * @return array Array of cleaned object properties + */ + protected function _cleanObjectDatas($object) + { + // phpcs:enable + $object = parent::_cleanObjectDatas($object); + + unset($object->note); + unset($object->address); + unset($object->barcode_type); + unset($object->barcode_type_code); + unset($object->barcode_type_label); + unset($object->barcode_type_coder); + + return $object; + } + + /** + * Validate fields before create or update object + * + * @param array $data Array with data to verify + * @return array + * @throws RestException + */ + private function _validate($data) + { + $commande = array(); + foreach (Orders::$FIELDS as $field) { + if (!isset($data[$field])) + throw new RestException(400, $field." field missing"); + $commande[$field] = $data[$field]; + } + return $commande; + } --- /tmp/dsg/dolibarr/htdocs/commande/class/github_19.0.3_commande.class.php +++ /tmp/dsg/dolibarr/htdocs/commande/class/client_commande.class.php @@ -6 +6 @@ - * Copyright (C) 2010-2020 Juanjo Menent + * Copyright (C) 2010-2016 Juanjo Menent @@ -12,4 +12,2 @@ - * Copyright (C) 2018 Nicolas ZABOURI - * Copyright (C) 2016-2022 Ferran Marcet - * Copyright (C) 2021-2023 Frédéric France - * Copyright (C) 2022 Gauthier VERDOL + * Copyright (C) 2018 Nicolas ZABOURI + * Copyright (C) 2016-2018 Ferran Marcet @@ -34 +32 @@ - * \brief class for orders + * \brief Fichier des classes de commandes @@ -36 +33,0 @@ - @@ -42,2 +38,0 @@ -require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; - @@ -87,5 +81,0 @@ - * @var int Does object support extrafields ? 0=No, 1=Yes - */ - public $isextrafieldmanaged = 1; - - /** @@ -103 +93 @@ - * @var int Thirdparty ID + * @var int Thirparty ID @@ -108 +98 @@ - * @var string Thirdparty ref of order + * @var string Thirparty ref of order @@ -112,6 +102,7 @@ - /** - * @var string Thirdparty ref of order - */ - public $ref_customer; - - /** + /** + * @var string Internal ref for order + * @deprecated + */ + public $ref_int; + + /** @@ -133,22 +124,9 @@ - /** - * @var string Limit date payment - */ - public $date_lim_reglement; - /** - * @var string Condition payment code - */ - public $cond_reglement_code; - - /** - * @var string Condition payment label - */ - public $cond_reglement_doc; - - /** - * @var double Deposit % for payment terms - */ - public $deposit_percent; - - /** - * @var int bank account ID - */ + /** + * @var int Draft Status of the order + */ + public $brouillon; + public $cond_reglement_code; + + /** + * @var int bank account ID + */ @@ -190,8 +168 @@ - /** - * @var int Source demand reason Id - */ - public $demand_reason_id; - - /** - * @var string Source reason code. Why we receive order (after a phone campaign, ...) - */ + public $demand_reason_id; // Source reason. Why we receive order (after a phone campaign, ...) @@ -199,3 +170,2 @@ - - /** - * @var int Date of order + /** + * @var int Date of order @@ -206 +176 @@ - * @var int Date of order + * @var int Date of order @@ -212,8 +182,5 @@ - /** - * @var int Date expected of shipment (date of start of shipment, not the reception that occurs some days after) - */ - public $delivery_date; - - /** - * @var int ID - */ + public $date_livraison; // Date expected of shipment (date starting shipment, not the reception that occurs some days after) + + /** + * @var int ID + */ @@ -222,3 +188,0 @@ - /** - * @deprecated - */ @@ -226 +190,4 @@ - + public $remise_absolue; + public $info_bits; + public $rang; + public $special_code; @@ -228,6 +194,0 @@ - - /** - * @var int Warehouse Id - */ - public $warehouse_id; - @@ -239,2 +200,2 @@ - * @var int User author ID - */ + * @var int User author ID + */ @@ -243,3 +204,3 @@ - /** - * @var int User validator ID - */ + /** + * @var int User validator ID + */ @@ -249,5 +209,0 @@ - * @var OrderLine one line of an order - */ - public $line; - - /** @@ -257,0 +214,16 @@ + // Multicurrency + /** + * @var int Currency ID + */ + public $fk_multicurrency; + + public $multicurrency_code; + public $multicurrency_tx; + public $multicurrency_total_ht; + public $multicurrency_total_tva; + public $multicurrency_total_ttc; + + /** + * @var Commande clone of order object + */ + public $oldcopy; @@ -263,11 +234,0 @@ - - /** - * @var array Array with line of all shipments - */ - public $expeditions; - - /** - * @var string payment url - */ - public $online_payment_url; - @@ -294 +255 @@ - * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") + * 'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") @@ -308,0 +270 @@ + 'ref_int' =>array('type'=>'varchar(255)', 'label'=>'RefInt', 'enabled'=>1, 'visible'=>0, 'position'=>27), // deprecated @@ -310,5 +272,9 @@ - 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>'isModEnabled("societe")', 'visible'=>-1, 'notnull'=>1, 'position'=>20), - 'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label'=>'Project', 'enabled'=>"isModEnabled('project')", 'visible'=>-1, 'position'=>25), - 'date_commande' =>array('type'=>'date', 'label'=>'Date', 'enabled'=>1, 'visible'=>1, 'position'=>60, 'csslist'=>'nowraponall'), - 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>62, 'csslist'=>'nowraponall'), - 'date_cloture' =>array('type'=>'datetime', 'label'=>'DateClosing', 'enabled'=>1, 'visible'=>-1, 'position'=>65, 'csslist'=>'nowraponall'), + 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20), + 'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Project', 'enabled'=>1, 'visible'=>-1, 'position'=>25), + 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>55), + 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>56), + 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>60), + 'date_cloture' =>array('type'=>'datetime', 'label'=>'DateClosing', 'enabled'=>1, 'visible'=>-1, 'position'=>65), + 'date_commande' =>array('type'=>'date', 'label'=>'Date', 'enabled'=>1, 'visible'=>-1, 'position'=>70), + 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'position'=>75), + 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>80), @@ -318 +284,5 @@ - 'total_tva' =>array('type'=>'double(24,8)', 'label'=>'VAT', 'enabled'=>1, 'visible'=>-1, 'position'=>125, 'isameasure'=>1), + //'amount_ht' =>array('type'=>'double(24,8)', 'label'=>'AmountHT', 'enabled'=>1, 'visible'=>-1, 'position'=>105), + 'remise_percent' =>array('type'=>'double', 'label'=>'RelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>110), + 'remise_absolue' =>array('type'=>'double', 'label'=>'CustomerRelativeDiscount', 'enabled'=>1, 'visible'=>-1, 'position'=>115), + //'remise' =>array('type'=>'double', 'label'=>'Remise', 'enabled'=>1, 'visible'=>-1, 'position'=>120), + 'tva' =>array('type'=>'double(24,8)', 'label'=>'VAT', 'enabled'=>1, 'visible'=>-1, 'position'=>125, 'isameasure'=>1), @@ -323,2 +293,2 @@ - 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>150), - 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>155), + 'note_private' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>150), + 'note_public' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>155), @@ -326 +296,2 @@ - 'fk_account' =>array('type'=>'integer', 'label'=>'BankAccount', 'enabled'=>'isModEnabled("banque")', 'visible'=>-1, 'position'=>170), + //'facture' =>array('type'=>'tinyint(4)', 'label'=>'ParentInvoice', 'enabled'=>1, 'visible'=>-1, 'position'=>165), + 'fk_account' =>array('type'=>'integer', 'label'=>'BankAccount', 'enabled'=>1, 'visible'=>-1, 'position'=>170), @@ -329 +299,0 @@ - 'deposit_percent' =>array('type'=>'varchar(63)', 'label'=>'DepositPercent', 'enabled'=>1, 'visible'=>-1, 'position'=>181), @@ -331 +301 @@ - 'date_livraison' =>array('type'=>'date', 'label'=>'DateDeliveryPlanned', 'enabled'=>1, 'visible'=>-1, 'position'=>190, 'csslist'=>'nowraponall'), + 'date_livraison' =>array('type'=>'date', 'label'=>'DateDeliveryPlanned', 'enabled'=>1, 'visible'=>-1, 'position'=>190), @@ -333 +303 @@ - 'fk_warehouse' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Fk warehouse', 'enabled'=>'isModEnabled("stock")', 'visible'=>-1, 'position'=>200), + 'fk_warehouse' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Fk warehouse', 'enabled'=>1, 'visible'=>-1, 'position'=>200), @@ -340,6 +310,6 @@ - 'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>240), - 'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'MulticurrencyCurrency', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>245), - 'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyRate', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>250, 'isameasure'=>1), - 'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountHT', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>255, 'isameasure'=>1), - 'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountVAT', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>260, 'isameasure'=>1), - 'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountTTC', 'enabled'=>'isModEnabled("multicurrency")', 'visible'=>-1, 'position'=>265, 'isameasure'=>1), + 'fk_multicurrency' =>array('type'=>'integer', 'label'=>'Fk multicurrency', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>240), + 'multicurrency_code' =>array('type'=>'varchar(255)', 'label'=>'MulticurrencyCurrency', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>245), + 'multicurrency_tx' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyRate', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>250, 'isameasure'=>1), + 'multicurrency_total_ht' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountHT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>255, 'isameasure'=>1), + 'multicurrency_total_tva' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountVAT', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>260, 'isameasure'=>1), + 'multicurrency_total_ttc' =>array('type'=>'double(24,8)', 'label'=>'MulticurrencyAmountTTC', 'enabled'=>'$conf->multicurrency->enabled', 'visible'=>-1, 'position'=>265, 'isameasure'=>1), @@ -349,5 +318,0 @@ - 'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'position'=>300), - 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>302), - 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>304, 'csslist'=>'nowraponall'), - 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>306), - 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>400), @@ -354,0 +320 @@ + 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>900), @@ -378,2 +344,2 @@ - const STATUS_SHIPMENTONPROCESS = 2; // We set this status when a shipment is validated - const STATUS_ACCEPTED = 2; // For backward compatibility. Use key STATUS_SHIPMENTONPROCESS instead. + const STATUS_SHIPMENTONPROCESS = 2; + const STATUS_ACCEPTED = 2; // For backward compatibility. Use key STATUS_SHIPMENTONPROCESS instead. @@ -394,0 +361,5 @@ + + $this->remise = 0; + $this->remise_percent = 0; + + $this->products = array(); @@ -409 +380,2 @@ - if (getDolGlobalString('COMMANDE_ADDON')) { + if (!empty($conf->global->COMMANDE_ADDON)) + { @@ -412 +384 @@ - $file = getDolGlobalString('COMMANDE_ADDON') . ".php"; + $file = $conf->global->COMMANDE_ADDON.".php"; @@ -417 +389,2 @@ - foreach ($dirmodels as $reldir) { + foreach ($dirmodels as $reldir) + { @@ -424,4 +397,5 @@ - if ($mybool === false) { - dol_print_error('', "Failed to include file ".$file); - return ''; - } + if ($mybool === false) + { + dol_print_error('', "Failed to include file ".$file); + return ''; + } @@ -432 +406,2 @@ - if ($numref != "") { + if ($numref != "") + { @@ -434 +409,3 @@ - } else { + } + else + { @@ -439 +416,3 @@ - } else { + } + else + { @@ -452 +431 @@ - * @return int Return integer <0 if KO, 0=Nothing done, >0 if OK + * @return int <=0 if OK, 0=Nothing done, >0 if KO @@ -463 +442,2 @@ - if ($this->statut == self::STATUS_VALIDATED) { + if ($this->statut == self::STATUS_VALIDATED) + { @@ -468,2 +448,3 @@ - if (!((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('commande', 'creer')) - || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('commande', 'order_advance', 'validate')))) { + if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer)) + || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) + { @@ -484 +465 @@ - $result = $soc->setAsCustomer(); + $result = $soc->set_as_client(); @@ -487 +468,2 @@ - if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life + if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life + { @@ -489 +471,3 @@ - } else { + } + else + { @@ -499,5 +483,4 @@ - $sql .= " fk_user_valid = ".($user->id > 0 ? (int) $user->id : "null").","; - $sql .= " fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id); - - dol_syslog(get_class($this)."::valid", LOG_DEBUG); + $sql .= " fk_user_valid = ".$user->id; + $sql .= " WHERE rowid = ".$this->id; + + dol_syslog(get_class($this)."::valid()", LOG_DEBUG); @@ -505 +488,2 @@ - if (!$resql) { + if (!$resql) + { @@ -511 +495,2 @@ - if (!$error) { + if (!$error) + { @@ -513 +498,2 @@ - if ($result >= 0 && isModEnabled('stock') && getDolGlobalInt('STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) { + if ($result >= 0 && !empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) + { @@ -519,2 +505,4 @@ - for ($i = 0; $i < $cpt; $i++) { - if ($this->lines[$i]->fk_product > 0) { + for ($i = 0; $i < $cpt; $i++) + { + if ($this->lines[$i]->fk_product > 0) + { @@ -523 +510,0 @@ - $mouvP->setOrigin($this->element, $this->id); @@ -526 +513,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -531,3 +519 @@ - if ($error) { - break; - } + if ($error) break; @@ -538 +524,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -541,3 +528 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -547 +532,2 @@ - if (!$error) { + if (!$error) + { @@ -551 +537,2 @@ - if (preg_match('/^[\(]?PROV/i', $this->ref)) { + if (preg_match('/^[\(]?PROV/i', $this->ref)) + { @@ -556,11 +543 @@ - if (!$resql) { - $error++; - $this->error = $this->db->lasterror(); - } - $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'commande/".$this->db->escape($this->newref)."'"; - $sql .= " WHERE filepath = 'commande/".$this->db->escape($this->ref)."' and entity = ".$conf->entity; - $resql = $this->db->query($sql); - if (!$resql) { - $error++; - $this->error = $this->db->lasterror(); - } + if (!$resql) { $error++; $this->error = $this->db->lasterror(); } @@ -573,4 +550,6 @@ - if (!$error && file_exists($dirsource)) { - dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest); - - if (@rename($dirsource, $dirdest)) { + if (!$error && file_exists($dirsource)) + { + dol_syslog(get_class($this)."::valid() rename dir ".$dirsource." into ".$dirdest); + + if (@rename($dirsource, $dirdest)) + { @@ -580 +559,2 @@ - foreach ($listoffiles as $fileentry) { + foreach ($listoffiles as $fileentry) + { @@ -593 +573,2 @@ - if (!$error) { + if (!$error) + { @@ -595,5 +576,6 @@ - $this->statut = self::STATUS_VALIDATED; // deprecated - $this->status = self::STATUS_VALIDATED; - } - - if (!$error) { + $this->statut = self::STATUS_VALIDATED; + $this->brouillon = 0; + } + + if (!$error) + { @@ -602 +584,3 @@ - } else { + } + else + { @@ -614,5 +598,5 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - public function setDraft($user, $idwarehouse = -1) - { - //phpcs:enable + * @return int <0 if KO, >0 if OK + */ + public function setDraft($user, $idwarehouse = -1) + { + //phpcs:enable @@ -624 +608,2 @@ - if ($this->statut <= self::STATUS_DRAFT) { + if ($this->statut <= self::STATUS_DRAFT) + { @@ -628,2 +613,3 @@ - if (!((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('commande', 'creer')) - || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('commande', 'order_advance', 'validate')))) { + if (!((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer)) + || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->validate)))) + { @@ -639,11 +625,13 @@ - $sql .= " SET fk_statut = ".self::STATUS_DRAFT.","; - $sql .= " fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id); - - if ($this->db->query($sql)) { - if (!$error) { - $this->oldcopy = clone $this; - } - - // If stock is decremented on validate order, we must reincrement it - if (isModEnabled('stock') && getDolGlobalInt('STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) { + $sql .= " SET fk_statut = ".self::STATUS_DRAFT; + $sql .= " WHERE rowid = ".$this->id; + + if ($this->db->query($sql)) + { + if (!$error) + { + $this->oldcopy = clone $this; + } + + // If stock is decremented on validate order, we must reincrement it + if (!empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) + { @@ -656,2 +644,4 @@ - for ($i = 0; $i < $num; $i++) { - if ($this->lines[$i]->fk_product > 0) { + for ($i = 0; $i < $num; $i++) + { + if ($this->lines[$i]->fk_product > 0) + { @@ -660 +649,0 @@ - $mouvP->setOrigin($this->element, $this->id); @@ -663,5 +652 @@ - if ($result < 0) { - $error++; - $this->error = $mouvP->error; - break; - } + if ($result < 0) { $error++; $this->error = $mouvP->error; break; } @@ -675,3 +660 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -688 +671,3 @@ - } else { + } + else + { @@ -693 +678 @@ - } + } @@ -702 +687 @@ - * @return int Return integer <0 if KO, 0 if nothing is done, >0 if OK + * @return int <0 if KO, 0 if nothing is done, >0 if OK @@ -706 +691 @@ - // phpcs:enable + // phpcs:enable @@ -709 +694,2 @@ - if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) { + if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) + { @@ -717,3 +703,2 @@ - $sql .= ' SET fk_statut='.self::STATUS_VALIDATED.', facture=0,'; - $sql .= " fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= ' SET fk_statut='.self::STATUS_VALIDATED.', facture=0'; + $sql .= ' WHERE rowid = '.$this->id; @@ -723 +708,2 @@ - if ($resql) { + if ($resql) + { @@ -726,3 +712 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -730 +714,3 @@ - } else { + } + else + { @@ -736 +722,2 @@ - if (!$error) { + if (!$error) + { @@ -742,2 +729,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -757 +747 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -765,7 +755,5 @@ - $usercanclose = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !empty($user->rights->commande->creer)) - || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !empty($user->rights->commande->order_advance->close))); - - if ($usercanclose) { - if ($this->statut == self::STATUS_CLOSED) { - return 0; - } + $usercanclose = ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->creer)) + || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->commande->order_advance->close))); + + if ($usercanclose) + { @@ -778,7 +766,8 @@ - $sql .= ' fk_user_cloture = '.((int) $user->id).','; - $sql .= " date_cloture = '".$this->db->idate($now)."',"; - $sql .= " fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > '.self::STATUS_DRAFT; - - if ($this->db->query($sql)) { - if (!$notrigger) { + $sql .= ' fk_user_cloture = '.$user->id.','; + $sql .= " date_cloture = '".$this->db->idate($now)."'"; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT; + + if ($this->db->query($sql)) + { + if (!$notrigger) + { @@ -787,3 +776 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -793 +780,2 @@ - if (!$error) { + if (!$error) + { @@ -798 +786,3 @@ - } else { + } + else + { @@ -802 +792,3 @@ - } else { + } + else + { @@ -817 +809 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -828,3 +820,2 @@ - $sql .= " SET fk_statut = ".self::STATUS_CANCELED.","; - $sql .= " fk_user_modif = ".((int) $user->id); - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= " SET fk_statut = ".self::STATUS_CANCELED; + $sql .= " WHERE rowid = ".$this->id; @@ -834 +825,2 @@ - if ($this->db->query($sql)) { + if ($this->db->query($sql)) + { @@ -836 +828,2 @@ - if (isModEnabled('stock') && getDolGlobalInt('STOCK_CALCULATE_ON_VALIDATE_ORDER') == 1) { + if (!empty($conf->stock->enabled) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) + { @@ -841,2 +834,4 @@ - for ($i = 0; $i < $num; $i++) { - if ($this->lines[$i]->fk_product > 0) { + for ($i = 0; $i < $num; $i++) + { + if ($this->lines[$i]->fk_product > 0) + { @@ -844 +838,0 @@ - $mouvP->setOrigin($this->element, $this->id); @@ -847 +841,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -856 +851,2 @@ - if (!$error) { + if (!$error) + { @@ -859,3 +855 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -865 +859,2 @@ - if (!$error) { + if (!$error) + { @@ -869,2 +864,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -877 +875,3 @@ - } else { + } + else + { @@ -890 +890 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -894 +894 @@ - global $conf, $langs, $mysoc; + global $conf, $langs; @@ -898,2 +898,3 @@ - - // Set tmp vars + $this->brouillon = 1; // set command as draft + + // $date_commande is deprecated @@ -901 +901,0 @@ - $delivery_date = $this->delivery_date; @@ -904,6 +904,4 @@ - if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) { - list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $date); - } else { - $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code); - } - if (empty($this->fk_multicurrency)) { + if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $date); + else $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code); + if (empty($this->fk_multicurrency)) + { @@ -918 +916,2 @@ - if (!empty($this->ref)) { // We check that ref is not already used + if (!empty($this->ref)) // We check that ref is not already used + { @@ -920 +919,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -930 +930,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -935 +936,2 @@ - if (getDolGlobalString('ORDER_REQUIRE_SOURCE') && $this->source < 0) { + if (!empty($conf->global->ORDER_REQUIRE_SOURCE) && $this->source < 0) + { @@ -946,2 +948,2 @@ - $sql .= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client"; - $sql .= ", model_pdf, fk_cond_reglement, deposit_percent, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address"; + $sql .= " ref, fk_soc, date_creation, fk_user_author, fk_projet, date_commande, source, note_private, note_public, ref_ext, ref_client, ref_int"; + $sql .= ", model_pdf, fk_cond_reglement, fk_mode_reglement, fk_account, fk_availability, fk_input_reason, date_livraison, fk_delivery_address"; @@ -949,0 +952 @@ + $sql .= ", remise_absolue, remise_percent"; @@ -956,2 +959,2 @@ - $sql .= " VALUES ('(PROV)', ".((int) $this->socid).", '".$this->db->idate($now)."', ".((int) $user->id); - $sql .= ", ".($this->fk_project > 0 ? ((int) $this->fk_project) : "null"); + $sql .= " VALUES ('(PROV)', ".$this->socid.", '".$this->db->idate($now)."', ".$user->id; + $sql .= ", ".($this->fk_project > 0 ? $this->fk_project : "null"); @@ -964,11 +967,13 @@ - $sql .= ", '".$this->db->escape($this->model_pdf)."'"; - $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null"); - $sql .= ", ".(!empty($this->deposit_percent) ? "'".$this->db->escape($this->deposit_percent)."'" : "null"); - $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null"); - $sql .= ", ".($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL'); - $sql .= ", ".($this->availability_id > 0 ? ((int) $this->availability_id) : "null"); - $sql .= ", ".($this->demand_reason_id > 0 ? ((int) $this->demand_reason_id) : "null"); - $sql .= ", ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : "null"); - $sql .= ", ".($this->fk_delivery_address > 0 ? ((int) $this->fk_delivery_address) : 'NULL'); - $sql .= ", ".(!empty($this->shipping_method_id) && $this->shipping_method_id > 0 ? ((int) $this->shipping_method_id) : 'NULL'); - $sql .= ", ".(!empty($this->warehouse_id) && $this->warehouse_id > 0 ? ((int) $this->warehouse_id) : 'NULL'); + $sql .= ", ".($this->ref_int ? "'".$this->db->escape($this->ref_int)."'" : "null"); + $sql .= ", '".$this->db->escape($this->modelpdf)."'"; + $sql .= ", ".($this->cond_reglement_id > 0 ? $this->cond_reglement_id : "null"); + $sql .= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : "null"); + $sql .= ", ".($this->fk_account > 0 ? $this->fk_account : 'NULL'); + $sql .= ", ".($this->availability_id > 0 ? $this->availability_id : "null"); + $sql .= ", ".($this->demand_reason_id > 0 ? $this->demand_reason_id : "null"); + $sql .= ", ".($this->date_livraison ? "'".$this->db->idate($this->date_livraison)."'" : "null"); + $sql .= ", ".($this->fk_delivery_address > 0 ? $this->fk_delivery_address : 'NULL'); + $sql .= ", ".($this->shipping_method_id > 0 ? $this->shipping_method_id : 'NULL'); + $sql .= ", ".($this->warehouse_id > 0 ? $this->warehouse_id : 'NULL'); + $sql .= ", ".($this->remise_absolue > 0 ? $this->db->escape($this->remise_absolue) : 'NULL'); + $sql .= ", ".($this->remise_percent > 0 ? $this->db->escape($this->remise_percent) : 0); @@ -978 +983 @@ - $sql .= ", ".($this->module_source ? "'".$this->db->escape($this->module_source)."'" : "null"); + $sql .= ", ".($this->module_source ? "'".$this->db->escape($this->module_source)."'" : "null"); @@ -982 +987 @@ - $sql .= ", ".(float) $this->multicurrency_tx; + $sql .= ", ".(double) $this->multicurrency_tx; @@ -987 +992,2 @@ - if ($resql) { + if ($resql) + { @@ -990 +996,2 @@ - if ($this->id) { + if ($this->id) + { @@ -997 +1004,2 @@ - for ($i = 0; $i < $num; $i++) { + for ($i = 0; $i < $num; $i++) + { @@ -1002,3 +1010 @@ - if (!is_object($line)) { - $line = (object) $line; - } + if (!is_object($line)) $line = (object) $line; @@ -1013,5 +1019,3 @@ - if ($line->vat_src_code && !preg_match('/\(.*\)/', $vatrate)) { - $vatrate .= ' ('.$line->vat_src_code.')'; - } - - if (getDolGlobalString('MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION')) { + if ($line->vat_src_code && !preg_match('/\(.*\)/', $vatrate)) $vatrate .= ' ('.$line->vat_src_code.')'; + + if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) { @@ -1025,6 +1029 @@ - // ref_ext - if (empty($line->ref_ext)) { - $line->ref_ext = ''; - } - - $result = $this->addline( + $result = $this->addline( @@ -1054,5 +1053,2 @@ - $origintype, - $originid, - 0, - $line->ref_ext, - 1 + $origintype, + $originid @@ -1060,2 +1056,4 @@ - if ($result < 0) { - if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) { + if ($result < 0) + { + if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) + { @@ -1075,2 +1072,0 @@ - $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. - @@ -1079,6 +1075,5 @@ - if (!empty($this->ref)) { - $initialref = $this->ref; - } - - $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='".$this->db->escape($initialref)."' WHERE rowid=".((int) $this->id); - if ($this->db->query($sql)) { + if (!empty($this->ref)) $initialref = $this->ref; + + $sql = 'UPDATE '.MAIN_DB_PREFIX."commande SET ref='".$this->db->escape($initialref)."' WHERE rowid=".$this->id; + if ($this->db->query($sql)) + { @@ -1087 +1082,2 @@ - if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects + if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects + { @@ -1092,4 +1088,8 @@ - if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) { - foreach ($this->linked_objects as $origin => $tmp_origin_id) { - if (is_array($tmp_origin_id)) { // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...)) - foreach ($tmp_origin_id as $origin_id) { + if (!$error && $this->id && is_array($this->linked_objects) && !empty($this->linked_objects)) + { + foreach ($this->linked_objects as $origin => $tmp_origin_id) + { + if (is_array($tmp_origin_id)) // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...)) + { + foreach ($tmp_origin_id as $origin_id) + { @@ -1097 +1097,2 @@ - if (!$ret) { + if (!$ret) + { @@ -1102 +1103,3 @@ - } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1)) + } + else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1)) + { @@ -1105 +1108,2 @@ - if (!$ret) { + if (!$ret) + { @@ -1113 +1117,2 @@ - if (!$error && $this->id && getDolGlobalString('MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN') && !empty($this->origin) && !empty($this->origin_id)) { // Get contact from origin object + if (!$error && $this->id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) // Get contact from origin object + { @@ -1116 +1121,2 @@ - if ($originforcontact == 'shipping') { // shipment and order share the same contacts. If creating from shipment we take data of order + if ($originforcontact == 'shipping') // shipment and order share the same contacts. If creating from shipment we take data of order + { @@ -1121,2 +1127,4 @@ - if (count($exp->linkedObjectsIds['commande']) > 0) { - foreach ($exp->linkedObjectsIds['commande'] as $key => $value) { + if (count($exp->linkedObjectsIds['commande']) > 0) + { + foreach ($exp->linkedObjectsIds['commande'] as $key => $value) + { @@ -1124,5 +1132,2 @@ - if (is_object($value)) { - $originidforcontact = $value->id; - } else { - $originidforcontact = $value; - } + if (is_object($value)) $originidforcontact = $value->id; + else $originidforcontact = $value; @@ -1135 +1140 @@ - $sqlcontact .= " WHERE element_id = ".((int) $originidforcontact)." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact)."'"; + $sqlcontact .= " WHERE element_id = ".$originidforcontact." AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$originforcontact."'"; @@ -1138,2 +1143,4 @@ - if ($resqlcontact) { - while ($objcontact = $this->db->fetch_object($resqlcontact)) { + if ($resqlcontact) + { + while ($objcontact = $this->db->fetch_object($resqlcontact)) + { @@ -1143,2 +1149,0 @@ - } else { - dol_print_error($resqlcontact); @@ -1145,0 +1151 @@ + else dol_print_error($resqlcontact); @@ -1148 +1154,2 @@ - if (!$error) { + if (!$error) + { @@ -1150,3 +1157 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -1155 +1160,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -1158,3 +1164 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -1164 +1168,2 @@ - if (!$error) { + if (!$error) + { @@ -1167 +1172,3 @@ - } else { + } + else + { @@ -1171 +1178,3 @@ - } else { + } + else + { @@ -1177,4 +1186,4 @@ - - return 0; - } else { - $this->error = $this->db->lasterror(); + } + else + { + dol_print_error($this->db); @@ -1203 +1212 @@ - foreach ($this->lines as $line) { + foreach ($this->lines as $line) @@ -1205 +1213,0 @@ - } @@ -1211 +1219,2 @@ - if (!empty($socid) && $socid != $this->socid) { + if (!empty($socid) && $socid != $this->socid) + { @@ -1214 +1223,2 @@ - if ($objsoc->fetch($socid) > 0) { + if ($objsoc->fetch($socid) > 0) + { @@ -1217 +1226,0 @@ - $this->deposit_percent = (!empty($objsoc->deposit_percent) ? $objsoc->deposit_percent : null); @@ -1232 +1241 @@ - $this->user_validation_id = 0; + $this->user_valid = ''; @@ -1237,10 +1246 @@ - if (!getDolGlobalString('MAIN_KEEP_REF_CUSTOMER_ON_CLONING')) { - $this->ref_client = ''; - $this->ref_customer = ''; - } - - // Do not clone ref_ext - $num = count($this->lines); - for ($i = 0; $i < $num; $i++) { - $this->lines[$i]->ref_ext = ''; - } + if (empty($conf->global->MAIN_KEEP_REF_CUSTOMER_ON_CLONING)) $this->ref_client = ''; @@ -1251,5 +1251,4 @@ - if ($result < 0) { - $error++; - } - - if (!$error) { + if ($result < 0) $error++; + + if (!$error) + { @@ -1257 +1256,2 @@ - if ($this->copy_linked_contact($objFrom, 'internal') < 0) { + if ($this->copy_linked_contact($objFrom, 'internal') < 0) + { @@ -1262 +1262,2 @@ - if (!$error) { + if (!$error) + { @@ -1264,2 +1265,3 @@ - if ($this->socid == $objFrom->socid) { - if ($this->copy_linked_contact($objFrom, 'external') < 0) { + if ($this->socid == $objFrom->socid) + { + if ($this->copy_linked_contact($objFrom, 'external') < 0) @@ -1267,5 +1269,5 @@ - } - } - } - - if (!$error) { + } + } + + if (!$error) + { @@ -1273 +1275,2 @@ - if (is_object($hookmanager)) { + if (is_object($hookmanager)) + { @@ -1277,4 +1280 @@ - if ($reshook < 0) { - $this->setErrorsFromObject($hookmanager); - $error++; - } + if ($reshook < 0) $error++; @@ -1287 +1287,2 @@ - if (!$error) { + if (!$error) + { @@ -1290 +1291,3 @@ - } else { + } + else + { @@ -1302 +1305 @@ - * @return int Return integer <0 if KO, 0 if nothing done, 1 if OK + * @return int <0 if KO, 0 if nothing done, 1 if OK @@ -1308 +1310,0 @@ - dol_include_once('/multicurrency/class/multicurrency.class.php'); @@ -1312,0 +1315 @@ + @@ -1314 +1316,0 @@ - $this->date = dol_now(); @@ -1318 +1320,2 @@ - for ($i = 0; $i < $num; $i++) { + for ($i = 0; $i < $num; $i++) + { @@ -1341 +1344 @@ - $line->date_start = $object->lines[$i]->date_start; + $line->date_start = $object->lines[$i]->date_start; @@ -1350,3 +1352,0 @@ - $line->origin = $object->element; - $line->origin_id = $object->lines[$i]->id; - @@ -1355 +1355 @@ - foreach ($object->lines[$i]->array_options as $options_key => $value) { + foreach ($object->lines[$i]->array_options as $options_key => $value) @@ -1357,3 +1357,2 @@ - } - - $this->lines[$i] = $line; + + $this->lines[$i] = $line; @@ -1366 +1364,0 @@ - $this->deposit_percent = $object->deposit_percent; @@ -1371 +1369 @@ - $this->delivery_date = $object->delivery_date; + $this->date_livraison = $object->date_livraison; @@ -1375 +1373 @@ - $this->contact_id = $object->contact_id; + $this->contact_id = $object->contactid; @@ -1377,5 +1375,5 @@ - $this->ref_customer = $object->ref_client; - - if (!getDolGlobalString('MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN')) { - $this->note_private = $object->note_private; - $this->note_public = $object->note_public; + + if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) + { + $this->note_private = $object->note_private; + $this->note_public = $object->note_public; @@ -1386,23 +1383,0 @@ - - // Multicurrency (test on $this->multicurrency_tx because we should take the default rate only if not using origin rate) - if (!empty($conf->multicurrency->enabled)) { - if (!empty($object->multicurrency_code)) { - $this->multicurrency_code = $object->multicurrency_code; - } - if (getDolGlobalString('MULTICURRENCY_USE_ORIGIN_TX') && !empty($object->multicurrency_tx)) { - $this->multicurrency_tx = $object->multicurrency_tx; - } - - if (!empty($this->multicurrency_code) && empty($this->multicurrency_tx)) { - $tmparray = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code, $this->date_commande); - $this->fk_multicurrency = $tmparray[0]; - $this->multicurrency_tx = $tmparray[1]; - } else { - $this->fk_multicurrency = MultiCurrency::getIdFromCode($this->db, $this->multicurrency_code); - } - if (empty($this->fk_multicurrency)) { - $this->multicurrency_code = $conf->currency; - $this->fk_multicurrency = 0; - $this->multicurrency_tx = 1; - } - } @@ -1423 +1398,2 @@ - if (isset($object->other_linked_objects) && is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) { + if (is_array($object->other_linked_objects) && !empty($object->other_linked_objects)) + { @@ -1429 +1405,2 @@ - if ($ret > 0) { + if ($ret > 0) + { @@ -1436,6 +1413,4 @@ - if ($reshook < 0) { - $this->setErrorsFromObject($hookmanager); - $error++; - } - - if (!$error) { + if ($reshook < 0) $error++; + + if (!$error) + { @@ -1443 +1418,2 @@ - if (getDolGlobalString('ORDER_VALID_AFTER_CLOSE_PROPAL')) { + if (!empty($conf->global->ORDER_VALID_AFTER_CLOSE_PROPAL)) + { @@ -1448,6 +1424,4 @@ - } else { - return -1; - } - } else { - return -1; - } + } + else return -1; + } + else return -1; @@ -1472,2 +1446,2 @@ - * @param int|string $date_start Start date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html) - * @param int|string $date_end End date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html) + * @param int $date_start Start date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html) + * @param int $date_end End date of the line - Added by Matelli (See http://matelli.fr/showcases/patchs-dolibarr/add-dates-in-order-lines.html) @@ -1486,2 +1459,0 @@ - * @param string $ref_ext line external reference - * @param int $noupdateafterinsertline No update after insert of line @@ -1497 +1469 @@ - public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $info_bits = 0, $fk_remise_except = 0, $price_base_type = 'HT', $pu_ttc = 0, $date_start = '', $date_end = '', $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = null, $pa_ht = 0, $label = '', $array_options = array(), $fk_unit = null, $origin = '', $origin_id = 0, $pu_ht_devise = 0, $ref_ext = '', $noupdateafterinsertline = 0) + public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $info_bits = 0, $fk_remise_except = 0, $price_base_type = 'HT', $pu_ttc = 0, $date_start = '', $date_end = '', $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = null, $pa_ht = 0, $label = '', $array_options = 0, $fk_unit = null, $origin = '', $origin_id = 0, $pu_ht_devise = 0) @@ -1503 +1475 @@ - $logtext .= ", date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise, ref_ext=$ref_ext"; + $logtext .= ", date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise"; @@ -1506 +1478,2 @@ - if ($this->statut == self::STATUS_DRAFT) { + if ($this->statut == self::STATUS_DRAFT) + { @@ -1510,31 +1483,9 @@ - - if (empty($remise_percent)) { - $remise_percent = 0; - } - if (empty($qty)) { - $qty = 0; - } - if (empty($info_bits)) { - $info_bits = 0; - } - if (empty($rang)) { - $rang = 0; - } - if (empty($txtva)) { - $txtva = 0; - } - if (empty($txlocaltax1)) { - $txlocaltax1 = 0; - } - if (empty($txlocaltax2)) { - $txlocaltax2 = 0; - } - if (empty($fk_parent_line) || $fk_parent_line < 0) { - $fk_parent_line = 0; - } - if (empty($this->fk_multicurrency)) { - $this->fk_multicurrency = 0; - } - if (empty($ref_ext)) { - $ref_ext = ''; - } + if (empty($remise_percent)) $remise_percent = 0; + if (empty($qty)) $qty = 0; + if (empty($info_bits)) $info_bits = 0; + if (empty($rang)) $rang = 0; + if (empty($txtva)) $txtva = 0; + if (empty($txlocaltax1)) $txlocaltax1 = 0; + if (empty($txlocaltax2)) $txlocaltax2 = 0; + if (empty($fk_parent_line) || $fk_parent_line < 0) $fk_parent_line = 0; + if (empty($this->fk_multicurrency)) $this->fk_multicurrency = 0; @@ -1553 +1504,2 @@ - if ($price_base_type == 'HT') { + if ($price_base_type == 'HT') + { @@ -1555 +1507,3 @@ - } else { + } + else + { @@ -1562,3 +1516 @@ - if ($type < 0) { - return -1; - } + if ($type < 0) return -1; @@ -1572 +1524 @@ - $this->db->begin(); + $this->db->begin(); @@ -1575 +1527,2 @@ - if (!empty($fk_product) && $fk_product > 0) { + if (!empty($fk_product)) + { @@ -1580 +1533,2 @@ - if (getDolGlobalString('STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) { + if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) + { @@ -1599 +1553,2 @@ - if (preg_match('/\((.*)\)/', $txtva, $reg)) { + if (preg_match('/\((.*)\)/', $txtva, $reg)) + { @@ -1629 +1584,2 @@ - if ($ranktouse == -1) { + if ($ranktouse == -1) + { @@ -1638 +1594,2 @@ - if ($remise_percent > 0) { + if ($remise_percent > 0) + { @@ -1652 +1608,0 @@ - $this->line->ref_ext = $ref_ext; @@ -1658,2 +1614,2 @@ - $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]; - $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]; + $this->line->localtax1_type = $localtaxes_type[0]; + $this->line->localtax2_type = $localtaxes_type[2]; @@ -1693,0 +1650 @@ + $this->line->remise = $remise; @@ -1700 +1657,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -1702,8 +1660 @@ - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $ranktouse; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); - } - } + if (!empty($fk_parent_line)) $this->line_order(true, 'DESC'); @@ -1712,5 +1663,3 @@ - if (empty($noupdateafterinsertline)) { - $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. - } - - if ($result > 0) { + $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. + if ($result > 0) + { @@ -1719 +1668,3 @@ - } else { + } + else + { @@ -1723 +1674,3 @@ - } else { + } + else + { @@ -1729 +1682,3 @@ - } else { + } + else + { @@ -1744,2 +1699,2 @@ - * @param int|string $date_start Start date of the line - * @param int|string $date_end End date of the line + * @param int $date_start Start date of the line + * @param int $date_end End date of the line @@ -1748,0 +1704 @@ + * insere dans tableau $this->products @@ -1752 +1708 @@ - // phpcs:enable + // phpcs:enable @@ -1755,5 +1711,4 @@ - if (!$qty) { - $qty = 1; - } - - if ($idproduct > 0) { + if (!$qty) $qty = 1; + + if ($idproduct > 0) + { @@ -1765,3 +1720 @@ - if (empty($tva_tx)) { - $tva_npr = 0; - } + if (empty($tva_tx)) $tva_npr = 0; @@ -1793,3 +1746,2 @@ - - $line->product_ref = $prod->ref; - $line->product_label = $prod->label; + $line->ref = $prod->ref; + $line->libelle = $prod->label; @@ -1800,6 +1752,2 @@ - if ($date_start) { - $line->date_start = $date_start; - } - if ($date_end) { - $line->date_end = $date_end; - } + if ($date_start) { $line->date_start = $date_start; } + if ($date_end) { $line->date_end = $date_end; } @@ -1810 +1758 @@ - if (!empty($conf->global->PRODUIT_SOUSPRODUITS)) + if (! empty($conf->global->PRODUIT_SOUSPRODUITS)) @@ -1818,7 +1766,9 @@ - foreach($prods_arbo as $key => $value) - { - // print "id : ".$value[1].' :qty: '.$value[0].'
'; - if not in lines { - $this->add_product($value[1], $value[0]); - } - } + foreach($prods_arbo as $key => $value) + { + // print "id : ".$value[1].' :qty: '.$value[0].'
'; + if(! in_array($value[1],$this->products)) + $this->add_product($value[1], $value[0]); + + } + } + @@ -1843,6 +1793,4 @@ - if (empty($id) && empty($ref) && empty($ref_ext)) { - return -1; - } - - $sql = 'SELECT c.rowid, c.entity, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_user_valid, c.fk_user_modif, c.fk_statut'; - $sql .= ', c.amount_ht, c.total_ht, c.total_ttc, c.total_tva, c.localtax1 as total_localtax1, c.localtax2 as total_localtax2, c.fk_cond_reglement, c.deposit_percent, c.fk_mode_reglement, c.fk_availability, c.fk_input_reason'; + if (empty($id) && empty($ref) && empty($ref_ext)) return -1; + + $sql = 'SELECT c.rowid, c.entity, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_user_valid, c.fk_statut'; + $sql .= ', c.amount_ht, c.total_ht, c.total_ttc, c.tva as total_tva, c.localtax1 as total_localtax1, c.localtax2 as total_localtax2, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_availability, c.fk_input_reason'; @@ -1851 +1799 @@ - $sql .= ', c.date_livraison as delivery_date'; + $sql .= ', c.date_livraison'; @@ -1854,2 +1802,2 @@ - $sql .= ', c.fk_projet as fk_project, c.source, c.facture as billed'; - $sql .= ', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams'; + $sql .= ', c.fk_projet as fk_project, c.remise_percent, c.remise, c.remise_absolue, c.source, c.facture as billed'; + $sql .= ', c.note_private, c.note_public, c.ref_client, c.ref_ext, c.ref_int, c.model_pdf, c.last_main_doc, c.fk_delivery_address, c.extraparams'; @@ -1859 +1807 @@ - $sql .= ", i.libelle as label_incoterms"; + $sql .= ", i.libelle as label_incoterms"; @@ -1871,12 +1819,6 @@ - if ($id) { - $sql .= " WHERE c.rowid=".((int) $id); - } else { - $sql .= " WHERE c.entity IN (".getEntity('commande').")"; // Dont't use entity if you use rowid - } - - if ($ref) { - $sql .= " AND c.ref='".$this->db->escape($ref)."'"; - } - if ($ref_ext) { - $sql .= " AND c.ref_ext='".$this->db->escape($ref_ext)."'"; - } + if ($id) $sql .= " WHERE c.rowid=".$id; + else $sql .= " WHERE c.entity IN (".getEntity('commande').")"; // Dont't use entity if you use rowid + + if ($ref) $sql .= " AND c.ref='".$this->db->escape($ref)."'"; + if ($ref_ext) $sql .= " AND c.ref_ext='".$this->db->escape($ref_ext)."'"; + if ($notused) $sql .= " AND c.ref_int='".$this->db->escape($notused)."'"; @@ -1886 +1828,2 @@ - if ($result) { + if ($result) + { @@ -1888 +1831,2 @@ - if ($obj) { + if ($obj) + { @@ -1895 +1839,2 @@ - $this->ref_ext = $obj->ref_ext; + $this->ref_ext = $obj->ref_ext; + $this->ref_int = $obj->ref_int; @@ -1904,2 +1848,0 @@ - $this->status = $obj->fk_statut; - @@ -1907,3 +1850 @@ - $this->user_creation_id = $obj->fk_user_author; - $this->user_validation_id = $obj->fk_user_valid; - $this->user_modification_id = $obj->fk_user_modif; + $this->user_valid = $obj->fk_user_valid; @@ -1918,2 +1859,5 @@ - $this->date_validation = $this->db->jdate($obj->date_valid); - $this->date_modification = $this->db->jdate($obj->tms); + $this->date_validation = $this->db->jdate($obj->date_valid); + $this->date_modification = $this->db->jdate($obj->tms); + $this->remise = $obj->remise; + $this->remise_percent = $obj->remise_percent; + $this->remise_absolue = $obj->remise_absolue; @@ -1925 +1869 @@ - $this->model_pdf = $obj->model_pdf; + $this->modelpdf = $obj->model_pdf; @@ -1934 +1877,0 @@ - $this->deposit_percent = $obj->deposit_percent; @@ -1941 +1884 @@ - $this->delivery_date = $this->db->jdate($obj->delivery_date); + $this->date_livraison = $this->db->jdate($obj->date_livraison); @@ -1961 +1904 @@ - $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams, true) : array(); + $this->extraparams = (array) json_decode($obj->extraparams, true); @@ -1965 +1908,3 @@ - // Retrieve all extrafield + if ($this->statut == self::STATUS_DRAFT) $this->brouillon = 1; + + // Retreive all extrafield @@ -1973 +1918,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -1977 +1923,3 @@ - } else { + } + else + { @@ -1981 +1929,3 @@ - } else { + } + else + { @@ -1997 +1947 @@ - // phpcs:enable + // phpcs:enable @@ -2008,2 +1958,4 @@ - if ($result > 0) { - if ($remise->fk_facture) { // Protection against multiple submission + if ($result > 0) + { + if ($remise->fk_facture) // Protection against multiple submission + { @@ -2025,0 +1978 @@ + $line->remise = 0; @@ -2035 +1988,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -2037 +1991,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -2040 +1995,3 @@ - } else { + } + else + { @@ -2044 +2001,3 @@ - } else { + } + else + { @@ -2046 +2004,0 @@ - $this->errors = $line->errors; @@ -2050 +2008,3 @@ - } else { + } + else + { @@ -2061 +2021 @@ - * @param int $only_product Return only physical products, not services + * @param int $only_product Return only physical products @@ -2063 +2023 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -2067 +2026,0 @@ - // phpcs:enable @@ -2069 +2028 @@ - + // phpcs:enable @@ -2072 +2031 @@ - $sql = 'SELECT l.rowid, l.fk_product, l.fk_parent_line, l.product_type, l.fk_commande, l.label as custom_label, l.description, l.price, l.qty, l.vat_src_code, l.tva_tx, l.ref_ext,'; + $sql = 'SELECT l.rowid, l.fk_product, l.fk_parent_line, l.product_type, l.fk_commande, l.label as custom_label, l.description, l.price, l.qty, l.vat_src_code, l.tva_tx,'; @@ -2077 +2036 @@ - $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tosell as product_tosell, p.tobuy as product_tobuy, p.tobatch as product_tobatch, p.barcode as product_barcode,'; + $sql .= ' p.ref as product_ref, p.description as product_desc, p.fk_product_type, p.label as product_label, p.tobatch as product_tobatch,'; @@ -2081,4 +2040,2 @@ - $sql .= ' WHERE l.fk_commande = '.((int) $this->id); - if ($only_product) { - $sql .= ' AND p.fk_product_type = 0'; - } + $sql .= ' WHERE l.fk_commande = '.$this->id; + if ($only_product) $sql .= ' AND p.fk_product_type = 0'; @@ -2089 +2046,2 @@ - if ($result) { + if ($result) + { @@ -2093 +2051,2 @@ - while ($i < $num) { + while ($i < $num) + { @@ -2107 +2065,0 @@ - $line->ref_ext = $objp->ref_ext; @@ -2135,0 +2094 @@ + $line->product_ref = $objp->product_ref; @@ -2137,2 +2095,0 @@ - - $line->product_ref = $objp->product_ref; @@ -2140,2 +2096,0 @@ - $line->product_tosell = $objp->product_tosell; - $line->product_tobuy = $objp->product_tobuy; @@ -2144,2 +2098,0 @@ - $line->product_barcode = $objp->product_barcode; - @@ -2168,9 +2121,7 @@ - if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) { - $tmpproduct = new Product($this->db); - $tmpproduct->fetch($objp->fk_product); - $tmpproduct->getMultiLangs(); - - $line->multilangs = $tmpproduct->multilangs; - } - - $this->lines[$i] = $line; + if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) { + $line = new Product($this->db); + $line->fetch($objp->fk_product); + $line->getMultiLangs(); + } + + $this->lines[$i] = $line; @@ -2184 +2135,3 @@ - } else { + } + else + { @@ -2194 +2147 @@ - * @return int Return integer <0 if KO, Nbr of product lines if OK + * @return int <0 if KO, Nbr of product lines if OK @@ -2199,4 +2152,3 @@ - foreach ($this->lines as $line) { - if ($line->product_type == 0) { - $nb++; - } + foreach ($this->lines as $line) + { + if ($line->product_type == 0) $nb++; @@ -2210 +2162 @@ - * @return int Return integer <0 if KO, Nbr of service lines if OK + * @return int <0 if KO, Nbr of service lines if OK @@ -2215,4 +2167,3 @@ - foreach ($this->lines as $line) { - if ($line->product_type == 1) { - $nb++; - } + foreach ($this->lines as $line) + { + if ($line->product_type == 1) $nb++; @@ -2226 +2177 @@ - * @return int Return integer <0 if KO, Nb of shipment found if OK + * @return int <0 if KO, Nb of shipment found if OK @@ -2237 +2188 @@ - $sql .= ' AND cd.fk_commande = '.((int) $this->id); + $sql .= ' AND cd.fk_commande ='.$this->id; @@ -2242 +2193,2 @@ - if ($resql) { + if ($resql) + { @@ -2244,3 +2196 @@ - if ($obj) { - $nb = $obj->nb; - } + if ($obj) $nb = $obj->nb; @@ -2250 +2200,3 @@ - } else { + } + else + { @@ -2261,4 +2213,3 @@ - * @param int $fk_product Add a filter on a product - * @return int Return integer <0 if KO, Nb of lines found if OK - */ - public function loadExpeditions($filtre_statut = -1, $fk_product = 0) + * @return int <0 if KO, Nb of lines found if OK + */ + public function loadExpeditions($filtre_statut = -1) @@ -2271,3 +2222 @@ - if ($filtre_statut >= 0) { - $sql .= ' '.MAIN_DB_PREFIX.'expedition as e,'; - } + if ($filtre_statut >= 0) $sql .= ' '.MAIN_DB_PREFIX.'expedition as e,'; @@ -2276,3 +2225 @@ - if ($filtre_statut >= 0) { - $sql .= ' ed.fk_expedition = e.rowid AND'; - } + if ($filtre_statut >= 0) $sql .= ' ed.fk_expedition = e.rowid AND'; @@ -2280,7 +2227,3 @@ - $sql .= ' AND cd.fk_commande = '.((int) $this->id); - if ($fk_product > 0) { - $sql .= ' AND cd.fk_product = '.((int) $fk_product); - } - if ($filtre_statut >= 0) { - $sql .= ' AND e.fk_statut >= '.((int) $filtre_statut); - } + $sql .= ' AND cd.fk_commande ='.$this->id; + if ($this->fk_product > 0) $sql .= ' AND cd.fk_product = '.$this->fk_product; + if ($filtre_statut >= 0) $sql .= ' AND e.fk_statut >= '.$filtre_statut; @@ -2292 +2235,2 @@ - if ($resql) { + if ($resql) + { @@ -2295 +2239,2 @@ - while ($i < $num) { + while ($i < $num) + { @@ -2302 +2247,3 @@ - } else { + } + else + { @@ -2308,2 +2255,3 @@ - /** - * Returns an array with expeditions lines number + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Returns a array with expeditions lines number @@ -2312,3 +2260,6 @@ - */ - public function countNbOfShipments() - { + * + * TODO deprecate, move to Shipping class + */ + public function nb_expedition() + { + // phpcs:enable @@ -2318,2 +2269 @@ - $sql .= ' WHERE el.fk_source = '.((int) $this->id); - $sql .= " AND el.sourcetype = 'commande'"; + $sql .= ' WHERE el.fk_source = '.$this->id; @@ -2324 +2274,2 @@ - if ($resql) { + if ($resql) + { @@ -2327,5 +2278,2 @@ - } else { - dol_print_error($this->db); - } - - return 0; + } + else dol_print_error($this->db); @@ -2343,3 +2291,3 @@ - /*public function stock_array($filtre_statut = self::STATUS_CANCELED) - { - // phpcs:enable + public function stock_array($filtre_statut = self::STATUS_CANCELED) + { + // phpcs:enable @@ -2353 +2301,2 @@ - if (count($array_of_product)) { + if (count($array_of_product)) + { @@ -2356,2 +2305,2 @@ - $sql .= " WHERE ps.fk_product IN (".$this->db->sanitize(join(',', $array_of_product)).")"; - $sql .= ' GROUP BY fk_product'; + $sql .= " WHERE ps.fk_product IN (".join(',', $array_of_product).")"; + $sql .= ' GROUP BY fk_product '; @@ -2359 +2308,2 @@ - if ($resql) { + if ($resql) + { @@ -2362 +2312,2 @@ - while ($i < $num) { + while ($i < $num) + { @@ -2371 +2322 @@ - }*/ + } @@ -2378 +2328,0 @@ - * @param int $id Id of object (for a check) @@ -2381,3 +2331,4 @@ - public function deleteline($user = null, $lineid = 0, $id = 0) - { - if ($this->statut == self::STATUS_DRAFT) { + public function deleteline($user = null, $lineid = 0) + { + if ($this->statut == self::STATUS_DRAFT) + { @@ -2386,10 +2337,53 @@ - // Delete line - $line = new OrderLine($this->db); - - $line->context = $this->context; - - // Load data - $line->fetch($lineid); - - if ($id > 0 && $line->fk_commande != $id) { - $this->error = 'ErrorLineIDDoesNotMatchWithObjectID'; + $sql = "SELECT fk_product, qty"; + $sql .= " FROM ".MAIN_DB_PREFIX."commandedet"; + $sql .= " WHERE rowid = ".$lineid; + + $result = $this->db->query($sql); + if ($result) + { + $obj = $this->db->fetch_object($result); + + if ($obj) + { + $product = new Product($this->db); + $product->id = $obj->fk_product; + + // Delete line + $line = new OrderLine($this->db); + + // For triggers + $line->fetch($lineid); + + if ($line->delete($user) > 0) + { + $result = $this->update_price(1); + + if ($result > 0) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + $this->error = $this->db->lasterror(); + return -1; + } + } + else + { + $this->db->rollback(); + $this->error = $line->error; + return -1; + } + } + else + { + $this->db->rollback(); + return 0; + } + } + else + { + $this->db->rollback(); + $this->error = $this->db->lasterror(); @@ -2398,22 +2392,3 @@ - - // Memorize previous line for triggers - $staticline = clone $line; - $line->oldline = $staticline; - - if ($line->delete($user) > 0) { - $result = $this->update_price(1); - - if ($result > 0) { - $this->db->commit(); - return 1; - } else { - $this->db->rollback(); - $this->error = $this->db->lasterror(); - return -1; - } - } else { - $this->db->rollback(); - $this->error = $line->error; - return -1; - } - } else { + } + else + { @@ -2429,2 +2403,0 @@ - * @deprecated - * @see setDiscount() @@ -2434 +2407 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -2438,19 +2411,5 @@ - // phpcs:enable - dol_syslog(get_class($this)."::set_remise is deprecated, use setDiscount instead", LOG_NOTICE); - return $this->setDiscount($user, $remise, $notrigger); - } - - /** - * Set a percentage discount - * - * @param User $user User setting the discount - * @param float $remise Discount (percent) - * @param int $notrigger 1=Does not execute triggers, 0= execute triggers - * @return int Return integer <0 if KO, >0 if OK - * @deprecated remise_percent is a deprecated field for object parent - */ - public function setDiscount($user, $remise, $notrigger = 0) - { - $remise = trim($remise) ? trim($remise) : 0; - - if ($user->hasRight('commande', 'creer')) { + // phpcs:enable + $remise = trim($remise) ?trim($remise) : 0; + + if ($user->rights->commande->creer) + { @@ -2461 +2420 @@ - $remise = price2num($remise, 2); + $remise = price2num($remise); @@ -2464,2 +2423,2 @@ - $sql .= ' SET remise_percent = '.((float) $remise); - $sql .= ' WHERE rowid = '.((int) $this->id).' AND fk_statut = '.((int) self::STATUS_DRAFT); + $sql .= ' SET remise_percent = '.$remise; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; @@ -2469 +2428,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2474 +2434,2 @@ - if (!$error) { + if (!$error) + { @@ -2480 +2441,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2483,3 +2445 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2489 +2449,2 @@ - if (!$error) { + if (!$error) + { @@ -2492,2 +2453,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2501,2 +2464,0 @@ - - return 0; @@ -2508 +2470 @@ - * Set a fixed amount discount + * Applique une remise absolue @@ -2513,3 +2475,2 @@ - * @return int Return integer <0 if KO, >0 if OK - */ - /* + * @return int <0 if KO, >0 if OK + */ @@ -2518,8 +2479,5 @@ - // phpcs:enable - if (empty($remise)) { - $remise = 0; - } - - $remise = price2num($remise); - - if ($user->hasRight('commande', 'creer')) { + // phpcs:enable + $remise = trim($remise) ?trim($remise) : 0; + + if ($user->rights->commande->creer) + { @@ -2529,0 +2488,2 @@ + $remise = price2num($remise); + @@ -2531,2 +2491,2 @@ - $sql .= ' SET remise_absolue = '.((float) $remise); - $sql .= ' WHERE rowid = '.((int) $this->id).' AND fk_statut = '.self::STATUS_DRAFT; + $sql .= ' SET remise_absolue = '.$remise; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = '.self::STATUS_DRAFT.' ;'; @@ -2536 +2496,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2541 +2502,2 @@ - if (!$error) { + if (!$error) + { @@ -2547 +2509,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2550,3 +2513 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2556 +2517,2 @@ - if (!$error) { + if (!$error) + { @@ -2559,2 +2521,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2568,4 +2533,2 @@ - - return 0; - } - */ + } + @@ -2580 +2543 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -2584,2 +2547,3 @@ - // phpcs:enable - if ($user->hasRight('commande', 'creer')) { + // phpcs:enable + if ($user->rights->commande->creer) + { @@ -2592 +2556 @@ - $sql .= " WHERE rowid = ".((int) $this->id)." AND fk_statut = ".((int) self::STATUS_DRAFT); + $sql .= " WHERE rowid = ".$this->id." AND fk_statut = ".self::STATUS_DRAFT; @@ -2596 +2560,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2601 +2566,2 @@ - if (!$error) { + if (!$error) + { @@ -2606 +2572,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2609,3 +2576 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2615 +2580,2 @@ - if (!$error) { + if (!$error) + { @@ -2618,2 +2584,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2626 +2595,3 @@ - } else { + } + else + { @@ -2633,15 +2603,0 @@ - * Set delivery date - * - * @param User $user Object user that modify - * @param int $delivery_date Delivery date - * @param int $notrigger 1=Does not execute triggers, 0= execute triggers - * @return int Return integer <0 if ko, >0 if ok - * @deprecated Use setDeliveryDate - */ - public function set_date_livraison($user, $delivery_date, $notrigger = 0) - { - // phpcs:enable - return $this->setDeliveryDate($user, $delivery_date, $notrigger); - } - - /** @@ -2651 +2607 @@ - * @param int $delivery_date Delivery date + * @param int $date_livraison Date de livraison @@ -2653,5 +2609,7 @@ - * @return int Return integer <0 si ko, >0 si ok - */ - public function setDeliveryDate($user, $delivery_date, $notrigger = 0) - { - if ($user->hasRight('commande', 'creer')) { + * @return int <0 si ko, >0 si ok + */ + public function set_date_livraison($user, $date_livraison, $notrigger = 0) + { + // phpcs:enable + if ($user->rights->commande->creer) + { @@ -2663,2 +2621,2 @@ - $sql .= " SET date_livraison = ".($delivery_date ? "'".$this->db->idate($delivery_date)."'" : 'null'); - $sql .= " WHERE rowid = ".((int) $this->id); + $sql .= " SET date_livraison = ".($date_livraison ? "'".$this->db->idate($date_livraison)."'" : 'null'); + $sql .= " WHERE rowid = ".$this->id; @@ -2668 +2626,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2673 +2632,2 @@ - if (!$error) { + if (!$error) + { @@ -2675,4 +2635,5 @@ - $this->delivery_date = $delivery_date; - } - - if (!$notrigger && empty($error)) { + $this->date_livraison = $date_livraison; + } + + if (!$notrigger && empty($error)) + { @@ -2681,3 +2642 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2687 +2646,2 @@ - if (!$error) { + if (!$error) + { @@ -2690,2 +2650,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2698 +2661,3 @@ - } else { + } + else + { @@ -2715,5 +2680,5 @@ - * @return int|array -1 if KO, array with result if OK - */ - public function liste_array($shortlist = 0, $draft = 0, $excluser = null, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'c.date_commande', $sortorder = 'DESC') - { - // phpcs:enable + * @return int -1 if KO, array with result if OK + */ + public function liste_array($shortlist = 0, $draft = 0, $excluser = '', $socid = 0, $limit = 0, $offset = 0, $sortfield = 'c.date_commande', $sortorder = 'DESC') + { + // phpcs:enable @@ -2726,3 +2691 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { - $sql .= ", sc.fk_soc, sc.fk_user"; - } + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user"; @@ -2730,3 +2693 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { - $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - } + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; @@ -2735,12 +2696,7 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$socid) { //restriction - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - if ($socid) { - $sql .= " AND s.rowid = ".((int) $socid); - } - if ($draft) { - $sql .= " AND c.fk_statut = ".self::STATUS_DRAFT; - } - if (is_object($excluser)) { - $sql .= " AND c.fk_user_author <> ".((int) $excluser->id); - } + if (!$user->rights->societe->client->voir && !$socid) //restriction + { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; + } + if ($socid) $sql .= " AND s.rowid = ".$socid; + if ($draft) $sql .= " AND c.fk_statut = ".self::STATUS_DRAFT; + if (is_object($excluser)) $sql .= " AND c.fk_user_author <> ".$excluser->id; @@ -2751 +2707,2 @@ - if ($result) { + if ($result) + { @@ -2753 +2710,2 @@ - if ($numc) { + if ($numc) + { @@ -2755 +2713,2 @@ - while ($i < $numc) { + while ($i < $numc) + { @@ -2758 +2717,2 @@ - if ($shortlist == 1) { + if ($shortlist == 1) + { @@ -2760 +2720,3 @@ - } elseif ($shortlist == 2) { + } + elseif ($shortlist == 2) + { @@ -2762 +2724,3 @@ - } else { + } + else + { @@ -2771 +2735,3 @@ - } else { + } + else + { @@ -2789 +2755,2 @@ - if ($this->statut >= self::STATUS_DRAFT) { + if ($this->statut >= self::STATUS_DRAFT) + { @@ -2795,2 +2762,2 @@ - $sql .= ' SET fk_availability = '.((int) $availability_id); - $sql .= ' WHERE rowid='.((int) $this->id); + $sql .= ' SET fk_availability = '.$availability_id; + $sql .= ' WHERE rowid='.$this->id; @@ -2800 +2767,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2805 +2773,2 @@ - if (!$error) { + if (!$error) + { @@ -2810 +2779,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2813,3 +2783 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2819 +2787,2 @@ - if (!$error) { + if (!$error) + { @@ -2822,2 +2791,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2830 +2802,3 @@ - } else { + } + else + { @@ -2849 +2823 @@ - // phpcs:enable + // phpcs:enable @@ -2853 +2827,2 @@ - if ($this->statut >= self::STATUS_DRAFT) { + if ($this->statut >= self::STATUS_DRAFT) + { @@ -2859,2 +2834,2 @@ - $sql .= ' SET fk_input_reason = '.((int) $demand_reason_id); - $sql .= ' WHERE rowid='.((int) $this->id); + $sql .= ' SET fk_input_reason = '.$demand_reason_id; + $sql .= ' WHERE rowid='.$this->id; @@ -2864 +2839,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2869 +2845,2 @@ - if (!$error) { + if (!$error) + { @@ -2874 +2851,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2877,3 +2855 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2883 +2859,2 @@ - if (!$error) { + if (!$error) + { @@ -2886,2 +2863,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2894 +2874,3 @@ - } else { + } + else + { @@ -2910 +2892 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -2914,2 +2896,3 @@ - // phpcs:enable - if ($user->hasRight('commande', 'creer')) { + // phpcs:enable + if ($user->rights->commande->creer) + { @@ -2921,2 +2904,2 @@ - $sql .= ' ref_client = '.(empty($ref_client) ? 'NULL' : "'".$this->db->escape($ref_client)."'"); - $sql .= ' WHERE rowid = '.((int) $this->id); + $sql .= ' ref_client = '.(empty($ref_client) ? 'NULL' : '\''.$this->db->escape($ref_client).'\''); + $sql .= ' WHERE rowid = '.$this->id; @@ -2926 +2909,2 @@ - if (!$resql) { + if (!$resql) + { @@ -2931 +2915,2 @@ - if (!$error) { + if (!$error) + { @@ -2934,4 +2919,4 @@ - $this->ref_customer = $ref_client; - } - - if (!$notrigger && empty($error)) { + } + + if (!$notrigger && empty($error)) + { @@ -2940,3 +2925 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2945 +2928,2 @@ - if (!$error) { + if (!$error) + { @@ -2948,2 +2932,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -2956 +2943,3 @@ - } else { + } + else + { @@ -2965,2 +2954,2 @@ - * @param int $notrigger 1=Does not execute triggers, 0=execute triggers - * @return int Return integer <0 if KO, 0 if already billed, >0 if OK + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers + * @return int <0 if KO, >0 if OK @@ -2972,4 +2960,0 @@ - if ($this->billed) { - return 0; - } - @@ -2979 +2964 @@ - $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > '.self::STATUS_DRAFT; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT; @@ -2982,2 +2967,4 @@ - if ($this->db->query($sql)) { - if (!$error) { + if ($this->db->query($sql)) + { + if (!$error) + { @@ -2988 +2975,2 @@ - if (!$notrigger && empty($error)) { + if (!$notrigger && empty($error)) + { @@ -2991,3 +2979 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -2997 +2983,2 @@ - if (!$error) { + if (!$error) + { @@ -3000,2 +2987,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -3008 +2998,3 @@ - } else { + } + else + { @@ -3018,6 +3010,5 @@ - * @param User $user Object user making the change - * @param int $notrigger 1=Does not execute triggers, 0=execute triggers - * @return int Return integer <0 if ko, >0 if ok - */ - public function classifyUnBilled(User $user, $notrigger = 0) - { + * @return int <0 if ko, >0 if ok + */ + public function classifyUnBilled() + { + global $conf, $user, $langs; @@ -3029 +3020 @@ - $sql .= " WHERE rowid = ".((int) $this->id).' AND fk_statut > '.self::STATUS_DRAFT; + $sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > '.self::STATUS_DRAFT; @@ -3032,2 +3023,4 @@ - if ($this->db->query($sql)) { - if (!$error) { + if ($this->db->query($sql)) + { + if (!$error) + { @@ -3038,10 +3031,7 @@ - if (!$notrigger && empty($error)) { - // Call trigger - $result = $this->call_trigger('ORDER_CLASSIFY_UNBILLED', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { + // Call trigger + $result = $this->call_trigger('ORDER_CLASSIFY_UNBILLED', $user); + if ($result < 0) $error++; + // End call triggers + + if (!$error) + { @@ -3052,2 +3042,5 @@ - } else { - foreach ($this->errors as $errmsg) { + } + else + { + foreach ($this->errors as $errmsg) + { @@ -3060 +3053,3 @@ - } else { + } + else + { @@ -3081,2 +3076,2 @@ - * @param int|string $date_start Start date of the line - * @param int|string $date_end End date of the line + * @param int $date_start Start date of the line + * @param int $date_end End date of the line @@ -3094,5 +3089,3 @@ - * @param string $ref_ext external reference - * @param integer $rang line rank - * @return int Return integer < 0 if KO, > 0 if OK - */ - public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $price_base_type = 'HT', $info_bits = 0, $date_start = '', $date_end = '', $type = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice = null, $pa_ht = 0, $label = '', $special_code = 0, $array_options = array(), $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext = '', $rang = 0) + * @return int < 0 if KO, > 0 if OK + */ + public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $price_base_type = 'HT', $info_bits = 0, $date_start = '', $date_end = '', $type = 0, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice = null, $pa_ht = 0, $label = '', $special_code = 0, $array_options = 0, $fk_unit = null, $pu_ht_devise = 0, $notrigger = 0) @@ -3102 +3095 @@ - dol_syslog(get_class($this)."::updateline id=$rowid, desc=$desc, pu=$pu, qty=$qty, remise_percent=$remise_percent, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, price_base_type=$price_base_type, info_bits=$info_bits, date_start=$date_start, date_end=$date_end, type=$type, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, special_code=$special_code, ref_ext=$ref_ext"); + dol_syslog(get_class($this)."::updateline id=$rowid, desc=$desc, pu=$pu, qty=$qty, remise_percent=$remise_percent, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, price_base_type=$price_base_type, info_bits=$info_bits, date_start=$date_start, date_end=$date_end, type=$type, fk_parent_line=$fk_parent_line, pa_ht=$pa_ht, special_code=$special_code"); @@ -3105 +3098,2 @@ - if ($this->statut == Commande::STATUS_DRAFT) { + if ($this->statut == Commande::STATUS_DRAFT) + { @@ -3107,24 +3101,7 @@ - if (empty($qty)) { - $qty = 0; - } - if (empty($info_bits)) { - $info_bits = 0; - } - if (empty($txtva)) { - $txtva = 0; - } - if (empty($txlocaltax1)) { - $txlocaltax1 = 0; - } - if (empty($txlocaltax2)) { - $txlocaltax2 = 0; - } - if (empty($remise_percent)) { - $remise_percent = 0; - } - if (empty($special_code) || $special_code == 3) { - $special_code = 0; - } - if (empty($ref_ext)) { - $ref_ext = ''; - } + if (empty($qty)) $qty = 0; + if (empty($info_bits)) $info_bits = 0; + if (empty($txtva)) $txtva = 0; + if (empty($txlocaltax1)) $txlocaltax1 = 0; + if (empty($txlocaltax2)) $txlocaltax2 = 0; + if (empty($remise_percent)) $remise_percent = 0; + if (empty($special_code) || $special_code == 3) $special_code = 0; @@ -3143,3 +3120 @@ - if (!preg_match('/\((.*)\)/', $txtva)) { - $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5' - } + $txtva = price2num($txtva); @@ -3160,2 +3135,2 @@ - $reg = array(); - if (preg_match('/\((.*)\)/', $txtva, $reg)) { + if (preg_match('/\((.*)\)/', $txtva, $reg)) + { @@ -3168 +3143 @@ - $total_ht = $tabprice[0]; + $total_ht = $tabprice[0]; @@ -3173 +3148 @@ - $pu_ht = $tabprice[3]; + $pu_ht = $tabprice[3]; @@ -3178 +3153 @@ - $multicurrency_total_ht = $tabprice[16]; + $multicurrency_total_ht = $tabprice[16]; @@ -3185 +3160,2 @@ - if ($price_base_type == 'TTC') { + if ($price_base_type == 'TTC') + { @@ -3187 +3163,3 @@ - } else { + } + else + { @@ -3191 +3169,2 @@ - if ($remise_percent > 0) { + if ($remise_percent > 0) + { @@ -3201 +3180,2 @@ - if (!empty($line->fk_product)) { + if (!empty($line->fk_product)) + { @@ -3206 +3186,2 @@ - if (getDolGlobalString('STOCK_MUST_BE_ENOUGH_FOR_ORDER') && $product_type == 0 && $product->stock_reel < $qty) { + if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER) && $product_type == 0 && $product->stock_reel < $qty) + { @@ -3210 +3190,0 @@ - @@ -3212 +3191,0 @@ - @@ -3223 +3201,0 @@ - $this->line->rang = $rang; @@ -3226 +3204,2 @@ - if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) { + if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) + { @@ -3235 +3213,0 @@ - $this->line->ref_ext = $ref_ext; @@ -3241,2 +3219,2 @@ - $this->line->localtax1_type = empty($localtaxes_type[0]) ? '' : $localtaxes_type[0]; - $this->line->localtax2_type = empty($localtaxes_type[2]) ? '' : $localtaxes_type[2]; + $this->line->localtax1_type = $localtaxes_type[0]; + $this->line->localtax2_type = $localtaxes_type[2]; @@ -3269,0 +3248 @@ + $this->line->remise = $remise; @@ -3279 +3258,2 @@ - if ($result > 0) { + if ($result > 0) + { @@ -3281,3 +3261 @@ - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } + if (!empty($fk_parent_line)) $this->line_order(true, 'DESC'); @@ -3286 +3264 @@ - $this->update_price(1, 'auto'); + $this->update_price(1); @@ -3290 +3268,3 @@ - } else { + } + else + { @@ -3296 +3276,3 @@ - } else { + } + else + { @@ -3308 +3290 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -3317,22 +3299,6 @@ - if (isset($this->ref)) { - $this->ref = trim($this->ref); - } - if (isset($this->ref_client)) { - $this->ref_client = trim($this->ref_client); - } - if (isset($this->ref_customer)) { - $this->ref_customer = trim($this->ref_customer); - } - if (isset($this->note) || isset($this->note_private)) { - $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note)); - } - if (isset($this->note_public)) { - $this->note_public = trim($this->note_public); - } - if (isset($this->model_pdf)) { - $this->model_pdf = trim($this->model_pdf); - } - if (isset($this->import_key)) { - $this->import_key = trim($this->import_key); - } - $delivery_date = $this->delivery_date; + if (isset($this->ref)) $this->ref = trim($this->ref); + if (isset($this->ref_client)) $this->ref_client = trim($this->ref_client); + if (isset($this->note) || isset($this->note_private)) $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note)); + if (isset($this->note_public)) $this->note_public = trim($this->note_public); + if (isset($this->modelpdf)) $this->modelpdf = trim($this->modelpdf); + if (isset($this->import_key)) $this->import_key = trim($this->import_key); @@ -3352 +3318 @@ - $sql .= " total_tva=".(isset($this->total_tva) ? $this->total_tva : "null").","; + $sql .= " tva=".(isset($this->total_tva) ? $this->total_tva : "null").","; @@ -3358,2 +3324,2 @@ - $sql .= " fk_user_modif=".(isset($user->id) ? $user->id : "null").","; - $sql .= " fk_user_valid=".((isset($this->user_validation_id) && $this->user_validation_id > 0) ? $this->user_validation_id : "null").","; + $sql .= " fk_user_author=".(isset($this->user_author_id) ? $this->user_author_id : "null").","; + $sql .= " fk_user_valid=".(isset($this->user_valid) ? $this->user_valid : "null").","; @@ -3362 +3327,0 @@ - $sql .= " deposit_percent=".(!empty($this->deposit_percent) ? strval($this->deposit_percent) : "null").","; @@ -3364,2 +3328,0 @@ - $sql .= " date_livraison=".(strval($this->delivery_date) != '' ? "'".$this->db->idate($this->delivery_date)."'" : 'null').","; - $sql .= " fk_shipping_method=".(isset($this->shipping_method_id) ? $this->shipping_method_id : "null").","; @@ -3367 +3329,0 @@ - $sql .= " fk_input_reason=".($this->demand_reason_id > 0 ? $this->demand_reason_id : "null").","; @@ -3370,4 +3332,4 @@ - $sql .= " model_pdf=".(isset($this->model_pdf) ? "'".$this->db->escape($this->model_pdf)."'" : "null").","; - $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null"); - - $sql .= " WHERE rowid=".((int) $this->id); + $sql .= " model_pdf=".(isset($this->modelpdf) ? "'".$this->db->escape($this->modelpdf)."'" : "null").","; + $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null").""; + + $sql .= " WHERE rowid=".$this->id; @@ -3380,5 +3342,5 @@ - $error++; - $this->errors[] = "Error ".$this->db->lasterror(); - } - - if (!$error) { + $error++; $this->errors[] = "Error ".$this->db->lasterror(); + } + + if (!$error) + { @@ -3386 +3348,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -3391 +3354,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -3394,3 +3358 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -3401,2 +3363,4 @@ - if ($error) { - foreach ($this->errors as $errmsg) { + if ($error) + { + foreach ($this->errors as $errmsg) + { @@ -3408 +3372,3 @@ - } else { + } + else + { @@ -3415 +3381 @@ - * Delete the sales order + * Delete the customer order @@ -3419 +3385 @@ - * @return int Return integer <=0 if KO, >0 if OK + * @return int <=0 if KO, >0 if OK @@ -3432 +3398,2 @@ - if (!$notrigger) { + if (!$error && !$notrigger) + { @@ -3435,3 +3402 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -3441,2 +3406,2 @@ - // Test we can delete - if ($this->countNbOfShipments() != 0) { + if ($this->nb_expedition() != 0) + { @@ -3447,6 +3412,8 @@ - // Delete extrafields of lines and lines - if (!$error && !empty($this->table_element_line)) { - $tabletodelete = $this->table_element_line; - $sqlef = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete."_extrafields WHERE fk_object IN (SELECT rowid FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id).")"; - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$tabletodelete." WHERE ".$this->fk_element." = ".((int) $this->id); - if (!$this->db->query($sqlef) || !$this->db->query($sql)) { + if (!$error) + { + // Delete extrafields of order details + $main = MAIN_DB_PREFIX.'commandedet'; + $ef = $main."_extrafields"; + $sql = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM $main WHERE fk_commande = ".$this->id.")"; + if (!$this->db->query($sql)) + { @@ -3454,7 +3421,17 @@ - $this->error = $this->db->lasterror(); - $this->errors[] = $this->error; - dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); - } - } - - if (!$error) { + $this->errors[] = $this->db->lasterror(); + } + } + + if (!$error) + { + // Delete order details + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE fk_commande = ".$this->id; + if (!$this->db->query($sql)) + { + $error++; + $this->errors[] = $this->db->lasterror(); + } + } + + if (!$error) + { @@ -3463,6 +3440,5 @@ - if ($res < 0) { - $error++; - } - } - - if (!$error) { + if ($res < 0) $error++; + } + + if (!$error) + { @@ -3471 +3447,9 @@ - if ($res < 0) { + if ($res < 0) $error++; + } + + if (!$error) + { + // Remove extrafields + $result = $this->deleteExtraFields(); + if ($result < 0) + { @@ -3473,7 +3457,10 @@ - } - } - - // Removed extrafields of object - if (!$error) { - $result = $this->deleteExtraFields(); - if ($result < 0) { + dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR); + } + } + + if (!$error) + { + // Delete object + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commande WHERE rowid = ".$this->id; + if (!$this->db->query($sql)) + { @@ -3481,32 +3468,14 @@ - dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); - } - } - - // Delete main record - if (!$error) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id); - $res = $this->db->query($sql); - if (!$res) { - $error++; - $this->error = $this->db->lasterror(); - $this->errors[] = $this->error; - dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR); - } - } - - // Delete record into ECM index and physically - if (!$error) { - $res = $this->deleteEcmFiles(0); // Deleting files physically is done later with the dol_delete_dir_recursive - $res = $this->deleteEcmFiles(1); // Deleting files physically is done later with the dol_delete_dir_recursive - if (!$res) { - $error++; - } - } - - if (!$error) { - // We remove directory - $ref = dol_sanitizeFileName($this->ref); - if ($conf->commande->multidir_output[$this->entity] && !empty($this->ref)) { - $dir = $conf->commande->multidir_output[$this->entity]."/".$ref; - $file = $dir."/".$ref.".pdf"; - if (file_exists($file)) { + $this->errors[] = $this->db->lasterror(); + } + } + + if (!$error) + { + // Remove directory with files + $comref = dol_sanitizeFileName($this->ref); + if ($conf->commande->multidir_output[$this->entity] && !empty($this->ref)) + { + $dir = $conf->commande->multidir_output[$this->entity]."/".$comref; + $file = $conf->commande->multidir_output[$this->entity]."/".$comref."/".$comref.".pdf"; + if (file_exists($file)) // We must delete all files before deleting directory + { @@ -3515,3 +3484,2 @@ - if (!dol_delete_file($file, 0, 0, 0, $this)) { - $this->error = 'ErrorFailToDeleteFile'; - $this->errors[] = $this->error; + if (!dol_delete_file($file, 0, 0, 0, $this)) // For triggers + { @@ -3522,5 +3490,5 @@ - if (file_exists($dir)) { - $res = @dol_delete_dir_recursive($dir); - if (!$res) { - $this->error = 'ErrorFailToDeleteDir'; - $this->errors[] = $this->error; + if (file_exists($dir)) + { + if (!dol_delete_dir_recursive($dir)) + { + $this->error = $langs->trans("ErrorCanNotDeleteDir", $dir); @@ -3534,2 +3502,2 @@ - if (!$error) { - dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG); + if (!$error) + { @@ -3538 +3506,7 @@ - } else { + } + else + { + foreach ($this->errors as $errmsg) + { + $this->error .= ($this->error ? ', '.$errmsg : $errmsg); + } @@ -3540 +3514 @@ - return -1; + return -1 * $error; @@ -3550,6 +3524,5 @@ - * @param string $mode Mode ('toship', 'tobill', 'shippedtobill') - * @return WorkboardResponse|int Return integer <0 if KO, WorkboardResponse if OK - */ - public function load_board($user, $mode) - { - // phpcs:enable + * @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK + */ + public function load_board($user) + { + // phpcs:enable @@ -3562 +3535,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) { + if (!$user->rights->societe->client->voir && !$user->socid) + { @@ -3564 +3538 @@ - $sql .= " WHERE sc.fk_user = ".((int) $user->id); + $sql .= " WHERE sc.fk_user = ".$user->id; @@ -3569,15 +3543,2 @@ - if ($mode == 'toship') { - // An order to ship is an open order (validated or in progress) - $sql .= " AND c.fk_statut IN (" . self::STATUS_VALIDATED . "," . self::STATUS_SHIPMENTONPROCESS . ")"; - } - if ($mode == 'tobill') { - // An order to bill is an order not already billed - $sql .= " AND c.fk_statut IN (" . self::STATUS_VALIDATED . "," . self::STATUS_SHIPMENTONPROCESS . ", " . self::STATUS_CLOSED . ") AND c.facture = 0"; - } - if ($mode == 'shippedtobill') { - // An order shipped and to bill is a delivered order not already billed - $sql .= " AND c.fk_statut IN (" . self::STATUS_CLOSED . ") AND c.facture = 0"; - } - if ($user->socid) { - $sql .= " AND c.fk_soc = ".((int) $user->socid); - } + $sql .= " AND ((c.fk_statut IN (".self::STATUS_VALIDATED.",".self::STATUS_SHIPMENTONPROCESS.")) OR (c.fk_statut = ".self::STATUS_CLOSED." AND c.facture = 0))"; // If status is 2 and facture=1, it must be selected + if ($user->socid) $sql .= " AND c.fk_soc = ".$user->socid; @@ -3586,20 +3547,2 @@ - if ($resql) { - $delay_warning = 0; - $label = $labelShort = $url = ''; - if ($mode == 'toship') { - $delay_warning = $conf->commande->client->warning_delay / 60 / 60 / 24; - $url = DOL_URL_ROOT.'/commande/list.php?search_status=-2&mainmenu=commercial&leftmenu=orders'; - $label = $langs->transnoentitiesnoconv("OrdersToProcess"); - $labelShort = $langs->transnoentitiesnoconv("Opened"); - } - if ($mode == 'tobill') { - $url = DOL_URL_ROOT.'/commande/list.php?search_status=-3&search_billed=0&mainmenu=commercial&leftmenu=orders'; - $label = $langs->trans("OrdersToBill"); // We set here bill but may be billed or ordered - $labelShort = $langs->trans("ToBill"); - } - if ($mode == 'shippedtobill') { - $url = DOL_URL_ROOT.'/commande/list.php?search_status=3&search_billed=0&mainmenu=commercial&leftmenu=orders'; - $label = $langs->trans("OrdersToBill"); // We set here bill but may be billed or ordered - $labelShort = $langs->trans("StatusOrderDelivered").' '.$langs->trans("and").' '.$langs->trans("ToBill"); - } - + if ($resql) + { @@ -3607,4 +3550,4 @@ - $response->warning_delay = $delay_warning; - $response->label = $label; - $response->labelShort = $labelShort; - $response->url = $url; + $response->warning_delay = $conf->commande->client->warning_delay / 60 / 60 / 24; + $response->label = $langs->trans("OrdersToProcess"); + $response->labelShort = $langs->trans("Opened"); + $response->url = DOL_URL_ROOT.'/commande/list.php?search_status=-3&mainmenu=commercial&leftmenu=orders'; @@ -3615 +3558,2 @@ - while ($obj = $this->db->fetch_object($resql)) { + while ($obj = $this->db->fetch_object($resql)) + { @@ -3621,4 +3565,3 @@ - $generic_commande->date = $this->db->jdate($obj->date_commande); - $generic_commande->delivery_date = $this->db->jdate($obj->delivery_date); - - if ($mode == 'toship' && $generic_commande->hasDelay()) { + $generic_commande->date_livraison = $this->db->jdate($obj->delivery_date); + + if ($generic_commande->hasDelay()) { @@ -3630 +3573,3 @@ - } else { + } + else + { @@ -3647,3 +3592 @@ - if ($label == 'OrderSource') { - return ''; - } + if ($label == 'OrderSource') return ''; @@ -3676,2 +3619,2 @@ - // phpcs:enable - global $langs, $hookmanager; + // phpcs:enable + global $langs, $conf; @@ -3680,5 +3623 @@ - if (empty($donotshowbilled)) { - $billedtext .= ($billed ? ' - '.$langs->transnoentitiesnoconv("Billed") : ''); - } - - $labelTooltip = ''; + if (empty($donotshowbilled)) $billedtext .= ($billed ? ' - '.$langs->trans("Billed") : ''); @@ -3687,106 +3626,44 @@ - $labelStatus = $langs->transnoentitiesnoconv('StatusOrderCanceled'); - $labelStatusShort = $langs->transnoentitiesnoconv('StatusOrderCanceledShort'); - $statusType = 'status9'; - } elseif ($status == self::STATUS_DRAFT) { - $labelStatus = $langs->transnoentitiesnoconv('StatusOrderDraft'); - $labelStatusShort = $langs->transnoentitiesnoconv('StatusOrderDraftShort'); - $statusType = 'status0'; - } elseif ($status == self::STATUS_VALIDATED) { - $labelStatus = $langs->transnoentitiesnoconv('StatusOrderValidated').$billedtext; - $labelStatusShort = $langs->transnoentitiesnoconv('StatusOrderValidatedShort').$billedtext; - $statusType = 'status1'; - } elseif ($status == self::STATUS_SHIPMENTONPROCESS) { - $labelStatus = $langs->transnoentitiesnoconv('StatusOrderSent').$billedtext; - $labelStatusShort = $langs->transnoentitiesnoconv('StatusOrderSentShort').$billedtext; - $labelTooltip = $langs->transnoentitiesnoconv("StatusOrderSent"); - if (!empty($this->delivery_date)) { - $labelTooltip .= ' - '.$langs->transnoentitiesnoconv("DateDeliveryPlanned").dol_print_date($this->delivery_date, 'day').$billedtext; - } - $statusType = 'status4'; - } elseif ($status == self::STATUS_CLOSED) { - $labelStatus = $langs->transnoentitiesnoconv('StatusOrderDelivered').$billedtext; - $labelStatusShort = $langs->transnoentitiesnoconv('StatusOrderDeliveredShort').$billedtext; - $statusType = 'status6'; - } else { - $labelStatus = $langs->transnoentitiesnoconv('Unknown'); - $labelStatusShort = ''; - $statusType = ''; - $mode = 0; - } - - $parameters = array( - 'status' => $status, - 'mode' => $mode, - 'billed' => $billed, - 'donotshowbilled' => $donotshowbilled - ); - - $reshook = $hookmanager->executeHooks('LibStatut', $parameters, $this); // Note that $action and $object may have been modified by hook - - if ($reshook > 0) { - return $hookmanager->resPrint; - } - - return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode, '', array('tooltip' => $labelTooltip)); - } - - /** - * getTooltipContentArray - * - * @param array $params params to construct tooltip data - * @return array - */ - public function getTooltipContentArray($params) - { - global $conf, $langs, $user; - - $langs->load('orders'); - $datas = []; - $nofetch = !empty($params['nofetch']); - - if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { - return ['optimize' => $langs->trans("Order")]; - } - - if ($user->hasRight('commande', 'lire')) { - $datas['picto'] = img_picto('', $this->picto).' '.$langs->trans("Order").''; - if (isset($this->statut)) { - $datas['status'] = ' '.$this->getLibStatut(5); - } - $datas['Ref'] = '
'.$langs->trans('Ref').': '.$this->ref; - if (!$nofetch) { - $langs->load('companies'); - if (empty($this->thirdparty)) { - $this->fetch_thirdparty(); - } - $datas['customer'] = '
'.$langs->trans('Customer').': '.$this->thirdparty->getNomUrl(1, '', 0, 1); - } - $datas['RefCustomer'] = '
'.$langs->trans('RefCustomer').': '.(empty($this->ref_customer) ? (empty($this->ref_client) ? '' : $this->ref_client) : $this->ref_customer); - if (!$nofetch) { - $langs->load('project'); - if (empty($this->project)) { - $res = $this->fetch_project(); - if ($res > 0) { - $datas['project'] = '
'.$langs->trans('Project').': '.$this->project->getNomUrl(1, '', 0, 1); - } - } - } - if (!empty($this->total_ht)) { - $datas['AmountHT'] = '
'.$langs->trans('AmountHT').': '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency); - } - if (!empty($this->total_tva)) { - $datas['VAT'] = '
'.$langs->trans('VAT').': '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency); - } - if (!empty($this->total_ttc)) { - $datas['AmountTTC'] = '
'.$langs->trans('AmountTTC').': '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency); - } - if (!empty($this->date)) { - $datas['Date'] = '
'.$langs->trans('Date').': '.dol_print_date($this->date, 'day'); - } - if (!empty($this->delivery_date)) { - $datas['DeliveryDate'] = '
'.$langs->trans('DeliveryDate').': '.dol_print_date($this->delivery_date, 'dayhour'); - } - } - - return $datas; - } + $labelStatus = $langs->trans('StatusOrderCanceled'); + $labelStatusShort = $langs->trans('StatusOrderCanceledShort'); + $statusType = 'status9'; + } + elseif ($status == self::STATUS_DRAFT) { + $labelStatus = $langs->trans('StatusOrderDraft'); + $labelStatusShort = $langs->trans('StatusOrderDraftShort'); + $statusType = 'status0'; + } + elseif ($status == self::STATUS_VALIDATED) { + $labelStatus = $langs->trans('StatusOrderValidated').$billedtext; + $labelStatusShort = $langs->trans('StatusOrderValidatedShort').$billedtext; + $statusType = 'status1'; + } + elseif ($status == self::STATUS_SHIPMENTONPROCESS) { + $labelStatus = $langs->trans('StatusOrderSentShort').$billedtext; + $labelStatusShort = $langs->trans('StatusOrderSentShort').$billedtext; + $statusType = 'status4'; + } + elseif ($status == self::STATUS_CLOSED && (!$billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) { + $labelStatus = $langs->trans('StatusOrderToBill'); + $labelStatusShort = $langs->trans('StatusOrderToBillShort'); + $statusType = 'status4'; + } + elseif ($status == self::STATUS_CLOSED && ($billed && empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) { + $labelStatus = $langs->trans('StatusOrderProcessed').$billedtext; + $labelStatusShort = $langs->trans('StatusOrderProcessed').$billedtext; + $statusType = 'status6'; + } + elseif ($status == self::STATUS_CLOSED && (!empty($conf->global->WORKFLOW_BILL_ON_SHIPMENT))) { + $labelStatus = $langs->trans('StatusOrderDelivered'); + $labelStatusShort = $langs->trans('StatusOrderDelivered'); + $statusType = 'status6'; + } + else { + $labelStatus = $langs->trans('Unknown'); + $labelStatusShort = ''; + $statusType = ''; + $mode = 0; + } + + return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode); + } + @@ -3804 +3680,0 @@ - * @param string $target attribute target for link @@ -3807,7 +3683,5 @@ - public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0, $target = '') - { - global $conf, $langs, $user, $hookmanager; - - if (!empty($conf->dol_no_mouse_hover)) { - $notooltip = 1; // Force disable tooltips - } + public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $notooltip = 0, $save_lastsearch_value = -1, $addlinktonotes = 0) + { + global $conf, $langs, $user; + + if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips @@ -3817,7 +3691,4 @@ - if (isModEnabled("expedition") && ($option == '1' || $option == '2')) { - $url = DOL_URL_ROOT.'/expedition/shipment.php?id='.$this->id; - } else { - $url = DOL_URL_ROOT.'/commande/card.php?id='.$this->id; - } - - if (!$user->hasRight('commande', 'lire')) { + if (!empty($conf->expedition->enabled) && ($option == '1' || $option == '2')) $url = DOL_URL_ROOT.'/expedition/shipment.php?id='.$this->id; + else $url = DOL_URL_ROOT.'/commande/card.php?id='.$this->id; + + if (!$user->rights->commande->lire) @@ -3825,3 +3696,3 @@ - } - - if ($option !== 'nolink') { + + if ($option !== 'nolink') + { @@ -3830,25 +3701,24 @@ - if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { - $add_save_lastsearch_values = 1; - } - if ($add_save_lastsearch_values) { - $url .= '&save_lastsearch_values=1'; - } - } - - if ($short) { - return $url; - } - $params = [ - 'id' => $this->id, - 'objecttype' => $this->element, - 'option' => $option, - 'nofetch' => 1, - ]; - $classfortooltip = 'classfortooltip'; - $dataparams = ''; - if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { - $classfortooltip = 'classforajaxtooltip'; - $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"'; - $label = ''; - } else { - $label = implode($this->getTooltipContentArray($params)); + if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1; + if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1'; + } + + if ($short) return $url; + + $label = ''; + + if ($user->rights->commande->lire) { + $label = ''.$langs->trans("Order").''; + $label .= '
'.$langs->trans('Ref').': '.$this->ref; + $label .= '
'.$langs->trans('RefCustomer').': '.($this->ref_customer ? $this->ref_customer : $this->ref_client); + if (!empty($this->total_ht)) { + $label .= '
'.$langs->trans('AmountHT').': '.price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency); + } + if (!empty($this->total_tva)) { + $label .= '
'.$langs->trans('VAT').': '.price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency); + } + if (!empty($this->total_ttc)) { + $label .= '
'.$langs->trans('AmountTTC').': '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency); + } + if (isset($this->statut)) { + $label .= '
'.$langs->trans("Status").": ".$this->getLibStatut(5); + } @@ -3858,2 +3728,4 @@ - if (empty($notooltip) && $user->hasRight('commande', 'lire')) { - if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { + if (empty($notooltip) && $user->rights->commande->lire) + { + if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) + { @@ -3863,7 +3735,2 @@ - $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"'); - $linkclose .= $dataparams.' class="'.$classfortooltip.'"'; - - $target_value = array('_self', '_blank', '_parent', '_top'); - if (in_array($target, $target_value)) { - $linkclose .= ' target="'.dol_escape_htmltag($target).'"'; - } + $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"'; + $linkclose .= ' class="classfortooltip"'; @@ -3882,6 +3749,2 @@ - if ($withpicto) { - $result .= img_object(($notooltip ? '' : $label), $this->picto, (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1); - } - if ($withpicto != 2) { - $result .= $this->ref; - } + if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); + if ($withpicto != 2) $result .= $this->ref; @@ -3890 +3753,2 @@ - if ($addlinktonotes) { + if ($addlinktonotes) + { @@ -3892 +3756,2 @@ - if ($txttoshow) { + if ($txttoshow) + { @@ -3904,9 +3768,0 @@ - global $action; - $hookmanager->initHooks(array($this->element . 'dao')); - $parameters = array('id'=>$this->id, 'getnomurl' => &$result); - $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks - if ($reshook > 0) { - $result = $hookmanager->resPrint; - } else { - $result .= $hookmanager->resPrint; - } @@ -3930 +3786 @@ - $sql .= ' WHERE c.rowid = '.((int) $id); + $sql .= ' WHERE c.rowid = '.$id; @@ -3932,2 +3788,4 @@ - if ($result) { - if ($this->db->num_rows($result)) { + if ($result) + { + if ($this->db->num_rows($result)) + { @@ -3936,2 +3794,5 @@ - if ($obj->fk_user_author) { - $this->user_creation_id = $obj->fk_user_author; + if ($obj->fk_user_author) + { + $cuser = new User($this->db); + $cuser->fetch($obj->fk_user_author); + $this->user_creation = $cuser; @@ -3939,2 +3800,6 @@ - if ($obj->fk_user_valid) { - $this->user_validation_id = $obj->fk_user_valid; + + if ($obj->fk_user_valid) + { + $vuser = new User($this->db); + $vuser->fetch($obj->fk_user_valid); + $this->user_validation = $vuser; @@ -3942,2 +3807,6 @@ - if ($obj->fk_user_cloture) { - $this->user_closing_id = $obj->fk_user_cloture; + + if ($obj->fk_user_cloture) + { + $cluser = new User($this->db); + $cluser->fetch($obj->fk_user_cloture); + $this->user_cloture = $cluser; @@ -3953 +3822,3 @@ - } else { + } + else + { @@ -3978,2 +3848,0 @@ - $sql .= $this->db->plimit(100); - @@ -3981 +3850,2 @@ - if ($resql) { + if ($resql) + { @@ -3984 +3854,2 @@ - while ($i < $num_prods) { + while ($i < $num_prods) + { @@ -3995 +3865,0 @@ - $this->entity = $conf->entity; @@ -4013 +3883,2 @@ - while ($xnbp < $nbp) { + while ($xnbp < $nbp) + { @@ -4021 +3892,2 @@ - if ($xnbp == 2) { + if ($xnbp == 2) + { @@ -4026 +3898,3 @@ - } else { + } + else + { @@ -4032 +3906,2 @@ - if ($num_prods > 0) { + if ($num_prods > 0) + { @@ -4053 +3928 @@ - * @return int Return integer <0 si ko, >0 si ok + * @return int <0 si ko, >0 si ok @@ -4057 +3932 @@ - // phpcs:enable + // phpcs:enable @@ -4066 +3941,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) { + if (!$user->rights->societe->client->voir && !$user->socid) + { @@ -4068 +3944 @@ - $sql .= " WHERE sc.fk_user = ".((int) $user->id); + $sql .= " WHERE sc.fk_user = ".$user->id; @@ -4074,2 +3950,4 @@ - if ($resql) { - while ($obj = $this->db->fetch_object($resql)) { + if ($resql) + { + while ($obj = $this->db->fetch_object($resql)) + { @@ -4080 +3958,3 @@ - } else { + } + else + { @@ -4113 +3992,0 @@ - $outputlangs->load("products"); @@ -4118,3 +3997,3 @@ - if (!empty($this->model_pdf)) { - $modele = $this->model_pdf; - } elseif (getDolGlobalString('COMMANDE_ADDON_PDF')) { + if ($this->modelpdf) { + $modele = $this->modelpdf; + } elseif (!empty($conf->global->COMMANDE_ADDON_PDF)) { @@ -4134,6 +4013,6 @@ - * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test - * @param int $origin_id Old thirdparty id - * @param int $dest_id New thirdparty id - * @return bool - */ - public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id) + * @param DoliDB $db Database handler + * @param int $origin_id Old thirdparty id + * @param int $dest_id New thirdparty id + * @return bool + */ + public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id) @@ -4145,22 +4024,5 @@ - return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables); - } - - /** - * Function used to replace a product id with another one. - * - * @param DoliDB $db Database handler - * @param int $origin_id Old product id - * @param int $dest_id New product id - * @return bool - */ - public static function replaceProduct(DoliDB $db, $origin_id, $dest_id) - { - $tables = array( - 'commandedet', - ); - - return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables); - } - - /** - * Is the sales order delayed? + return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); + } + + /** + * Is the customer order delayed? @@ -4180 +4042 @@ - return max($this->date, $this->delivery_date) < ($now - $conf->commande->client->warning_delay); + return max($this->date_commande, $this->date_livraison) < ($now - $conf->commande->client->warning_delay); @@ -4192,5 +4054,2 @@ - if (empty($this->delivery_date)) { - $text = $langs->trans("OrderDate").' '.dol_print_date($this->date, 'day'); - } else { - $text = $text = $langs->trans("DeliveryDate").' '.dol_print_date($this->delivery_date, 'day'); - } + if (empty($this->date_livraison)) $text = $langs->trans("OrderDate").' '.dol_print_date($this->date_commande, 'day'); + else $text = $text = $langs->trans("DeliveryDate").' '.dol_print_date($this->date_livraison, 'day'); @@ -4231,0 +4091 @@ + // From llx_commandedet @@ -4233,4 +4092,0 @@ - - /** - * @var int Id of invoice - */ @@ -4240,3 +4096,3 @@ - * @var string External ref - */ - public $ref_ext; + * @var string Order lines label + */ + public $label; @@ -4245,4 +4100,0 @@ - - /** - * @var int line rank - */ @@ -4278,4 +4130,4 @@ - public function __construct($db) - { - $this->db = $db; - } + public function __construct($db) + { + $this->db = $db; + } @@ -4287 +4139 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -4292 +4144 @@ - $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,'; + $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice,'; @@ -4296,2 +4148,2 @@ - $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,'; - $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code'; + $sql .= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc, p.tobatch as product_tobatch,'; + $sql .= ' cd.date_start, cd.date_end'; @@ -4300 +4152 @@ - $sql .= ' WHERE cd.rowid = '.((int) $rowid); + $sql .= ' WHERE cd.rowid = '.$rowid; @@ -4302 +4154,2 @@ - if ($result) { + if ($result) + { @@ -4304,6 +4156,0 @@ - - if (!$objp) { - $this->error = 'OrderLine with id '. $rowid .' not found sql='.$sql; - return 0; - } - @@ -4319 +4165,0 @@ - $this->ref_ext = $objp->ref_ext; @@ -4345,3 +4191,3 @@ - - $this->product_ref = $objp->product_ref; - $this->product_label = $objp->product_label; + $this->product_ref = $objp->product_ref; + $this->libelle = $objp->product_libelle; // deprecated + $this->product_label = $objp->product_libelle; @@ -4362,2 +4207,0 @@ - $this->fetch_optionals(); - @@ -4367 +4211,3 @@ - } else { + } + else + { @@ -4378 +4224 @@ - * @return int Return integer <0 si ko, >0 si ok + * @return int <0 si ko, >0 si ok @@ -4386,13 +4232,70 @@ - if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility - $this->id = $this->rowid; - } - - // check if order line is not in a shipment line before deleting - $sqlCheckShipmentLine = "SELECT"; - $sqlCheckShipmentLine .= " ed.rowid"; - $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed"; - $sqlCheckShipmentLine .= " WHERE ed.fk_origin_line = " . ((int) $this->id); - - $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine); - if (!$resqlCheckShipmentLine) { - $error++; + // check if order line is not in a shipment line before deleting + $sqlCheckShipmentLine = "SELECT"; + $sqlCheckShipmentLine .= " ed.rowid"; + $sqlCheckShipmentLine .= " FROM ".MAIN_DB_PREFIX."expeditiondet ed"; + $sqlCheckShipmentLine .= " WHERE ed.fk_origin_line = ".$this->rowid; + + $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine); + if (!$resqlCheckShipmentLine) { + $error++; + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; + } else { + $langs->load('errors'); + $num = $this->db->num_rows($resqlCheckShipmentLine); + if ($num > 0) { + $error++; + $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine); + $this->error = $langs->trans('ErrorRecordAlreadyExists').' : '.$langs->trans('ShipmentLine').' '.$objCheckShipmentLine->rowid; + $this->errors[] = $this->error; + } + $this->db->free($resqlCheckShipmentLine); + } + if ($error) { + dol_syslog(__METHOD__.'Error ; '.$this->error, LOG_ERR); + return -1; + } + + $this->db->begin(); + + $sql = 'DELETE FROM '.MAIN_DB_PREFIX."commandedet WHERE rowid=".$this->rowid; + + dol_syslog("OrderLine::delete", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + // Remove extrafields + if (!$error) + { + $this->id = $this->rowid; + $result = $this->deleteExtraFields(); + if ($result < 0) + { + $error++; + dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR); + } + } + + if (!$error && !$notrigger) + { + // Call trigger + $result = $this->call_trigger('LINEORDER_DELETE', $user); + if ($result < 0) $error++; + // End call triggers + } + + if (!$error) { + $this->db->commit(); + return 1; + } + + 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 + { @@ -4400,14 +4302,0 @@ - $this->errors[] = $this->error; - } else { - $langs->load('errors'); - $num = $this->db->num_rows($resqlCheckShipmentLine); - if ($num > 0) { - $error++; - $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine); - $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid; - $this->errors[] = $this->error; - } - $this->db->free($resqlCheckShipmentLine); - } - if ($error) { - dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR); @@ -4416,43 +4304,0 @@ - - $this->db->begin(); - - if (!$notrigger) { - // Call trigger - $result = $this->call_trigger('LINEORDER_DELETE', $user); - if ($result < 0) { - $error++; - } - // End call triggers - } - - if (!$error) { - $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id); - - dol_syslog("OrderLine::delete", LOG_DEBUG); - $resql = $this->db->query($sql); - if (!$resql) { - $this->error = $this->db->lasterror(); - $error++; - } - } - - // Remove extrafields - if (!$error) { - $result = $this->deleteExtraFields(); - if ($result < 0) { - $error++; - dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR); - } - } - - if (!$error) { - $this->db->commit(); - return 1; - } - - 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; @@ -4466 +4312 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -4469,0 +4316,2 @@ + global $langs, $conf; + @@ -4477,42 +4325,14 @@ - if (empty($this->tva_tx)) { - $this->tva_tx = 0; - } - if (empty($this->localtax1_tx)) { - $this->localtax1_tx = 0; - } - if (empty($this->localtax2_tx)) { - $this->localtax2_tx = 0; - } - if (empty($this->localtax1_type)) { - $this->localtax1_type = 0; - } - if (empty($this->localtax2_type)) { - $this->localtax2_type = 0; - } - if (empty($this->total_localtax1)) { - $this->total_localtax1 = 0; - } - if (empty($this->total_localtax2)) { - $this->total_localtax2 = 0; - } - if (empty($this->rang)) { - $this->rang = 0; - } - if (empty($this->remise_percent)) { - $this->remise_percent = 0; - } - if (empty($this->info_bits)) { - $this->info_bits = 0; - } - if (empty($this->special_code)) { - $this->special_code = 0; - } - if (empty($this->fk_parent_line)) { - $this->fk_parent_line = 0; - } - if (empty($this->pa_ht)) { - $this->pa_ht = 0; - } - if (empty($this->ref_ext)) { - $this->ref_ext = ''; - } + if (empty($this->tva_tx)) $this->tva_tx = 0; + if (empty($this->localtax1_tx)) $this->localtax1_tx = 0; + if (empty($this->localtax2_tx)) $this->localtax2_tx = 0; + if (empty($this->localtax1_type)) $this->localtax1_type = 0; + if (empty($this->localtax2_type)) $this->localtax2_type = 0; + if (empty($this->total_localtax1)) $this->total_localtax1 = 0; + if (empty($this->total_localtax2)) $this->total_localtax2 = 0; + if (empty($this->rang)) $this->rang = 0; + if (empty($this->remise)) $this->remise = 0; + if (empty($this->remise_percent)) $this->remise_percent = 0; + if (empty($this->info_bits)) $this->info_bits = 0; + if (empty($this->special_code)) $this->special_code = 0; + if (empty($this->fk_parent_line)) $this->fk_parent_line = 0; + if (empty($this->pa_ht)) $this->pa_ht = 0; @@ -4521,3 +4341,4 @@ - if ($this->pa_ht == 0 && $pa_ht_isemptystring) { - $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product); - if ($result < 0) { + if ($this->pa_ht == 0 && $pa_ht_isemptystring) + { + if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) + { @@ -4525 +4346,3 @@ - } else { + } + else + { @@ -4531,3 +4354 @@ - if ($this->product_type < 0) { - return -1; - } + if ($this->product_type < 0) return -1; @@ -4539 +4360 @@ - $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,'; + $sql .= ' (fk_commande, fk_parent_line, label, description, qty, '; @@ -4541 +4362 @@ - $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,'; + $sql .= ' fk_product, product_type, remise_percent, subprice, price, remise, fk_remise_except,'; @@ -4544,2 +4365,2 @@ - $sql .= ' fk_unit,'; - $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc'; + $sql .= ' fk_unit'; + $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc'; @@ -4552 +4372,0 @@ - $sql .= " '".$this->db->escape($this->ref_ext)."',"; @@ -4559 +4379 @@ - $sql .= ' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null").','; + $sql .= ' '.(!empty($this->fk_product) ? $this->fk_product : "null").','; @@ -4562 +4382 @@ - $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice) : "null").","; + $sql .= " ".(price2num($this->subprice) !== '' ?price2num($this->subprice) : "null").","; @@ -4563,0 +4384 @@ + $sql .= " '".price2num($this->remise)."',"; @@ -4565,2 +4386,2 @@ - $sql .= ' '.((int) $this->special_code).','; - $sql .= ' '.((int) $this->rang).','; + $sql .= ' '.$this->special_code.','; + $sql .= ' '.$this->rang.','; @@ -4569,6 +4390,6 @@ - $sql .= " ".((int) $this->info_bits).","; - $sql .= " ".price2num($this->total_ht, 'MT').","; - $sql .= " ".price2num($this->total_tva, 'MT').","; - $sql .= " ".price2num($this->total_localtax1, 'MT').","; - $sql .= " ".price2num($this->total_localtax2, 'MT').","; - $sql .= " ".price2num($this->total_ttc, 'MT').","; + $sql .= " '".$this->db->escape($this->info_bits)."',"; + $sql .= " ".price2num($this->total_ht).","; + $sql .= " ".price2num($this->total_tva).","; + $sql .= " ".price2num($this->total_localtax1).","; + $sql .= " ".price2num($this->total_localtax2).","; + $sql .= " ".price2num($this->total_ttc).","; @@ -4577,2 +4398,2 @@ - $sql .= ' '.(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit)); - $sql .= ", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL'); + $sql .= ' '.(!$this->fk_unit ? 'NULL' : $this->fk_unit); + $sql .= ", ".(!empty($this->fk_multicurrency) ? $this->fk_multicurrency : 'NULL'); @@ -4580,4 +4401,4 @@ - $sql .= ", ".price2num($this->multicurrency_subprice, 'CU'); - $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT'); - $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT'); - $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT'); + $sql .= ", ".$this->multicurrency_subprice; + $sql .= ", ".$this->multicurrency_total_ht; + $sql .= ", ".$this->multicurrency_total_tva; + $sql .= ", ".$this->multicurrency_total_ttc; @@ -4588 +4409,2 @@ - if ($resql) { + if ($resql) + { @@ -4592 +4414,2 @@ - if (!$error) { + if (!$error) + { @@ -4594 +4417,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -4599 +4423,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -4602,3 +4427 @@ - if ($result < 0) { - $error++; - } + if ($result < 0) $error++; @@ -4613 +4436,2 @@ - foreach ($this->errors as $errmsg) { + foreach ($this->errors as $errmsg) + { @@ -4619 +4443,3 @@ - } else { + } + else + { @@ -4631 +4457 @@ - * @return int Return integer <0 si ko, >0 si ok + * @return int <0 si ko, >0 si ok @@ -4634,0 +4461,2 @@ + global $conf, $langs; + @@ -4640,54 +4468,17 @@ - if (empty($this->tva_tx)) { - $this->tva_tx = 0; - } - if (empty($this->localtax1_tx)) { - $this->localtax1_tx = 0; - } - if (empty($this->localtax2_tx)) { - $this->localtax2_tx = 0; - } - if (empty($this->localtax1_type)) { - $this->localtax1_type = 0; - } - if (empty($this->localtax2_type)) { - $this->localtax2_type = 0; - } - if (empty($this->qty)) { - $this->qty = 0; - } - if (empty($this->total_localtax1)) { - $this->total_localtax1 = 0; - } - if (empty($this->total_localtax2)) { - $this->total_localtax2 = 0; - } - if (empty($this->marque_tx)) { - $this->marque_tx = 0; - } - if (empty($this->marge_tx)) { - $this->marge_tx = 0; - } - if (empty($this->remise_percent)) { - $this->remise_percent = 0; - } - if (empty($this->remise)) { - $this->remise = 0; - } - if (empty($this->info_bits)) { - $this->info_bits = 0; - } - if (empty($this->special_code)) { - $this->special_code = 0; - } - if (empty($this->product_type)) { - $this->product_type = 0; - } - if (empty($this->fk_parent_line)) { - $this->fk_parent_line = 0; - } - if (empty($this->pa_ht)) { - $this->pa_ht = 0; - } - if (empty($this->ref_ext)) { - $this->ref_ext = ''; - } + if (empty($this->tva_tx)) $this->tva_tx = 0; + if (empty($this->localtax1_tx)) $this->localtax1_tx = 0; + if (empty($this->localtax2_tx)) $this->localtax2_tx = 0; + if (empty($this->localtax1_type)) $this->localtax1_type = 0; + if (empty($this->localtax2_type)) $this->localtax2_type = 0; + if (empty($this->qty)) $this->qty = 0; + if (empty($this->total_localtax1)) $this->total_localtax1 = 0; + if (empty($this->total_localtax2)) $this->total_localtax2 = 0; + if (empty($this->marque_tx)) $this->marque_tx = 0; + if (empty($this->marge_tx)) $this->marge_tx = 0; + if (empty($this->remise)) $this->remise = 0; + if (empty($this->remise_percent)) $this->remise_percent = 0; + if (empty($this->info_bits)) $this->info_bits = 0; + if (empty($this->special_code)) $this->special_code = 0; + if (empty($this->product_type)) $this->product_type = 0; + if (empty($this->fk_parent_line)) $this->fk_parent_line = 0; + if (empty($this->pa_ht)) $this->pa_ht = 0; @@ -4696,3 +4487,4 @@ - if ($this->pa_ht == 0 && $pa_ht_isemptystring) { - $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product); - if ($result < 0) { + if ($this->pa_ht == 0 && $pa_ht_isemptystring) + { + if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) + { @@ -4700 +4492,3 @@ - } else { + } + else + { @@ -4718,9 +4512,9 @@ - $sql .= " , ref_ext='".$this->db->escape($this->ref_ext)."'"; - $sql .= " , subprice=".price2num($this->subprice); - $sql .= " , remise_percent=".price2num($this->remise_percent); - $sql .= " , price=".price2num($this->price); // TODO A virer - $sql .= " , remise=".price2num($this->remise); // TODO A virer - if (empty($this->skip_update_total)) { - $sql .= " , total_ht=".price2num($this->total_ht); - $sql .= " , total_tva=".price2num($this->total_tva); - $sql .= " , total_ttc=".price2num($this->total_ttc); + $sql .= " , subprice=".price2num($this->subprice).""; + $sql .= " , remise_percent=".price2num($this->remise_percent).""; + $sql .= " , price=".price2num($this->price).""; // TODO A virer + $sql .= " , remise=".price2num($this->remise).""; // TODO A virer + if (empty($this->skip_update_total)) + { + $sql .= " , total_ht=".price2num($this->total_ht).""; + $sql .= " , total_tva=".price2num($this->total_tva).""; + $sql .= " , total_ttc=".price2num($this->total_ttc).""; @@ -4732,2 +4526,2 @@ - $sql .= " , info_bits=".((int) $this->info_bits); - $sql .= " , special_code=".((int) $this->special_code); + $sql .= " , info_bits=".$this->info_bits; + $sql .= " , special_code=".$this->special_code; @@ -4738,3 +4532 @@ - if (!empty($this->rang)) { - $sql .= ", rang=".((int) $this->rang); - } + if (!empty($this->rang)) $sql .= ", rang=".$this->rang; @@ -4744,6 +4536,6 @@ - $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice); - $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht); - $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva); - $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc); - - $sql .= " WHERE rowid = ".((int) $this->rowid); + $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice).""; + $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht).""; + $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva).""; + $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc).""; + + $sql .= " WHERE rowid = ".$this->rowid; @@ -4753,2 +4545,4 @@ - if ($resql) { - if (!$error) { + if ($resql) + { + if (!$error) + { @@ -4757 +4551,2 @@ - if ($result < 0) { + if ($result < 0) + { @@ -4762 +4557,2 @@ - if (!$error && !$notrigger) { + if (!$error && !$notrigger) + { @@ -4764,4 +4560,2 @@ - $result = $this->call_trigger('LINEORDER_MODIFY', $user); - if ($result < 0) { - $error++; - } + $result = $this->call_trigger('LINEORDER_UPDATE', $user); + if ($result < 0) $error++; @@ -4776 +4570,2 @@ - foreach ($this->errors as $errmsg) { + foreach ($this->errors as $errmsg) + { @@ -4782 +4577,3 @@ - } else { + } + else + { @@ -4794 +4591 @@ - * @return int Return integer <0 if KO, >0 if OK + * @return int <0 if KO, >0 if OK @@ -4798 +4595 @@ - // phpcs:enable + // phpcs:enable @@ -4802,6 +4599,2 @@ - if (empty($this->total_localtax1)) { - $this->total_localtax1 = 0; - } - if (empty($this->total_localtax2)) { - $this->total_localtax2 = 0; - } + if (empty($this->total_localtax1)) $this->total_localtax1 = 0; + if (empty($this->total_localtax2)) $this->total_localtax2 = 0; @@ -4816 +4609 @@ - $sql .= " WHERE rowid = ".((int) $this->rowid); + $sql .= " WHERE rowid = ".$this->rowid; @@ -4821 +4614,2 @@ - if ($resql) { + if ($resql) + { @@ -4824 +4618,3 @@ - } else { + } + else + { --- /tmp/dsg/dolibarr/htdocs/commande/class/github_19.0.3_commandestats.class.php +++ /tmp/dsg/dolibarr/htdocs/commande/class/client_commandestats.class.php @@ -23,3 +23,3 @@ - * \file htdocs/commande/class/commandestats.class.php - * \ingroup commandes - * \brief File of class to manage order statistics + * \file htdocs/commande/class/commandestats.class.php + * \ingroup commandes + * \brief File of class to manage order statistics @@ -38,15 +38,12 @@ - /** - * @var string Name of table without prefix where object is stored - */ - public $table_element; - - public $socid; - public $userid; - - public $from; - public $from_line; - public $field; - public $field_line; - public $categ_link; - public $where; - public $join; + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element; + + public $socid; + public $userid; + + public $from; + public $field; + public $where; + public $join; @@ -62,2 +59,2 @@ - * @param int $typentid Id typent of thirdpary for filter - * @param int $categid Id category of thirdpary for filter + * @param int $typentid Id typent of thirdpary for filter + * @param int $categid Id category of thirdpary for filter @@ -72 +69 @@ - $this->userid = $userid; + $this->userid = $userid; @@ -74,3 +71,4 @@ - $this->join = ''; - - if ($mode == 'customer') { + $this->join = ''; + + if ($mode == 'customer') + { @@ -82,3 +80,4 @@ - //$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled - $this->categ_link = MAIN_DB_PREFIX.'categorie_societe'; - } elseif ($mode == 'supplier') { + $this->where .= " c.fk_statut > 0"; // Not draft and not cancelled + } + elseif ($mode == 'supplier') + { @@ -90,2 +89 @@ - //$this->where .= " c.fk_statut > 2"; // Only approved & ordered - $this->categ_link = MAIN_DB_PREFIX.'categorie_fournisseur'; + $this->where .= " c.fk_statut > 2"; // Only approved & ordered @@ -94,4 +92,6 @@ - $this->where .= ($this->where ? ' AND ' : '').'c.entity IN ('.getEntity('commande').')'; - - if ($this->socid) { - $this->where .= " AND c.fk_soc = ".((int) $this->socid); + $this->where .= ' AND c.entity IN ('.getEntity('commande').')'; + + if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id; + if ($this->socid) + { + $this->where .= " AND c.fk_soc = ".$this->socid; @@ -99,12 +99,14 @@ - if ($this->userid > 0) { - $this->where .= ' AND c.fk_user_author = '.((int) $this->userid); - } - - if ($typentid) { - $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc'; - $this->where .= ' AND s.fk_typent = '.((int) $typentid); - } - - if ($categid) { - $this->where .= ' AND EXISTS (SELECT rowid FROM '.$this->categ_link.' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')'; - } + if ($this->userid > 0) $this->where .= ' AND c.fk_user_author = '.$this->userid; + + if ($typentid) + { + $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'societe as s ON s.rowid = c.fk_soc'; + $this->where .= ' AND s.fk_typent = '.$typentid; + } + + if ($categid) + { + $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cats ON cats.fk_soc = c.fk_soc'; + $this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as cat ON cat.rowid = cats.fk_categorie'; + $this->where .= ' AND cat.rowid = '.$categid; + } @@ -117 +119 @@ - * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month + * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month @@ -126,4 +128,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -133 +133 @@ - $sql .= $this->db->order('dm', 'DESC'); + $sql .= $this->db->order('dm', 'DESC'); @@ -151,4 +151,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -157 +155 @@ - $sql .= $this->db->order('dm', 'DESC'); + $sql .= $this->db->order('dm', 'DESC'); @@ -166 +164 @@ - * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month + * @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month @@ -175,4 +173,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -182 +178 @@ - $sql .= $this->db->order('dm', 'DESC'); + $sql .= $this->db->order('dm', 'DESC'); @@ -200,4 +196,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -207 +201 @@ - $sql .= $this->db->order('dm', 'DESC'); + $sql .= $this->db->order('dm', 'DESC'); @@ -223,4 +217,2 @@ - if (!$user->hasRight('societe', 'client', 'voir') && !$this->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -229 +221 @@ - $sql .= $this->db->order('year', 'DESC'); + $sql .= $this->db->order('year', 'DESC'); @@ -238 +230 @@ - * @param int $limit Limit + * @param int $limit Limit @@ -246,7 +238,3 @@ - $sql .= " FROM ".$this->from; - $sql .= " INNER JOIN ".$this->from_line." ON c.rowid = tl.fk_commande"; - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."product as product ON tl.fk_product = product.rowid"; - if (!$user->hasRight('societe', 'client', 'voir') && !$user->socid) { - $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id); - } - $sql .= $this->join; + $sql .= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product"; + if (!$user->rights->societe->client->voir && !$user->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= $this->join; @@ -254 +242,2 @@ - $sql .= " AND c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'"; + $sql .= " AND c.rowid = tl.fk_commande AND tl.fk_product = product.rowid"; + $sql .= " AND c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year, 1, false))."' AND '".$this->db->idate(dol_get_last_day($year, 12, false))."'"; @@ -256,4 +245,4 @@ - $sql .= $this->db->order('nb', 'DESC'); - //$sql.= $this->db->plimit(20); - - return $this->_getAllByProduct($sql, $limit); + $sql .= $this->db->order('nb', 'DESC'); + //$sql.= $this->db->plimit(20); + + return $this->_getAllByProduct($sql, $limit);