dolibarr 21.0.0-alpha
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
66 public $qty;
67
76 public $type;
77
81 public $datem = '';
82 public $price;
83
87 public $fk_user_author;
88
92 public $label;
93
99 public $fk_origin;
100
104 public $origin_id;
105
111 public $origintype;
112
116 public $origin_type;
117 public $line_id_oject_src;
118 public $line_id_oject_origin;
119
123 public $inventorycode;
124
128 public $batch;
129
130 public $line_id_object_src;
131 public $line_id_object_origin;
132
136 public $eatby;
137
141 public $sellby;
142
143
144
145 public $fields = array(
146 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10, 'showoncombobox' => 1),
147 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
148 'datem' => array('type' => 'datetime', 'label' => 'Datem', 'enabled' => 1, 'visible' => -1, 'position' => 20),
149 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'Product', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 25),
150 'fk_entrepot' => array('type' => 'integer:Entrepot:product/stock/class/entrepot.class.php', 'label' => 'Warehouse', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
151 'value' => array('type' => 'double', 'label' => 'Value', 'enabled' => 1, 'visible' => -1, 'position' => 35),
152 'price' => array('type' => 'double(24,8)', 'label' => 'Price', 'enabled' => 1, 'visible' => -1, 'position' => 40),
153 'type_mouvement' => array('type' => 'smallint(6)', 'label' => 'Type mouvement', 'enabled' => 1, 'visible' => -1, 'position' => 45),
154 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 50),
155 'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'position' => 55),
156 'fk_origin' => array('type' => 'integer', 'label' => 'Fk origin', 'enabled' => 1, 'visible' => -1, 'position' => 60),
157 'origintype' => array('type' => 'varchar(32)', 'label' => 'Origintype', 'enabled' => 1, 'visible' => -1, 'position' => 65),
158 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 70),
159 '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),
160 'inventorycode' => array('type' => 'varchar(128)', 'label' => 'InventoryCode', 'enabled' => 1, 'visible' => -1, 'position' => 80),
161 'batch' => array('type' => 'varchar(30)', 'label' => 'Batch', 'enabled' => 1, 'visible' => -1, 'position' => 85),
162 'eatby' => array('type' => 'date', 'label' => 'Eatby', 'enabled' => 1, 'visible' => -1, 'position' => 90),
163 'sellby' => array('type' => 'date', 'label' => 'Sellby', 'enabled' => 1, 'visible' => -1, 'position' => 95),
164 'fk_project' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Fk project', 'enabled' => 1, 'visible' => -1, 'position' => 100),
165 );
166
167
168
174 public function __construct($db)
175 {
176 $this->db = $db;
177 }
178
179 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
207 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)
208 {
209 // phpcs:enable
210 global $conf, $langs;
211
212 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
213 require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
214
215 $error = 0;
216 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));
217
218 // Call hook at beginning
219 global $action, $hookmanager;
220 $hookmanager->initHooks(array('mouvementstock'));
221
222 if (is_object($hookmanager)) {
223 $parameters = array(
224 'currentcontext' => 'mouvementstock',
225 'user' => &$user,
226 'fk_product' => &$fk_product,
227 'entrepot_id' => &$entrepot_id,
228 'qty' => &$qty,
229 'type' => &$type,
230 'price' => &$price,
231 'label' => &$label,
232 'inventorycode' => &$inventorycode,
233 'datem' => &$datem,
234 'eatby' => &$eatby,
235 'sellby' => &$sellby,
236 'batch' => &$batch,
237 'skip_batch' => &$skip_batch,
238 'id_product_batch' => &$id_product_batch
239 );
240 $reshook = $hookmanager->executeHooks('stockMovementCreate', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
241
242 if ($reshook < 0) {
243 if (!empty($hookmanager->resPrint)) {
244 dol_print_error(null, $hookmanager->resPrint);
245 }
246 return $reshook;
247 } elseif ($reshook > 0) {
248 return $reshook;
249 }
250 }
251 // end hook at beginning
252
253 // Clean parameters
254 $price = price2num($price, 'MU'); // Clean value for the casse we receive a float zero value, to have it a real zero value.
255 if (empty($price)) {
256 $price = 0;
257 }
258 $now = (!empty($datem) ? $datem : dol_now());
259
260 // Check parameters
261 if (!($fk_product > 0)) {
262 return 0;
263 }
264 if (!($entrepot_id > 0)) {
265 return 0;
266 }
267
268 if (is_numeric($eatby) && $eatby < 0) {
269 dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
270 $this->errors[] = 'ErrorBadValueForParameterEatBy';
271 return -1;
272 }
273 if (is_numeric($sellby) && $sellby < 0) {
274 dol_syslog(get_class($this)."::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
275 $this->errors[] = 'ErrorBadValueForParameterSellBy';
276 return -1;
277 }
278
279 // Set properties of movement
280 $this->product_id = $fk_product;
281 $this->entrepot_id = $entrepot_id; // deprecated
282 $this->warehouse_id = $entrepot_id;
283 $this->qty = $qty;
284 $this->type = $type;
285 $this->price = price2num($price);
286 $this->label = $label;
287 $this->inventorycode = $inventorycode;
288 $this->datem = $now;
289 $this->batch = $batch;
290
291 $mvid = 0;
292
293 $product = new Product($this->db);
294
295 $result = $product->fetch($fk_product);
296 if ($result < 0) {
297 $this->error = $product->error;
298 $this->errors = $product->errors;
299 dol_print_error(null, "Failed to fetch product");
300 return -1;
301 }
302 if ($product->id <= 0) { // Can happen if database is corrupted (a product id exist in stock with product that has been removed)
303 return 0;
304 }
305
306 // Define if we must make the stock change (If product type is a service or if stock is used also for services)
307 // Only record into stock tables will be disabled by this (the rest like writing into lot table or movement of subproucts are done)
308 $movestock = 0;
309 if ($product->type != Product::TYPE_SERVICE || getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
310 $movestock = 1;
311 }
312
313 $this->db->begin();
314
315 // Set value $product->stock_reel and detail per warehouse into $product->stock_warehouse array
316 if ($movestock) {
317 $product->load_stock('novirtual');
318 }
319
320 // Test if product require batch data. If yes, and there is not or values are not correct, we throw an error.
321 if (isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
322 if (empty($batch)) {
323 $langs->load("errors");
324 $this->errors[] = $langs->transnoentitiesnoconv("ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
325 dol_syslog("Try to make a movement of a product with status_batch on without any batch data", LOG_ERR);
326
327 $this->db->rollback();
328 return -2;
329 }
330
331 // Check table llx_product_lot from batchnumber for same product
332 // If found and eatby/sellby defined into table and provided and differs, return error
333 // If found and eatby/sellby defined into table and not provided, we take value from table
334 // If found and eatby/sellby not defined into table and provided, we update table
335 // If found and eatby/sellby not defined into table and not provided, we do nothing
336 // If not found, we add record
337 $sql = "SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix()."product_lot as pb";
338 $sql .= " WHERE pb.fk_product = ".((int) $fk_product)." AND pb.batch = '".$this->db->escape($batch)."'";
339
340 dol_syslog(get_class($this)."::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
341
342 $resql = $this->db->query($sql);
343 if ($resql) {
344 $num = $this->db->num_rows($resql);
345 $i = 0;
346 if ($num > 0) {
347 while ($i < $num) {
348 $obj = $this->db->fetch_object($resql);
349 if ($obj->eatby) {
350 if ($eatby) {
351 $tmparray = dol_getdate($eatby, true);
352 $eatbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
353 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) { // We test date without hours and with hours for backward compatibility
354 // If found and eatby/sellby defined into table and provided and differs, return error
355 $langs->load("stocks");
356 $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->eatby), 'dayhour'), dol_print_date($eatbywithouthour, 'dayhour'));
357 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);
358 $this->db->rollback();
359 return -3;
360 }
361 } else {
362 $eatby = $obj->eatby; // If found and eatby/sellby defined into table and not provided, we take value from table
363 }
364 } else {
365 if ($eatby) { // If found and eatby/sellby not defined into table and provided, we update table
366 $productlot = new Productlot($this->db);
367 $result = $productlot->fetch($obj->rowid);
368 $productlot->eatby = $eatby;
369 $result = $productlot->update($user);
370 if ($result <= 0) {
371 $this->error = $productlot->error;
372 $this->errors = $productlot->errors;
373 $this->db->rollback();
374 return -5;
375 }
376 }
377 }
378 if ($obj->sellby) {
379 if ($sellby) {
380 $tmparray = dol_getdate($sellby, true);
381 $sellbywithouthour = dol_mktime(0, 0, 0, $tmparray['mon'], $tmparray['mday'], $tmparray['year']);
382 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) { // We test date without hours and with hours for backward compatibility
383 // If found and eatby/sellby defined into table and provided and differs, return error
384 $this->errors[] = $langs->transnoentitiesnoconv("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby));
385 dol_syslog($langs->transnoentities("ThisSerialAlreadyExistWithDifferentDate", $batch, dol_print_date($this->db->jdate($obj->sellby)), dol_print_date($sellby)), LOG_ERR);
386 $this->db->rollback();
387 return -3;
388 }
389 } else {
390 $sellby = $obj->sellby; // If found and eatby/sellby defined into table and not provided, we take value from table
391 }
392 } else {
393 if ($sellby) { // If found and eatby/sellby not defined into table and provided, we update table
394 $productlot = new Productlot($this->db);
395 $result = $productlot->fetch($obj->rowid);
396 $productlot->sellby = $sellby;
397 $result = $productlot->update($user);
398 if ($result <= 0) {
399 $this->error = $productlot->error;
400 $this->errors = $productlot->errors;
401 $this->db->rollback();
402 return -5;
403 }
404 }
405 }
406
407 $i++;
408 }
409 } else { // If not found, we add record
410 $productlot = new Productlot($this->db);
411 $productlot->origin_type = !empty($this->origin_type) ? $this->origin_type : '';
412 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
413 $productlot->entity = $conf->entity;
414 $productlot->fk_product = $fk_product;
415 $productlot->batch = $batch;
416 // If we are here = first time we manage this batch, so we used dates provided by users to create lot
417 $productlot->eatby = $eatby;
418 $productlot->sellby = $sellby;
419 $result = $productlot->create($user);
420 if ($result <= 0) {
421 $this->error = $productlot->error;
422 $this->errors = $productlot->errors;
423 $this->db->rollback();
424 return -4;
425 }
426 }
427 } else {
428 dol_print_error($this->db);
429 $this->db->rollback();
430 return -1;
431 }
432 }
433
434 // Check if stock is enough when qty is < 0.
435 // THIS MUST BE DONE AT END OF MOVEMENTS
436 // Note that qty should be > 0 with type 0 or 3, < 0 with type 1 or 2.
437 if ($movestock && $qty < 0 && !getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER')) {
438 if (isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
439 $foundforbatch = 0;
440 $qtyisnotenough = 0;
441 if (isset($product->stock_warehouse[$entrepot_id])) {
442 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
443 if ((string) $batch != (string) $batchcursor) { // Lot '59' must be different than lot '59c'
444 continue;
445 }
446
447 $foundforbatch = 1;
448 if ($prodbatch->qty < abs($qty)) {
449 $qtyisnotenough = $prodbatch->qty;
450 }
451 break;
452 }
453 }
454 if (!$foundforbatch || $qtyisnotenough) {
455 $langs->load("stocks");
456 include_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
457 $tmpwarehouse = new Entrepot($this->db);
458 $tmpwarehouse->fetch($entrepot_id);
459
460 $this->error = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
461 $this->errors[] = $langs->trans('qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
462 $this->db->rollback();
463 return -8;
464 }
465 } else {
466 if (isset($product->stock_warehouse[$entrepot_id]) && (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty))) {
467 $langs->load("stocks");
468 $this->error = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
469 $this->errors[] = $langs->trans('qtyToTranferIsNotEnough').' : '.$product->ref;
470 $this->db->rollback();
471 return -8;
472 }
473 }
474 }
475
476 if ($movestock) { // Change stock for current product, change for subproduct is done after
477 // Set $origin_type, origin_id and fk_project
478 $fk_project = $this->fk_project;
479 if (!empty($this->origin_type)) { // This is set by caller for tracking reason
480 $origin_type = $this->origin_type;
481 $origin_id = $this->origin_id;
482 if (empty($fk_project) && $origin_type == 'project') {
483 $fk_project = $origin_id;
484 $origin_type = '';
485 $origin_id = 0;
486 }
487 } else {
488 $fk_project = 0;
489 $origin_type = '';
490 $origin_id = 0;
491 }
492
493 $sql = "INSERT INTO ".$this->db->prefix()."stock_mouvement(";
494 $sql .= " datem, fk_product, batch, eatby, sellby,";
495 $sql .= " fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
496 $sql .= ")";
497 $sql .= " VALUES ('".$this->db->idate($this->datem)."', ".((int) $this->product_id).", ";
498 $sql .= " ".($batch ? "'".$this->db->escape($batch)."'" : "null").", ";
499 $sql .= " ".($eatby ? "'".$this->db->idate($eatby)."'" : "null").", ";
500 $sql .= " ".($sellby ? "'".$this->db->idate($sellby)."'" : "null").", ";
501 $sql .= " ".((int) $this->entrepot_id).", ".((float) $this->qty).", ".((int) $this->type).",";
502 $sql .= " ".((int) $user->id).",";
503 $sql .= " '".$this->db->escape($label)."',";
504 $sql .= " ".($inventorycode ? "'".$this->db->escape($inventorycode)."'" : "null").",";
505 $sql .= " ".((float) price2num($price)).",";
506 $sql .= " ".((int) $origin_id).",";
507 $sql .= " '".$this->db->escape($origin_type)."',";
508 $sql .= " ".((int) $fk_project);
509 $sql .= ")";
510
511 dol_syslog(get_class($this)."::_create insert record into stock_mouvement", LOG_DEBUG);
512 $resql = $this->db->query($sql);
513
514 if ($resql) {
515 $mvid = $this->db->last_insert_id($this->db->prefix()."stock_mouvement");
516 $this->id = $mvid;
517 } else {
518 $this->error = $this->db->lasterror();
519 $this->errors[] = $this->error;
520 $error = -1;
521 }
522
523 // Define current values for qty and pmp
524 $oldqty = $product->stock_reel;
525 $oldpmp = $product->pmp;
526 $oldqtywarehouse = 0;
527
528 // Test if there is already a record for couple (warehouse / product), so later we will make an update or create.
529 $alreadyarecord = 0;
530 if (!$error) {
531 $sql = "SELECT rowid, reel FROM ".$this->db->prefix()."product_stock";
532 $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product); // This is a unique key
533
534 dol_syslog(get_class($this)."::_create check if a record already exists in product_stock", LOG_DEBUG);
535 $resql = $this->db->query($sql);
536 if ($resql) {
537 $obj = $this->db->fetch_object($resql);
538 if ($obj) {
539 $alreadyarecord = 1;
540 $oldqtywarehouse = $obj->reel;
541 $fk_product_stock = $obj->rowid;
542 }
543 $this->db->free($resql);
544 } else {
545 $this->errors[] = $this->db->lasterror();
546 $error = -2;
547 }
548 }
549
550 // Calculate new AWP (PMP)
551 $newpmp = 0;
552 if (!$error) {
553 if ($type == 0 || $type == 3) {
554 // After a stock increase
555 // Note: PMP is calculated on stock input only (type of movement = 0 or 3). If type == 0 or 3, qty should be > 0.
556 // Note: Price should always be >0 or 0. PMP should be always >0 (calculated on input)
557 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')))) {
558 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
559 // We make a test on oldpmp>0 to avoid to use normal rule on old data with no pmp field defined
560 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
561 $newpmp = price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty), 'MU');
562 } else {
563 $newpmp = $price; // For this product, PMP was not yet set. We set it to input price.
564 }
565 //print "oldqtytouse=".$oldqtytouse." oldpmp=".$oldpmp." oldqtywarehousetouse=".$oldqtywarehousetouse." ";
566 //print "qty=".$qty." newpmp=".$newpmp;
567 //exit;
568 } else {
569 $newpmp = $oldpmp;
570 }
571 } else {
572 // ($type == 1 || $type == 2)
573 // -> After a stock decrease, we don't change value of the AWP/PMP of a product.
574 // else
575 // Type of movement unknown
576 $newpmp = $oldpmp;
577 }
578 }
579 // Update stock quantity
580 if (!$error) {
581 if ($alreadyarecord > 0) {
582 $sql = "UPDATE ".$this->db->prefix()."product_stock SET reel = " . ((float) $oldqtywarehouse + (float) $qty);
583 $sql .= " WHERE fk_entrepot = ".((int) $entrepot_id)." AND fk_product = ".((int) $fk_product);
584 } else {
585 $sql = "INSERT INTO ".$this->db->prefix()."product_stock";
586 $sql .= " (reel, fk_entrepot, fk_product) VALUES ";
587 $sql .= " (".((float) $qty).", ".((int) $entrepot_id).", ".((int) $fk_product).")";
588 }
589
590 dol_syslog(get_class($this)."::_create update stock value", LOG_DEBUG);
591 $resql = $this->db->query($sql);
592 if (!$resql) {
593 $this->errors[] = $this->db->lasterror();
594 $error = -3;
595 } elseif (empty($fk_product_stock)) {
596 $fk_product_stock = $this->db->last_insert_id($this->db->prefix()."product_stock");
597 }
598 }
599
600 // Update detail of stock for the lot.
601 if (!$error && isModEnabled('productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
602 if ($id_product_batch > 0) {
603 $result = $this->createBatch($id_product_batch, $qty);
604 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
605 $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch);
606 $result = $this->createBatch($param_batch, $qty);
607 }
608 } else {
609 $param_batch = array('fk_product_stock' => $fk_product_stock, 'batchnumber' => $batch);
610 $result = $this->createBatch($param_batch, $qty);
611 }
612 if ($result < 0) {
613 $error++;
614 }
615 }
616
617 // Update PMP and denormalized value of stock qty at product level
618 if (!$error) {
619 $newpmp = price2num($newpmp, 'MU');
620
621 // $sql = "UPDATE ".$this->db->prefix()."product SET pmp = ".$newpmp.", stock = ".$this->db->ifsql("stock IS NULL", 0, "stock") . " + ".$qty;
622 // $sql.= " WHERE rowid = ".((int) $fk_product);
623 // Update pmp + denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
624 $sql = "UPDATE ".$this->db->prefix()."product as p SET pmp = ".((float) $newpmp).",";
625 $sql .= " stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix()."product_stock as ps WHERE ps.fk_product = p.rowid)";
626 $sql .= " WHERE rowid = ".((int) $fk_product);
627
628 dol_syslog(get_class($this)."::_create update AWP", LOG_DEBUG);
629 $resql = $this->db->query($sql);
630 if (!$resql) {
631 $this->errors[] = $this->db->lasterror();
632 $error = -4;
633 }
634 }
635
636 if (empty($donotcleanemptylines)) {
637 // 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
638 // having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
639 $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)";
640 $resql = $this->db->query($sql);
641 // We do not test error, it can fails if there is child in batch details
642 }
643 }
644
645 // Add movement for sub products (recursive call)
646 if (!$error && getDolGlobalString('PRODUIT_SOUSPRODUITS') && !getDolGlobalString('INDEPENDANT_SUBPRODUCT_STOCK') && empty($disablestockchangeforsubproduct)) {
647 $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
648 }
649
650 if ($movestock && !$error) {
651 // Call trigger
652 $result = $this->call_trigger('STOCK_MOVEMENT', $user);
653 if ($result < 0) {
654 $error++;
655 }
656 // End call triggers
657 // Check unicity for serial numbered equipment once all movement were done.
658 if (!$error && isModEnabled('productbatch') && $product->hasbatch() && !$skip_batch) {
659 if ($product->status_batch == 2 && $qty > 0) { // We check only if we increased qty
660 if ($this->getBatchCount($fk_product, $batch) > 1) {
661 $error++;
662 $this->errors[] = $langs->trans("TooManyQtyForSerialNumber", $product->ref, $batch);
663 }
664 }
665 }
666 }
667
668 if (!$error) {
669 $this->db->commit();
670 return $mvid;
671 } else {
672 $this->db->rollback();
673 dol_syslog(get_class($this)."::_create error code=".$error, LOG_ERR);
674 return -6;
675 }
676 }
677
678
679
687 public function fetch($id)
688 {
689 dol_syslog(__METHOD__, LOG_DEBUG);
690
691 $sql = "SELECT";
692 $sql .= " t.rowid,";
693 $sql .= " t.tms,";
694 $sql .= " t.datem,";
695 $sql .= " t.fk_product,";
696 $sql .= " t.fk_entrepot,";
697 $sql .= " t.value,";
698 $sql .= " t.price,";
699 $sql .= " t.type_mouvement,";
700 $sql .= " t.fk_user_author,";
701 $sql .= " t.label,";
702 $sql .= " t.fk_origin as origin_id,";
703 $sql .= " t.origintype as origin_type,";
704 $sql .= " t.inventorycode,";
705 $sql .= " t.batch,";
706 $sql .= " t.eatby,";
707 $sql .= " t.sellby,";
708 $sql .= " t.fk_projet as fk_project";
709 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
710 $sql .= " WHERE t.rowid = ".((int) $id);
711
712 $resql = $this->db->query($sql);
713 if ($resql) {
714 $numrows = $this->db->num_rows($resql);
715 if ($numrows) {
716 $obj = $this->db->fetch_object($resql);
717
718 $this->id = $obj->rowid;
719
720 $this->product_id = $obj->fk_product;
721 $this->warehouse_id = $obj->fk_entrepot;
722 $this->qty = $obj->value;
723 $this->type = $obj->type_mouvement;
724
725 $this->tms = $this->db->jdate($obj->tms);
726 $this->datem = $this->db->jdate($obj->datem);
727 $this->price = $obj->price;
728 $this->fk_user_author = $obj->fk_user_author;
729 $this->label = $obj->label;
730 $this->fk_origin = $obj->origin_id; // For backward compatibility
731 $this->origintype = $obj->origin_type; // For backward compatibility
732 $this->origin_id = $obj->origin_id;
733 $this->origin_type = $obj->origin_type;
734 $this->inventorycode = $obj->inventorycode;
735 $this->batch = $obj->batch;
736 $this->eatby = $this->db->jdate($obj->eatby);
737 $this->sellby = $this->db->jdate($obj->sellby);
738 $this->fk_project = $obj->fk_project;
739 }
740
741 // Retrieve all extrafield
742 $this->fetch_optionals();
743
744 // $this->fetch_lines();
745
746 $this->db->free($resql);
747
748 if ($numrows) {
749 return 1;
750 } else {
751 return 0;
752 }
753 } else {
754 $this->errors[] = 'Error '.$this->db->lasterror();
755 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
756
757 return -1;
758 }
759 }
760
761
762
763
778 private function _createSubProduct($user, $idProduct, $entrepot_id, $qty, $type, $price = 0, $label = '', $inventorycode = '', $datem = '')
779 {
780 global $langs;
781
782 $error = 0;
783 $pids = array();
784 $pqtys = array();
785
786 $sql = "SELECT fk_product_pere, fk_product_fils, qty";
787 $sql .= " FROM ".$this->db->prefix()."product_association";
788 $sql .= " WHERE fk_product_pere = ".((int) $idProduct);
789 $sql .= " AND incdec = 1";
790
791 dol_syslog(get_class($this)."::_createSubProduct for parent product ".$idProduct, LOG_DEBUG);
792 $resql = $this->db->query($sql);
793 if ($resql) {
794 $i = 0;
795 while ($obj = $this->db->fetch_object($resql)) {
796 $pids[$i] = $obj->fk_product_fils;
797 $pqtys[$i] = $obj->qty;
798 $i++;
799 }
800 $this->db->free($resql);
801 } else {
802 $error = -2;
803 }
804
805 // Create movement for each subproduct
806 foreach ($pids as $key => $value) {
807 if (!$error) {
808 $tmpmove = dol_clone($this, 1);
809
810 $result = $tmpmove->_create($user, $pids[$key], $entrepot_id, ($qty * $pqtys[$key]), $type, 0, $label, $inventorycode, $datem); // This will also call _createSubProduct making this recursive
811 if ($result < 0) {
812 $this->error = $tmpmove->error;
813 $this->errors = array_merge($this->errors, $tmpmove->errors);
814 if ($result == -2) {
815 $this->errors[] = $langs->trans("ErrorNoteAlsoThatSubProductCantBeFollowedByLot");
816 }
817 $error = $result;
818 }
819 unset($tmpmove);
820 }
821 }
822
823 return $error;
824 }
825
826
845 public function livraison($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $datem = '', $eatby = '', $sellby = '', $batch = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
846 {
847 global $conf;
848
849 $skip_batch = empty($conf->productbatch->enabled);
850
851 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);
852 }
853
872 public function reception($user, $fk_product, $entrepot_id, $qty, $price = 0, $label = '', $eatby = '', $sellby = '', $batch = '', $datem = '', $id_product_batch = 0, $inventorycode = '', $donotcleanemptylines = 0)
873 {
874 global $conf;
875
876 $skip_batch = empty($conf->productbatch->enabled);
877
878 return $this->_create($user, $fk_product, $entrepot_id, $qty, 3, $price, $label, $inventorycode, $datem, $eatby, $sellby, $batch, $skip_batch, $id_product_batch, 0, $donotcleanemptylines);
879 }
880
888 public function calculateBalanceForProductBefore($productidselected, $datebefore)
889 {
890 $nb = 0;
891
892 $sql = "SELECT SUM(value) as nb from ".$this->db->prefix()."stock_mouvement";
893 $sql .= " WHERE fk_product = ".((int) $productidselected);
894 $sql .= " AND datem < '".$this->db->idate($datebefore)."'";
895
896 dol_syslog(get_class($this).__METHOD__, LOG_DEBUG);
897 $resql = $this->db->query($sql);
898 if ($resql) {
899 $obj = $this->db->fetch_object($resql);
900 if ($obj) {
901 $nb = $obj->nb;
902 }
903 return (empty($nb) ? 0 : $nb);
904 } else {
905 dol_print_error($this->db);
906 return -1;
907 }
908 }
909
919 private function createBatch($dluo, $qty)
920 {
921 global $user, $langs;
922
923 $langs->load('productbatch');
924
925 $pdluo = new Productbatch($this->db);
926
927 $result = 0;
928
929 // Try to find an existing record with same batch number or id
930 if (is_numeric($dluo)) {
931 $result = $pdluo->fetch($dluo);
932 if (empty($pdluo->id)) {
933 // We didn't find the line. May be it was deleted before by a previous move in same transaction.
934 $this->error = $langs->trans('CantMoveNonExistantSerial');
935 $this->errors[] = $this->error;
936 $result = -2;
937 }
938 } elseif (is_array($dluo)) {
939 if (isset($dluo['fk_product_stock'])) {
940 $vfk_product_stock = $dluo['fk_product_stock'];
941 $vbatchnumber = $dluo['batchnumber'];
942
943 $result = $pdluo->find($vfk_product_stock, '', '', $vbatchnumber); // Search on batch number only (eatby and sellby are deprecated here)
944 } else {
945 dol_syslog(get_class($this)."::createBatch array param dluo must contain at least key fk_product_stock", LOG_ERR);
946 $result = -1;
947 }
948 } else {
949 dol_syslog(get_class($this)."::createBatch error invalid param dluo", LOG_ERR);
950 $result = -1;
951 }
952
953 if ($result >= 0) {
954 // No error
955 if ($pdluo->id > 0) { // product_batch record found
956 //print "Avant ".$pdluo->qty." Apres ".($pdluo->qty + $qty)."<br>";
957 $pdluo->qty += $qty;
958 if ($pdluo->qty == 0) {
959 $result = $pdluo->delete($user, 1);
960 } else {
961 $result = $pdluo->update($user, 1);
962 }
963 } else { // product_batch record not found
964 $pdluo->fk_product_stock = $vfk_product_stock;
965 $pdluo->qty = $qty;
966 $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.
967 $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.
968 $pdluo->batch = $vbatchnumber;
969
970 $result = $pdluo->create($user, 1);
971 if ($result < 0) {
972 $this->error = $pdluo->error;
973 $this->errors = $pdluo->errors;
974 }
975 }
976 }
977
978 return $result;
979 }
980
981 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
989 public function get_origin($origin_id, $origin_type)
990 {
991 // phpcs:enable
992 $origin = '';
993
994 switch ($origin_type) {
995 case 'commande':
996 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
997 $origin = new Commande($this->db);
998 break;
999 case 'shipping':
1000 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1001 $origin = new Expedition($this->db);
1002 break;
1003 case 'facture':
1004 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1005 $origin = new Facture($this->db);
1006 break;
1007 case 'order_supplier':
1008 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
1009 $origin = new CommandeFournisseur($this->db);
1010 break;
1011 case 'invoice_supplier':
1012 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1013 $origin = new FactureFournisseur($this->db);
1014 break;
1015 case 'project':
1016 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
1017 $origin = new Project($this->db);
1018 break;
1019 case 'mo':
1020 require_once DOL_DOCUMENT_ROOT.'/mrp/class/mo.class.php';
1021 $origin = new Mo($this->db);
1022 break;
1023 case 'user':
1024 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1025 $origin = new User($this->db);
1026 break;
1027 case 'reception':
1028 require_once DOL_DOCUMENT_ROOT.'/reception/class/reception.class.php';
1029 $origin = new Reception($this->db);
1030 break;
1031 case 'inventory':
1032 require_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
1033 $origin = new Inventory($this->db);
1034 break;
1035 default:
1036 if ($origin_type) {
1037 // Separate origin_type with "@" : left part is class name, right part is module name
1038 $origin_type_array = explode('@', $origin_type);
1039 $classname = $origin_type_array[0];
1040 $modulename = empty($origin_type_array[1]) ? strtolower($classname) : $origin_type_array[1];
1041
1042 $result = dol_include_once('/'.$modulename.'/class/'.$classname.'.class.php');
1043
1044 if ($result) {
1045 $classname = ucfirst($classname);
1046 $origin = new $classname($this->db);
1047 }
1048 }
1049 break;
1050 }
1051
1052 if (empty($origin) || !is_object($origin)) {
1053 return '';
1054 }
1055
1056 if ($origin->fetch($origin_id) > 0) {
1057 return $origin->getNomUrl(1);
1058 }
1059
1060 return '';
1061 }
1062
1073 public function setOrigin($origin_element, $origin_id, $line_id_object_src = 0, $line_id_object_origin = 0)
1074 {
1075 $this->origin_type = $origin_element;
1076 $this->origin_id = $origin_id;
1077 $this->line_id_object_src = $line_id_object_src;
1078 $this->line_id_object_origin = $line_id_object_origin;
1079 // For backward compatibility
1080 $this->origintype = $origin_element;
1081 $this->fk_origin = $origin_id;
1082 }
1083
1084
1092 public function initAsSpecimen()
1093 {
1094 // Initialize parameters
1095 $this->id = 0;
1096
1097 // There is no specific properties. All data into insert are provided as method parameter.
1098
1099 return 1;
1100 }
1101
1108 public function getTypeMovement($withlabel = 0)
1109 {
1110 global $langs;
1111
1112 $s = '';
1113 switch ($this->type) {
1114 case "0":
1115 $s = '<span class="fa fa-level-down-alt stockmovemententry stockmovementtransfer" title="'.$langs->trans('StockIncreaseAfterCorrectTransfer').'"></span>';
1116 if ($withlabel) {
1117 $s .= $langs->trans('StockIncreaseAfterCorrectTransfer');
1118 }
1119 break;
1120 case "1":
1121 $s = '<span class="fa fa-level-up-alt stockmovementexit stockmovementtransfer" title="'.$langs->trans('StockDecreaseAfterCorrectTransfer').'"></span>';
1122 if ($withlabel) {
1123 $s .= $langs->trans('StockDecreaseAfterCorrectTransfer');
1124 }
1125 break;
1126 case "2":
1127 $s = '<span class="fa fa-long-arrow-alt-up stockmovementexit stockmovement" title="'.$langs->trans('StockDecrease').'"></span>';
1128 if ($withlabel) {
1129 $s .= $langs->trans('StockDecrease');
1130 }
1131 break;
1132 case "3":
1133 $s = '<span class="fa fa-long-arrow-alt-down stockmovemententry stockmovement" title="'.$langs->trans('StockIncrease').'"></span>';
1134 if ($withlabel) {
1135 $s .= $langs->trans('StockIncrease');
1136 }
1137 break;
1138 }
1139
1140 return $s;
1141 }
1142
1154 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
1155 {
1156 global $langs, $conf, $db;
1157
1158 $result = '';
1159
1160 $label = img_picto('', 'stock', 'class="pictofixedwidth"').'<u>'.$langs->trans("StockMovement").'</u>';
1161 $label .= '<div width="100%">';
1162 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->id;
1163 $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
1164 $qtylabel = (($this->qty > 0) ? '<span class="stockmovemententry">+' : '<span class="stockmovementexit">') . $this->qty . '</span>';
1165 if ($this->inventorycode) {
1166 $label .= '<br><b>'.$langs->trans('InventoryCode').':</b> '.$this->inventorycode;
1167 }
1168 $label .= '<br><b>'.$langs->trans('Qty').':</b> ' . $qtylabel;
1169 if ($this->batch) {
1170 $label .= '<br><b>'.$langs->trans('Batch').':</b> '.$this->batch;
1171 }
1172 /* TODO Get also warehouse label in a property instead of id
1173 if ($this->warehouse_id > 0) {
1174 $label .= '<br><b>'.$langs->trans('Warehouse').':</b> '.$this->warehouse_id;
1175 }*/
1176 $label .= '</div>';
1177
1178 // Link to page of warehouse tab
1179 if ($option == 'movements') {
1180 $url = DOL_URL_ROOT.'/product/stock/movement_list.php?search_ref='.$this->id;
1181 } else {
1182 $url = DOL_URL_ROOT.'/product/stock/movement_list.php?id='.$this->warehouse_id.'&msid='.$this->id;
1183 }
1184
1185 $link = '<a href="'.$url.'"'.($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
1186 $link .= '>';
1187 $linkend = '</a>';
1188
1189 if ($withpicto) {
1190 $result .= ($link.img_object(($notooltip ? '' : $label), 'stock', ($notooltip ? '' : 'class="classfortooltip"')).$linkend);
1191 if ($withpicto != 2) {
1192 $result .= ' ';
1193 }
1194 }
1195 $result .= $link.$this->id.$linkend;
1196 return $result;
1197 }
1198
1205 public function getLibStatut($mode = 0)
1206 {
1207 return $this->LibStatut($mode);
1208 }
1209
1210 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1217 public function LibStatut($mode = 0)
1218 {
1219 // phpcs:enable
1220 global $langs;
1221
1222 if ($mode == 0 || $mode == 1) {
1223 return $langs->trans('StatusNotApplicable');
1224 } elseif ($mode == 2) {
1225 return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1226 } elseif ($mode == 3) {
1227 return img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1228 } elseif ($mode == 4) {
1229 return img_picto($langs->trans('StatusNotApplicable'), 'statut9').' '.$langs->trans('StatusNotApplicable');
1230 } elseif ($mode == 5) {
1231 return $langs->trans('StatusNotApplicable').' '.img_picto($langs->trans('StatusNotApplicable'), 'statut9');
1232 }
1233
1234 return 'Bad value for mode';
1235 }
1236
1247 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1248 {
1249 global $conf, $user, $langs;
1250
1251 $langs->load("stocks");
1252 $outputlangs->load("products");
1253
1254 if (!dol_strlen($modele)) {
1255 $modele = 'stdmovement';
1256
1257 if ($this->model_pdf) {
1258 $modele = $this->model_pdf;
1259 } elseif (getDolGlobalString('MOUVEMENT_ADDON_PDF')) {
1260 $modele = getDolGlobalString('MOUVEMENT_ADDON_PDF');
1261 }
1262 }
1263
1264 $modelpath = "core/modules/stock/doc/";
1265
1266 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1267 }
1268
1276 public function delete(User $user, $notrigger = 0)
1277 {
1278 return $this->deleteCommon($user, $notrigger);
1279 //return $this->deleteCommon($user, $notrigger, 1);
1280 }
1281
1289 private function getBatchCount($fk_product, $batch)
1290 {
1291 $cpt = 0;
1292
1293 $sql = "SELECT sum(pb.qty) as cpt";
1294 $sql .= " FROM ".$this->db->prefix()."product_batch as pb";
1295 $sql .= " INNER JOIN ".$this->db->prefix()."product_stock as ps ON ps.rowid = pb.fk_product_stock";
1296 $sql .= " WHERE ps.fk_product = " . ((int) $fk_product);
1297 $sql .= " AND pb.batch = '" . $this->db->escape($batch) . "'";
1298
1299 $result = $this->db->query($sql);
1300 if ($result) {
1301 if ($this->db->num_rows($result)) {
1302 $obj = $this->db->fetch_object($result);
1303 $cpt = $obj->cpt;
1304 }
1305
1306 $this->db->free($result);
1307 } else {
1308 dol_print_error($this->db);
1309 return -1;
1310 }
1311
1312 return $cpt;
1313 }
1314
1319 public function reverseMouvement()
1320 {
1321 $formattedDate = "REVERTMV" .dol_print_date($this->datem, '%Y%m%d%His');
1322 if ($this->label == 'Annulation movement ID'.$this->id) {
1323 return -1;
1324 }
1325 if ($this->inventorycode == $formattedDate) {
1326 return -1;
1327 }
1328
1329 $sql = "UPDATE ".$this->db->prefix()."stock_mouvement SET";
1330 $sql .= " label = 'Annulation movement ID ".((int) $this->id)."',";
1331 $sql .= "inventorycode = '".($formattedDate)."'";
1332 $sql .= " WHERE rowid = ".((int) $this->id);
1333
1334 $resql = $this->db->query($sql);
1335
1336 if ($resql) {
1337 $this->db->commit();
1338 return 1;
1339 } else {
1340 $this->db->rollback();
1341 return -1;
1342 }
1343 }
1344
1353 public function getDateLastMovementProductBatch($fk_entrepot, $fk_product, $batch)
1354 {
1355 $date = '';
1356
1357 $sql = "SELECT MAX(datem) as datem";
1358 $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement";
1359 $sql .= " WHERE fk_product = " . ((int) $fk_product);
1360 $sql .= " AND fk_entrepot = " .((int) $fk_entrepot);
1361 $sql .= " AND batch = '" . $this->db->escape($batch) . "'";
1362
1363 $result = $this->db->query($sql);
1364 if ($result) {
1365 if ($this->db->num_rows($result)) {
1366 $dateObj = $this->db->fetch_object($result);
1367 $date = $dateObj->datem;
1368 }
1369 $this->db->free($result);
1370 } else {
1371 dol_print_error($this->db);
1372 return $date;
1373 }
1374
1375 return $date;
1376 }
1377}
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 suppliers invoices.
Class to manage invoices.
Class for Inventory.
Class for Mo.
Definition mo.class.php:34
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.
getDateLastMovementProductBatch($fk_entrepot, $fk_product, $batch)
Retrieve date of last stock movement for.
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.
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.
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).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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 a Dolibarr global constant string value.
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.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137