Reuse the existing product_price_history view

This commit is contained in:
Bernd Bestel 2023-04-01 15:39:44 +02:00
parent 132dfb8e6b
commit 6cfee78b95
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 53 additions and 34 deletions

View File

@ -6,42 +6,58 @@ class StockReportsController extends BaseController
{ {
public function Spendings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) 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']; $startDate = $request->getQueryParams()['start_date'];
$endDate = $request->getQueryParams()['end_date']; $endDate = $request->getQueryParams()['end_date'];
$where = "purchased_date >= '$startDate' AND purchased_date <= '$endDate'"; $where = "pph.purchased_date BETWEEN '$startDate' AND '$endDate'";
} }
else else
{ {
// Default this month // Default to this month
$where = "purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')"; $where = "pph.purchased_date >= DATE(DATE('now', 'localtime'), 'start of month')";
} }
if (isset($request->getQueryParams()['byGroup'])) if (isset($request->getQueryParams()['byGroup']))
{ {
$sql = " $sql = "
SELECT product_group_id as id, product_group as name, sum(quantity * price) as total SELECT
FROM product_purchase_history 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 WHERE $where
GROUP BY product_group GROUP BY pg.id
ORDER BY product_group ORDER BY pg.NAME COLLATE NOCASE
"; ";
} }
else else
{ {
if (isset($request->getQueryParams()['product_group']) and $request->getQueryParams()['product_group'] != 'all') 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 = " $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 SELECT
FROM product_purchase_history 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 WHERE $where
GROUP BY product_name GROUP BY p.id
ORDER BY product_name ORDER BY p.NAME COLLATE NOCASE
"; ";
} }

View File

@ -1,20 +1,25 @@
CREATE VIEW product_purchase_history DROP VIEW product_price_history;
CREATE VIEW product_price_history
AS AS
SELECT SELECT
1 AS id, -- Dummy, LessQL needs an id column sl.product_id AS id, -- Dummy, LessQL needs an id column
p.id AS product_id, sl.product_id,
p.name AS product_name, sl.price,
g.id AS product_group_id, sl.amount,
g.name AS product_group, sl.purchased_date,
s.amount AS quantity, sl.shopping_location_id
s.price AS price, FROM stock_log sl
s.purchased_date AS purchased_date WHERE sl.transaction_type IN ('purchase', 'inventory-correction', 'stock-edit-new')
FROM product_groups g AND sl.undone = 0
JOIN products p AND IFNULL(sl.price, 0) > 0
ON p.product_group_id = g.id AND IFNULL(sl.amount, 0) > 0
JOIN stock_log s AND sl.id NOT IN (
ON s.product_id = p.id -- These are edited purchase and inventory-correction rows
WHERE s.transaction_type = 'purchase' SELECT sl_origin.id
AND s.undone = 0 FROM stock_log sl_origin
AND s.price IS NOT NULL JOIN stock_log sl_edit
ORDER BY p.name ASC; 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')
);

View File

@ -76,9 +76,7 @@
id="product-group-filter"> id="product-group-filter">
<option value="all">{{ $__t('All') }}</option> <option value="all">{{ $__t('All') }}</option>
@foreach($productGroups as $productGroup) @foreach($productGroups as $productGroup)
<option @if($selectedGroup==$productGroup->id) <option @if($productGroup->id == $selectedGroup) selected="selected" @endif
selected="selected"
@endif
value="{{ $productGroup->id }}">{{ $productGroup->name }}</option> value="{{ $productGroup->id }}">{{ $productGroup->name }}</option>
@endforeach @endforeach
</select> </select>