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