diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 34faaddfe4e..71213bd923e 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3518,20 +3518,26 @@ class Product extends CommonObject * @param string $label Label of stock movement * @param double $price Unit price HT of product, used to calculate average weighted price (PMP in french). If 0, average weighted price is not changed. * @param string $inventorycode Inventory code + * @param string $origin_element Origin element type + * @param int $origin_id Origin id of element * @return int <0 if KO, >0 if OK */ - function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $inventorycode='') + function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $inventorycode='', $origin_element='', $origin_id=null) { if ($id_entrepot) { $this->db->begin(); require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php'; + + // If remove stock then save it the actual PMP as price (the new calculation of PMP is triggered only with $movement = 0 || 3 ) + if ($movement == 1) $price = $this->pmp; $op[0] = "+".trim($nbpiece); $op[1] = "-".trim($nbpiece); $movementstock=new MouvementStock($this->db); + $movementstock->setOrigin($origin_element, $origin_id); $result=$movementstock->_create($user,$this->id,$id_entrepot,$op[$movement],$movement,$price,$label,$inventorycode); if ($result >= 0) @@ -3563,9 +3569,11 @@ class Product extends CommonObject * @param date $dluo sell-by date * @param string $lot Lot number * @param string $inventorycode Inventory code + * @param string $origin_element Origin element type + * @param int $origin_id Origin id of element * @return int <0 if KO, >0 if OK */ - function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $dlc='', $dluo='',$lot='', $inventorycode='') + function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label='', $price=0, $dlc='', $dluo='',$lot='', $inventorycode='', $origin_element='', $origin_id=null) { if ($id_entrepot) { @@ -3573,10 +3581,14 @@ class Product extends CommonObject require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php'; + // If remove stock then save it the actual PMP as price (the new calculation of PMP is triggered only with $movement = 0 || 3 ) + if ($movement == 1) $price = $this->pmp; + $op[0] = "+".trim($nbpiece); $op[1] = "-".trim($nbpiece); $movementstock=new MouvementStock($this->db); + $movementstock->setOrigin($origin_element, $origin_id); $result=$movementstock->_create($user,$this->id,$id_entrepot,$op[$movement],$movement,$price,$label,$inventorycode,'',$dlc,$dluo,$lot); if ($result >= 0) diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php index dee344b6a5b..6a09dde054c 100644 --- a/htdocs/product/stock/class/mouvementstock.class.php +++ b/htdocs/product/stock/class/mouvementstock.class.php @@ -893,6 +893,33 @@ class MouvementStock extends CommonObject return ''; } + + /** + * Set attribute origin to object + * + * @param string $origin_element + * @param int $origin_id + * + * @return void + */ + function setOrigin($origin_element, $origin_id) + { + if (!empty($origin_element) && $origin_id > 0) + { + $origin=''; + if ($origin_element == 'project') + { + if (!class_exists('Project')) require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; + $origin = new Project($this->db); + } + + if (!empty($origin)) + { + $this->origin = $origin; + $this->origin->id = $origin_id; + } + } + } /** diff --git a/htdocs/product/stock/mouvement.php b/htdocs/product/stock/mouvement.php index 8e6775e2e6e..f5d9cbab8e2 100644 --- a/htdocs/product/stock/mouvement.php +++ b/htdocs/product/stock/mouvement.php @@ -34,6 +34,11 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +if (! empty($conf->projet->enabled)) +{ + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; + require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; +} $langs->load("products"); $langs->load("stocks"); @@ -156,6 +161,15 @@ if ($action == "correct_stock") if (! $error) { + $origin_element = ''; + $origin_id = null; + + if (GETPOST('projectid', 'int')) + { + $origin_element = 'project'; + $origin_id = GETPOST('projectid', 'int'); + } + if ($product->hasbatch()) { $batch=GETPOST('batch_number'); @@ -173,7 +187,9 @@ if ($action == "correct_stock") GETPOST("label",'san_alpha'), GETPOST('unitprice'), $eatby,$sellby,$batch, - GETPOST('inventorycode') + GETPOST('inventorycode'), + $origin_element, + $origin_id ); // We do not change value of stock for a correction } else @@ -185,7 +201,9 @@ if ($action == "correct_stock") GETPOST("mouvement"), GETPOST("label",'san_alpha'), GETPOST('unitprice'), - GETPOST('inventorycode') + GETPOST('inventorycode'), + $origin_element, + $origin_id ); // We do not change value of stock for a correction } @@ -392,6 +410,7 @@ $userstatic=new User($db); $form=new Form($db); $formother=new FormOther($db); $formproduct=new FormProduct($db); +if (!empty($conf->projet->enabled)) $formproject=new FormProjets($db); $sql = "SELECT p.rowid, p.ref as product_ref, p.label as produit, p.fk_product_type as type, p.entity,"; $sql.= " e.label as stock, e.rowid as entrepot_id, e.lieu,"; diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index 6bcf98e96c1..158440c5855 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -37,6 +37,11 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productstockentrepot.class.php'; if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; +if (! empty($conf->projet->enabled)) +{ + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; + require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; +} $langs->load("products"); $langs->load("orders"); @@ -220,6 +225,15 @@ if ($action == "correct_stock" && ! $cancel) $priceunit=price2num(GETPOST("unitprice")); if (is_numeric(GETPOST("nbpiece")) && $id) { + $origin_element = ''; + $origin_id = null; + + if (GETPOST('projectid', 'int')) + { + $origin_element = 'project'; + $origin_id = GETPOST('projectid', 'int'); + } + if (empty($object)) { $object = new Product($db); $result=$object->fetch($id); @@ -236,7 +250,9 @@ if ($action == "correct_stock" && ! $cancel) $d_eatby, $d_sellby, $batchnumber, - GETPOST('inventorycode') + GETPOST('inventorycode'), + $origin_element, + $origin_id ); // We do not change value of stock for a correction } else @@ -248,7 +264,9 @@ if ($action == "correct_stock" && ! $cancel) GETPOST("mouvement"), GETPOST("label"), $priceunit, - GETPOST('inventorycode') + GETPOST('inventorycode'), + $origin_element, + $origin_id ); // We do not change value of stock for a correction } @@ -490,7 +508,7 @@ if ($action == 'updateline' && GETPOST('save') == $langs->trans('Save')) $form = new Form($db); $formproduct=new FormProduct($db); - +if (! empty($conf->projet->enabled)) $formproject=new FormProjets($db); if ($id > 0 || $ref) { diff --git a/htdocs/product/stock/tpl/stockcorrection.tpl.php b/htdocs/product/stock/tpl/stockcorrection.tpl.php index ac37566457f..d08cf334671 100644 --- a/htdocs/product/stock/tpl/stockcorrection.tpl.php +++ b/htdocs/product/stock/tpl/stockcorrection.tpl.php @@ -77,7 +77,14 @@ // Purchase price print ''; print ''.$langs->trans("UnitPurchaseValue").''; - print ''; + print ''; + if (! empty($conf->projet->enabled)) + { + print ''.$langs->trans('Project').''; + print ''; + $formproject->select_projects(); + print ''; + } print ''; // Serial / Eat-by date