سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 22:00 UTC
Add deterministic golden accounting core journey
davoodmehraban89 committed
Sep 29, 2026 at 22:29 UTC
2a4872ac29595015c44f264af23a8968e5acdea5
1 file changed
+54
scripts/golden-accounting-core-check.mjs
new
+54
@@ -0,0 +1,54 @@
1
+import fs from 'node:fs';
2
+import vm from 'node:vm';
3
+
4
+let seq=0;
5
+const datastore={postingProfiles:[],journalVouchers:[],journalLines:[],journalLineDimensions:[]};
6
+const accounts=[
7
+ {id:'cash',code:'1101',title:'بانک',accountType:'asset',active:true,postingAllowed:true},
8
+ {id:'ar',code:'1102',title:'دریافتنی',accountType:'asset',active:true,postingAllowed:true},
9
+ {id:'vatrec',code:'1108',title:'مالیات خرید',accountType:'asset',active:true,postingAllowed:true},
10
+ {id:'ap',code:'2101',title:'پرداختنی',accountType:'liability',active:true,postingAllowed:true},
11
+ {id:'vatpay',code:'2102',title:'مالیات فروش',accountType:'liability',active:true,postingAllowed:true},
12
+ {id:'rev',code:'4101',title:'فروش',accountType:'revenue',active:true,postingAllowed:true},
13
+ {id:'inc',code:'4201',title:'سایر درآمد',accountType:'revenue',active:true,postingAllowed:true},
14
+ {id:'buy',code:'5101',title:'بهای تمامشده',accountType:'expense',active:true,postingAllowed:true},
15
+ {id:'exp',code:'6103',title:'هزینه عمومی',accountType:'expense',active:true,postingAllowed:true}
16
+];
17
+const fiscal=[{id:'FY1',startDate:'1405/01/01',endDate:'1405/12/29',status:'open'}];
18
+const ctx={window:{},console,datastore,currentUser:{id:'U1'},requireWrite:()=>true,afCompanyId:()=> 'C1',afId:p=>p+'_'+(++seq),toEnDigits:String,getJalaliNumeric:()=> '1405/07/07',
19
+ getMyFiscalYears:()=>fiscal,getMyAccounts:()=>accounts,getMyAccountDimensionRules:()=>[],getMyDimensionTypes:()=>[],afResolvedValues:()=>[],saveDatastore:()=>true,
20
+ getMyJournalLineDimensions:()=>datastore.journalLineDimensions,getMyGlobalProjects:()=>[],getMyContracts:()=>[],getMyInvoices:()=>[],confirm:()=>false,esc:String};
21
+vm.createContext(ctx);
22
+vm.runInContext(fs.readFileSync('js/journal-engine.js','utf8'),ctx);
23
+ctx.jeEnsureDefaultProfiles();
24
+
25
+const assert=(ok,msg)=>{if(!ok)throw new Error(msg)};
26
+const sale={id:'S1',date:'1405/07/01',grandTotal:110,vat:10,paymentMethod:'credit',accountingVersion:1};
27
+const saleV=ctx.jePostSourceRecord('sale',sale);
28
+const receiptV=ctx.jePostSourceRecord('receipt',{id:'R1',date:'1405/07/02',amount:110,accountingVersion:1});
29
+const purchase={id:'P1',date:'1405/07/03',grandTotal:55,vat:5,paymentMethod:'credit',accountingVersion:1};
30
+const purchaseV=ctx.jePostSourceRecord('purchase',purchase);
31
+const paymentV=ctx.jePostSourceRecord('payment',{id:'PAY1',date:'1405/07/04',amount:55,accountingVersion:1});
32
+const expense={id:'E1',date:'1405/07/05',amount:20,accountingVersion:1};
33
+const expenseV=ctx.jePostSourceRecord('expense',expense);
34
+const incomeV=ctx.jePostSourceRecord('income',{id:'I1',date:'1405/07/06',amount:7,accountingVersion:1});
35
+ctx.jeReverse(expenseV.id,'1405/07/07','golden journey reversal');
36
+
37
+for(const v of [saleV,receiptV,purchaseV,paymentV,expenseV,incomeV]){
38
+ const lines=datastore.journalLines.filter(x=>x.voucherId===v.id);
39
+ assert(lines.reduce((s,x)=>s+x.debit,0)===lines.reduce((s,x)=>s+x.credit,0),'voucher '+v.id+' is not balanced');
40
+}
41
+const tb=ctx.jeTrialBalance(),by=id=>tb.find(x=>x.accountId===id)||{debit:0,credit:0,balance:0};
42
+assert(by('ar').balance===0,'receivable did not settle to zero');
43
+assert(by('ap').balance===0,'payable did not settle to zero');
44
+assert(by('vatpay').balance===-10,'sales VAT liability mismatch');
45
+assert(by('vatrec').balance===5,'recoverable purchase VAT mismatch');
46
+assert(by('cash').balance===62,'cash reconciliation mismatch');
47
+assert(Math.abs(tb.reduce((s,x)=>s+x.balance,0))<0.0001,'trial balance does not reconcile');
48
+
49
+vm.runInContext(fs.readFileSync('js/phase6-reporting.js','utf8'),ctx);
50
+const f=ctx.window.P6Reports.financials('1405/07/01','1405/07/31');
51
+assert(f.revenue===107,'revenue report mismatch');
52
+assert(f.expense===50,'expense report mismatch after reversal');
53
+assert(f.profit===57,'profit report mismatch');
54
+console.log('Golden accounting core journey passed: sale -> receipt -> purchase -> payment -> expense/income -> reversal -> trial balance -> financial statements.');