Can anybody help me to solve this? For example, I have 273 total stocks and a balance remaining as 263 in the database record. I need 10 more for completed. But if I entered 12 it should add as 263 10 in this record and another remaining 2 should go next record as my paper_stock query. But the first record 10 add and save without problem, but the next one added as 68 I don't understand why?
$enterdQty = 12;
for ($i = 0; $i <= $enterdQty; $i ) {
$paper_stock = paper_stock::where('paper_id', $pid)->where('status', 0)->whereRaw('balance < qty')->first();
//first time $paper_stock->qty = 273
//first time $paper_stock->balance = 263
$paper_stock->balance = $paper_stock->balance $i;
$paper_stock->save();
if ($paper_stock->qty == $paper_stock->balance) {
$paper_stock->status = 1;
$paper_stock->save();
}
}
As a summary, I want to update the stock balance and if ented values go to more than the remaining value it should add to the next record balance.
CodePudding user response:
I've improved your code what I guess from your question. Try this one. I hope it will work.
$enterdQty = 12;
//for ($i = 0; $i <= $enterdQty; $i ) {
$paper_stock = paper_stock::where('paper_id', $pid)->where('status', 0)->whereRaw('balance < qty')->first();
//first time $paper_stock->qty = 273
//first time $paper_stock->balance = 263
if ($paper_stock->qty == $paper_stock->balance) {
$paper_stock->status = 1;
$paper_stock->save();
}else{
$stock_diff = $paper_stock->qty - $paper_stock->balance;
if ($stock_diff > $enterdQty) {
$paper_stock->balance = $paper_stock->balance $stock_diff;
$paper_stock->save();
$remaining_stock = $enterdQty - $stock_diff;
$paper_stock = paper_stock::where('paper_id', $pid)->where('status', 0)->whereRaw('balance < qty')->first();
$paper_stock->balance = $paper_stock->balance $remaining_stock;
$paper_stock->save();
} else {
$paper_stock->balance = $paper_stock->balance $stock_diff;
$paper_stock->save();
}
}
CodePudding user response:
Finally, I've figured out the issue, and the below code is working for me. Thanks a lot for helping me.
$enterdQty = $this->balance;
for ($i = 1; $i <= $enterdQty; $i ) {
$paper_stock = paper_stock::where('paper_id', $pid)->where('status', 0)->whereRaw('balance < qty')->first();
$check[] = $i;
$paper_stock->balance = $paper_stock->balance 1;
$paper_stock->save();
if ($paper_stock->qty == $paper_stock->balance) {
$paper_stock->status = 1;
$paper_stock->save();
$enterdQty = $enterdQty - $i;
$i = 0;
}
}