dolibarr  13.0.2
massstockmove.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2020 Laurent Destaileur <ely@users.sourceforge.net>
3  * Copyright (C) 2014 Regis Houssin <regis.houssin@inodbox.com>
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.'/product/class/product.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array('products', 'stocks', 'orders', 'productbatch'));
36 
37 //init Hook
38 $hookmanager->initHooks(array('massstockmove'));
39 
40 // Security check
41 if ($user->socid) {
42  $socid = $user->socid;
43 }
44 $result = restrictedArea($user, 'produit|service');
45 
46 //checks if a product has been ordered
47 
48 $action = GETPOST('action', 'aZ09');
49 $id_product = GETPOST('productid', 'int');
50 $id_sw = GETPOST('id_sw', 'int');
51 $id_tw = GETPOST('id_tw', 'int');
52 $batch = GETPOST('batch');
53 $qty = GETPOST('qty');
54 $idline = GETPOST('idline');
55 
56 $sortfield = GETPOST('sortfield', 'aZ09comma');
57 $sortorder = GETPOST('sortorder', 'aZ09comma');
58 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
59 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
60 
61 if (!$sortfield) {
62  $sortfield = 'p.ref';
63 }
64 
65 if (!$sortorder) {
66  $sortorder = 'ASC';
67 }
68 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
69 $offset = $limit * $page;
70 
71 $listofdata = array();
72 if (!empty($_SESSION['massstockmove'])) $listofdata = json_decode($_SESSION['massstockmove'], true);
73 
74 
75 /*
76  * Actions
77  */
78 
79 if ($action == 'addline')
80 {
81  if (!($id_product > 0))
82  {
83  $error++;
84  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors');
85  }
86  if (!($id_sw > 0))
87  {
88  $error++;
89  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseSource")), null, 'errors');
90  }
91  if (!($id_tw > 0))
92  {
93  $error++;
94  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WarehouseTarget")), null, 'errors');
95  }
96  if ($id_sw > 0 && $id_tw == $id_sw)
97  {
98  $error++;
99  $langs->load("errors");
100  setEventMessages($langs->trans("ErrorWarehouseMustDiffers"), null, 'errors');
101  }
102  if (!$qty)
103  {
104  $error++;
105  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Qty")), null, 'errors');
106  }
107 
108  // Check a batch number is provided if product need it
109  if (!$error)
110  {
111  $producttmp = new Product($db);
112  $producttmp->fetch($id_product);
113  if ($producttmp->hasbatch())
114  {
115  if (empty($batch))
116  {
117  $error++;
118  $langs->load("errors");
119  setEventMessages($langs->trans("ErrorTryToMakeMoveOnProductRequiringBatchData", $producttmp->ref), null, 'errors');
120  }
121  }
122  }
123 
124  // TODO Check qty is ok for stock move. Note qty may not be enough yet, but we make a check now to report a warning.
125  // What is more important is to have qty when doing action 'createmovements'
126  if (!$error)
127  {
128  // Warning, don't forget lines already added into the $_SESSION['massstockmove']
129  if ($producttmp->hasbatch())
130  {
131  } else {
132  }
133  }
134 
135  if (!$error)
136  {
137  if (count(array_keys($listofdata)) > 0) $id = max(array_keys($listofdata)) + 1;
138  else $id = 1;
139  $listofdata[$id] = array('id'=>$id, 'id_product'=>$id_product, 'qty'=>$qty, 'id_sw'=>$id_sw, 'id_tw'=>$id_tw, 'batch'=>$batch);
140  $_SESSION['massstockmove'] = json_encode($listofdata);
141 
142  //unset($id_sw);
143  //unset($id_tw);
144  unset($id_product);
145  unset($batch);
146  unset($qty);
147  }
148 }
149 
150 if ($action == 'delline' && $idline != '')
151 {
152  if (!empty($listofdata[$idline])) unset($listofdata[$idline]);
153  if (count($listofdata) > 0) $_SESSION['massstockmove'] = json_encode($listofdata);
154  else unset($_SESSION['massstockmove']);
155 }
156 
157 if ($action == 'createmovements')
158 {
159  $error = 0;
160 
161  if (!GETPOST("label"))
162  {
163  $error++;
164  setEventMessages($langs->trans("ErrorFieldRequired"), $langs->transnoentitiesnoconv("MovementLabel"), null, 'errors');
165  }
166 
167  $db->begin();
168 
169  if (!$error)
170  {
171  $product = new Product($db);
172 
173  foreach ($listofdata as $key => $val) // Loop on each movement to do
174  {
175  $id = $val['id'];
176  $id_product = $val['id_product'];
177  $id_sw = $val['id_sw'];
178  $id_tw = $val['id_tw'];
179  $qty = price2num($val['qty']);
180  $batch = $val['batch'];
181  $dlc = -1; // They are loaded later from serial
182  $dluo = -1; // They are loaded later from serial
183 
184  if (!$error && $id_sw <> $id_tw && is_numeric($qty) && $id_product)
185  {
186  $result = $product->fetch($id_product);
187 
188  $product->load_stock('novirtual'); // Load array product->stock_warehouse
189 
190  // Define value of products moved
191  $pricesrc = 0;
192  if (!empty($product->pmp)) $pricesrc = $product->pmp;
193  $pricedest = $pricesrc;
194 
195  //print 'price src='.$pricesrc.', price dest='.$pricedest;exit;
196 
197  if (empty($conf->productbatch->enabled) || !$product->hasbatch()) // If product does not need lot/serial
198  {
199  // Remove stock
200  $result1 = $product->correct_stock(
201  $user,
202  $id_sw,
203  $qty,
204  1,
205  GETPOST("label"),
206  $pricesrc,
207  GETPOST("codemove")
208  );
209  if ($result1 < 0)
210  {
211  $error++;
212  setEventMessages($product->errors, $product->errorss, 'errors');
213  }
214 
215  // Add stock
216  $result2 = $product->correct_stock(
217  $user,
218  $id_tw,
219  $qty,
220  0,
221  GETPOST("label"),
222  $pricedest,
223  GETPOST("codemove")
224  );
225  if ($result2 < 0)
226  {
227  $error++;
228  setEventMessages($product->errors, $product->errorss, 'errors');
229  }
230  } else {
231  $arraybatchinfo = $product->loadBatchInfo($batch);
232  if (count($arraybatchinfo) > 0)
233  {
234  $firstrecord = array_shift($arraybatchinfo);
235  $dlc = $firstrecord['eatby'];
236  $dluo = $firstrecord['sellby'];
237  //var_dump($batch); var_dump($arraybatchinfo); var_dump($firstrecord); var_dump($dlc); var_dump($dluo); exit;
238  } else {
239  $dlc = '';
240  $dluo = '';
241  }
242 
243  // Remove stock
244  $result1 = $product->correct_stock_batch(
245  $user,
246  $id_sw,
247  $qty,
248  1,
249  GETPOST("label"),
250  $pricesrc,
251  $dlc,
252  $dluo,
253  $batch,
254  GETPOST("codemove")
255  );
256  if ($result1 < 0)
257  {
258  $error++;
259  setEventMessages($product->errors, $product->errorss, 'errors');
260  }
261 
262  // Add stock
263  $result2 = $product->correct_stock_batch(
264  $user,
265  $id_tw,
266  $qty,
267  0,
268  GETPOST("label"),
269  $pricedest,
270  $dlc,
271  $dluo,
272  $batch,
273  GETPOST("codemove")
274  );
275  if ($result2 < 0)
276  {
277  $error++;
278  setEventMessages($product->errors, $product->errorss, 'errors');
279  }
280  }
281  } else {
282  // dol_print_error('',"Bad value saved into sessions");
283  $error++;
284  }
285  }
286  }
287 
288  if (!$error)
289  {
290  unset($_SESSION['massstockmove']);
291 
292  $db->commit();
293  setEventMessages($langs->trans("StockMovementRecorded"), null, 'mesgs');
294  header("Location: ".DOL_URL_ROOT.'/product/stock/index.php'); // Redirect to avoid pb when using back
295  exit;
296  } else {
297  $db->rollback();
298  setEventMessages($langs->trans("Error"), null, 'errors');
299  }
300 }
301 
302 
303 
304 /*
305  * View
306  */
307 
308 $now = dol_now();
309 
310 $form = new Form($db);
311 $formproduct = new FormProduct($db);
312 $productstatic = new Product($db);
313 $warehousestatics = new Entrepot($db);
314 $warehousestatict = new Entrepot($db);
315 
316 $title = $langs->trans('MassMovement');
317 
318 llxHeader('', $title);
319 
320 print load_fiche_titre($langs->trans("MassStockTransferShort"), '', 'stock');
321 
322 $titletoadd = $langs->trans("Select");
323 $buttonrecord = $langs->trans("RecordMovement");
324 $titletoaddnoent = $langs->transnoentitiesnoconv("Select");
325 $buttonrecordnoent = $langs->transnoentitiesnoconv("RecordMovement");
326 print '<span class="opacitymedium">'.$langs->trans("SelectProductInAndOutWareHouse", $titletoaddnoent, $buttonrecordnoent).'</span><br>';
327 print '<br>'."\n";
328 
329 // Form to add a line
330 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formulaire">';
331 print '<input type="hidden" name="token" value="'.newToken().'">';
332 print '<input type="hidden" name="action" value="addline">';
333 
334 
335 print '<div class="div-table-responsive-no-min">';
336 print '<table class="liste centpercent">';
337 //print '<div class="tagtable centpercent">';
338 
339 $param = '';
340 
341 print '<tr class="liste_titre">';
342 print getTitleFieldOfList($langs->trans('WarehouseSource'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
343 print getTitleFieldOfList($langs->trans('WarehouseTarget'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
344 print getTitleFieldOfList($langs->trans('ProductRef'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
345 if ($conf->productbatch->enabled) {
346  print getTitleFieldOfList($langs->trans('Batch'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'tagtd maxwidthonsmartphone ');
347 }
348 print getTitleFieldOfList($langs->trans('Qty'), 0, $_SERVER["PHP_SELF"], '', $param, '', '', $sortfield, $sortorder, 'center tagtd maxwidthonsmartphone ');
350 print '</tr>';
351 
352 
353 print '<tr class="oddeven">';
354 // From warehouse
355 print '<td>';
356 print $formproduct->selectWarehouses($id_sw, 'id_sw', 'warehouseopen,warehouseinternal', 1, 0, 0, '', 0, 0, array(), 'minwidth200imp maxwidth200');
357 print '</td>';
358 // To warehouse
359 print '<td>';
360 print $formproduct->selectWarehouses($id_tw, 'id_tw', 'warehouseopen,warehouseinternal', 1, 0, 0, '', 0, 0, array(), 'minwidth200imp maxwidth200');
361 print '</td>';
362 // Product
363 print '<td class="titlefield">';
364 $filtertype = 0;
365 if (!empty($conf->global->STOCK_SUPPORTS_SERVICES)) $filtertype = '';
366 if ($conf->global->PRODUIT_LIMIT_SIZE <= 0) {
367  $limit = '';
368 } else {
369  $limit = $conf->global->PRODUIT_LIMIT_SIZE;
370 }
371 
372 $form->select_produits($id_product, 'productid', $filtertype, $limit, 0, -1, 2, '', 1, array(), 0, '1', 0, 'minwidth200imp maxwidth300', 1);
373 print '</td>';
374 // Batch number
375 if ($conf->productbatch->enabled)
376 {
377  print '<td>';
378  print '<input type="text" name="batch" class="flat maxwidth50" value="'.$batch.'">';
379  print '</td>';
380 }
381 // Qty
382 print '<td class="center"><input type="text" class="flat maxwidth50" name="qty" value="'.$qty.'"></td>';
383 // Button to add line
384 print '<td class="right"><input type="submit" class="button" name="addline" value="'.dol_escape_htmltag($titletoadd).'"></td>';
385 
386 print '</tr>';
387 
388 
389 foreach ($listofdata as $key => $val)
390 {
391  $productstatic->fetch($val['id_product']);
392  $warehousestatics->fetch($val['id_sw']);
393  $warehousestatict->fetch($val['id_tw']);
394 
395  print '<tr class="oddeven">';
396  print '<td>';
397  print $warehousestatics->getNomUrl(1);
398  print '</td>';
399  print '<td>';
400  print $warehousestatict->getNomUrl(1);
401  print '</td>';
402  print '<td>';
403  print $productstatic->getNomUrl(1).' - '.$productstatic->label;
404  print '</td>';
405  if ($conf->productbatch->enabled)
406  {
407  print '<td>';
408  print $val['batch'];
409  print '</td>';
410  }
411  print '<td class="center">'.$val['qty'].'</td>';
412  print '<td class="right"><a href="'.$_SERVER["PHP_SELF"].'?action=delline&idline='.$val['id'].'">'.img_delete($langs->trans("Remove")).'</a></td>';
413 
414  print '</tr>';
415 }
416 
417 print '</table>';
418 print '</div>';
419 
420 print '</form>';
421 
422 
423 print '<br>';
424 
425 
426 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" name="formulaire2" class="formconsumeproduce">';
427 print '<input type="hidden" name="token" value="'.newToken().'">';
428 print '<input type="hidden" name="action" value="createmovements">';
429 
430 // Button to record mass movement
431 $codemove = (GETPOSTISSET("codemove") ? GETPOST("codemove", 'alpha') : dol_print_date(dol_now(), '%Y%m%d%H%M%S'));
432 $labelmovement = GETPOST("label") ? GETPOST('label') : $langs->trans("StockTransfer").' '.dol_print_date($now, '%Y-%m-%d %H:%M');
433 
434 print '<div class="center">';
435 print '<span class="fieldrequired">'.$langs->trans("InventoryCode").':</span> ';
436 print '<input type="text" name="codemove" class="maxwidth300" value="'.dol_escape_htmltag($codemove).'"> &nbsp; ';
437 print '<span class="clearbothonsmartphone"></span>';
438 print $langs->trans("MovementLabel").': ';
439 print '<input type="text" name="label" class="minwidth300" value="'.dol_escape_htmltag($labelmovement).'"><br>';
440 print '<br>';
441 
442 print '<div class="center"><input class="button" type="submit" name="valid" value="'.dol_escape_htmltag($buttonrecord).'"></div>';
443 
444 print '<br>';
445 print '</div>';
446 
447 print '</form>';
448 
449 // End of page
450 llxFooter();
451 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
Class with static methods for building HTML components related to products Only components common to ...
llxHeader()
Empty header.
Definition: wrapper.php:45
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $disablesortlink=0, $tooltip= '', $forcenowrapcolumntitle=0)
Get title line of an array.
llxFooter()
Empty footer.
Definition: wrapper.php:59
img_delete($titlealt= 'default', $other= 'class="pictodelete"', $morecss= '')
Show delete logo.
Class to manage warehouses.