diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 745fea5ab76..d625f2acbf1 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -967,6 +967,103 @@ class Orders extends DolibarrApi 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 + */ + public function getOrderShipments($id) + { + require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; + if (!DolibarrApiAccess::$user->rights->expedition->lire) { + throw new RestException(401); + } + $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 = ".$this->db->escape($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 shipments 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 + */ + 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; + $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()); + } + $i++; + } + return $shipment->id; + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /**