grocy/migrations/0216.sql
Travis Raup 340832c361
Feature: Stock Purchase Metrics (#2135)
* Feature: Stock Purchase Metrics

* chart update

* Refactor to chartjs

* More suggestion edits

- locale in javascript
- global translations
- commit migrations sql file

* Rename 0215.sql to 0216.sql

Fixed merge conflict

* Fixed merge conflict

* Applied code style

* Added missing demo data translations

* Removed unused package "canvasjs"

* Don't include daterangepicker globally when only needed on a single page / fixed view section imports

* Rename this to "Spendings" / name it more generically "Stock reports"

* Reuse the existing product_price_history view

* Final cleanup

* Whitespace fix

---------

Co-authored-by: Travis Raup <travis.raup@platform.sh>
Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2023-04-01 17:05:41 +02:00

26 lines
770 B
SQL

DROP VIEW product_price_history;
CREATE VIEW product_price_history
AS
SELECT
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')
);