سامانه صدور فاکتور و حسابداری
Update fmt.Println message from 'Hello' to 'Goodbye'
davoodmehraban89 committed
Sep 16, 2026 at 12:08 UTC
ac88a8c2abdc1e20e35fbe544057e143d07a6530
1 file changed
+1146
index.html
new
+1146
@@ -0,0 +1,1146 @@
1
+<!DOCTYPE html>
2
+<html lang="fa" dir="rtl">
3
+<head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
6
+ <title>فاکتورا | سامانه صدور فاکتور و حسابداری</title>
7
+
8
+ <!-- تنظیمات PWA و آیفون -->
9
+ <link rel="manifest" href="manifest.json" />
10
+ <meta name="apple-mobile-web-app-capable" content="yes" />
11
+ <meta name="apple-mobile-web-app-status-bar-style" content="default" />
12
+ <meta name="apple-mobile-web-app-title" content="فاکتورا" />
13
+ <meta name="theme-color" content="#1e3a8a" />
14
+
15
+ <!-- فونت وزیرمتن با اعداد فارسی -->
16
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/misc/Farsi-Digits/Vazirmatn-FD-font-face.css" />
17
+
18
+ <style>
19
+ :root {
20
+ --primary: #1e3a8a;
21
+ --primary-hover: #172554;
22
+ --accent: #2563eb;
23
+ --bg: #f8fafc;
24
+ --card-bg: #ffffff;
25
+ --text: #0f172a;
26
+ --text-muted: #64748b;
27
+ --border: #e2e8f0;
28
+ --danger: #dc2626;
29
+ --success: #059669;
30
+ --warning: #d97706;
31
+ --sidebar-w: 240px;
32
+ --nav-h: 68px;
33
+ }
34
+
35
+ * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Vazirmatn FD', Tahoma, sans-serif; -webkit-tap-highlight-color: transparent; }
36
+ body { background-color: var(--bg); color: var(--text); overflow-x: hidden; }
37
+
38
+ .app-root { display: flex; min-height: 100vh; }
39
+ .sidebar { width: var(--sidebar-w); background: var(--card-bg); border-left: 1px solid var(--border); display: flex; flex-direction: column; position: fixed; top: 0; bottom: 0; right: 0; z-index: 100; }
40
+ .brand-box { padding: 20px; display: flex; align-items: center; gap: 12px; border-bottom: 1px solid var(--border); }
41
+ .brand-icon { width: 40px; height: 40px; background: linear-gradient(135deg, var(--primary), var(--accent)); border-radius: 10px; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: bold; font-size: 20px; }
42
+
43
+ .sidebar-menu { list-style: none; padding: 16px 8px; flex: 1; overflow-y: auto; }
44
+ .sidebar-menu li a { display: flex; align-items: center; gap: 12px; padding: 12px 16px; color: var(--text); text-decoration: none; border-radius: 8px; margin-bottom: 4px; font-size: 14px; transition: all 0.2s; }
45
+ .sidebar-menu li a:hover, .sidebar-menu li a.active { background: #eff6ff; color: var(--accent); font-weight: bold; }
46
+
47
+ .main-surface { flex: 1; margin-right: var(--sidebar-w); padding: 24px; padding-bottom: 90px; }
48
+
49
+ .mobile-dock { display: none; position: fixed; bottom: 0; left: 0; right: 0; height: var(--nav-h); background: rgba(255,255,255,0.96); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--border); z-index: 200; justify-content: space-around; align-items: center; padding-bottom: env(safe-area-inset-bottom); }
50
+ .dock-tab { display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--text-muted); text-decoration: none; font-size: 11px; flex: 1; cursor: pointer; }
51
+ .dock-tab.active { color: var(--accent); font-weight: bold; }
52
+ .dock-tab svg { width: 22px; height: 22px; }
53
+
54
+ .card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin-bottom: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.02); }
55
+ .grid-kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; margin-bottom: 20px; }
56
+ .kpi-unit { background: var(--card-bg); border: 1px solid var(--border); border-radius: 12px; padding: 16px; display: flex; align-items: center; gap: 14px; }
57
+ .kpi-icon { width: 46px; height: 46px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 20px; }
58
+ .kpi-val { font-size: 17px; font-weight: bold; margin-top: 4px; }
59
+ .kpi-lbl { color: var(--text-muted); font-size: 12px; }
60
+
61
+ .btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 18px; background: var(--primary); color: #fff; border: none; border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 500; transition: background 0.2s; }
62
+ .btn:hover { background: var(--primary-hover); }
63
+ .btn-secondary { background: #e2e8f0; color: var(--text); }
64
+ .btn-secondary:hover { background: #cbd5e1; }
65
+ .btn-success { background: var(--success); }
66
+ .btn-danger { background: var(--danger); }
67
+
68
+ .form-group { margin-bottom: 14px; }
69
+ .form-group label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-muted); }
70
+ .form-control { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; outline: none; transition: border-color 0.2s; }
71
+ .form-control:focus { border-color: var(--accent); }
72
+ .form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
73
+
74
+ .table-responsive { overflow-x: auto; }
75
+ table { width: 100%; border-collapse: collapse; text-align: right; font-size: 13px; }
76
+ th, td { padding: 12px 14px; border-bottom: 1px solid var(--border); }
77
+ th { background: #f1f5f9; color: var(--text-muted); font-weight: 600; }
78
+
79
+ .badge { display: inline-block; padding: 3px 8px; border-radius: 6px; font-size: 11px; font-weight: 600; }
80
+ .badge-success { background: #d1fae5; color: #065f46; }
81
+ .badge-warning { background: #fef3c7; color: #92400e; }
82
+ .badge-danger { background: #fee2e2; color: #991b1b; }
83
+
84
+ .view-pane { display: none; }
85
+ .view-pane.active { display: block; }
86
+
87
+ @media print {
88
+ body { background: #fff; }
89
+ .sidebar, .mobile-dock, .no-print { display: none !important; }
90
+ .main-surface { margin: 0; padding: 0; }
91
+ .card { border: none; box-shadow: none; padding: 0; }
92
+ #printable-invoice { display: block !important; }
93
+ }
94
+
95
+ @media (max-width: 768px) {
96
+ .sidebar { display: none; }
97
+ .main-surface { margin-right: 0; padding: 16px; padding-bottom: calc(var(--nav-h) + 24px); }
98
+ .mobile-dock { display: flex; }
99
+ }
100
+ </style>
101
+</head>
102
+<body>
103
+
104
+ <div class="app-root">
105
+ <!-- منوی کناری مخصوص دسکتاپ -->
106
+ <aside class="sidebar">
107
+ <div class="brand-box">
108
+ <div class="brand-icon">فـ</div>
109
+ <div>
110
+ <h2 style="font-size: 15px; font-weight: bold;">فاکتورا وب</h2>
111
+ <small style="color: var(--text-muted); font-size: 11px;">سیستم جامع مالی و صدور فاکتور</small>
112
+ </div>
113
+ </div>
114
+ <ul class="sidebar-menu">
115
+ <li><a href="#" class="nav-link active" data-view="view-dashboard">📊 داشبورد تحلیلی</a></li>
116
+ <li><a href="#" class="nav-link" data-view="view-invoices">🧾 فاکتورها و اسناد</a></li>
117
+ <li><a href="#" class="nav-link" data-view="view-products">📦 کالاها و انبار</a></li>
118
+ <li><a href="#" class="nav-link" data-view="view-contacts">👥 طرفحسابها و معین</a></li>
119
+ <li><a href="#" class="nav-link" data-view="view-cheques">💳 چکها و صیاد</a></li>
120
+ <li><a href="#" class="nav-link" data-view="view-expenses">💰 هزینهها و درآمد</a></li>
121
+ <li><a href="#" class="nav-link" data-view="view-settings">⚙️ پشتیبانگیری و تنظیمات</a></li>
122
+ </ul>
123
+ </aside>
124
+
125
+ <!-- بدنه اصلی برنامه -->
126
+ <main class="main-surface">
127
+
128
+ <!-- ۱. داشبورد -->
129
+ <section id="view-dashboard" class="view-pane active">
130
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
131
+ <div>
132
+ <h1 style="font-size: 20px; font-weight: bold;">مرکز مدیریت و وضعیت فروشگاه</h1>
133
+ <p style="color: var(--text-muted); font-size: 13px;" id="dashboard-date-display"></p>
134
+ </div>
135
+ <button class="btn no-print" onclick="switchView('view-invoices')">➕ صدور فاکتور جدید</button>
136
+ </div>
137
+
138
+ <div class="grid-kpi">
139
+ <div class="kpi-unit">
140
+ <div class="kpi-icon" style="background:#eff6ff; color:var(--accent);">💰</div>
141
+ <div>
142
+ <div class="kpi-lbl">مجموع فروش ثبتشده</div>
143
+ <div class="kpi-val" id="kpi-sales-sum">۰ تومان</div>
144
+ </div>
145
+ </div>
146
+ <div class="kpi-unit">
147
+ <div class="kpi-icon" style="background:#ecfdf5; color:var(--success);">📥</div>
148
+ <div>
149
+ <div class="kpi-lbl">دریافت وصول شده</div>
150
+ <div class="kpi-val" id="kpi-settled-sum">۰ تومان</div>
151
+ </div>
152
+ </div>
153
+ <div class="kpi-unit">
154
+ <div class="kpi-icon" style="background:#fffbeb; color:var(--warning);">⏳</div>
155
+ <div>
156
+ <div class="kpi-lbl">مانده مطالبات نسیه</div>
157
+ <div class="kpi-val" id="kpi-debtors-sum">۰ تومان</div>
158
+ </div>
159
+ </div>
160
+ <div class="kpi-unit">
161
+ <div class="kpi-icon" style="background:#fef2f2; color:var(--danger);">💳</div>
162
+ <div>
163
+ <div class="kpi-lbl">چکهای باز صیادی</div>
164
+ <div class="kpi-val" id="kpi-cheques-count">۰ فقره</div>
165
+ </div>
166
+ </div>
167
+ </div>
168
+
169
+ <div class="card">
170
+ <h3 style="font-size: 15px; margin-bottom: 14px;">آخرین اسناد فاکتور صادره</h3>
171
+ <div class="table-responsive">
172
+ <table>
173
+ <thead>
174
+ <tr>
175
+ <th>شماره</th>
176
+ <th>طرف حساب</th>
177
+ <th>تاریخ</th>
178
+ <th>مبلغ نهایی (تومان)</th>
179
+ <th>روش پرداخت</th>
180
+ <th>وضعیت</th>
181
+ </tr>
182
+ </thead>
183
+ <tbody id="dashboard-recent-table">
184
+ <tr><td colspan="6" style="text-align:center; color:var(--text-muted);">سندی ثبت نشده است.</td></tr>
185
+ </tbody>
186
+ </table>
187
+ </div>
188
+ </div>
189
+ </section>
190
+
191
+ <!-- ۲. صدور و بایگانی فاکتور -->
192
+ <section id="view-invoices" class="view-pane">
193
+ <div class="card no-print">
194
+ <h2 style="font-size: 17px; margin-bottom: 16px;">صدور سند فاکتور فروش / پیشفاکتور</h2>
195
+ <div class="form-row">
196
+ <div class="form-group">
197
+ <label>نوع سند</label>
198
+ <select id="invoice-kind" class="form-control">
199
+ <option value="sale">فاکتور فروش قطعی</option>
200
+ <option value="pre_invoice">پیشفاکتور رسمی</option>
201
+ <option value="return">برگشت از فروش</option>
202
+ </select>
203
+ </div>
204
+ <div class="form-group">
205
+ <label>شماره فاکتور</label>
206
+ <input type="text" id="invoice-number" class="form-control" placeholder="1085" />
207
+ </div>
208
+ <div class="form-group">
209
+ <label>خریدار / طرف حساب</label>
210
+ <select id="invoice-contact-id" class="form-control"></select>
211
+ </div>
212
+ <div class="form-group">
213
+ <label>تاریخ سند (شمسی)</label>
214
+ <input type="text" id="invoice-date-input" class="form-control" />
215
+ </div>
216
+ </div>
217
+
218
+ <div style="margin: 16px 0;">
219
+ <h4 style="font-size: 14px; margin-bottom: 10px;">جدول اقلام کالا و خدمات</h4>
220
+ <div class="table-responsive">
221
+ <table>
222
+ <thead>
223
+ <tr>
224
+ <th>شرح کالا یا خدمت</th>
225
+ <th style="width:120px;">تعداد / مقدار</th>
226
+ <th style="width:160px;">فی واحد (تومان)</th>
227
+ <th style="width:160px;">جمع کل سطر</th>
228
+ <th style="width:50px;">حذف</th>
229
+ </tr>
230
+ </thead>
231
+ <tbody id="invoice-items-table-body"></tbody>
232
+ </table>
233
+ </div>
234
+ <button class="btn btn-secondary" style="margin-top: 10px;" onclick="addInvoiceItemRow()">➕ افزودن ردیف کالا</button>
235
+ </div>
236
+
237
+ <div class="form-row" style="background:#f8fafc; padding:14px; border-radius:8px; margin-top:12px;">
238
+ <div class="form-group">
239
+ <label>نحوه تسویه</label>
240
+ <select id="invoice-payment-method" class="form-control">
241
+ <option value="cash">نقدی / کارتخوان</option>
242
+ <option value="credit">نسیه (بدهکار در معین)</option>
243
+ <option value="cheque">چک صیادی</option>
244
+ <option value="installment">فروش اقساطی</option>
245
+ </select>
246
+ </div>
247
+ <div class="form-group">
248
+ <label>تخفیف کل (تومان)</label>
249
+ <input type="number" id="invoice-discount-fixed" class="form-control" value="0" oninput="recomputeTotals()" />
250
+ </div>
251
+ <div class="form-group">
252
+ <label>مالیات ارزش افزوده (۱۰٪)</label>
253
+ <select id="invoice-vat-toggle" class="form-control" onchange="recomputeTotals()">
254
+ <option value="1">اعمال مالیات ۱۰٪</option>
255
+ <option value="0">معاف از مالیات</option>
256
+ </select>
257
+ </div>
258
+ <div class="form-group">
259
+ <label>مبلغ نهایی قابل پرداخت</label>
260
+ <input type="text" id="invoice-grand-total-display" class="form-control" readonly style="font-weight:bold; background:#e2e8f0;" />
261
+ </div>
262
+ </div>
263
+
264
+ <div style="display:flex; gap:10px; margin-top: 16px;">
265
+ <button class="btn btn-success" onclick="commitSaveInvoice()">💾 ثبت نهایی سند</button>
266
+ <button class="btn btn-secondary" onclick="printActiveInvoice()">🖨️ چاپ مستقیم / PDF</button>
267
+ </div>
268
+ </div>
269
+
270
+ <div class="card no-print">
271
+ <h3 style="font-size: 15px; margin-bottom: 12px;">دفتر بایگانی فاکتورها</h3>
272
+ <div class="table-responsive">
273
+ <table>
274
+ <thead>
275
+ <tr>
276
+ <th>شماره</th>
277
+ <th>خریدار</th>
278
+ <th>تاریخ</th>
279
+ <th>مبلغ نهایی (تومان)</th>
280
+ <th>تسویه</th>
281
+ <th>عملیات</th>
282
+ </tr>
283
+ </thead>
284
+ <tbody id="invoice-archive-table-body"></tbody>
285
+ </table>
286
+ </div>
287
+ </div>
288
+ </section>
289
+
290
+ <!-- ۳. کالاها و موجودی انبار -->
291
+ <section id="view-products" class="view-pane">
292
+ <div class="card no-print">
293
+ <h2 style="font-size: 17px; margin-bottom: 16px;">ثبت کالا یا خدمت جدید</h2>
294
+ <div class="form-row">
295
+ <div class="form-group">
296
+ <label>کد کالا</label>
297
+ <input type="text" id="prod-code-input" class="form-control" placeholder="101" />
298
+ </div>
299
+ <div class="form-group">
300
+ <label>عنوان کالا یا خدمت</label>
301
+ <input type="text" id="prod-name-input" class="form-control" />
302
+ </div>
303
+ <div class="form-group">
304
+ <label>نوع ردیف</label>
305
+ <select id="prod-type-input" class="form-control">
306
+ <option value="good">کالای فیزیکی (کنترل انبار)</option>
307
+ <option value="service">خدمات (نامحدود)</option>
308
+ </select>
309
+ </div>
310
+ <div class="form-group">
311
+ <label>واحد اندازهگیری</label>
312
+ <input type="text" id="prod-unit-input" class="form-control" value="عدد" />
313
+ </div>
314
+ </div>
315
+ <div class="form-row">
316
+ <div class="form-group">
317
+ <label>قیمت خرید انبار (تومان)</label>
318
+ <input type="number" id="prod-buy-input" class="form-control" value="0" />
319
+ </div>
320
+ <div class="form-group">
321
+ <label>قیمت فروش مصوب (تومان)</label>
322
+ <input type="number" id="prod-sale-input" class="form-control" value="0" />
323
+ </div>
324
+ <div class="form-group">
325
+ <label>موجودی فیزیکی اولیه</label>
326
+ <input type="number" id="prod-stock-input" class="form-control" value="0" />
327
+ </div>
328
+ <div class="form-group">
329
+ <label>حد هشدار سفارش (حداقل)</label>
330
+ <input type="number" id="prod-reorder-input" class="form-control" value="5" />
331
+ </div>
332
+ </div>
333
+ <button class="btn btn-success" onclick="commitSaveProduct()">➕ افزودن به انبار</button>
334
+ </div>
335
+
336
+ <div class="card">
337
+ <h3 style="font-size: 15px; margin-bottom: 12px;">کاردکس و موجودی فعلی انبار</h3>
338
+ <div class="table-responsive">
339
+ <table>
340
+ <thead>
341
+ <tr>
342
+ <th>کد</th>
343
+ <th>عنوان کالا</th>
344
+ <th>واحد</th>
345
+ <th>قیمت خرید</th>
346
+ <th>قیمت فروش</th>
347
+ <th>موجودی</th>
348
+ <th>وضعیت انبار</th>
349
+ </tr>
350
+ </thead>
351
+ <tbody id="products-catalog-table-body"></tbody>
352
+ </table>
353
+ </div>
354
+ </div>
355
+ </section>
356
+
357
+ <!-- ۴. اشخاص و طرفحسابها -->
358
+ <section id="view-contacts" class="view-pane">
359
+ <div class="card no-print">
360
+ <h2 style="font-size: 17px; margin-bottom: 16px;">ثبت طرف حساب تجاری جدید</h2>
361
+ <div class="form-row">
362
+ <div class="form-group">
363
+ <label>نام و نام خانوادگی / نام شرکت</label>
364
+ <input type="text" id="contact-name-input" class="form-control" />
365
+ </div>
366
+ <div class="form-group">
367
+ <label>نقش تجاری</label>
368
+ <select id="contact-role-input" class="form-control">
369
+ <option value="customer">مشتری (خریدار)</option>
370
+ <option value="supplier">تامینکننده (فروشنده)</option>
371
+ <option value="both">دوگانه</option>
372
+ </select>
373
+ </div>
374
+ <div class="form-group">
375
+ <label>شماره تماس همراه</label>
376
+ <input type="text" id="contact-mobile-input" class="form-control" placeholder="0912..." />
377
+ </div>
378
+ <div class="form-group">
379
+ <label>کد ملی / شناسه اقتصادی</label>
380
+ <input type="text" id="contact-national-input" class="form-control" />
381
+ </div>
382
+ </div>
383
+ <div class="form-group">
384
+ <label>نشانی کامل و کد پستی</label>
385
+ <input type="text" id="contact-address-input" class="form-control" />
386
+ </div>
387
+ <button class="btn btn-success" onclick="commitSaveContact()">➕ ثبت طرف حساب</button>
388
+ </div>
389
+
390
+ <div class="card">
391
+ <h3 style="font-size: 15px; margin-bottom: 12px;">دفتر اشخاص و وضعیت معین مانده</h3>
392
+ <div class="table-responsive">
393
+ <table>
394
+ <thead>
395
+ <tr>
396
+ <th>نام شخص / شرکت</th>
397
+ <th>نقش</th>
398
+ <th>شماره همراه</th>
399
+ <th>مانده حساب معین (تومان)</th>
400
+ <th>وضعیت حساب</th>
401
+ </tr>
402
+ </thead>
403
+ <tbody id="contacts-ledger-table-body"></tbody>
404
+ </table>
405
+ </div>
406
+ </div>
407
+ </section>
408
+
409
+ <!-- ۵. چکهای صیادی -->
410
+ <section id="view-cheques" class="view-pane">
411
+ <div class="card no-print">
412
+ <h2 style="font-size: 17px; margin-bottom: 16px;">ثبت برگ چک صیادی (دریافتی / پرداختی)</h2>
413
+ <div class="form-row">
414
+ <div class="form-group">
415
+ <label>گردش چک</label>
416
+ <select id="chq-direction-input" class="form-control">
417
+ <option value="inbound">چک دریافتی از مشتری</option>
418
+ <option value="outbound">چک صادره پرداختی به تامینکننده</option>
419
+ </select>
420
+ </div>
421
+ <div class="form-group">
422
+ <label>طرف حساب</label>
423
+ <select id="chq-contact-select" class="form-control"></select>
424
+ </div>
425
+ <div class="form-group">
426
+ <label>شناسه ۱۶ رقمی صیاد</label>
427
+ <input type="text" id="chq-sayad-input" class="form-control" placeholder="0812345678901235" />
428
+ </div>
429
+ <div class="form-group">
430
+ <label>بانک و شعبه</label>
431
+ <input type="text" id="chq-bank-input" class="form-control" placeholder="ملت - مرکزی" />
432
+ </div>
433
+ </div>
434
+ <div class="form-row">
435
+ <div class="form-group">
436
+ <label>مبلغ چک (تومان)</label>
437
+ <input type="number" id="chq-amount-input" class="form-control" />
438
+ </div>
439
+ <div class="form-group">
440
+ <label>تاریخ سررسید (شمسی)</label>
441
+ <input type="text" id="chq-due-input" class="form-control" placeholder="1405/04/15" />
442
+ </div>
443
+ <div class="form-group">
444
+ <label>وضعیت صیادی</label>
445
+ <select id="chq-status-input" class="form-control">
446
+ <option value="registered">ثبت شده در جریان وصول</option>
447
+ <option value="cleared">وصول شده</option>
448
+ <option value="returned">برگشت خورده</option>
449
+ </select>
450
+ </div>
451
+ </div>
452
+ <button class="btn btn-success" onclick="commitSaveCheque()">➕ ثبت سند چک</button>
453
+ </div>
454
+
455
+ <div class="card">
456
+ <h3 style="font-size: 15px; margin-bottom: 12px;">کارتابل سررسید چکهای صیاد</h3>
457
+ <div class="table-responsive">
458
+ <table>
459
+ <thead>
460
+ <tr>
461
+ <th>جهت</th>
462
+ <th>طرف حساب</th>
463
+ <th>شناسه صیاد و بانک</th>
464
+ <th>مبلغ (تومان)</th>
465
+ <th>سررسید</th>
466
+ <th>وضعیت</th>
467
+ </tr>
468
+ </thead>
469
+ <tbody id="cheques-ledger-table-body"></tbody>
470
+ </table>
471
+ </div>
472
+ </div>
473
+ </section>
474
+
475
+ <!-- ۶. هزینهها و درآمدها -->
476
+ <section id="view-expenses" class="view-pane">
477
+ <div class="card no-print">
478
+ <h2 style="font-size: 17px; margin-bottom: 16px;">ثبت سند هزینه یا درآمد</h2>
479
+ <div class="form-row">
480
+ <div class="form-group">
481
+ <label>نوع تراکنش</label>
482
+ <select id="trx-kind-input" class="form-control">
483
+ <option value="expense">هزینه (پرداخت نقدی/بانکی)</option>
484
+ <option value="income">درآمد متفرقه (دریافت)</option>
485
+ </select>
486
+ </div>
487
+ <div class="form-group">
488
+ <label>سرفصل</label>
489
+ <select id="trx-category-input" class="form-control">
490
+ <option value="قبوض">قبوض آب، برق، گاز و ارتباطات</option>
491
+ <option value="اجاره">اجاره دفتر / فروشگاه</option>
492
+ <option value="حقوق">حقوق و دستمزد</option>
493
+ <option value="حمل و نقل">حمل و نقل و پیک</option>
494
+ <option value="سایر">سایر موارد</option>
495
+ </select>
496
+ </div>
497
+ <div class="form-group">
498
+ <label>مبلغ تراکنش (تومان)</label>
499
+ <input type="number" id="trx-amount-input" class="form-control" />
500
+ </div>
501
+ <div class="form-group">
502
+ <label>شرح سند</label>
503
+ <input type="text" id="trx-desc-input" class="form-control" placeholder="توضیحات تکمیلی" />
504
+ </div>
505
+ </div>
506
+ <button class="btn btn-success" onclick="commitSaveExpense()">➕ ثبت در دفاتر</button>
507
+ </div>
508
+
509
+ <div class="card">
510
+ <h3 style="font-size: 15px; margin-bottom: 12px;">دفتر روزنامه هزینهها و درآمدها</h3>
511
+ <div class="table-responsive">
512
+ <table>
513
+ <thead>
514
+ <tr>
515
+ <th>نوع</th>
516
+ <th>سرفصل</th>
517
+ <th>شرح بابت</th>
518
+ <th>مبلغ (تومان)</th>
519
+ </tr>
520
+ </thead>
521
+ <tbody id="expenses-ledger-table-body"></tbody>
522
+ </table>
523
+ </div>
524
+ </div>
525
+ </section>
526
+
527
+ <!-- ۷. تنظیمات و پشتیبانگیری -->
528
+ <section id="view-settings" class="view-pane">
529
+ <div class="card">
530
+ <h2 style="font-size: 17px; margin-bottom: 16px;">مشخصات فروشگاه (جهت درج در سربرگ فاکتور)</h2>
531
+ <div class="form-row">
532
+ <div class="form-group">
533
+ <label>نام فروشگاه / شرکت</label>
534
+ <input type="text" id="set-store-name" class="form-control" />
535
+ </div>
536
+ <div class="form-group">
537
+ <label>شماره تماس</label>
538
+ <input type="text" id="set-phone-input" class="form-control" />
539
+ </div>
540
+ <div class="form-group">
541
+ <label>کد اقتصادی / شناسه ملی</label>
542
+ <input type="text" id="set-tax-number" class="form-control" />
543
+ </div>
544
+ </div>
545
+ <div class="form-group">
546
+ <label>نشانی فروشگاه</label>
547
+ <input type="text" id="set-address-input" class="form-control" />
548
+ </div>
549
+ <div class="form-group">
550
+ <label>متن پاورقی فاکتور</label>
551
+ <textarea id="set-footer-text" class="form-control" rows="2"></textarea>
552
+ </div>
553
+ <button class="btn btn-primary" onclick="commitSaveSettings()">ذخیره تنظیمات</button>
554
+ </div>
555
+
556
+ <div class="card">
557
+ <h3 style="font-size: 15px; margin-bottom: 14px;">پشتیبانگیری و انتقال اطلاعات</h3>
558
+ <p style="color:var(--text-muted); font-size:13px; margin-bottom:12px;">
559
+ جهت جابهجایی اطلاعات بین گوشی آیفون، اندروید و کامپیوتر میتوانید فایل بکاپ JSON را دانلود و در دستگاه دیگر بازگردانی نمایید.
560
+ </p>
561
+ <div style="display:flex; gap:10px;">
562
+ <button class="btn btn-secondary" onclick="exportDataBlob()">📥 دانلود پشتیبان (JSON)</button>
563
+ <label class="btn btn-secondary" style="margin-bottom:0;">
564
+ 📤 بازگردانی فایل پشتیبان
565
+ <input type="file" id="import-json-file" style="display:none;" onchange="importDataBlob(event)" accept=".json" />
566
+ </label>
567
+ </div>
568
+ </div>
569
+ </section>
570
+
571
+ <!-- قالب چاپ فاکتور A4 -->
572
+ <div id="printable-invoice" style="display:none; background:#fff; padding:24px; border:1px solid #111; margin-top:20px; border-radius:4px;">
573
+ <div style="display:flex; justify-content:space-between; align-items:center; border-bottom:2px solid #111; padding-bottom:12px;">
574
+ <div>
575
+ <h1 id="p-org-name" style="font-size:18px; margin:0;">فاکتورا</h1>
576
+ <p id="p-org-details" style="font-size:11px; color:#444; margin-top:4px;"></p>
577
+ </div>
578
+ <div style="text-align:left;">
579
+ <h2 id="p-doc-kind" style="font-size:16px; margin:0;">صورتحساب فروش</h2>
580
+ <p style="font-size:11px; margin-top:4px;">شماره: <span id="p-doc-number"></span></p>
581
+ <p style="font-size:11px;">تاریخ: <span id="p-doc-date"></span></p>
582
+ </div>
583
+ </div>
584
+
585
+ <div style="margin:12px 0; font-size:12px; background:#f8fafc; padding:8px; border-radius:4px; border:1px solid #e2e8f0;">
586
+ خریدار: <strong id="p-target-name"></strong> | تلفن: <span id="p-target-phone"></span>
587
+ </div>
588
+
589
+ <table style="width:100%; border-collapse:collapse; margin:14px 0;">
590
+ <thead>
591
+ <tr style="background:#f1f5f9;">
592
+ <th style="border:1px solid #ccc; padding:6px; width:40px; text-align:center;">ردیف</th>
593
+ <th style="border:1px solid #ccc; padding:6px; text-align:right;">شرح کالا یا خدمت</th>
594
+ <th style="border:1px solid #ccc; padding:6px; width:60px; text-align:center;">تعداد</th>
595
+ <th style="border:1px solid #ccc; padding:6px; width:120px; text-align:left;">قیمت واحد (تومان)</th>
596
+ <th style="border:1px solid #ccc; padding:6px; width:140px; text-align:left;">جمع کل (تومان)</th>
597
+ </tr>
598
+ </thead>
599
+ <tbody id="p-target-items-table"></tbody>
600
+ </table>
601
+
602
+ <div style="display:flex; justify-content:space-between; margin-top:16px;">
603
+ <div style="width:55%; font-size:11px;" id="p-doc-footer-note"></div>
604
+ <div style="width:40%; font-size:12px;">
605
+ <table style="width:100%; border-collapse:collapse;">
606
+ <tr><td style="padding:4px;">جمع اقلام:</td><td style="padding:4px; text-align:left;"><span id="p-calc-subtotal"></span> تومان</td></tr>
607
+ <tr><td style="padding:4px;">تخفیف:</td><td style="padding:4px; text-align:left;"><span id="p-calc-discount"></span> تومان</td></tr>
608
+ <tr><td style="padding:4px;">مالیات ارزش افزوده (۱۰٪):</td><td style="padding:4px; text-align:left;"><span id="p-calc-tax"></span> تومان</td></tr>
609
+ <tr style="border-top:1px solid #111; font-weight:bold; font-size:13px;">
610
+ <td style="padding:6px 4px;">مبلغ نهایی:</td>
611
+ <td style="padding:6px 4px; text-align:left;"><span id="p-calc-grandtotal"></span> تومان</td>
612
+ </tr>
613
+ </table>
614
+ <p style="font-size:10px; color:#555; margin-top:6px;">به حروف: <span id="p-calc-words"></span></p>
615
+ </div>
616
+ </div>
617
+ </div>
618
+
619
+ </main>
620
+ </div>
621
+
622
+ <!-- منوی پایین ویژه آیفون -->
623
+ <nav class="mobile-dock">
624
+ <div class="dock-tab active" onclick="switchView('view-dashboard')">
625
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"></path></svg>
626
+ <span>داشبورد</span>
627
+ </div>
628
+ <div class="dock-tab" onclick="switchView('view-invoices')">
629
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
630
+ <span>فاکتورها</span>
631
+ </div>
632
+ <div class="dock-tab" onclick="switchView('view-products')">
633
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path></svg>
634
+ <span>انبار کالا</span>
635
+ </div>
636
+ <div class="dock-tab" onclick="switchView('view-contacts')">
637
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
638
+ <span>اشخاص</span>
639
+ </div>
640
+ <div class="dock-tab" onclick="switchView('view-cheques')">
641
+ <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"></path></svg>
642
+ <span>چک صیاد</span>
643
+ </div>
644
+ </nav>
645
+
646
+ <script>
647
+ const STORAGE_KEY = 'faktora_enterprise_datastore';
648
+
649
+ let datastore = {
650
+ settings: {
651
+ store_name: 'فروشگاه آوانتک',
652
+ phone: '09120000000',
653
+ tax_number: '123456789',
654
+ address: 'تهران، خیابان ولیعصر',
655
+ footer: 'کالای فروخته شده تا ۲۴ ساعت با ارائه فاکتور تعویض میگردد.'
656
+ },
657
+ contacts: [
658
+ { id: 'C-1', name: 'نادر بحری', mobile: '09121111111', national_id: '0012345678', address: 'تهران', balance: 2950000, role: 'customer' },
659
+ { id: 'C-2', name: 'شرکت کیبیسی', mobile: '02188888888', national_id: '10102030405', address: 'تهران', balance: -5000000, role: 'supplier' }
660
+ ],
661
+ products: [
662
+ { id: 'P-1', code: '101', name: 'کابل شارژ تایپسی اورجینال', unit: 'عدد', buy_price: 120000, sale_price: 190000, stock: 45, reorder: 10, type: 'good' },
663
+ { id: 'P-2', code: '102', name: 'آداپتور ۲۰ وات اصل', unit: 'عدد', buy_price: 750000, sale_price: 1150000, stock: 8, reorder: 5, type: 'good' },
664
+ { id: 'P-3', code: '103', name: 'سرویس نرمافزار و سختافزار', unit: 'مورد', buy_price: 0, sale_price: 350000, stock: 999, reorder: 0, type: 'service' }
665
+ ],
666
+ invoices: [],
667
+ cheques: [],
668
+ expenses: []
669
+ };
670
+
671
+ function initDatastore() {
672
+ const persisted = localStorage.getItem(STORAGE_KEY);
673
+ if (persisted) {
674
+ try { datastore = JSON.parse(persisted); } catch(e) { console.error(e); }
675
+ }
676
+ persistDatastore();
677
+ refreshAllSurfaces();
678
+ }
679
+
680
+ function persistDatastore() {
681
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(datastore));
682
+ }
683
+
684
+ function getJalaliDate() {
685
+ return new Intl.DateTimeFormat('fa-IR-u-ca-persian', { dateStyle: 'full' }).format(new Date());
686
+ }
687
+
688
+ function getJalaliNumeric() {
689
+ const now = new Date();
690
+ return new Intl.DateTimeFormat('fa-IR-u-ca-persian', { year: 'numeric', month: '2-digit', day: '2-digit' }).format(now);
691
+ }
692
+
693
+ function switchView(viewId) {
694
+ document.querySelectorAll('.view-pane').forEach(el => el.classList.remove('active'));
695
+ const target = document.getElementById(viewId);
696
+ if (target) target.classList.add('active');
697
+
698
+ document.querySelectorAll('.nav-link').forEach(btn => {
699
+ btn.classList.toggle('active', btn.getAttribute('data-view') === viewId);
700
+ });
701
+
702
+ const dockViews = ['view-dashboard', 'view-invoices', 'view-products', 'view-contacts', 'view-cheques'];
703
+ document.querySelectorAll('.dock-tab').forEach((dock, idx) => {
704
+ dock.classList.toggle('active', dockViews[idx] === viewId);
705
+ });
706
+
707
+ window.scrollTo({ top: 0, behavior: 'smooth' });
708
+ }
709
+
710
+ document.querySelectorAll('.nav-link').forEach(btn => {
711
+ btn.addEventListener('click', (e) => {
712
+ e.preventDefault();
713
+ switchView(btn.getAttribute('data-view'));
714
+ });
715
+ });
716
+
717
+ function refreshAllSurfaces() {
718
+ document.getElementById('dashboard-date-display').innerText = getJalaliDate();
719
+ document.getElementById('invoice-date-input').value = getJalaliNumeric();
720
+
721
+ renderDashboard();
722
+ renderContacts();
723
+ renderProducts();
724
+ renderInvoices();
725
+ renderCheques();
726
+ renderExpenses();
727
+ renderSettings();
728
+ }
729
+
730
+ function renderDashboard() {
731
+ let salesSum = 0;
732
+ let debtorsSum = 0;
733
+
734
+ datastore.invoices.forEach(inv => {
735
+ salesSum += inv.grandTotal;
736
+ if (inv.paymentMethod === 'credit') debtorsSum += inv.grandTotal;
737
+ });
738
+
739
+ document.getElementById('kpi-sales-sum').innerText = salesSum.toLocaleString('fa-IR') + ' تومان';
740
+ document.getElementById('kpi-settled-sum').innerText = (salesSum - debtorsSum).toLocaleString('fa-IR') + ' تومان';
741
+ document.getElementById('kpi-debtors-sum').innerText = debtorsSum.toLocaleString('fa-IR') + ' تومان';
742
+ document.getElementById('kpi-cheques-count').innerText = datastore.cheques.filter(c => c.status === 'registered').length.toLocaleString('fa-IR') + ' فقره';
743
+
744
+ const recentTbody = document.getElementById('dashboard-recent-table');
745
+ if (datastore.invoices.length === 0) {
746
+ recentTbody.innerHTML = '<tr><td colspan="6" style="text-align:center; color:var(--text-muted);">سندی ثبت نشده است.</td></tr>';
747
+ return;
748
+ }
749
+
750
+ recentTbody.innerHTML = datastore.invoices.slice(-5).reverse().map(inv => `
751
+ <tr>
752
+ <td><strong>#${inv.number}</strong></td>
753
+ <td>${inv.contactName}</td>
754
+ <td>${inv.date}</td>
755
+ <td>${inv.grandTotal.toLocaleString('fa-IR')}</td>
756
+ <td>${inv.paymentMethodLabel}</td>
757
+ <td><span class="badge badge-success">ثبت شده</span></td>
758
+ </tr>
759
+ `).join('');
760
+ }
761
+
762
+ function addInvoiceItemRow() {
763
+ const tbody = document.getElementById('invoice-items-table-body');
764
+ const rowKey = 'item_' + Date.now() + '_' + Math.random().toString(36).substr(2, 4);
765
+ const tr = document.createElement('tr');
766
+ tr.id = rowKey;
767
+
768
+ const productOptions = datastore.products.map(p =>
769
+ `<option value="${p.id}" data-price="${p.sale_price}">${p.name} (${p.unit}) - موجودی: ${p.stock}</option>`
770
+ ).join('');
771
+
772
+ tr.innerHTML = `
773
+ <td>
774
+ <select class="form-control row-product-select" onchange="onRowProductChange('${rowKey}')">
775
+ <option value="">-- انتخاب کالا / خدمات --</option>
776
+ ${productOptions}
777
+ </select>
778
+ </td>
779
+ <td><input type="number" class="form-control row-quantity" value="1" min="1" oninput="recomputeTotals()" /></td>
780
+ <td><input type="number" class="form-control row-price" value="0" oninput="recomputeTotals()" /></td>
781
+ <td class="row-linetotal" style="font-weight:bold; padding-top:18px;">۰</td>
782
+ <td><button class="btn btn-danger" style="padding:4px 8px; font-size:12px;" onclick="document.getElementById('${rowKey}').remove(); recomputeTotals();">✕</button></td>
783
+ `;
784
+ tbody.appendChild(tr);
785
+ }
786
+
787
+ function onRowProductChange(rowKey) {
788
+ const tr = document.getElementById(rowKey);
789
+ const sel = tr.querySelector('.row-product-select');
790
+ const opt = sel.options[sel.selectedIndex];
791
+ const prc = opt.getAttribute('data-price') || 0;
792
+ tr.querySelector('.row-price').value = prc;
793
+ recomputeTotals();
794
+ }
795
+
796
+ function recomputeTotals() {
797
+ let subtotal = 0;
798
+ document.querySelectorAll('#invoice-items-table-body tr').forEach(tr => {
799
+ const qty = parseFloat(tr.querySelector('.row-quantity').value) || 0;
800
+ const prc = parseFloat(tr.querySelector('.row-price').value) || 0;
801
+ const line = qty * prc;
802
+ tr.querySelector('.row-linetotal').innerText = line.toLocaleString('fa-IR');
803
+ subtotal += line;
804
+ });
805
+
806
+ const fixedDiscount = parseFloat(document.getElementById('invoice-discount-fixed').value) || 0;
807
+ const isVat = document.getElementById('invoice-vat-toggle').value === '1';
808
+
809
+ const afterDiscount = Math.max(0, subtotal - fixedDiscount);
810
+ const vatAmount = isVat ? Math.round(afterDiscount * 0.10) : 0;
811
+ const grandTotal = afterDiscount + vatAmount;
812
+
813
+ document.getElementById('invoice-grand-total-display').value = grandTotal.toLocaleString('fa-IR') + ' تومان';
814
+ return { subtotal, fixedDiscount, vatAmount, grandTotal };
815
+ }
816
+
817
+ function commitSaveInvoice() {
818
+ const contactSelect = document.getElementById('invoice-contact-id');
819
+ const contactId = contactSelect.value;
820
+ const contactName = contactSelect.options[contactSelect.selectedIndex]?.text || 'مشتری آزاد';
821
+ const number = document.getElementById('invoice-number').value || ('INV-' + (datastore.invoices.length + 1084));
822
+ const date = document.getElementById('invoice-date-input').value;
823
+ const kind = document.getElementById('invoice-kind').value;
824
+ const paymentMethod = document.getElementById('invoice-payment-method').value;
825
+
826
+ const items = [];
827
+ let stockValidationFailed = false;
828
+
829
+ document.querySelectorAll('#invoice-items-table-body tr').forEach(tr => {
830
+ const sel = tr.querySelector('.row-product-select');
831
+ const prodId = sel.value;
832
+ const prodName = sel.options[sel.selectedIndex]?.text || '';
833
+ const qty = parseFloat(tr.querySelector('.row-quantity').value) || 0;
834
+ const price = parseFloat(tr.querySelector('.row-price').value) || 0;
835
+
836
+ if (prodId && qty > 0) {
837
+ const product = datastore.products.find(p => p.id === prodId);
838
+ if (product && product.type === 'good') {
839
+ if (product.stock < qty && kind === 'sale') {
840
+ alert(`موجودی کالای "${product.name}" کافی نیست. موجودی: ${product.stock}`);
841
+ stockValidationFailed = true;
842
+ return;
843
+ }
844
+ }
845
+ items.push({ prodId, prodName, qty, price, lineTotal: qty * price });
846
+ }
847
+ });
848
+
849
+ if (stockValidationFailed) return;
850
+ if (items.length === 0) {
851
+ alert('افزودن حداقل یک ردیف کالا یا خدمات اجباری است.');
852
+ return;
853
+ }
854
+
855
+ const totals = recomputeTotals();
856
+ const methodLabels = { cash: 'نقدی', credit: 'نسیه', cheque: 'چک صیاد', installment: 'اقساطی' };
857
+
858
+ if (kind === 'sale') {
859
+ items.forEach(it => {
860
+ const product = datastore.products.find(p => p.id === it.prodId);
861
+ if (product && product.type === 'good') {
862
+ product.stock -= it.qty;
863
+ }
864
+ });
865
+ }
866
+
867
+ const record = {
868
+ id: 'INV_' + Date.now(),
869
+ number,
870
+ date,
871
+ kind,
872
+ contactId,
873
+ contactName,
874
+ paymentMethod,
875
+ paymentMethodLabel: methodLabels[paymentMethod],
876
+ subtotal: totals.subtotal,
877
+ discount: totals.fixedDiscount,
878
+ vat: totals.vatAmount,
879
+ grandTotal: totals.grandTotal,
880
+ items
881
+ };
882
+
883
+ datastore.invoices.push(record);
884
+ persistDatastore();
885
+ alert('فاکتور با موفقیت در سیستم ذخیره و موجودی انبار کسر شد.');
886
+ document.getElementById('invoice-items-table-body').innerHTML = '';
887
+ addInvoiceItemRow();
888
+ refreshAllSurfaces();
889
+ }
890
+
891
+ function renderInvoices() {
892
+ const tbody = document.getElementById('invoice-archive-table-body');
893
+ if (datastore.invoices.length === 0) {
894
+ tbody.innerHTML = '<tr><td colspan="6" style="text-align:center; color:var(--text-muted);">سابقهای موجود نیست.</td></tr>';
895
+ return;
896
+ }
897
+
898
+ tbody.innerHTML = datastore.invoices.map(inv => `
899
+ <tr>
900
+ <td><strong>#${inv.number}</strong></td>
901
+ <td>${inv.contactName}</td>
902
+ <td>${inv.date}</td>
903
+ <td>${inv.grandTotal.toLocaleString('fa-IR')}</td>
904
+ <td>${inv.paymentMethodLabel}</td>
905
+ <td>
906
+ <button class="btn btn-secondary" style="padding:4px 8px; font-size:12px;" onclick="renderAndPrintDirect('${inv.id}')">🖨️ چاپ</button>
907
+ </td>
908
+ </tr>
909
+ `).join('');
910
+ }
911
+
912
+ function renderAndPrintDirect(invId) {
913
+ const inv = datastore.invoices.find(i => i.id === invId);
914
+ if (!inv) return;
915
+
916
+ document.getElementById('p-org-name').innerText = datastore.settings.store_name;
917
+ document.getElementById('p-org-details').innerText = `شماره تماس: ${datastore.settings.phone} | نشانی: ${datastore.settings.address}`;
918
+ document.getElementById('p-doc-number').innerText = inv.number;
919
+ document.getElementById('p-doc-date').innerText = inv.date;
920
+ document.getElementById('p-target-name').innerText = inv.contactName;
921
+ document.getElementById('p-doc-footer-note').innerText = datastore.settings.footer;
922
+
923
+ document.getElementById('p-target-items-table').innerHTML = inv.items.map((it, idx) => `
924
+ <tr>
925
+ <td style="border:1px solid #ccc; padding:6px; text-align:center;">${idx + 1}</td>
926
+ <td style="border:1px solid #ccc; padding:6px; text-align:right;">${it.prodName}</td>
927
+ <td style="border:1px solid #ccc; padding:6px; text-align:center;">${it.qty}</td>
928
+ <td style="border:1px solid #ccc; padding:6px; text-align:left;">${it.price.toLocaleString('fa-IR')}</td>
929
+ <td style="border:1px solid #ccc; padding:6px; text-align:left;">${it.lineTotal.toLocaleString('fa-IR')}</td>
930
+ </tr>
931
+ `).join('');
932
+
933
+ document.getElementById('p-calc-subtotal').innerText = inv.subtotal.toLocaleString('fa-IR');
934
+ document.getElementById('p-calc-discount').innerText = inv.discount.toLocaleString('fa-IR');
935
+ document.getElementById('p-calc-tax').innerText = inv.vat.toLocaleString('fa-IR');
936
+ document.getElementById('p-calc-grandtotal').innerText = inv.grandTotal.toLocaleString('fa-IR');
937
+ document.getElementById('p-calc-words').innerText = inv.grandTotal.toLocaleString('fa-IR') + ' تومان';
938
+
939
+ document.getElementById('printable-invoice').style.display = 'block';
940
+ window.print();
941
+ document.getElementById('printable-invoice').style.display = 'none';
942
+ }
943
+
944
+ function printActiveInvoice() {
945
+ alert('جهت چاپ و خروجی PDF، لطفاً ابتدا فاکتور را ثبت کنید.');
946
+ }
947
+
948
+ function commitSaveProduct() {
949
+ const code = document.getElementById('prod-code-input').value || (datastore.products.length + 101).toString();
950
+ const name = document.getElementById('prod-name-input').value;
951
+ const type = document.getElementById('prod-type-input').value;
952
+ const unit = document.getElementById('prod-unit-input').value || 'عدد';
953
+ const buy_price = parseFloat(document.getElementById('prod-buy-input').value) || 0;
954
+ const sale_price = parseFloat(document.getElementById('prod-sale-input').value) || 0;
955
+ const stock = parseFloat(document.getElementById('prod-stock-input').value) || 0;
956
+ const reorder = parseFloat(document.getElementById('prod-reorder-input').value) || 0;
957
+
958
+ if (!name) { alert('وارد کردن عنوان کالا الزامی است.'); return; }
959
+
960
+ datastore.products.push({ id: 'P_' + Date.now(), code, name, type, unit, buy_price, sale_price, stock, reorder });
961
+ persistDatastore();
962
+ alert('کالا با موفقیت ذخیره شد.');
963
+ document.getElementById('prod-name-input').value = '';
964
+ refreshAllSurfaces();
965
+ }
966
+
967
+ function renderProducts() {
968
+ const tbody = document.getElementById('products-catalog-table-body');
969
+ tbody.innerHTML = datastore.products.map(p => {
970
+ let badge = '<span class="badge badge-success">کافی</span>';
971
+ if (p.type === 'good' && p.stock <= p.reorder) {
972
+ badge = '<span class="badge badge-danger">کمبود موجودی</span>';
973
+ }
974
+ return `
975
+ <tr>
976
+ <td>${p.code}</td>
977
+ <td><strong>${p.name}</strong></td>
978
+ <td>${p.unit}</td>
979
+ <td>${p.buy_price.toLocaleString('fa-IR')}</td>
980
+ <td>${p.sale_price.toLocaleString('fa-IR')}</td>
981
+ <td>${p.type === 'good' ? p.stock.toLocaleString('fa-IR') : 'نامحدود'}</td>
982
+ <td>${p.type === 'good' ? badge : '<span class="badge">خدمات</span>'}</td>
983
+ </tr>
984
+ `;
985
+ }).join('');
986
+ }
987
+
988
+ function commitSaveContact() {
989
+ const name = document.getElementById('contact-name-input').value;
990
+ const role = document.getElementById('contact-role-input').value;
991
+ const mobile = document.getElementById('contact-mobile-input').value;
992
+ const national_id = document.getElementById('contact-national-input').value;
993
+ const address = document.getElementById('contact-address-input').value;
994
+
995
+ if (!name) { alert('ورود نام طرف حساب الزامی است.'); return; }
996
+
997
+ datastore.contacts.push({ id: 'C_' + Date.now(), name, role, mobile, national_id, address, balance: 0 });
998
+ persistDatastore();
999
+ alert('طرف حساب با موفقیت ثبت شد.');
1000
+ document.getElementById('contact-name-input').value = '';
1001
+ refreshAllSurfaces();
1002
+ }
1003
+
1004
+ function renderContacts() {
1005
+ const tbody = document.getElementById('contacts-ledger-table-body');
1006
+ const invSelect = document.getElementById('invoice-contact-id');
1007
+ const chqSelect = document.getElementById('chq-contact-select');
1008
+
1009
+ tbody.innerHTML = datastore.contacts.map(c => `
1010
+ <tr>
1011
+ <td><strong>${c.name}</strong></td>
1012
+ <td>${c.role === 'customer' ? 'مشتری' : (c.role === 'supplier' ? 'تامینکننده' : 'دوگانه')}</td>
1013
+ <td>${c.mobile || '—'}</td>
1014
+ <td>${Math.abs(c.balance || 0).toLocaleString('fa-IR')}</td>
1015
+ <td>${c.balance > 0 ? '<span class="badge badge-warning">بدهکار</span>' : (c.balance < 0 ? '<span class="badge badge-success">بستانکار</span>' : '<span class="badge">تسویه</span>')}</td>
1016
+ </tr>
1017
+ `).join('');
1018
+
1019
+ const options = datastore.contacts.map(c => `<option value="${c.id}">${c.name}</option>`).join('');
1020
+ invSelect.innerHTML = options;
1021
+ chqSelect.innerHTML = options;
1022
+ }
1023
+
1024
+ function commitSaveCheque() {
1025
+ const direction = document.getElementById('chq-direction-input').value;
1026
+ const contactSelect = document.getElementById('chq-contact-select');
1027
+ const contactName = contactSelect.options[contactSelect.selectedIndex]?.text || '';
1028
+ const sayad_id = document.getElementById('chq-sayad-input').value;
1029
+ const bank = document.getElementById('chq-bank-input').value;
1030
+ const amount = parseFloat(document.getElementById('chq-amount-input').value) || 0;
1031
+ const due_date = document.getElementById('chq-due-input').value;
1032
+ const status = document.getElementById('chq-status-input').value;
1033
+
1034
+ if (!sayad_id || amount <= 0) {
1035
+ alert('ورود شناسه صیاد و مبلغ معتبر الزامی است.');
1036
+ return;
1037
+ }
1038
+
1039
+ datastore.cheques.push({ id: 'CHQ_' + Date.now(), direction, contactName, sayad_id, bank, amount, due_date, status });
1040
+ persistDatastore();
1041
+ alert('چک صیاد با موفقیت ذخیره شد.');
1042
+ refreshAllSurfaces();
1043
+ }
1044
+
1045
+ function renderCheques() {
1046
+ const tbody = document.getElementById('cheques-ledger-table-body');
1047
+ if (datastore.cheques.length === 0) {
1048
+ tbody.innerHTML = '<tr><td colspan="6" style="text-align:center; color:var(--text-muted);">چک صیادی ثبت نشده است.</td></tr>';
1049
+ return;
1050
+ }
1051
+
1052
+ tbody.innerHTML = datastore.cheques.map(c => `
1053
+ <tr>
1054
+ <td>${c.direction === 'inbound' ? '📥 دریافتی' : '📤 پرداختی'}</td>
1055
+ <td>${c.contactName}</td>
1056
+ <td>${c.bank} - صیاد: ${c.sayad_id}</td>
1057
+ <td>${c.amount.toLocaleString('fa-IR')}</td>
1058
+ <td>${c.due_date}</td>
1059
+ <td><span class="badge ${c.status === 'cleared' ? 'badge-success' : (c.status === 'returned' ? 'badge-danger' : 'badge-warning')}">${c.status === 'cleared' ? 'وصول شد' : (c.status === 'returned' ? 'برگشتی' : 'در جریان')}</span></td>
1060
+ </tr>
1061
+ `).join('');
1062
+ }
1063
+
1064
+ function commitSaveExpense() {
1065
+ const kind = document.getElementById('trx-kind-input').value;
1066
+ const category = document.getElementById('trx-category-input').value;
1067
+ const amount = parseFloat(document.getElementById('trx-amount-input').value) || 0;
1068
+ const desc = document.getElementById('trx-desc-input').value;
1069
+
1070
+ if (amount <= 0) { alert('ورود مبلغ معتبر الزامی است.'); return; }
1071
+
1072
+ datastore.expenses.push({ id: 'TRX_' + Date.now(), kind, category, amount, desc });
1073
+ persistDatastore();
1074
+ alert('سند با موفقیت ذخیره شد.');
1075
+ refreshAllSurfaces();
1076
+ }
1077
+
1078
+ function renderExpenses() {
1079
+ const tbody = document.getElementById('expenses-ledger-table-body');
1080
+ if (datastore.expenses.length === 0) {
1081
+ tbody.innerHTML = '<tr><td colspan="4" style="text-align:center; color:var(--text-muted);">سندی ثبت نشده است.</td></tr>';
1082
+ return;
1083
+ }
1084
+
1085
+ tbody.innerHTML = datastore.expenses.map(e => `
1086
+ <tr>
1087
+ <td>${e.kind === 'expense' ? '🔴 هزینه' : '🟢 درآمد'}</td>
1088
+ <td>${e.category}</td>
1089
+ <td>${e.desc || '—'}</td>
1090
+ <td>${e.amount.toLocaleString('fa-IR')}</td>
1091
+ </tr>
1092
+ `).join('');
1093
+ }
1094
+
1095
+ function commitSaveSettings() {
1096
+ datastore.settings.store_name = document.getElementById('set-store-name').value;
1097
+ datastore.settings.phone = document.getElementById('set-phone-input').value;
1098
+ datastore.settings.tax_number = document.getElementById('set-tax-number').value;
1099
+ datastore.settings.address = document.getElementById('set-address-input').value;
1100
+ datastore.settings.footer = document.getElementById('set-footer-text').value;
1101
+ persistDatastore();
1102
+ alert('مشخصات با موفقیت ذخیره شد.');
1103
+ }
1104
+
1105
+ function renderSettings() {
1106
+ document.getElementById('set-store-name').value = datastore.settings.store_name || '';
1107
+ document.getElementById('set-phone-input').value = datastore.settings.phone || '';
1108
+ document.getElementById('set-tax-number').value = datastore.settings.tax_number || '';
1109
+ document.getElementById('set-address-input').value = datastore.settings.address || '';
1110
+ document.getElementById('set-footer-text').value = datastore.settings.footer || '';
1111
+ }
1112
+
1113
+ function exportDataBlob() {
1114
+ const blobString = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(datastore, null, 2));
1115
+ const anchor = document.createElement('a');
1116
+ anchor.setAttribute("href", blobString);
1117
+ anchor.setAttribute("download", `faktora-backup-${Date.now()}.json`);
1118
+ document.body.appendChild(anchor);
1119
+ anchor.click();
1120
+ anchor.remove();
1121
+ }
1122
+
1123
+ function importDataBlob(event) {
1124
+ const file = event.target.files[0];
1125
+ if (!file) return;
1126
+ const reader = new FileReader();
1127
+ reader.onload = function(e) {
1128
+ try {
1129
+ datastore = JSON.parse(e.target.result);
1130
+ persistDatastore();
1131
+ refreshAllSurfaces();
1132
+ alert('اطلاعات با موفقیت بازگردانی شد.');
1133
+ } catch(err) {
1134
+ alert('فرمت فایل پشتیبان معتبر نیست.');
1135
+ }
1136
+ };
1137
+ reader.readAsText(file);
1138
+ }
1139
+
1140
+ window.addEventListener('DOMContentLoaded', () => {
1141
+ initDatastore();
1142
+ addInvoiceItemRow();
1143
+ });
1144
+ </script>
1145
+</body>
1146
+</html>