NEW: Can create a lot from list

Make code like modulebuilder
This commit is contained in:
Laurent Destailleur 2021-02-26 12:34:41 +01:00
parent 34ed5519fd
commit 00d2d28d68
3 changed files with 174 additions and 101 deletions

View File

@ -95,7 +95,8 @@ class Productlot extends CommonObject
'datec' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501),
'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510, 'foreignkey'=>'llx_user.rowid'),
'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511)
'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511),
'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000),
);
/**

View File

@ -36,8 +36,35 @@ $langs->loadLangs(array('stocks', 'other', 'productbatch'));
// Get parameters
$id = GETPOST('id', 'int');
$action = GETPOST('action', 'alpha');
$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');
$cancel = GETPOST('cancel', 'aZ09');
$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'myobjectcard'; // To manage different context of search
$backtopage = GETPOST('backtopage', 'alpha');
// Initialize technical objects
$object = new ProductLot($db);
$extrafields = new ExtraFields($db);
$hookmanager->initHooks(array('productlotcard', 'globalcard')); // Note that conf->hooks_modules contains array
// Fetch optionals attributes and labels
$extrafields->fetch_name_optionals_label($object->table_element);
$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
// Initialize array of search criterias
$search_all = GETPOST("search_all", 'alpha');
$search = array();
foreach ($object->fields as $key => $val) {
if (GETPOST('search_'.$key, 'alpha')) {
$search[$key] = GETPOST('search_'.$key, 'alpha');
}
}
if (empty($action) && empty($id) && empty($ref)) {
$action = 'view';
}
$batch = GETPOST('batch', 'alpha');
$productid = GETPOST('productid', 'int');
$ref = GETPOST('ref', 'alpha'); // ref is productid_batch
@ -51,22 +78,6 @@ $search_import_key = GETPOST('search_import_key', 'int');
if (empty($action) && empty($id) && empty($ref)) $action = 'list';
// Protection if external user
if ($user->socid > 0)
{
//accessforbidden();
}
//$result = restrictedArea($user, 'mymodule', $id);
$object = new ProductLot($db);
$extrafields = new ExtraFields($db);
$formfile = new FormFile($db);
// fetch optionals attributes and labels
$extrafields->fetch_name_optionals_label($object->table_element);
// Load object
//include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals
if ($id || $ref)
@ -81,6 +92,15 @@ if ($id || $ref)
$object->ref = $object->batch; // For document management ( it use $object->ref)
}
// Protection if external user
if ($user->socid > 0)
{
//accessforbidden();
}
//$result = restrictedArea($user, 'mymodule', $id);
// Initialize technical object to manage hooks of modules. Note that conf->hooks_modules contains array array
$hookmanager->initHooks(array('productlotcard', 'globalcard'));
@ -93,16 +113,26 @@ $usercanread = $user->rights->produit->lire;
$usercancreate = $user->rights->produit->creer;
$usercandelete = $user->rights->produit->supprimer;
$upload_dir = $conf->productbatch->multidir_output[$conf->entity];
$permissiontoadd = $usercancreate;
/*
* Actions
*/
$parameters = array();
$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if ($reshook < 0) {
setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
}
if (empty($reshook)) {
$error = 0;
$backurlforlist = dol_buildpath('/product/stock/productlot_list.php', 1);
if (empty($reshook))
{
if ($action == 'seteatby' && $user->rights->stock->creer)
{
$newvalue = dol_mktime(12, 0, 0, $_POST['eatbymonth'], $_POST['eatbyday'], $_POST['eatbyyear']);
@ -117,6 +147,11 @@ if (empty($reshook))
if ($result < 0) dol_print_error($db, $object->error);
}
$triggermodname = 'PRODUCT_LOT_MODIFY'; // Name of trigger action code to execute when we modify record
// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
/*
if ($action == 'update_extras')
{
$object->oldcopy = dol_clone($object);
@ -152,8 +187,6 @@ if (empty($reshook))
$error = 0;
/* object_prop_getpost_prop */
$object->entity = GETPOST('entity', 'int');
$object->fk_product = GETPOST('fk_product', 'int');
$object->batch = GETPOST('batch', 'alpha');
@ -241,11 +274,15 @@ if (empty($reshook))
else setEventMessages($object->error, null, 'errors');
}
}
// Actions to build doc
$upload_dir = $conf->productbatch->multidir_output[$conf->entity];
$permissiontoadd = $usercancreate;
*/
// Action to build doc
include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
// Actions to send emails
$triggersendname = 'PRODUCT_LOT_SENTBYMAIL';
$autocopy = 'MAIN_MAIL_AUTOCOPY_PRODUCT_LOT_TO';
$trackid = 'productlot'.$object->id;
include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
}
@ -255,69 +292,98 @@ if (empty($reshook))
* View
*/
llxHeader('', 'ProductLot', '');
$form = new Form($db);
$formfile = new FormFile($db);
$title = $langs->trans("ProductLot");
$help_url = '';
llxHeader('', $title, $help_url);
// Part to create
if ($action == 'create')
{
print load_fiche_titre($langs->trans("Batch"));
if ($action == 'create') {
print load_fiche_titre($langs->trans("Batch"), '', 'object_'.$object->picto);
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="add">';
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
if ($backtopage) {
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
}
if ($backtopageforcancel) {
print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
}
print dol_get_fiche_head();
print dol_get_fiche_head(array(), '');
print '<table class="border centpercent">'."\n";
// print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td><input class="flat" type="text" size="36" name="label" value="'.$label.'"></td></tr>';
//
print '<tr><td class="fieldrequired">'.$langs->trans("Fieldfk_product").'</td><td><input class="flat" type="text" name="fk_product" value="'.GETPOST('fk_product').'"></td></tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("Fieldbatch").'</td><td><input class="flat" type="text" name="batch" value="'.GETPOST('batch').'"></td></tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("Fieldfk_user_creat").'</td><td><input class="flat" type="text" name="fk_user_creat" value="'.GETPOST('fk_user_creat').'"></td></tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("Fieldfk_user_modif").'</td><td><input class="flat" type="text" name="fk_user_modif" value="'.GETPOST('fk_user_modif').'"></td></tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("Fieldimport_key").'</td><td><input class="flat" type="text" name="import_key" value="'.GETPOST('import_key').'"></td></tr>';
// Set some default values
//if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';
print '<table class="border centpercent tableforfieldcreate">'."\n";
// Common attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
// Other attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
print '</table>'."\n";
print dol_get_fiche_end();
print '<div class="center"><input type="submit" class="button" name="add" value="'.$langs->trans("Create").'"> &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'"></div>';
print '<div class="center">';
print '<input type="submit" class="button" name="add" value="'.dol_escape_htmltag($langs->trans("Create")).'">';
print '&nbsp; ';
print '<input type="'.($backtopage ? "submit" : "button").'" class="button button-cancel" name="cancel" value="'.dol_escape_htmltag($langs->trans("Cancel")).'"'.($backtopage ? '' : ' onclick="javascript:history.go(-1)"').'>'; // Cancel for create does not post form if we don't know the backtopage
print '</div>';
print '</form>';
//dol_set_focus('input[name="ref"]');
}
// Part to show record
if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create')))
{
if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
$res = $object->fetch_optionals();
//print load_fiche_titre($langs->trans("Batch"));
$head = productlot_prepare_head($object);
print dol_get_fiche_head($head, 'card', $langs->trans("Batch"), -1, 'barcode');
print dol_get_fiche_head($head, 'card', $langs->trans("Batch"), -1, $object->picto);
$formconfirm = '';
// Confirmation to delete
if ($action == 'delete') {
$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBatch'), $langs->trans('ConfirmDeleteBatch'), 'confirm_delete', '', 0, 1);
print $formconfirm;
}
// 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 = '<a href="'.DOL_URL_ROOT.'/product/stock/productlot_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
$shownav = 1;
if ($user->socid && !in_array('batch', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
dol_banner_tab($object, 'id', $linkback, $shownav, 'rowid', 'batch');
$morehtmlref = '';
dol_banner_tab($object, 'id', $linkback, $shownav, 'rowid', 'batch', $morehtmlref);
print '<div class="fichecenter">';
print '<div class="underbanner clearboth"></div>';
print '<table class="border centpercent">'."\n";
print '<table class="border centpercent tableforfield">'."\n";
// Product
print '<tr><td class="titlefield">'.$langs->trans("Product").'</td><td>';
@ -345,44 +411,49 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
print '</td>';
print '</tr>';
}
// Other attributes
$cols = 2;
// Other attributes. Fields from hook formObjectOptions and Extrafields.
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
print '</table>';
print '</div>';
print '<div class="clearboth"></div>';
print dol_get_fiche_end();
// Buttons
print '<div class="tabsAction">'."\n";
$parameters = array();
$reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if (empty($reshook))
{
/*TODO if ($user->rights->stock->lire)
{
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit">'.$langs->trans("Modify").'</a></div>'."\n";
}
if ($user->rights->stock->supprimer)
{
print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete&amp;token='.newToken().'">'.$langs->trans('Delete').'</a></div>'."\n";
}
*/
}
print '</div>'."\n";
// Link to other lists
print '<a href="'.DOL_URL_ROOT.'/product/reassortlot.php?sref='.urlencode($producttmp->ref).'&search_batch='.urlencode($object->batch).'">'.$langs->trans("ShowCurrentStockOfLot").'</a><br>';
print '<br>';
print '<a href="'.DOL_URL_ROOT.'/product/stock/movement_list.php?search_product_ref='.urlencode($producttmp->ref).'&search_batch='.urlencode($object->batch).'">'.$langs->trans("ShowLogOfMovementIfLot").'</a><br>';
print '<br>';
// Buttons for actions
if ($action != 'presend' && $action != 'editline') {
print '<div class="tabsAction">'."\n";
$parameters = array();
$reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if (empty($reshook)) {
/*TODO if ($user->rights->stock->lire)
{
print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit">'.$langs->trans("Modify").'</a></div>'."\n";
}
if ($user->rights->stock->supprimer)
{
print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete&amp;token='.newToken().'">'.$langs->trans('Delete').'</a></div>'."\n";
}
*/
}
print '</div>'."\n";
}
}
@ -391,30 +462,32 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
* Documents generes
*/
if (empty($action))
{
if ($action != 'presend') {
print '<div class="fichecenter"><div class="fichehalfleft">';
print '<a name="builddoc"></a>'; // ancre
$includedocgeneration = 1;
// Documents
$filedir = $conf->productbatch->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 0, $object, 'product_batch').dol_sanitizeFileName($object->ref);
$urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
$genallowed = $usercanread;
$delallowed = $usercancreate;
if ($includedocgeneration) {
$objref = dol_sanitizeFileName($object->ref);
$relativepath = $objref.'/'.$objref.'.pdf';
$filedir = $conf->productbatch->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 0, $object, 'product_batch').dol_sanitizeFileName($object->ref);
$urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
$genallowed = $usercanread; // If you can read, you can build the PDF to read content
$delallowed = $usercancreate; // If you can create/edit, you can remove a file on card
print $formfile->showdocuments('product_batch', $objref, $filedir, $urlsource, $genallowed, $delallowed, '', 0, 0, 0, 28, 0, '', 0, '', $langs->default_lang, '', $object);
}
print $formfile->showdocuments('product_batch', dol_sanitizeFileName($object->ref), $filedir, $urlsource, $genallowed, $delallowed, '', 0, 0, 0, 28, 0, '', 0, '', $object->default_lang, '', $object);
$somethingshown = $formfile->numoffiles;
print '</div>';
// List of actions on element
print '<div class="fichehalfright"><div class="ficheaddleft">';
print '</div><div class="fichehalfright"><div class="ficheaddleft">';
$MAXEVENT = 10;
include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
$formactions = new FormActions($db);
$somethingshown = $formactions->showactions($object, 'productlot', $socid, 1);
print '</div></div>';
$somethingshown = $formactions->showactions($object, 'productlot', $socid, 1, '', $MAXEVENT);
print '</div>';
print '</div></div></div>';
}
// End of page

View File

@ -124,9 +124,9 @@ if (is_array($extrafields->attributes[$object->table_element]['label']) && count
$object->fields = dol_sort_array($object->fields, 'position');
$arrayfields = dol_sort_array($arrayfields, 'position');
$permissiontoread = $user->rights->stock->read;
$permissiontoadd = $user->rights->stock->write;
$permissiontodelete = $user->rights->stock->delete;
$permissiontoread = $user->rights->stock->lire;
$permissiontoadd = $user->rights->stock->mouvement->creer;
//$permissiontodelete = $user->rights->stock->supprimer;
@ -321,8 +321,7 @@ print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
//print '<input type="hidden" name="page" value="'.$page.'">';
print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
$newcardbutton = dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', dol_buildpath('/mymodule/myobject_card.php', 1).'?action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
$newcardbutton = '';
$newcardbutton = dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', dol_buildpath('/product/stock/productlot_card.php', 1).'?action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'object_'.$object->picto, 0, $newcardbutton, '', $limit, 0, 0, 1);