diff --git a/src/routes/purchaseorder/+page.svelte b/src/routes/purchaseorder/+page.svelte index f5aa5bd..fb68dbc 100644 --- a/src/routes/purchaseorder/+page.svelte +++ b/src/routes/purchaseorder/+page.svelte @@ -3,24 +3,9 @@ import { supabase } from "$lib/supabaseClient"; import logo from "$lib/images/logo.webp"; - type TimesheetForm = { - entered_by: string; - work_description: string; - type_of_work: "Running" | "Periodic" | "Irregular"; - category_of_work: - | "Cleaning" - | "Gardening/Pool" - | "Maintenance" - | "Supervision" - | "Guest Service" - | "Administration" - | "Non Billable"; - villa_id: string; - datetime_in: string; - datetime_out: string; - total_work_hour: number; - remarks: string; - approval: boolean | null; // Allow null for new entries + type POItem = { + id: string; + item_name: string; }; type Villa = { @@ -33,37 +18,20 @@ name: string; }; - let employees: Employee[] = []; + let poItemOptions: POItem[] = []; let villas: Villa[] = []; + let employees: Employee[] = []; - let form: TimesheetForm = { - entered_by: "", - work_description: "", - type_of_work: "Running", - category_of_work: "Cleaning", + let addPOForm = { + po_type: "", villa_id: "", - datetime_in: "", - datetime_out: "", - total_work_hour: 0, - remarks: "", - approval: null, // Default null + po_item: "", + po_quantity: 0, + po_remark: "", + requested_by: "", + requested_date: "" }; - const typeOfWorkOptions: TimesheetForm["type_of_work"][] = [ - "Running", - "Periodic", - "Irregular", - ]; - const categoryOptions: TimesheetForm["category_of_work"][] = [ - "Cleaning", - "Gardening/Pool", - "Maintenance", - "Supervision", - "Guest Service", - "Administration", - "Non Billable", - ]; - onMount(async () => { // Fetch villas const { data: villaData, error: villaError } = await supabase @@ -90,49 +58,58 @@ } else if (empData) { employees = empData.map((e) => ({ id: e.id, name: e.employee_name })); } + + // Fetch PO Items + const { data: poItemData, error: poItemError } = await supabase + .from("vb_po_item") + .select("id, item_name") + .order("item_name", { ascending: true }); + + if (poItemError) { + console.error("Failed to fetch PO items:", poItemError.message); + } else if (poItemData) { + poItemOptions = poItemData; + } }); - function calculateTotalHours() { - if (form.datetime_in && form.datetime_out) { - const start = new Date(form.datetime_in); - const end = new Date(form.datetime_out); - const diffInMs = end.getTime() - start.getTime(); - const hours = diffInMs / (1000 * 60 * 60); - form.total_work_hour = Math.max(Number(hours.toFixed(2)), 0); - } else { - form.total_work_hour = 0; - } - } - async function submitForm() { - calculateTotalHours(); - - if (!form.entered_by) { + if (!addPOForm.requested_by) { alert("Please select an employee."); return; } - if (!form.villa_id) { + if (!addPOForm.villa_id) { alert("Please select a villa."); return; } + if (!addPOForm.po_item) { + alert("Please select a PO item."); + return; + } - const { error } = await supabase.from("vb_timesheet").insert([form]); + const { error } = await supabase.from("vb_purchase_orders").insert({ + po_type: addPOForm.po_type, + villa_id: addPOForm.villa_id, + po_item: addPOForm.po_item, + po_quantity: addPOForm.po_quantity, + po_status: "requested", + po_remark: addPOForm.po_remark, + requested_by: addPOForm.requested_by, + requested_date: addPOForm.requested_date, + created_at: new Date().toISOString() + }); if (error) { - alert("Failed to submit timesheet: " + error.message); + alert("Failed to submit Purchase Order: " + error.message); } else { - alert("Timesheet submitted successfully!"); - form = { - entered_by: "", - work_description: "", - type_of_work: "Running", - category_of_work: "Cleaning", + alert("Purchase Order submitted successfully!"); + addPOForm = { + po_type: "", villa_id: "", - datetime_in: "", - datetime_out: "", - total_work_hour: 0, - remarks: "", - approval: null, + po_item: "", + po_quantity: 0, + po_remark: "", + requested_by: "", + requested_date: "" }; } } @@ -148,127 +125,64 @@

Timesheet Entry

- - + +
- - + +
- - + +
- - + +
- - + +
- - + +
- - + +
- -
- -
{form.total_work_hour}
-
- -
- - -
-