Skip to content

Commit

Permalink
manage packaging on supplier prices
Browse files Browse the repository at this point in the history
  • Loading branch information
atm-greg committed Nov 23, 2019
1 parent 6ef01a0 commit ce15af0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
19 changes: 18 additions & 1 deletion htdocs/fourn/class/fournisseur.product.class.php
Expand Up @@ -273,6 +273,7 @@ public function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn
$charges = price2num($charges, 'MU');
$qty = price2num($qty, 'MS');
$unitBuyPrice = price2num($buyprice / $qty, 'MU');
$packaging = ($this->packaging < $qty) ? $qty : $this->packaging;

$error = 0;
$now = dol_now();
Expand Down Expand Up @@ -359,6 +360,7 @@ public function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn
$sql .= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
$sql .= " barcode = ".(empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
$sql .= " fk_barcode_type = ".(empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", packaging = ".(empty($packaging) ? 1 : $packaging);
$sql .= " WHERE rowid = ".$this->product_fourn_price_id;
// TODO Add price_base_type and price_ttc

Expand Down Expand Up @@ -409,6 +411,7 @@ public function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
$sql .= " multicurrency_price, multicurrency_unitprice, multicurrency_tx, fk_multicurrency, multicurrency_code,";
$sql .= "datec, fk_product, fk_soc, ref_fourn, desc_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, fk_availability, default_vat_code, info_bits, entity, delivery_time_days, supplier_reputation, barcode, fk_barcode_type)";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", packaging";
$sql .= " values(";
$sql .= (isset($multicurrency_buyprice) ? "'".$this->db->escape(price2num($multicurrency_buyprice))."'" : 'null').",";
$sql .= (isset($multicurrency_unitBuyPrice) ? "'".$this->db->escape(price2num($multicurrency_unitBuyPrice))."'" : 'null').",";
Expand Down Expand Up @@ -436,6 +439,7 @@ public function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn
$sql .= (empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'").",";
$sql .= (empty($barcode) ? 'NULL' : "'".$this->db->escape($barcode)."'").",";
$sql .= (empty($fk_barcode_type) ? 'NULL' : "'".$this->db->escape($fk_barcode_type)."'");
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", ".(empty($this->packaging) ? 1 : $this->db->escape($this->packaging)) ;
$sql .= ")";

$idinserted = 0;
Expand Down Expand Up @@ -502,6 +506,7 @@ public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
$sql .= " pfp.supplier_reputation, pfp.fk_user, pfp.datec,";
$sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code,";
$sql .= " pfp.barcode, pfp.fk_barcode_type";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", pfp.packaging";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql .= " WHERE pfp.rowid = ".(int) $rowid;

Expand Down Expand Up @@ -545,6 +550,12 @@ public function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
$this->fourn_barcode = $obj->barcode;
$this->fourn_fk_barcode_type = $obj->fk_barcode_type;
}

if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
$this->packaging = $obj->packaging;
if ($this->packaging < $this->fourn_qty) $this->packaging = $this->fourn_qty;
}

if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
{
$priceparser = new PriceParser($this->db);
Expand Down Expand Up @@ -599,7 +610,8 @@ public function list_product_fournisseur_price($prodid, $sortfield = '', $sortor
$sql .= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.info_bits, pfp.delivery_time_days, pfp.supplier_reputation,";
$sql .= " pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code, pfp.datec, pfp.tms,";
$sql .= " pfp.barcode, pfp.fk_barcode_type";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."societe as s";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", pfp.packaging";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."societe as s";
$sql .= " WHERE pfp.entity IN (".getEntity('productsupplierprice').")";
$sql .= " AND pfp.fk_soc = s.rowid";
$sql .= " AND s.status=1"; // only enabled company selected
Expand Down Expand Up @@ -648,6 +660,11 @@ public function list_product_fournisseur_price($prodid, $sortfield = '', $sortor
$prodfourn->fourn_multicurrency_id = $record["fk_multicurrency"];
$prodfourn->fourn_multicurrency_code = $record["multicurrency_code"];

if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) {
$prodfourn->packaging = $record["packaging"];
if ($prodfourn->packaging < $prodfourn->fourn_qty) $prodfourn->packaging = $prodfourn->fourn_qty;
}

if ($conf->barcode->enabled) {
$prodfourn->barcode = $record["barcode"];
$prodfourn->fk_barcode_type = $record["fk_barcode_type"];
Expand Down
6 changes: 5 additions & 1 deletion htdocs/langs/en_US/products.lang
Expand Up @@ -303,6 +303,10 @@ PossibleValues=Possible values
GoOnMenuToCreateVairants=Go on menu %s - %s to prepare attribute variants (like colors, size, ...)
UseProductFournDesc=Add a feature to define the descriptions of products defined by the vendors in addition to descriptions for customers
ProductSupplierDescription=Vendor description for the product
UseProductSupplierPackaging=Use packaging on supplier prices (recalculate quantities according to packaging set on supplier price when adding/updating line in supplier documents)
PackagingForThisProduct=Packaging
QtyRecalculatedWithPackaging=The quantity of the line were recalculated according to supplier packaging

#Attributes
VariantAttributes=Variant attributes
ProductAttributes=Variant attributes for products
Expand Down Expand Up @@ -347,4 +351,4 @@ ErrorDestinationProductNotFound=Destination product not found
ErrorProductCombinationNotFound=Product variant not found
ActionAvailableOnVariantProductOnly=Action only available on the variant of product
ProductsPricePerCustomer=Product prices per customers
ProductSupplierExtraFields=Additional Attributes (Supplier Prices)
ProductSupplierExtraFields=Additional Attributes (Supplier Prices)
19 changes: 19 additions & 0 deletions htdocs/product/admin/product.php
Expand Up @@ -156,6 +156,18 @@
$resql_new = $db->query($sql_new);
}
}

$value = GETPOST('activate_useProdSupplierPackaging', 'alpha');
$res = dolibarr_set_const($db, "PRODUCT_USE_SUPPLIER_PACKAGING", $value,'chaine',0,'',$conf->entity);
if ($value) {
$sql_test = "SELECT count(packaging) as cpt FROM ".MAIN_DB_PREFIX."product_fournisseur_price WHERE 1";
$resql = $db->query($sql_test);
if (!$resql && $db->lasterrno == 'DB_ERROR_NOSUCHFIELD') // if the field does not exist, we create it
{
$sql_new = "ALTER TABLE ".MAIN_DB_PREFIX."product_fournisseur_price ADD COLUMN packaging double(24,8) DEFAULT 1";
$resql_new = $db->query($sql_new);
}
}
}

if ($action == 'specimen') // For products
Expand Down Expand Up @@ -676,6 +688,13 @@
print $form->selectyesno("activate_useProdFournDesc", (! empty($conf->global->PRODUIT_FOURN_TEXTS)?$conf->global->PRODUIT_FOURN_TEXTS:0), 1);
print '</td>';
print '</tr>';

print '<tr class="oddeven">';
print '<td>'.$langs->trans("UseProductSupplierPackaging").'</td>';
print '<td width="60" align="right">';
print $form->selectyesno("activate_useProdSupplierPackaging", (! empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)?$conf->global->PRODUCT_USE_SUPPLIER_PACKAGING:0), 1);
print '</td>';
print '</tr>';
}


Expand Down
10 changes: 8 additions & 2 deletions htdocs/product/class/product.class.php
Expand Up @@ -1730,6 +1730,7 @@ public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref
$sql .= " pfp.fk_product, pfp.ref_fourn, pfp.desc_fourn, pfp.fk_soc, pfp.tva_tx, pfp.fk_supplier_price_expression";
$sql .= " ,pfp.default_vat_code";
$sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", pfp.packaging";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql .= " WHERE pfp.rowid = ".$prodfournprice;
if ($qty > 0) { $sql .= " AND pfp.quantity <= ".$qty;
Expand Down Expand Up @@ -1772,6 +1773,7 @@ public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref
$this->fourn_multicurrency_tx = $obj->multicurrency_tx;
$this->fourn_multicurrency_id = $obj->fk_multicurrency;
$this->fourn_multicurrency_code = $obj->multicurrency_code;
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $this->packaging = $obj->packaging;
$result = $obj->fk_product;
return $result;
}
Expand All @@ -1782,6 +1784,7 @@ public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref
$sql .= " pfp.fk_product, pfp.ref_fourn as ref_supplier, pfp.desc_fourn as desc_supplier, pfp.tva_tx, pfp.fk_supplier_price_expression";
$sql .= " ,pfp.default_vat_code";
$sql .= " ,pfp.multicurrency_price, pfp.multicurrency_unitprice, pfp.multicurrency_tx, pfp.fk_multicurrency, pfp.multicurrency_code";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", pfp.packaging";
$sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql .= " WHERE pfp.fk_product = ".$product_id;
if ($fourn_ref != 'none') { $sql .= " AND pfp.ref_fourn = '".$fourn_ref."'";
Expand Down Expand Up @@ -1830,7 +1833,8 @@ public function get_buyprice($prodfournprice, $qty, $product_id = 0, $fourn_ref
$this->fourn_multicurrency_tx = $obj->multicurrency_tx;
$this->fourn_multicurrency_id = $obj->fk_multicurrency;
$this->fourn_multicurrency_code = $obj->multicurrency_code;
$result = $obj->fk_product;
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $this->packaging = $obj->packaging;
$result = $obj->fk_product;
return $result;
}
else
Expand Down Expand Up @@ -3669,7 +3673,8 @@ public function add_fournisseur($user, $id_fourn, $ref_fourn, $quantity)
$sql.= ", quantity";
$sql.= ", fk_user";
$sql.= ", tva_tx";
$sql.= ") VALUES (";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", packaging";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."'";
$sql.= ", ".$conf->entity;
$sql.= ", ".$this->id;
Expand All @@ -3678,6 +3683,7 @@ public function add_fournisseur($user, $id_fourn, $ref_fourn, $quantity)
$sql.= ", ".$quantity;
$sql.= ", ".$user->id;
$sql.= ", 0";
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) $sql.= ", ".(!empty($this->packaging) ? $this->packaging : 1);
$sql.= ")";

if ($this->db->query($sql)) {
Expand Down
25 changes: 25 additions & 0 deletions htdocs/product/fournisseurs.php
Expand Up @@ -170,6 +170,7 @@
$supplier_description = GETPOST('supplier_description', 'alpha');
$barcode = GETPOST('barcode', 'alpha');
$fk_barcode_type = GETPOST('fk_barcode_type', 'int');
$packaging = GETPOST('packaging', 'int');

if ($tva_tx == '')
{
Expand Down Expand Up @@ -232,6 +233,12 @@
}
}

if (empty($packaging)) $packaging = 1;

if ($packaging < $quantity) $packaging = $quantity;

$object->packaging = $packaging;

if (!$error)
{
$db->begin();
Expand Down Expand Up @@ -765,6 +772,15 @@ function update_price_from_multicurrency() {
print '</tr>';
}

// Product packaging
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
{
print '<tr>';
print '<td>'.$langs->trans('PackagingForThisProduct').'</td>';
print '<td><input class="flat" name="packaging" size="4" value="'.($rowid ? price2num($object->packaging) : 1).'"></td>';
print '</tr>';
}

$extrafields->fetch_name_optionals_label("product_fournisseur_price");
$extralabels = $extrafields->attributes["product_fournisseur_price"]['label'];
// Extrafields
Expand Down Expand Up @@ -872,6 +888,7 @@ function update_price_from_multicurrency() {
print_liste_field_titre("BarcodeValue", $_SERVER["PHP_SELF"], "pfp.barcode", "", $param, '', $sortfield, $sortorder, 'center ');
print_liste_field_titre("BarcodeType", $_SERVER["PHP_SELF"], "pfp.fk_barcode_type", "", $param, '', $sortfield, $sortorder, 'center ');
}
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING)) print_liste_field_titre("PackagingForThisProduct",$_SERVER["PHP_SELF"],"pfp.packaging","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre("DateModification", $_SERVER["PHP_SELF"], "pfp.tms", "", $param, '', $sortfield, $sortorder, 'right ');

// fetch optionals attributes and labels
Expand Down Expand Up @@ -1002,6 +1019,14 @@ function update_price_from_multicurrency() {
print '</td>';
}

// Packaging
if (!empty($conf->global->PRODUCT_USE_SUPPLIER_PACKAGING))
{
print '<td align="center">';
print price2num($productfourn->packaging);
print '</td>';
}

// Date
print '<td align="right">';
print dol_print_date(($productfourn->fourn_date_modification ? $productfourn->fourn_date_modification : $productfourn->date_modification), "dayhour");
Expand Down

0 comments on commit ce15af0

Please sign in to comment.