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

Update index.html

davoodmehraban89 committed Sep 24, 2026 at 01:04 UTC 65ed2aa8c34b2ad884aef1ccddb70ee656c61500
1 file changed +208
index.html
+208
@@ -424,6 +424,18 @@ html,body{background:#fff!important;color:#000!important;margin:0!important;padd
424 <button class="btn btn-danger btn-inline no-print" style="padding:6px 12px;font-size:12px;min-height:32px" onclick="handleLogout()">🚪 خروج از حساب کاربری</button>
425 </div>
426
427 +<div class="accordion-item">
428 +<div class="accordion-header" onclick="toggleAccordion(this)"><span>🔐 حساب کاربری و رمز عبور</span><span>▼</span></div>
429 +<div class="accordion-body">
430 +<p style="font-size:13px;color:var(--text-muted);margin-bottom:10px">با تعیین رمز عبور، دفعات بعد می‌توانید علاوه بر ورود با گوگل، مستقیم با ایمیل و رمز عبور هم وارد سامانه شوید.</p>
431 +<div class="form-row">
432 +<div class="form-group"><label>رمز عبور جدید</label><input type="password" id="acct-newpass" class="form-control" placeholder="حداقل ۸ کاراکتر" /></div>
433 +<div class="form-group"><label>تکرار رمز عبور</label><input type="password" id="acct-newpass2" class="form-control" /></div>
434 +</div>
435 +<button class="btn btn-primary" onclick="changeAccountPassword()">💾 ذخیره رمز عبور</button>
436 +</div>
437 +</div>
438 +
439 <div class="accordion-item open">
440 <div class="accordion-header" onclick="toggleAccordion(this)"><span>🏢 اطلاعات فروشگاه و چاپ (مدیریت شرکت‌ها)</span><span>▼</span></div>
441 <div class="accordion-body">
@@ -2512,5 +2524,201 @@ window.addEventListener('DOMContentLoaded',()=>{initAuthSystem();});
2524 })();
2525 </script>
2526
2527 +<script>
2528 +// ============== خروج خودکار بعد از عدم فعالیت + حفظ پیش‌نویس فاکتور باز ==============
2529 +(function(){
2530 + const IDLE_TIMEOUT_MS = 30*60*1000; // ۳۰ دقیقه
2531 + const WARN_LEAD_MS = 60*1000; // ۱ دقیقه قبل هشدار
2532 + const DRAFT_KEY = 'factoreasy_invoice_draft_v1';
2533 + let idleTimer=null, warnTimer=null, countdownInterval=null, warningVisible=false, lastActivityReset=0;
2534 +
2535 + function ensureIdleModal(){
2536 + if(document.getElementById('modal-idle-warning'))return;
2537 + const div=document.createElement('div');
2538 + div.className='modal-backdrop';
2539 + div.id='modal-idle-warning';
2540 + div.innerHTML=`<div class="modal-card" style="max-width:420px;text-align:center">
2541 + <h3 style="font-size:16px;margin-bottom:10px">⏰ عدم فعالیت</h3>
2542 + <p style="font-size:13px;color:var(--text-muted);margin-bottom:14px">به دلیل عدم فعالیت، تا <span id="idle-countdown" style="font-weight:bold;color:#dc2626">60</span> ثانیه دیگر از حساب خارج می‌شوید.</p>
2543 + <div style="display:flex;gap:10px;justify-content:center">
2544 + <button class="btn btn-success" onclick="window.__idleContinueWorking()">✅ ادامه کار</button>
2545 + <button class="btn btn-secondary" onclick="window.__idleLogoutNow()">🚪 خروج فوری</button>
2546 + </div>
2547 + </div>`;
2548 + document.body.appendChild(div);
2549 + }
2550 + function hideWarning(){
2551 + warningVisible=false;
2552 + clearInterval(countdownInterval);
2553 + const el=document.getElementById('modal-idle-warning');
2554 + if(el)el.classList.remove('active');
2555 + }
2556 + function showWarning(){
2557 + ensureIdleModal();
2558 + warningVisible=true;
2559 + let remain=Math.round(WARN_LEAD_MS/1000);
2560 + const cd=document.getElementById('idle-countdown');
2561 + if(cd)cd.innerText=remain;
2562 + document.getElementById('modal-idle-warning').classList.add('active');
2563 + countdownInterval=setInterval(()=>{
2564 + remain--;
2565 + const el=document.getElementById('idle-countdown');
2566 + if(el)el.innerText=Math.max(remain,0);
2567 + if(remain<=0)clearInterval(countdownInterval);
2568 + },1000);
2569 + }
2570 +
2571 + function getInvoiceFormSnapshot(){
2572 + const rows=[...document.querySelectorAll('#invoice-items-table-body tr')].map(tr=>({
2573 + prodId: tr.querySelector('.row-product-select')?.value||'',
2574 + unit: tr.querySelector('.row-unit-input')?.value||'',
2575 + qty: tr.querySelector('.row-quantity')?.value||'',
2576 + price: tr.querySelector('.row-price')?.value||''
2577 + })).filter(r=>r.prodId);
2578 + return {
2579 + editId: document.getElementById('edit-invoice-id')?.value||'',
2580 + companyId: document.getElementById('invoice-company-id')?.value||'',
2581 + kind: document.getElementById('invoice-kind')?.value||'',
2582 + vatMode: document.getElementById('invoice-vat-mode')?.value||'',
2583 + vatType: document.getElementById('invoice-vat-type')?.value||'',
2584 + vatRate: document.getElementById('invoice-vat-rate-input')?.value||'',
2585 + number: document.getElementById('invoice-number')?.value||'',
2586 + contactId: document.getElementById('invoice-contact-id')?.value||'',
2587 + date: document.getElementById('invoice-date-input')?.value||'',
2588 + discountType: document.getElementById('invoice-discount-type')?.value||'',
2589 + discountFixed: document.getElementById('invoice-discount-fixed')?.value||'',
2590 + desc: document.getElementById('invoice-desc-input')?.value||'',
2591 + items: rows,
2592 + savedAt: Date.now(),
2593 + userId: (typeof currentUser!=='undefined'&&currentUser)?currentUser.id:null
2594 + };
2595 + }
2596 + function isSnapshotWorthSaving(snap){
2597 + return !!(snap.contactId || (snap.desc&&snap.desc.trim()) || (snap.items&&snap.items.length>0));
2598 + }
2599 + function saveInvoiceDraftIfNeeded(){
2600 + try{
2601 + const snap=getInvoiceFormSnapshot();
2602 + if(!isSnapshotWorthSaving(snap))return;
2603 + localStorage.setItem(DRAFT_KEY, JSON.stringify(snap));
2604 + }catch(e){console.error('draft save failed',e);}
2605 + }
2606 +
2607 + async function performIdleLogout(){
2608 + hideWarning();
2609 + saveInvoiceDraftIfNeeded();
2610 + try{await sb.auth.signOut();}catch(e){}
2611 + }
2612 + window.__idleContinueWorking=function(){ hideWarning(); resetIdleTimer(); };
2613 + window.__idleLogoutNow=async function(){
2614 + hideWarning();
2615 + saveInvoiceDraftIfNeeded();
2616 + try{await sb.auth.signOut();}catch(e){}
2617 + };
2618 +
2619 + function resetIdleTimer(){
2620 + if(!window.__idleWatcherActive)return;
2621 + clearTimeout(idleTimer);
2622 + clearTimeout(warnTimer);
2623 + warnTimer=setTimeout(showWarning, IDLE_TIMEOUT_MS-WARN_LEAD_MS);
2624 + idleTimer=setTimeout(performIdleLogout, IDLE_TIMEOUT_MS);
2625 + }
2626 + function onActivity(){
2627 + if(!window.__idleWatcherActive)return;
2628 + const now=Date.now();
2629 + if(warningVisible)hideWarning();
2630 + if(now-lastActivityReset<3000)return;
2631 + lastActivityReset=now;
2632 + resetIdleTimer();
2633 + }
2634 + ['mousemove','mousedown','keydown','scroll','touchstart','click'].forEach(evt=>{
2635 + window.addEventListener(evt, onActivity, {passive:true});
2636 + });
2637 +
2638 + window.startIdleWatcher=function(){ window.__idleWatcherActive=true; resetIdleTimer(); };
2639 + window.stopIdleWatcher=function(){
2640 + window.__idleWatcherActive=false;
2641 + clearTimeout(idleTimer); clearTimeout(warnTimer); hideWarning();
2642 + };
2643 +
2644 + window.offerInvoiceDraftRestoreIfAny=function(){
2645 + try{
2646 + const raw=localStorage.getItem(DRAFT_KEY);
2647 + if(!raw)return;
2648 + const snap=JSON.parse(raw);
2649 + const uid=(typeof currentUser!=='undefined'&&currentUser)?currentUser.id:null;
2650 + if(!uid||snap.userId!==uid){localStorage.removeItem(DRAFT_KEY);return;}
2651 + const ageMin=Math.max(1,Math.round((Date.now()-(snap.savedAt||0))/60000));
2652 + if(!confirm(`یک فاکتور پیش‌نویس ذخیره‌نشده (حدود ${ageMin} دقیقه پیش، به‌دلیل خروج خودکار به‌خاطر عدم فعالیت) پیدا شد. بازیابی شود؟`)){
2653 + localStorage.removeItem(DRAFT_KEY); return;
2654 + }
2655 + switchView('view-invoices');
2656 + document.getElementById('edit-invoice-id').value=snap.editId||'';
2657 + if(snap.editId){
2658 + document.getElementById('invoice-form-title').innerText='✏️ ویرایش فاکتور (پیش‌نویس بازیابی‌شده)';
2659 + document.getElementById('btn-cancel-edit').style.display='inline-flex';
2660 + document.getElementById('btn-save-invoice').innerText='💾 ذخیره تغییرات';
2661 + }else{
2662 + document.getElementById('invoice-form-title').innerText='📝 فاکتور پیش‌نویس بازیابی‌شده';
2663 + }
2664 + document.getElementById('invoice-company-id').value=snap.companyId||'';
2665 + document.getElementById('invoice-kind').value=snap.kind||'non_formal';
2666 + document.getElementById('invoice-vat-mode').value=snap.vatMode||'none';
2667 + document.getElementById('invoice-vat-type').value=snap.vatType||'percent';
2668 + document.getElementById('invoice-vat-rate-input').value=snap.vatRate||10;
2669 + document.getElementById('invoice-number').value=snap.number||'';
2670 + document.getElementById('invoice-contact-id').value=snap.contactId||'';
2671 + document.getElementById('invoice-date-input').value=snap.date||'';
2672 + document.getElementById('invoice-discount-type').value=snap.discountType||'fixed';
2673 + document.getElementById('invoice-discount-fixed').value=snap.discountFixed||0;
2674 + document.getElementById('invoice-desc-input').value=snap.desc||'';
2675 + if(typeof handleInvoiceKindChange==='function')handleInvoiceKindChange();
2676 + const tbody=document.getElementById('invoice-items-table-body');
2677 + tbody.innerHTML='';
2678 + (snap.items||[]).forEach(it=>{
2679 + addInvoiceItemRow(it.prodId, parseFloat(it.qty)||1, it.price!==''?parseFloat(it.price):null);
2680 + const lastRow=tbody.lastElementChild;
2681 + if(lastRow&&it.unit)lastRow.querySelector('.row-unit-input').value=it.unit;
2682 + });
2683 + if(typeof updateInvoiceItemRowNumbers==='function')updateInvoiceItemRowNumbers();
2684 + if(typeof recomputeTotals==='function')recomputeTotals();
2685 + localStorage.removeItem(DRAFT_KEY);
2686 + }catch(e){console.error('draft restore failed',e);}
2687 + };
2688 +
2689 + // اتصال بدون دست‌زدن به کد اصلی enterApp/handleLogout
2690 + const oldEnterApp=window.enterApp;
2691 + window.enterApp=async function(authUser){
2692 + await oldEnterApp(authUser);
2693 + if(typeof currentUser!=='undefined'&&currentUser){
2694 + window.startIdleWatcher();
2695 + window.offerInvoiceDraftRestoreIfAny();
2696 + }
2697 + };
2698 + const oldHandleLogout=window.handleLogout;
2699 + window.handleLogout=async function(){
2700 + window.stopIdleWatcher();
2701 + return oldHandleLogout();
2702 + };
2703 +})();
2704 +
2705 +// ============== تغییر/تعیین رمز عبور از داخل تنظیمات (برای کاربران گوگل هم کار می‌کند) ==============
2706 +async function changeAccountPassword(){
2707 + const p1=document.getElementById('acct-newpass').value;
2708 + const p2=document.getElementById('acct-newpass2').value;
2709 + if(p1.length<8){alert('رمز عبور باید حداقل ۸ کاراکتر باشد.');return;}
2710 + if(p1!==p2){alert('تکرار رمز عبور با رمز اصلی یکی نیست.');return;}
2711 + try{
2712 + const {error}=await sb.auth.updateUser({password:p1,data:{has_password:true}});
2713 + if(error)throw error;
2714 + document.getElementById('acct-newpass').value='';
2715 + document.getElementById('acct-newpass2').value='';
2716 + alert('رمز عبور با موفقیت ذخیره شد. دفعات بعد می‌توانید با ایمیل و همین رمز هم وارد شوید.');
2717 + }catch(err){
2718 + alert(typeof authMsg==='function'?authMsg(err):(err.message||'خطا در ذخیره رمز عبور.'));
2719 + }
2720 +}
2721 +</script>
2722 +
2723 </body>
2724 </html>