@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 63787ba
Fork 0

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

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

Synced Dec 3, 19:30 UTC

Implement safe migration logic for datastore

Added safe migration logic for datastore and updated default company handling.

davoodmehraban89 committed Sep 16, 2026 at 21:34 UTC 63787ba025ab6a1a37770a1d0e5b89000f6a2d73
1 file changed +64 -20
index.html
+64 -20
@@ -950,6 +950,18 @@
950 if (persisted) {
951 try { datastore = JSON.parse(persisted); } catch(e) {}
952 }
953 +
954 + // Safe migration logic
955 + if (!datastore.default_settings) {
956 + datastore.default_settings = {};
957 + }
958 + if (!Array.isArray(datastore.companies)) {
959 + datastore.companies = [];
960 + }
961 + if (!datastore.default_settings.default_company_id && datastore.companies.length > 0) {
962 + datastore.default_settings.default_company_id = datastore.companies[0].id;
963 + }
964 +
965 persistDatastore();
966 refreshAllSurfaces();
967 }
@@ -991,7 +1003,6 @@
1003 function refreshAllSurfaces() {
1004 document.getElementById('dashboard-date-display').innerText = getJalaliDate();
1005 document.getElementById('invoice-date-input').value = getJalaliNumeric();
994 - document.getElementById('lbl-build-date').innerText = APP_BUILD_TIMESTAMP;
1006
1007 renderCompanies();
1008 renderContacts();
@@ -1064,7 +1075,7 @@
1075 function renderDashboard() {
1076 let salesSum = 0;
1077 let debtorsSum = 0;
1067 - const activeCompId = document.getElementById('invoice-company-id')?.value || datastore.companies[0]?.id;
1078 + const activeCompId = datastore.default_settings?.default_company_id || document.getElementById('invoice-company-id')?.value || (datastore.companies[0]?.id);
1079 const filteredInvoices = datastore.invoices.filter(inv => inv.companyId === activeCompId);
1080
1081 filteredInvoices.forEach(inv => {
@@ -1176,15 +1187,22 @@
1187 document.getElementById('summary-vat').innerText = vatAmount.toLocaleString('fa-IR') + ' ریال';
1188 document.getElementById('summary-grandtotal').innerText = grandTotal.toLocaleString('fa-IR') + ' ریال';
1189 document.getElementById('summary-words').innerText = 'به حروف: ' + grandTotal.toLocaleString('fa-IR') + ' ریال';
1179 - document.getElementById('invoice-grand-total-display').value = grandTotal.toLocaleString('fa-IR') + ' ریال';
1190
1191 return { subtotal, fixedDiscount, vatAmount, grandTotal, vatRate };
1192 }
1193
1194 function commitSaveInvoice() {
1195 const companySelect = document.getElementById('invoice-company-id');
1186 - const companyId = companySelect.value;
1187 - const companyName = companySelect.options[companySelect.selectedIndex]?.text || '';
1196 + const selectedCompanyId = companySelect ? companySelect.value : (datastore.default_settings?.default_company_id);
1197 +
1198 + const sellerCompany = datastore.companies.find(
1199 + company => company.id === selectedCompanyId
1200 + ) || datastore.companies.find(
1201 + company => company.id === datastore.default_settings?.default_company_id
1202 + ) || datastore.companies[0];
1203 +
1204 + const companyId = sellerCompany ? sellerCompany.id : selectedCompanyId;
1205 + const companyName = sellerCompany ? sellerCompany.name : (companySelect?.options[companySelect.selectedIndex]?.text || '');
1206
1207 const contactSelect = document.getElementById('invoice-contact-id');
1208 const contactId = contactSelect.value;
@@ -1192,7 +1210,7 @@
1210 const number = document.getElementById('invoice-number').value || ('PINV-' + (datastore.invoices.length + 31));
1211 const date = document.getElementById('invoice-date-input').value;
1212 const kind = document.getElementById('invoice-kind').value;
1195 - const paymentMethod = document.getElementById('invoice-payment-method').value;
1213 + const paymentMethod = document.getElementById('invoice-payment-method')?.value || 'cash';
1214 const description = document.getElementById('invoice-desc-input').value;
1215 const editId = document.getElementById('edit-invoice-id').value;
1216
@@ -1281,7 +1299,8 @@
1299 document.getElementById('invoice-number').value = inv.number;
1300 document.getElementById('invoice-contact-id').value = inv.contactId;
1301 document.getElementById('invoice-date-input').value = inv.date;
1284 - document.getElementById('invoice-payment-method').value = inv.paymentMethod;
1302 + const pmEl = document.getElementById('invoice-payment-method');
1303 + if (pmEl) pmEl.value = inv.paymentMethod;
1304 document.getElementById('invoice-discount-fixed').value = inv.discount || 0;
1305 document.getElementById('invoice-desc-input').value = inv.description || '';
1306
@@ -1345,7 +1364,7 @@
1364 const inv = datastore.invoices.find(i => i.id === invId);
1365 if (!inv) return;
1366
1348 - const seller = datastore.companies.find(c => c.id === inv.companyId) || datastore.companies[0] || {};
1367 + const seller = datastore.companies.find(c => c.id === inv.companyId) || datastore.companies.find(c => c.id === datastore.default_settings?.default_company_id) || datastore.companies[0] || {};
1368 const contact = datastore.contacts.find(c => c.id === inv.contactId) || {};
1369
1370 const itemsPerPage = 10;
@@ -1502,7 +1521,7 @@
1521 const code = document.getElementById('prod-code-input').value || (datastore.products.length + 1).toString();
1522 const name = document.getElementById('prod-name-input').value;
1523 const spec = document.getElementById('prod-spec-input').value || '';
1505 - const type = document.getElementById('prod-type-input').value;
1524 + const type = document.getElementById('prod-type-input')?.value || 'good';
1525 const unit = document.getElementById('prod-unit-input').value || 'عدد';
1526 const buy_price = parseFloat(document.getElementById('prod-buy-input').value) || 0;
1527 const sale_price = parseFloat(document.getElementById('prod-sale-input').value) || 0;
@@ -1766,7 +1785,7 @@
1785 datastore.expenses.map(e => `<tr><td>${e.kind}</td><td>${e.category}</td><td>${e.desc}</td><td>${e.amount.toLocaleString('fa-IR')}</td></tr>`).join('');
1786 }
1787
1769 - // ۷. شرکت‌ها (اصلاح کامل ثبت شرکت)
1788 + // ۷. شرکت‌ها (اصلاح کامل ثبت شرکت و فعال‌سازی فوری)
1789 function commitSaveCompany() {
1790 const entity_type = document.getElementById('company-entity-type').value;
1791 const raw_name = document.getElementById('company-name-input').value;
@@ -1786,6 +1805,12 @@
1805 };
1806
1807 datastore.companies.push(newComp);
1808 +
1809 + if (!datastore.default_settings) {
1810 + datastore.default_settings = {};
1811 + }
1812 + datastore.default_settings.default_company_id = newComp.id;
1813 +
1814 persistDatastore();
1815 alert('شرکت با موفقیت ثبت و ذخیره شد.');
1816 document.getElementById('company-name-input').value = '';
@@ -1795,6 +1820,9 @@
1820 document.getElementById('company-reg-input').value = '';
1821 document.getElementById('company-postal-input').value = '';
1822 document.getElementById('company-address-input').value = '';
1823 + if (document.getElementById('company-footer-input')) {
1824 + document.getElementById('company-footer-input').value = '';
1825 + }
1826 refreshAllSurfaces();
1827 }
1828
@@ -1804,17 +1832,25 @@
1832 const filterCompSelect = document.getElementById('filter-invoice-company');
1833 const defCompId = datastore.default_settings?.default_company_id || (datastore.companies[0]?.id);
1834
1807 - tbody.innerHTML = datastore.companies.map(c => `
1808 - <tr>
1809 - <td><strong>${c.name}</strong></td>
1810 - <td>${c.national_id || '—'}</td>
1811 - <td>${c.economic_code || '—'}</td>
1812 - <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>
1813 - <td>${datastore.companies.length > 1 ? `<button class="btn btn-danger btn-inline" style="padding:2px 8px; font-size:11px;" onclick="deleteCompany('${c.id}')">حذف</button>` : '—'}</td>
1814 - </tr>
1815 - `).join('');
1835 + if (tbody) {
1836 + tbody.innerHTML = datastore.companies.map(c => `
1837 + <tr>
1838 + <td><strong>${c.name}</strong></td>
1839 + <td>${c.national_id || '—'}</td>
1840 + <td>${c.economic_code || '—'}</td>
1841 + <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>
1842 + <td>${datastore.companies.length > 1 ? `<button class="btn btn-danger btn-inline" style="padding:2px 8px; font-size:11px;" onclick="deleteCompany('${c.id}')">حذف</button>` : '—'}</td>
1843 + </tr>
1844 + `).join('');
1845 + }
1846 +
1847 + if (invoiceCompSelect) {
1848 + invoiceCompSelect.innerHTML = datastore.companies.map(c => `<option value="${c.id}" ${c.id === defCompId ? 'selected' : ''}>${c.name}</option>`).join('');
1849 + if (defCompId) {
1850 + invoiceCompSelect.value = defCompId;
1851 + }
1852 + }
1853
1817 - invoiceCompSelect.innerHTML = datastore.companies.map(c => `<option value="${c.id}" ${c.id === defCompId ? 'selected' : ''}>${c.name}</option>`).join('');
1854 if (filterCompSelect) {
1855 const currentVal = filterCompSelect.value || 'ALL';
1856 filterCompSelect.innerHTML = `<option value="ALL">نمایش همه شرکت‌ها</option>` + datastore.companies.map(c => `<option value="${c.id}">فقط ${c.name}</option>`).join('');
@@ -1832,6 +1868,9 @@
1868 function deleteCompany(id) {
1869 if (confirm('حذف شود؟')) {
1870 datastore.companies = datastore.companies.filter(c => c.id !== id);
1871 + if (datastore.default_settings?.default_company_id === id) {
1872 + datastore.default_settings.default_company_id = datastore.companies[0]?.id || null;
1873 + }
1874 persistDatastore();
1875 refreshAllSurfaces();
1876 }
@@ -1854,6 +1893,11 @@
1893 reader.onload = function(e) {
1894 try {
1895 datastore = JSON.parse(e.target.result);
1896 + if (!datastore.default_settings) datastore.default_settings = {};
1897 + if (!Array.isArray(datastore.companies)) datastore.companies = [];
1898 + if (!datastore.default_settings.default_company_id && datastore.companies.length > 0) {
1899 + datastore.default_settings.default_company_id = datastore.companies[0].id;
1900 + }
1901 persistDatastore();
1902 refreshAllSurfaces();
1903 alert('اطلاعات بازگردانی شد.');