*
* 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
\n";
if ($stock_movement_qty != 0) {
if ($stock_movement_qty < 0) {
$movement_type = 1;
} else {
$movement_type = 0;
}
$datemovement = '';
//$inventorycode = 'INV'.$object->id;
$inventorycode = 'INV-'.$object->ref;
$price = 0;
if (!empty($line->pmp_real) && getDolGlobalString('INVENTORY_MANAGE_REAL_PMP')) {
$price = $line->pmp_real;
}
$idstockmove = $stockmovment->_create($user, $line->fk_product, $line->fk_warehouse, $stock_movement_qty, $movement_type, $price, $langs->trans('LabelOfInventoryMovemement', $object->ref), $inventorycode, $datemovement, '', '', $line->batch);
if ($idstockmove < 0) {
$error++;
setEventMessages($stockmovment->error, $stockmovment->errors, 'errors');
break;
}
// Update line with id of stock movement (and the start quantity if it has changed this last recording)
$sqlupdate = "UPDATE ".MAIN_DB_PREFIX."inventorydet";
$sqlupdate .= " SET fk_movement = ".((int) $idstockmove);
if ($qty_stock != $realqtynow) {
$sqlupdate .= ", qty_stock = ".((float) $realqtynow);
}
$sqlupdate .= " WHERE rowid = ".((int) $line->rowid);
$resqlupdate = $db->query($sqlupdate);
if (! $resqlupdate) {
$error++;
setEventMessages($db->lasterror(), null, 'errors');
break;
}
}
if (!empty($line->pmp_real) && getDolGlobalString('INVENTORY_MANAGE_REAL_PMP')) {
$sqlpmp = 'UPDATE '.MAIN_DB_PREFIX.'product SET pmp = '.((float) $line->pmp_real).' WHERE rowid = '.((int) $line->fk_product);
$resqlpmp = $db->query($sqlpmp);
if (! $resqlpmp) {
$error++;
setEventMessages($db->lasterror(), null, 'errors');
break;
}
if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
$sqlpmp = 'UPDATE '.MAIN_DB_PREFIX.'product_perentity SET pmp = '.((float) $line->pmp_real).' WHERE fk_product = '.((int) $line->fk_product).' AND entity='.$conf->entity;
$resqlpmp = $db->query($sqlpmp);
if (! $resqlpmp) {
$error++;
setEventMessages($db->lasterror(), null, 'errors');
break;
}
}
}
}
$i++;
}
if (!$error) {
$object->setRecorded($user);
}
} else {
setEventMessages($db->lasterror, null, 'errors');
$error++;
}
if (! $error) {
$db->commit();
} else {
$db->rollback();
}
}
// Save quantity found during inventory (when we click on Save button on inventory page)
if ($action == 'updateinventorylines' && $permissiontoadd) {
$sql = 'SELECT id.rowid, id.datec as date_creation, id.tms as date_modification, id.fk_inventory, id.fk_warehouse,';
$sql .= ' id.fk_product, id.batch, id.qty_stock, id.qty_view, id.qty_regulated';
$sql .= ' FROM '.MAIN_DB_PREFIX.'inventorydet as id';
$sql .= ' WHERE id.fk_inventory = '.((int) $object->id);
$sql .= $db->order('id.rowid', 'ASC');
$sql .= $db->plimit($limit, $offset);
$db->begin();
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
$i = 0;
$totalarray = array();
$inventoryline = new InventoryLine($db);
while ($i < $num) {
$line = $db->fetch_object($resql);
$lineid = $line->rowid;
$result = 0;
$resultupdate = 0;
if (GETPOST("id_".$lineid, 'alpha') != '') { // If a value was set ('0' or something else)
$qtytoupdate = price2num(GETPOST("id_".$lineid, 'alpha'), 'MS');
$result = $inventoryline->fetch($lineid);
if ($qtytoupdate < 0) {
$result = -1;
setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors');
}
if ($result > 0) {
$inventoryline->qty_stock = price2num(GETPOST('stock_qty_'.$lineid, 'alpha'), 'MS'); // The new value that was set in as hidden field
$inventoryline->qty_view = $qtytoupdate; // The new value we want
$inventoryline->pmp_real = price2num(GETPOST('realpmp_'.$lineid, 'alpha'), 'MS');
$inventoryline->pmp_expected = price2num(GETPOST('expectedpmp_'.$lineid, 'alpha'), 'MS');
$resultupdate = $inventoryline->update($user);
}
} elseif (GETPOSTISSET('id_' . $lineid)) {
// Delete record
$result = $inventoryline->fetch($lineid);
if ($result > 0) {
$inventoryline->qty_view = null; // The new value we want
$inventoryline->pmp_real = price2num(GETPOST('realpmp_'.$lineid, 'alpha'), 'MS');
$inventoryline->pmp_expected = price2num(GETPOST('expectedpmp_'.$lineid, 'alpha'), 'MS');
$resultupdate = $inventoryline->update($user);
}
}
if ($result < 0 || $resultupdate < 0) {
$error++;
}
$i++;
}
}
// Update line with id of stock movement (and the start quantity if it has changed this last recording)
if (! $error) {
$sqlupdate = "UPDATE ".MAIN_DB_PREFIX."inventory";
$sqlupdate .= " SET fk_user_modif = ".((int) $user->id);
$sqlupdate .= " WHERE rowid = ".((int) $object->id);
$resqlupdate = $db->query($sqlupdate);
if (! $resqlupdate) {
$error++;
setEventMessages($db->lasterror(), null, 'errors');
}
}
if (!$error) {
$db->commit();
} else {
$db->rollback();
}
}
$backurlforlist = DOL_URL_ROOT.'/product/inventory/list.php';
$backtopage = DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id.'&page='.$page.$paramwithsearch;
// Actions cancel, add, update, delete or clone
include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
// Actions when linking object each other
include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
// Actions when printing a doc from card
include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
// Actions to send emails
/*$triggersendname = 'MYOBJECT_SENTBYMAIL';
$autocopy='MAIN_MAIL_AUTOCOPY_MYOBJECT_TO';
$trackid='stockinv'.$object->id;
include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';*/
if (GETPOST('addline', 'alpha')) {
$qty = (GETPOST('qtytoadd') != '' ? price2num(GETPOST('qtytoadd', 'MS')) : null);
if ($fk_warehouse <= 0) {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
}
if ($fk_product <= 0) {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors');
}
if (price2num(GETPOST('qtytoadd'), 'MS') < 0) {
$error++;
setEventMessages($langs->trans("FieldCannotBeNegative", $langs->transnoentitiesnoconv("RealQty")), null, 'errors');
}
if (!$error && isModEnabled('productbatch')) {
$tmpproduct = new Product($db);
$result = $tmpproduct->fetch($fk_product);
if (empty($error) && $tmpproduct->status_batch > 0 && empty($batch)) {
$error++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorProductNeedBatchNumber", $tmpproduct->ref), null, 'errors');
}
if (empty($error) && $tmpproduct->status_batch == 2 && !empty($batch) && $qty > 1) {
$error++;
$langs->load("errors");
setEventMessages($langs->trans("TooManyQtyForSerialNumber", $tmpproduct->ref, $batch), null, 'errors');
}
if (empty($error) && empty($tmpproduct->status_batch) && !empty($batch)) {
$error++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorProductDoesNotNeedBatchNumber", $tmpproduct->ref), null, 'errors');
}
}
if (!$error) {
$tmp = new InventoryLine($db);
$tmp->fk_inventory = $object->id;
$tmp->fk_warehouse = $fk_warehouse;
$tmp->fk_product = $fk_product;
$tmp->batch = $batch;
$tmp->datec = $now;
$tmp->qty_view = $qty;
$result = $tmp->create($user);
if ($result < 0) {
if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$langs->load("errors");
setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
} else {
dol_print_error($db, $tmp->error, $tmp->errors);
}
} else {
// Clear var
$_POST['batch'] = '';
$_POST['qtytoadd'] = '';
}
}
}
}
/*
* View
*/
$form = new Form($db);
$formproduct = new FormProduct($db);
$help_url = '';
llxHeader('', $langs->trans('Inventory'), $help_url);
// Part to show record
if ($object->id <= 0) {
dol_print_error(null, 'Bad value for object id');
exit;
}
$res = $object->fetch_optionals();
$head = inventoryPrepareHead($object);
print dol_get_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
$formconfirm = '';
// Confirmation to delete
if ($action == 'delete') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteInventory'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 1);
}
// Confirmation to delete line
if ($action == 'deleteline') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid.'&page='.$page.$paramwithsearch, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
}
// Clone confirmation
if ($action == 'clone') {
// Create an array for form
$formquestion = array();
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneMyObject', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
}
// Confirmation to close
if ($action == 'record') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&page='.$page.$paramwithsearch, $langs->trans('Close'), $langs->trans('ConfirmFinish'), 'update', '', 0, 1);
$action = 'view';
}
// Confirmation to close
if ($action == 'confirm_cancel') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('Cancel'), $langs->trans('ConfirmCancel'), 'cancel_record', '', 0, 1);
$action = 'view';
}
if ($action == 'validate') {
$form = new Form($db);
$formquestion = '';
if (getDolGlobalInt('INVENTORY_INCLUDE_SUB_WAREHOUSE') && !empty($object->fk_warehouse)) {
$formquestion = array(
array('type' => 'checkbox', 'name' => 'include_sub_warehouse', 'label' => $langs->trans("IncludeSubWarehouse"), 'value' => 1, 'size' => '10'),
);
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateInventory'), $langs->trans('IncludeSubWarehouseExplanation'), 'confirm_validate', $formquestion, '', 1);
}
}
// Call Hook formConfirm
$parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
$reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
if (empty($reshook)) {
$formconfirm .= $hookmanager->resPrint;
} elseif ($reshook > 0) {
$formconfirm = $hookmanager->resPrint;
}
// Print form confirm
print $formconfirm;
// Object card
// ------------------------------------------------------------
$linkback = ''.$langs->trans("BackToList").'';
$morehtmlref = '
| '.$langs->trans("InventoryCode").' | INV'.$object->id.' |