180 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)
183 global $conf, $langs;
185 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
186 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
189 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);
192 global $action, $hookmanager;
193 $hookmanager->initHooks(array(
'mouvementstock'));
195 if (is_object($hookmanager)) {
197 'currentcontext' =>
'mouvementstock',
199 'fk_product' => &$fk_product,
200 'entrepot_id' => &$entrepot_id,
205 'inventorycode' => &$inventorycode,
208 'sellby' => &$sellby,
210 'skip_batch' => &$skip_batch,
211 'id_product_batch' => &$id_product_batch
213 $reshook = $hookmanager->executeHooks(
'stockMovementCreate', $parameters, $this, $action);
216 if (!empty($hookmanager->resPrint)) {
220 } elseif ($reshook > 0) {
221 return $hookmanager->resPrint;
228 if (empty($price)) $price = 0;
229 $now = (!empty($datem) ? $datem :
dol_now());
232 if (!($fk_product > 0))
return 0;
233 if (!($entrepot_id > 0))
return 0;
235 if (is_numeric($eatby) && $eatby < 0) {
236 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterEatBy eatby = ".$eatby);
237 $this->errors[] =
'ErrorBadValueForParameterEatBy';
240 if (is_numeric($sellby) && $sellby < 0) {
241 dol_syslog(get_class($this).
"::_create start ErrorBadValueForParameterSellBy sellby = ".$sellby);
242 $this->errors[] =
'ErrorBadValueForParameterSellBy';
247 $this->product_id = $fk_product;
248 $this->entrepot_id = $entrepot_id;
249 $this->warehouse_id = $entrepot_id;
253 $this->label = $label;
254 $this->inventorycode = $inventorycode;
256 $this->batch = $batch;
260 $product =
new Product($this->db);
262 $result = $product->fetch($fk_product);
264 $this->error = $product->error;
265 $this->errors = $product->errors;
269 if ($product->id <= 0) {
276 if ($product->type !=
Product::TYPE_SERVICE || !empty($conf->global->STOCK_SUPPORTS_SERVICES)) $movestock = 1;
282 $product->load_stock(
'novirtual');
286 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
288 $langs->load(
"errors");
289 $this->errors[] = $langs->transnoentitiesnoconv(
"ErrorTryToMakeMoveOnProductRequiringBatchData", $product->ref);
290 dol_syslog(
"Try to make a movement of a product with status_batch on without any batch data");
292 $this->db->rollback();
302 $sql =
"SELECT pb.rowid, pb.batch, pb.eatby, pb.sellby FROM ".$this->db->prefix().
"product_lot as pb";
303 $sql .=
" WHERE pb.fk_product = ".((int) $fk_product).
" AND pb.batch = '".$this->db->escape($batch).
"'";
305 dol_syslog(get_class($this).
"::_create scan serial for this product to check if eatby and sellby match", LOG_DEBUG);
307 $resql = $this->db->query($sql);
309 $num = $this->db->num_rows($resql);
313 $obj = $this->db->fetch_object($resql);
317 $eatbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
318 if ($this->db->jdate($obj->eatby) != $eatby && $this->db->jdate($obj->eatby) != $eatbywithouthour) {
320 $langs->load(
"stocks");
321 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->eatby),
'dayhour'),
dol_print_date($eatbywithouthour,
'dayhour'));
322 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);
323 $this->db->rollback();
327 $eatby = $obj->eatby;
332 $result = $productlot->fetch($obj->rowid);
333 $productlot->eatby = $eatby;
334 $result = $productlot->update($user);
336 $this->error = $productlot->error;
337 $this->errors = $productlot->errors;
338 $this->db->rollback();
346 $sellbywithouthour =
dol_mktime(0, 0, 0, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
347 if ($this->db->jdate($obj->sellby) != $sellby && $this->db->jdate($obj->sellby) != $sellbywithouthour) {
349 $this->errors[] = $langs->transnoentitiesnoconv(
"ThisSerialAlreadyExistWithDifferentDate", $batch,
dol_print_date($this->db->jdate($obj->sellby)),
dol_print_date($sellby));
351 $this->db->rollback();
355 $sellby = $obj->sellby;
360 $result = $productlot->fetch($obj->rowid);
361 $productlot->sellby = $sellby;
362 $result = $productlot->update($user);
364 $this->error = $productlot->error;
365 $this->errors = $productlot->errors;
366 $this->db->rollback();
376 $productlot->origin = !empty($this->origin_type) ? $this->origin_type :
'';
377 $productlot->origin_id = !empty($this->origin_id) ? $this->origin_id : 0;
378 $productlot->entity = $conf->entity;
379 $productlot->fk_product = $fk_product;
380 $productlot->batch = $batch;
382 $productlot->eatby = $eatby;
383 $productlot->sellby = $sellby;
384 $result = $productlot->create($user);
386 $this->error = $productlot->error;
387 $this->errors = $productlot->errors;
388 $this->db->rollback();
394 $this->db->rollback();
401 if ($movestock && $qty < 0 && !
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER')) {
402 if (isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
406 foreach ($product->stock_warehouse[$entrepot_id]->detail_batch as $batchcursor => $prodbatch) {
407 if ((
string) $batch != (
string) $batchcursor) {
412 if ($prodbatch->qty < abs($qty)) $qtyisnotenough = $prodbatch->qty;
415 if (!$foundforbatch || $qtyisnotenough) {
416 $langs->load(
"stocks");
417 include_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
418 $tmpwarehouse =
new Entrepot($this->db);
419 $tmpwarehouse->fetch($entrepot_id);
421 $this->error = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
422 $this->errors[] = $langs->trans(
'qtyToTranferLotIsNotEnough', $product->ref, $batch, $qtyisnotenough, $tmpwarehouse->ref);
423 $this->db->rollback();
427 if (empty($product->stock_warehouse[$entrepot_id]->real) || $product->stock_warehouse[$entrepot_id]->real < abs($qty)) {
428 $langs->load(
"stocks");
429 $this->error = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
430 $this->errors[] = $langs->trans(
'qtyToTranferIsNotEnough').
' : '.$product->ref;
431 $this->db->rollback();
439 $fk_project = $this->fk_project;
440 if (!empty($this->origin_type)) {
441 $origin_type = $this->origin_type;
442 $origin_id = $this->origin_id;
443 if (empty($fk_project) && $origin_type ==
'project') {
444 $fk_project = $origin_id;
454 $sql =
"INSERT INTO ".$this->db->prefix().
"stock_mouvement(";
455 $sql .=
" datem, fk_product, batch, eatby, sellby,";
456 $sql .=
" fk_entrepot, value, type_mouvement, fk_user_author, label, inventorycode, price, fk_origin, origintype, fk_projet";
458 $sql .=
" VALUES ('".$this->db->idate($this->datem).
"', ".((int) $this->product_id).
", ";
459 $sql .=
" ".($batch ?
"'".$this->db->escape($batch).
"'" :
"null").
", ";
460 $sql .=
" ".($eatby ?
"'".$this->db->idate($eatby).
"'" :
"null").
", ";
461 $sql .=
" ".($sellby ?
"'".$this->db->idate($sellby).
"'" :
"null").
", ";
462 $sql .=
" ".((int) $this->entrepot_id).
", ".((float) $this->qty).
", ".((int) $this->
type).
",";
463 $sql .=
" ".((int) $user->id).
",";
464 $sql .=
" '".$this->db->escape($label).
"',";
465 $sql .=
" ".($inventorycode ?
"'".$this->db->escape($inventorycode).
"'" :
"null").
",";
466 $sql .=
" ".((float)
price2num($price)).
",";
467 $sql .=
" ".((int) $origin_id).
",";
468 $sql .=
" '".$this->db->escape($origin_type).
"',";
469 $sql .=
" ".((int) $fk_project);
472 dol_syslog(get_class($this).
"::_create insert record into stock_mouvement", LOG_DEBUG);
473 $resql = $this->db->query($sql);
476 $mvid = $this->db->last_insert_id($this->db->prefix().
"stock_mouvement");
479 $this->error = $this->db->lasterror();
480 $this->errors[] = $this->error;
485 $oldqty = $product->stock_reel;
486 $oldpmp = $product->pmp;
487 $oldqtywarehouse = 0;
492 $sql =
"SELECT rowid, reel FROM ".$this->db->prefix().
"product_stock";
493 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
495 dol_syslog(get_class($this).
"::_create check if a record already exists in product_stock", LOG_DEBUG);
496 $resql = $this->db->query($sql);
498 $obj = $this->db->fetch_object($resql);
501 $oldqtywarehouse = $obj->reel;
502 $fk_product_stock = $obj->rowid;
504 $this->db->free($resql);
506 $this->errors[] = $this->db->lasterror();
514 if ($type == 0 || $type == 3) {
518 if ($price > 0 || (!empty($conf->global->STOCK_UPDATE_AWP_EVEN_WHEN_ENTRY_PRICE_IS_NULL) && $price == 0)) {
519 $oldqtytouse = ($oldqty >= 0 ? $oldqty : 0);
522 $newpmp =
price2num((($oldqtytouse * $oldpmp) + ($qty * $price)) / ($oldqtytouse + $qty),
'MU');
532 } elseif ($type == 1 || $type == 2) {
542 if ($alreadyarecord > 0) {
543 $sql =
"UPDATE ".$this->db->prefix().
"product_stock SET reel = COALESCE(reel, 0) + ".((float) $qty);
544 $sql .=
" WHERE fk_entrepot = ".((int) $entrepot_id).
" AND fk_product = ".((int) $fk_product);
546 $sql =
"INSERT INTO ".$this->db->prefix().
"product_stock";
547 $sql .=
" (reel, fk_entrepot, fk_product) VALUES ";
548 $sql .=
" (".((float) $qty).
", ".((int) $entrepot_id).
", ".((int) $fk_product).
")";
551 dol_syslog(get_class($this).
"::_create update stock value", LOG_DEBUG);
552 $resql = $this->db->query($sql);
554 $this->errors[] = $this->db->lasterror();
556 } elseif (empty($fk_product_stock)) {
557 $fk_product_stock = $this->db->last_insert_id($this->db->prefix().
"product_stock");
562 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
563 if ($id_product_batch > 0) {
564 $result = $this->
createBatch($id_product_batch, $qty);
565 if ($result == -2 && $fk_product_stock > 0) {
566 $param_batch = array(
'fk_product_stock' =>$fk_product_stock,
'batchnumber'=>$batch);
570 $param_batch = array(
'fk_product_stock' =>$fk_product_stock,
'batchnumber'=>$batch);
585 $sql =
"UPDATE ".$this->db->prefix().
"product as p SET pmp = ".((float) $newpmp).
",";
586 $sql .=
" stock=(SELECT SUM(ps.reel) FROM ".$this->db->prefix().
"product_stock as ps WHERE ps.fk_product = p.rowid)";
587 $sql .=
" WHERE rowid = ".((int) $fk_product);
589 dol_syslog(get_class($this).
"::_create update AWP", LOG_DEBUG);
590 $resql = $this->db->query($sql);
592 $this->errors[] = $this->db->lasterror();
597 if (empty($donotcleanemptylines)) {
600 $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)";
601 $resql = $this->db->query($sql);
607 if (!$error && !empty($conf->global->PRODUIT_SOUSPRODUITS) && empty($conf->global->INDEPENDANT_SUBPRODUCT_STOCK) && empty($disablestockchangeforsubproduct)) {
608 $error = $this->
_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0, $label, $inventorycode, $datem);
611 if ($movestock && !$error) {
614 if ($result < 0) $error++;
618 if (!$error && isModEnabled(
'productbatch') && $product->hasbatch() && !$skip_batch) {
619 if ($product->status_batch == 2 && $qty > 0) {
622 $this->errors[] = $langs->trans(
"TooManyQtyForSerialNumber", $product->ref, $batch);
632 $this->db->rollback();
633 dol_syslog(get_class($this).
"::_create error code=".$error, LOG_ERR);