@davoodmehraban89 / Factor-easy / commits / d10d4bd
Fork 0

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

Protect projects referenced by financial documents

davoodmehraban89 committed Sep 28, 2026 at 13:41 UTC d10d4bde8771bf793e7d57b0ac57f859aea3a85a
1 file changed +17 -2
js/projects.js
+17 -2
@@ -4,10 +4,25 @@ function projectDirectionLabel(d){return d==='sale'?'فروش':d==='purchase'?'
4 function askProjectDirection(current='both'){const v=(prompt('نوع پروژه: sale = فروش، purchase = خرید، both = هر دو',current)||current).trim().toLowerCase();return ['sale','purchase','both'].includes(v)?v:'both';}
5 function addProjectToContact(contactId){if(!requireWrite())return;const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;const name=prompt('نام پروژه جدید را وارد کنید:');if(!name||!name.trim())return;getContactProjects(contactId).push({id:'PRJ_'+Date.now(),name:name.trim(),direction:askProjectDirection(),subprojects:[]});c.project_mode='multi';saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
6 function editContactProject(contactId,projectId){if(!requireWrite())return;const p=getContactProjects(contactId).find(x=>x.id===projectId);if(!p)return;const n=prompt('نام پروژه:',p.name);if(!n||!n.trim())return;p.name=n.trim();p.direction=askProjectDirection(p.direction);saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
7 -function deleteContactProject(contactId,projectId){if(!requireWrite()||!confirm('پروژه و همه زیرپروژه‌های آن حذف شود؟'))return;const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;c.projects=getContactProjects(contactId).filter(x=>x.id!==projectId);saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
7 +function deleteContactProject(contactId,projectId){
8 + if(!requireWrite())return;
9 + const p=getContactProjects(contactId).find(x=>x.id===projectId);if(!p)return;
10 + const ids=[projectId,...(p.subprojects||[]).map(s=>s.id)];
11 + const usedSale=getMyInvoices().some(i=>ids.includes(i.projectId));
12 + const usedBuy=(datastore.purchases||[]).some(x=>x.ownerUserId===currentUser?.id&&ids.includes(x.costCenterId));
13 + if(usedSale||usedBuy){alert('این پروژه یا یکی از زیرپروژه‌های آن در سند مالی استفاده شده و برای حفظ سابقه قابل حذف نیست.');return;}
14 + if(!confirm('پروژه و همه زیرپروژه‌های آن حذف شود؟'))return;
15 + const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;c.projects=getContactProjects(contactId).filter(x=>x.id!==projectId);saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);
16 +}
17 function addSubproject(contactId,projectId){if(!requireWrite())return;const p=getContactProjects(contactId).find(x=>x.id===projectId);if(!p)return;const n=prompt('نام زیرپروژه:');if(!n||!n.trim())return;p.subprojects.push({id:'SUB_'+Date.now(),name:n.trim()});saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
18 function editSubproject(contactId,projectId,subId){if(!requireWrite())return;const p=getContactProjects(contactId).find(x=>x.id===projectId),s=p?.subprojects?.find(x=>x.id===subId);if(!s)return;const n=prompt('نام زیرپروژه:',s.name);if(!n||!n.trim())return;s.name=n.trim();saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
10 -function deleteSubproject(contactId,projectId,subId){if(!requireWrite()||!confirm('زیرپروژه حذف شود؟'))return;const p=getContactProjects(contactId).find(x=>x.id===projectId);if(!p)return;p.subprojects=p.subprojects.filter(x=>x.id!==subId);saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);}
19 +function deleteSubproject(contactId,projectId,subId){
20 + if(!requireWrite())return;
21 + const usedSale=getMyInvoices().some(i=>i.projectId===subId);
22 + const usedBuy=(datastore.purchases||[]).some(x=>x.ownerUserId===currentUser?.id&&x.costCenterId===subId);
23 + if(usedSale||usedBuy){alert('این زیرپروژه در سند مالی استفاده شده و برای حفظ سابقه قابل حذف نیست.');return;}
24 + if(!confirm('زیرپروژه حذف شود؟'))return;const p=getContactProjects(contactId).find(x=>x.id===projectId);if(!p)return;p.subprojects=p.subprojects.filter(x=>x.id!==subId);saveDatastore();refreshAllSurfaces();manageContactProjects(contactId);
25 +}
26 function ensureProjectModal(){if(document.getElementById('project-manager-modal'))return;const d=document.createElement('div');d.id='project-manager-modal';d.className='modal-backdrop';d.innerHTML='<div class="modal-card" style="max-width:900px;max-height:85vh;overflow:auto"><div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px"><h3 id="project-manager-title"></h3><button class="btn btn-secondary btn-inline" onclick="document.getElementById(\'project-manager-modal\').classList.remove(\'active\')">✕ بستن</button></div><div id="project-manager-body"></div></div>';document.body.appendChild(d);}
27 function manageContactProjects(contactId){const c=getMyContacts().find(x=>x.id===contactId);if(!c)return;ensureProjectModal();document.getElementById('project-manager-title').innerText='پروژه‌ها و زیرپروژه‌های '+c.name;const ps=getContactProjects(contactId);const rows=ps.length?ps.map(p=>`<tr><td><b>${esc(p.name)}</b><div style="font-size:11px;color:var(--text-muted)">${projectDirectionLabel(p.direction)}</div></td><td>${p.subprojects.length?p.subprojects.map(s=>`<div style="padding:5px 0;border-bottom:1px solid #eee;display:flex;justify-content:space-between;gap:8px"><span>${esc(s.name)}</span><span><button class="btn btn-secondary btn-inline" onclick="editSubproject('${contactId}','${p.id}','${s.id}')">اصلاح</button> <button class="btn btn-danger btn-inline" onclick="deleteSubproject('${contactId}','${p.id}','${s.id}')">حذف</button></span></div>`).join(''):'—'}</td><td><button class="btn btn-secondary btn-inline" onclick="addSubproject('${contactId}','${p.id}')">➕ زیرپروژه</button> <button class="btn btn-secondary btn-inline" onclick="editContactProject('${contactId}','${p.id}')">✏️ اصلاح</button> <button class="btn btn-danger btn-inline" onclick="deleteContactProject('${contactId}','${p.id}')">حذف</button></td></tr>`).join(''):'<tr><td colspan="3" style="text-align:center;padding:18px">پروژه‌ای تعریف نشده است.</td></tr>';document.getElementById('project-manager-body').innerHTML=`<button class="btn btn-primary btn-inline" style="margin-bottom:12px" onclick="addProjectToContact('${contactId}')">➕ پروژه جدید</button><div class="table-responsive"><table><thead><tr><th>پروژه</th><th>زیرپروژه‌ها</th><th>عملیات</th></tr></thead><tbody>${rows}</tbody></table></div>`;document.getElementById('project-manager-modal').classList.add('active');}
28 function handleInvoiceContactChange(){const contactId=document.getElementById('invoice-contact-id')?.value,c=getMyContacts().find(x=>x.id===contactId),row=document.getElementById('box-invoice-project-row'),sel=document.getElementById('invoice-project-id');if(!row||!sel)return;const opts=[];if(c&&c.project_mode==='multi')getContactProjects(contactId).filter(p=>p.direction!=='purchase').forEach(p=>{opts.push(`<option value="${p.id}">${esc(p.name)}</option>`);p.subprojects.forEach(s=>opts.push(`<option value="${s.id}">↳ ${esc(p.name)} / ${esc(s.name)}</option>`));});row.style.display=opts.length?'flex':'none';sel.innerHTML=opts.join('');}