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)
210 global $conf, $langs;
212 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
213 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
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));
219 global $action, $hookmanager;
220 $hookmanager->initHooks(array(
'mouvementstock'));
222 if (is_object($hookmanager)) {
224 'currentcontext' =>
'mouvementstock',
226 'fk_product' => &$fk_product,
227 'entrepot_id' => &$entrepot_id,
232 'inventorycode' => &$inventorycode,
235 'sellby' => &$sellby,
237 'skip_batch' => &$skip_batch,
238 'id_product_batch' => &$id_product_batch
240 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
243 if (!empty($hookmanager->resPrint)) {
247 } elseif ($reshook > 0) {
258 $now = (!empty($datem) ? $datem :
dol_now());
261 if (!($fk_product > 0)) {
264 if (!($entrepot_id > 0)) {
268 if (is_numeric($eatby) && $eatby < 0) {
269 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
270 $this->errors[] =
'ErrorBadValueForParameterEatBy';
273 if (is_numeric($sellby) && $sellby < 0) {
274 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
275 $this->errors[] =
'ErrorBadValueForParameterSellBy';
280 $this->product_id = $fk_product;
281 $this->entrepot_id = $entrepot_id;
282 $this->warehouse_id = $entrepot_id;
286 $this->label = $label;
287 $this->inventorycode = $inventorycode;
289 $this->batch = $batch;
293 $product =
new Product($this->db);
295 $result = $product->fetch($fk_product);
297 $this->error = $product->error;
298 $this->errors = $product->errors;
302 if ($product->id <= 0) {
317 $product->load_stock(
'novirtual');
321 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_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);
327 $this->db->rollback();
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).
"'";
340 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
342 $resql = $this->db->query($sql);
344 $num = $this->db->num_rows($resql);
348 $obj = $this->db->fetch_object($resql);
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) {
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();
362 $eatby = $obj->eatby;
367 $result = $productlot->fetch($obj->rowid);
368 $productlot->eatby = $eatby;
369 $result = $productlot->update($user);
371 $this->error = $productlot->error;
372 $this->errors = $productlot->errors;
373 $this->db->rollback();
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) {
384 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
386 $this->db->rollback();
390 $sellby = $obj->sellby;
395 $result = $productlot->fetch($obj->rowid);
396 $productlot->sellby = $sellby;
397 $result = $productlot->update($user);
399 $this->error = $productlot->error;
400 $this->errors = $productlot->errors;
401 $this->db->rollback();
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;
417 $productlot->eatby = $eatby;
418 $productlot->sellby = $sellby;
419 $result = $productlot->create($user);
421 $this->error = $productlot->error;
422 $this->errors = $productlot->errors;
423 $this->db->rollback();
429 $this->db->rollback();
437 if ($movestock && $qty < 0 && !
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER')) {
438 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
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) {
448 if ($prodbatch->qty < abs($qty)) {
449 $qtyisnotenough = $prodbatch->qty;
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);
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();
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();
478 $fk_project = $this->fk_project;
479 if (!empty($this->origin_type)) {
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;
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";
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);
511 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
512 $resql = $this->db->query($sql);
515 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
518 $this->error = $this->db->lasterror();
519 $this->errors[] = $this->error;
524 $oldqty = $product->stock_reel;
525 $oldpmp = $product->pmp;
526 $oldqtywarehouse = 0;
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);
534 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
535 $resql = $this->db->query($sql);
537 $obj = $this->db->fetch_object($resql);
540 $oldqtywarehouse = $obj->reel;
541 $fk_product_stock = $obj->rowid;
543 $this->db->free($resql);
545 $this->errors[] = $this->db->lasterror();
553 if ($type == 0 || $type == 3) {
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);
560 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
561 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
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);
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).
")";
590 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
591 $resql = $this->db->query($sql);
593 $this->errors[] = $this->db->lasterror();
595 } elseif (empty($fk_product_stock)) {
596 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
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) {
605 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
609 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
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);
628 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
629 $resql = $this->db->query($sql);
631 $this->errors[] = $this->db->lasterror();
636 if (empty($donotcleanemptylines)) {
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);
647 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
650 if ($movestock && !$error) {
658 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
659 if ($product->status_batch == 2 && $qty > 0) {
662 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, $batch);
672 $this->db->rollback();
673 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);