سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 09:15 UTC
fix(company): export active company only and reject importing foreign-company records
davoodmehraban89 committed
Oct 14, 2026 at 11:53 UTC
517d26e824acacc5f6c581b87b15e401686d8a46
1 file changed
+11
-4
js/phase5-enterprise.js
+11
-4
@@ -99,14 +99,21 @@ function p5SaveCostCenter(){
99
const cid=p5Need();if(!cid)return;const code=document.getElementById('p5-cc-code').value.trim(),name=document.getElementById('p5-cc-name').value.trim();if(!code||!name)return alert('کد و عنوان مرکز هزینه الزامی است.');if(getMyCostCenters().some(x=>x.code===code))return alert('کد تکراری است.');const r={id:p5Id('CC'),ownerUserId:currentUser.id,companyId:cid,code,name,active:true};datastore.costCenters.push(r);p5Audit('create','costCenter',r.id,null,r);saveDatastore();p5RenderDataCenter();
100
}
101
function p5SaveCompanyOpsSettings(){const cid=p5Need();if(!cid)return;const s=getMySettings(),warehouseId=document.getElementById('p5-set-default-wh')?.value||'',baseCurrencyCode=document.getElementById('p5-set-base-cur')?.value||'IRR',inventoryCostMethod=document.getElementById('p5-set-cost-method')?.value||'weighted_average';if(!getMyWarehouses().some(x=>x.id===warehouseId))return alert('انبار پیشفرض معتبر نیست.');if(!getMyCurrencies().some(x=>x.code===baseCurrencyCode))return alert('ارز پایه معتبر نیست.');if((s.baseCurrencyCode||'IRR')!==baseCurrencyCode&&getMyExchangeRates().length)return alert('پس از ثبت نرخ ارز، تغییر مستقیم ارز پایه مجاز نیست؛ ابتدا باید مهاجرت کنترلشده نرخها انجام شود.');const before={defaultWarehouseId:s.defaultWarehouseId||'',baseCurrencyCode:s.baseCurrencyCode||'IRR',inventoryCostMethod:s.inventoryCostMethod||'weighted_average'};Object.assign(s,{defaultWarehouseId:warehouseId,baseCurrencyCode,inventoryCostMethod});p5Audit('update','companyOpsSettings',cid,before,{defaultWarehouseId,baseCurrencyCode,inventoryCostMethod});saveDatastore();p5PopulateWarehouseSelects();p5RenderDataCenter()}
102
-function p5BackupPayload(){const o={formatVersion:5,user:currentUser?.username,exportedAt:new Date().toISOString()};COLLS.forEach(k=>o[k]=(datastore[k]||[]).filter(x=>x.ownerUserId===currentUser?.id).map(x=>{const y={...x};delete y.ownerUserId;return y}));return o}
103
-function p5ExportBackup(){const blob=new Blob([JSON.stringify(p5BackupPayload(),null,2)],{type:'application/json'}),a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download='finora-full-backup-'+Date.now()+'.json';a.click();setTimeout(()=>URL.revokeObjectURL(a.href),500)}
102
+function p5BackupPayload(){
103
+ const cid=afCompanyId(),single=getMyCompanies().length===1,
104
+ o={formatVersion:5,user:currentUser?.username,exportedAt:new Date().toISOString()};
105
+ COLLS.forEach(k=>o[k]=(datastore[k]||[]).filter(x=>x.ownerUserId===currentUser?.id&&
106
+ (k==='companies'?x.id===cid:k==='settings'?true:x.companyId===cid||(single&&!x.companyId)))
107
+ .map(x=>{const y={...x};delete y.ownerUserId;return y}));
108
+ return o;
109
+}
110
+function p5ExportBackup(){const blob=new Blob([JSON.stringify(p5BackupPayload(),null,2)],{type:'application/json'}),a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download='finora-company-'+afCompanyId()+'-'+Date.now()+'.json';a.click();setTimeout(()=>URL.revokeObjectURL(a.href),500)}
111
function p5PreviewRestore(event){
105
- const file=event.target.files[0];if(!file)return;if(file.size>20*1024*1024){event.target.value='';return document.getElementById('p5-import-preview').textContent='نامعتبر: حجم فایل پشتیبان نباید بیشتر از ۲۰ مگابایت باشد.'}const r=new FileReader();r.onload=()=>{try{const p=JSON.parse(r.result);P5Domain.validateRestore(p,COLLS);const counts=COLLS.filter(k=>Array.isArray(p[k])&&p[k].length).map(k=>k+': '+p[k].length);window.p5PendingRestore=p;document.getElementById('p5-import-preview').innerHTML='پیشنمایش معتبر — '+esc(counts.join(' | '))+' <button class="btn btn-danger btn-inline" onclick="p5ApplyRestore()">اعمال ادغام امن</button>';datastore.importBatches.push({id:p5Id('IMP'),ownerUserId:currentUser.id,companyId:afCompanyId(),fileName:file.name,mode:'preview',counts:Object.fromEntries(COLLS.map(k=>[k,(p[k]||[]).length])),status:'validated',createdAt:new Date().toISOString()});saveDatastore()}catch(e){document.getElementById('p5-import-preview').textContent='نامعتبر: '+e.message}};r.readAsText(file)
112
+ const file=event.target.files[0];if(!file)return;if(file.size>20*1024*1024){event.target.value='';return document.getElementById('p5-import-preview').textContent='نامعتبر: حجم فایل پشتیبان نباید بیشتر از ۲۰ مگابایت باشد.'}const r=new FileReader();r.onload=()=>{try{const p=JSON.parse(r.result);P5Domain.validateRestore(p,COLLS);finoraValidateImportCompanyScope(p);const counts=COLLS.filter(k=>Array.isArray(p[k])&&p[k].length).map(k=>k+': '+p[k].length);window.p5PendingRestore=p;document.getElementById('p5-import-preview').innerHTML='پیشنمایش معتبر — '+esc(counts.join(' | '))+' <button class="btn btn-danger btn-inline" onclick="p5ApplyRestore()">اعمال ادغام امن</button>';datastore.importBatches.push({id:p5Id('IMP'),ownerUserId:currentUser.id,companyId:afCompanyId(),fileName:file.name,mode:'preview',counts:Object.fromEntries(COLLS.map(k=>[k,(p[k]||[]).length])),status:'validated',createdAt:new Date().toISOString()});saveDatastore()}catch(e){document.getElementById('p5-import-preview').textContent='نامعتبر: '+e.message}};r.readAsText(file)
113
}
114
function p5ApplyRestore(){
115
const p=window.p5PendingRestore;if(!p||!currentUser||!requireWrite())return;if(!confirm('فایل اعتبارسنجی شده با دادههای حساب شما ادغام شود؟ سابقه قطعی موجود بازنویسی نمیشود.'))return;
109
- try{const n=absorbRecords(p);datastore.importBatches.push({id:p5Id('IMP'),ownerUserId:currentUser.id,companyId:afCompanyId(),fileName:'validated-backup.json',mode:'merge',counts:{records:n},status:'applied',createdAt:new Date().toISOString()});p5Audit('restore','backup','merge',null,{records:n});saveDatastore();window.p5PendingRestore=null;refreshAllSurfaces();alert(n.toLocaleString('fa-IR')+' رکورد با موفقیت ادغام شد.')}catch(e){alert('بازگردانی انجام نشد: '+e.message)}
116
+ try{finoraValidateImportCompanyScope(p);const n=absorbRecords(p);datastore.importBatches.push({id:p5Id('IMP'),ownerUserId:currentUser.id,companyId:afCompanyId(),fileName:'validated-backup.json',mode:'merge',counts:{records:n},status:'applied',createdAt:new Date().toISOString()});p5Audit('restore','backup','merge',null,{records:n});saveDatastore();window.p5PendingRestore=null;refreshAllSurfaces();alert(n.toLocaleString('fa-IR')+' رکورد با موفقیت ادغام شد.')}catch(e){alert('بازگردانی انجام نشد: '+e.message)}
117
}
118
function p5ValidateIntegrationEndpoint(raw){
119
const value=String(raw||'').trim();if(!value)return '';let u;try{u=new URL(value)}catch(_){throw new Error('نشانی اتصال معتبر نیست.')}if(u.protocol!=='https:')throw new Error('نشانی اتصال باید HTTPS باشد.');if(u.username||u.password)throw new Error('قرار دادن نام کاربری/رمز در URL اتصال مجاز نیست.');const h=u.hostname.toLowerCase(),privateHost=h==='localhost'||h==='::1'||h.endsWith('.local')||/^127\./.test(h)||/^10\./.test(h)||/^192\.168\./.test(h)||/^169\.254\./.test(h)||/^172\.(1[6-9]|2\d|3[01])\./.test(h);if(privateHost)throw new Error('Endpoint شبکه خصوصی/محلی برای اتصال خارجی مجاز نیست.');return u.href;