191 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)
194 global $conf, $langs;
196 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
197 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
200 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));
203 global $action, $hookmanager;
204 $hookmanager->initHooks(array(
'mouvementstock'));
206 if (is_object($hookmanager)) {
208 'currentcontext' =>
'mouvementstock',
210 'fk_product' => &$fk_product,
211 'entrepot_id' => &$entrepot_id,
216 'inventorycode' => &$inventorycode,
219 'sellby' => &$sellby,
221 'skip_batch' => &$skip_batch,
222 'id_product_batch' => &$id_product_batch
224 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
227 if (!empty($hookmanager->resPrint)) {
231 } elseif ($reshook > 0) {
242 $now = (!empty($datem) ? $datem :
dol_now());
245 if (!($fk_product > 0)) {
248 if (!($entrepot_id > 0)) {
252 if (is_numeric($eatby) && $eatby < 0) {
253 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
254 $this->errors[] =
'ErrorBadValueForParameterEatBy';
257 if (is_numeric($sellby) && $sellby < 0) {
258 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
259 $this->errors[] =
'ErrorBadValueForParameterSellBy';
264 $this->product_id = $fk_product;
265 $this->entrepot_id = $entrepot_id;
266 $this->warehouse_id = $entrepot_id;
270 $this->label = $label;
271 $this->inventorycode = $inventorycode;
273 $this->batch = $batch;
277 $product =
new Product($this->db);
279 $result = $product->fetch($fk_product);
281 $this->error = $product->error;
282 $this->errors = $product->errors;
286 if ($product->id <= 0) {
301 $product->load_stock(
'novirtual');
305 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
307 $langs->load(
"errors");
308 $this->errors[] = $langs->transnoentitiesnoconv(
"ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
309 dol_syslog(
"Try to make a movement of a product with status_batch on without any batch data", LOG_ERR);
311 $this->db->rollback();
321 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
322 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
324 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
326 $resql = $this->db->query($sql);
328 $num = $this->db->num_rows($resql);
332 $obj = $this->db->fetch_object($resql);
336 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
337 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
339 $langs->load(
"stocks");
340 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
341 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);
342 $this->db->rollback();
346 $eatby = $obj->eatby;
351 $result = $productlot->fetch($obj->rowid);
352 $productlot->eatby = $eatby;
353 $result = $productlot->update($user);
355 $this->error = $productlot->error;
356 $this->errors = $productlot->errors;
357 $this->db->rollback();
365 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
366 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
368 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
370 $this->db->rollback();
374 $sellby = $obj->sellby;
379 $result = $productlot->fetch($obj->rowid);
380 $productlot->sellby = $sellby;
381 $result = $productlot->update($user);
383 $this->error = $productlot->error;
384 $this->errors = $productlot->errors;
385 $this->db->rollback();
395 $productlot->origin = !empty($this->origin_type) ? $this->origin_type :
'';
396 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
397 $productlot->entity = $conf->entity;
398 $productlot->fk_product = $fk_product;
399 $productlot->batch = $batch;
401 $productlot->eatby = $eatby;
402 $productlot->sellby = $sellby;
403 $result = $productlot->create($user);
405 $this->error = $productlot->error;
406 $this->errors = $productlot->errors;
407 $this->db->rollback();
413 $this->db->rollback();
420 if ($movestock && $qty < 0 && !
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER')) {
421 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
424 if (isset($product->stock_warehouse[$entrepot_id])) {
425 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
426 if ((
string) $batch != (
string) $batchcursor) {
431 if ($prodbatch->qty < abs($qty)) {
432 $qtyisnotenough = $prodbatch->qty;
437 if (!$foundforbatch || $qtyisnotenough) {
438 $langs->load(
"stocks");
439 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
440 $tmpwarehouse =
new Entrepot($this->db);
441 $tmpwarehouse->fetch($entrepot_id);
443 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
444 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
445 $this->db->rollback();
449 if (isset($product->stock_warehouse[$entrepot_id]) && (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty))) {
450 $langs->load(
"stocks");
451 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
452 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
453 $this->db->rollback();
461 $fk_project = $this->fk_project;
462 if (!empty($this->origin_type)) {
463 $origin_type = $this->origin_type;
464 $origin_id = $this->origin_id;
465 if (empty($fk_project) && $origin_type ==
'project') {
466 $fk_project = $origin_id;
476 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
477 $sql .=
" datem, fk_product, batch, eatby, sellby,";
478 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
480 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
481 $sql .=
" ".($batch ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
482 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
483 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
484 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
485 $sql .=
" ".((int) $user->id).
",";
486 $sql .=
" '".$this->db->escape($label).
"',";
487 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
488 $sql .=
" ".((float)
price2num($price)).
",";
489 $sql .=
" ".((int) $origin_id).
",";
490 $sql .=
" '".$this->db->escape($origin_type).
"',";
491 $sql .=
" ".((int) $fk_project);
494 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
495 $resql = $this->db->query($sql);
498 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
501 $this->error = $this->db->lasterror();
502 $this->errors[] = $this->error;
507 $oldqty = $product->stock_reel;
508 $oldpmp = $product->pmp;
509 $oldqtywarehouse = 0;
514 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
515 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
517 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
518 $resql = $this->db->query($sql);
520 $obj = $this->db->fetch_object($resql);
523 $oldqtywarehouse = $obj->reel;
524 $fk_product_stock = $obj->rowid;
526 $this->db->free($resql);
528 $this->errors[] = $this->db->lasterror();
536 if ($type == 0 || $type == 3) {
540 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')))) {
541 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
544 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
564 if ($alreadyarecord > 0) {
565 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = " . ((float) $oldqtywarehouse + (
float) $qty);
566 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
568 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
569 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
570 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
573 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
574 $resql = $this->db->query($sql);
576 $this->errors[] = $this->db->lasterror();
578 } elseif (empty($fk_product_stock)) {
579 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
584 if (!$error && isModEnabled(
'productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
585 if ($id_product_batch > 0) {
586 $result = $this->
createBatch($id_product_batch, $qty);
587 if ($result == -2 && $fk_product_stock > 0) {
588 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
592 $param_batch = array(
'fk_product_stock' => $fk_product_stock,
'batchnumber' => $batch);
607 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
608 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
609 $sql .=
" WHERE rowid = ".((int) $fk_product);
611 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
612 $resql = $this->db->query($sql);
614 $this->errors[] = $this->db->lasterror();
619 if (empty($donotcleanemptylines)) {
622 $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)";
623 $resql = $this->db->query($sql);
630 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
633 if ($movestock && !$error) {
641 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
642 if ($product->status_batch == 2 && $qty > 0) {
645 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, $batch);
655 $this->db->rollback();
656 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);