diff --git a/controllers/StockReportsController.php b/controllers/StockReportsController.php index ba0b7aed..96a327d6 100644 --- a/controllers/StockReportsController.php +++ b/controllers/StockReportsController.php @@ -9,16 +9,18 @@ class StockReportsController extends BaseController { public function Spendings(Request $request, Response $response, array $args) { + $where = "pph.transaction_type != 'self-production'"; + 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 = "pph.purchased_date BETWEEN '$startDate' AND '$endDate'"; + $where .= " AND pph.purchased_date BETWEEN '$startDate' AND '$endDate'"; } else { // Default to this month - $where = "pph.purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')"; + $where .= " AND pph.purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')"; } $groupBy = 'product'; diff --git a/migrations/0239.sql b/migrations/0239.sql new file mode 100644 index 00000000..ab94b48b --- /dev/null +++ b/migrations/0239.sql @@ -0,0 +1,24 @@ +DROP VIEW products_price_history; +CREATE VIEW products_price_history +AS +SELECT + sl.product_id AS id, -- Dummy, LessQL needs an id column + sl.product_id, + sl.price, + IFNULL(sl.edited_origin_amount, sl.amount) AS amount, + sl.purchased_date, + sl.shopping_location_id, + sl.transaction_type +FROM ( + SELECT sl.*, CASE WHEN sl.transaction_type = 'stock-edit-new' THEN see.edited_origin_amount END AS edited_origin_amount + FROM stock_log sl + LEFT JOIN stock_edited_entries see + ON sl.stock_id = see.stock_id +) sl +WHERE sl.undone = 0 + AND ( + (sl.transaction_type IN ('purchase', 'inventory-correction', 'self-production') AND sl.stock_id NOT IN (SELECT stock_id FROM stock_edited_entries)) -- Unedited origin entries + OR (sl.transaction_type = 'stock-edit-new' AND sl.id IN (SELECT stock_log_id_of_newest_edited_entry FROM stock_edited_entries)) -- Edited origin entries => take the newest "stock-edit-new" one + ) + AND IFNULL(sl.price, 0) > 0 + AND IFNULL(sl.amount, 0) > 0;