@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 71234fc
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 10:30 UTC

fix(company): port reviewed active-company isolation in js/sync.js

davoodmehraban89 committed Oct 14, 2026 at 13:15 UTC 71234fca289188ea965007cb158e2ce58a141841
1 file changed +50 -7
js/sync.js
+50 -7
@@ -1,12 +1,55 @@
1 let datastore={companies:[],contacts:[],products:[],invoices:[],purchases:[],cheques:[],expenses:[],payments:[],settings:[],fiscalYears:[],accounts:[],dimensionTypes:[],dimensionValues:[],accountDimensionRules:[],branches:[],projects:[],projectLinks:[],postingProfiles:[],journalVouchers:[],journalLines:[],journalLineDimensions:[],contracts:[],contractAmendments:[],contractParties:[],contractDeductions:[],guarantees:[],guaranteeEvents:[],contractStatements:[],phase4Audit:[],warehouses:[],stockMovements:[],inventoryCounts:[],fixedAssets:[],assetDepreciations:[],currencies:[],exchangeRates:[],costCenters:[],importBatches:[],integrationConnections:[],integrationOutbox:[],phase5Audit:[],employees:[],employmentContracts:[],employeeLeave:[],cashboxes:[],pettyCashFunds:[],complianceRules:[],payrollRuns:[],payrollLines:[],employeeBenefitAccruals:[],pettyCashTransactions:[],budgets:[],costDrivers:[],costAllocations:[],intercompanyTransactions:[],enterpriseAudit:[]};
2 -function getMyProducts(){return !currentUser?[]:datastore.products.filter(p=>p.ownerUserId===currentUser.id);}
3 -function getMyContacts(){return !currentUser?[]:datastore.contacts.filter(c=>c.ownerUserId===currentUser.id);}
2 function getMyCompanies(){return !currentUser?[]:datastore.companies.filter(c=>c.ownerUserId===currentUser.id);}
5 -function getMyInvoices(){return !currentUser?[]:datastore.invoices.filter(i=>i.ownerUserId===currentUser.id);}
6 -function getMyPurchases(){return !currentUser?[]:(datastore.purchases||[]).filter(i=>i.ownerUserId===currentUser.id);}
7 -function getMyCheques(){return !currentUser?[]:datastore.cheques.filter(c=>c.ownerUserId===currentUser.id);}
8 -function getMyExpenses(){return !currentUser?[]:datastore.expenses.filter(e=>e.ownerUserId===currentUser.id);}
9 -function getMyPayments(){return !currentUser?[]:(datastore.payments||[]).filter(p=>p.ownerUserId===currentUser.id);}
3 +// Active company is one exact owned company, NOT all companies or a holding-wide
4 +// writable context. Orphaned data can be reviewed separately, never broadcast.
5 +function finoraActiveCompanyId(){
6 + if(!currentUser)return '';
7 + const companies=getMyCompanies(),selected=getMySettings().default_company_id;
8 + if(selected)return companies.some(c=>c.id===selected)?selected:'';
9 + return companies[0]?.id||'';
10 +}
11 +function finoraInActiveCompany(x,{allowSingleCompanyLegacy=true}={}){
12 + if(!currentUser||!x||x.ownerUserId!==currentUser.id)return false;
13 + const cid=finoraActiveCompanyId();if(!cid)return false;
14 + if(x.companyId)return x.companyId===cid;
15 + // Old unassigned records are only shown while a single company exists.
16 + // When additional companies are created they become unassigned-review cases.
17 + return allowSingleCompanyLegacy&&getMyCompanies().length===1;
18 +}
19 +function finoraScopedRows(key,{allowSingleCompanyLegacy=true}={}){
20 + return !currentUser?[]:(datastore[key]||[]).filter(x=>finoraInActiveCompany(x,{allowSingleCompanyLegacy}));
21 +}
22 +// Never import a multi-company backup through an active-company restore UI.
23 +// This is a read-only preflight: it does not assign orphan historical records.
24 +function finoraValidateImportCompanyScope(payload){
25 + const cid=finoraActiveCompanyId();
26 + if(!cid)throw new Error('ابتدا شرکت فعال معتبر را انتخاب کنید.');
27 + const single=getMyCompanies().length===1;
28 + if(payload?.settings&&!Array.isArray(payload.settings)&&payload.settings.default_company_id&&payload.settings.default_company_id!==cid)
29 + throw new Error('پشتیبان متعلق به شرکت فعال نیست.');
30 + for(const [kind,rows] of Object.entries(payload||{})){
31 + if(!Array.isArray(rows))continue;
32 + for(const item of rows){
33 + if(!item||typeof item!=='object')throw new Error('رکورد پشتیبان معتبر نیست.');
34 + if(kind==='companies'){
35 + if(item.id!==cid)throw new Error('بازیابی هم‌زمان شرکت دیگر در محیط شرکت فعال مجاز نیست.');
36 + }else if(kind==='settings'){
37 + if(item.default_company_id&&item.default_company_id!==cid)throw new Error('شرکت پیش‌فرض در پشتیبان متفاوت است.');
38 + }else if(kind!=='settings'&&kind!=='companies'&&Array.isArray(datastore[kind])){
39 + if(item.companyId!==cid&&!(single&&!item.companyId))
40 + throw new Error('رکورد '+kind+' متعلق به شرکت فعال نیست یا شرکت مشخص ندارد.');
41 + }
42 + }
43 + }
44 + return true;
45 +}
46 +function getMyProducts(){return finoraScopedRows('products');}
47 +function getMyContacts(){return finoraScopedRows('contacts');}
48 +function getMyInvoices(){return finoraScopedRows('invoices');}
49 +function getMyPurchases(){return finoraScopedRows('purchases');}
50 +function getMyCheques(){return finoraScopedRows('cheques');}
51 +function getMyExpenses(){return finoraScopedRows('expenses');}
52 +function getMyPayments(){return finoraScopedRows('payments');}
53 function getMySettings(){
54 if(!currentUser)return{invoice_kind:'non_formal',vat_mode:'none',vat_rate:10,default_company_id:'',currency_mode:'rial',currency_custom:''};
55 if(!Array.isArray(datastore.settings))datastore.settings=[];