سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 00:30 UTC
Implement Phase 2 accounting foundation model
davoodmehraban89 committed
Sep 28, 2026 at 17:47 UTC
13a29ea41d291c6e5b463636d164760d9d349b57
1 file changed
+43
js/accounting-foundation.js
new
+43
@@ -0,0 +1,43 @@
1
+/* Phase 2: accounting foundation */
2
+const AF_LEVELS={gl:'کل',subsidiary:'معین',detail:'تفصیلی'},AF_NATURE={debit:'بدهکار',credit:'بستانکار',both:'دوماهیتی'};
3
+function afCompanyId(){const s=getMySettings(),c=getMyCompanies();return s.default_company_id||c[0]?.id||'';}
4
+function afOwned(k){const cid=afCompanyId();return(datastore[k]||[]).filter(x=>x.ownerUserId===currentUser?.id&&(!x.companyId||x.companyId===cid));}
5
+function getMyFiscalYears(){return afOwned('fiscalYears')} function getMyAccounts(){return afOwned('accounts')}
6
+function getMyDimensionTypes(){return afOwned('dimensionTypes')} function getMyDimensionValues(){return afOwned('dimensionValues')}
7
+function getMyAccountDimensionRules(){return afOwned('accountDimensionRules')} function getMyBranches(){return afOwned('branches')}
8
+function getMyGlobalProjects(){return afOwned('projects')}
9
+function afId(p){return p+'_'+Date.now().toString(36)+'_'+Math.random().toString(36).slice(2,7)}
10
+function afCode(v){return String(v||'').trim().replace(/\s+/g,'-').slice(0,32)}
11
+function afNeedCompany(){const id=afCompanyId();if(!id)alert('ابتدا یک شرکت تعریف و فعال کنید.');return id}
12
+function afProjectUsed(id){return getMyInvoices().some(x=>x.projectId===id)||getMyPurchases().some(x=>x.projectId===id||x.costCenterId===id)||getMyExpenses().some(x=>x.projectId===id)||getMyPayments().some(x=>x.projectId===id)}
13
+function afResolvedValues(t){if(t.sourceEntity==='contact')return getMyContacts().map(x=>({id:x.id,title:x.name}));if(t.sourceEntity==='project')return getMyGlobalProjects().map(x=>({id:x.id,title:x.name}));if(t.sourceEntity==='branch')return getMyBranches().map(x=>({id:x.id,title:x.name}));return getMyDimensionValues().filter(x=>x.dimensionTypeId===t.id)}
14
+function afSeed(){
15
+ if(!currentUser||!afCompanyId())return;let ch=false,cid=afCompanyId();
16
+ if(!getMyFiscalYears().length){const y=toEnDigits(String(getJalaliNumeric()).split('/')[0]);datastore.fiscalYears.push({id:'FY_'+y,ownerUserId:currentUser.id,companyId:cid,title:'سال مالی '+y,startDate:y+'/01/01',endDate:y+'/12/29',status:'open'});ch=true}
17
+ [['DIM_BRANCH','شعبه','branch'],['DIM_COUNTERPARTY','طرف حساب','contact'],['DIM_PROJECT','پروژه','project'],['DIM_ANALYTIC4','تحلیل شناور ۴','manual']].forEach(([id,title,sourceEntity])=>{if(!getMyDimensionTypes().some(x=>x.id===id)){datastore.dimensionTypes.push({id,ownerUserId:currentUser.id,companyId:cid,code:id.slice(4),title,maxDepth:sourceEntity==='project'?4:1,leafOnlyPosting:true,active:true,sourceType:sourceEntity==='manual'?'manual':'entity-backed',sourceEntity});ch=true}});
18
+ if(ch)saveDatastore();
19
+}
20
+function afMigrateLegacyProjects(){
21
+ if(!currentUser||!afCompanyId())return;const s=getMySettings();if(s.accountingFoundationMigrationV1)return;let ch=false;
22
+ getMyContacts().forEach(c=>(c.projects||[]).forEach(p=>{let g=datastore.projects.find(x=>x.id===p.id&&x.ownerUserId===currentUser.id);if(!g){g={id:p.id,ownerUserId:currentUser.id,companyId:afCompanyId(),code:p.id,name:p.name,parentId:'',direction:p.direction||'both',linkedContactIds:[c.id],active:true,legacy:true};datastore.projects.push(g);ch=true}else if(!(g.linkedContactIds||[]).includes(c.id)){g.linkedContactIds=[...(g.linkedContactIds||[]),c.id];ch=true}(p.subprojects||[]).forEach(z=>{let q=datastore.projects.find(x=>x.id===z.id&&x.ownerUserId===currentUser.id);if(!q){datastore.projects.push({id:z.id,ownerUserId:currentUser.id,companyId:afCompanyId(),code:z.id,name:z.name,parentId:p.id,direction:z.direction||p.direction||'both',linkedContactIds:[c.id],active:true,legacy:true});ch=true}})}));
23
+ s.accountingFoundationMigrationV1={completedAt:new Date().toISOString(),mode:'copy-preserve-ids'};saveDatastore();
24
+}
25
+function afSaveAccount(){
26
+ if(!requireWrite())return;const cid=afNeedCompany();if(!cid)return;
27
+ const level=document.getElementById('af-account-level').value,code=afCode(document.getElementById('af-account-code').value),title=document.getElementById('af-account-title').value.trim(),normalBalance=document.getElementById('af-account-nature').value,parentId=document.getElementById('af-account-parent').value;
28
+ if(!code||!title)return alert('کد و عنوان حساب الزامی است.');if(getMyAccounts().some(x=>x.code===code))return alert('کد حساب تکراری است.');
29
+ const need=level==='subsidiary'?'gl':level==='detail'?'subsidiary':'';if(need&&!getMyAccounts().some(x=>x.id===parentId&&x.level===need))return alert('حساب والد معتبر انتخاب کنید.');
30
+ datastore.accounts.push({id:afId('ACC'),ownerUserId:currentUser.id,companyId:cid,code,title,level,parentId:parentId||'',normalBalance,postingAllowed:level==='detail',active:true});saveDatastore();renderAccountingFoundation();
31
+}
32
+function afDeleteAccount(id){if(!requireWrite())return;if(getMyAccounts().some(x=>x.parentId===id))return alert('حساب دارای زیرحساب است.');if(getMyAccountDimensionRules().some(x=>x.accountId===id))return alert('ابتدا قواعد تفصیلی حساب را حذف کنید.');if(confirm('حساب حذف شود؟')){datastore.accounts=datastore.accounts.filter(x=>x.id!==id);saveDatastore();renderAccountingFoundation()}}
33
+function afSaveRule(){if(!requireWrite())return;const cid=afNeedCompany(),accountId=document.getElementById('af-rule-account').value,dimensionTypeId=document.getElementById('af-rule-dimension').value,applicability=document.getElementById('af-rule-app').value,a=getMyAccounts().find(x=>x.id===accountId);if(!cid||!a?.postingAllowed)return alert('حساب ثبتپذیر انتخاب کنید.');let r=getMyAccountDimensionRules().find(x=>x.accountId===accountId&&x.dimensionTypeId===dimensionTypeId);if(r)r.applicability=applicability;else datastore.accountDimensionRules.push({id:afId('ADR'),ownerUserId:currentUser.id,companyId:cid,accountId,dimensionTypeId,applicability,allowedValuesMode:'all',active:true});saveDatastore();renderAccountingFoundation()}
34
+function afDeleteRule(id){if(!requireWrite())return;datastore.accountDimensionRules=datastore.accountDimensionRules.filter(x=>x.id!==id);saveDatastore();renderAccountingFoundation()}
35
+function afSaveDimension(){if(!requireWrite())return;const cid=afNeedCompany(),code=afCode(document.getElementById('af-dim-code').value).toUpperCase(),title=document.getElementById('af-dim-title').value.trim(),sourceEntity=document.getElementById('af-dim-source').value,maxDepth=Math.max(1,Math.min(8,+document.getElementById('af-dim-depth').value||1));if(!cid||!code||!title)return alert('کد و عنوان الزامی است.');if(getMyDimensionTypes().some(x=>x.code===code))return alert('کد تکراری است.');datastore.dimensionTypes.push({id:afId('DIM'),ownerUserId:currentUser.id,companyId:cid,code,title,maxDepth,leafOnlyPosting:true,active:true,sourceType:sourceEntity==='manual'?'manual':'entity-backed',sourceEntity});saveDatastore();renderAccountingFoundation()}
36
+function afSaveDimensionValue(){if(!requireWrite())return;const cid=afNeedCompany(),dimensionTypeId=document.getElementById('af-value-dim').value,t=getMyDimensionTypes().find(x=>x.id===dimensionTypeId);if(!cid||t?.sourceEntity!=='manual')return alert('فقط تفصیلی دستی مقدار مستقل میپذیرد.');const code=afCode(document.getElementById('af-value-code').value),title=document.getElementById('af-value-title').value.trim(),parentId=document.getElementById('af-value-parent').value;let depth=1;if(parentId){const p=getMyDimensionValues().find(x=>x.id===parentId);if(!p)return alert('والد معتبر نیست.');depth=(p.depth||1)+1}if(!code||!title||depth>t.maxDepth)return alert('کد، عنوان یا عمق معتبر نیست.');datastore.dimensionValues.push({id:afId('DV'),ownerUserId:currentUser.id,companyId:cid,dimensionTypeId,code,title,parentId:parentId||'',depth,active:true});saveDatastore();renderAccountingFoundation()}
37
+function afDeleteValue(id){if(!requireWrite())return;if(getMyDimensionValues().some(x=>x.parentId===id))return alert('این مقدار زیرمجموعه دارد.');datastore.dimensionValues=datastore.dimensionValues.filter(x=>x.id!==id);saveDatastore();renderAccountingFoundation()}
38
+function afSaveFiscal(){if(!requireWrite())return;const cid=afNeedCompany(),title=document.getElementById('af-fy-title').value.trim(),startDate=toEnDigits(document.getElementById('af-fy-start').value),endDate=toEnDigits(document.getElementById('af-fy-end').value);if(!cid||!title||!/^\d{4}\/\d{2}\/\d{2}$/.test(startDate)||!/^\d{4}\/\d{2}\/\d{2}$/.test(endDate))return alert('عنوان و تاریخ شمسی معتبر وارد کنید.');datastore.fiscalYears.push({id:afId('FY'),ownerUserId:currentUser.id,companyId:cid,title,startDate,endDate,status:'open'});saveDatastore();renderAccountingFoundation()}
39
+function afToggleFiscal(id){if(!requireWrite())return;const f=getMyFiscalYears().find(x=>x.id===id);if(!f)return;f.status=f.status==='locked'?'open':'locked';f.lockedAt=f.status==='locked'?new Date().toISOString():'';saveDatastore();renderAccountingFoundation()}
40
+function afSaveBranch(){if(!requireWrite())return;const cid=afNeedCompany(),code=afCode(document.getElementById('af-branch-code').value),name=document.getElementById('af-branch-name').value.trim();if(!cid||!code||!name)return alert('کد و نام شعبه الزامی است.');if(getMyBranches().some(x=>x.code===code))return alert('کد شعبه تکراری است.');datastore.branches.push({id:afId('BR'),ownerUserId:currentUser.id,companyId:cid,code,name,active:true});saveDatastore();renderAccountingFoundation()}
41
+function afDeleteBranch(id){if(!requireWrite())return;if(getMyGlobalProjects().some(x=>x.branchId===id))return alert('شعبه در پروژه استفاده شده است.');datastore.branches=datastore.branches.filter(x=>x.id!==id);saveDatastore();renderAccountingFoundation()}
42
+function afSaveProject(){if(!requireWrite())return;const cid=afNeedCompany(),code=afCode(document.getElementById('af-project-code').value),name=document.getElementById('af-project-name').value.trim(),parentId=document.getElementById('af-project-parent').value,branchId=document.getElementById('af-project-branch').value;if(!cid||!code||!name)return alert('کد و نام پروژه الزامی است.');if(getMyGlobalProjects().some(x=>x.code===code))return alert('کد پروژه تکراری است.');const linkedContactIds=[...document.getElementById('af-project-contacts').selectedOptions].map(x=>x.value);datastore.projects.push({id:afId('PRJ'),ownerUserId:currentUser.id,companyId:cid,code,name,parentId:parentId||'',branchId:branchId||'',linkedContactIds,direction:'both',active:true});saveDatastore();renderAccountingFoundation()}
43
+function afDeleteProject(id){if(!requireWrite())return;if(getMyGlobalProjects().some(x=>x.parentId===id))return alert('پروژه زیرمجموعه دارد.');if(afProjectUsed(id))return alert('پروژه در سند مالی استفاده شده و قابل حذف نیست.');datastore.projects=datastore.projects.filter(x=>x.id!==id);saveDatastore();renderAccountingFoundation()}