const STORAGE_KEY = "shvm_water_company_data_v3"; const DEFAULT_CONSUMPTION_LISTS = { arbaab:[], mataraffe:[], khorak:[], barq:[], teel:[], custom:[] }; /* The areas are not personal data, so they live here in the code (they used to come from data/customers.js). */ const BUILTIN_AREAS = [{"id":"shahrak_shora","label":"شهرک همت","labelEn":"Shahrak Hemmat"},{"id":"sangchal","label":"سنگچال","labelEn":"Sangchal"},{"id":"sarak_kohna_awal","label":"سرک کهنه اول","labelEn":"Sarak Kohna Awal"},{"id":"sarak_kohna_doom","label":"سرک کهنه دوم","labelEn":"Sarak Kohna Doom"},{"id":"qala_naw_awal","label":"قلعه نو اول","labelEn":"Qala Naw Awal"},{"id":"qala_naw_doom","label":"قلعه نو دوم","labelEn":"Qala Naw Doom"},{"id":"bagh_haji_nawroz","label":"باغ حاجی نوروز","labelEn":"Bagh Haji Nawroz"},{"id":"bazar","label":"بازار","labelEn":"Bazar"},{"id":"saha_zamin","label":"ساحه ظامین","labelEn":"Saha Zamin"}]; /* Built-in data files (data/customers.js, data/lists.js, data/code-lists.js). They contain personal data and are only needed until everything has been copied to Firestore. After that they are no longer published, and the app must keep working without them - so they are only ever read through these `typeof` checks (a missing file then simply means "no built-in data"). */ const BUILTIN = { hasCustomers: typeof IMPORTED_DATA !== "undefined", customers: typeof IMPORTED_DATA !== "undefined" ? IMPORTED_DATA.customers : [], hasLists: typeof IMPORTED_LISTS !== "undefined", lists: typeof IMPORTED_LISTS !== "undefined" ? IMPORTED_LISTS : { consumptionLists: { ...DEFAULT_CONSUMPTION_LISTS }, employeeExpenses: [] }, hasCodeBase: typeof CODE_ZAMIN_DATA !== "undefined" && typeof CODE_OMOMI_DATA !== "undefined" && typeof CODE_MAWAQAT_DATA !== "undefined", codeBase: (typeof CODE_ZAMIN_DATA !== "undefined" && typeof CODE_OMOMI_DATA !== "undefined" && typeof CODE_MAWAQAT_DATA !== "undefined") ? { zamin: CODE_ZAMIN_DATA, omomi: CODE_OMOMI_DATA, mawaqat: CODE_MAWAQAT_DATA } : null, }; /* The base rows of the code lists (کد ضامن / عمومی / موقت). Stored in Firestore as app/codeBase; the built-in file is only used until that copy exists. Returns undefined when neither exists, so nothing empty is ever written over a real cloud copy. codeEdits refer to these rows by position, so the order is kept exactly as it is. */ function pickCodeBase(saved){ const cb = saved && saved.codeBase; if(cb && Array.isArray(cb.zamin) && Array.isArray(cb.omomi) && Array.isArray(cb.mawaqat)) return cb; return BUILTIN.codeBase || undefined; } function loadData(){ const defaultData = { company: { nameDari: "شرکت خدمات آبرسانی شهید وحدت ملی", nameEn: "Shaheed Wahdat-e-Milli Water Supply Services Company", address: "", phone: "", }, // No default users: the first employee is approved via Settings > System // Users after a one-time Firestore bootstrap step (see FIREBASE_SETUP.md), // then creates their own account (email + password) on the sign-up screen. users: [], areas: BUILTIN_AREAS, customers: BUILTIN.customers, employees: [], expenses: { employee: BUILTIN.lists.employeeExpenses, other: [], purchases: [] }, consumptionLists: { ...BUILTIN.lists.consumptionLists }, codeBase: BUILTIN.codeBase || undefined, codeLists: { zamin: [], omomi: [], mawaqat: [] }, codeEdits: { zamin: {}, omomi: {}, mawaqat: {} }, }; try{ const raw = FBStore.getRaw(); if(raw){ const saved = JSON.parse(raw); // Everything the user has saved is kept permanently and is never reset // from the built-in IMPORTED data on reload. The built-in data is only a // fallback for things that have never been saved yet. const tombstones = (saved.tombstones && typeof saved.tombstones === "object") ? saved.tombstones : {}; const deletedCust = tombstones.customers || {}; const savedCustomers = Array.isArray(saved.customers) ? saved.customers : []; const savedCustById = new Map(savedCustomers.map(sc => [sc.id, sc])); const pickList = (savedVal, fallback) => Array.isArray(savedVal) ? savedVal : fallback; const savedCL = saved.consumptionLists || {}; const consumptionLists = { ...BUILTIN.lists.consumptionLists }; Object.keys(consumptionLists).forEach(k => { consumptionLists[k] = pickList(savedCL[k], consumptionLists[k]); }); Object.keys(savedCL).forEach(k => { if(!(k in consumptionLists) && Array.isArray(savedCL[k])) consumptionLists[k] = savedCL[k]; }); consumptionLists.custom = pickList(savedCL.custom, []); return { ...defaultData, ...saved, company: saved.company || defaultData.company, users: saved.users || defaultData.users, employees: saved.employees || [], areas: BUILTIN_AREAS, customers: BUILTIN.customers // keep the user's saved version of each customer (all edits + receipts) .map(freshCust => { const savedCust = savedCustById.get(freshCust.id); if(savedCust) return { ...freshCust, ...savedCust, receipts: savedCust.receipts || freshCust.receipts || [] }; if(savedCustomers.length && deletedCust[freshCust.id]) return null; // deleted by the user — stays deleted return freshCust; }) .filter(Boolean) .concat( // customers added through the app savedCustomers.filter(sc => !BUILTIN.customers.some(fc=>fc.id===sc.id)) ), expenses: { employee: pickList(saved.expenses?.employee, BUILTIN.lists.employeeExpenses), other: saved.expenses?.other || [], purchases: saved.expenses?.purchases || [], }, consumptionLists, codeBase: pickCodeBase(saved), codeLists: { zamin: saved.codeLists?.zamin || [], omomi: saved.codeLists?.omomi || [], mawaqat: saved.codeLists?.mawaqat || [], }, codeEdits: { zamin: saved.codeEdits?.zamin || {}, omomi: saved.codeEdits?.omomi || {}, mawaqat: saved.codeEdits?.mawaqat || {}, }, tombstones, }; } }catch(e){} return defaultData; } function saveData(data){ try{ FBStore.save(data); }catch(e){} }