diff --git a/controllers/StockReportsController.php b/controllers/StockReportsController.php index 7b00ef36..1c78eb34 100644 --- a/controllers/StockReportsController.php +++ b/controllers/StockReportsController.php @@ -6,42 +6,58 @@ class StockReportsController extends BaseController { public function Spendings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - if (isset($request->getQueryParams()['start_date']) and isset($request->getQueryParams()['end_date'])) + if (isset($request->getQueryParams()['start_date']) && isset($request->getQueryParams()['end_date']) && IsIsoDate($request->getQueryParams()['start_date']) && IsIsoDate($request->getQueryParams()['end_date'])) { $startDate = $request->getQueryParams()['start_date']; $endDate = $request->getQueryParams()['end_date']; - $where = "purchased_date >= '$startDate' AND purchased_date <= '$endDate'"; + $where = "pph.purchased_date BETWEEN '$startDate' AND '$endDate'"; } else { - // Default this month - $where = "purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')"; + // Default to this month + $where = "pph.purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')"; } if (isset($request->getQueryParams()['byGroup'])) { $sql = " - SELECT product_group_id as id, product_group as name, sum(quantity * price) as total - FROM product_purchase_history + SELECT + pg.id AS id, + pg.name AS name, + SUM(pph.amount * pph.price) AS total + FROM product_price_history pph + JOIN products p + ON pph.product_id = p.id + JOIN product_groups pg + ON p.product_group_id = pg.id WHERE $where - GROUP BY product_group - ORDER BY product_group + GROUP BY pg.id + ORDER BY pg.NAME COLLATE NOCASE "; } else { if (isset($request->getQueryParams()['product_group']) and $request->getQueryParams()['product_group'] != 'all') { - $where = $where . ' AND product_group_id = ' . $request->getQueryParams()['product_group']; + $where .= ' AND pg.id = ' . $request->getQueryParams()['product_group']; } $sql = " - SELECT product_id as id, product_name as name, product_group_id as group_id, product_group as group_name, sum(quantity * price) as total - FROM product_purchase_history + SELECT + p.id AS id, + p.name AS name, + pg.id AS group_id, + pg.name AS group_name, + SUM(pph.amount * pph.price) AS total + FROM product_price_history pph + JOIN products p + ON pph.product_id = p.id + JOIN product_groups pg + ON p.product_group_id = pg.id WHERE $where - GROUP BY product_name - ORDER BY product_name + GROUP BY p.id + ORDER BY p.NAME COLLATE NOCASE "; } diff --git a/migrations/0216.sql b/migrations/0216.sql index 2935b1cb..dd08cb0d 100644 --- a/migrations/0216.sql +++ b/migrations/0216.sql @@ -1,20 +1,25 @@ -CREATE VIEW product_purchase_history +DROP VIEW product_price_history; +CREATE VIEW product_price_history AS SELECT - 1 AS id, -- Dummy, LessQL needs an id column - p.id AS product_id, - p.name AS product_name, - g.id AS product_group_id, - g.name AS product_group, - s.amount AS quantity, - s.price AS price, - s.purchased_date AS purchased_date -FROM product_groups g -JOIN products p - ON p.product_group_id = g.id -JOIN stock_log s - ON s.product_id = p.id -WHERE s.transaction_type = 'purchase' - AND s.undone = 0 - AND s.price IS NOT NULL -ORDER BY p.name ASC; + sl.product_id AS id, -- Dummy, LessQL needs an id column + sl.product_id, + sl.price, + sl.amount, + sl.purchased_date, + sl.shopping_location_id +FROM stock_log sl +WHERE sl.transaction_type IN ('purchase', 'inventory-correction', 'stock-edit-new') + AND sl.undone = 0 + AND IFNULL(sl.price, 0) > 0 + AND IFNULL(sl.amount, 0) > 0 + AND sl.id NOT IN ( + -- These are edited purchase and inventory-correction rows + SELECT sl_origin.id + FROM stock_log sl_origin + JOIN stock_log sl_edit + ON sl_origin.stock_id = sl_edit.stock_id + AND sl_edit.transaction_type = 'stock-edit-new' + AND sl_edit.id > sl_origin.id + WHERE sl_origin.transaction_type IN ('purchase', 'inventory-correction') + ); diff --git a/views/stockreportspendings.blade.php b/views/stockreportspendings.blade.php index af4df07a..d68e2126 100644 --- a/views/stockreportspendings.blade.php +++ b/views/stockreportspendings.blade.php @@ -76,9 +76,7 @@ id="product-group-filter"> @foreach($productGroups as $productGroup) - @endforeach