mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 17:45:39 +00:00
Exclude self-produced product prices from spending reports (#2596)
* Exclude self-produced product prices from spending reports * Fix SQL query * Code style --------- Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
parent
9e1020b7f8
commit
5b48004449
@ -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';
|
||||
|
24
migrations/0239.sql
Normal file
24
migrations/0239.sql
Normal file
@ -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;
|
Loading…
x
Reference in New Issue
Block a user