gl aggregate table (ETL), cronjob by NODEJS

This commit is contained in:
Thanakorn S
2026-05-23 10:52:27 +07:00
parent 5a3bfa3435
commit 5780183b82
15 changed files with 1048 additions and 230 deletions
+18 -15
View File
@@ -71,7 +71,7 @@ class GlManager
':updated_at' => $now,
]);
$this->insertLines((int)$this->pdo->lastInsertId(), $lines);
$this->upsertEtl($lines, $period);
$this->upsertEtl($lines, $period, 1, $now);
}
public function replaceManual(int $gl_id, string $reference, string $description, string $journal_date, string $period, array $lines): void
@@ -126,8 +126,8 @@ class GlManager
]);
$this->insertLines($gl_id, $lines);
$this->upsertEtl($old_lines, $old_period, -1);
$this->upsertEtl($lines, $period);
$this->upsertEtl($old_lines, $old_period, -1, $now);
$this->upsertEtl($lines, $period, 1, $now);
}
public function postManual(string $reference, string $description, string $journal_date, string $period, array $lines): int
@@ -154,7 +154,7 @@ class GlManager
]);
$gl_id = (int)$this->pdo->lastInsertId();
$this->insertLines($gl_id, $lines);
$this->upsertEtl($lines, $period);
$this->upsertEtl($lines, $period, 1, $now);
return $gl_id;
}
@@ -163,7 +163,7 @@ class GlManager
$now = date('Y-m-d H:i:s');
$sth = $this->pdo->prepare(
"SELECT id, current_version, formula_id, history, journal_date
"SELECT id, current_version, formula_id, history, journal_date, period
FROM td_gl
WHERE company_id = :cid
AND source_type = :source_type
@@ -220,8 +220,8 @@ class GlManager
]);
$this->insertLines($gl_id, $lines);
$this->upsertEtl($old_lines, $old_period, -1);
$this->upsertEtl($lines, $period);
$this->upsertEtl($old_lines, $old_period, -1, $now);
$this->upsertEtl($lines, $period, 1, $now);
}
public function delete(string $source_type, int $source_id): void
@@ -278,7 +278,7 @@ class GlManager
]);
$reversal_gl_id = (int)$this->pdo->lastInsertId();
$this->insertLines($reversal_gl_id, $reversal_lines);
$this->upsertEtl($reversal_lines, $gl['period']);
$this->upsertEtl($reversal_lines, $gl['period'], 1, $now);
}
// Mark original as voided — keeps audit trail; getBySource() won't match 'voided' source_type
@@ -342,20 +342,22 @@ class GlManager
}
// sign=1 to add, sign=-1 to subtract (used when replacing old lines)
private function upsertEtl(array $lines, string $period, int $sign = 1): void
private function upsertEtl(array $lines, string $period, int $sign = 1, string $now = ''): void
{
if (empty($lines) || empty($period)) return;
if (!$now) $now = date('Y-m-d H:i:s');
$sth = $this->pdo->prepare(
"INSERT INTO etl_gl_summary
(company_id, acc_code, period, debit, credit, count, updated_at)
(company_id, acc_code, period, debit, credit, count, source_updated_at, updated_at)
VALUES
(:cid, :acc, :period, :debit, :credit, :cnt, NOW())
(:cid, :acc, :period, :debit, :credit, :cnt, :src_ts, :src_ts)
ON DUPLICATE KEY UPDATE
debit = debit + VALUES(debit),
credit = credit + VALUES(credit),
count = count + VALUES(count),
updated_at = NOW()"
debit = debit + VALUES(debit),
credit = credit + VALUES(credit),
count = count + VALUES(count),
source_updated_at = GREATEST(COALESCE(source_updated_at, VALUES(source_updated_at)), VALUES(source_updated_at)),
updated_at = VALUES(updated_at)"
);
foreach ($lines as $line) {
$sth->execute([
@@ -365,6 +367,7 @@ class GlManager
':debit' => (float)($line['debit'] ?? 0) * $sign,
':credit' => (float)($line['credit'] ?? 0) * $sign,
':cnt' => $sign,
':src_ts' => $now,
]);
}
}