From e70604c48582bba174c886b60590392b0d0457fe Mon Sep 17 00:00:00 2001 From: Salvatore Chiariello Date: Fri, 28 Jan 2022 14:57:36 +0100 Subject: [PATCH] NEW|New #17123 added ExtraFields for Stock Mouvement --- htdocs/core/class/extrafields.class.php | 3 - htdocs/core/lib/stock.lib.php | 5 + .../llx_stock_mouvement_extrafields.sql | 24 ++++ htdocs/langs/en_US/products.lang | 1 + htdocs/langs/it_IT/products.lang | 1 + .../admin/stock_mouvement_extrafields.php | 126 ++++++++++++++++++ htdocs/product/class/product.class.php | 14 +- htdocs/product/stock/movement_list.php | 36 ++++- .../product/stock/tpl/extrafields_add.tpl.php | 52 ++++++++ .../product/stock/tpl/stockcorrection.tpl.php | 3 + .../product/stock/tpl/stocktransfer.tpl.php | 3 + 11 files changed, 257 insertions(+), 11 deletions(-) create mode 100644 htdocs/install/mysql/tables/llx_stock_mouvement_extrafields.sql create mode 100644 htdocs/product/admin/stock_mouvement_extrafields.php create mode 100644 htdocs/product/stock/tpl/extrafields_add.tpl.php diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 4c0ec1ff5a5..a6f09493a74 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -804,9 +804,6 @@ class ExtraFields if ($elementtype == 'order_supplier') { $elementtype = 'commande_fournisseur'; } - if ($elementtype == 'stock_mouvement') { - $elementtype = 'movement'; - } $array_name_label = array(); diff --git a/htdocs/core/lib/stock.lib.php b/htdocs/core/lib/stock.lib.php index 2e58e6c8082..066bf2165be 100644 --- a/htdocs/core/lib/stock.lib.php +++ b/htdocs/core/lib/stock.lib.php @@ -109,6 +109,11 @@ function stock_admin_prepare_head() $head[$h][2] = 'attributes'; $h++; + $head[$h][0] = DOL_URL_ROOT.'/product/admin/stock_mouvement_extrafields.php'; + $head[$h][1] = $langs->trans("StockMouvementExtraFields"); + $head[$h][2] = 'stockMouvementAttributes'; + $h++; + complete_head_from_modules($conf, $langs, null, $head, $h, 'stock_admin', 'remove'); return $head; diff --git a/htdocs/install/mysql/tables/llx_stock_mouvement_extrafields.sql b/htdocs/install/mysql/tables/llx_stock_mouvement_extrafields.sql new file mode 100644 index 00000000000..c13218b631e --- /dev/null +++ b/htdocs/install/mysql/tables/llx_stock_mouvement_extrafields.sql @@ -0,0 +1,24 @@ +-- =================================================================== +-- Copyright (C) 2022 Salvatore Chiariello +-- +-- 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 . +-- +-- =================================================================== + +CREATE TABLE llx_stock_mouvement_extrafields ( + rowid integer AUTO_INCREMENT PRIMARY KEY, + tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + fk_object integer NOT NULL, + import_key varchar(14) +)ENGINE=innodb; \ No newline at end of file diff --git a/htdocs/langs/en_US/products.lang b/htdocs/langs/en_US/products.lang index a8942bd337f..14715670882 100644 --- a/htdocs/langs/en_US/products.lang +++ b/htdocs/langs/en_US/products.lang @@ -410,3 +410,4 @@ DefaultBOMDesc=The default BOM recommended to use to manufacture this product. T Rank=Rank SwitchOnSaleStatus=Switch on sale status SwitchOnPurchaseStatus=Switch on purchase status +StockMouvementExtraFields= Extra Fields (stock mouvement) diff --git a/htdocs/langs/it_IT/products.lang b/htdocs/langs/it_IT/products.lang index f33ca8759bf..f5b9043b4e8 100644 --- a/htdocs/langs/it_IT/products.lang +++ b/htdocs/langs/it_IT/products.lang @@ -410,3 +410,4 @@ DefaultBOMDesc=The default BOM recommended to use to manufacture this product. T Rank=Rank SwitchOnSaleStatus=Switch on sale status SwitchOnPurchaseStatus=Switch on purchase status +StockMouvementExtraFields= Campi extra (movimenti scorte) diff --git a/htdocs/product/admin/stock_mouvement_extrafields.php b/htdocs/product/admin/stock_mouvement_extrafields.php new file mode 100644 index 00000000000..0369c397e22 --- /dev/null +++ b/htdocs/product/admin/stock_mouvement_extrafields.php @@ -0,0 +1,126 @@ + + * Copyright (C) 2003 Jean-Louis Bergamo + * Copyright (C) 2004-2011 Laurent Destailleur + * Copyright (C) 2012 Regis Houssin + * Copyright (C) 2014 Florian Henry + * Copyright (C) 2015 Jean-François Ferry + * + * 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/admin/stock_mouvement_extrafields.php + * \ingroup stock + * \brief Page to setup extra fields of stock mouvement + */ + +// Load Dolibarr environment +$res = 0; +// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined) +if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php"; +// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME +$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1; +while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) { $i--; $j--; } +if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php"; +if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php"; +// Try main.inc.php using relative path +if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php"; +if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php"; +if (!$res) die("Include of main fails"); + +require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/stock.lib.php'; + +// Load translation files required by the page +$langs->loadLangs(array('stock@stock', 'admin')); + +$extrafields = new ExtraFields($db); +$form = new Form($db); + +// List of supported format +$tmptype2label = ExtraFields::$type2label; +$type2label = array(''); +foreach ($tmptype2label as $key => $val) $type2label[$key] = $langs->transnoentitiesnoconv($val); + +$action = GETPOST('action', 'aZ09'); +$attrname = GETPOST('attrname', 'alpha'); +$elementtype = 'stock_mouvement'; //Must be the $table_element of the class that manage extrafield + +if (!$user->admin) accessforbidden(); + + +/* + * Actions + */ + +require DOL_DOCUMENT_ROOT.'/core/actions_extrafields.inc.php'; + + + +/* + * View + */ + + +llxHeader('', $langs->trans("StockSetup"), $help_url); + + +$linkback = ''.$langs->trans("BackToModuleList").''; +print load_fiche_titre($langs->trans("StockSetup"), $linkback, 'title_setup'); + + +$head = stock_admin_prepare_head(); + +print dol_get_fiche_head($head, 'stockMouvementAttributes', $langs->trans("StockMouvementExtraFields"), -1, 'account'); + +require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields_view.tpl.php'; + +print dol_get_fiche_end(); + + +// Buttons +if ($action != 'create' && $action != 'edit') +{ + print '
'; + print "".$langs->trans("NewAttribute").""; + print "
"; +} + + +/* + * Creation of an optional field + */ +if ($action == 'create') +{ + print '
'; + print load_fiche_titre($langs->trans('NewAttribute')); + + require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields_add.tpl.php'; +} + +/* + * Edition of an optional field + */ +if ($action == 'edit' && !empty($attrname)) +{ + print "
"; + print load_fiche_titre($langs->trans("FieldEdition", $attrname)); + + require DOL_DOCUMENT_ROOT.'/core/tpl/admin_extrafields_edit.tpl.php'; +} + +// End of page +llxFooter(); +$db->close(); diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 27b2bdaff2c..c141c9ab526 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -5122,7 +5122,7 @@ class Product extends CommonObject * @param int $disablestockchangeforsubproduct Disable stock change for sub-products of kit (usefull only if product is a subproduct) * @return int <0 if KO, >0 if OK */ - public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label = '', $price = 0, $inventorycode = '', $origin_element = '', $origin_id = null, $disablestockchangeforsubproduct = 0) + public function correct_stock($user, $id_entrepot, $nbpiece, $movement, $label = '', $price = 0, $inventorycode = '', $origin_element = '', $origin_id = null, $disablestockchangeforsubproduct = 0,$extrafields = null) { // phpcs:enable if ($id_entrepot) { @@ -5145,6 +5145,11 @@ class Product extends CommonObject $result = $movementstock->_create($user, $this->id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode, '', '', '', '', false, 0, $disablestockchangeforsubproduct); if ($result >= 0) { + if($extrafields){ + $array_options = $extrafields->getOptionalsFromPost('stock_mouvement'); + $movementstock->array_options = $array_options; + $movementstock->insertExtraFields(); + } $this->db->commit(); return 1; } else { @@ -5176,7 +5181,7 @@ class Product extends CommonObject * @param int $disablestockchangeforsubproduct Disable stock change for sub-products of kit (usefull only if product is a subproduct) * @return int <0 if KO, >0 if OK */ - public function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label = '', $price = 0, $dlc = '', $dluo = '', $lot = '', $inventorycode = '', $origin_element = '', $origin_id = null, $disablestockchangeforsubproduct = 0) + public function correct_stock_batch($user, $id_entrepot, $nbpiece, $movement, $label = '', $price = 0, $dlc = '', $dluo = '', $lot = '', $inventorycode = '', $origin_element = '', $origin_id = null, $disablestockchangeforsubproduct = 0,$extrafields = null) { // phpcs:enable if ($id_entrepot) { @@ -5199,6 +5204,11 @@ class Product extends CommonObject $result = $movementstock->_create($user, $this->id, $id_entrepot, $op[$movement], $movement, $price, $label, $inventorycode, '', $dlc, $dluo, $lot, false, 0, $disablestockchangeforsubproduct); if ($result >= 0) { + if($extrafields){ + $array_options = $extrafields->getOptionalsFromPost('stock_mouvement'); + $movementstock->array_options = $array_options; + $movementstock->insertExtraFields(); + } $this->db->commit(); return 1; } else { diff --git a/htdocs/product/stock/movement_list.php b/htdocs/product/stock/movement_list.php index 405528a3480..73c6751262a 100644 --- a/htdocs/product/stock/movement_list.php +++ b/htdocs/product/stock/movement_list.php @@ -139,6 +139,9 @@ $arrayfields = array( //'m.datec'=>array('label'=>"DateCreation", 'checked'=>0, 'position'=>500), //'m.tms'=>array('label'=>"DateModificationShort", 'checked'=>0, 'position'=>500) ); + +include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php'; + if (!empty($conf->global->PRODUCT_DISABLE_SELLBY)) { unset($arrayfields['pl.sellby']); } @@ -381,7 +384,9 @@ if ($action == "correct_stock") { $batch, GETPOST('inventorycode', 'alphanohtml'), $origin_element, - $origin_id + $origin_id, + 0, + $extrafields ); // We do not change value of stock for a correction } else { $result = $product->correct_stock( @@ -393,7 +398,9 @@ if ($action == "correct_stock") { price2num(GETPOST('unitprice'), 'MT'), GETPOST('inventorycode', 'alphanohtml'), $origin_element, - $origin_id + $origin_id, + 0, + $extrafields ); // We do not change value of stock for a correction } @@ -500,7 +507,11 @@ if ($action == "transfert_stock" && !$cancel) { $eatby, $sellby, $batch, - GETPOST('inventorycode') + GETPOST('inventorycode'), + '', + null, + 0, + $extrafields ); // Add stock $result2 = $product->correct_stock_batch( @@ -513,7 +524,11 @@ if ($action == "transfert_stock" && !$cancel) { $eatby, $sellby, $batch, - GETPOST('inventorycode', 'alphanohtml') + GETPOST('inventorycode', 'alphanohtml'), + '', + null, + 0, + $extrafields ); } } else { @@ -525,7 +540,11 @@ if ($action == "transfert_stock" && !$cancel) { 1, GETPOST("label", 'san_alpha'), $pricesrc, - GETPOST('inventorycode', 'alphanohtml') + GETPOST('inventorycode', 'alphanohtml'), + '', + null, + 0, + $extrafields ); // Add stock @@ -536,7 +555,11 @@ if ($action == "transfert_stock" && !$cancel) { 0, GETPOST("label", 'san_alpha'), $pricedest, - GETPOST('inventorycode', 'alphanohtml') + GETPOST('inventorycode', 'alphanohtml'), + '', + null, + 0, + $extrafields ); } if (!$error && $result1 >= 0 && $result2 >= 0) { @@ -1434,6 +1457,7 @@ while ($i < ($limit ? min($num, $limit) : $num)) { } // Extra fields + $object = $movement; include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php'; // Fields from hook $parameters = array('arrayfields'=>$arrayfields, 'object'=>$object, 'obj'=>$obj, 'i'=>$i, 'totalarray'=>&$totalarray); diff --git a/htdocs/product/stock/tpl/extrafields_add.tpl.php b/htdocs/product/stock/tpl/extrafields_add.tpl.php new file mode 100644 index 00000000000..9cf61564ebe --- /dev/null +++ b/htdocs/product/stock/tpl/extrafields_add.tpl.php @@ -0,0 +1,52 @@ + + * Copyright (C) 2014 Juanjo Menent + * + * 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 . + * + * Need to have following variables defined: + * $object (invoice, order, ...) + * $action + * $conf + * $langs + * + * $parameters + * $cols + */ + +// Protection to avoid direct call of template +if (empty($conf) || !is_object($conf)) +{ + print "Error, template page can't be called as URL"; + exit; +} + +?> + +executeHooks('formObjectOptions', $parameters, $movement, $action); // Note that $action and $object may have been modified by hook +print $hookmanager->resPrint; +if (empty($reshook)) { + $params = array(); + if (isset($tpl_context)) $params['tpl_context'] = $tpl_context; + $params['cols'] = $parameters['colspanvalue']; + print $movement->showOptionals($extrafields, 'create', $params); +} + +?> + diff --git a/htdocs/product/stock/tpl/stockcorrection.tpl.php b/htdocs/product/stock/tpl/stockcorrection.tpl.php index 00699b95e18..eb84df7d14e 100644 --- a/htdocs/product/stock/tpl/stockcorrection.tpl.php +++ b/htdocs/product/stock/tpl/stockcorrection.tpl.php @@ -179,6 +179,9 @@ print '