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

test(A01): verify native purchase return ledger, outbound stock and over-quantity rollback

Drive actual NSIS-installed Tauri/WebView2 InvoiceFormView to post a reference-linked supplier return, verify balanced ledger, stock-out, immutable source, correction link, no automatic treasury payment, and fail-closed over-quantity with zero writes. PR Quality #38956709911 and native Windows VM #38956709934 passed. Synthetic only; no final accountant/real-data acceptance.

davoodmehraban89 committed Oct 16, 2026 at 23:18 UTC 15c318d688d81c176db6ebadea0467331949315b
1 file changed +95
tests/windows-vm-native-functional-matrix.cjs
+95
@@ -346,6 +346,100 @@ async function nativeSaleReturnGuardrails() {
346 };
347 }
348
349 +async function nativePurchaseReturnGuardrails() {
350 + const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
351 + const check = (ok,msg) => { if (!ok) throw Error(msg); };
352 + const S = FINORA.Storage;
353 + const original = (await S.getAll('invoices')).find(x =>
354 + x.kind === 'purchase' && x.financePosted && x.grandTotal === 1000);
355 + const contact = original && await S.get('contacts', original.contactId);
356 + const product = (await S.getAll('products')).find(x => x.code === 'VM-E2E-100');
357 + check(original && contact && product, 'A01 purchase-return source fixtures missing');
358 + const originalJournal = original.journalId;
359 + const makeReturn = async (qty, price) => {
360 + await FINORA.Router.go('invoices');
361 + await wait(200);
362 + await window.InvoiceFormView.open();
363 + await wait(140);
364 + document.getElementById('invKind').value = 'purchase_return';
365 + await window.InvoiceFormView._onKindChange();
366 + const source = document.getElementById('invOriginalInvoice');
367 + check([...source.options].some(x => x.value === original.id && !x.disabled),
368 + 'Posted purchase not an eligible source in return form');
369 + source.value = original.id;
370 + document.getElementById('invDate').value = '1405/07/18';
371 + const search = document.getElementById('invContactSearch');
372 + search.value = contact.name;
373 + window.InvoiceFormView._showContactOptions(true);
374 + const option = document.querySelector('.contact-option[data-id="' + contact.id + '"]');
375 + check(option, 'Original supplier cannot be selected');
376 + option.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
377 + const productSearch = document.querySelector('.row-product-search');
378 + productSearch.value = product.code;
379 + window.InvoiceFormView._showProductOptions(0, true);
380 + const prod = document.querySelector('.product-option[data-id="' + product.id + '"]');
381 + check(prod, 'Purchased product cannot be selected');
382 + prod.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
383 + document.querySelector('.row-qty').value = String(qty);
384 + window.InvoiceFormView._onFieldChange(0, 'qty', String(qty));
385 + document.querySelector('.row-price').value = String(price);
386 + window.InvoiceFormView._onFieldChange(0, 'price', String(price));
387 + document.getElementById('invPayment').value = 'credit';
388 + window.InvoiceFormView._onPaymentChange();
389 + await window.InvoiceFormView._save(false);
390 + await wait(180);
391 + };
392 + await makeReturn(1, 100);
393 + check(!document.querySelector('.toast-error'), 'Valid supplier return showed unexpected error');
394 + const found = (await S.getAll('invoices')).filter(x =>
395 + x.kind === 'purchase_return' && x.originalInvoiceId === original.id);
396 + check(found.length === 1 && found[0].financePosted &&
397 + found[0].paidAmount === 0 && found[0].grandTotal === 100,
398 + 'Purchase return was not correctly posted without automatic cash receipt');
399 + const returned = found[0];
400 + const ledger = await S.get('journal_entries', returned.journalId);
401 + check(ledger?.status === 'posted' && ledger.sourceId === returned.id,
402 + 'Purchase return journal link missing');
403 + const lines = (await S.getAll('journal_lines')).filter(x => x.journalId === ledger.id);
404 + const debit = lines.reduce((n,x)=>n+x.debit,0);
405 + const credit = lines.reduce((n,x)=>n+x.credit,0);
406 + check(lines.length >= 2 && debit > 0 && debit === credit,
407 + 'Native purchase return failed ledger balancing');
408 + check((await S.getAll('stock_movements')).some(x =>
409 + x.refId === returned.id && x.type === 'out' && x.productId === product.id),
410 + 'Returned purchased inventory did not leave warehouse');
411 + check((await S.getAll('document_corrections')).some(x =>
412 + x.sourceId === original.id && x.resultId === returned.id),
413 + 'Supplier return must be a linked correction');
414 + check((await S.get('invoices', original.id)).journalId === originalJournal,
415 + 'Supplier return illegally changed source purchase');
416 + check(!(await S.getAll('treasury')).some(x =>
417 + x.recordType === 'transaction' && x.invoiceId === returned.id),
418 + 'Supplier return created unauthorized treasury cash event');
419 + const before = {
420 + invoices:(await S.getAll('invoices')).length,
421 + movements:(await S.getAll('stock_movements')).length,
422 + audits:(await S.getAll('audit_events')).length
423 + };
424 + // 10 more units exceeds the 9 remaining, even though the proposed
425 + // 500-rial credit is BELOW the remaining purchase monetary cap.
426 + await makeReturn(10, 50);
427 + const toast = [...document.querySelectorAll('.toast-error')].find(x =>
428 + x.textContent.includes('تعداد مرجوعی'));
429 + check(toast, 'Over-quantity supplier return did not fail visibly');
430 + check((await S.getAll('invoices')).length === before.invoices &&
431 + (await S.getAll('stock_movements')).length === before.movements &&
432 + (await S.getAll('audit_events')).length === before.audits,
433 + 'Rejected supplier return wrote an invoice, stock or audit event');
434 + toast.querySelector('.toast-close')?.click();
435 + FINORA.Modal.close();
436 + check(!document.querySelector('.toast-error') && !document.querySelector('.modal-backdrop'),
437 + 'A01 supplier return negative test left UI error or modal');
438 + return { postedSupplierReturn:true, balancedLedger:true, stockIssued:true,
439 + originalPurchaseImmutable:true, noAutomaticTreasury:true,
440 + overReturnRejectedAtomically:true };
441 +}
442 +
443 async function nativeBackupRestore() {
444 const check = (value, message) => { if (!value) throw Error(message); };
445 const S = FINORA.Storage;
@@ -562,6 +656,7 @@ module.exports = async function nativeFunctionalMatrix(cdp) {
656 ['purchase-and-post-ledger', purchaseInvoice],
657 ['partial-sale-and-inventory', saleInvoice],
658 ['sale-return-immutable-stock-and-ledger', nativeSaleReturnGuardrails],
659 + ['purchase-return-and-stock-guardrails', nativePurchaseReturnGuardrails],
660 ['backup-and-atomic-rejection', nativeBackupRestore],
661 ['rtl-invoice-print-settings', invoicePrintLayout],
662 ['fiscal-close-atomic-guardrails', fiscalCloseGuardrails],