dolibarr  16.0.5
mouvementstock.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2005-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2011 Jean Heimburger <jean@tiaris.info>
5  * Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
32 {
36  public $element = 'stockmouvement';
37 
41  public $table_element = 'stock_mouvement';
42 
43 
47  public $product_id;
48 
52  public $warehouse_id;
53  public $qty;
54 
63  public $type;
64 
65  public $tms = '';
66  public $datem = '';
67  public $price;
68 
72  public $fk_user_author;
73 
77  public $label;
78 
84  public $fk_origin;
85 
89  public $origin_id;
90 
96  public $origintype;
97 
101  public $origin_type;
102 
103 
104  public $inventorycode;
105  public $batch;
106 
107 
108  public $fields = array(
109  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10, 'showoncombobox'=>1),
110  'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
111  'datem' =>array('type'=>'datetime', 'label'=>'Datem', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
112  'fk_product' =>array('type'=>'integer:Product:product/class/product.class.php:1', 'label'=>'Product', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
113  'fk_entrepot' =>array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php', 'label'=>'Warehouse', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30),
114  'value' =>array('type'=>'double', 'label'=>'Value', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
115  'price' =>array('type'=>'double(24,8)', 'label'=>'Price', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
116  'type_mouvement' =>array('type'=>'smallint(6)', 'label'=>'Type mouvement', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
117  'fk_user_author' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'Fk user author', 'enabled'=>1, 'visible'=>-1, 'position'=>50),
118  'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
119  'fk_origin' =>array('type'=>'integer', 'label'=>'Fk origin', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
120  'origintype' =>array('type'=>'varchar(32)', 'label'=>'Origintype', 'enabled'=>1, 'visible'=>-1, 'position'=>65),
121  'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>70),
122  'fk_projet' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Project', 'enabled'=>'$conf->project->enabled', 'visible'=>-1, 'notnull'=>1, 'position'=>75),
123  'inventorycode' =>array('type'=>'varchar(128)', 'label'=>'InventoryCode', 'enabled'=>1, 'visible'=>-1, 'position'=>80),
124  'batch' =>array('type'=>'varchar(30)', 'label'=>'Batch', 'enabled'=>1, 'visible'=>-1, 'position'=>85),
125  'eatby' =>array('type'=>'date', 'label'=>'Eatby', 'enabled'=>1, 'visible'=>-1, 'position'=>90),
126  'sellby' =>array('type'=>'date', 'label'=>'Sellby', 'enabled'=>1, 'visible'=>-1, 'position'=>95),
127  'fk_project' =>array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Fk project', 'enabled'=>1, 'visible'=>-1, 'position'=>100),
128  );
129 
130 
131 
137  public function __construct($db)
138  {
139  $this->db = $db;
140  }
141 
142  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
169  public function _create($user, $fk_product, $entrepot_id, $qty, $type, $price = 0, $label = '', $inventorycode = '', $datem = '', $eatby = '', $sellby = '', $batch = '', $skip_batch = false, $id_product_batch = 0, $disablestockchangeforsubproduct = 0, $donotcleanemptylines = 0)
170  {
171  // phpcs:enable
172  global $conf, $langs;
173 
174  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
175  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
176 
177  $error = 0;
178  dol_syslog(get_class($this)."::_create start userid=$user->id, fk_product=$fk_product, warehouse_id=$entrepot_id, qty=$qty, type=$type, price=$price, label=$label, inventorycode=$inventorycode, datem=".$datem.", eatby=".$eatby.", sellby=".$sellby.", batch=".$batch.", skip_batch=".$skip_batch);
179 
180  // Call hook at beginning
181  global $action, $hookmanager;
182  $hookmanager->initHooks(array('mouvementstock'));
183 
184  if (is_object($hookmanager)) {
185  $parameters = array(
186  'currentcontext' => 'mouvementstock',
187  'user' => &$user,
188  'fk_product' => &$fk_product,
189  'entrepot_id' => &$entrepot_id,
190  'qty' => &$qty,
191  'type' => &$type,
192  'price' => &$price,
193  'label' => &$label,
194  'inventorycode' => &$inventorycode,
195  'datem' => &$datem,
196  'eatby' => &$eatby,
197  'sellby' => &$sellby,
198  'batch' => &$batch,
199  'skip_batch' => &$skip_batch,
200  'id_product_batch' => &$id_product_batch
201  );
202  $reshook = $hookmanager->executeHooks('stockMovementCreate', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
203 
204  if ($reshook < 0) {
205  if (!empty($hookmanager->resPrint)) {
206  dol_print_error('', $hookmanager->resPrint);
207  }
208  return $reshook;
209  } elseif ($reshook > 0) {
210  return $hookmanager->resPrint;
211  }
212  }
213  // end hook at beginning
214 
215  // Clean parameters
216  $price = price2num($price, 'MU'); // Clean value for the casse we receive a float zero value, to have it a real zero value.
217  if (empty($price)) $price = 0;
218  $now = (!empty($datem) ? $datem : dol_now());
219 
220  // Check parameters
221  if (!($fk_product > 0)) return 0;
222  if (!($entrepot_id > 0)) return 0;
223 
224  if (is_numeric($eatby) && $eatby < 0) {
225  dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
226  $this->errors[] = 'ErrorBadValueForParameterEatBy';
227  return -1;
228  }
229  if (is_numeric($sellby) && $sellby < 0) {
230  dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
231  $this->errors[] = 'ErrorBadValueForParameterSellBy';
232  return -1;
233  }
234 
235  // Set properties of movement
236  $this->product_id = $fk_product;
237  $this->entrepot_id = $entrepot_id; // deprecated
238  $this->warehouse_id = $entrepot_id;
239  $this->qty = $qty;
240  $this->type = $type;
241  $this->price = price2num($price);
242  $this->label = $label;
243  $this->inventorycode = $inventorycode;
244  $this->datem = $now;
245  $this->batch = $batch;
246 
247  $mvid = 0;
248 
249  $product = new Product($this->db);
250 
251  $result = $product->fetch($fk_product);
252  if ($result < 0) {
253  $this->error = $product->error;
254  $this->errors = $product->errors;
255  dol_print_error('', "Failed to fetch product");
256  return -1;
257  }
258  if ($product->id <= 0) { // Can happen if database is corrupted (a product id exist in stock with product that has been removed)
259  return 0;
260  }
261 
262  // Define if we must make the stock change (If product type is a service or if stock is used also for services)
263  // Only record into stock tables wil be disabled by this (the rest like writing into lot table or movement of subproucts are done)
264  $movestock = 0;
265  if ($product->type != Product::TYPE_SERVICE || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) $movestock = 1;
266 
267  $this->db->begin();
268 
269  // Set value $product->stock_reel and detail per warehouse into $product->stock_warehouse array
270  if ($movestock) {
271  $product->load_stock('novirtual');
272  }
273 
274  // Test if product require batch data. If yes, and there is not or values are not correct, we throw an error.
275  if (!empty($conf->productbatch->enabled) && $product->hasbatch() && !$skip_batch) {
276  if (empty($batch)) {
277  $langs->load("errors");
278  $this->errors[] = $langs->transnoentitiesnoconv("ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
279  dol_syslog("Try to make a movement of a product with status_batch on without any batch data");
280 
281  $this->db->rollback();
282  return -2;
283  }
284 
285  // Check table llx_product_lot from batchnumber for same product
286  // If found and eatby/sellby defined into table and provided and differs, return error
287  // If found and eatby/sellby defined into table and not provided, we take value from table
288  // If found and eatby/sellby not defined into table and provided, we update table
289  // If found and eatby/sellby not defined into table and not provided, we do nothing
290  // If not found, we add record
291  $sql = "SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix()."product_lot as pb";
292  $sql .= " WHERE pb.fk_product = ".((int) $fk_product)." AND pb.batch = '".$this->db->escape($batch)."'";
293  dol_syslog(get_class($this)."::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
294  $resql = $this->db->query($sql);
295  if ($resql) {
296  $num = $this->db->num_rows($resql);
297  $i = 0;
298  if ($num > 0) {
299  while ($i < $num) {
300  $obj = $this->db->fetch_object($resql);
301  if ($obj->eatby) {
302  if ($eatby) {
303  $tmparray = dol_getdate($eatby, true);
304  $eatbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
305  if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) { // We test date without hours and with hours for backward compatibility
306  // If found and eatby/sellby defined into table and provided and differs, return error
307  $langs->load("stocks");
308  $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->eatby), 'dayhour'), dol_print_date($eatbywithouthour, 'dayhour'));
309  dol_syslog("ThisSerialAlreadyExistWithDifferentDate batch=".$batch.", eatby found into product_lot = ".$obj->eatby." = ".dol_print_date($this->db->jdate($obj->eatby), 'dayhourrfc')." so eatbywithouthour = ".$eatbywithouthour." = ".dol_print_date($eatbywithouthour)." - eatby provided = ".$eatby." = ".dol_print_date($eatby, 'dayhourrfc'), LOG_ERR);
310  $this->db->rollback();
311  return -3;
312  }
313  } else {
314  $eatby = $obj->eatby; // If found and eatby/sellby defined into table and not provided, we take value from table
315  }
316  } else {
317  if ($eatby) { // If found and eatby/sellby not defined into table and provided, we update table
318  $productlot = new Productlot($this->db);
319  $result = $productlot->fetch($obj->rowid);
320  $productlot->eatby = $eatby;
321  $result = $productlot->update($user);
322  if ($result <= 0) {
323  $this->error = $productlot->error;
324  $this->errors = $productlot->errors;
325  $this->db->rollback();
326  return -5;
327  }
328  }
329  }
330  if ($obj->sellby) {
331  if ($sellby) {
332  $tmparray = dol_getdate($sellby, true);
333  $sellbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
334  if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) { // We test date without hours and with hours for backward compatibility
335  // If found and eatby/sellby defined into table and provided and differs, return error
336  $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby));
337  dol_syslog($langs->transnoentities("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby)), LOG_ERR);
338  $this->db->rollback();
339  return -3;
340  }
341  } else {
342  $sellby = $obj->sellby; // If found and eatby/sellby defined into table and not provided, we take value from table
343  }
344  } else {
345  if ($sellby) { // If found and eatby/sellby not defined into table and provided, we update table
346  $productlot = new Productlot($this->db);
347  $result = $productlot->fetch($obj->rowid);
348  $productlot->sellby = $sellby;
349  $result = $productlot->update($user);
350  if ($result <= 0) {
351  $this->error = $productlot->error;
352  $this->errors = $productlot->errors;
353  $this->db->rollback();
354  return -5;
355  }
356  }
357  }
358 
359  $i++;
360  }
361  } else { // If not found, we add record
362  $productlot = new Productlot($this->db);
363  $productlot->entity = $conf->entity;
364  $productlot->fk_product = $fk_product;
365  $productlot->batch = $batch;
366  // If we are here = first time we manage this batch, so we used dates provided by users to create lot
367  $productlot->eatby = $eatby;
368  $productlot->sellby = $sellby;
369  $result = $productlot->create($user);
370  if ($result <= 0) {
371  $this->error = $productlot->error;
372  $this->errors = $productlot->errors;
373  $this->db->rollback();
374  return -4;
375  }
376  }
377  } else {
378  dol_print_error($this->db);
379  $this->db->rollback();
380  return -1;
381  }
382  }
383 
384  // Check if stock is enough when qty is < 0
385  // Note that qty should be > 0 with type 0 or 3, < 0 with type 1 or 2.
386  if ($movestock && $qty < 0 && empty($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER)) {
387  if (!empty($conf->productbatch->enabled) && $product->hasbatch() && !$skip_batch) {
388  $foundforbatch = 0;
389  $qtyisnotenough = 0;
390 
391  foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
392  if ((string) $batch != (string) $batchcursor) { // Lot '59' must be different than lot '59c'
393  continue;
394  }
395 
396  $foundforbatch = 1;
397  if ($prodbatch->qty < abs($qty)) $qtyisnotenough = $prodbatch->qty;
398  break;
399  }
400  if (!$foundforbatch || $qtyisnotenough) {
401  $langs->load("stocks");
402  include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
403  $tmpwarehouse = new Entrepot($this->db);
404  $tmpwarehouse->fetch($entrepot_id);
405 
406  $this->error = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
407  $this->errors[] = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
408  $this->db->rollback();
409  return -8;
410  }
411  } else {
412  if (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty)) {
413  $langs->load("stocks");
414  $this->error = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
415  $this->errors[] = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
416  $this->db->rollback();
417  return -8;
418  }
419  }
420  }
421 
422  if ($movestock) { // Change stock for current product, change for subproduct is done after
423  // Set $origin_type, origin_id and fk_project
424  $fk_project = $this->fk_project;
425  if (!empty($this->origin_type)) { // This is set by caller for tracking reason
426  $origin_type = $this->origin_type;
427  $origin_id = $this->origin_id;
428  if (empty($fk_project) && $origin_type == 'project') {
429  $fk_project = $origin_id;
430  $origin_type = '';
431  $origin_id = 0;
432  }
433  } else {
434  $fk_project = 0;
435  $origin_type = '';
436  $origin_id = 0;
437  }
438 
439  $sql = "INSERT INTO ".$this->db->prefix()."stock_mouvement(";
440  $sql .= " datem, fk_product, batch, eatby, sellby,";
441  $sql .= " fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
442  $sql .= ")";
443  $sql .= " VALUES ('".$this->db->idate($this->datem)."', ".((int) $this->product_id).", ";
444  $sql .= " ".($batch ? "'".$this->db->escape($batch)."'" : "null").", ";
445  $sql .= " ".($eatby ? "'".$this->db->idate($eatby)."'" : "null").", ";
446  $sql .= " ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", ";
447  $sql .= " ".((int) $this->entrepot_id).", ".((float) $this->qty).", ".((int) $this->type).",";
448  $sql .= " ".((int) $user->id).",";
449  $sql .= " '".$this->db->escape($label)."',";
450  $sql .= " ".($inventorycode ? "'".$this->db->escape($inventorycode)."'" : "null").",";
451  $sql .= " ".((float) price2num($price)).",";
452  $sql .= " ".((int) $origin_id).",";
453  $sql .= " '".$this->db->escape($origin_type)."',";
454  $sql .= " ".((int) $fk_project);
455  $sql .= ")";
456 
457  dol_syslog(get_class($this)."::_create insert record into stock_mouvement", LOG_DEBUG);
458  $resql = $this->db->query($sql);
459 
460  if ($resql) {
461  $mvid = $this->db->last_insert_id($this->db->prefix()."stock_mouvement");
462  $this->id = $mvid;
463  } else {
464  $this->error = $this->db->lasterror();
465  $this->errors[] = $this->error;
466  $error = -1;
467  }
468 
469  // Define current values for qty and pmp
470  $oldqty = $product->stock_reel;
471  $oldpmp = $product->pmp;
472  $oldqtywarehouse = 0;
473 
474  // Test if there is already a record for couple (warehouse / product), so later we will make an update or create.
475  $alreadyarecord = 0;
476  if (!$error) {
477  $sql = "SELECT rowid, reel FROM ".$this->db->prefix()."product_stock";
478  $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product); // This is a unique key
479 
480  dol_syslog(get_class($this)."::_create check if a record already exists in product_stock", LOG_DEBUG);
481  $resql = $this->db->query($sql);
482  if ($resql) {
483  $obj = $this->db->fetch_object($resql);
484  if ($obj) {
485  $alreadyarecord = 1;
486  $oldqtywarehouse = $obj->reel;
487  $fk_product_stock = $obj->rowid;
488  }
489  $this->db->free($resql);
490  } else {
491  $this->errors[] = $this->db->lasterror();
492  $error = -2;
493  }
494  }
495 
496  // Calculate new AWP (PMP)
497  $newpmp = 0;
498  if (!$error) {
499  if ($type == 0 || $type == 3) {
500  // After a stock increase
501  // Note: PMP is calculated on stock input only (type of movement = 0 or 3). If type == 0 or 3, qty should be > 0.
502  // Note: Price should always be >0 or 0. PMP should be always >0 (calculated on input)
503  if ($price > 0 || (!empty($conf->global->STOCK_UPDATE_AWP_EVEN_WHEN_ENTRY_PRICE_IS_NULL) && $price == 0)) {
504  $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
505  // We make a test on oldpmp>0 to avoid to use normal rule on old data with no pmp field defined
506  if ($oldpmp > 0) {
507  $newpmp = price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty), 'MU');
508  } else {
509  $newpmp = $price; // For this product, PMP was not yet set. We set it to input price.
510  }
511  //print "oldqtytouse=".$oldqtytouse." oldpmp=".$oldpmp." oldqtywarehousetouse=".$oldqtywarehousetouse." ";
512  //print "qty=".$qty." newpmp=".$newpmp;
513  //exit;
514  } else {
515  $newpmp = $oldpmp;
516  }
517  } elseif ($type == 1 || $type == 2) {
518  // After a stock decrease, we don't change value of the AWP/PMP of a product.
519  $newpmp = $oldpmp;
520  } else {
521  // Type of movement unknown
522  $newpmp = $oldpmp;
523  }
524  }
525  // Update stock quantity
526  if (!$error) {
527  if ($alreadyarecord > 0) {
528  $sql = "UPDATE ".$this->db->prefix()."product_stock SET reel = reel + ".((float) $qty);
529  $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product);
530  } else {
531  $sql = "INSERT INTO ".$this->db->prefix()."product_stock";
532  $sql .= " (reel, fk_entrepot, fk_product) VALUES ";
533  $sql .= " (".((float) $qty).", ".((int) $entrepot_id).", ".((int) $fk_product).")";
534  }
535 
536  dol_syslog(get_class($this)."::_create update stock value", LOG_DEBUG);
537  $resql = $this->db->query($sql);
538  if (!$resql) {
539  $this->errors[] = $this->db->lasterror();
540  $error = -3;
541  } elseif (empty($fk_product_stock)) {
542  $fk_product_stock = $this->db->last_insert_id($this->db->prefix()."product_stock");
543  }
544  }
545 
546  // Update detail of stock for the lot.
547  if (!$error && !empty($conf->productbatch->enabled) && $product->hasbatch() && !$skip_batch) {
548  if ($id_product_batch > 0) {
549  $result = $this->createBatch($id_product_batch, $qty);
550  } else {
551  $param_batch = array('fk_product_stock' =>$fk_product_stock, 'batchnumber'=>$batch);
552  $result = $this->createBatch($param_batch, $qty);
553  }
554  if ($result < 0) {
555  $error++;
556  }
557  }
558 
559  // Update PMP and denormalized value of stock qty at product level
560  if (!$error) {
561  $newpmp = price2num($newpmp, 'MU');
562 
563  // $sql = "UPDATE ".$this->db->prefix()."product SET pmp = ".$newpmp.", stock = ".$this->db->ifsql("stock IS NULL", 0, "stock") . " + ".$qty;
564  // $sql.= " WHERE rowid = ".((int) $fk_product);
565  // Update pmp + denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
566  $sql = "UPDATE ".$this->db->prefix()."product as p SET pmp = ".((float) $newpmp).",";
567  $sql .= " stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix()."product_stock as ps WHERE ps.fk_product = p.rowid)";
568  $sql .= " WHERE rowid = ".((int) $fk_product);
569 
570  dol_syslog(get_class($this)."::_create update AWP", LOG_DEBUG);
571  $resql = $this->db->query($sql);
572  if (!$resql) {
573  $this->errors[] = $this->db->lasterror();
574  $error = -4;
575  }
576  }
577 
578  if (empty($donotcleanemptylines)) {
579  // If stock is now 0, we can remove entry into llx_product_stock, but only if there is no child lines into llx_product_batch (detail of batch, because we can imagine
580  // having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
581  $sql = "DELETE FROM ".$this->db->prefix()."product_stock WHERE reel = 0 AND rowid NOT IN (SELECT fk_product_stock FROM ".$this->db->prefix()."product_batch as pb)";
582  $resql = $this->db->query($sql);
583  // We do not test error, it can fails if there is child in batch details
584  }
585  }
586 
587  // Add movement for sub products (recursive call)
588  if (!$error && !empty($conf->global->PRODUIT_SOUSPRODUITS) && empty($conf->global->INDEPENDANT_SUBPRODUCT_STOCK) && empty($disablestockchangeforsubproduct)) {
589  $error = $this->_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode); // we use 0 as price, because AWP must not change for subproduct
590  }
591 
592  if ($movestock && !$error) {
593  // Call trigger
594  $result = $this->call_trigger('STOCK_MOVEMENT', $user);
595  if ($result < 0) $error++;
596  // End call triggers
597 
598  // Check unicity for serial numbered equipments once all movement were done.
599  if (!$error && !empty($conf->productbatch->enabled) && $product->hasbatch() && !$skip_batch) {
600  if ($product->status_batch == 2 && $qty > 0) { // We check only if we increased qty
601  if ($this->getBatchCount($fk_product, $batch) > 1) {
602  $error++;
603  $this->errors[] = $langs->trans("TooManyQtyForSerialNumber", $product->ref, $batch);
604  }
605  }
606  }
607  }
608 
609  if (!$error) {
610  $this->db->commit();
611  return $mvid;
612  } else {
613  $this->db->rollback();
614  dol_syslog(get_class($this)."::_create error code=".$error, LOG_ERR);
615  return -6;
616  }
617  }
618 
619 
620 
628  public function fetch($id)
629  {
630  dol_syslog(__METHOD__, LOG_DEBUG);
631 
632  $sql = "SELECT";
633  $sql .= " t.rowid,";
634  $sql .= " t.tms,";
635  $sql .= " t.datem,";
636  $sql .= " t.fk_product,";
637  $sql .= " t.fk_entrepot,";
638  $sql .= " t.value,";
639  $sql .= " t.price,";
640  $sql .= " t.type_mouvement,";
641  $sql .= " t.fk_user_author,";
642  $sql .= " t.label,";
643  $sql .= " t.fk_origin as origin_id,";
644  $sql .= " t.origintype as origin_type,";
645  $sql .= " t.inventorycode,";
646  $sql .= " t.batch,";
647  $sql .= " t.eatby,";
648  $sql .= " t.sellby,";
649  $sql .= " t.fk_projet as fk_project";
650  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
651  $sql .= " WHERE t.rowid = ".((int) $id);
652 
653  $resql = $this->db->query($sql);
654  if ($resql) {
655  $numrows = $this->db->num_rows($resql);
656  if ($numrows) {
657  $obj = $this->db->fetch_object($resql);
658 
659  $this->id = $obj->rowid;
660 
661  $this->product_id = $obj->fk_product;
662  $this->warehouse_id = $obj->fk_entrepot;
663  $this->qty = $obj->value;
664  $this->type = $obj->type_mouvement;
665 
666  $this->tms = $this->db->jdate($obj->tms);
667  $this->datem = $this->db->jdate($obj->datem);
668  $this->price = $obj->price;
669  $this->fk_user_author = $obj->fk_user_author;
670  $this->label = $obj->label;
671  $this->fk_origin = $obj->origin_id; // For backward compatibility
672  $this->origintype = $obj->origin_type; // For backward compatibility
673  $this->origin_id = $obj->origin_id;
674  $this->origin_type = $obj->origin_type;
675  $this->inventorycode = $obj->inventorycode;
676  $this->batch = $obj->batch;
677  $this->eatby = $this->db->jdate($obj->eatby);
678  $this->sellby = $this->db->jdate($obj->sellby);
679  $this->fk_project = $obj->fk_project;
680  }
681 
682  // Retrieve all extrafield
683  $this->fetch_optionals();
684 
685  // $this->fetch_lines();
686 
687  $this->db->free($resql);
688 
689  if ($numrows) {
690  return 1;
691  } else {
692  return 0;
693  }
694  } else {
695  $this->errors[] = 'Error '.$this->db->lasterror();
696  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
697 
698  return -1;
699  }
700  }
701 
702 
703 
704 
718  private function _createSubProduct($user, $idProduct, $entrepot_id, $qty, $type, $price = 0, $label = '', $inventorycode = '')
719  {
720  global $langs;
721 
722  $error = 0;
723  $pids = array();
724  $pqtys = array();
725 
726  $sql = "SELECT fk_product_pere, fk_product_fils, qty";
727  $sql .= " FROM ".$this->db->prefix()."product_association";
728  $sql .= " WHERE fk_product_pere = ".((int) $idProduct);
729  $sql .= " AND incdec = 1";
730 
731  dol_syslog(get_class($this)."::_createSubProduct for parent product ".$idProduct, LOG_DEBUG);
732  $resql = $this->db->query($sql);
733  if ($resql) {
734  $i = 0;
735  while ($obj = $this->db->fetch_object($resql)) {
736  $pids[$i] = $obj->fk_product_fils;
737  $pqtys[$i] = $obj->qty;
738  $i++;
739  }
740  $this->db->free($resql);
741  } else {
742  $error = -2;
743  }
744 
745  // Create movement for each subproduct
746  foreach ($pids as $key => $value) {
747  if (!$error) {
748  $tmpmove = dol_clone($this, 1);
749 
750  $result = $tmpmove->_create($user, $pids[$key], $entrepot_id, ($qty * $pqtys[$key]), $type, 0, $label, $inventorycode); // This will also call _createSubProduct making this recursive
751  if ($result < 0) {
752  $this->error = $tmpmove->error;
753  $this->errors = array_merge($this->errors, $tmpmove->errors);
754  if ($result == -2) {
755  $this->errors[] = $langs->trans("ErrorNoteAlsoThatSubProductCantBeFollowedByLot");
756  }
757  $error = $result;
758  }
759  unset($tmpmove);
760  }
761  }
762 
763  return $error;
764  }
765 
766 
785  public function livraison($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $datem = '', $eatby = '', $sellby = '', $batch = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
786  {
787  global $conf;
788 
789  $skip_batch = empty($conf->productbatch->enabled);
790 
791  return $this->_create($user, $fk_product, $entrepot_id, (0 - $qty), 2, $price, $label, $inventorycode, $datem, $eatby, $sellby, $batch, $skip_batch, $id_product_batch, 0, $donotcleanemptylines);
792  }
793 
812  public function reception($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $eatby = '', $sellby = '', $batch = '', $datem = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
813  {
814  global $conf;
815 
816  $skip_batch = empty($conf->productbatch->enabled);
817 
818  return $this->_create($user, $fk_product, $entrepot_id, $qty, 3, $price, $label, $inventorycode, $datem, $eatby, $sellby, $batch, $skip_batch, $id_product_batch, 0, $donotcleanemptylines);
819  }
820 
828  public function calculateBalanceForProductBefore($productidselected, $datebefore)
829  {
830  $nb = 0;
831 
832  $sql = "SELECT SUM(value) as nb from ".$this->db->prefix()."stock_mouvement";
833  $sql .= " WHERE fk_product = ".((int) $productidselected);
834  $sql .= " AND datem < '".$this->db->idate($datebefore)."'";
835 
836  dol_syslog(get_class($this).__METHOD__.'', LOG_DEBUG);
837  $resql = $this->db->query($sql);
838  if ($resql) {
839  $obj = $this->db->fetch_object($resql);
840  if ($obj) $nb = $obj->nb;
841  return (empty($nb) ? 0 : $nb);
842  } else {
843  dol_print_error($this->db);
844  return -1;
845  }
846  }
847 
857  private function createBatch($dluo, $qty)
858  {
859  global $user, $langs;
860 
861  $langs->load('productbatch');
862 
863  $pdluo = new Productbatch($this->db);
864 
865  $result = 0;
866 
867  // Try to find an existing record with same batch number or id
868  if (is_numeric($dluo)) {
869  $result = $pdluo->fetch($dluo);
870  if (empty($pdluo->id)) {
871  // We didn't find the line. May be it was deleted before by a previous move in same transaction.
872  $this->error = $langs->trans('CantMoveNonExistantSerial');
873  $this->errors[] = $this->error;
874  $result = -2;
875  }
876  } elseif (is_array($dluo)) {
877  if (isset($dluo['fk_product_stock'])) {
878  $vfk_product_stock = $dluo['fk_product_stock'];
879  $vbatchnumber = $dluo['batchnumber'];
880 
881  $result = $pdluo->find($vfk_product_stock, '', '', $vbatchnumber); // Search on batch number only (eatby and sellby are deprecated here)
882  } else {
883  dol_syslog(get_class($this)."::createBatch array param dluo must contain at least key fk_product_stock", LOG_ERR);
884  $result = -1;
885  }
886  } else {
887  dol_syslog(get_class($this)."::createBatch error invalid param dluo", LOG_ERR);
888  $result = -1;
889  }
890 
891  if ($result >= 0) {
892  // No error
893  if ($pdluo->id > 0) { // product_batch record found
894  //print "Avant ".$pdluo->qty." Apres ".($pdluo->qty + $qty)."<br>";
895  $pdluo->qty += $qty;
896  if ($pdluo->qty == 0) {
897  $result = $pdluo->delete($user, 1);
898  } else {
899  $result = $pdluo->update($user, 1);
900  }
901  } else { // product_batch record not found
902  $pdluo->fk_product_stock = $vfk_product_stock;
903  $pdluo->qty = $qty;
904  $pdluo->eatby = empty($dluo['eatby']) ? '' : $dluo['eatby']; // No more used. Now eatby date is store in table of lot, no more into prouct_batch table.
905  $pdluo->sellby = empty($dluo['sellby']) ? '' : $dluo['sellby']; // No more used. Now sellby date is store in table of lot, no more into prouct_batch table.
906  $pdluo->batch = $vbatchnumber;
907 
908  $result = $pdluo->create($user, 1);
909  if ($result < 0) {
910  $this->error = $pdluo->error;
911  $this->errors = $pdluo->errors;
912  }
913  }
914  }
915 
916  return $result;
917  }
918 
919  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
927  public function get_origin($origin_id, $origin_type)
928  {
929  // phpcs:enable
930  $origin = '';
931 
932  switch ($origin_type) {
933  case 'commande':
934  require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
935  $origin = new Commande($this->db);
936  break;
937  case 'shipping':
938  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
939  $origin = new Expedition($this->db);
940  break;
941  case 'facture':
942  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
943  $origin = new Facture($this->db);
944  break;
945  case 'order_supplier':
946  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
947  $origin = new CommandeFournisseur($this->db);
948  break;
949  case 'invoice_supplier':
950  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
951  $origin = new FactureFournisseur($this->db);
952  break;
953  case 'project':
954  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
955  $origin = new Project($this->db);
956  break;
957  case 'mo':
958  require_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
959  $origin = new Mo($this->db);
960  break;
961  case 'user':
962  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
963  $origin = new User($this->db);
964  break;
965  case 'reception':
966  require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
967  $origin = new Reception($this->db);
968  break;
969  case 'inventory':
970  require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
971  $origin = new Inventory($this->db);
972  break;
973 
974  default:
975  if ($origin_type) {
976  // Separate originetype with "@" : left part is class name, right part is module name
977  $origin_type_array = explode('@', $origin_type);
978  $classname = $origin_type_array[0];
979  $modulename = empty($origin_type_array[1]) ? $classname : $origin_type_array[1];
980  $result = dol_include_once('/'.$modulename.'/class/'.$classname.'.class.php');
981  if ($result) {
982  $classname = ucfirst($classname);
983  $origin = new $classname($this->db);
984  }
985  }
986  break;
987  }
988 
989  if (empty($origin) || !is_object($origin)) {
990  return '';
991  }
992 
993  if ($origin->fetch($origin_id) > 0) {
994  return $origin->getNomUrl(1);
995  }
996 
997  return '';
998  }
999 
1008  public function setOrigin($origin_element, $origin_id)
1009  {
1010  $this->origin_type = $origin_element;
1011  $this->origin_id = $origin_id;
1012  // For backward compatibility
1013  $this->origintype = $origin_element;
1014  $this->fk_origin = $origin_id;
1015  }
1016 
1017 
1025  public function initAsSpecimen()
1026  {
1027  global $user, $langs, $conf, $mysoc;
1028 
1029  // Initialize parameters
1030  $this->id = 0;
1031 
1032  // There is no specific properties. All data into insert are provided as method parameter.
1033  }
1034 
1041  public function getTypeMovement($withlabel = 0)
1042  {
1043  global $langs;
1044 
1045  $s = '';
1046  switch ($this->type) {
1047  case "0":
1048  $s = '<span class="fa fa-level-down-alt stockmovemententry stockmovementtransfer" title="'.$langs->trans('StockIncreaseAfterCorrectTransfer').'"></span>';
1049  if ($withlabel) {
1050  $s .= $langs->trans('StockIncreaseAfterCorrectTransfer');
1051  }
1052  break;
1053  case "1":
1054  $s = '<span class="fa fa-level-up-alt stockmovementexit stockmovementtransfer" title="'.$langs->trans('StockDecreaseAfterCorrectTransfer').'"></span>';
1055  if ($withlabel) {
1056  $s .= $langs->trans('StockDecreaseAfterCorrectTransfer');
1057  }
1058  break;
1059  case "2":
1060  $s = '<span class="fa fa-long-arrow-alt-up stockmovementexit stockmovement" title="'.$langs->trans('StockDecrease').'"></span>';
1061  if ($withlabel) {
1062  $s .= $langs->trans('StockDecrease');
1063  }
1064  break;
1065  case "3":
1066  $s = '<span class="fa fa-long-arrow-alt-down stockmovemententry stockmovement" title="'.$langs->trans('StockIncrease').'"></span>';
1067  if ($withlabel) {
1068  $s .= $langs->trans('StockIncrease');
1069  }
1070  break;
1071  }
1072 
1073  return $s;
1074  }
1075 
1087  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
1088  {
1089  global $langs, $conf, $db;
1090 
1091  $result = '';
1092 
1093  $label = img_picto('', 'stock', 'class="pictofixedwidth"').'<u>'.$langs->trans("Movement").' '.$this->id.'</u>';
1094  $label .= '<div width="100%">';
1095  $label .= '<b>'.$langs->trans('Label').':</b> '.$this->label;
1096  $label .= '<br><b>'.$langs->trans('Qty').':</b> '.($this->qty > 0 ? '+' : '').$this->qty;
1097  $label .= '</div>';
1098 
1099  // Link to page of warehouse tab
1100  if ($option == 'movements') {
1101  $url = DOL_URL_ROOT.'/product/stock/movement_list.php?search_ref='.$this->id;
1102  } else {
1103  $url = DOL_URL_ROOT.'/product/stock/movement_list.php?id='.$this->warehouse_id.'&msid='.$this->id;
1104  }
1105 
1106  $link = '<a href="'.$url.'"'.($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
1107  $link .= '>';
1108  $linkend = '</a>';
1109 
1110  if ($withpicto) {
1111  $result .= ($link.img_object(($notooltip ? '' : $label), 'stock', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
1112  if ($withpicto != 2) {
1113  $result .= ' ';
1114  }
1115  }
1116  $result .= $link.$this->id.$linkend;
1117  return $result;
1118  }
1119 
1126  public function getLibStatut($mode = 0)
1127  {
1128  return $this->LibStatut($mode);
1129  }
1130 
1131  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1138  public function LibStatut($mode = 0)
1139  {
1140  // phpcs:enable
1141  global $langs;
1142 
1143  if ($mode == 0 || $mode == 1) {
1144  return $langs->trans('StatusNotApplicable');
1145  } elseif ($mode == 2) {
1146  return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1147  } elseif ($mode == 3) {
1148  return img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1149  } elseif ($mode == 4) {
1150  return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1151  } elseif ($mode == 5) {
1152  return $langs->trans('StatusNotApplicable').' '.img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1153  }
1154  }
1155 
1166  public function generateDocument($modele, $outputlangs = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1167  {
1168  global $conf, $user, $langs;
1169 
1170  $langs->load("stocks");
1171  $outputlangs->load("products");
1172 
1173  if (!dol_strlen($modele)) {
1174  $modele = 'stdmovement';
1175 
1176  if ($this->model_pdf) {
1177  $modele = $this->model_pdf;
1178  } elseif (!empty($conf->global->MOUVEMENT_ADDON_PDF)) {
1179  $modele = $conf->global->MOUVEMENT_ADDON_PDF;
1180  }
1181  }
1182 
1183  $modelpath = "core/modules/stock/doc/";
1184 
1185  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1186  }
1187 
1195  public function delete(User $user, $notrigger = false)
1196  {
1197  return $this->deleteCommon($user, $notrigger);
1198  //return $this->deleteCommon($user, $notrigger, 1);
1199  }
1200 
1208  private function getBatchCount($fk_product, $batch)
1209  {
1210  $cpt = 0;
1211 
1212  $sql = "SELECT sum(pb.qty) as cpt";
1213  $sql .= " FROM ".$this->db->prefix()."product_batch as pb";
1214  $sql .= " INNER JOIN ".$this->db->prefix()."product_stock as ps ON ps.rowid = pb.fk_product_stock";
1215  $sql .= " WHERE ps.fk_product = " . ((int) $fk_product);
1216  $sql .= " AND pb.batch = '" . $this->db->escape($batch) . "'";
1217 
1218  $result = $this->db->query($sql);
1219  if ($result) {
1220  if ($this->db->num_rows($result)) {
1221  $obj = $this->db->fetch_object($result);
1222  $cpt = $obj->cpt;
1223  }
1224 
1225  $this->db->free($result);
1226  } else {
1227  dol_print_error($this->db);
1228  return -1;
1229  }
1230 
1231  return $cpt;
1232  }
1233 }
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2713
db
$conf db
API class for accounts.
Definition: inc.php:41
Productbatch
Manage record for batch number management.
Definition: productbatch.class.php:31
Reception
Class to manage receptions.
Definition: reception.class.php:49
Expedition
Class to manage shipments.
Definition: expedition.class.php:52
Project
Class to manage projects.
Definition: project.class.php:35
MouvementStock\LibStatut
LibStatut($mode=0)
Renvoi le libelle d'un status donne.
Definition: mouvementstock.class.php:1138
FactureFournisseur
Class to manage suppliers invoices.
Definition: fournisseur.facture.class.php:53
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
MouvementStock\reception
reception($user, $fk_product, $entrepot_id, $qty, $price=0, $label='', $eatby='', $sellby='', $batch='', $datem='', $id_product_batch=0, $inventorycode='', $donotcleanemptylines=0)
Increase stock for product and subproducts.
Definition: mouvementstock.class.php:812
MouvementStock\getTypeMovement
getTypeMovement($withlabel=0)
Return html string with picto for type of movement.
Definition: mouvementstock.class.php:1041
MouvementStock\setOrigin
setOrigin($origin_element, $origin_id)
Set attribute origin_type and fk_origin to object.
Definition: mouvementstock.class.php:1008
Inventory
Class for Inventory.
Definition: inventory.class.php:36
Facture
Class to manage invoices.
Definition: facture.class.php:60
Mo
Class for Mo.
Definition: mo.class.php:35
dol_clone
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
Definition: functions.lib.php:1158
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
MouvementStock\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link (with optionaly the picto) Use this->id,this->lastname, this->firstname.
Definition: mouvementstock.class.php:1087
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Productlot
Class with list of lots and properties.
Definition: productlot.class.php:36
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
MouvementStock\createBatch
createBatch($dluo, $qty)
Create or update batch record (update table llx_product_batch).
Definition: mouvementstock.class.php:857
CommonObject\commonGenerateDocument
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
Definition: commonobject.class.php:5406
MouvementStock\_create
_create($user, $fk_product, $entrepot_id, $qty, $type, $price=0, $label='', $inventorycode='', $datem='', $eatby='', $sellby='', $batch='', $skip_batch=false, $id_product_batch=0, $disablestockchangeforsubproduct=0, $donotcleanemptylines=0)
Add a movement of stock (in one direction only).
Definition: mouvementstock.class.php:169
MouvementStock
Class to manage stock movements.
Definition: mouvementstock.class.php:31
MouvementStock\calculateBalanceForProductBefore
calculateBalanceForProductBefore($productidselected, $datebefore)
Count number of product in stock before a specific date.
Definition: mouvementstock.class.php:828
MouvementStock\fetch
fetch($id)
Load object in memory from the database.
Definition: mouvementstock.class.php:628
Commande
Class to manage customers orders.
Definition: commande.class.php:46
MouvementStock\get_origin
get_origin($origin_id, $origin_type)
Return Url link of origin object.
Definition: mouvementstock.class.php:927
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CommonObject\fetch_optionals
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
Definition: commonobject.class.php:6007
MouvementStock\_createSubProduct
_createSubProduct($user, $idProduct, $entrepot_id, $qty, $type, $price=0, $label='', $inventorycode='')
Create movement in database for all subproducts.
Definition: mouvementstock.class.php:718
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
CommandeFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.commande.class.php:47
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Product
Class to manage products or services.
Definition: product.class.php:46
MouvementStock\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: mouvementstock.class.php:1025
MouvementStock\__construct
__construct($db)
Constructor.
Definition: mouvementstock.class.php:137
Entrepot
Class to manage warehouses.
Definition: entrepot.class.php:35
MouvementStock\getLibStatut
getLibStatut($mode=0)
Return label statut.
Definition: mouvementstock.class.php:1126
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
MouvementStock\generateDocument
generateDocument($modele, $outputlangs='', $hidedetails=0, $hidedesc=0, $hideref=0)
Create object on disk.
Definition: mouvementstock.class.php:1166
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541
Product\TYPE_SERVICE
const TYPE_SERVICE
Service.
Definition: product.class.php:504
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
dol_mktime
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...
Definition: functions.lib.php:2757
MouvementStock\getBatchCount
getBatchCount($fk_product, $batch)
Retrieve number of equipments for a product lot/serial.
Definition: mouvementstock.class.php:1208
MouvementStock\livraison
livraison($user, $fk_product, $entrepot_id, $qty, $price=0, $label='', $datem='', $eatby='', $sellby='', $batch='', $id_product_batch=0, $inventorycode='', $donotcleanemptylines=0)
Decrease stock for product and subproducts.
Definition: mouvementstock.class.php:785
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
float
div float
Buy price without taxes.
Definition: style.css.php:809