Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #28533 Mo::deleteLine removes the "main" MoLine if consumed line is delete #28535

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 94 additions & 58 deletions htdocs/mrp/class/mo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,78 +891,58 @@ public function delete(User $user, $notrigger = false, $also_cancel_consumed_and
*/
public function deleteLine(User $user, $idline, $notrigger = false)
{
global $langs;
$langs->loadLangs(array('stocks', 'mrp'));
$error = 0;

if ($this->status < 0) {
$this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
return -2;
}
$productstatic = new Product($this->db);
$fk_movement = GETPOST('fk_movement', 'int');
$arrayoflines = $this->fetchLinesLinked('consumed', $idline);

if (!empty($arrayoflines)) {
$this->db->begin();

$stockmove = new MouvementStock($this->db);
$stockmove->setOrigin($this->element, $this->id);
$moline = new MoLine($this->db);
$moline->fetch($idline);

if (!empty($fk_movement)) {
$moline = new MoLine($this->db);
$TArrayMoLine = $moline->fetchAll('', '', 1, 0, array('customsql' => 'fk_stock_movement ='.$fk_movement));
$moline = array_shift($TArrayMoLine);
$affectedLinkedMoLines;
// Check if it's linked or a main line
if (empty($moline->fk_mrp_production)) {
// Check if the main line has linked lines
$affectedLinkedMoLines = $this->fetchLinesLinked('consumed', $moline->id);
}

$movement = new MouvementStock($this->db);
$movement->fetch($fk_movement);
$productstatic->fetch($movement->product_id);
$qtytoprocess = $movement->qty;
$this->db->begin();

// Reverse stock movement
$labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref);
$codemovementCancel = $langs->trans("StockIncrease");
// undo stockmovements and remove linked lines
if (!empty($affectedLinkedMoLines)) {
foreach ($affectedLinkedMoLines as $linkedLine) {
if (!$error) {
$linkedMoline = new MoLine($this->db);
$linkedMoline->fetch($linkedLine['rowid']);
$result = $linkedMoline->delete($user, $notrigger);

if (($qtytoprocess >= 0)) {
$idstockmove = $stockmove->reception($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, '', '', $movement->batch, dol_now(), 0, $codemovementCancel);
} else {
$idstockmove = $stockmove->livraison($user, $movement->product_id, $movement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, dol_now(), '', '', $movement->batch, 0, $codemovementCancel);
}
if ($idstockmove < 0) {
$this->error++;
$this->db->rollback();
setEventMessages($stockmove->error, $stockmove->errors, 'errors');
} else {
$this->db->commit();
if ($result < 0) {
$error++;
setEventMessages($linkedMoline->error, $linkedMoline->errors, 'errors');
}
}
return $moline->delete($user, $notrigger);
} else {
foreach ($arrayoflines as $key => $arrayofline) {
$lineDetails = $arrayoflines[$key];
$productstatic->fetch($lineDetails['fk_product']);
$qtytoprocess = $lineDetails['qty'];
}
}

// Reverse stock movement
$labelmovementCancel = $langs->trans("CancelProductionForRef", $productstatic->ref);
$codemovementCancel = $langs->trans("StockIncrease");
if (!$error) {
$result = $moline->delete($user, $notrigger);

if ($qtytoprocess >= 0) {
$idstockmove = $stockmove->reception($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, '', '', $lineDetails['batch'], dol_now(), 0, $codemovementCancel);
} else {
$idstockmove = $stockmove->livraison($user, $lineDetails['fk_product'], $lineDetails['fk_warehouse'], $qtytoprocess, 0, $labelmovementCancel, dol_now(), '', '', $lineDetails['batch'], 0, $codemovementCancel);
}
if ($idstockmove < 0) {
$this->error++;
$this->db->rollback();
setEventMessages($stockmove->error, $stockmove->errors, 'errors');
} else {
$this->db->commit();
}
}
return $this->deleteLineCommon($user, $idline, $notrigger);
if ($result < 0) {
$error++;
setEventMessages($moline->error, $moline->errors, 'errors');
}
}

if (!$error) {
$this->db->commit();
$result = 1;
} else {
return $this->deleteLineCommon($user, $idline, $notrigger);
$this->db->rollback();
$result = -1;
}
return $result;
}


Expand Down Expand Up @@ -2222,7 +2202,63 @@ public function update(User $user, $notrigger = false)
*/
public function delete(User $user, $notrigger = false)
{
return $this->deleteCommon($user, $notrigger);
//return $this->deleteCommon($user, $notrigger, 1);
global $langs;

$error = 0;
$result = -1;

// the stockmovements it was created for this line revise
if (!empty($this->fk_warehouse)) {
$langs->loadLangs(array('stocks', 'mrp'));

// load the mo for this line
$tmpmo = new Mo($this->db);
$tmpmo->fetch($this->fk_mo);

// load the (old) linked stockmovement and the associated product
$linkedMovement = new MouvementStock($this->db);
$linkedMovement->fetch($this->fk_stock_movement);
$productForLinkedMovement = new Product($this->db);
$productForLinkedMovement->fetch($linkedMovement->product_id);

// create new stockmovement to revise the linked stockmovement
$stockmove = new MouvementStock($this->db);
$stockmove->setOrigin($tmpmo->element, $tmpmo->id);

// Reverse stock movement
$labelmovementCancel = $langs->trans("CancelProductionForRef", $productForLinkedMovement->ref);
$codemovementCancel = dol_print_date(dol_now(), 'dayhourlog');

$idstockmove = -1;
if (($linkedMovement->qty < 0)) {
$qtytoprocess = $linkedMovement->qty * -1;
$idstockmove = $stockmove->reception($user, $linkedMovement->product_id, $linkedMovement->warehouse_id, $qtytoprocess, 0, $labelmovementCancel, '', '', $linkedMovement->batch, dol_now(), 0, $codemovementCancel);
} else {
$idstockmove = $stockmove->livraison($user, $linkedMovement->product_id, $linkedMovement->warehouse_id, $linkedMovement->qty, 0, $labelmovementCancel, dol_now(), '', '', $linkedMovement->batch, 0, $codemovementCancel);
}

if ($idstockmove < 0) {
$this->error++;
setEventMessages($stockmove->error, $stockmove->errors, 'errors');
}
}

if (!$error) {
$result = $this->deleteCommon($user, $notrigger);
if ($result < 0) {
$this->error++;
setEventMessages($stockmove->error, $stockmove->errors, 'errors');
}
}

if (!$error) {
$this->db->commit();
$result = 1;
} else {
$this->db->rollback();
$result = -1;
}

return $result;
}
}
2 changes: 1 addition & 1 deletion htdocs/mrp/mo_production.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@

// Action delete line
if ($permissiontodelete) {
$href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line->id).'&fk_movement='.((int) $line2['fk_stock_movement']);
$href = $_SERVER["PHP_SELF"].'?id='.((int) $object->id).'&action=deleteline&token='.newToken().'&lineid='.((int) $line2['rowid']);
print '<td class="center">';
print '<a class="reposition" href="'.$href.'">';
print img_picto($langs->trans('TooltipDeleteAndRevertStockMovement'), 'delete');
Expand Down
Loading