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)) {
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);
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();
366 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
367 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
369 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
371 $resql = $this->db->query($sql);
373 $num = $this->db->num_rows($resql);
376 while ($i < $num && $obj = $this->db->fetch_object($resql)) {
380 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
381 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
383 $langs->load(
"stocks");
384 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
385 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);
386 $this->db->rollback();
390 $eatby = $obj->eatby;
395 $result = $productlot->fetch($obj->rowid);
396 $productlot->eatby = $eatby;
397 $result = $productlot->update($user);
400 $this->db->rollback();
408 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
409 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
411 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
413 $this->db->rollback();
417 $sellby = $obj->sellby;
422 $result = $productlot->fetch($obj->rowid);
423 $productlot->sellby = $sellby;
424 $result = $productlot->update($user);
427 $this->db->rollback();
437 $productlot->origin_type = !empty($this->origin_type) ? $this->origin_type :
'';
438 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
439 $productlot->entity = $conf->entity;
440 $productlot->fk_product = $fk_product;
441 $productlot->batch = $batch;
443 $productlot->eatby = $eatby;
444 $productlot->sellby = $sellby;
445 $result = $productlot->create($user);
448 $this->db->rollback();
454 $this->db->rollback();
462 if ($movestock && $qty < 0 &&
getDolGlobalInt(
'STOCK_DISALLOW_NEGATIVE_TRANSFER')) {
463 if (
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
466 if (isset($product->stock_warehouse[$entrepot_id])) {
467 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
469 if ((
string) $batch !== (
string) $batchcursor) {
474 if ($prodbatch->qty < abs($qty)) {
475 $qtyisnotenough = $prodbatch->qty;
480 if (!$foundforbatch || $qtyisnotenough) {
481 $langs->load(
"stocks");
482 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
483 $tmpwarehouse =
new Entrepot($this->db);
484 $tmpwarehouse->fetch($entrepot_id);
486 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
487 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
488 $this->db->rollback();
492 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))) {
493 $langs->load(
"stocks");
494 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
495 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
496 $this->db->rollback();
502 if ($movestock && $product->stockable_product == Product::ENABLED_STOCK) {
504 $fk_project = $this->fk_project;
505 if (!empty($this->origin_type)) {
506 $origin_type = $this->origin_type;
507 $origin_id = $this->origin_id;
508 if (empty($fk_project) && $origin_type ==
'project') {
509 $fk_project = $origin_id;
519 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
520 $sql .=
" datem, fk_product, batch, eatby, sellby,";
521 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
523 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
524 $sql .=
" ".(isset($batch) ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
525 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
526 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
527 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
528 $sql .=
" ".((int) $user->id).
",";
529 $sql .=
" '".$this->db->escape($label).
"',";
530 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
531 $sql .=
" ".((float)
price2num($price)).
",";
532 $sql .=
" ".((int) $origin_id).
",";
533 $sql .=
" '".$this->db->escape($origin_type).
"',";
534 $sql .=
" ".((int) $fk_project);
537 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
538 $resql = $this->db->query($sql);
541 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
544 $this->error = $this->db->lasterror();
545 $this->errors[] = $this->error;
550 $oldqty = $product->stock_reel;
551 $oldpmp = $product->pmp;
552 $oldqtywarehouse = 0;
556 $fk_product_stock = 0;
558 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
559 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
561 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
562 $resql = $this->db->query($sql);
564 $obj = $this->db->fetch_object($resql);
567 $oldqtywarehouse = $obj->reel;
568 $fk_product_stock = $obj->rowid;
570 $this->db->free($resql);
572 $this->errors[] = $this->db->lasterror();
580 if ($type == 0 || $type == 3) {
584 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')))) {
585 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
587 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
588 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
608 if ($alreadyarecord > 0) {
609 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = " . ((float) $oldqtywarehouse + (
float) $qty);
610 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
612 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
613 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
614 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
617 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
618 $resql = $this->db->query($sql);
620 $this->errors[] = $this->db->lasterror();
622 } elseif (empty($fk_product_stock)) {
623 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
628 if (!$error &&
isModEnabled(
'productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
629 if ($id_product_batch > 0) {
630 $result = $this->
createBatch($id_product_batch, $qty);
631 if ($result == -2 && $fk_product_stock > 0) {
632 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
636 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
651 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
652 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
653 $sql .=
" WHERE rowid = ".((int) $fk_product);
655 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
656 $resql = $this->db->query($sql);
658 $this->errors[] = $this->db->lasterror();
663 if (empty($donotcleanemptylines)) {
666 $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)";
667 $resql = $this->db->query($sql);
673 if (!$error &&
getDolGlobalString(
'PRODUIT_SOUSPRODUITS') && !
getDolGlobalString(
'PRODUIT_SOUSPRODUITS_ALWAYS_DISABLE_CHILDREN_STOCK_MOVE') && empty($disablestockchangeforsubproduct)) {
674 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
677 if ($movestock && !$error) {
680 $result = $this->call_trigger(
'STOCK_MOVEMENT', $user);
687 if (!$error &&
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
688 if ($product->status_batch == 2 && $qty > 0) {
689 if ($this->
getBatchCount($fk_product, (
string) $batch) > 1) {
691 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, (
string) $batch);
701 $this->db->rollback();
702 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);