stock aggregate table
This commit is contained in:
@@ -1831,7 +1831,7 @@ class WarehouseManager {
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Adjust the running balance for a (warehouse, product_sku) pair in warehouse_balance.
|
||||
* Adjust the running balance for a (warehouse, product_sku) pair in etl_stock_summary.
|
||||
*
|
||||
* Uses an INSERT ... ON DUPLICATE KEY UPDATE to atomically upsert the balance row,
|
||||
* partitioned by month (YYYY-MM) for efficient monthly reporting queries.
|
||||
@@ -1851,19 +1851,21 @@ class WarehouseManager {
|
||||
*/
|
||||
public function adjustBalance(
|
||||
$type, $warehouse_id, $product_sku, $old_qty, $new_qty,
|
||||
int $stock_id = 0, string $source = '', int $source_id = 0
|
||||
int $stock_id = 0, string $source = '', int $source_id = 0,
|
||||
string $stock_date = ''
|
||||
): void {
|
||||
$column = $type === 'in' ? 'total_in' : 'total_out';
|
||||
$delta = $new_qty - $old_qty;
|
||||
$month = date('Y-m');
|
||||
$month = $stock_date ? date('Y-m', strtotime($stock_date)) : date('Y-m');
|
||||
|
||||
$this->pdo->prepare(
|
||||
"INSERT INTO warehouse_balance
|
||||
(company_id, warehouse_id, product_sku, month, `{$column}`)
|
||||
"INSERT INTO etl_stock_summary
|
||||
(company_id, warehouse_id, product_sku, month, `{$column}`, source_updated_at)
|
||||
VALUES
|
||||
(:company_id, :warehouse_id, :product_sku, :month, :delta)
|
||||
(:company_id, :warehouse_id, :product_sku, :month, :delta, NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
`{$column}` = `{$column}` + VALUES(`{$column}`)"
|
||||
`{$column}` = `{$column}` + VALUES(`{$column}`),
|
||||
source_updated_at = NOW()"
|
||||
)->execute([
|
||||
':company_id' => $this->company_id,
|
||||
':warehouse_id' => $warehouse_id,
|
||||
@@ -2173,7 +2175,7 @@ class WarehouseManager {
|
||||
);
|
||||
|
||||
if ((int)$row['status'] === 1) {
|
||||
$this->adjustBalance('in', $warehouse_id, $product_sku, (int)$row['in'], 0);
|
||||
$this->adjustBalance('in', $warehouse_id, $product_sku, (int)$row['in'], 0, 0, '', 0, $row['date'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2235,7 +2237,7 @@ class WarehouseManager {
|
||||
[(int)$row['ref_id']],
|
||||
$row['date']
|
||||
);
|
||||
$this->adjustBalance('out', $warehouse_id, $product_sku, (int)$row['out'], 0);
|
||||
$this->adjustBalance('out', $warehouse_id, $product_sku, (int)$row['out'], 0, 0, '', 0, $row['date'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2334,8 +2336,8 @@ class WarehouseManager {
|
||||
|
||||
// Reverse balances on both sides
|
||||
$quantity = (int)$from_row['out'];
|
||||
$this->adjustBalance('out', $from_warehouse_id, $product_sku, $quantity, 0);
|
||||
$this->adjustBalance('in', $to_warehouse, $product_sku, $quantity, 0);
|
||||
$this->adjustBalance('out', $from_warehouse_id, $product_sku, $quantity, 0, 0, '', 0, $from_row['date'] ?? '');
|
||||
$this->adjustBalance('in', $to_warehouse, $product_sku, $quantity, 0, 0, '', 0, $from_row['date'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user