zenityx commited on
Commit
6a9cf26
·
verified ·
1 Parent(s): 3bd567e

Update admin.js

Browse files
Files changed (1) hide show
  1. admin.js +40 -22
admin.js CHANGED
@@ -59,7 +59,6 @@ const menu = [
59
  }
60
  ];
61
 
62
- // สร้าง dropdown เมนู
63
  function renderMenuDropdown() {
64
  const select = document.getElementById("add-menu");
65
  let html = `<option value="">เลือกเมนู</option>`;
@@ -71,7 +70,6 @@ function renderMenuDropdown() {
71
  select.innerHTML = html;
72
  }
73
 
74
- // filter ออเดอร์ที่ status=unpaid และเลขโต๊ะตรงเป๊ะ
75
  function filterOrders(data, table) {
76
  return data.filter(
77
  i => (i.status ?? "unpaid") === "unpaid" && String(i.table).trim() === String(table)
@@ -113,23 +111,17 @@ async function loadOrder() {
113
  document.getElementById("total").textContent = sum ? `รวมทั้งหมด ${sum} ฿` : "";
114
  }
115
 
116
- // เพิ่มรายการออเดอร์
117
  async function addOrder() {
118
  const table = document.getElementById("table").value;
119
  const menuName = document.getElementById("add-menu").value;
120
  const qty = parseInt(document.getElementById("add-qty").value, 10) || 1;
121
-
122
- // หาราคาจากเมนู
123
  let price = 0;
124
  menu.forEach(section => {
125
- section.items.forEach(item => {
126
- if(item.name === menuName) price = item.price;
127
- });
128
  });
129
-
130
  if (!menuName || !qty) return alert("กรุณาเลือกเมนูและจำนวน");
131
-
132
- const now = new Date().toISOString().split(".")[0] + "Z"; // ไม่มี millisecond
133
  for (let i = 0; i < qty; i++) {
134
  const order = {
135
  table: table,
@@ -150,28 +142,54 @@ async function addOrder() {
150
  loadOrder();
151
  }
152
 
153
- // ลบเมนู = PATCH เป็น deleted (เฉพาะ status=unpaid)
154
  async function removeOrder(menuName) {
155
  const table = document.getElementById("table").value;
156
  if (!confirm(`ต้องการลบเมนู "${menuName}" ออกจากโต๊ะ ${table} ?`)) return;
157
-
158
- await fetch(`${scriptURL}?table=${encodeURIComponent(table)}&menu=${encodeURIComponent(menuName)}&status=unpaid`, {
159
- method: "PATCH",
 
 
 
 
 
 
 
 
 
160
  headers: { "Content-Type": "application/json" },
161
- body: JSON.stringify({ status: "deleted" }),
162
  });
163
  alert("ลบเมนูสำเร็จ");
164
  loadOrder();
165
  }
166
 
167
- // เช็กบิล (เปลี่ยน status เป็น paid เฉพาะออเดอที่ยัง unpaid)
168
  async function checkout() {
169
  const table = document.getElementById("table").value;
170
- await fetch(`${scriptURL}?table=${encodeURIComponent(table)}&status=unpaid`, {
171
- method: "PATCH",
172
- headers: { "Content-Type": "application/json" },
173
- body: JSON.stringify({ status: "paid" }),
174
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  alert("เช็กบิลเรียบร้อย!");
176
  loadOrder();
177
  }
 
59
  }
60
  ];
61
 
 
62
  function renderMenuDropdown() {
63
  const select = document.getElementById("add-menu");
64
  let html = `<option value="">เลือกเมนู</option>`;
 
70
  select.innerHTML = html;
71
  }
72
 
 
73
  function filterOrders(data, table) {
74
  return data.filter(
75
  i => (i.status ?? "unpaid") === "unpaid" && String(i.table).trim() === String(table)
 
111
  document.getElementById("total").textContent = sum ? `รวมทั้งหมด ${sum} ฿` : "";
112
  }
113
 
114
+ // เพิ่มออเดอร์ (POST status=unpaid)
115
  async function addOrder() {
116
  const table = document.getElementById("table").value;
117
  const menuName = document.getElementById("add-menu").value;
118
  const qty = parseInt(document.getElementById("add-qty").value, 10) || 1;
 
 
119
  let price = 0;
120
  menu.forEach(section => {
121
+ section.items.forEach(item => { if(item.name === menuName) price = item.price; });
 
 
122
  });
 
123
  if (!menuName || !qty) return alert("กรุณาเลือกเมนูและจำนวน");
124
+ const now = new Date().toISOString().split(".")[0] + "Z";
 
125
  for (let i = 0; i < qty; i++) {
126
  const order = {
127
  table: table,
 
142
  loadOrder();
143
  }
144
 
145
+ // ลบเมนู = POST status=deleted
146
  async function removeOrder(menuName) {
147
  const table = document.getElementById("table").value;
148
  if (!confirm(`ต้องการลบเมนู "${menuName}" ออกจากโต๊ะ ${table} ?`)) return;
149
+ const now = new Date().toISOString().split(".")[0] + "Z";
150
+ const order = {
151
+ table: table,
152
+ menu: menuName,
153
+ price: 0,
154
+ qty: 1,
155
+ note: "[ลบโดยแอดมิน]",
156
+ timestamp: now,
157
+ status: "deleted"
158
+ };
159
+ await fetch(scriptURL, {
160
+ method: "POST",
161
  headers: { "Content-Type": "application/json" },
162
+ body: JSON.stringify(order),
163
  });
164
  alert("ลบเมนูสำเร็จ");
165
  loadOrder();
166
  }
167
 
168
+ // เช็กบิล = POST status=paid สำหับุกเมนูที่ยัง unpaid ในโต๊ะนี้
169
  async function checkout() {
170
  const table = document.getElementById("table").value;
171
+ const res = await fetch(`${scriptURL}?table=${table}`);
172
+ const data = await res.json();
173
+ const unpaidOrders = data.filter(
174
+ i => (i.status ?? "unpaid") === "unpaid" && String(i.table).trim() === String(table)
175
+ );
176
+ const now = new Date().toISOString().split(".")[0] + "Z";
177
+ for (const order of unpaidOrders) {
178
+ const paidOrder = {
179
+ table: order.table,
180
+ menu: order.menu,
181
+ price: order.price,
182
+ qty: order.qty,
183
+ note: "[เช็กบิล]",
184
+ timestamp: now,
185
+ status: "paid"
186
+ };
187
+ await fetch(scriptURL, {
188
+ method: "POST",
189
+ headers: { "Content-Type": "application/json" },
190
+ body: JSON.stringify(paidOrder),
191
+ });
192
+ }
193
  alert("เช็กบิลเรียบร้อย!");
194
  loadOrder();
195
  }