*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* \file htdocs/product/stock/massstockmove.php
* \ingroup stock
* \brief This page allows to select several products, then incoming warehouse and
* outgoing warehouse and create all stock movements for this.
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
$langs->load("products");
$langs->load("stocks");
$langs->load("orders");
// Security check
if ($user->societe_id) {
$socid = $user->societe_id;
}
$result=restrictedArea($user,'produit|service');
//checks if a product has been ordered
$action = GETPOST('action','alpha');
$id_product = GETPOST('productid', 'productid');
$id_sw = GETPOST('id_sw', 'id_sw');
$id_tw = GETPOST('id_tw', 'id_tw');
$qty = GETPOST('qty');
$idline = GETPOST('idline');
$sortfield = GETPOST('sortfield','alpha');
$sortorder = GETPOST('sortorder','alpha');
$page = GETPOST('page','int');
if (!$sortfield) {
$sortfield = 'p.ref';
}
if (!$sortorder) {
$sortorder = 'ASC';
}
$limit = $conf->liste_limit;
$offset = $limit * $page ;
$listofdata=array();
if (! empty($_SESSION['massstockmove'])) $listofdata=dol_json_decode($_SESSION['massstockmove'],true);
/*
* Actions
*/
if ($action == 'addline')
{
if (! ($id_product > 0))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Product")),'errors');
}
if (! $qty)
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Qty")),'errors');
}
if (! ($id_sw > 0))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("WarehouseSource")),'errors');
}
if (! ($id_tw > 0))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("WarehouseTarget")),'errors');
}
if ($id_sw > 0 && $id_tw == $id_sw)
{
$error++;
$langs->load("errors");
setEventMessage($langs->trans("ErrorWarehouseMustDiffers"),'errors');
}
if (! $error)
{
if (count(array_keys($listofdata)) > 0) $id=max(array_keys($listofdata)) + 1;
else $id=1;
$listofdata[$id]=array('id'=>$id, 'id_product'=>$id_product, 'qty'=>$qty, 'id_sw'=>$id_sw, 'id_tw'=>$id_tw);
$_SESSION['massstockmove']=dol_json_encode($listofdata);
unset($id_product);
//unset($id_sw);
//unset($id_tw);
unset($qty);
}
}
if ($action == 'delline' && $idline != '')
{
if (! empty($listofdata[$idline])) unset($listofdata[$idline]);
if (count($listofdata) > 0) $_SESSION['massstockmove']=dol_json_encode($listofdata);
else unset($_SESSION['massstockmove']);
}
if ($action == 'createmovements')
{
$error=0;
if (! GETPOST("label"))
{
$error++;
setEventMessage($langs->trans("ErrorFieldRequired"),$langs->transnoentitiesnoconv("LabelMovement"));
}
$db->begin();
if (! $error)
{
$product = new Product($db);
foreach($listofdata as $key => $val) // Loop on each movement to do
{
$id=$val['id'];
$id_product=$val['id_product'];
$id_sw=$val['id_sw'];
$id_tw=$val['id_tw'];
$qty=price2num($val['qty']);
if (! $error && $id_sw <> $id_tw && is_numeric($qty) && $id_product)
{
$result=$product->fetch($id_product);
$product->load_stock(); // Load array product->stock_warehouse
// Define value of products moved
$pricesrc=0;
if (isset($product->stock_warehouse[$id_sw]->pmp)) $pricesrc=$product->stock_warehouse[$id_sw]->pmp;
$pricedest=$pricesrc;
//print 'price src='.$pricesrc.', price dest='.$pricedest;exit;
// Remove stock
$result1=$product->correct_stock(
$user,
$id_sw,
$qty,
1,
GETPOST("label"),
$pricesrc
);
if ($result1 < 0)
{
$error++;
setEventMessage($product->errors,'errors');
}
// Add stock
$result2=$product->correct_stock(
$user,
$id_tw,
$qty,
0,
GETPOST("label"),
$pricedest
);
if ($result2 < 0)
{
$error++;
setEventMessage($product->errors,'errors');
}
}
else
{
dol_print_error('',"Bad value saved into sessions");
$error++;
}
}
}
if (! $error)
{
unset($_SESSION['massstockmove']);
$db->commit();
setEventMessage($langs->trans("StockMovementRecorded"),'mesgs');
header("Location: ".DOL_URL_ROOT.'/product/stock/index.php'); // Redirect to avoid pb when using back
exit;
}
else
{
$db->rollback();
setEventMessage($langs->trans("Error"),'errors');
}
}
/*
* View
*/
$now=dol_now();
$form=new Form($db);
$formproduct=new FormProduct($db);
$productstatic = new Product($db);
$warehousestatics = new Entrepot($db);
$warehousestatict = new Entrepot($db);
$title = $langs->trans('MassMovement');
llxHeader('', $title, $helpurl, '');
print_fiche_titre($langs->trans("MassStockMovement")).'
';
$titletoadd=$langs->trans("Select");
$titletoaddnoent=$langs->transnoentitiesnoconv("Select");
$buttonrecord=$langs->trans("RecordMovement");
$buttonrecordnoent=$langs->trans("RecordMovement");
print $langs->trans("SelectProductInAndOutWareHouse",$titletoaddnoent,$buttonrecordnoent).'
';
print '
'."\n";
$var=true;
// Form to add a line
print '
';
print '
';
print '';
llxFooter();
$db->close();
?>