فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(backup): validate return-credit allocations without treasury rows
Davood Mehraban committed
Nov 23, 2026 at 16:18 UTC
75ad6329a1fc881263cc053db7ac61694af46794
2 files changed
+48
-4
src/core/CompanyBackupService.js
+16
-4
@@ -250,10 +250,22 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
250
const invoices=(await tx.getAll('invoices')).filter(x=>x.companyId===companyId);
251
const transactions=(await tx.getAll('treasury')).filter(x=>x.companyId===companyId);
252
const settlements=await tx.getAllByIndex('settlements','companyId',companyId);
253
- for(const row of settlements)if(!invoices.some(i=>i.id===row.invoiceId)||
254
- !transactions.some(t=>t.id===row.transactionId&&t.recordType==='transaction')||
255
- !Number.isSafeInteger(row.amount)||row.amount<=0)
256
- throw Error('تسویه بازیابیشده به فاکتور یا تراکنش نامعتبر اشاره دارد');
253
+ for(const row of settlements){
254
+ const invoice=invoices.find(i=>i.id===row.invoiceId);
255
+ const validAmount=Number.isSafeInteger(row.amount)&&row.amount>0;
256
+ // Return-credit allocations settle an original receivable/payable
257
+ // against a posted return invoice, without a treasury transaction.
258
+ // Cash settlements instead require a real treasury transaction.
259
+ const validLink=row.settlementType==='credit_allocation'
260
+ ? row.transactionId==null&&row.targetInvoiceId===row.invoiceId&&
261
+ !!invoices.find(i=>i.id===row.sourceInvoiceId&&i.financePosted&&
262
+ (i.kind==='sale_return'||i.kind==='purchase_return')&&
263
+ i.originalInvoiceId===row.invoiceId)&&
264
+ !!invoice&&invoice.financePosted
265
+ : transactions.some(t=>t.id===row.transactionId&&t.recordType==='transaction');
266
+ if(!invoice||!validAmount||!validLink)
267
+ throw Error('تسویه بازیابیشده به فاکتور یا تراکنش نامعتبر اشاره دارد');
268
+ }
269
}
270
// Restore only this company's finance activation marker, never global preferences
271
// or another company's settings.
tests/backup-restore-integration.test.mjs
+32
@@ -222,3 +222,35 @@ test('A04: older backup envelopes without optional checksum retain supported com
222
await SettingsController.importBackup(backup);
223
assert.equal((await StorageService.get('contacts', 'legacy-contact')).name, 'Legacy');
224
});
225
+
226
+
227
+test('A04: legitimate return credit allocation restores without a treasury transaction', async () => {
228
+ await setup();
229
+ await StorageService.put('invoices', {
230
+ id:'origin-credit',companyId:'c1',kind:'sale',financePosted:true
231
+ });
232
+ await StorageService.put('invoices', {
233
+ id:'return-credit',companyId:'c1',kind:'sale_return',
234
+ originalInvoiceId:'origin-credit',financePosted:true
235
+ });
236
+ const allocation={
237
+ id:'allocation-credit',companyId:'c1',settlementType:'credit_allocation',
238
+ invoiceId:'origin-credit',targetInvoiceId:'origin-credit',
239
+ sourceInvoiceId:'return-credit',transactionId:null,amount:50
240
+ };
241
+ const data={contacts:[{id:'restored-credit-contact',companyId:'c1',name:'Valid'}],
242
+ settlements:[allocation]};
243
+ await restoreCompanyBackup(data,{companyId:'c1',userId:'restore-admin'});
244
+ assert.equal((await StorageService.get('settlements',allocation.id)).amount,50);
245
+ assert.equal((await StorageService.get('contacts','restored-credit-contact')).name,'Valid');
246
+ const beforeAudits=(await StorageService.getAll('audit_events')).length;
247
+ const invalid={...allocation,id:'forged-credit',sourceInvoiceId:'missing-return'};
248
+ await assert.rejects(
249
+ restoreCompanyBackup({contacts:[{id:'rolled-back-credit-contact',companyId:'c1',name:'Invalid'}],
250
+ settlements:[invalid]},{companyId:'c1',userId:'restore-admin'}),
251
+ /تسویه بازیابیشده/
252
+ );
253
+ assert.equal(await StorageService.get('contacts','rolled-back-credit-contact'),undefined);
254
+ assert.equal(await StorageService.get('settlements','forged-credit'),undefined);
255
+ assert.equal((await StorageService.getAll('audit_events')).length,beforeAudits);
256
+});