فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

feat(A05): reconcile cheque lifecycle and duplicated treasury settlement rows

davoodmehraban89 committed Oct 17, 2026 at 00:34 UTC 6e91d7b69e068bdd129abb213b35d79b048f077b
1 file changed +71 -2
src/core/ReconciliationCore.mjs
+71 -2
@@ -12,7 +12,7 @@ const safeAdd = (a,b) => {
12
13 export function reconcileFinancialData({
14 accounts = [], journals = [], journalLines = [], invoices = [],
15 - settlements = [], transactions = [], inventoryBalances = [],
15 + settlements = [], transactions = [], cheques = [], inventoryBalances = [],
16 stockMovements = [], integrated = false
17 } = {}) {
18 const issues = [];
@@ -58,10 +58,27 @@ export function reconcileFinancialData({
58
59 const allTransactions = new Map(transactions.filter(t => t.recordType === 'transaction').map(t => [t.id,t]));
60 const invoiceById = new Map(invoices.map(i => [i.id,i]));
61 + const settlementTransactionUse = new Map();
62 const settlementsByInvoice = new Map();
63 for (const row of settlements) {
64 if (!invoiceById.has(row.invoiceId)) report('ORPHAN_SETTLEMENT', 'تسویه به فاکتور ناموجود متصل است', row.id);
64 - if (!allTransactions.has(row.transactionId)) report('MISSING_SETTLEMENT_TRANSACTION', 'تراکنش مربوط به تسویه یافت نشد', row.id);
65 + const cash = allTransactions.get(row.transactionId);
66 + if (!cash) {
67 + report('MISSING_SETTLEMENT_TRANSACTION', 'تراکنش مربوط به تسویه یافت نشد', row.id);
68 + } else {
69 + if (cash.refInvoiceId !== undefined && cash.refInvoiceId !== null &&
70 + cash.refInvoiceId !== row.invoiceId) {
71 + report('SETTLEMENT_REFERENCE_MISMATCH', 'ارجاع تسویه و تراکنش خزانه به فاکتور متفاوت است', row.id);
72 + }
73 + if (safeMoney(cash.amount) && cash.amount !== row.amount) {
74 + report('SETTLEMENT_CASH_MISMATCH', 'مبلغ تسویه با تراکنش خزانه متناظر برابر نیست', row.id,
75 + 'error', row.amount, cash.amount);
76 + }
77 + if (settlementTransactionUse.has(row.transactionId)) {
78 + report('DUPLICATE_SETTLEMENT_TRANSACTION', 'یک تراکنش خزانه در چند تسویه استفاده شده است', row.id);
79 + }
80 + settlementTransactionUse.set(row.transactionId, row.id);
81 + }
82 if (!safeMoney(row.amount) || row.amount === 0) report('INVALID_SETTLEMENT_AMOUNT', 'مبلغ تسویه نامعتبر است', row.id);
83 if (!settlementsByInvoice.has(row.invoiceId)) settlementsByInvoice.set(row.invoiceId, []);
84 settlementsByInvoice.get(row.invoiceId).push(row);
@@ -90,6 +107,57 @@ export function reconcileFinancialData({
107 }
108 }
109
110 +
111 + // A05: read-only cheque lifecycle and its bank/cash evidence.
112 + // Legacy unposted cheques are not asserted to have journal references.
113 + // Only posted integrated cheques can be reconciled against double-entry GL.
114 + if (integrated) {
115 + const chequeById = new Map(cheques.map(row => [row.id, row]));
116 + const moneyByCheque = new Map();
117 + for (const transaction of transactions.filter(row =>
118 + row.recordType === 'transaction' && row.refChequeId)) {
119 + if (!moneyByCheque.has(transaction.refChequeId)) moneyByCheque.set(transaction.refChequeId, []);
120 + moneyByCheque.get(transaction.refChequeId).push(transaction);
121 + if (!chequeById.has(transaction.refChequeId)) {
122 + report('ORPHAN_CHEQUE_CASH', 'دریافت یا پرداخت چک به چک ناموجود متصل است', transaction.id);
123 + }
124 + }
125 + for (const cheque of cheques.filter(row => row.financePosted)) {
126 + const origin = byJournal.get(cheque.journalId);
127 + if (!origin || origin.status !== 'posted' || origin.sourceType !== 'cheque' ||
128 + origin.sourceId !== cheque.id) {
129 + report('CHEQUE_SOURCE_JOURNAL_MISSING', 'چک قطعی سند اولیه معتبر ندارد', cheque.id);
130 + }
131 + const money = moneyByCheque.get(cheque.id) || [];
132 + if (cheque.status === 'pending') {
133 + if (cheque.statusJournalId || money.length) {
134 + report('PENDING_CHEQUE_HAS_SETTLEMENT', 'چک در جریان دارای سند یا تراکنش تعیین‌تکلیف است', cheque.id);
135 + }
136 + continue;
137 + }
138 + const final = byJournal.get(cheque.statusJournalId);
139 + if (!final || final.status !== 'posted' || final.sourceType !== 'cheque_status' ||
140 + final.sourceId !== cheque.id) {
141 + report('CHEQUE_STATUS_JOURNAL_MISSING', 'وضعیت قطعی چک سند تعیین‌تکلیف معتبر ندارد', cheque.id);
142 + }
143 + if (cheque.status === 'cleared') {
144 + if (money.length !== 1) {
145 + report('CHEQUE_CLEARING_CASH_COUNT', 'چک وصول‌شده باید دقیقاً یک دریافت یا پرداخت خزانه داشته باشد',
146 + cheque.id, 'error', 1, money.length);
147 + }
148 + for (const transaction of money) {
149 + const direction = cheque.direction === 'inbound' ? 'receipt' : 'payment';
150 + if (transaction.type !== direction || transaction.amount !== cheque.amount ||
151 + transaction.journalId !== cheque.statusJournalId || !transaction.financePosted) {
152 + report('CHEQUE_CLEARING_CASH_MISMATCH', 'وصول چک با جهت یا مبلغ یا سند خزانه مغایرت دارد', cheque.id);
153 + }
154 + }
155 + } else if (money.length) {
156 + report('NONCLEARED_CHEQUE_HAS_CASH', 'چک وصول‌نشده دارای دریافت یا پرداخت خزانه است', cheque.id);
157 + }
158 + }
159 + }
160 +
161 let inventoryBook = 0, inventoryLedger = 0;
162 let stockUnits = 0;
163 if (integrated) {
@@ -144,6 +212,7 @@ export function reconcileFinancialData({
212 return {
213 integrated, checkedAt: new Date().toISOString(),
214 counts: { journals: posted.length, invoices: checkedInvoices, settlements: settlements.length,
215 + cheques: cheques.filter(row => row.financePosted).length,
216 inventoryBalances: inventoryBalances.length, issues: issues.length },
217 totals: { inventoryBook, inventoryLedger, stockUnits },
218 issues