accounting modules
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
class PeriodManager
|
||||
{
|
||||
private PDO $pdo;
|
||||
private int $companyId;
|
||||
|
||||
public function __construct(PDO $pdo, int $company_id)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
$this->companyId = $company_id;
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT * FROM ac_period
|
||||
WHERE company_id = :cid
|
||||
ORDER BY period DESC"
|
||||
);
|
||||
$sth->execute([':cid' => $this->companyId]);
|
||||
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function getById(int $id): ?array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT * FROM ac_period WHERE company_id = :cid AND id = :id LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->companyId, ':id' => $id]);
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
public function isOpen(string $period): bool
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT status FROM ac_period
|
||||
WHERE company_id = :cid AND period = :period LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->companyId, ':period' => $period]);
|
||||
$status = $sth->fetchColumn();
|
||||
return $status === 'open';
|
||||
}
|
||||
|
||||
// Get existing period or auto-create it as open
|
||||
public function getOrCreate(string $period): array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT * FROM ac_period WHERE company_id = :cid AND period = :period LIMIT 1"
|
||||
);
|
||||
$sth->execute([':cid' => $this->companyId, ':period' => $period]);
|
||||
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
if ($row) return $row;
|
||||
|
||||
$this->pdo->prepare(
|
||||
"INSERT INTO ac_period (company_id, period, status) VALUES (:cid, :period, 'open')"
|
||||
)->execute([':cid' => $this->companyId, ':period' => $period]);
|
||||
|
||||
return $this->getOrCreate($period);
|
||||
}
|
||||
|
||||
// Create a single period (manual)
|
||||
public function create(string $period): void
|
||||
{
|
||||
$this->pdo->prepare(
|
||||
"INSERT IGNORE INTO ac_period (company_id, period, status) VALUES (:cid, :period, 'open')"
|
||||
)->execute([':cid' => $this->companyId, ':period' => $period]);
|
||||
}
|
||||
|
||||
// Bulk-create all 12 months of a year
|
||||
public function createYear(int $year): void
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"INSERT IGNORE INTO ac_period (company_id, period, status) VALUES (:cid, :period, 'open')"
|
||||
);
|
||||
for ($m = 1; $m <= 12; $m++) {
|
||||
$sth->execute([
|
||||
':cid' => $this->companyId,
|
||||
':period' => sprintf('%04d-%02d', $year, $m),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function open(int $id): void
|
||||
{
|
||||
$this->pdo->prepare(
|
||||
"UPDATE ac_period SET status = 'open', closed_at = NULL
|
||||
WHERE company_id = :cid AND id = :id AND status = 'closed'"
|
||||
)->execute([':cid' => $this->companyId, ':id' => $id]);
|
||||
}
|
||||
|
||||
public function close(int $id): void
|
||||
{
|
||||
$this->pdo->prepare(
|
||||
"UPDATE ac_period SET status = 'closed', closed_at = NOW()
|
||||
WHERE company_id = :cid AND id = :id AND status = 'open'"
|
||||
)->execute([':cid' => $this->companyId, ':id' => $id]);
|
||||
}
|
||||
|
||||
public function lock(int $id): void
|
||||
{
|
||||
$this->pdo->prepare(
|
||||
"UPDATE ac_period SET status = 'locked', locked_at = NOW()
|
||||
WHERE company_id = :cid AND id = :id AND status = 'closed'"
|
||||
)->execute([':cid' => $this->companyId, ':id' => $id]);
|
||||
}
|
||||
|
||||
public function getStats(): array
|
||||
{
|
||||
$sth = $this->pdo->prepare(
|
||||
"SELECT COUNT(*) AS total,
|
||||
SUM(status = 'open') AS open_count,
|
||||
SUM(status = 'closed') AS closed_count,
|
||||
SUM(status = 'locked') AS locked_count
|
||||
FROM ac_period WHERE company_id = :cid"
|
||||
);
|
||||
$sth->execute([':cid' => $this->companyId]);
|
||||
return $sth->fetch(PDO::FETCH_ASSOC) ?: [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user