NODE JS introduction: socket polling

This commit is contained in:
Thanakorn S
2026-05-22 17:01:59 +07:00
parent 648efee991
commit 5a3bfa3435
17 changed files with 1520 additions and 2135 deletions
+44 -2
View File
@@ -71,6 +71,7 @@ class GlManager
':updated_at' => $now,
]);
$this->insertLines((int)$this->pdo->lastInsertId(), $lines);
$this->upsertEtl($lines, $period);
}
public function replaceManual(int $gl_id, string $reference, string $description, string $journal_date, string $period, array $lines): void
@@ -99,6 +100,9 @@ class GlManager
'lines' => $this->getLines($gl_id),
];
$old_lines = $history[count($history) - 1]['lines']; // captured just above
$old_period = $gl['period'];
$this->pdo->prepare(
"DELETE FROM td_gl_item WHERE company_id = :cid AND gl_id = :gl_id"
)->execute([':cid' => $this->companyId, ':gl_id' => $gl_id]);
@@ -122,6 +126,8 @@ class GlManager
]);
$this->insertLines($gl_id, $lines);
$this->upsertEtl($old_lines, $old_period, -1);
$this->upsertEtl($lines, $period);
}
public function postManual(string $reference, string $description, string $journal_date, string $period, array $lines): int
@@ -148,6 +154,7 @@ class GlManager
]);
$gl_id = (int)$this->pdo->lastInsertId();
$this->insertLines($gl_id, $lines);
$this->upsertEtl($lines, $period);
return $gl_id;
}
@@ -179,11 +186,14 @@ class GlManager
$history = json_decode($gl['history'], true) ?: [];
// Snapshot current lines into history
$old_lines = $this->getLines($gl_id);
$old_period = $gl['period'];
$history[] = [
'version' => $version,
'formula_id' => (int)$gl['formula_id'],
'replaced_at' => $now,
'lines' => $this->getLines($gl_id),
'lines' => $old_lines,
];
$this->pdo->prepare(
@@ -210,6 +220,8 @@ class GlManager
]);
$this->insertLines($gl_id, $lines);
$this->upsertEtl($old_lines, $old_period, -1);
$this->upsertEtl($lines, $period);
}
public function delete(string $source_type, int $source_id): void
@@ -264,7 +276,9 @@ class GlManager
':created_at' => $now,
':updated_at' => $now,
]);
$this->insertLines((int)$this->pdo->lastInsertId(), $reversal_lines);
$reversal_gl_id = (int)$this->pdo->lastInsertId();
$this->insertLines($reversal_gl_id, $reversal_lines);
$this->upsertEtl($reversal_lines, $gl['period']);
}
// Mark original as voided — keeps audit trail; getBySource() won't match 'voided' source_type
@@ -326,4 +340,32 @@ 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
{
if (empty($lines) || empty($period)) return;
$sth = $this->pdo->prepare(
"INSERT INTO etl_gl_summary
(company_id, acc_code, period, debit, credit, count, updated_at)
VALUES
(:cid, :acc, :period, :debit, :credit, :cnt, NOW())
ON DUPLICATE KEY UPDATE
debit = debit + VALUES(debit),
credit = credit + VALUES(credit),
count = count + VALUES(count),
updated_at = NOW()"
);
foreach ($lines as $line) {
$sth->execute([
':cid' => $this->companyId,
':acc' => (int)($line['account_code'] ?? 0),
':period' => $period,
':debit' => (float)($line['debit'] ?? 0) * $sign,
':credit' => (float)($line['credit'] ?? 0) * $sign,
':cnt' => $sign,
]);
}
}
}