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)
225 global
$conf, $langs;
227 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
228 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
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));
234 global $action, $hookmanager;
235 $hookmanager->initHooks(array(
'mouvementstock'));
237 if (is_object($hookmanager)) {
239 'currentcontext' =>
'mouvementstock',
241 'fk_product' => &$fk_product,
242 'entrepot_id' => &$entrepot_id,
247 'inventorycode' => &$inventorycode,
250 'sellby' => &$sellby,
252 'skip_batch' => &$skip_batch,
253 'id_product_batch' => &$id_product_batch
255 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
258 if (!empty($hookmanager->resPrint)) {
262 } elseif ($reshook > 0) {
273 $now = (!empty($datem) ? $datem :
dol_now());
276 if (!($fk_product > 0)) {
279 if (!($entrepot_id > 0)) {
283 if (is_numeric($eatby) && $eatby < 0) {
284 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
285 $this->errors[] =
'ErrorBadValueForParameterEatBy';
288 if (is_numeric($sellby) && $sellby < 0) {
289 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
290 $this->errors[] =
'ErrorBadValueForParameterSellBy';
295 $this->product_id = $fk_product;
296 $this->entrepot_id = $entrepot_id;
297 $this->warehouse_id = $entrepot_id;
301 $this->label = $label;
302 $this->inventorycode = $inventorycode;
304 $this->batch = $batch;
308 $product =
new Product($this->db);
310 $result = $product->fetch($fk_product);
312 $this->error = $product->error;
313 $this->errors = $product->errors;
317 if ($product->id <= 0) {
332 $product->load_stock(
'novirtual');
336 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_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);
342 $this->db->rollback();
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).
"'";
355 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
357 $resql = $this->db->query($sql);
359 $num = $this->db->num_rows($resql);
363 $obj = $this->db->fetch_object($resql);
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) {
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();
377 $eatby = $obj->eatby;
382 $result = $productlot->fetch($obj->rowid);
383 $productlot->eatby = $eatby;
384 $result = $productlot->update($user);
386 $this->error = $productlot->error;
387 $this->errors = $productlot->errors;
388 $this->db->rollback();
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) {
399 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
401 $this->db->rollback();
405 $sellby = $obj->sellby;
410 $result = $productlot->fetch($obj->rowid);
411 $productlot->sellby = $sellby;
412 $result = $productlot->update($user);
414 $this->error = $productlot->error;
415 $this->errors = $productlot->errors;
416 $this->db->rollback();
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;
432 $productlot->eatby = $eatby;
433 $productlot->sellby = $sellby;
434 $result = $productlot->create($user);
436 $this->error = $productlot->error;
437 $this->errors = $productlot->errors;
438 $this->db->rollback();
444 $this->db->rollback();
452 if ($movestock && $qty < 0 && !
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER')) {
453 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
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) {
463 if ($prodbatch->qty < abs($qty)) {
464 $qtyisnotenough = $prodbatch->qty;
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);
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();
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();
493 $fk_project = $this->fk_project;
494 if (!empty($this->origin_type)) {
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;
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";
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);
526 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
527 $resql = $this->db->query($sql);
530 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
533 $this->error = $this->db->lasterror();
534 $this->errors[] = $this->error;
539 $oldqty = $product->stock_reel;
540 $oldpmp = $product->pmp;
541 $oldqtywarehouse = 0;
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);
549 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
550 $resql = $this->db->query($sql);
552 $obj = $this->db->fetch_object($resql);
555 $oldqtywarehouse = $obj->reel;
556 $fk_product_stock = $obj->rowid;
558 $this->db->free($resql);
560 $this->errors[] = $this->db->lasterror();
568 if ($type == 0 || $type == 3) {
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);
575 if ($oldpmp > 0 && ($oldqtytouse + $qty) != 0) {
576 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
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);
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).
")";
605 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
606 $resql = $this->db->query($sql);
608 $this->errors[] = $this->db->lasterror();
610 } elseif (empty($fk_product_stock)) {
611 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
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) {
620 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
624 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
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);
643 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
644 $resql = $this->db->query($sql);
646 $this->errors[] = $this->db->lasterror();
651 if (empty($donotcleanemptylines)) {
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);
662 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
665 if ($movestock && !$error) {
673 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
674 if ($product->status_batch == 2 && $qty > 0) {
677 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, $batch);
687 $this->db->rollback();
688 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);