Merge pull request #21682 from ptibogxiv/patch-36

NEW manage virtual stock at future date
This commit is contained in:
Laurent Destailleur 2022-08-10 04:24:41 +02:00 committed by GitHub
commit 63f3c2535d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 34 deletions

View File

@ -253,7 +253,7 @@ class Products extends DolibarrApi
if (!$ids_only) {
$product_static = new Product($this->db);
if ($product_static->fetch($obj->rowid)) {
if ($includestockdata && DolibarrApiAccess::$user->rights->stock->lire) {
if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
$product_static->load_stock();
if (is_array($product_static->stock_warehouse)) {
@ -1644,10 +1644,10 @@ class Products extends DolibarrApi
$combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id);
$combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
if ($includestock==1 && DolibarrApiAccess::$user->rights->stock->lire) {
if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
$productModel = new Product($this->db);
$productModel->fetch((int) $combination->fk_product_child);
$productModel->load_stock();
$productModel->load_stock($includestock);
$combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
}
}
@ -2040,8 +2040,8 @@ class Products extends DolibarrApi
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
if ($includestockdata && DolibarrApiAccess::$user->rights->stock->lire) {
$this->product->load_stock();
if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
$this->product->load_stock($includestockdata);
if (is_array($this->product->stock_warehouse)) {
foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {

View File

@ -3038,12 +3038,13 @@ class Product extends CommonObject
/**
* Charge tableau des stats commande fournisseur pour le produit/service
*
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id des statuts pour filtrer sur des statuts
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @return int Array of stats in $this->stats_commande_fournisseur, <0 if ko or >0 if ok
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id des statuts pour filtrer sur des statuts
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @param int $dateofvirtualstock Date of virtual stock
* @return int Array of stats in $this->stats_commande_fournisseur, <0 if ko or >0 if ok
*/
public function load_stats_commande_fournisseur($socid = 0, $filtrestatut = '', $forVirtualStock = 0)
public function load_stats_commande_fournisseur($socid = 0, $filtrestatut = '', $forVirtualStock = 0, $dateofvirtualstock = null)
{
// phpcs:enable
global $conf, $user, $hookmanager, $action;
@ -3069,6 +3070,9 @@ class Product extends CommonObject
if ($filtrestatut != '') {
$sql .= " AND c.fk_statut in (".$this->db->sanitize($filtrestatut).")"; // Peut valoir 0
}
if (!empty($dateofvirtualstock)) {
$sql .= " AND c.date_livraison <= '".$this->db->idate($dateofvirtualstock)."'";
}
$result = $this->db->query($sql);
if ($result) {
@ -3181,12 +3185,13 @@ class Product extends CommonObject
/**
* Charge tableau des stats réception fournisseur pour le produit/service
*
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id statut pour filtrer sur un statut
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id statut pour filtrer sur un statut
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @param int $dateofvirtualstock Date of virtual stock
* @return int Array of stats in $this->stats_reception, <0 if ko or >0 if ok
*/
public function load_stats_reception($socid = 0, $filtrestatut = '', $forVirtualStock = 0)
public function load_stats_reception($socid = 0, $filtrestatut = '', $forVirtualStock = 0, $dateofvirtualstock = null)
{
// phpcs:enable
global $conf, $user, $hookmanager, $action;
@ -3212,6 +3217,9 @@ class Product extends CommonObject
if ($filtrestatut <> '') {
$sql .= " AND cf.fk_statut IN (".$this->db->sanitize($filtrestatut).")";
}
if (!empty($dateofvirtualstock)) {
$sql .= " AND fd.datec <= '".$this->db->idate($dateofvirtualstock)."'";
}
$result = $this->db->query($sql);
if ($result) {
@ -3238,12 +3246,13 @@ class Product extends CommonObject
/**
* Charge tableau des stats production pour le produit/service
*
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id statut pour filtrer sur un statut
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @param int $socid Id societe pour filtrer sur une societe
* @param string $filtrestatut Id statut pour filtrer sur un statut
* @param int $forVirtualStock Ignore rights filter for virtual stock calculation.
* @param int $dateofvirtualstock Date of virtual stock
* @return integer Array of stats in $this->stats_mrptoproduce (nb=nb of order, qty=qty ordered), <0 if ko or >0 if ok
*/
public function load_stats_inproduction($socid = 0, $filtrestatut = '', $forVirtualStock = 0)
public function load_stats_inproduction($socid = 0, $filtrestatut = '', $forVirtualStock = 0, $dateofvirtualstock = null)
{
// phpcs:enable
global $conf, $user, $hookmanager, $action;
@ -3268,6 +3277,9 @@ class Product extends CommonObject
if ($filtrestatut <> '') {
$sql .= " AND m.status IN (".$this->db->sanitize($filtrestatut).")";
}
if (!empty($dateofvirtualstock)) {
$sql .= " AND m.date_valid <= '".$this->db->idate($dateofvirtualstock)."'"; // better date to code ? end of production ?
}
$sql .= " GROUP BY role";
$this->stats_mrptoconsume['customers'] = 0;
@ -5346,10 +5358,11 @@ class Product extends CommonObject
* @param string $option '' = Load all stock info, also from closed and internal warehouses, 'nobatch', 'novirtual'
* You can also filter on 'warehouseclosed', 'warehouseopen', 'warehouseinternal'
* @param int $includedraftpoforvirtual Include draft status of PO for virtual stock calculation
* @param int $dateofvirtualstock Date of virtual stock
* @return int < 0 if KO, > 0 if OK
* @see load_virtual_stock(), loadBatchInfo()
*/
public function load_stock($option = '', $includedraftpoforvirtual = null)
public function load_stock($option = '', $includedraftpoforvirtual = null, $dateofvirtualstock = null)
{
// phpcs:enable
global $conf;
@ -5407,7 +5420,7 @@ class Product extends CommonObject
$this->db->free($result);
if (!preg_match('/novirtual/', $option)) {
$this->load_virtual_stock($includedraftpoforvirtual); // This also load all arrays stats_xxx...
$this->load_virtual_stock($includedraftpoforvirtual, $dateofvirtualstock); // This also load all arrays stats_xxx...
}
return 1;
@ -5424,10 +5437,11 @@ class Product extends CommonObject
* This function need a lot of load. If you use it on list, use a cache to execute it one for each product id.
*
* @param int $includedraftpoforvirtual Include draft status and not yet approved Purchase Orders for virtual stock calculation
* @param int $dateofvirtualstock Date of virtual stock
* @return int < 0 if KO, > 0 if OK
* @see load_stock(), loadBatchInfo()
*/
public function load_virtual_stock($includedraftpoforvirtual = null)
public function load_virtual_stock($includedraftpoforvirtual = null, $dateofvirtualstock = null)
{
// phpcs:enable
global $conf, $hookmanager, $action;
@ -5466,7 +5480,7 @@ class Product extends CommonObject
if (isset($includedraftpoforvirtual)) {
$filterStatus = '0,1,2,'.$filterStatus; // 1,2 may have already been inside $filterStatus but it is better to have twice than missing $filterStatus does not include them
}
$result = $this->load_stats_commande_fournisseur(0, $filterStatus, 1);
$result = $this->load_stats_commande_fournisseur(0, $filterStatus, 1, $dateofvirtualstock);
if ($result < 0) {
dol_print_error($this->db, $this->error);
}
@ -5478,7 +5492,7 @@ class Product extends CommonObject
if (isset($includedraftpoforvirtual)) {
$filterStatus = '0,'.$filterStatus;
}
$result = $this->load_stats_reception(0, $filterStatus, 1);
$result = $this->load_stats_reception(0, $filterStatus, 1, $dateofvirtualstock);
if ($result < 0) {
dol_print_error($this->db, $this->error);
}
@ -5490,14 +5504,14 @@ class Product extends CommonObject
if (isset($includedraftpoforvirtual)) {
$filterStatus = '0,'.$filterStatus;
}
$result = $this->load_stats_reception(0, $filterStatus, 1); // Use same tables than when module reception is not used.
$result = $this->load_stats_reception(0, $filterStatus, 1, $dateofvirtualstock); // Use same tables than when module reception is not used.
if ($result < 0) {
dol_print_error($this->db, $this->error);
}
$stock_reception_fournisseur = $this->stats_reception['qty'];
}
if (!empty($conf->mrp->enabled)) {
$result = $this->load_stats_inproduction(0, '1,2', 1);
$result = $this->load_stats_inproduction(0, '1,2', 1, $dateofvirtualstock);
if ($result < 0) {
dol_print_error($this->db, $this->error);
}
@ -6196,8 +6210,6 @@ class Product extends CommonObject
}
}
/**
* Class to manage products or services.
* Do not use 'Service' as class name since it is already used by APIs.

View File

@ -518,13 +518,9 @@ while ($i < ($limit ? min($num, $limit) : $num)) {
}
if ($mode == 'future') {
$prod->load_stock('warehouseopen, warehouseinternal', 0); // This call also ->load_virtual_stock()
//$result = $prod->load_stats_reception(0, '4');
//print $prod->stats_commande_fournisseur['qty'].'<br>'."\n";
//print $prod->stats_reception['qty'];
$stock = '<span class="opacitymedium">'.$langs->trans("FeatureNotYetAvailable").'</span>';
$prod->load_stock('warehouseopen, warehouseinternal', 0, $dateendofday);
$stock = $prod->stock_theorique;
$prod->load_stock('warehouseopen, warehouseinternal', 0);
$virtualstock = $prod->stock_theorique;
} else {
if ($fk_warehouse > 0) {