@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 23d595f
Fork 0

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

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

Synced Dec 3, 17:00 UTC

Add remaining small js/ modules: products, contacts, cheques, expenses, companies, backup, words, bootstrap (part 4/4)

davoodmehraban89 committed Sep 26, 2026 at 13:01 UTC 23d595f463c7f02f038ddfa657fd4436e20d3c73
8 files changed +325
js/backup.js new
+27
@@ -0,0 +1,27 @@
1 +function exportDataBlob(){
2 + const userExport={user:currentUser?.username,exportedAt:new Date().toISOString(),companies:getMyCompanies(),contacts:getMyContacts(),products:getMyProducts(),invoices:getMyInvoices(),cheques:getMyCheques(),expenses:getMyExpenses(),settings:getMySettings()};
3 + const blobString="data:text/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(userExport,null,2));
4 + const anchor=document.createElement('a');
5 + anchor.setAttribute("href",blobString);
6 + anchor.setAttribute("download",`finora-${currentUser?.username||'data'}-${Date.now()}.json`);
7 + document.body.appendChild(anchor);
8 + anchor.click();
9 + anchor.remove();
10 +}
11 +function importDataBlob(event){
12 + if(!requireWrite())return;
13 + if(!currentUser)return;
14 + const file=event.target.files[0];
15 + if(!file)return;
16 + const reader=new FileReader();
17 + reader.onload=function(e){
18 + try{
19 + const imported=JSON.parse(e.target.result);
20 + if(!imported||typeof imported!=='object')throw new Error('bad');
21 + absorbRecords(imported);
22 + saveDatastore();refreshAllSurfaces();
23 + alert('اطلاعات با موفقیت به حساب کاربری شما اضافه شد.');
24 + }catch(err){alert('فایل نامعتبر است.');}
25 + };
26 + reader.readAsText(file);
27 +}
js/bootstrap.js new
+1
@@ -0,0 +1 @@
1 +window.addEventListener('DOMContentLoaded',()=>{initAuthSystem();});
js/cheques.js new
+20
@@ -0,0 +1,20 @@
1 +function commitSaveCheque(){
2 + if(!requireWrite())return;
3 + if(!currentUser)return;
4 + const direction=document.getElementById('chq-direction-input').value;
5 + const contactSelect=document.getElementById('chq-contact-select');
6 + const contactName=contactSelect.options[contactSelect.selectedIndex]?.text||'';
7 + const sayad_id=document.getElementById('chq-sayad-input').value;
8 + const bank=document.getElementById('chq-bank-input').value;
9 + const amount=parseFloat(document.getElementById('chq-amount-input').value)||0;
10 + if(!sayad_id||amount<=0){alert('اطلاعات چک نامعتبر است.');return;}
11 + datastore.cheques.push({id:'CHQ_'+Date.now(),ownerUserId:currentUser.id,direction,contactName,sayad_id,bank,amount,due_date:document.getElementById('chq-due-input').value,status:document.getElementById('chq-status-input').value});
12 + saveDatastore();
13 + alert('چک ثبت شد.');
14 + refreshAllSurfaces();
15 +}
16 +function renderCheques(){
17 + const tbody=document.getElementById('cheques-ledger-table-body');
18 + const myCheques=getMyCheques();
19 + tbody.innerHTML=myCheques.length===0?'<tr><td colspan="6" style="text-align:center;padding:20px;color:var(--text-muted)">چکی ثبت نشده است.</td></tr>':myCheques.map(c=>`<tr><td>${c.direction==='inbound'?'دریافتی':'پرداختی'}</td><td>${esc(c.contactName)}</td><td>${esc(c.bank)} - ${esc(c.sayad_id)}</td><td>${c.amount.toLocaleString('fa-IR')}</td><td>${esc(c.due_date)}</td><td>${esc(c.status)}</td></tr>`).join('');
20 +}
js/companies.js new
+51
@@ -0,0 +1,51 @@
1 +function commitSaveCompany(){
2 + if(!requireWrite())return;
3 + if(!currentUser)return;
4 + const entity_type=document.getElementById('company-entity-type').value;
5 + const raw_name=document.getElementById('company-name-input').value;
6 + if(!raw_name.trim()){alert('نام شرکت الزامی است.');return;}
7 + const newComp={id:'COMP_'+Date.now(),ownerUserId:currentUser.id,entity_type,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: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||'از خرید شما سپاسگزاریم.'};
8 + datastore.companies.push(newComp);
9 + const s=getMySettings();
10 + s.default_company_id=newComp.id;
11 + saveDatastore();
12 + alert('شرکت با موفقیت ثبت و ذخیره شد و به عنوان فروشنده پیش‌فرض تنظیم گردید.');
13 + ['company-name-input','company-phone-input','company-national-input','company-economic-input','company-reg-input','company-postal-input','company-address-input','company-footer-input'].forEach(id=>{const el=document.getElementById(id);if(el)el.value='';});
14 + refreshAllSurfaces();
15 +}
16 +function renderCompanies(){
17 + const tbody=document.getElementById('companies-table-body');
18 + const invoiceCompSelect=document.getElementById('invoice-company-id');
19 + const filterCompSelect=document.getElementById('filter-invoice-company');
20 + const myCompanies=getMyCompanies();
21 + const mySettings=getMySettings();
22 + const defCompId=mySettings.default_company_id||(myCompanies[0]?.id);
23 + if(tbody){
24 + if(myCompanies.length===0){
25 + tbody.innerHTML='<tr><td colspan="5" style="text-align:center;color:var(--text-muted);padding:20px">هیچ شرکتی ثبت نشده است. لطفاً ابتدا شرکت خود را ثبت کنید.</td></tr>';
26 + }else{
27 + tbody.innerHTML=myCompanies.map(c=>`<tr><td><strong>${esc(c.name)}</strong></td><td>${esc(c.national_id||'—')}</td><td>${esc(c.economic_code||'—')}</td><td>${c.id===defCompId?'<span class="badge badge-success">پیش‌فرض</span>':`<button class="btn btn-secondary btn-inline" style="padding:2px 8px;font-size:11px" onclick="setDefaultCompany('${c.id}')">انتخاب</button>`}</td><td>${myCompanies.length>1?`<button class="btn btn-danger btn-inline" style="padding:2px 8px;font-size:11px" onclick="deleteCompany('${c.id}')">حذف</button>`:'—'}</td></tr>`).join('');
28 + }
29 + }
30 + if(invoiceCompSelect){
31 + invoiceCompSelect.innerHTML=myCompanies.map(c=>`<option value="${c.id}" ${c.id===defCompId?'selected':''}>${esc(c.name)}</option>`).join('');
32 + if(defCompId)invoiceCompSelect.value=defCompId;
33 + }
34 + if(filterCompSelect){
35 + const currentVal=filterCompSelect.value||'ALL';
36 + filterCompSelect.innerHTML=`<option value="ALL">نمایش همه شرکت‌ها</option>`+myCompanies.map(c=>`<option value="${c.id}">فقط ${esc(c.name)}</option>`).join('');
37 + filterCompSelect.value=currentVal;
38 + }
39 +}
40 +function setDefaultCompany(id){if(!requireWrite())return;const s=getMySettings();s.default_company_id=id;saveDatastore();refreshAllSurfaces();}
41 +function deleteCompany(id){
42 + if(!requireWrite())return;
43 + if(!currentUser)return;
44 + if(confirm('حذف شود؟')){
45 + datastore.companies=datastore.companies.filter(c=>!(c.id===id&&c.ownerUserId===currentUser.id));
46 + const myCompanies=getMyCompanies();
47 + const s=getMySettings();
48 + if(s.default_company_id===id)s.default_company_id=myCompanies[0]?.id||'';
49 + saveDatastore();refreshAllSurfaces();
50 + }
51 +}
js/contacts.js new
+90
@@ -0,0 +1,90 @@
1 +function commitSaveContact(){
2 + if(!requireWrite())return;
3 + if(!currentUser)return;
4 + const entity_type=document.getElementById('contact-entity-type').value;
5 + const custom_prefix=entity_type==='natural'?document.getElementById('contact-prefix-select')?.value:'';
6 + const raw_name=document.getElementById('contact-name-input').value;
7 + const role=document.getElementById('contact-role-input').value;
8 + const mobile=document.getElementById('contact-mobile-input').value;
9 + const national_id=document.getElementById('contact-national-input').value;
10 + const economic_code=(entity_type==='natural')?'':document.getElementById('contact-economic-input').value;
11 + const reg_number=(entity_type==='natural')?'':document.getElementById('contact-reg-input').value;
12 + const postal_code=document.getElementById('contact-postal-input').value;
13 + const address=document.getElementById('contact-address-input').value;
14 + if(!raw_name.trim()){alert('نام طرف حساب الزامی است.');return;}
15 + const formatted_name=formatEntityName(raw_name,entity_type,custom_prefix);
16 + datastore.contacts.push({id:'C_'+Date.now(),ownerUserId:currentUser.id,entity_type,name:formatted_name,role,mobile,national_id,economic_code,reg_number,postal_code,address,balance:0});
17 + saveDatastore();
18 + alert('طرف حساب ثبت شد.');
19 + document.getElementById('contact-name-input').value='';
20 + refreshAllSurfaces();
21 +}
22 +function renderContacts(){
23 + const tbody=document.getElementById('contacts-ledger-table-body');
24 + const invSelect=document.getElementById('invoice-contact-id');
25 + const chqSelect=document.getElementById('chq-contact-select');
26 + const myContacts=getMyContacts();
27 + if(myContacts.length===0){
28 + tbody.innerHTML='<tr><td colspan="8" style="text-align:center;color:var(--text-muted);padding:20px">هیچ طرف حسابی ثبت نشده است.</td></tr>';
29 + }else{
30 + tbody.innerHTML=myContacts.map(c=>`<tr><td><strong>${esc(c.name)}</strong></td><td><span class="badge ${c.entity_type==='legal'?'badge-warning':'badge-success'}">${c.entity_type==='legal'?'حقوقی':'حقیقی'}</span></td><td>${esc(c.national_id||'—')}</td><td>${esc(c.economic_code||'—')}</td><td>${esc(c.postal_code||'—')}</td><td>${esc(c.mobile||'—')}</td><td>${Math.abs(c.balance||0).toLocaleString('fa-IR')}</td><td><button class="btn btn-danger btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="deleteContact('${c.id}')">حذف</button></td></tr>`).join('');
31 + }
32 + const options=myContacts.map(c=>`<option value="${c.id}">${esc(c.name)}</option>`).join('');
33 + if(invSelect)invSelect.innerHTML=options;
34 + if(chqSelect)chqSelect.innerHTML=options;
35 +}
36 +function deleteContact(id){
37 + if(!requireWrite())return;
38 + if(!currentUser)return;
39 + if(confirm('حذف شود؟')){
40 + datastore.contacts=datastore.contacts.filter(c=>!(c.id===id&&c.ownerUserId===currentUser.id));
41 + saveDatastore();refreshAllSurfaces();
42 + }
43 +}
44 +function openQuickContactModal(){document.getElementById('modal-quick-contact').classList.add('active');handleQuickCEntityChange();}
45 +function closeQuickContactModal(){document.getElementById('modal-quick-contact').classList.remove('active');}
46 +function saveQuickContact(){
47 + if(!requireWrite())return;
48 + if(!currentUser)return;
49 + const entity_type=document.getElementById('quick-c-entity').value;
50 + const prefix=entity_type==='natural'?document.getElementById('quick-c-prefix')?.value:'';
51 + const raw_name=document.getElementById('quick-c-name').value;
52 + if(!raw_name.trim()){alert('نام الزامی است.');return;}
53 + const newId='C_'+Date.now();
54 + datastore.contacts.push({id:newId,ownerUserId:currentUser.id,entity_type,name:formatEntityName(raw_name,entity_type,prefix),role:'customer',mobile:document.getElementById('quick-c-mobile').value,national_id:document.getElementById('quick-c-national').value,economic_code:(entity_type==='natural')?'':document.getElementById('quick-c-economic').value,reg_number:(entity_type==='natural')?'':document.getElementById('quick-c-reg').value,postal_code:document.getElementById('quick-c-postal').value,address:document.getElementById('quick-c-address').value,balance:0});
55 + saveDatastore();
56 + closeQuickContactModal();
57 + refreshAllSurfaces();
58 + document.getElementById('invoice-contact-id').value=newId;
59 +}
60 +function downloadContactsExcelTemplate(){
61 + const ws_data=[["نام شخص یا شرکت","نوع (حقیقی/حقوقی)","شماره همراه","شناسه یا کد ملی","کد اقتصادی","شماره ثبت","کد پستی","نشانی"],["دانشگاه صنعتی شریف","حقوقی","02166165000","14002830256","14002830256","","1458889694","طرشت محله تیموری پلاک 435"]];
62 + const ws=XLSX.utils.aoa_to_sheet(ws_data);
63 + const wb=XLSX.utils.book_new();
64 + XLSX.utils.book_append_sheet(wb,ws,"اشخاص");
65 + XLSX.writeFile(wb,"نمونه_اشخاص_فینورا.xlsx");
66 +}
67 +function importContactsFromExcel(event){
68 + if(!requireWrite())return;
69 + if(!currentUser)return;
70 + const file=event.target.files[0];
71 + if(!file)return;
72 + const reader=new FileReader();
73 + reader.onload=function(e){
74 + try{
75 + const data=new Uint8Array(e.target.result);
76 + const workbook=XLSX.read(data,{type:'array'});
77 + const rows=XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);
78 + rows.forEach((r,idx)=>{
79 + const raw_name=r["نام شخص یا شرکت"]||r["نام"];
80 + if(raw_name){
81 + const entity_type=(r["نوع (حقیقی/حقوقی)"]||'').includes('حقوقی')?'legal':'natural';
82 + datastore.contacts.push({id:'C_'+Date.now()+'_'+idx,ownerUserId:currentUser.id,entity_type,name:formatEntityName(raw_name.toString(),entity_type,'آقا/خانم'),role:'customer',mobile:(r["شماره همراه"]||'').toString(),national_id:(r["شناسه یا کد ملی"]||'').toString(),economic_code:(entity_type==='legal'?(r["کد اقتصادی"]||''):'').toString(),reg_number:(entity_type==='legal'?(r["شماره ثبت"]||''):'').toString(),postal_code:(r["کد پستی"]||'').toString(),address:(r["نشانی"]||'').toString(),balance:0});
83 + }
84 + });
85 + saveDatastore();refreshAllSurfaces();
86 + alert('اشخاص ایمپورت شدند.');
87 + }catch(err){alert('خطا در فایل اکسل!');}
88 + };
89 + reader.readAsArrayBuffer(file);
90 +}
js/expenses.js new
+15
@@ -0,0 +1,15 @@
1 +function commitSaveExpense(){
2 + if(!requireWrite())return;
3 + if(!currentUser)return;
4 + const amount=parseFloat(document.getElementById('trx-amount-input').value)||0;
5 + if(amount<=0){alert('مبلغ معتبر نیست.');return;}
6 + datastore.expenses.push({id:'TRX_'+Date.now(),ownerUserId:currentUser.id,kind:document.getElementById('trx-kind-input').value,category:document.getElementById('trx-category-input').value,amount,desc:document.getElementById('trx-desc-input').value});
7 + saveDatastore();
8 + alert('سند ثبت شد.');
9 + refreshAllSurfaces();
10 +}
11 +function renderExpenses(){
12 + const tbody=document.getElementById('expenses-ledger-table-body');
13 + const myExpenses=getMyExpenses();
14 + tbody.innerHTML=myExpenses.length===0?'<tr><td colspan="4" style="text-align:center;padding:20px;color:var(--text-muted)">موردی ثبت نشده است.</td></tr>':myExpenses.map(e=>`<tr><td>${esc(e.kind)}</td><td>${esc(e.category)}</td><td>${esc(e.desc)}</td><td>${e.amount.toLocaleString('fa-IR')}</td></tr>`).join('');
15 +}
js/products.js new
+85
@@ -0,0 +1,85 @@
1 +function commitSaveProduct(){
2 + if(!requireWrite())return;
3 + if(!currentUser)return;
4 + const myProducts=getMyProducts();
5 + const code=document.getElementById('prod-code-input').value||(myProducts.length+1).toString();
6 + const name=document.getElementById('prod-name-input').value;
7 + const spec=document.getElementById('prod-spec-input').value||'';
8 + const unit=document.getElementById('prod-unit-input').value||'عدد';
9 + const buy_price=parseFloat(document.getElementById('prod-buy-input').value)||0;
10 + const sale_price=parseFloat(document.getElementById('prod-sale-input').value)||0;
11 + const internal_id=document.getElementById('prod-internal-id-input').value||'';
12 + if(!name){alert('عنوان کالا الزامی است.');return;}
13 + datastore.products.push({id:'P_'+Date.now(),ownerUserId:currentUser.id,code,name,spec,type:'good',unit,buy_price,sale_price,stock:0,internal_id});
14 + saveDatastore();
15 + alert('کالا ثبت شد.');
16 + document.getElementById('prod-name-input').value='';
17 + document.getElementById('prod-spec-input').value='';
18 + document.getElementById('prod-internal-id-input').value='';
19 + refreshAllSurfaces();
20 +}
21 +function renderProducts(){
22 + const tbody=document.getElementById('products-catalog-table-body');
23 + const myProducts=getMyProducts();
24 + if(myProducts.length===0){tbody.innerHTML='<tr><td colspan="7" style="text-align:center;color:var(--text-muted);padding:20px">هیچ کالایی ثبت نشده است.</td></tr>';return;}
25 + tbody.innerHTML=myProducts.map(p=>`<tr><td>${esc(p.code)}</td><td><strong>${esc(p.name)}</strong></td><td>${esc(p.spec||'—')}</td><td>${esc(p.unit)}</td><td>${p.sale_price.toLocaleString('fa-IR')}</td><td>${esc(p.internal_id||'—')}</td><td><button class="btn btn-danger btn-inline" style="padding:4px 8px;font-size:12px;min-height:30px" onclick="deleteProduct('${p.id}')">حذف</button></td></tr>`).join('');
26 +}
27 +function deleteProduct(id){
28 + if(!requireWrite())return;
29 + if(!currentUser)return;
30 + if(confirm('حذف شود؟')){
31 + datastore.products=datastore.products.filter(p=>!(p.id===id&&p.ownerUserId===currentUser.id));
32 + saveDatastore();refreshAllSurfaces();
33 + }
34 +}
35 +function openQuickProductModal(){document.getElementById('modal-quick-product').classList.add('active');}
36 +function closeQuickProductModal(){document.getElementById('modal-quick-product').classList.remove('active');}
37 +function saveQuickProduct(){
38 + if(!requireWrite())return;
39 + if(!currentUser)return;
40 + const myProducts=getMyProducts();
41 + const code=document.getElementById('quick-p-code').value||(myProducts.length+1).toString();
42 + const name=document.getElementById('quick-p-name').value;
43 + const spec=document.getElementById('quick-p-spec').value||'';
44 + const unit=document.getElementById('quick-p-unit').value||'عدد';
45 + const price=parseFloat(document.getElementById('quick-p-price').value)||0;
46 + const internal_id=document.getElementById('quick-p-internal-id').value||'';
47 + if(!name){alert('عنوان کالا الزامی است.');return;}
48 + const newId='P_'+Date.now();
49 + datastore.products.push({id:newId,ownerUserId:currentUser.id,code,name,spec,unit,buy_price:0,sale_price:price,stock:0,type:'good',internal_id});
50 + saveDatastore();
51 + closeQuickProductModal();
52 + refreshAllSurfaces();
53 + addInvoiceItemRow(newId,1,price);
54 +}
55 +function downloadProductsExcelTemplate(){
56 + const ws_data=[["کد کالا","نام کالا","مشخصه و نوع","واحد","قیمت فروش (ریال)","شناسه کالا/خدمت داخلی"],["1","سوئیچ شبکه 24 پورت","با 2 پاور","عدد",2180000000,""],["2","هارد 2.5 اینچ سرور","","عدد",37000000,""]];
57 + const ws=XLSX.utils.aoa_to_sheet(ws_data);
58 + const wb=XLSX.utils.book_new();
59 + XLSX.utils.book_append_sheet(wb,ws,"کالاها");
60 + XLSX.writeFile(wb,"نمونه_کالاها_فینورا.xlsx");
61 +}
62 +function importProductsFromExcel(event){
63 + if(!requireWrite())return;
64 + if(!currentUser)return;
65 + const file=event.target.files[0];
66 + if(!file)return;
67 + const reader=new FileReader();
68 + reader.onload=function(e){
69 + try{
70 + const data=new Uint8Array(e.target.result);
71 + const workbook=XLSX.read(data,{type:'array'});
72 + const rows=XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);
73 + const myProducts=getMyProducts();
74 + rows.forEach((r,idx)=>{
75 + const name=r["نام کالا"]||r["عنوان"];
76 + if(name){
77 + datastore.products.push({id:'P_'+Date.now()+'_'+idx,ownerUserId:currentUser.id,code:(r["کد کالا"]||(myProducts.length+idx+1)).toString(),name:name.toString(),spec:(r["مشخصه و نوع"]||'').toString(),unit:(r["واحد"]||'عدد').toString(),buy_price:0,sale_price:parseFloat(r["قیمت فروش (ریال)"]||r["قیمت فروش"]||0)||0,stock:0,type:'good',internal_id:(r["شناسه کالا/خدمت داخلی"]||'').toString()});
78 + }
79 + });
80 + saveDatastore();refreshAllSurfaces();
81 + alert('کالاها ایمپورت شدند.');
82 + }catch(err){alert('خطا در فایل اکسل!');}
83 + };
84 + reader.readAsArrayBuffer(file);
85 +}
js/words.js new
+36
@@ -0,0 +1,36 @@
1 +function numberToPersianWords(num,currencyLabel){
2 + const cur=currencyLabel||'ریال';
3 + if(!num||isNaN(num)||num==0)return 'صفر '+cur;
4 + const yegan=["","یک","دو","سه","چهار","پنج","شش","هفت","هشت","نه"];
5 + const dahgan=["","","بیست","سی","چهل","پنجاه","شصت","هفتاد","هشتاد","نود"];
6 + const dah_ta_bist=["ده","یازده","دوازده","سیزده","چهارده","پانزده","شانزده","هفده","هجده","نوزده"];
7 + const sadgan=["","صد","دویست","سیصد","چهارصد","پانصد","ششصد","هفتصد","هشتصد","نهصد"];
8 + const part_names=["","هزار","میلیون","میلیارد","تریلیون"];
9 + function threeDigitToWords(n){
10 + let c=Math.floor(n/100);
11 + let d=Math.floor((n%100)/10);
12 + let y=n%10;
13 + let parts=[];
14 + if(c>0)parts.push(sadgan[c]);
15 + if(d===1)parts.push(dah_ta_bist[y]);
16 + else{
17 + if(d>1)parts.push(dahgan[d]);
18 + if(y>0)parts.push(yegan[y]);
19 + }
20 + return parts.join(" و ");
21 + }
22 + let parts=[];
23 + let chunkIdx=0;
24 + let temp=Math.abs(Math.round(num));
25 + while(temp>0){
26 + let chunk=temp%1000;
27 + if(chunk>0){
28 + let w=threeDigitToWords(chunk);
29 + if(part_names[chunkIdx])w+=" "+part_names[chunkIdx];
30 + parts.unshift(w);
31 + }
32 + temp=Math.floor(temp/1000);
33 + chunkIdx++;
34 + }
35 + return parts.join(" و ")+" "+cur;
36 +}