156 lines
5.4 KiB
PHP
156 lines
5.4 KiB
PHP
<?php
|
|
class ChartOfAccounts
|
|
{
|
|
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 md_account
|
|
WHERE company_id = :cid
|
|
ORDER BY account_code ASC"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId]);
|
|
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function getByCode(string $account_code): ?array
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT * FROM md_account
|
|
WHERE company_id = :cid AND account_code = :code
|
|
LIMIT 1"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId, ':code' => $account_code]);
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
|
return $row ?: null;
|
|
}
|
|
|
|
public function getByType(string $account_type): array
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT * FROM md_account
|
|
WHERE company_id = :cid AND account_type = :type AND status = 1
|
|
ORDER BY account_code ASC"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId, ':type' => $account_type]);
|
|
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function getById(int $id): ?array
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT * FROM md_account
|
|
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 create(array $data): int
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"INSERT INTO md_account
|
|
(company_id, account_code, account_name, account_type, account_category, parent_code, is_posting, status)
|
|
VALUES
|
|
(:cid, :code, :name, :type, :category, :parent, :posting, :status)"
|
|
);
|
|
$sth->execute([
|
|
':cid' => $this->companyId,
|
|
':code' => trim($data['account_code']),
|
|
':name' => trim($data['account_name']),
|
|
':type' => $data['account_type'],
|
|
':category' => !empty($data['account_category']) ? trim($data['account_category']) : '',
|
|
':parent' => !empty($data['parent_code']) ? trim($data['parent_code']) : null,
|
|
':posting' => (int)($data['is_posting'] ?? 1),
|
|
':status' => (int)($data['status'] ?? 1),
|
|
]);
|
|
return (int)$this->pdo->lastInsertId();
|
|
}
|
|
|
|
public function update(int $id, array $data): void
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"UPDATE md_account SET
|
|
account_code = :code,
|
|
account_name = :name,
|
|
account_type = :type,
|
|
account_category = :category,
|
|
parent_code = :parent,
|
|
is_posting = :posting,
|
|
status = :status
|
|
WHERE company_id = :cid AND id = :id"
|
|
);
|
|
$sth->execute([
|
|
':cid' => $this->companyId,
|
|
':id' => $id,
|
|
':code' => trim($data['account_code']),
|
|
':name' => trim($data['account_name']),
|
|
':type' => $data['account_type'],
|
|
':category' => !empty($data['account_category']) ? trim($data['account_category']) : '',
|
|
':parent' => !empty($data['parent_code']) ? trim($data['parent_code']) : null,
|
|
':posting' => (int)($data['is_posting'] ?? 1),
|
|
':status' => (int)($data['status'] ?? 1),
|
|
]);
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"UPDATE md_account SET status = 0
|
|
WHERE company_id = :cid AND id = :id"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId, ':id' => $id]);
|
|
}
|
|
|
|
public function isPostingAccount(string $account_code): bool
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT is_posting FROM md_account
|
|
WHERE company_id = :cid AND account_code = :code AND status = 1
|
|
LIMIT 1"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId, ':code' => $account_code]);
|
|
return (bool)$sth->fetchColumn();
|
|
}
|
|
|
|
public function getStats(): array
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT
|
|
COUNT(*) AS total,
|
|
SUM(status = 1) AS active,
|
|
SUM(status = 0) AS inactive,
|
|
SUM(account_type = 'asset') AS asset,
|
|
SUM(account_type = 'liability') AS liability,
|
|
SUM(account_type = 'equity') AS equity,
|
|
SUM(account_type = 'revenue') AS revenue,
|
|
SUM(account_type = 'expense') AS expense
|
|
FROM md_account
|
|
WHERE company_id = :cid"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId]);
|
|
return $sth->fetch(PDO::FETCH_ASSOC) ?: [];
|
|
}
|
|
|
|
public function getByCategory(string $category): array
|
|
{
|
|
$sth = $this->pdo->prepare(
|
|
"SELECT * FROM md_account
|
|
WHERE company_id = :cid AND account_category = :cat AND status = 1
|
|
ORDER BY account_code ASC"
|
|
);
|
|
$sth->execute([':cid' => $this->companyId, ':cat' => $category]);
|
|
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
}
|