dolibarr  20.0.0-beta
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
34 {
38  public $element = 'stockmouvement';
39 
43  public $table_element = 'stock_mouvement';
44 
45 
49  public $product_id;
50 
56  public $entrepot_id;
57 
61  public $warehouse_id;
62 
63  public $qty;
64 
73  public $type;
74 
75  public $datem = '';
76  public $price;
77 
81  public $fk_user_author;
82 
86  public $label;
87 
93  public $fk_origin;
94 
98  public $origin_id;
99 
105  public $origintype;
106 
110  public $origin_type;
111  public $line_id_oject_src;
112  public $line_id_oject_origin;
113 
114 
115  public $inventorycode;
116  public $batch;
117 
118  public $line_id_object_src;
119  public $line_id_object_origin;
120 
121  public $eatby;
122  public $sellby;
123 
124 
125 
126  public $fields = array(
127  'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10, 'showoncombobox' => 1),
128  'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
129  'datem' => array('type' => 'datetime', 'label' => 'Datem', 'enabled' => 1, 'visible' => -1, 'position' => 20),
130  'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 25),
131  'fk_entrepot' => array('type' => 'integer:Entrepot:product/stock/class/entrepot.class.php', 'label' => 'Warehouse', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
132  'value' => array('type' => 'double', 'label' => 'Value', 'enabled' => 1, 'visible' => -1, 'position' => 35),
133  'price' => array('type' => 'double(24,8)', 'label' => 'Price', 'enabled' => 1, 'visible' => -1, 'position' => 40),
134  'type_mouvement' => array('type' => 'smallint(6)', 'label' => 'Type mouvement', 'enabled' => 1, 'visible' => -1, 'position' => 45),
135  'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 50),
136  'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'position' => 55),
137  'fk_origin' => array('type' => 'integer', 'label' => 'Fk origin', 'enabled' => 1, 'visible' => -1, 'position' => 60),
138  'origintype' => array('type' => 'varchar(32)', 'label' => 'Origintype', 'enabled' => 1, 'visible' => -1, 'position' => 65),
139  'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 70),
140  '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),
141  'inventorycode' => array('type' => 'varchar(128)', 'label' => 'InventoryCode', 'enabled' => 1, 'visible' => -1, 'position' => 80),
142  'batch' => array('type' => 'varchar(30)', 'label' => 'Batch', 'enabled' => 1, 'visible' => -1, 'position' => 85),
143  'eatby' => array('type' => 'date', 'label' => 'Eatby', 'enabled' => 1, 'visible' => -1, 'position' => 90),
144  'sellby' => array('type' => 'date', 'label' => 'Sellby', 'enabled' => 1, 'visible' => -1, 'position' => 95),
145  'fk_project' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Fk project', 'enabled' => 1, 'visible' => -1, 'position' => 100),
146  );
147 
148 
149 
155  public function __construct($db)
156  {
157  $this->db = $db;
158  }
159 
160  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
188  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, $force_update_batch = false)
189  {
190  // phpcs:enable
191  global $conf, $langs;
192 
193  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
194  require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
195 
196  $error = 0;
197  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=".json_encode($skip_batch));
198 
199  // Call hook at beginning
200  global $action, $hookmanager;
201  $hookmanager->initHooks(array('mouvementstock'));
202 
203  if (is_object($hookmanager)) {
204  $parameters = array(
205  'currentcontext' => 'mouvementstock',
206  'user' => &$user,
207  'fk_product' => &$fk_product,
208  'entrepot_id' => &$entrepot_id,
209  'qty' => &$qty,
210  'type' => &$type,
211  'price' => &$price,
212  'label' => &$label,
213  'inventorycode' => &$inventorycode,
214  'datem' => &$datem,
215  'eatby' => &$eatby,
216  'sellby' => &$sellby,
217  'batch' => &$batch,
218  'skip_batch' => &$skip_batch,
219  'id_product_batch' => &$id_product_batch
220  );
221  $reshook = $hookmanager->executeHooks('stockMovementCreate', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
222 
223  if ($reshook < 0) {
224  if (!empty($hookmanager->resPrint)) {
225  dol_print_error(null, $hookmanager->resPrint);
226  }
227  return $reshook;
228  } elseif ($reshook > 0) {
229  return $hookmanager->resPrint;
230  }
231  }
232  // end hook at beginning
233 
234  // Clean parameters
235  $price = price2num($price, 'MU'); // Clean value for the casse we receive a float zero value, to have it a real zero value.
236  if (empty($price)) {
237  $price = 0;
238  }
239  $now = (!empty($datem) ? $datem : dol_now());
240 
241  // Check parameters
242  if (!($fk_product > 0)) {
243  return 0;
244  }
245  if (!($entrepot_id > 0)) {
246  return 0;
247  }
248 
249  if (is_numeric($eatby) && $eatby < 0) {
250  dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
251  $this->errors[] = 'ErrorBadValueForParameterEatBy';
252  return -1;
253  }
254  if (is_numeric($sellby) && $sellby < 0) {
255  dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
256  $this->errors[] = 'ErrorBadValueForParameterSellBy';
257  return -1;
258  }
259 
260  // Set properties of movement
261  $this->product_id = $fk_product;
262  $this->entrepot_id = $entrepot_id; // deprecated
263  $this->warehouse_id = $entrepot_id;
264  $this->qty = $qty;
265  $this->type = $type;
266  $this->price = price2num($price);
267  $this->label = $label;
268  $this->inventorycode = $inventorycode;
269  $this->datem = $now;
270  $this->batch = $batch;
271 
272  $mvid = 0;
273 
274  $product = new Product($this->db);
275 
276  $result = $product->fetch($fk_product);
277  if ($result < 0) {
278  $this->error = $product->error;
279  $this->errors = $product->errors;
280  dol_print_error(null, "Failed to fetch product");
281  return -1;
282  }
283  if ($product->id <= 0) { // Can happen if database is corrupted (a product id exist in stock with product that has been removed)
284  return 0;
285  }
286 
287  // Define if we must make the stock change (If product type is a service or if stock is used also for services)
288  // Only record into stock tables will be disabled by this (the rest like writing into lot table or movement of subproucts are done)
289  $movestock = 0;
290  if ($product->type != Product::TYPE_SERVICE || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
291  $movestock = 1;
292  }
293 
294  $this->db->begin();
295 
296  // Set value $product->stock_reel and detail per warehouse into $product->stock_warehouse array
297  if ($movestock) {
298  $product->load_stock('novirtual');
299  }
300 
301  // Test if product require batch data. If yes, and there is not or values are not correct, we throw an error.
302  if (isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
303  if (empty($batch)) {
304  $langs->load("errors");
305  $this->errors[] = $langs->transnoentitiesnoconv("ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
306  dol_syslog("Try to make a movement of a product with status_batch on without any batch data");
307 
308  $this->db->rollback();
309  return -2;
310  }
311 
312  // Check table llx_product_lot from batchnumber for same product
313  // If found and eatby/sellby defined into table and provided and differs, return error
314  // If found and eatby/sellby defined into table and not provided, we take value from table
315  // If found and eatby/sellby not defined into table and provided, we update table
316  // If found and eatby/sellby not defined into table and not provided, we do nothing
317  // If not found, we add record
318  $sql = "SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix()."product_lot as pb";
319  $sql .= " WHERE pb.fk_product = ".((int) $fk_product)." AND pb.batch = '".$this->db->escape($batch)."'";
320 
321  dol_syslog(get_class($this)."::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
322 
323  $resql = $this->db->query($sql);
324  if ($resql) {
325  $num = $this->db->num_rows($resql);
326  $i = 0;
327  if ($num > 0) {
328  while ($i < $num) {
329  $obj = $this->db->fetch_object($resql);
330  if ($obj->eatby) {
331  if ($eatby) {
332  $tmparray = dol_getdate($eatby, true);
333  $eatbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
334  if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) { // 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  $langs->load("stocks");
337  $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->eatby), 'dayhour'), dol_print_date($eatbywithouthour, 'dayhour'));
338  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);
339  $this->db->rollback();
340  return -3;
341  }
342  } else {
343  $eatby = $obj->eatby; // If found and eatby/sellby defined into table and not provided, we take value from table
344  }
345  } else {
346  if ($eatby) { // If found and eatby/sellby not defined into table and provided, we update table
347  $productlot = new Productlot($this->db);
348  $result = $productlot->fetch($obj->rowid);
349  $productlot->eatby = $eatby;
350  $result = $productlot->update($user);
351  if ($result <= 0) {
352  $this->error = $productlot->error;
353  $this->errors = $productlot->errors;
354  $this->db->rollback();
355  return -5;
356  }
357  }
358  }
359  if ($obj->sellby) {
360  if ($sellby) {
361  $tmparray = dol_getdate($sellby, true);
362  $sellbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
363  if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) { // We test date without hours and with hours for backward compatibility
364  // If found and eatby/sellby defined into table and provided and differs, return error
365  $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby));
366  dol_syslog($langs->transnoentities("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby)), LOG_ERR);
367  $this->db->rollback();
368  return -3;
369  }
370  } else {
371  $sellby = $obj->sellby; // If found and eatby/sellby defined into table and not provided, we take value from table
372  }
373  } else {
374  if ($sellby) { // If found and eatby/sellby not defined into table and provided, we update table
375  $productlot = new Productlot($this->db);
376  $result = $productlot->fetch($obj->rowid);
377  $productlot->sellby = $sellby;
378  $result = $productlot->update($user);
379  if ($result <= 0) {
380  $this->error = $productlot->error;
381  $this->errors = $productlot->errors;
382  $this->db->rollback();
383  return -5;
384  }
385  }
386  }
387 
388  $i++;
389  }
390  } else { // If not found, we add record
391  $productlot = new Productlot($this->db);
392  $productlot->origin = !empty($this->origin_type) ? $this->origin_type : '';
393  $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
394  $productlot->entity = $conf->entity;
395  $productlot->fk_product = $fk_product;
396  $productlot->batch = $batch;
397  // If we are here = first time we manage this batch, so we used dates provided by users to create lot
398  $productlot->eatby = $eatby;
399  $productlot->sellby = $sellby;
400  $result = $productlot->create($user);
401  if ($result <= 0) {
402  $this->error = $productlot->error;
403  $this->errors = $productlot->errors;
404  $this->db->rollback();
405  return -4;
406  }
407  }
408  } else {
409  dol_print_error($this->db);
410  $this->db->rollback();
411  return -1;
412  }
413  }
414 
415  // Check if stock is enough when qty is < 0
416  // Note that qty should be > 0 with type 0 or 3, < 0 with type 1 or 2.
417  if ($movestock && $qty < 0 && !getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER')) {
418  if (isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
419  $foundforbatch = 0;
420  $qtyisnotenough = 0;
421 
422  foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
423  if ((string) $batch != (string) $batchcursor) { // Lot '59' must be different than lot '59c'
424  continue;
425  }
426 
427  $foundforbatch = 1;
428  if ($prodbatch->qty < abs($qty)) {
429  $qtyisnotenough = $prodbatch->qty;
430  }
431  break;
432  }
433  if (!$foundforbatch || $qtyisnotenough) {
434  $langs->load("stocks");
435  include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
436  $tmpwarehouse = new Entrepot($this->db);
437  $tmpwarehouse->fetch($entrepot_id);
438 
439  $this->error = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
440  $this->errors[] = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
441  $this->db->rollback();
442  return -8;
443  }
444  } else {
445  if (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty)) {
446  $langs->load("stocks");
447  $this->error = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
448  $this->errors[] = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
449  $this->db->rollback();
450  return -8;
451  }
452  }
453  }
454 
455  if ($movestock) { // Change stock for current product, change for subproduct is done after
456  // Set $origin_type, origin_id and fk_project
457  $fk_project = $this->fk_project;
458  if (!empty($this->origin_type)) { // This is set by caller for tracking reason
459  $origin_type = $this->origin_type;
460  $origin_id = $this->origin_id;
461  if (empty($fk_project) && $origin_type == 'project') {
462  $fk_project = $origin_id;
463  $origin_type = '';
464  $origin_id = 0;
465  }
466  } else {
467  $fk_project = 0;
468  $origin_type = '';
469  $origin_id = 0;
470  }
471 
472  $sql = "INSERT INTO ".$this->db->prefix()."stock_mouvement(";
473  $sql .= " datem, fk_product, batch, eatby, sellby,";
474  $sql .= " fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
475  $sql .= ")";
476  $sql .= " VALUES ('".$this->db->idate($this->datem)."', ".((int) $this->product_id).", ";
477  $sql .= " ".($batch ? "'".$this->db->escape($batch)."'" : "null").", ";
478  $sql .= " ".($eatby ? "'".$this->db->idate($eatby)."'" : "null").", ";
479  $sql .= " ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", ";
480  $sql .= " ".((int) $this->entrepot_id).", ".((float) $this->qty).", ".((int) $this->type).",";
481  $sql .= " ".((int) $user->id).",";
482  $sql .= " '".$this->db->escape($label)."',";
483  $sql .= " ".($inventorycode ? "'".$this->db->escape($inventorycode)."'" : "null").",";
484  $sql .= " ".((float) price2num($price)).",";
485  $sql .= " ".((int) $origin_id).",";
486  $sql .= " '".$this->db->escape($origin_type)."',";
487  $sql .= " ".((int) $fk_project);
488  $sql .= ")";
489 
490  dol_syslog(get_class($this)."::_create insert record into stock_mouvement", LOG_DEBUG);
491  $resql = $this->db->query($sql);
492 
493  if ($resql) {
494  $mvid = $this->db->last_insert_id($this->db->prefix()."stock_mouvement");
495  $this->id = $mvid;
496  } else {
497  $this->error = $this->db->lasterror();
498  $this->errors[] = $this->error;
499  $error = -1;
500  }
501 
502  // Define current values for qty and pmp
503  $oldqty = $product->stock_reel;
504  $oldpmp = $product->pmp;
505  $oldqtywarehouse = 0;
506 
507  // Test if there is already a record for couple (warehouse / product), so later we will make an update or create.
508  $alreadyarecord = 0;
509  if (!$error) {
510  $sql = "SELECT rowid, reel FROM ".$this->db->prefix()."product_stock";
511  $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product); // This is a unique key
512 
513  dol_syslog(get_class($this)."::_create check if a record already exists in product_stock", LOG_DEBUG);
514  $resql = $this->db->query($sql);
515  if ($resql) {
516  $obj = $this->db->fetch_object($resql);
517  if ($obj) {
518  $alreadyarecord = 1;
519  $oldqtywarehouse = $obj->reel;
520  $fk_product_stock = $obj->rowid;
521  }
522  $this->db->free($resql);
523  } else {
524  $this->errors[] = $this->db->lasterror();
525  $error = -2;
526  }
527  }
528 
529  // Calculate new AWP (PMP)
530  $newpmp = 0;
531  if (!$error) {
532  if ($type == 0 || $type == 3) {
533  // After a stock increase
534  // Note: PMP is calculated on stock input only (type of movement = 0 or 3). If type == 0 or 3, qty should be > 0.
535  // Note: Price should always be >0 or 0. PMP should be always >0 (calculated on input)
536  if ($price > 0 || (getDolGlobalString('STOCK_UPDATE_AWP_EVEN_WHEN_ENTRY_PRICE_IS_NULL') && $price == 0 && in_array($this->origin_type, array('order_supplier', 'invoice_supplier')))) {
537  $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
538  // We make a test on oldpmp>0 to avoid to use normal rule on old data with no pmp field defined
539  if ($oldpmp > 0) {
540  $newpmp = price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty), 'MU');
541  } else {
542  $newpmp = $price; // For this product, PMP was not yet set. We set it to input price.
543  }
544  //print "oldqtytouse=".$oldqtytouse." oldpmp=".$oldpmp." oldqtywarehousetouse=".$oldqtywarehousetouse." ";
545  //print "qty=".$qty." newpmp=".$newpmp;
546  //exit;
547  } else {
548  $newpmp = $oldpmp;
549  }
550  } else {
551  // ($type == 1 || $type == 2)
552  // -> After a stock decrease, we don't change value of the AWP/PMP of a product.
553  // else
554  // Type of movement unknown
555  $newpmp = $oldpmp;
556  }
557  }
558  // Update stock quantity
559  if (!$error) {
560  if ($alreadyarecord > 0) {
561  $sql = "UPDATE ".$this->db->prefix()."product_stock SET reel = " . ((float) $oldqtywarehouse + (float) $qty);
562  $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product);
563  } else {
564  $sql = "INSERT INTO ".$this->db->prefix()."product_stock";
565  $sql .= " (reel, fk_entrepot, fk_product) VALUES ";
566  $sql .= " (".((float) $qty).", ".((int) $entrepot_id).", ".((int) $fk_product).")";
567  }
568 
569  dol_syslog(get_class($this)."::_create update stock value", LOG_DEBUG);
570  $resql = $this->db->query($sql);
571  if (!$resql) {
572  $this->errors[] = $this->db->lasterror();
573  $error = -3;
574  } elseif (empty($fk_product_stock)) {
575  $fk_product_stock = $this->db->last_insert_id($this->db->prefix()."product_stock");
576  }
577  }
578 
579  // Update detail of stock for the lot.
580  if (!$error && isModEnabled('productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
581  if ($id_product_batch > 0) {
582  $result = $this->createBatch($id_product_batch, $qty);
583  if ($result == -2 && $fk_product_stock > 0) { // The entry for this product batch does not exists anymore, bu we already have a llx_product_stock, so we recreate the batch entry in product_batch
584  $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch);
585  $result = $this->createBatch($param_batch, $qty);
586  }
587  } else {
588  $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch);
589  $result = $this->createBatch($param_batch, $qty);
590  }
591  if ($result < 0) {
592  $error++;
593  }
594  }
595 
596  // Update PMP and denormalized value of stock qty at product level
597  if (!$error) {
598  $newpmp = price2num($newpmp, 'MU');
599 
600  // $sql = "UPDATE ".$this->db->prefix()."product SET pmp = ".$newpmp.", stock = ".$this->db->ifsql("stock IS NULL", 0, "stock") . " + ".$qty;
601  // $sql.= " WHERE rowid = ".((int) $fk_product);
602  // Update pmp + denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
603  $sql = "UPDATE ".$this->db->prefix()."product as p SET pmp = ".((float) $newpmp).",";
604  $sql .= " stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix()."product_stock as ps WHERE ps.fk_product = p.rowid)";
605  $sql .= " WHERE rowid = ".((int) $fk_product);
606 
607  dol_syslog(get_class($this)."::_create update AWP", LOG_DEBUG);
608  $resql = $this->db->query($sql);
609  if (!$resql) {
610  $this->errors[] = $this->db->lasterror();
611  $error = -4;
612  }
613  }
614 
615  if (empty($donotcleanemptylines)) {
616  // 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
617  // having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
618  $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)";
619  $resql = $this->db->query($sql);
620  // We do not test error, it can fails if there is child in batch details
621  }
622  }
623 
624  // Add movement for sub products (recursive call)
625  if (!$error && getDolGlobalString('PRODUIT_SOUSPRODUITS') && !getDolGlobalString('INDEPENDANT_SUBPRODUCT_STOCK') && empty($disablestockchangeforsubproduct)) {
626  $error = $this->_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem); // we use 0 as price, because AWP must not change for subproduct
627  }
628 
629  if ($movestock && !$error) {
630  // Call trigger
631  $result = $this->call_trigger('STOCK_MOVEMENT', $user);
632  if ($result < 0) {
633  $error++;
634  }
635  // End call triggers
636 
637  // Check unicity for serial numbered equipment once all movement were done.
638  if (!$error && isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
639  if ($product->status_batch == 2 && $qty > 0) { // We check only if we increased qty
640  if ($this->getBatchCount($fk_product, $batch) > 1) {
641  $error++;
642  $this->errors[] = $langs->trans("TooManyQtyForSerialNumber", $product->ref, $batch);
643  }
644  }
645  }
646  }
647 
648  if (!$error) {
649  $this->db->commit();
650  return $mvid;
651  } else {
652  $this->db->rollback();
653  dol_syslog(get_class($this)."::_create error code=".$error, LOG_ERR);
654  return -6;
655  }
656  }
657 
658 
659 
667  public function fetch($id)
668  {
669  dol_syslog(__METHOD__, LOG_DEBUG);
670 
671  $sql = "SELECT";
672  $sql .= " t.rowid,";
673  $sql .= " t.tms,";
674  $sql .= " t.datem,";
675  $sql .= " t.fk_product,";
676  $sql .= " t.fk_entrepot,";
677  $sql .= " t.value,";
678  $sql .= " t.price,";
679  $sql .= " t.type_mouvement,";
680  $sql .= " t.fk_user_author,";
681  $sql .= " t.label,";
682  $sql .= " t.fk_origin as origin_id,";
683  $sql .= " t.origintype as origin_type,";
684  $sql .= " t.inventorycode,";
685  $sql .= " t.batch,";
686  $sql .= " t.eatby,";
687  $sql .= " t.sellby,";
688  $sql .= " t.fk_projet as fk_project";
689  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
690  $sql .= " WHERE t.rowid = ".((int) $id);
691 
692  $resql = $this->db->query($sql);
693  if ($resql) {
694  $numrows = $this->db->num_rows($resql);
695  if ($numrows) {
696  $obj = $this->db->fetch_object($resql);
697 
698  $this->id = $obj->rowid;
699 
700  $this->product_id = $obj->fk_product;
701  $this->warehouse_id = $obj->fk_entrepot;
702  $this->qty = $obj->value;
703  $this->type = $obj->type_mouvement;
704 
705  $this->tms = $this->db->jdate($obj->tms);
706  $this->datem = $this->db->jdate($obj->datem);
707  $this->price = $obj->price;
708  $this->fk_user_author = $obj->fk_user_author;
709  $this->label = $obj->label;
710  $this->fk_origin = $obj->origin_id; // For backward compatibility
711  $this->origintype = $obj->origin_type; // For backward compatibility
712  $this->origin_id = $obj->origin_id;
713  $this->origin_type = $obj->origin_type;
714  $this->inventorycode = $obj->inventorycode;
715  $this->batch = $obj->batch;
716  $this->eatby = $this->db->jdate($obj->eatby);
717  $this->sellby = $this->db->jdate($obj->sellby);
718  $this->fk_project = $obj->fk_project;
719  }
720 
721  // Retrieve all extrafield
722  $this->fetch_optionals();
723 
724  // $this->fetch_lines();
725 
726  $this->db->free($resql);
727 
728  if ($numrows) {
729  return 1;
730  } else {
731  return 0;
732  }
733  } else {
734  $this->errors[] = 'Error '.$this->db->lasterror();
735  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
736 
737  return -1;
738  }
739  }
740 
741 
742 
743 
758  private function _createSubProduct($user, $idProduct, $entrepot_id, $qty, $type, $price = 0, $label = '', $inventorycode = '', $datem = '')
759  {
760  global $langs;
761 
762  $error = 0;
763  $pids = array();
764  $pqtys = array();
765 
766  $sql = "SELECT fk_product_pere, fk_product_fils, qty";
767  $sql .= " FROM ".$this->db->prefix()."product_association";
768  $sql .= " WHERE fk_product_pere = ".((int) $idProduct);
769  $sql .= " AND incdec = 1";
770 
771  dol_syslog(get_class($this)."::_createSubProduct for parent product ".$idProduct, LOG_DEBUG);
772  $resql = $this->db->query($sql);
773  if ($resql) {
774  $i = 0;
775  while ($obj = $this->db->fetch_object($resql)) {
776  $pids[$i] = $obj->fk_product_fils;
777  $pqtys[$i] = $obj->qty;
778  $i++;
779  }
780  $this->db->free($resql);
781  } else {
782  $error = -2;
783  }
784 
785  // Create movement for each subproduct
786  foreach ($pids as $key => $value) {
787  if (!$error) {
788  $tmpmove = dol_clone($this, 1);
789 
790  $result = $tmpmove->_create($user, $pids[$key], $entrepot_id, ($qty * $pqtys[$key]), $type, 0, $label, $inventorycode, $datem); // This will also call _createSubProduct making this recursive
791  if ($result < 0) {
792  $this->error = $tmpmove->error;
793  $this->errors = array_merge($this->errors, $tmpmove->errors);
794  if ($result == -2) {
795  $this->errors[] = $langs->trans("ErrorNoteAlsoThatSubProductCantBeFollowedByLot");
796  }
797  $error = $result;
798  }
799  unset($tmpmove);
800  }
801  }
802 
803  return $error;
804  }
805 
806 
825  public function livraison($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $datem = '', $eatby = '', $sellby = '', $batch = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
826  {
827  global $conf;
828 
829  $skip_batch = empty($conf->productbatch->enabled);
830 
831  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);
832  }
833 
852  public function reception($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $eatby = '', $sellby = '', $batch = '', $datem = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
853  {
854  global $conf;
855 
856  $skip_batch = empty($conf->productbatch->enabled);
857 
858  return $this->_create($user, $fk_product, $entrepot_id, $qty, 3, $price, $label, $inventorycode, $datem, $eatby, $sellby, $batch, $skip_batch, $id_product_batch, 0, $donotcleanemptylines);
859  }
860 
868  public function calculateBalanceForProductBefore($productidselected, $datebefore)
869  {
870  $nb = 0;
871 
872  $sql = "SELECT SUM(value) as nb from ".$this->db->prefix()."stock_mouvement";
873  $sql .= " WHERE fk_product = ".((int) $productidselected);
874  $sql .= " AND datem < '".$this->db->idate($datebefore)."'";
875 
876  dol_syslog(get_class($this).__METHOD__, LOG_DEBUG);
877  $resql = $this->db->query($sql);
878  if ($resql) {
879  $obj = $this->db->fetch_object($resql);
880  if ($obj) {
881  $nb = $obj->nb;
882  }
883  return (empty($nb) ? 0 : $nb);
884  } else {
885  dol_print_error($this->db);
886  return -1;
887  }
888  }
889 
899  private function createBatch($dluo, $qty)
900  {
901  global $user, $langs;
902 
903  $langs->load('productbatch');
904 
905  $pdluo = new Productbatch($this->db);
906 
907  $result = 0;
908 
909  // Try to find an existing record with same batch number or id
910  if (is_numeric($dluo)) {
911  $result = $pdluo->fetch($dluo);
912  if (empty($pdluo->id)) {
913  // We didn't find the line. May be it was deleted before by a previous move in same transaction.
914  $this->error = $langs->trans('CantMoveNonExistantSerial');
915  $this->errors[] = $this->error;
916  $result = -2;
917  }
918  } elseif (is_array($dluo)) {
919  if (isset($dluo['fk_product_stock'])) {
920  $vfk_product_stock = $dluo['fk_product_stock'];
921  $vbatchnumber = $dluo['batchnumber'];
922 
923  $result = $pdluo->find($vfk_product_stock, '', '', $vbatchnumber); // Search on batch number only (eatby and sellby are deprecated here)
924  } else {
925  dol_syslog(get_class($this)."::createBatch array param dluo must contain at least key fk_product_stock", LOG_ERR);
926  $result = -1;
927  }
928  } else {
929  dol_syslog(get_class($this)."::createBatch error invalid param dluo", LOG_ERR);
930  $result = -1;
931  }
932 
933  if ($result >= 0) {
934  // No error
935  if ($pdluo->id > 0) { // product_batch record found
936  //print "Avant ".$pdluo->qty." Apres ".($pdluo->qty + $qty)."<br>";
937  $pdluo->qty += $qty;
938  if ($pdluo->qty == 0) {
939  $result = $pdluo->delete($user, 1);
940  } else {
941  $result = $pdluo->update($user, 1);
942  }
943  } else { // product_batch record not found
944  $pdluo->fk_product_stock = $vfk_product_stock;
945  $pdluo->qty = $qty;
946  $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.
947  $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.
948  $pdluo->batch = $vbatchnumber;
949 
950  $result = $pdluo->create($user, 1);
951  if ($result < 0) {
952  $this->error = $pdluo->error;
953  $this->errors = $pdluo->errors;
954  }
955  }
956  }
957 
958  return $result;
959  }
960 
961  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
969  public function get_origin($origin_id, $origin_type)
970  {
971  // phpcs:enable
972  $origin = '';
973 
974  switch ($origin_type) {
975  case 'commande':
976  require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
977  $origin = new Commande($this->db);
978  break;
979  case 'shipping':
980  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
981  $origin = new Expedition($this->db);
982  break;
983  case 'facture':
984  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
985  $origin = new Facture($this->db);
986  break;
987  case 'order_supplier':
988  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
989  $origin = new CommandeFournisseur($this->db);
990  break;
991  case 'invoice_supplier':
992  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
993  $origin = new FactureFournisseur($this->db);
994  break;
995  case 'project':
996  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
997  $origin = new Project($this->db);
998  break;
999  case 'mo':
1000  require_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
1001  $origin = new Mo($this->db);
1002  break;
1003  case 'user':
1004  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1005  $origin = new User($this->db);
1006  break;
1007  case 'reception':
1008  require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
1009  $origin = new Reception($this->db);
1010  break;
1011  case 'inventory':
1012  require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
1013  $origin = new Inventory($this->db);
1014  break;
1015  default:
1016  if ($origin_type) {
1017  // Separate origin_type with "@" : left part is class name, right part is module name
1018  $origin_type_array = explode('@', $origin_type);
1019  $classname = $origin_type_array[0];
1020  $modulename = empty($origin_type_array[1]) ? strtolower($classname) : $origin_type_array[1];
1021 
1022  $result = dol_include_once('/'.$modulename.'/class/'.$classname.'.class.php');
1023 
1024  if ($result) {
1025  $classname = ucfirst($classname);
1026  $origin = new $classname($this->db);
1027  }
1028  }
1029  break;
1030  }
1031 
1032  if (empty($origin) || !is_object($origin)) {
1033  return '';
1034  }
1035 
1036  if ($origin->fetch($origin_id) > 0) {
1037  return $origin->getNomUrl(1);
1038  }
1039 
1040  return '';
1041  }
1042 
1053  public function setOrigin($origin_element, $origin_id, $line_id_object_src = 0, $line_id_object_origin = 0)
1054  {
1055  $this->origin_type = $origin_element;
1056  $this->origin_id = $origin_id;
1057  $this->line_id_object_src = $line_id_object_src;
1058  $this->line_id_object_origin = $line_id_object_origin;
1059  // For backward compatibility
1060  $this->origintype = $origin_element;
1061  $this->fk_origin = $origin_id;
1062  }
1063 
1064 
1072  public function initAsSpecimen()
1073  {
1074  // Initialize parameters
1075  $this->id = 0;
1076 
1077  // There is no specific properties. All data into insert are provided as method parameter.
1078 
1079  return 1;
1080  }
1081 
1088  public function getTypeMovement($withlabel = 0)
1089  {
1090  global $langs;
1091 
1092  $s = '';
1093  switch ($this->type) {
1094  case "0":
1095  $s = '<span class="fa fa-level-down-alt stockmovemententry stockmovementtransfer" title="'.$langs->trans('StockIncreaseAfterCorrectTransfer').'"></span>';
1096  if ($withlabel) {
1097  $s .= $langs->trans('StockIncreaseAfterCorrectTransfer');
1098  }
1099  break;
1100  case "1":
1101  $s = '<span class="fa fa-level-up-alt stockmovementexit stockmovementtransfer" title="'.$langs->trans('StockDecreaseAfterCorrectTransfer').'"></span>';
1102  if ($withlabel) {
1103  $s .= $langs->trans('StockDecreaseAfterCorrectTransfer');
1104  }
1105  break;
1106  case "2":
1107  $s = '<span class="fa fa-long-arrow-alt-up stockmovementexit stockmovement" title="'.$langs->trans('StockDecrease').'"></span>';
1108  if ($withlabel) {
1109  $s .= $langs->trans('StockDecrease');
1110  }
1111  break;
1112  case "3":
1113  $s = '<span class="fa fa-long-arrow-alt-down stockmovemententry stockmovement" title="'.$langs->trans('StockIncrease').'"></span>';
1114  if ($withlabel) {
1115  $s .= $langs->trans('StockIncrease');
1116  }
1117  break;
1118  }
1119 
1120  return $s;
1121  }
1122 
1134  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
1135  {
1136  global $langs, $conf, $db;
1137 
1138  $result = '';
1139 
1140  $label = img_picto('', 'stock', 'class="pictofixedwidth"').'<u>'.$langs->trans("StockMovement").'</u>';
1141  $label .= '<div width="100%">';
1142  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->id;
1143  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1144  $qtylabel = (($this->qty > 0) ? '<span class="stockmovemententry">+' : '<span class="stockmovementexit">') . $this->qty . '</span>';
1145  $label .= '<br><b>'.$langs->trans('Qty').':</b> ' . $qtylabel;
1146  if ($this->batch) {
1147  $label .= '<br><b>'.$langs->trans('Batch').':</b> '.$this->batch;
1148  }
1149  /* TODO Get also warehouse label in a property instead of id
1150  if ($this->warehouse_id > 0) {
1151  $label .= '<br><b>'.$langs->trans('Warehouse').':</b> '.$this->warehouse_id;
1152  }*/
1153  $label .= '</div>';
1154 
1155  // Link to page of warehouse tab
1156  if ($option == 'movements') {
1157  $url = DOL_URL_ROOT.'/product/stock/movement_list.php?search_ref='.$this->id;
1158  } else {
1159  $url = DOL_URL_ROOT.'/product/stock/movement_list.php?id='.$this->warehouse_id.'&msid='.$this->id;
1160  }
1161 
1162  $link = '<a href="'.$url.'"'.($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
1163  $link .= '>';
1164  $linkend = '</a>';
1165 
1166  if ($withpicto) {
1167  $result .= ($link.img_object(($notooltip ? '' : $label), 'stock', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
1168  if ($withpicto != 2) {
1169  $result .= ' ';
1170  }
1171  }
1172  $result .= $link.$this->id.$linkend;
1173  return $result;
1174  }
1175 
1182  public function getLibStatut($mode = 0)
1183  {
1184  return $this->LibStatut($mode);
1185  }
1186 
1187  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1194  public function LibStatut($mode = 0)
1195  {
1196  // phpcs:enable
1197  global $langs;
1198 
1199  if ($mode == 0 || $mode == 1) {
1200  return $langs->trans('StatusNotApplicable');
1201  } elseif ($mode == 2) {
1202  return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1203  } elseif ($mode == 3) {
1204  return img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1205  } elseif ($mode == 4) {
1206  return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1207  } elseif ($mode == 5) {
1208  return $langs->trans('StatusNotApplicable').' '.img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1209  }
1210 
1211  return 'Bad value for mode';
1212  }
1213 
1224  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1225  {
1226  global $conf, $user, $langs;
1227 
1228  $langs->load("stocks");
1229  $outputlangs->load("products");
1230 
1231  if (!dol_strlen($modele)) {
1232  $modele = 'stdmovement';
1233 
1234  if ($this->model_pdf) {
1235  $modele = $this->model_pdf;
1236  } elseif (getDolGlobalString('MOUVEMENT_ADDON_PDF')) {
1237  $modele = getDolGlobalString('MOUVEMENT_ADDON_PDF');
1238  }
1239  }
1240 
1241  $modelpath = "core/modules/stock/doc/";
1242 
1243  return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1244  }
1245 
1253  public function delete(User $user, $notrigger = 0)
1254  {
1255  return $this->deleteCommon($user, $notrigger);
1256  //return $this->deleteCommon($user, $notrigger, 1);
1257  }
1258 
1266  private function getBatchCount($fk_product, $batch)
1267  {
1268  $cpt = 0;
1269 
1270  $sql = "SELECT sum(pb.qty) as cpt";
1271  $sql .= " FROM ".$this->db->prefix()."product_batch as pb";
1272  $sql .= " INNER JOIN ".$this->db->prefix()."product_stock as ps ON ps.rowid = pb.fk_product_stock";
1273  $sql .= " WHERE ps.fk_product = " . ((int) $fk_product);
1274  $sql .= " AND pb.batch = '" . $this->db->escape($batch) . "'";
1275 
1276  $result = $this->db->query($sql);
1277  if ($result) {
1278  if ($this->db->num_rows($result)) {
1279  $obj = $this->db->fetch_object($result);
1280  $cpt = $obj->cpt;
1281  }
1282 
1283  $this->db->free($result);
1284  } else {
1285  dol_print_error($this->db);
1286  return -1;
1287  }
1288 
1289  return $cpt;
1290  }
1291 
1296  public function reverseMouvement()
1297  {
1298  $formattedDate = "REVERTMV" .dol_print_date($this->datem, '%Y%m%d%His');
1299  if ($this->label == 'Annulation movement ID'.$this->id) {
1300  return -1;
1301  }
1302  if ($this->inventorycode == $formattedDate) {
1303  return -1;
1304  }
1305 
1306  $sql = "UPDATE ".$this->db->prefix()."stock_mouvement SET";
1307  $sql .= " label = 'Annulation movement ID ".((int) $this->id)."',";
1308  $sql .= "inventorycode = '".($formattedDate)."'";
1309  $sql .= " WHERE rowid = ".((int) $this->id);
1310 
1311  $resql = $this->db->query($sql);
1312 
1313  if ($resql) {
1314  $this->db->commit();
1315  return 1;
1316  } else {
1317  $this->db->rollback();
1318  return -1;
1319  }
1320  }
1321 }
Class to manage predefined suppliers products.
Class to manage customers orders.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage warehouses.
Class to manage shipments.
Class to manage suppliers invoices.
Class to manage invoices.
Class for Inventory.
Class for Mo.
Definition: mo.class.php:36
Class to manage stock movements.
__construct($db)
Constructor.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create object on disk.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link (with optionally the picto) Use this->id,this->lastname, this->firstname.
setOrigin($origin_element, $origin_id, $line_id_object_src=0, $line_id_object_origin=0)
Set attribute origin_type and fk_origin to object.
LibStatut($mode=0)
Return the label of the status.
createBatch($dluo, $qty)
Create or update batch record (update table llx_product_batch).
getBatchCount($fk_product, $batch)
Retrieve number of equipment for a product lot/serial.
get_origin($origin_id, $origin_type)
Return Url link of origin object.
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.
_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, $force_update_batch=false)
Add a movement of stock (in one direction only).
fetch($id)
Load object in memory from the database.
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.
_createSubProduct($user, $idProduct, $entrepot_id, $qty, $type, $price=0, $label='', $inventorycode='', $datem='')
Create movement in database for all subproducts.
initAsSpecimen()
Initialise an instance with random values.
getTypeMovement($withlabel=0)
Return html string with picto for type of movement.
calculateBalanceForProductBefore($productidselected, $datebefore)
Count number of product in stock before a specific date.
getLibStatut($mode=0)
Return label statut.
reverseMouvement()
reverse movement for object by updating infos
Class to manage products or services.
const TYPE_SERVICE
Service.
Manage record for batch number management.
Class with list of lots and properties.
Class to manage projects.
Class to manage receptions.
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
div float
Buy price without taxes.
Definition: style.css.php:960
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123