187 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)
190 global $conf, $langs;
192 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
193 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
196 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=".$skip_batch);
199 global $action, $hookmanager;
200 $hookmanager->initHooks(array(
'mouvementstock'));
202 if (is_object($hookmanager)) {
204 'currentcontext' =>
'mouvementstock',
206 'fk_product' => &$fk_product,
207 'entrepot_id' => &$entrepot_id,
212 'inventorycode' => &$inventorycode,
215 'sellby' => &$sellby,
217 'skip_batch' => &$skip_batch,
218 'id_product_batch' => &$id_product_batch
220 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
223 if (!empty($hookmanager->resPrint)) {
227 } elseif ($reshook > 0) {
228 return $hookmanager->resPrint;
238 $now = (!empty($datem) ? $datem :
dol_now());
241 if (!($fk_product > 0)) {
244 if (!($entrepot_id > 0)) {
248 if (is_numeric($eatby) && $eatby < 0) {
249 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
250 $this->errors[] =
'ErrorBadValueForParameterEatBy';
253 if (is_numeric($sellby) && $sellby < 0) {
254 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
255 $this->errors[] =
'ErrorBadValueForParameterSellBy';
260 $this->product_id = $fk_product;
261 $this->entrepot_id = $entrepot_id;
262 $this->warehouse_id = $entrepot_id;
266 $this->label = $label;
267 $this->inventorycode = $inventorycode;
269 $this->batch = $batch;
273 $product =
new Product($this->db);
275 $result = $product->fetch($fk_product);
277 $this->error = $product->error;
278 $this->errors = $product->errors;
282 if ($product->id <= 0) {
297 $product->load_stock(
'novirtual');
301 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
303 $langs->load(
"errors");
304 $this->errors[] = $langs->transnoentitiesnoconv(
"ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
305 dol_syslog(
"Try to make a movement of a product with status_batch on without any batch data");
307 $this->db->rollback();
317 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
318 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
320 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
322 $resql = $this->db->query($sql);
324 $num = $this->db->num_rows($resql);
328 $obj = $this->db->fetch_object($resql);
332 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
333 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
335 $langs->load(
"stocks");
336 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
337 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);
338 $this->db->rollback();
342 $eatby = $obj->eatby;
347 $result = $productlot->fetch($obj->rowid);
348 $productlot->eatby = $eatby;
349 $result = $productlot->update($user);
351 $this->error = $productlot->error;
352 $this->errors = $productlot->errors;
353 $this->db->rollback();
361 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
362 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
364 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
366 $this->db->rollback();
370 $sellby = $obj->sellby;
375 $result = $productlot->fetch($obj->rowid);
376 $productlot->sellby = $sellby;
377 $result = $productlot->update($user);
379 $this->error = $productlot->error;
380 $this->errors = $productlot->errors;
381 $this->db->rollback();
391 $productlot->origin = !empty($this->origin_type) ? $this->origin_type :
'';
392 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
393 $productlot->entity = $conf->entity;
394 $productlot->fk_product = $fk_product;
395 $productlot->batch = $batch;
397 $productlot->eatby = $eatby;
398 $productlot->sellby = $sellby;
399 $result = $productlot->create($user);
401 $this->error = $productlot->error;
402 $this->errors = $productlot->errors;
403 $this->db->rollback();
409 $this->db->rollback();
416 if ($movestock && $qty < 0 && !
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER')) {
417 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
421 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
422 if ((
string) $batch != (
string) $batchcursor) {
427 if ($prodbatch->qty < abs($qty)) {
428 $qtyisnotenough = $prodbatch->qty;
432 if (!$foundforbatch || $qtyisnotenough) {
433 $langs->load(
"stocks");
434 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
435 $tmpwarehouse =
new Entrepot($this->db);
436 $tmpwarehouse->fetch($entrepot_id);
438 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
439 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
440 $this->db->rollback();
444 if (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty)) {
445 $langs->load(
"stocks");
446 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
447 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
448 $this->db->rollback();
456 $fk_project = $this->fk_project;
457 if (!empty($this->origin_type)) {
458 $origin_type = $this->origin_type;
459 $origin_id = $this->origin_id;
460 if (empty($fk_project) && $origin_type ==
'project') {
461 $fk_project = $origin_id;
471 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
472 $sql .=
" datem, fk_product, batch, eatby, sellby,";
473 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
475 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
476 $sql .=
" ".($batch ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
477 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
478 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
479 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
480 $sql .=
" ".((int) $user->id).
",";
481 $sql .=
" '".$this->db->escape($label).
"',";
482 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
483 $sql .=
" ".((float)
price2num($price)).
",";
484 $sql .=
" ".((int) $origin_id).
",";
485 $sql .=
" '".$this->db->escape($origin_type).
"',";
486 $sql .=
" ".((int) $fk_project);
489 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
490 $resql = $this->db->query($sql);
493 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
496 $this->error = $this->db->lasterror();
497 $this->errors[] = $this->error;
502 $oldqty = $product->stock_reel;
503 $oldpmp = $product->pmp;
504 $oldqtywarehouse = 0;
509 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
510 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
512 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
513 $resql = $this->db->query($sql);
515 $obj = $this->db->fetch_object($resql);
518 $oldqtywarehouse = $obj->reel;
519 $fk_product_stock = $obj->rowid;
521 $this->db->free($resql);
523 $this->errors[] = $this->db->lasterror();
531 if ($type == 0 || $type == 3) {
535 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')))) {
536 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
539 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
549 } elseif ($type == 1 || $type == 2) {
559 if ($alreadyarecord > 0) {
560 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = " . ((float) $oldqtywarehouse + (
float) $qty);
561 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
563 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
564 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
565 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
568 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
569 $resql = $this->db->query($sql);
571 $this->errors[] = $this->db->lasterror();
573 } elseif (empty($fk_product_stock)) {
574 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
579 if (!$error && isModEnabled(
'productbatch') && (($product->hasbatch() && !$skip_batch) || $force_update_batch)) {
580 if ($id_product_batch > 0) {
581 $result = $this->
createBatch($id_product_batch, $qty);
582 if ($result == -2 && $fk_product_stock > 0) {
583 $param_batch = array(
'fk_product_stock' =>$fk_product_stock,
'batchnumber'=>$batch);
587 $param_batch = array(
'fk_product_stock' =>$fk_product_stock,
'batchnumber'=>$batch);
602 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
603 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
604 $sql .=
" WHERE rowid = ".((int) $fk_product);
606 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
607 $resql = $this->db->query($sql);
609 $this->errors[] = $this->db->lasterror();
614 if (empty($donotcleanemptylines)) {
617 $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)";
618 $resql = $this->db->query($sql);
625 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
628 if ($movestock && !$error) {
637 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
638 if ($product->status_batch == 2 && $qty > 0) {
641 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, $batch);
651 $this->db->rollback();
652 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);