From 1c350cbfa5ecac49f824b2826d89d005de8243d4 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 4 Nov 2019 22:34:26 +0100 Subject: [PATCH] Added transaction_id and correlation_id to stock_log entries to group them together --- migrations/0095.sql | 6 ++++++ services/StockService.php | 44 +++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/migrations/0095.sql b/migrations/0095.sql index f0cf4d5d..b67063ba 100644 --- a/migrations/0095.sql +++ b/migrations/0095.sql @@ -14,6 +14,12 @@ BEGIN AND location_id IS NULL; END; +ALTER TABLE stock_log +ADD correlation_id TEXT; + +ALTER TABLE stock_log +ADD transaction_id TEXT; + DROP VIEW stock_current_locations; CREATE VIEW stock_current_locations AS diff --git a/services/StockService.php b/services/StockService.php index 9a980156..73d216e4 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -189,7 +189,7 @@ class StockService extends BaseService return FindAllObjectsInArrayByPropertyValue($stockEntries, 'location_id', $locationId); } - public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null) + public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null, &$transactionId = null) { if (!$this->ProductExists($productId)) { @@ -229,6 +229,11 @@ class StockService extends BaseService if ($transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION) { + if ($transactionId === null) + { + $transactionId = uniqid(); + } + $stockId = uniqid(); $logRow = $this->Database->stock_log()->createRow(array( @@ -264,7 +269,7 @@ class StockService extends BaseService } } - public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null) + public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null) { if (!$this->ProductExists($productId)) { @@ -313,6 +318,11 @@ class StockService extends BaseService $potentialStockEntries = FindAllObjectsInArrayByPropertyValue($potentialStockEntries, 'stock_id', $specificStockEntryId); } + if ($transactionId === null) + { + $transactionId = uniqid(); + } + foreach ($potentialStockEntries as $stockEntry) { if ($amount == 0) @@ -333,7 +343,8 @@ class StockService extends BaseService 'transaction_type' => $transactionType, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'recipe_id' => $recipeId + 'recipe_id' => $recipeId, + 'transaction_id' => $transactionId )); $logRow->save(); @@ -356,7 +367,8 @@ class StockService extends BaseService 'transaction_type' => $transactionType, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'recipe_id' => $recipeId + 'recipe_id' => $recipeId, + 'transaction_id' => $transactionId )); $logRow->save(); @@ -376,7 +388,7 @@ class StockService extends BaseService } } - public function TransferProduct(int $productId, float $amount, int $locationIdFrom, int $locationIdTo, $specificStockEntryId = 'default') + public function TransferProduct(int $productId, float $amount, int $locationIdFrom, int $locationIdTo, $specificStockEntryId = 'default', &$transactionId = null) { if (!$this->ProductExists($productId)) { @@ -423,6 +435,11 @@ class StockService extends BaseService $potentialStockEntriesAtFromLocation = FindAllObjectsInArrayByPropertyValue($potentialStockEntriesAtFromLocation, 'stock_id', $specificStockEntryId); } + if ($transactionId === null) + { + $transactionId = uniqid(); + } + foreach ($potentialStockEntriesAtFromLocation as $stockEntry) { if ($amount == 0) @@ -430,6 +447,7 @@ class StockService extends BaseService break; } + $correlationId = uniqid(); if ($amount >= $stockEntry->amount) // Take the whole stock entry { $logRowForLocationFrom = $this->Database->stock_log()->createRow(array( @@ -441,7 +459,9 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_TRANSFER_FROM, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'location_id' => $stockEntry->location_id + 'location_id' => $stockEntry->location_id, + 'correlation_id' => $correlationId, + 'transaction_Id' => $transactionId )); $logRowForLocationFrom->save(); @@ -454,7 +474,9 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_TRANSFER_TO, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'location_id' => $locationIdTo + 'location_id' => $locationIdTo, + 'correlation_id' => $correlationId, + 'transaction_Id' => $transactionId )); $logRowForLocationTo->save(); @@ -477,7 +499,9 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_TRANSFER_FROM, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'location_id' => $stockEntry->location_id + 'location_id' => $stockEntry->location_id, + 'correlation_id' => $correlationId, + 'transaction_Id' => $transactionId )); $logRowForLocationFrom->save(); @@ -490,7 +514,9 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_TRANSFER_TO, 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, - 'location_id' => $locationIdTo + 'location_id' => $locationIdTo, + 'correlation_id' => $correlationId, + 'transaction_Id' => $transactionId )); $logRowForLocationTo->save();