Fix occurrence MCP schema and expose product sales
This commit is contained in:
1 parent
d871b94c52
commit
b8ba2db7a5
4 files changed
+51
-4
No files matched your search
@@ -1040,6 +1040,45 @@ function market_management_get_occurrence(PDO $pdo, int $occurrenceId, bool $inc
|
||||
$occurrenceSalesCount += (int) $salesRow['sales_count'];
|
||||
}
|
||||
unset($day);
|
||||
$salesItemsStmt = $pdo->prepare(
|
||||
'SELECT so.market_occurrence_day_id AS day_id,
|
||||
d.date,
|
||||
p.id AS product_id,
|
||||
p.sku,
|
||||
p.name AS product_name,
|
||||
SUM(GREATEST(sol.qty - sol.qty_cancelled, 0) * sic.qty_per_item) AS quantity,
|
||||
SUM(CASE WHEN sol.line_status = \'cancelled\' OR sol.line_total IS NULL THEN 0
|
||||
ELSE sol.line_total * GREATEST(sol.qty - sol.qty_cancelled, 0) / NULLIF(sol.qty, 0) END) AS gross_revenue
|
||||
FROM sales_order so
|
||||
INNER JOIN sales_order_line sol ON sol.sales_order_id = so.id
|
||||
INNER JOIN sellable_item_component sic ON sic.sellable_item_id = sol.sellable_item_id
|
||||
INNER JOIN product p ON p.id = sic.product_id
|
||||
INNER JOIN market_occurrence_day d ON d.id = so.market_occurrence_day_id
|
||||
WHERE so.market_occurrence_id = :occurrence_id
|
||||
AND so.market_occurrence_day_id IS NOT NULL
|
||||
AND so.order_source = \'direct\'
|
||||
AND so.order_status <> \'cancelled\'
|
||||
GROUP BY so.market_occurrence_day_id, d.date, p.id, p.sku, p.name
|
||||
ORDER BY d.date ASC, p.sku ASC, p.id ASC'
|
||||
);
|
||||
$salesItemsStmt->execute([':occurrence_id' => $occurrenceId]);
|
||||
$occurrence['sales_items'] = array_map(
|
||||
static function (array $item): array {
|
||||
$quantity = (float) $item['quantity'];
|
||||
$grossRevenue = (float) $item['gross_revenue'];
|
||||
return [
|
||||
'day_id' => (int) $item['day_id'],
|
||||
'date' => (string) $item['date'],
|
||||
'product_id' => (int) $item['product_id'],
|
||||
'sku' => (string) $item['sku'],
|
||||
'product_name' => (string) $item['product_name'],
|
||||
'quantity' => $quantity,
|
||||
'gross_revenue' => $grossRevenue,
|
||||
'unit_price' => $quantity > 0 ? $grossRevenue / $quantity : null,
|
||||
];
|
||||
},
|
||||
$salesItemsStmt->fetchAll()
|
||||
);
|
||||
$occurrence['sales_summary'] = $occurrenceSalesCount === 0
|
||||
? [
|
||||
'sales_count' => 0,
|
||||
|
||||
Reference in new issue
Block a user