diff --git a/controllers/StockController.php b/controllers/StockController.php index 1126b3f0..ddb785e2 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -8,12 +8,9 @@ class StockController extends BaseController { public function Consume(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - $sql = 'select group_concat(barcode) barcodes, product_id from product_barcodes group by product_id'; - $productBarcodes = $this->getDatabaseService()->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); - return $this->renderPage($response, 'consume', [ 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name'), - 'barcodes' => $productBarcodes, + 'barcodes' => $this->getDatabase()->product_barcodes_comma_separated(), 'recipes' => $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name', 'COLLATE NOCASE'), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), @@ -23,12 +20,9 @@ class StockController extends BaseController public function Inventory(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - $sql = 'select group_concat(barcode) barcodes, product_id from product_barcodes group by product_id'; - $productBarcodes = $this->getDatabaseService()->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); - return $this->renderPage($response, 'inventory', [ 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), - 'barcodes' => $productBarcodes, + 'barcodes' => $this->getDatabase()->product_barcodes_comma_separated(), 'shoppinglocations' => $this->getDatabase()->shopping_locations()->orderBy('name', 'COLLATE NOCASE'), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), @@ -229,12 +223,9 @@ class StockController extends BaseController public function Purchase(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - $sql = 'select group_concat(barcode) barcodes, product_id from product_barcodes group by product_id'; - $productBarcodes = $this->getDatabaseService()->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); - return $this->renderPage($response, 'purchase', [ 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), - 'barcodes' => $productBarcodes, + 'barcodes' => $this->getDatabase()->product_barcodes_comma_separated(), 'shoppinglocations' => $this->getDatabase()->shopping_locations()->orderBy('name', 'COLLATE NOCASE'), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), @@ -466,12 +457,9 @@ class StockController extends BaseController public function Transfer(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - $sql = 'select group_concat(barcode) barcodes, product_id from product_barcodes group by product_id'; - $productBarcodes = $this->getDatabaseService()->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_OBJ); - return $this->renderPage($response, 'transfer', [ 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), - 'barcodes' => $productBarcodes, + 'barcodes' => $this->getDatabase()->product_barcodes_comma_separated(), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name', 'COLLATE NOCASE'), 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved() diff --git a/migrations/0123.sql b/migrations/0123.sql index c9a66596..c0f4c9cc 100644 --- a/migrations/0123.sql +++ b/migrations/0123.sql @@ -1,6 +1,18 @@ ALTER TABLE product_barcodes ADD note TEXT; +CREATE VIEW product_barcodes_comma_separated +AS +SELECT + pb.id, -- Dummy, LessQL needs an id column + pb.product_id, + GROUP_CONCAT(pb.barcode) AS barcodes +FROM product_barcodes pb +JOIN products p + ON pb.product_id = p.id +WHERE p.active = 1 +GROUP BY pb.product_id; + CREATE VIEW uihelper_shopping_list AS SELECT @@ -11,7 +23,8 @@ SELECT st.name AS default_shopping_location_name, qu.name AS qu_name, qu.name_plural AS qu_name_plural, - pg.name AS product_group_name + pg.name AS product_group_name, + pbcs.barcodes AS product_barcodes FROM shopping_list sl LEFT JOIN products p ON sl.product_id = p.id @@ -26,4 +39,6 @@ LEFT JOIN shopping_locations st LEFT JOIN quantity_units qu ON sl.qu_id = qu.id LEFT JOIN product_groups pg - ON p.product_group_id = pg.id; + ON p.product_group_id = pg.id +LEFT JOIN product_barcodes_comma_separated pbcs + ON sl.product_id = pbcs.product_id; diff --git a/services/DemoDataGeneratorService.php b/services/DemoDataGeneratorService.php index 23c7f463..559f9e3d 100644 --- a/services/DemoDataGeneratorService.php +++ b/services/DemoDataGeneratorService.php @@ -94,6 +94,10 @@ class DemoDataGeneratorService extends BaseService INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock, product_group_id) VALUES ('{$this->__t_sql('Waffle rolls')}', 4, 3, 3, 1, 1); --26 UPDATE products SET calories = 123 WHERE IFNULL(calories, 0) = 0; + INSERT INTO product_barcodes (product_id, barcode) VALUES (8, '22111968'); + INSERT INTO product_barcodes (product_id, barcode) VALUES (8, '22114358'); + INSERT INTO product_barcodes (product_id, barcode) VALUES (4, '42141099'); + /* Prevent invalid quantity unit assignments */ UPDATE products SET qu_id_stock = (SELECT MIN(id) FROM quantity_units) WHERE id IN (SELECT id FROM products WHERE qu_id_stock NOT IN (SELECT id FROM quantity_units)); UPDATE products SET qu_id_purchase = (SELECT MIN(id) FROM quantity_units) WHERE id IN (SELECT id FROM products WHERE qu_id_purchase NOT IN (SELECT id FROM quantity_units));