230 public function _create($user, $fk_product, $entrepot_id, $qty, $type, $price = 0, $label =
'', $inventorycode =
'', $datem =
'', $eatby = 0, $sellby = 0, $batch =
'', $skip_batch =
false, $id_product_batch = 0, $disablestockchangeforsubproduct = 0, $donotcleanemptylines = 0, $force_update_batch =
false)
233 global
$conf, $langs;
235 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
236 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
239 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));
242 global $action, $hookmanager;
243 $hookmanager->initHooks(array(
'mouvementstock'));
245 if (is_object($hookmanager)) {
247 'currentcontext' =>
'mouvementstock',
249 'fk_product' => &$fk_product,
250 'entrepot_id' => &$entrepot_id,
255 'inventorycode' => &$inventorycode,
258 'sellby' => &$sellby,
260 'skip_batch' => &$skip_batch,
261 'id_product_batch' => &$id_product_batch
263 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
266 if (!empty($hookmanager->resPrint)) {
267 dol_print_error(
null, $hookmanager->resPrint);
270 } elseif ($reshook > 0) {
281 $now = (!empty($datem) ? $datem :
dol_now());
286 if (!($fk_product > 0)) {
289 if (!($entrepot_id > 0)) {
293 if (is_numeric($eatby) && $eatby < 0) {
294 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
295 $this->errors[] =
'ErrorBadValueForParameterEatBy';
298 if (is_numeric($sellby) && $sellby < 0) {
299 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
300 $this->errors[] =
'ErrorBadValueForParameterSellBy';
305 $this->product_id = $fk_product;
306 $this->entrepot_id = $entrepot_id;
307 $this->warehouse_id = $entrepot_id;
311 $this->label = $label;
312 $this->inventorycode = $inventorycode;
314 $this->batch = $batch;
318 $product =
new Product($this->db);
320 $result = $product->fetch($fk_product);
323 dol_print_error(
null,
"Failed to fetch product");
326 if ($product->id <= 0) {
333 $productChildrenNb = 0;
335 $productChildrenNb = $product->hasFatherOrChild(1);
338 if ($product->isStockManaged() && ($productChildrenNb == 0 ||
getDolGlobalInt(
'PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE'))) {
346 $product->load_stock(
'novirtual');
350 if (
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
352 $langs->load(
"errors");
353 $this->errors[] = $langs->transnoentitiesnoconv(
"ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
354 dol_syslog(
"Try to make a movement of a product with status_batch on without any batch data", LOG_ERR);
356 $this->db->rollback();
374 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
375 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
377 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
379 $resql = $this->db->query($sql);
381 $num = $this->db->num_rows($resql);
384 while ($i < $num && $obj = $this->db->fetch_object($resql)) {
388 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
389 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
391 $langs->load(
"stocks");
392 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
393 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);
394 $this->db->rollback();
398 $eatby = $obj->eatby;
403 $result = $productlot->fetch($obj->rowid);
404 $productlot->eatby = $eatby;
405 $result = $productlot->update($user);
408 $this->db->rollback();
416 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
417 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
419 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
421 $this->db->rollback();
425 $sellby = $obj->sellby;
430 $result = $productlot->fetch($obj->rowid);
431 $productlot->sellby = $sellby;
432 $result = $productlot->update($user);
435 $this->db->rollback();
445 $productlot->origin_type = !empty($this->origin_type) ? $this->origin_type :
'';
446 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
447 $productlot->entity =
$conf->entity;
448 $productlot->fk_product = $fk_product;
449 $productlot->batch = $batch;
451 $productlot->eatby = $eatby;
452 $productlot->sellby = $sellby;
453 $result = $productlot->create($user);
456 $this->db->rollback();
461 dol_print_error($this->db);
462 $this->db->rollback();
470 if ($movestock && $qty < 0 &&
getDolGlobalInt(
'STOCK_DISALLOW_NEGATIVE_TRANSFER')) {
471 if (
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
474 if (isset($product->stock_warehouse[$entrepot_id])) {
475 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
477 if ((
string) $batch !== (
string) $batchcursor) {
482 if ($prodbatch->qty < abs($qty)) {
483 $qtyisnotenough = $prodbatch->qty;
488 if (!$foundforbatch || $qtyisnotenough) {
489 $langs->load(
"stocks");
490 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
491 $tmpwarehouse =
new Entrepot($this->db);
492 $tmpwarehouse->fetch($entrepot_id);
494 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
495 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
496 $this->db->rollback();
500 if ($product->stockable_product == Product::ENABLED_STOCK && (empty($product->stock_warehouse[$entrepot_id]) || empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty))) {
501 $langs->load(
"stocks");
502 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
503 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
504 $this->db->rollback();
510 if ($movestock && $product->stockable_product == Product::ENABLED_STOCK) {
512 $fk_project = $this->fk_project;
513 if (!empty($this->origin_type)) {
514 $origin_type = $this->origin_type;
515 $origin_id = $this->origin_id;
516 if (empty($fk_project) && $origin_type ==
'project') {
517 $fk_project = $origin_id;
528 $label =
dol_trunc((
string) $label, 255,
'right',
'UTF-8', 1);
530 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
531 $sql .=
" datem, fk_product, batch, eatby, sellby,";
532 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
534 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
535 $sql .=
" ".(isset($batch) ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
536 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
537 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
538 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
539 $sql .=
" ".((int) $user->id).
",";
540 $sql .=
" '".$this->db->escape($label).
"',";
541 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
542 $sql .=
" ".((float)
price2num($price)).
",";
543 $sql .=
" ".((int) $origin_id).
",";
544 $sql .=
" '".$this->db->escape($origin_type).
"',";
545 $sql .=
" ".((int) $fk_project);
548 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
549 $resql = $this->db->query($sql);
552 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
555 $this->error = $this->db->lasterror();
556 $this->errors[] = $this->error;
561 $oldqty = $product->stock_reel;
562 $oldpmp = $product->pmp;
563 $oldqtywarehouse = 0;
567 $fk_product_stock = 0;
569 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
570 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
572 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
573 $resql = $this->db->query($sql);
575 $obj = $this->db->fetch_object($resql);
578 $oldqtywarehouse = $obj->reel;
579 $fk_product_stock = $obj->rowid;
581 $this->db->free($resql);
583 $this->errors[] = $this->db->lasterror();
591 if ($type == 0 || $type == 3) {
595 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')))) {
596 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
598 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
599 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
619 if ($alreadyarecord > 0) {
620 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = " . ((float) $oldqtywarehouse + (
float) $qty);
621 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
623 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
624 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
625 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
628 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
629 $resql = $this->db->query($sql);
631 $this->errors[] = $this->db->lasterror();
633 } elseif (empty($fk_product_stock)) {
634 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
639 if (!$error &&
isModEnabled(
'productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
640 if ($id_product_batch > 0) {
641 $result = $this->
createBatch($id_product_batch, $qty);
642 if ($result == -2 && $fk_product_stock > 0) {
643 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
647 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
662 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
663 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
664 $sql .=
" WHERE rowid = ".((int) $fk_product);
666 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
667 $resql = $this->db->query($sql);
669 $this->errors[] = $this->db->lasterror();
674 if (empty($donotcleanemptylines)) {
677 $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)";
678 $resql = $this->db->query($sql);
686 $this->errors[] = $this->db->lasterror();
693 if (!$error &&
getDolGlobalString(
'PRODUIT_SOUSPRODUITS') && !
getDolGlobalString(
'PRODUIT_SOUSPRODUITS_ALWAYS_DISABLE_CHILDREN_STOCK_MOVE') && empty($disablestockchangeforsubproduct)) {
694 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
697 if ($movestock && !$error) {
700 $result = $this->call_trigger(
'STOCK_MOVEMENT', $user);
707 if (!$error &&
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
708 if ($product->status_batch == 2 && $qty > 0) {
709 if ($this->
getBatchCount($fk_product, (
string) $batch) > 1) {
711 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, (
string) $batch);
721 $this->db->rollback();
722 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);