فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(A01): verify native posted sales return ledger, stock and excess-return rejection
Extend actual NSIS-installed Windows Tauri/WebView2 E2E tests: posted sale return gets a separate balanced ledger, inbound inventory and correction link, with immutable original and no automatic refund; over-quantity return atomically fails. Both required checks success: CI #38947474622, Windows VM #38947474640. A01 financial acceptance remains open.
davoodmehraban89 committed
Oct 16, 2026 at 22:05 UTC
8f49d5ff9f40226de0207899ab243340d6ebcdf9
1 file changed
+109
tests/windows-vm-native-functional-matrix.cjs
+109
@@ -155,6 +155,114 @@ async function saleInvoice() {
155
return { salePaid: 200, saleRemaining: 400, salePosted: true, cashLedger: true };
156
}
157
158
+async function nativeSaleReturnGuardrails() {
159
+ const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
160
+ const check = (ok, message) => { if (!ok) throw Error(message); };
161
+ const S = FINORA.Storage;
162
+ const sale = (await S.getAll('invoices'))
163
+ .find(i => i.kind === 'sale' && i.financePosted && i.grandTotal === 600);
164
+ const contact = sale && await S.get('contacts', sale.contactId);
165
+ const product = (await S.getAll('products')).find(p => p.code === 'VM-E2E-100');
166
+ check(sale && contact && product, 'Posted original sale fixture is required');
167
+ const originalAmount = sale.grandTotal;
168
+ const originalPaid = sale.paidAmount;
169
+ const buildReturn = async (qty, price) => {
170
+ await FINORA.Router.go('invoices');
171
+ await wait(220);
172
+ await window.InvoiceFormView.open();
173
+ await wait(160);
174
+ const kind = document.getElementById('invKind');
175
+ check(kind, 'Native return invoice form missing');
176
+ kind.value = 'sale_return';
177
+ await window.InvoiceFormView._onKindChange();
178
+ const reference = document.getElementById('invOriginalInvoice');
179
+ check([...reference.options].some(x => x.value === sale.id),
180
+ 'Posted sale does not appear as the reference invoice');
181
+ reference.value = sale.id;
182
+ document.getElementById('invDate').value = '1405/07/18';
183
+ const contactSearch = document.getElementById('invContactSearch');
184
+ contactSearch.value = contact.name;
185
+ window.InvoiceFormView._showContactOptions(true);
186
+ const contactItem = document.querySelector('.contact-option[data-id="' + contact.id + '"]');
187
+ check(contactItem, 'Cannot select original customer in return form');
188
+ contactItem.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
189
+ const productSearch = document.querySelector('.row-product-search');
190
+ check(productSearch, 'Return product search field missing');
191
+ productSearch.value = product.code;
192
+ window.InvoiceFormView._showProductOptions(0, true);
193
+ const selected = document.querySelector('.product-option[data-id="' + product.id + '"]');
194
+ check(selected, 'Original sold product not selectable');
195
+ selected.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
196
+ document.querySelector('.row-qty').value = String(qty);
197
+ window.InvoiceFormView._onFieldChange(0, 'qty', String(qty));
198
+ document.querySelector('.row-price').value = String(price);
199
+ window.InvoiceFormView._onFieldChange(0, 'price', String(price));
200
+ document.getElementById('invPayment').value = 'credit';
201
+ window.InvoiceFormView._onPaymentChange();
202
+ await window.InvoiceFormView._save(false);
203
+ await wait(170);
204
+ };
205
+
206
+ await buildReturn(1, 200);
207
+ check(!document.querySelector('.toast-error'), 'Valid return showed an unexpected error');
208
+ const returns = (await S.getAll('invoices')).filter(x =>
209
+ x.kind === 'sale_return' && x.originalInvoiceId === sale.id);
210
+ check(returns.length === 1 && returns[0].financePosted &&
211
+ returns[0].grandTotal === 200 && returns[0].paidAmount === 0,
212
+ 'Native return did not post a separate unpaid credit document');
213
+ const returned = returns[0];
214
+ const journal = await S.get('journal_entries', returned.journalId);
215
+ check(journal?.status === 'posted' && journal.sourceId === returned.id,
216
+ 'Native sales return lacks its own posted ledger journal');
217
+ const lines = (await S.getAll('journal_lines')).filter(x => x.journalId === journal.id);
218
+ check(lines.length >= 2 && lines.reduce((n, x) => n + x.debit, 0) > 0 &&
219
+ lines.reduce((n, x) => n + x.debit, 0) ===
220
+ lines.reduce((n, x) => n + x.credit, 0),
221
+ 'Return journal has invalid debit/credit balance');
222
+ check((await S.getAll('stock_movements'))
223
+ .some(x => x.refId === returned.id && x.type === 'in' && x.productId === product.id),
224
+ 'Native sales return did not receive returned inventory');
225
+ check((await S.getAll('document_corrections')).some(x =>
226
+ x.sourceId === sale.id && x.resultId === returned.id),
227
+ 'Return correction did not link to its immutable original');
228
+ check(!(await S.getAll('treasury')).some(x => x.invoiceId === returned.id &&
229
+ x.recordType === 'transaction'),
230
+ 'Native return illegally generated an automatic refund');
231
+ const unchanged = await S.get('invoices', sale.id);
232
+ check(unchanged.grandTotal === originalAmount && unchanged.paidAmount === originalPaid &&
233
+ unchanged.financePosted, 'Posted source sale mutated during return');
234
+ const beforeInvalid = {
235
+ returns: (await S.getAll('invoices')).filter(x => x.kind === 'sale_return' &&
236
+ x.originalInvoiceId === sale.id).length,
237
+ stock: (await S.getAll('stock_movements')).length,
238
+ audits: (await S.getAll('audit_events')).length
239
+ };
240
+
241
+ // Quantity 3 would exceed the 2 units remaining from the original sale,
242
+ // while its 300 rial value is still below the remaining financial cap.
243
+ await buildReturn(3, 100);
244
+ let error = [...document.querySelectorAll('.toast-error')].find(x =>
245
+ x.textContent.includes('تعداد مرجوعی'));
246
+ check(error, 'Over-return quantity did not produce its expected UI rejection');
247
+ check((await S.getAll('invoices')).filter(x => x.kind === 'sale_return' &&
248
+ x.originalInvoiceId === sale.id).length === beforeInvalid.returns,
249
+ 'Over-return created an extra financial invoice');
250
+ check((await S.getAll('stock_movements')).length === beforeInvalid.stock &&
251
+ (await S.getAll('audit_events')).length === beforeInvalid.audits,
252
+ 'Rejected over-return left stock or audit side effects');
253
+ error.querySelector('.toast-close')?.click();
254
+ FINORA.Modal.close();
255
+ check(!document.querySelector('.toast-error') && !document.querySelector('.modal-backdrop'),
256
+ 'Return guardrail left an error toast or modal after expected rejection');
257
+ return {
258
+ separatePostedReturn: true,
259
+ balancedLedgerAndReturnedStock: true,
260
+ postedOriginalImmutable: true,
261
+ noAutomaticCashRefund: true,
262
+ overReturnRejectedWithoutWrites: true
263
+ };
264
+}
265
+
266
async function nativeBackupRestore() {
267
const check = (value, message) => { if (!value) throw Error(message); };
268
const S = FINORA.Storage;
@@ -305,6 +413,7 @@ module.exports = async function nativeFunctionalMatrix(cdp) {
413
['activate-accounting-and-inventory', activateAccounting],
414
['purchase-and-post-ledger', purchaseInvoice],
415
['partial-sale-and-inventory', saleInvoice],
416
+ ['sale-return-immutable-stock-and-ledger', nativeSaleReturnGuardrails],
417
['backup-and-atomic-rejection', nativeBackupRestore],
418
['rtl-invoice-print-settings', invoicePrintLayout],
419
['fiscal-close-atomic-guardrails', fiscalCloseGuardrails]