-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathap.js
More file actions
268 lines (208 loc) · 7.92 KB
/
ap.js
File metadata and controls
268 lines (208 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
document.addEventListener("DOMContentLoaded", function () {
/* =========================
SAFE GETTER
========================== */
const $ = (id) => document.getElementById(id);
function parseAmount(value) {
if (!value) return 0;
return Number(String(value).replace(/[^\d.-]/g, "")) || 0;
}
/* =========================
TAB SYSTEM FIXED
========================== */
window.showTab = function (id) {
document.querySelectorAll(".tab").forEach(tab => {
tab.style.display = "none";
});
const activeTab = $(id);
if (activeTab) activeTab.style.display = "block";
};
/* =========================
SHARE APP
========================== */
window.shareApp = function () {
const url = window.location.origin;
if (navigator.share) {
navigator.share({
title: "Ghar Manager",
text: "Check out my Ghar Manager App 🔥",
url
}).catch(() => {});
} else {
window.open("https://wa.me/?text=" + encodeURIComponent(url));
}
};
/* =========================
DATA LOAD
========================== */
let kitchenData = JSON.parse(localStorage.getItem("kitchenData")) || [];
let tempItems = [];
/* =========================
MASTER ITEMS
========================== */
const masterItems = {
Spices: ["Mirch/मिर्च","Haldi/हल्दी","Dhaniya/धनिया","Jeera/जीरा","Garam Masala/गरम मसाला","कड़ी पत्ता","बेसन","मैदा"],
Oils: ["Mustard Oil/सरसों तेल 1L","Mustard Oil 5L","Refined Oil/रिफाइंड 1L"],
Grains: ["Rice/चावल","Atta/आटा","सफेद चना","काले छोले","मूंग दाल","चना दाल","काली दाल"],
Shabji: ["टमाटर","मटर","गोभी","आलू","प्याज","बैंगन","पत्ता गोभी"],
Dairy: ["देसी घी","पनीर","दूध","दही"],
Snacks: ["बिस्कुट","चिप्स","भुजिया","समोसा"],
Bathroom: ["Soap 2 Pack","Soap 4 Pack","Surf Excel","Vanish"]
};
const categoryEl = $("category");
const itemEl = $("itemSelect");
function loadCategories() {
if (!categoryEl) return;
categoryEl.innerHTML =
`<option disabled selected>Select Category</option>` +
Object.keys(masterItems)
.map(cat => `<option value="${cat}">${cat}</option>`)
.join("");
loadItems();
}
function loadItems() {
if (!itemEl) return;
const selectedCategory = categoryEl.value;
if (!masterItems[selectedCategory]) return;
itemEl.innerHTML = masterItems[selectedCategory]
.map(item => `<option value="${item}">${item}</option>`)
.join("");
}
categoryEl?.addEventListener("change", loadItems);
/* =========================
PDF EXPORT
========================== */
$("pdfBtn")?.addEventListener("click", async function () {
const rows = document.querySelectorAll("#kitchenTable tr");
if (!rows.length) return alert("No data available!");
const invoice = $("invoiceTemplate");
if (!invoice) return;
const tbody = invoice.querySelector("tbody");
if (!tbody) return;
tbody.innerHTML = "";
let grandTotal = 0;
rows.forEach(row => {
const cols = row.querySelectorAll("td");
if (cols.length < 3) return;
const date = cols[0].innerText;
const items = cols[1].innerText;
const total = cols[2].innerText;
const amount = parseAmount(total);
grandTotal += amount;
const tr = document.createElement("tr");
tr.innerHTML = `
<td style="border:1px solid #ccc;padding:6px;">${date}</td>
<td style="border:1px solid #ccc;padding:6px;">${items}</td>
<td style="border:1px solid #ccc;padding:6px;color:red;">₹ ${amount}</td>
`;
tbody.appendChild(tr);
});
if ($("invoiceDate"))
$("invoiceDate").innerText = "Generated: " + new Date().toLocaleString();
if ($("invoiceGrandTotal"))
$("invoiceGrandTotal").innerText = "Grand Total: ₹ " + grandTotal;
await new Promise(r => setTimeout(r, 300));
const canvas = await html2canvas(invoice, { scale: 2 });
const imgData = canvas.toDataURL("image/png");
const { jsPDF } = window.jspdf;
const doc = new jsPDF("p", "mm", "a4");
const imgWidth = 190;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
doc.addImage(imgData, "PNG", 10, 10, imgWidth, imgHeight);
doc.save("Ghar_Manager_Invoice.pdf");
});
/* =========================
IMAGE EXPORT
========================== */
$("imgBtn")?.addEventListener("click", async function () {
const table = $("kitchenTable");
if (!table || table.innerHTML.trim() === "")
return alert("No data to capture!");
await new Promise(r => setTimeout(r, 200));
const canvas = await html2canvas(table, { scale: 2 });
const link = document.createElement("a");
link.download = "Kitchen_Table.png";
link.href = canvas.toDataURL("image/png");
link.click();
});
/* =========================
WHATSAPP SHARE
========================== */
window.shareTable = function () {
const rows = document.querySelectorAll("#kitchenTable tr");
if (!rows.length) return alert("No data to share!");
let totalAmount = 0;
let message =
"━━━━━━━━━━━━━━━━━━━━\n🏠 *GHAR MANAGER*\n🍳 *Kitchen Report*\n━━━━━━━━━━━━━━━━━━━━\n\n";
rows.forEach(row => {
const cols = row.querySelectorAll("td");
if (cols.length < 3) return;
const date = cols[0].innerText;
const items = cols[1].innerText;
const total = cols[2].innerText;
const amount = parseAmount(total);
totalAmount += amount;
message +=
`📅 ${date}
🛒 ${items}
💰 ₹ ${amount}
──────────────────\n\n`;
});
message += `💎 Grand Total: ₹ ${totalAmount}`;
const url =
/Android|iPhone/i.test(navigator.userAgent)
? `https://wa.me/?text=${encodeURIComponent(message)}`
: `https://web.whatsapp.com/send?text=${encodeURIComponent(message)}`;
window.open(url, "_blank");
};
/* =========================
SAVE ENTRY
========================== */
window.saveKitchenEntry = function () {
const total = parseAmount($("totalAmount")?.value);
if (!tempItems.length) return alert("Add items first!");
kitchenData.push({
date: new Date().toISOString(),
items: [...tempItems],
total
});
localStorage.setItem("kitchenData", JSON.stringify(kitchenData));
tempItems = [];
renderKitchen();
};
/* =========================
RENDER TABLE
========================== */
function renderKitchen() {
const table = $("kitchenTable");
if (!table) return;
table.innerHTML = kitchenData.map((entry, index) => `
<tr>
<td>${new Date(entry.date).toLocaleString()}</td>
<td>${entry.items.join("<br>")}</td>
<td>₹ ${entry.total}</td>
<td><button type="button" onclick="deleteEntry(${index})">Delete</button></td>
</tr>
`).join("");
updateSummary();
}
window.deleteEntry = function (i) {
kitchenData.splice(i, 1);
localStorage.setItem("kitchenData", JSON.stringify(kitchenData));
renderKitchen();
};
function updateSummary() {
const total = kitchenData.reduce((sum, e) => sum + e.total, 0);
if ($("grandTotal")) $("grandTotal").innerText = total;
if ($("kitchenTotal")) $("kitchenTotal").innerText = total;
if ($("todayTotal")) $("todayTotal").innerText = total;
if ($("weekTotal")) $("weekTotal").innerText = total;
if ($("monthTotal")) $("monthTotal").innerText = total;
}
/* =========================
INIT
========================== */
showTab("dashboard"); // default tab
renderKitchen();
loadCategories();
});