accounting workflows
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
class StockSourceManager
|
||||
{
|
||||
private PDO $pdo;
|
||||
private int $companyId;
|
||||
|
||||
public function __construct(PDO $pdo, int $company_id)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
$this->companyId = $company_id;
|
||||
}
|
||||
|
||||
public function getBySource(string $source, int $source_id): array
|
||||
{
|
||||
$source = trim($source);
|
||||
if ($source === '' || $source_id <= 0) {
|
||||
throw new Exception('source and source_id are required.');
|
||||
}
|
||||
|
||||
$warehouses = $this->getWarehouseNames();
|
||||
$rows = [];
|
||||
|
||||
foreach ($this->getStockTables() as $table) {
|
||||
if (!preg_match('/^td_stock_(\d+)$/', $table, $match)) continue;
|
||||
|
||||
$warehouse_id = (int)$match[1];
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT s.id, s.type, s.product_sku, s.`in`, s.`out`,
|
||||
s.zone, s.aisle, s.rack, s.lot_number, s.serial_number,
|
||||
s.status, s.date, s.price,
|
||||
p.product_name
|
||||
FROM `{$table}` s
|
||||
LEFT JOIN md_product p
|
||||
ON p.company_id = s.company_id
|
||||
AND p.sku = s.product_sku
|
||||
WHERE s.company_id = :company_id
|
||||
AND s.source = :source
|
||||
AND s.source_id = :source_id
|
||||
ORDER BY s.date ASC"
|
||||
);
|
||||
$sth->execute([
|
||||
':company_id' => $this->companyId,
|
||||
':source' => $source,
|
||||
':source_id' => $source_id,
|
||||
]);
|
||||
|
||||
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
$row['warehouse_id'] = $warehouse_id;
|
||||
$row['warehouse_name'] = $warehouses[$warehouse_id] ?? "WH {$warehouse_id}";
|
||||
$row['quantity'] = $row['type'] === 'out' ? (float)$row['out'] : (float)$row['in'];
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function getWarehouseNames(): array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT id, warehouse_name
|
||||
FROM md_warehouse
|
||||
WHERE company_id = :company_id"
|
||||
);
|
||||
$sth->execute([':company_id' => $this->companyId]);
|
||||
|
||||
$warehouses = [];
|
||||
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $warehouse) {
|
||||
$warehouses[(int)$warehouse['id']] = $warehouse['warehouse_name'];
|
||||
}
|
||||
return $warehouses;
|
||||
}
|
||||
|
||||
private function getStockTables(): array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name LIKE 'td_stock_%'"
|
||||
);
|
||||
$sth->execute();
|
||||
return $sth->fetchAll(PDO::FETCH_COLUMN);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user