229 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)
232 global
$conf, $langs;
234 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
235 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
238 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));
241 global $action, $hookmanager;
242 $hookmanager->initHooks(array(
'mouvementstock'));
244 if (is_object($hookmanager)) {
246 'currentcontext' =>
'mouvementstock',
248 'fk_product' => &$fk_product,
249 'entrepot_id' => &$entrepot_id,
254 'inventorycode' => &$inventorycode,
257 'sellby' => &$sellby,
259 'skip_batch' => &$skip_batch,
260 'id_product_batch' => &$id_product_batch
262 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
265 if (!empty($hookmanager->resPrint)) {
269 } elseif ($reshook > 0) {
280 $now = (!empty($datem) ? $datem :
dol_now());
285 if (!($fk_product > 0)) {
288 if (!($entrepot_id > 0)) {
292 if (is_numeric($eatby) && $eatby < 0) {
293 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
294 $this->errors[] =
'ErrorBadValueForParameterEatBy';
297 if (is_numeric($sellby) && $sellby < 0) {
298 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
299 $this->errors[] =
'ErrorBadValueForParameterSellBy';
304 $this->product_id = $fk_product;
305 $this->entrepot_id = $entrepot_id;
306 $this->warehouse_id = $entrepot_id;
310 $this->label = $label;
311 $this->inventorycode = $inventorycode;
313 $this->batch = $batch;
317 $product =
new Product($this->db);
319 $result = $product->fetch($fk_product);
325 if ($product->id <= 0) {
332 $productChildrenNb = 0;
334 $productChildrenNb = $product->hasFatherOrChild(1);
337 if ($product->isStockManaged() && ($productChildrenNb == 0 ||
getDolGlobalInt(
'PRODUIT_SOUSPRODUITS_ALSO_ENABLE_PARENT_STOCK_MOVE'))) {
345 $product->load_stock(
'novirtual');
349 if (
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
351 $langs->load(
"errors");
352 $this->errors[] = $langs->transnoentitiesnoconv(
"ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
353 dol_syslog(
"Try to make a movement of a product with status_batch on without any batch data", LOG_ERR);
355 $this->db->rollback();
365 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
366 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
368 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
370 $resql = $this->db->query($sql);
372 $num = $this->db->num_rows($resql);
375 while ($i < $num && $obj = $this->db->fetch_object($resql)) {
379 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
380 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
382 $langs->load(
"stocks");
383 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
384 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);
385 $this->db->rollback();
389 $eatby = $obj->eatby;
394 $result = $productlot->fetch($obj->rowid);
395 $productlot->eatby = $eatby;
396 $result = $productlot->update($user);
399 $this->db->rollback();
407 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
408 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
410 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
412 $this->db->rollback();
416 $sellby = $obj->sellby;
421 $result = $productlot->fetch($obj->rowid);
422 $productlot->sellby = $sellby;
423 $result = $productlot->update($user);
426 $this->db->rollback();
436 $productlot->origin_type = !empty($this->origin_type) ? $this->origin_type :
'';
437 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
438 $productlot->entity =
$conf->entity;
439 $productlot->fk_product = $fk_product;
440 $productlot->batch = $batch;
442 $productlot->eatby = $eatby;
443 $productlot->sellby = $sellby;
444 $result = $productlot->create($user);
447 $this->db->rollback();
453 $this->db->rollback();
461 if ($movestock && $qty < 0 &&
getDolGlobalInt(
'STOCK_DISALLOW_NEGATIVE_TRANSFER')) {
462 if (
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
465 if (isset($product->stock_warehouse[$entrepot_id])) {
466 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
468 if ((
string) $batch !== (
string) $batchcursor) {
473 if ($prodbatch->qty < abs($qty)) {
474 $qtyisnotenough = $prodbatch->qty;
479 if (!$foundforbatch || $qtyisnotenough) {
480 $langs->load(
"stocks");
481 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
482 $tmpwarehouse =
new Entrepot($this->db);
483 $tmpwarehouse->fetch($entrepot_id);
485 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
486 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, (
string) $batch, $qtyisnotenough, $tmpwarehouse->ref);
487 $this->db->rollback();
491 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))) {
492 $langs->load(
"stocks");
493 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
494 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
495 $this->db->rollback();
501 if ($movestock && $product->stockable_product == Product::ENABLED_STOCK) {
503 $fk_project = $this->fk_project;
504 if (!empty($this->origin_type)) {
505 $origin_type = $this->origin_type;
506 $origin_id = $this->origin_id;
507 if (empty($fk_project) && $origin_type ==
'project') {
508 $fk_project = $origin_id;
518 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
519 $sql .=
" datem, fk_product, batch, eatby, sellby,";
520 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
522 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
523 $sql .=
" ".(isset($batch) ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
524 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
525 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
526 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
527 $sql .=
" ".((int) $user->id).
",";
528 $sql .=
" '".$this->db->escape($label).
"',";
529 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
530 $sql .=
" ".((float)
price2num($price)).
",";
531 $sql .=
" ".((int) $origin_id).
",";
532 $sql .=
" '".$this->db->escape($origin_type).
"',";
533 $sql .=
" ".((int) $fk_project);
536 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
537 $resql = $this->db->query($sql);
540 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
543 $this->error = $this->db->lasterror();
544 $this->errors[] = $this->error;
549 $oldqty = $product->stock_reel;
550 $oldpmp = $product->pmp;
551 $oldqtywarehouse = 0;
555 $fk_product_stock = 0;
557 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
558 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
560 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
561 $resql = $this->db->query($sql);
563 $obj = $this->db->fetch_object($resql);
566 $oldqtywarehouse = $obj->reel;
567 $fk_product_stock = $obj->rowid;
569 $this->db->free($resql);
571 $this->errors[] = $this->db->lasterror();
579 if ($type == 0 || $type == 3) {
583 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')))) {
584 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
586 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
587 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
607 if ($alreadyarecord > 0) {
608 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = " . ((float) $oldqtywarehouse + (
float) $qty);
609 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
611 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
612 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
613 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
616 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
617 $resql = $this->db->query($sql);
619 $this->errors[] = $this->db->lasterror();
621 } elseif (empty($fk_product_stock)) {
622 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
627 if (!$error &&
isModEnabled(
'productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
628 if ($id_product_batch > 0) {
629 $result = $this->
createBatch($id_product_batch, $qty);
630 if ($result == -2 && $fk_product_stock > 0) {
631 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
635 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => (
string) $batch);
650 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
651 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
652 $sql .=
" WHERE rowid = ".((int) $fk_product);
654 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
655 $resql = $this->db->query($sql);
657 $this->errors[] = $this->db->lasterror();
662 if (empty($donotcleanemptylines)) {
665 $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)";
666 $resql = $this->db->query($sql);
672 if (!$error &&
getDolGlobalString(
'PRODUIT_SOUSPRODUITS') && !
getDolGlobalString(
'PRODUIT_SOUSPRODUITS_ALWAYS_DISABLE_CHILDREN_STOCK_MOVE') && empty($disablestockchangeforsubproduct)) {
673 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
676 if ($movestock && !$error) {
679 $result = $this->call_trigger(
'STOCK_MOVEMENT', $user);
686 if (!$error &&
isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
687 if ($product->status_batch == 2 && $qty > 0) {
688 if ($this->
getBatchCount($fk_product, (
string) $batch) > 1) {
690 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, (
string) $batch);
700 $this->db->rollback();
701 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);