dolibarr  13.0.2
productlot_card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2018 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2018 All-3kcis <contact@all-3kcis.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
26 require '../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array('stocks', 'other', 'productbatch'));
36 
37 // Get parameters
38 $id = GETPOST('id', 'int');
39 $action = GETPOST('action', 'alpha');
40 $backtopage = GETPOST('backtopage', 'alpha');
41 $batch = GETPOST('batch', 'alpha');
42 $productid = GETPOST('productid', 'int');
43 $ref = GETPOST('ref', 'alpha'); // ref is productid_batch
44 
45 $search_entity = GETPOST('search_entity', 'int');
46 $search_fk_product = GETPOST('search_fk_product', 'int');
47 $search_batch = GETPOST('search_batch', 'alpha');
48 $search_fk_user_creat = GETPOST('search_fk_user_creat', 'int');
49 $search_fk_user_modif = GETPOST('search_fk_user_modif', 'int');
50 $search_import_key = GETPOST('search_import_key', 'int');
51 
52 if (empty($action) && empty($id) && empty($ref)) $action = 'list';
53 
54 
55 // Protection if external user
56 if ($user->socid > 0)
57 {
58  //accessforbidden();
59 }
60 //$result = restrictedArea($user, 'mymodule', $id);
61 
62 
63 $object = new ProductLot($db);
64 $extrafields = new ExtraFields($db);
65 $formfile = new FormFile($db);
66 
67 // fetch optionals attributes and labels
68 $extrafields->fetch_name_optionals_label($object->table_element);
69 
70 // Load object
71 //include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals
72 if ($id || $ref)
73 {
74  if ($ref)
75  {
76  $tmp = explode('_', $ref);
77  $productid = $tmp[0];
78  $batch = $tmp[1];
79  }
80  $object->fetch($id, $productid, $batch);
81  $object->ref = $object->batch; // For document management ( it use $object->ref)
82 }
83 
84 // Initialize technical object to manage hooks of modules. Note that conf->hooks_modules contains array array
85 $hookmanager->initHooks(array('productlotcard', 'globalcard'));
86 
87 
88 $permissionnote = $user->rights->stock->creer; // Used by the include of actions_setnotes.inc.php
89 $permissiondellink = $user->rights->stock->creer; // Used by the include of actions_dellink.inc.php
90 $permissiontoadd = $user->rights->stock->creer; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
91 
92 $usercanread = $user->rights->produit->lire;
93 $usercancreate = $user->rights->produit->creer;
94 $usercandelete = $user->rights->produit->supprimer;
95 
96 /*
97  * Actions
98  */
99 
100 $parameters = array();
101 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
102 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
103 
104 if (empty($reshook))
105 {
106  if ($action == 'seteatby' && $user->rights->stock->creer) {
107  $newvalue = dol_mktime(12, 0, 0, GETPOST('eatbymonth', 'int'), GETPOST('eatbyday', 'int'), GETPOST('eatbyyear', 'int'));
108  $result = $object->setValueFrom('eatby', $newvalue, '', null, 'date', '', $user, 'PRODUCTLOT_MODIFY');
109  if ($result < 0) {
110  setEventMessages($object->error, null, 'errors');
111  $action == 'editeatby';
112  } else {
113  $action = 'view';
114  }
115  }
116 
117  if ($action == 'setsellby' && $user->rights->stock->creer) {
118  $newvalue = dol_mktime(12, 0, 0, GETPOST('sellbymonth', 'int'), GETPOST('sellbyday', 'int'), GETPOST('sellbyyear', 'int'));
119  $result = $object->setValueFrom('sellby', $newvalue, '', null, 'date', '', $user, 'PRODUCTLOT_MODIFY');
120  if ($result < 0) {
121  setEventMessages($object->error, null, 'errors');
122  $action == 'editsellby';
123  } else {
124  $action = 'view';
125  }
126  }
127 
128  if ($action == 'update_extras')
129  {
130  $object->oldcopy = dol_clone($object);
131 
132  // Fill array 'array_options' with data from update form
133  $ret = $extrafields->setOptionalsFromPost(null, $object, GETPOST('attribute', 'restricthtml'));
134  if ($ret < 0) $error++;
135 
136  if (!$error)
137  {
138  // Actions on extra fields
139  $result = $object->insertExtraFields('PRODUCT_LOT_MODIFY');
140  if ($result < 0)
141  {
142  setEventMessages($object->error, $object->errors, 'errors');
143  $error++;
144  }
145  }
146 
147  if ($error) {
148  $action = 'edit_extras';
149  }
150  }
151 
152  // Action to add record
153  if ($action == 'add')
154  {
155  if (GETPOST('cancel', 'alpha'))
156  {
157  $urltogo = $backtopage ? $backtopage : dol_buildpath('/stock/list.php', 1);
158  header("Location: ".$urltogo);
159  exit;
160  }
161 
162  $error = 0;
163 
164  /* object_prop_getpost_prop */
165 
166  $object->entity = GETPOST('entity', 'int');
167  $object->fk_product = GETPOST('fk_product', 'int');
168  $object->batch = GETPOST('batch', 'alpha');
169  $object->fk_user_creat = GETPOST('fk_user_creat', 'int');
170  $object->fk_user_modif = GETPOST('fk_user_modif', 'int');
171  $object->import_key = GETPOST('import_key', 'int');
172 
173  if (empty($object->ref))
174  {
175  $error++;
176  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")), null, 'errors');
177  }
178 
179  if (!$error)
180  {
181  $result = $object->create($user);
182  if ($result > 0)
183  {
184  // Creation OK
185  $urltogo = $backtopage ? $backtopage : dol_buildpath('/stock/list.php', 1);
186  header("Location: ".$urltogo);
187  exit;
188  }
189  {
190  // Creation KO
191  if (!empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
192  else setEventMessages($object->error, null, 'errors');
193  $action = 'create';
194  }
195  } else {
196  $action = 'create';
197  }
198  }
199 
200  // Cancel
201  if ($action == 'update' && GETPOST('cancel', 'alpha')) $action = 'view';
202 
203  // Action to update record
204  if ($action == 'update' && !GETPOST('cancel', 'alpha'))
205  {
206  $error = 0;
207 
208  $object->entity = GETPOST('entity', 'int');
209  $object->fk_product = GETPOST('fk_product', 'int');
210  $object->batch = GETPOST('batch', 'alpha');
211  $object->fk_user_creat = GETPOST('fk_user_creat', 'int');
212  $object->fk_user_modif = GETPOST('fk_user_modif', 'int');
213  $object->import_key = GETPOST('import_key', 'int');
214 
215  if (empty($object->ref))
216  {
217  $error++;
218  setEventMessages($langs->transnoentitiesnoconv("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")), null, 'errors');
219  }
220 
221  if (!$error)
222  {
223  $result = $object->update($user);
224  if ($result > 0)
225  {
226  $action = 'view';
227  } else {
228  // Creation KO
229  if (!empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
230  else setEventMessages($object->error, null, 'errors');
231  $action = 'edit';
232  }
233  } else {
234  $action = 'edit';
235  }
236  }
237 
238  // Action to delete
239  if ($action == 'confirm_delete')
240  {
241  $result = $object->delete($user);
242  if ($result > 0)
243  {
244  // Delete OK
245  setEventMessages("RecordDeleted", null, 'mesgs');
246  header("Location: ".dol_buildpath('/stock/list.php', 1));
247  exit;
248  } else {
249  if (!empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
250  else setEventMessages($object->error, null, 'errors');
251  }
252  }
253 
254  // Actions to build doc
255  $upload_dir = $conf->productbatch->multidir_output[$conf->entity];
256  $permissiontoadd = $usercancreate;
257  include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
258 }
259 
260 
261 
262 
263 /*
264  * View
265  */
266 
267 llxHeader('', 'ProductLot', '');
268 
269 $form = new Form($db);
270 
271 
272 // Part to create
273 if ($action == 'create')
274 {
275  print load_fiche_titre($langs->trans("Batch"));
276 
277  print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
278  print '<input type="hidden" name="action" value="add">';
279  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
280 
282 
283  print '<table class="border centpercent">'."\n";
284  // print '<tr><td class="fieldrequired">'.$langs->trans("Label").'</td><td><input class="flat" type="text" size="36" name="label" value="'.$label.'"></td></tr>';
285  //
286  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>';
287  print '<tr><td class="fieldrequired">'.$langs->trans("Fieldbatch").'</td><td><input class="flat" type="text" name="batch" value="'.GETPOST('batch').'"></td></tr>';
288  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>';
289  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>';
290  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>';
291 
292  print '</table>'."\n";
293 
295 
296  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>';
297 
298  print '</form>';
299 }
300 
301 
302 // Part to show record
303 if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create')))
304 {
305  $res = $object->fetch_optionals();
306 
307  //print load_fiche_titre($langs->trans("Batch"));
308 
309  $head = productlot_prepare_head($object);
310  print dol_get_fiche_head($head, 'card', $langs->trans("Batch"), -1, 'barcode');
311 
312 
313  if ($action == 'delete') {
314  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBatch'), $langs->trans('ConfirmDeleteBatch'), 'confirm_delete', '', 0, 1);
315  print $formconfirm;
316  }
317 
318 
319  $linkback = '<a href="'.DOL_URL_ROOT.'/product/stock/productlot_list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
320 
321  $shownav = 1;
322  if ($user->socid && !in_array('batch', explode(',', $conf->global->MAIN_MODULES_FOR_EXTERNAL))) $shownav = 0;
323 
324  dol_banner_tab($object, 'id', $linkback, $shownav, 'rowid', 'batch');
325 
326  print '<div class="fichecenter">';
327  print '<div class="underbanner clearboth"></div>';
328 
329  print '<table class="border centpercent">'."\n";
330 
331  // Product
332  print '<tr><td class="titlefield">'.$langs->trans("Product").'</td><td>';
333  $producttmp = new Product($db);
334  $producttmp->fetch($object->fk_product);
335  print $producttmp->getNomUrl(1, 'stock')." - ".$producttmp->label;
336  print '</td></tr>';
337 
338  // Eat by
339  if (empty($conf->global->PRODUCT_DISABLE_EATBY)) {
340  print '<tr><td>';
341  print $form->editfieldkey($langs->trans('EatByDate'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
342  print '</td><td>';
343  print $form->editfieldval($langs->trans('EatByDate'), 'eatby', $object->eatby, $object, $user->rights->stock->creer, 'datepicker');
344  print '</td>';
345  print '</tr>';
346  }
347 
348  // Sell by
349  if (empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
350  print '<tr><td>';
351  print $form->editfieldkey($langs->trans('SellByDate'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
352  print '</td><td>';
353  print $form->editfieldval($langs->trans('SellByDate'), 'sellby', $object->sellby, $object, $user->rights->stock->creer, 'datepicker');
354  print '</td>';
355  print '</tr>';
356  }
357  // Other attributes
358  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
359 
360  print '</table>';
361 
362  print '</div>';
363 
365 
366 
367  // Buttons
368  print '<div class="tabsAction">'."\n";
369  $parameters = array();
370  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
371  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
372 
373  if (empty($reshook))
374  {
375  /*TODO if ($user->rights->stock->lire)
376  {
377  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";
378  }
379 
380  if ($user->rights->stock->supprimer)
381  {
382  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";
383  }
384  */
385  }
386  print '</div>'."\n";
387 
388 
389  print '<a href="'.DOL_URL_ROOT.'/product/reassortlot.php?sref='.urlencode($producttmp->ref).'&search_batch='.urlencode($object->batch).'">'.$langs->trans("ShowCurrentStockOfLot").'</a><br>';
390  print '<br>';
391  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>';
392 
393  print '<br>';
394 }
395 
396 
397 
398 /*
399  * Documents generes
400  */
401 
402 if (empty($action) || $action == 'view')
403 {
404  print '<div class="fichecenter"><div class="fichehalfleft">';
405  print '<a name="builddoc"></a>'; // ancre
406 
407  // Documents
408  $filedir = $conf->productbatch->multidir_output[$object->entity].'/'.get_exdir(0, 0, 0, 0, $object, 'product_batch');
409  $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
410  $genallowed = $usercanread;
411  $delallowed = $usercancreate;
412 
413  print $formfile->showdocuments('product_batch', dol_sanitizeFileName($object->ref), $filedir, $urlsource, $genallowed, $delallowed, '', 0, 0, 0, 28, 0, '', 0, '', $object->default_lang, '', $object);
414  $somethingshown = $formfile->numoffiles;
415 
416  print '</div>';
417 }
418 
419 // End of page
420 llxFooter();
421 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
productlot_prepare_head($object)
Prepare array with list of tabs.
Class to manage products or services.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for properties) With native = 0: P...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
llxHeader()
Empty header.
Definition: wrapper.php:45
Class to manage standard extra fields.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
Class to offer components to list and upload files.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59