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

feat(A01): expose read-only company-scoped sales return preview in service

davoodmehraban89 committed Oct 14, 2026 at 22:51 UTC 44bc5f18af6c060b727212624134d0e60dd371c7
1 file changed +36
src/core/RetailPostingService.js
+36
@@ -3,6 +3,7 @@ import { StorageService } from './StorageService.js';
3 import { DataScope } from './DataScope.js';
4 import { Auth } from './Auth.js';
5 import { validateJournal, journalSourceKey, assertJalaliDate } from './AccountingCore.mjs';
6 +import { previewSaleReturnSettlement } from './ReturnSettlementCore.mjs';
7 import { LEDGER_CODES, INVOICE_DIRECTIONS, amount, positiveAmount,
8 qtyUnits, fromQtyUnits, receiveStock, issueStock, allocateDiscount,
9 postLine, differenceLines, assertInvoiceTotals } from './RetailFinanceCore.mjs';
@@ -41,6 +42,41 @@ const cashLedger=(ctx,bank)=>{
42 };
43
44 class RetailPostingServiceImpl {
45 + /**
46 + * A01 read-only preview: reconcile a reference-linked sales return with
47 + * cash already allocated to the original invoice. No journal or refund is
48 + * created; the actual command must be a separate authorized transaction.
49 + */
50 + async previewSaleReturn(originalInvoiceId, returnAmount) {
51 + const company = await DataScope.companyId();
52 + if (typeof originalInvoiceId !== 'string' || !originalInvoiceId.trim()) {
53 + throw Error('شناسه فاکتور مرجع الزامی است');
54 + }
55 + return StorageService.transaction(['invoices'], async tx => {
56 + const origin = sameCompany(await tx.get('invoices', originalInvoiceId), company, 'فاکتور مرجع');
57 + if (!['sale', 'non_formal'].includes(origin.kind) || origin.isPreInvoice || !origin.financePosted) {
58 + throw Error('فقط فاکتور فروش قطعی قابل پیش‌نمایش مرجوعی است');
59 + }
60 + const all = await tx.getAll('invoices');
61 + let priorReturns = 0;
62 + for (const row of all) {
63 + if (row.companyId !== company || row.kind !== 'sale_return' ||
64 + row.originalInvoiceId !== origin.id || !row.financePosted) continue;
65 + if (!Number.isSafeInteger(row.grandTotal) || row.grandTotal < 0 ||
66 + !Number.isSafeInteger(priorReturns + row.grandTotal)) {
67 + throw Error('مجموع مرجوعی‌های ثبت‌شده نامعتبر است');
68 + }
69 + priorReturns += row.grandTotal;
70 + }
71 + const breakdown = previewSaleReturnSettlement({
72 + invoiceTotal: origin.grandTotal,
73 + paidAmount: origin.paidAmount ?? 0,
74 + priorReturns,
75 + returnAmount
76 + });
77 + return Object.freeze({ originalInvoiceId: origin.id, companyId: company, ...breakdown });
78 + }, 'readonly');
79 + }
80 async isEnabled() {
81 const company=await DataScope.companyId();
82 return !!(await StorageService.get('settings',activeKey(company)))?.enabled;