mirror of
https://github.com/grocy/grocy.git
synced 2026-04-09 14:06:16 +02:00
fixed bug regarding wrong calculation for subproductsubstitution
This commit is contained in:
parent
ff056f8d81
commit
6d3f1d1a05
|
|
@ -406,17 +406,15 @@ class StockService extends BaseService
|
||||||
$potentialStockEntries = FindAllObjectsInArrayByPropertyValue($potentialStockEntries, 'stock_id', $specificStockEntryId);
|
$potentialStockEntries = FindAllObjectsInArrayByPropertyValue($potentialStockEntries, 'stock_id', $specificStockEntryId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$productStockAmount = floatval($productDetails->stock_amount_aggregated);
|
|
||||||
if ($amount > $productStockAmount)
|
|
||||||
{
|
|
||||||
throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($transactionId === null)
|
if ($transactionId === null)
|
||||||
{
|
{
|
||||||
$transactionId = uniqid();
|
$transactionId = uniqid();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//first simulate stock consumption. Otherwhise calculations are wrong when stock units of parent and child products have a big conversion factor
|
||||||
|
$stockeEntriesToConsume = [];
|
||||||
|
|
||||||
|
// collect all stockentries
|
||||||
foreach ($potentialStockEntries as $stockEntry)
|
foreach ($potentialStockEntries as $stockEntry)
|
||||||
{
|
{
|
||||||
if ($amount == 0)
|
if ($amount == 0)
|
||||||
|
|
@ -431,13 +429,51 @@ class StockService extends BaseService
|
||||||
$conversion = $this->getDatabase()->quantity_unit_conversions_resolved()->where('product_id = :1 AND from_qu_id = :2 AND to_qu_id = :3', $stockEntry->product_id, $productDetails->product->qu_id_stock, $subProduct->qu_id_stock)->fetch();
|
$conversion = $this->getDatabase()->quantity_unit_conversions_resolved()->where('product_id = :1 AND from_qu_id = :2 AND to_qu_id = :3', $stockEntry->product_id, $productDetails->product->qu_id_stock, $subProduct->qu_id_stock)->fetch();
|
||||||
if ($conversion != null)
|
if ($conversion != null)
|
||||||
{
|
{
|
||||||
$amount = $amount * floatval($conversion->factor);
|
//remember factor for later
|
||||||
|
$factor = floatval($conversion->factor);
|
||||||
|
$amount = $amount * $factor;
|
||||||
|
}else{
|
||||||
|
//fail when no conversion is provided, calculattions will be wrong otherwhise
|
||||||
|
throw new \Exception('No conversion from child to parent product');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//collect data
|
||||||
if ($amount >= $stockEntry->amount)
|
if ($amount >= $stockEntry->amount)
|
||||||
{
|
{
|
||||||
|
$stockeEntriesToConsume[] = [
|
||||||
|
"entry" => $stockEntry,
|
||||||
|
"amount" => $stockEntry->amount
|
||||||
|
];
|
||||||
|
|
||||||
|
$amount -= $stockEntry->amount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$stockeEntriesToConsume[] = [
|
||||||
|
"entry" => $stockEntry,
|
||||||
|
"amount" => $amount
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
$amount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert back when $amount had been scaled beforehand
|
||||||
|
if ($allowSubproductSubstitution && $stockEntry->product_id != $productId){
|
||||||
|
$amount=$amount/$factor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//only when stock can be fullfilled, persist it
|
||||||
|
if($amount<=0){
|
||||||
|
foreach($stockeEntriesToConsume as $entry){
|
||||||
|
$stockEntry = $entry['entry'];
|
||||||
|
$amount = $entry['amount'];
|
||||||
// Take the whole stock entry
|
// Take the whole stock entry
|
||||||
|
if($amount==$stockEntry->amount){
|
||||||
$logRow = $this->getDatabase()->stock_log()->createRow([
|
$logRow = $this->getDatabase()->stock_log()->createRow([
|
||||||
'product_id' => $stockEntry->product_id,
|
'product_id' => $stockEntry->product_id,
|
||||||
'amount' => $stockEntry->amount * -1,
|
'amount' => $stockEntry->amount * -1,
|
||||||
|
|
@ -458,11 +494,7 @@ class StockService extends BaseService
|
||||||
$logRow->save();
|
$logRow->save();
|
||||||
|
|
||||||
$stockEntry->delete();
|
$stockEntry->delete();
|
||||||
|
}else{
|
||||||
$amount -= $stockEntry->amount;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
|
// Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
|
||||||
$restStockAmount = $stockEntry->amount - $amount;
|
$restStockAmount = $stockEntry->amount - $amount;
|
||||||
|
|
||||||
|
|
@ -488,10 +520,11 @@ class StockService extends BaseService
|
||||||
$stockEntry->update([
|
$stockEntry->update([
|
||||||
'amount' => $restStockAmount
|
'amount' => $restStockAmount
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$amount = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
throw new \Exception('Amount to be consumed cannot be > current stock amount (if supplied, at the desired location)');
|
||||||
|
}
|
||||||
|
|
||||||
if (boolval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount')))
|
if (boolval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount')))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user