@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 3844df7
Fork 0

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

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

Synced Dec 4, 10:30 UTC

fix(company): reconcile holding consent and safe company switch with released invoice UI

davoodmehraban89 committed Oct 14, 2026 at 13:15 UTC 3844df7d986fe64e819c4d874540189259592cdf
1 file changed +40 -14
js/companies.js
+40 -14
@@ -2,7 +2,7 @@ let editingCompanyId='';
2 const COMPANY_ACTIVITY_LABELS={trading:'بازرگانی',service:'خدماتی',manufacturing:'تولیدی',contracting:'پیمانکاری',retail:'فروشگاهی',distribution:'پخش',nonprofit:'غیرانتفاعی',professional:'خدمات حرفه‌ای'};
3 const COMPANY_LEGAL_FORM_LABELS={natural_person:'شخص حقیقی',private_joint_stock:'سهامی خاص',public_joint_stock:'سهامی عام',limited_liability:'با مسئولیت محدود',general_partnership:'تضامنی',relative_partnership:'نسبی',mixed_non_stock:'مختلط غیرسهامی',mixed_stock:'مختلط سهامی',cooperative:'تعاونی',noncommercial_institution:'مؤسسه غیرتجاری',other:'سایر / نامشخص'};
4 const COMPANY_SECTOR_LABELS={private:'خصوصی',cooperative:'تعاونی',public_non_government:'عمومی غیردولتی',government:'دولتی / حکومتی',other:'سایر'};
5 -function handleCompanyOrganizationRoleChange(){const role=document.getElementById('company-organization-role')?.value||'standalone',box=document.getElementById('company-parent-box'),sel=document.getElementById('company-parent-id');if(box)box.style.display=role==='subsidiary'?'':'none';if(sel){const current=sel.value,editing=editingCompanyId;sel.innerHTML='<option value="">— انتخاب شرکت مادر —</option>'+getMyCompanies().filter(c=>c.id!==editing&&(c.organizationRole==='holding_parent'||c.organizationRole==='standalone')).map(c=>'<option value="'+esc(c.id)+'">'+esc(c.name)+'</option>').join('');if([...sel.options].some(o=>o.value===current))sel.value=current;}}
5 +function handleCompanyOrganizationRoleChange(){const role=document.getElementById('company-organization-role')?.value||'standalone',box=document.getElementById('company-parent-box'),sel=document.getElementById('company-parent-id');if(box)box.style.display=role==='subsidiary'?'':'none';const parentReport=document.getElementById('company-parent-report-box'),childReport=document.getElementById('company-child-report-box');if(parentReport)parentReport.style.display=role==='holding_parent'?'':'none';if(childReport)childReport.style.display=role==='subsidiary'?'':'none';if(sel){const current=sel.value,editing=editingCompanyId;sel.innerHTML='<option value="">— انتخاب شرکت مادر —</option>'+getMyCompanies().filter(c=>c.id!==editing&&(c.organizationRole==='holding_parent'||c.organizationRole==='standalone')).map(c=>'<option value="'+esc(c.id)+'">'+esc(c.name)+'</option>').join('');if([...sel.options].some(o=>o.value===current))sel.value=current;}}
6 function handleCompanyEntityTypeChange(){
7 const entity=document.getElementById('company-entity-type'),sectorBox=document.getElementById('company-sector-box'),legalBox=document.getElementById('company-legal-form-box'),orgBox=document.getElementById('company-organization-box'),parentBox=document.getElementById('company-parent-box'),registrationBox=document.getElementById('company-registration-box'),sector=document.getElementById('company-sector'),legal=document.getElementById('company-legal-form'),orgRole=document.getElementById('company-organization-role'),parent=document.getElementById('company-parent-id'),reg=document.getElementById('company-reg-input'),nameLabel=document.getElementById('company-name-label'),nationalLabel=document.getElementById('company-national-label');
8 const isLegal=(entity?.value||'legal')==='legal';
@@ -26,6 +26,7 @@ function resetCompanyForm(){
26 const sector=document.getElementById('company-sector');if(sector)sector.value='private';
27 const legal=document.getElementById('company-legal-form');if(legal)legal.value='limited_liability';
28 handleCompanyEntityTypeChange();const orgRole=document.getElementById('company-organization-role');if(orgRole)orgRole.value='standalone';const parent=document.getElementById('company-parent-id');if(parent)parent.value='';handleCompanyOrganizationRoleChange();
29 + for(const id of ['company-parent-report-enabled','company-child-report-allowed']){const el=document.getElementById(id);if(el)el.checked=false;}
30 const activity=document.getElementById('company-activity-type');if(activity)activity.value='trading';
31 const template=document.getElementById('company-accounting-template');if(template){template.value='recommended';template.disabled=false;}
32 const saveBtn=document.getElementById('company-save-btn');
@@ -40,7 +41,10 @@ function editCompany(id){
41 editingCompanyId=id;
42 const values={'company-entity-type':comp.entity_type||'legal','company-sector':comp.organizationSector||'private','company-legal-form':comp.legalForm||'other','company-organization-role':comp.organizationRole||'standalone','company-parent-id':comp.parentCompanyId||'','company-activity-type':comp.activityType||'trading','company-name-input':comp.name||'','company-phone-input':comp.phone||'','company-national-input':comp.national_id||'','company-economic-input':comp.economic_code||'','company-reg-input':comp.reg_number||'','company-postal-input':comp.postal_code||'','company-address-input':comp.address||'','company-footer-input':comp.footer||''};
43 Object.keys(values).forEach(id=>{const el=document.getElementById(id);if(el)el.value=values[id];});
43 - handleCompanyEntityTypeChange();handleCompanyOrganizationRoleChange();const parentSel=document.getElementById('company-parent-id');if(parentSel&&comp.parentCompanyId)parentSel.value=comp.parentCompanyId;
44 + handleCompanyEntityTypeChange();handleCompanyOrganizationRoleChange();
45 + const groupCheck=document.getElementById('company-parent-report-enabled');if(groupCheck)groupCheck.checked=comp.showSubsidiaryOverview===true;
46 + const childCheck=document.getElementById('company-child-report-allowed');if(childCheck)childCheck.checked=comp.allowParentOverview===true;
47 + const parentSel=document.getElementById('company-parent-id');if(parentSel&&comp.parentCompanyId)parentSel.value=comp.parentCompanyId;
48 const template=document.getElementById('company-accounting-template');if(template){const hasChart=(datastore.accounts||[]).some(a=>a.ownerUserId===currentUser.id&&a.companyId===comp.id);template.value=comp.accountingTemplate?'recommended':'empty';template.disabled=!!comp.accountingTemplate||hasChart;}
49 const saveBtn=document.getElementById('company-save-btn');
50 if(saveBtn){saveBtn.innerHTML='💾 ذخیره اصلاحات';saveBtn.className='btn btn-primary';}
@@ -60,8 +64,13 @@ function commitSaveCompany(){
64 const organizationSector=entity_type==='legal'?(document.getElementById('company-sector')?.value||'private'):'private';
65 const legalForm=entity_type==='legal'?(document.getElementById('company-legal-form')?.value||'other'):'natural_person';
66 const organizationRole=entity_type==='legal'?(document.getElementById('company-organization-role')?.value||'standalone'):'standalone',parentCompanyId=entity_type==='legal'&&organizationRole==='subsidiary'?(document.getElementById('company-parent-id')?.value||''):'';if(entity_type==='legal'&&organizationRole==='subsidiary'&&!parentCompanyId){alert('برای شرکت تابعه، شرکت مادر را مشخص کنید.');return;}
67 + const parentGroup=getMyCompanies().find(x=>x.id===parentCompanyId);
68 + if(parentCompanyId&&(!parentGroup||!['holding_parent','standalone'].includes(parentGroup.organizationRole||'standalone')||parentGroup.id===editingCompanyId))return alert('شرکت مادر انتخاب‌شده معتبر نیست.');
69 const templateChoice=document.getElementById('company-accounting-template')?.value||'empty';
64 - const payload={entity_type,organizationSector,legalForm,organizationRole,parentCompanyId,activityType,name:formatEntityName(raw_name,entity_type),phone:document.getElementById('company-phone-input').value||'',national_id:document.getElementById('company-national-input').value||'',economic_code:document.getElementById('company-economic-input').value||'',reg_number:entity_type==='legal'?(document.getElementById('company-reg-input').value||''):'',postal_code:document.getElementById('company-postal-input').value||'',address:document.getElementById('company-address-input').value||'',footer:document.getElementById('company-footer-input')?.value||'از خرید شما سپاسگزاریم.'};
70 + const payload={entity_type,organizationSector,legalForm,organizationRole,parentCompanyId,
71 + showSubsidiaryOverview:organizationRole==='holding_parent'&&document.getElementById('company-parent-report-enabled')?.checked===true,
72 + allowParentOverview:organizationRole==='subsidiary'&&document.getElementById('company-child-report-allowed')?.checked===true,
73 + activityType,name:formatEntityName(raw_name,entity_type),phone:document.getElementById('company-phone-input').value||'',national_id:document.getElementById('company-national-input').value||'',economic_code:document.getElementById('company-economic-input').value||'',reg_number:entity_type==='legal'?(document.getElementById('company-reg-input').value||''):'',postal_code:document.getElementById('company-postal-input').value||'',address:document.getElementById('company-address-input').value||'',footer:document.getElementById('company-footer-input')?.value||'از خرید شما سپاسگزاریم.'};
74 if(payload.national_id&&getMyCompanies().some(x=>x.id!==editingCompanyId&&x.national_id===payload.national_id)){alert('شناسه ملی شرکت تکراری است.');return;}
75 if(editingCompanyId){
76 const comp=datastore.companies.find(c=>c.id===editingCompanyId&&c.ownerUserId===currentUser.id);
@@ -111,20 +120,37 @@ function renderCompanies(){
120 filterCompSelect.innerHTML=activeCompany?`<option value="${esc(activeCompany.id)}">فقط ${esc(activeCompany.name)}</option>`:'<option value="">شرکت فعالی انتخاب نشده است</option>';
121 filterCompSelect.disabled=true;
122 }
123 + renderCompanyGroupOverview();
124 +}
125 +function finoraGroupOverviewRows(){
126 + const companies=getMyCompanies(),parent=companies.find(x=>x.id===finoraActiveCompanyId());
127 + if(!parent||parent.organizationRole!=='holding_parent'||parent.showSubsidiaryOverview!==true)return [];
128 + const children=companies.filter(x=>x.organizationRole==='subsidiary'&&x.parentCompanyId===parent.id&&x.allowParentOverview===true);
129 + return [parent,...children].map(c=>({
130 + companyId:c.id,name:c.name,
131 + invoices:(datastore.invoices||[]).filter(x=>x.ownerUserId===currentUser?.id&&x.companyId===c.id).length,
132 + purchases:(datastore.purchases||[]).filter(x=>x.ownerUserId===currentUser?.id&&x.companyId===c.id).length
133 + }));
134 +}
135 +function renderCompanyGroupOverview(){
136 + const box=document.getElementById('company-group-overview');if(!box)return;
137 + const rows=finoraGroupOverviewRows();
138 + box.innerHTML=rows.length?'<div class="af-hint"><b>نمای گروهی صرفاً اطلاعاتی (بدون تجمیع دفاتر مالی)</b> — فقط شرکت‌های انتخاب‌شده و دارای رضایت نمایش داده می‌شوند.</div><table><thead><tr><th>شرکت</th><th>تعداد فاکتورهای فروش ثبت‌شده</th><th>تعداد فاکتورهای خرید ثبت‌شده</th></tr></thead><tbody>'+rows.map(x=>'<tr><td>'+esc(x.name)+'</td><td>'+Number(x.invoices).toLocaleString('fa-IR')+'</td><td>'+Number(x.purchases).toLocaleString('fa-IR')+'</td></tr>').join('')+'</tbody></table>':'';
139 }
140 function setDefaultCompany(id){
141 if(!requireWrite())return;
117 - const owned=getMyCompanies().find(c=>c.id===id);
118 - if(!owned){alert('شرکت انتخابی متعلق به حساب جاری نیست.');return;}
119 - const s=getMySettings();
120 - if(s.default_company_id!==owned.id){
121 - if(document.getElementById('edit-invoice-id')?.value&&typeof cancelInvoiceEdit==='function')cancelInvoiceEdit();
122 - if(document.getElementById('pur-edit-id')?.value&&typeof purCancelEdit==='function')purCancelEdit();
123 - s.default_company_id=owned.id;saveDatastore();
124 - }
125 - refreshAllSurfaces();
126 - const topbar=document.getElementById('topbar-company');
127 - if(topbar)topbar.textContent=owned.name||'انتخاب شرکت';
142 + if(!getMyCompanies().some(x=>x.id===id))return alert('شرکت انتخاب‌شده در این حساب موجود نیست.');
143 + if(typeof syncPending!=='undefined'&&(syncPending||syncRunning))return alert('ابتدا منتظر ثبت کامل تغییرات شرکت فعلی بمانید.');
144 + if(typeof cancelInvoiceEdit==='function')cancelInvoiceEdit();
145 + if(typeof purCancelEdit==='function')purCancelEdit();
146 + if(typeof resetPaymentForm==='function')resetPaymentForm();
147 + if(typeof cancelProductEdit==='function')cancelProductEdit(false);
148 + if(typeof resetCompanyForm==='function')resetCompanyForm();
149 + for(const fn of ['cancelContactEdit','cancelChequeEdit','cancelExpenseEdit'])if(typeof window[fn]==='function')window[fn]();
150 + // Quick edit dialogs from the previous company must not retain foreign IDs.
151 + document.querySelectorAll('.modal-backdrop.active').forEach(el=>el.classList.remove('active'));
152 + const s=getMySettings();s.default_company_id=id;saveDatastore();refreshAllSurfaces();
153 + const topbar=document.getElementById('topbar-company');if(topbar)topbar.textContent=getMyCompanies().find(c=>c.id===id)?.name||'انتخاب شرکت';
154 if(typeof renderDocumentManagement==='function')renderDocumentManagement();
155 }
156 function deleteCompany(id){