diff --git a/controllers/StockController.php b/controllers/StockController.php index e7d56684..016bb41b 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -33,10 +33,8 @@ class StockController extends BaseController public function Journal(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { return $this->renderPage($response, 'stockjournal', [ - 'stockLog' => $this->getDatabase()->stock_log()->orderBy('row_created_timestamp', 'DESC'), - 'locations' => $this->getDatabase()->locations()->orderBy('name'), + 'stockLog' => $this->getDatabase()->uihelper_stock_journal()->orderBy('row_created_timestamp', 'DESC'), 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name'), - 'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name') ]); } diff --git a/migrations/0115.sql b/migrations/0115.sql new file mode 100644 index 00000000..9eda39a5 --- /dev/null +++ b/migrations/0115.sql @@ -0,0 +1,74 @@ +ALTER TABLE stock_log + RENAME TO stock_log_old; + +CREATE TABLE stock_log( + id INTEGER not null primary key autoincrement unique, + product_id INTEGER not null, + amount DECIMAL(15, 2) not null, + best_before_date DATE, + purchased_date DATE, + used_date DATE, + spoiled INTEGER default 0 not null, + stock_id TEXT not null, + transaction_type TEXT not null, + price DECIMAL(15, 2), + undone TINYINT default 0 not null CHECK(undone IN (0, 1)), + undone_timestamp DATETIME, + opened_date DATETIME, + row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), + location_id INTEGER, + recipe_id INTEGER, + correlation_id TEXT, + transaction_id TEXT, + stock_row_id INTEGER, + shopping_location_id INTEGER, + qu_factor_purchase_to_stock REAL default 1.0 not null, + user_id INTEGER NOT NULL + ); + + +INSERT INTO stock_log +(product_id, amount, best_before_date, purchased_date, used_date, spoiled, stock_id, transaction_type, price, undone, + undone_timestamp, opened_date, row_created_timestamp, user_id) +SELECT product_id, + amount, + best_before_date, + purchased_date, + used_date, + spoiled, + stock_id, + transaction_type, + price, + undone, + undone_timestamp, + opened_date, + row_created_timestamp, + (SELECT id FROM users WHERE username = 'admin') +FROM stock_log_old; + +DROP TABLE stock_log_old; + + +CREATE VIEW uihelper_stock_journal +AS +SELECT stock_log.id, + stock_log.row_created_timestamp, + stock_log.correlation_id, + stock_log.undone, + stock_log.undone_timestamp, + stock_log.row_created_timestamp, + stock_log.transaction_type, + stock_log.spoiled, + stock_log.amount, + stock_log.location_id, + l.name AS location_name, + p.name AS product_name, + qu.name AS qu_name, + qu.name_plural AS qu_name_plural, + u.display_name AS user_display_name + +FROM stock_log + JOIN users_dto u on stock_log.user_id = u.id + JOIN products p on stock_log.product_id = p.id + JOIN locations l on p.location_id = l.id + JOIN quantity_units qu ON p.qu_id_stock = qu.id; diff --git a/services/StockService.php b/services/StockService.php index 3a6ca876..768c3e92 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -112,7 +112,8 @@ class StockService extends BaseService 'location_id' => $locationId, 'transaction_id' => $transactionId, 'shopping_location_id' => $shoppingLocationId, - 'qu_factor_purchase_to_stock' => $quFactorPurchaseToStock + 'qu_factor_purchase_to_stock' => $quFactorPurchaseToStock, + 'user_id' => GROCY_USER_ID ]); $logRow->save(); @@ -259,7 +260,8 @@ class StockService extends BaseService 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, 'recipe_id' => $recipeId, - 'transaction_id' => $transactionId + 'transaction_id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRow->save(); @@ -283,7 +285,8 @@ class StockService extends BaseService 'price' => $stockEntry->price, 'opened_date' => $stockEntry->opened_date, 'recipe_id' => $recipeId, - 'transaction_id' => $transactionId + 'transaction_id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRow->save(); @@ -328,7 +331,8 @@ class StockService extends BaseService 'qu_factor_purchase_to_stock' => $stockRow->qu_factor_purchase_to_stock, 'correlation_id' => $correlationId, 'transaction_id' => $transactionId, - 'stock_row_id' => $stockRow->id + 'stock_row_id' => $stockRow->id, + 'user_id' => GROCY_USER_ID ]); $logOldRowForStockUpdate->save(); @@ -369,7 +373,8 @@ class StockService extends BaseService 'qu_factor_purchase_to_stock' => $stockRow->qu_factor_purchase_to_stock, 'correlation_id' => $correlationId, 'transaction_id' => $transactionId, - 'stock_row_id' => $stockRow->id + 'stock_row_id' => $stockRow->id, + 'user_id' => GROCY_USER_ID ]); $logNewRowForStockUpdate->save(); @@ -738,7 +743,8 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_PRODUCT_OPENED, 'price' => $stockEntry->price, 'opened_date' => date('Y-m-d'), - 'transaction_id' => $transactionId + 'transaction_id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRow->save(); @@ -777,7 +783,8 @@ class StockService extends BaseService 'transaction_type' => self::TRANSACTION_TYPE_PRODUCT_OPENED, 'price' => $stockEntry->price, 'opened_date' => date('Y-m-d'), - 'transaction_id' => $transactionId + 'transaction_id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRow->save(); @@ -914,7 +921,8 @@ class StockService extends BaseService 'opened_date' => $stockEntry->opened_date, 'location_id' => $stockEntry->location_id, 'correlation_id' => $correlationId, - 'transaction_Id' => $transactionId + 'transaction_Id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRowForLocationFrom->save(); @@ -930,7 +938,8 @@ class StockService extends BaseService 'opened_date' => $stockEntry->opened_date, 'location_id' => $locationIdTo, 'correlation_id' => $correlationId, - 'transaction_Id' => $transactionId + 'transaction_Id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRowForLocationTo->save(); @@ -957,7 +966,8 @@ class StockService extends BaseService 'opened_date' => $stockEntry->opened_date, 'location_id' => $stockEntry->location_id, 'correlation_id' => $correlationId, - 'transaction_Id' => $transactionId + 'transaction_Id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRowForLocationFrom->save(); @@ -973,7 +983,8 @@ class StockService extends BaseService 'opened_date' => $stockEntry->opened_date, 'location_id' => $locationIdTo, 'correlation_id' => $correlationId, - 'transaction_Id' => $transactionId + 'transaction_Id' => $transactionId, + 'user_id' => GROCY_USER_ID ]); $logRowForLocationTo->save(); diff --git a/views/stockjournal.blade.php b/views/stockjournal.blade.php index bb6fd994..b2d0a913 100644 --- a/views/stockjournal.blade.php +++ b/views/stockjournal.blade.php @@ -52,6 +52,7 @@ {{ $__t('Booking time') }} {{ $__t('Booking type') }} {{ $__t('Location') }} + {{ $__t('Done by') }} @@ -70,7 +71,7 @@ - {{ FindObjectInArrayByPropertyValue($products, 'id', $stockLogEntry->product_id)->name }} + {{ $stockLogEntry->product_name }} @if($stockLogEntry->undone == 1)
{{ $__t('Undone on') . ' ' . $stockLogEntry->undone_timestamp }} @@ -79,7 +80,7 @@ @endif - {{ $stockLogEntry->amount }} {{ $__n($stockLogEntry->amount, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $stockLogEntry->product_id)->qu_id_stock)->name, FindObjectInArrayByPropertyValue($quantityunits, 'id', FindObjectInArrayByPropertyValue($products, 'id', $stockLogEntry->product_id)->qu_id_stock)->name_plural) }} + {{ $stockLogEntry->amount }} {{ $__n($stockLogEntry->amount, $stockLogEntry->qu_name, $stockLogEntry->qu_name_plural) }} {{ $stockLogEntry->row_created_timestamp }} @@ -93,7 +94,10 @@ @endif - {{ FindObjectInArrayByPropertyValue($locations, 'id', $stockLogEntry->location_id)->name }} + {{ $stockLogEntry->location_name }} + + + {{ $stockLogEntry->user_display_name }} @endforeach