|
@@ -33,7 +33,7 @@
|
|
|
* \brief File of class to manage suppliers orders
|
|
|
*/
|
|
|
|
|
|
-include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
|
|
|
+require_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
|
|
|
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
|
|
|
if (!empty($conf->productbatch->enabled)) {
|
|
|
require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
|
|
@@ -438,6 +438,7 @@ class CommandeFournisseur extends CommonOrder
|
|
|
* Lines
|
|
|
*/
|
|
|
$result = $this->fetch_lines();
|
|
|
+
|
|
|
if ($result < 0) {
|
|
|
return -1;
|
|
|
} else {
|
|
@@ -460,7 +461,7 @@ class CommandeFournisseur extends CommonOrder
|
|
|
{
|
|
|
global $conf;
|
|
|
// phpcs:enable
|
|
|
- //$result=$this->fetch_lines();
|
|
|
+
|
|
|
$this->lines = array();
|
|
|
|
|
|
$sql = "SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description, l.qty,";
|
|
@@ -471,22 +472,12 @@ class CommandeFournisseur extends CommonOrder
|
|
|
$sql .= " l.fk_unit,";
|
|
|
$sql .= " l.date_start, l.date_end,";
|
|
|
$sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc';
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql .= ", pfp.rowid as fk_pfp, pfp.packaging, MAX(pfp.quantity) as max_qty";
|
|
|
- }
|
|
|
$sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
|
|
|
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON pfp.entity IN (".getEntity('product_fournisseur_price').") AND l.fk_product = pfp.fk_product and l.ref = pfp.ref_fourn AND l.qty >= pfp.quantity AND pfp.fk_soc = ".((int) $this->socid);
|
|
|
- }
|
|
|
$sql .= " WHERE l.fk_commande = ".((int) $this->id);
|
|
|
if ($only_product) {
|
|
|
$sql .= ' AND p.fk_product_type = 0';
|
|
|
}
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql.= " GROUP BY l.rowid";
|
|
|
- $sql.= " HAVING (max_qty = MAX(pfp.quantity) OR max_qty IS NULL)";
|
|
|
- }
|
|
|
$sql .= " ORDER BY l.rang, l.rowid";
|
|
|
//print $sql;
|
|
|
|
|
@@ -536,11 +527,34 @@ class CommandeFournisseur extends CommonOrder
|
|
|
$line->ref_supplier = $objp->ref_supplier; // The supplier ref of price when product was added. May have change since
|
|
|
|
|
|
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $line->fk_fournprice = $objp->fk_pfp;
|
|
|
- $line->packaging = $objp->packaging;
|
|
|
+ // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line.
|
|
|
+ // Move this into another method and call it when required.
|
|
|
+
|
|
|
+ // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty)
|
|
|
+ $sqlsearchpackage = 'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX."product_fournisseur_price";
|
|
|
+ $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")";
|
|
|
+ $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product);
|
|
|
+ $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'";
|
|
|
+ $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified
|
|
|
+ $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified
|
|
|
+ $sqlsearchpackage .= " AND fk_soc = ".((int) $this->socid);
|
|
|
+ $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first
|
|
|
+ $sqlsearchpackage .= " LIMIT 1";
|
|
|
+
|
|
|
+ $resqlsearchpackage = $this->db->query($sqlsearchpackage);
|
|
|
+ if ($resqlsearchpackage) {
|
|
|
+ $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
|
|
|
+ if ($objsearchpackage) {
|
|
|
+ $line->fk_fournprice = $objsearchpackage->rowid;
|
|
|
+ $line->packaging = $objsearchpackage->packaging;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $this->error = $this->db->lasterror();
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- $line->date_start = $this->db->jdate($objp->date_start);
|
|
|
+ $line->date_start = $this->db->jdate($objp->date_start);
|
|
|
$line->date_end = $this->db->jdate($objp->date_end);
|
|
|
$line->fk_unit = $objp->fk_unit;
|
|
|
|
|
@@ -1826,6 +1840,7 @@ class CommandeFournisseur extends CommonOrder
|
|
|
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
$prod = new Product($this->db, $fk_product);
|
|
|
$prod->get_buyprice($fk_prod_fourn_price, $qty, $fk_product, 'none', ($this->fk_soc ? $this->fk_soc : $this->socid));
|
|
|
+
|
|
|
if ($qty < $prod->packaging) {
|
|
|
$qty = $prod->packaging;
|
|
|
} else {
|
|
@@ -3539,25 +3554,18 @@ class CommandeFournisseurLigne extends CommonOrderLine
|
|
|
global $conf;
|
|
|
|
|
|
$sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
|
|
|
- $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref,';
|
|
|
+ $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
|
|
|
$sql .= ' cd.remise, cd.remise_percent, cd.subprice,';
|
|
|
$sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
|
|
|
$sql .= ' cd.total_localtax1, cd.total_localtax2,';
|
|
|
$sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
|
|
|
$sql .= ' cd.date_start, cd.date_end, cd.fk_unit,';
|
|
|
- $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc';
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql .= ", pfp.rowid as fk_pfp, pfp.packaging, MAX(pfp.quantity) as max_qty";
|
|
|
- }
|
|
|
- $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
|
|
|
+ $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
|
|
|
+ $sql .= ' c.fk_soc as socid';
|
|
|
+ $sql .= ' FROM '.MAIN_DB_PREFIX.'commande_fournisseur as c, '.MAIN_DB_PREFIX.'commande_fournisseurdet as cd';
|
|
|
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON pfp.entity IN (".getEntity('product_fournisseur_price').") AND cd.fk_product = pfp.fk_product and cd.ref = pfp.ref_fourn AND cd.qty >= pfp.quantity";
|
|
|
- }
|
|
|
- $sql .= ' WHERE cd.rowid = '.((int) $rowid);
|
|
|
- if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $sql .= " GROUP BY cd.rowid HAVING (max_qty = MAX(pfp.quantity) OR max_qty IS NULL)";
|
|
|
- }
|
|
|
+ $sql .= ' WHERE cd.fk_commande = c.rowid AND cd.rowid = '.((int) $rowid);
|
|
|
+
|
|
|
$result = $this->db->query($sql);
|
|
|
if ($result) {
|
|
|
$objp = $this->db->fetch_object($result);
|
|
@@ -3568,8 +3576,8 @@ class CommandeFournisseurLigne extends CommonOrderLine
|
|
|
$this->fk_commande = $objp->fk_commande;
|
|
|
$this->desc = $objp->description;
|
|
|
$this->qty = $objp->qty;
|
|
|
- $this->ref_fourn = $objp->ref;
|
|
|
- $this->ref_supplier = $objp->ref;
|
|
|
+ $this->ref_fourn = $objp->ref_supplier;
|
|
|
+ $this->ref_supplier = $objp->ref_supplier;
|
|
|
$this->subprice = $objp->subprice;
|
|
|
$this->tva_tx = $objp->tva_tx;
|
|
|
$this->localtax1_tx = $objp->localtax1_tx;
|
|
@@ -3593,9 +3601,33 @@ class CommandeFournisseurLigne extends CommonOrderLine
|
|
|
$this->product_ref = $objp->product_ref;
|
|
|
$this->product_label = $objp->product_label;
|
|
|
$this->product_desc = $objp->product_desc;
|
|
|
+
|
|
|
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
|
|
|
- $this->packaging = $objp->packaging;
|
|
|
- $this->fk_fournprice = $objp->fk_pfp;
|
|
|
+ // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line.
|
|
|
+ // Move this into another method and call it when required.
|
|
|
+
|
|
|
+ // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty)
|
|
|
+ $sqlsearchpackage = 'SELECT rowid, packaging FROM '.MAIN_DB_PREFIX."product_fournisseur_price";
|
|
|
+ $sqlsearchpackage .= ' WHERE entity IN ('.getEntity('product_fournisseur_price').")";
|
|
|
+ $sqlsearchpackage .= " AND fk_product = ".((int) $objp->fk_product);
|
|
|
+ $sqlsearchpackage .= " AND ref_fourn = '".$this->db->escape($objp->ref_supplier)."'";
|
|
|
+ $sqlsearchpackage .= " AND quantity <= ".((float) $objp->qty); // required to be qualified
|
|
|
+ $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= ".((float) $objp->qty).")"; // required to be qualified
|
|
|
+ $sqlsearchpackage .= " AND fk_soc = ".((int) $objp->socid);
|
|
|
+ $sqlsearchpackage .= " ORDER BY packaging ASC"; // Take the smaller package first
|
|
|
+ $sqlsearchpackage .= " LIMIT 1";
|
|
|
+
|
|
|
+ $resqlsearchpackage = $this->db->query($sqlsearchpackage);
|
|
|
+ if ($resqlsearchpackage) {
|
|
|
+ $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
|
|
|
+ if ($objsearchpackage) {
|
|
|
+ $this->fk_fournprice = $objsearchpackage->rowid;
|
|
|
+ $this->packaging = $objsearchpackage->packaging;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $this->error = $this->db->lasterror();
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
$this->date_start = $this->db->jdate($objp->date_start);
|