This commit is contained in:
Aji Setiaji
2025-07-14 12:13:10 +07:00
3 changed files with 131 additions and 354 deletions

View File

@@ -7,9 +7,9 @@
| "vb_issues"
| "vb_villas"
| "vb_timesheet"
| "vb_inventories"
| "vb_inventory"
| "vb_projects"
| "vb_vendors"
| "vb_vendor"
| "vb_employee"
| "vb_transport"
| "vb_dinning"
@@ -20,9 +20,9 @@
vb_issues: 0,
vb_villas: 0,
vb_timesheet: 0,
vb_inventories: 0,
vb_inventory: 0,
vb_projects: 0,
vb_vendors: 0,
vb_vendor: 0,
vb_employee: 0,
vb_transport: 0,
vb_dinning: 0,
@@ -67,13 +67,13 @@
},
{
label: "Total Inventories",
key: "vb_inventories",
key: "vb_inventory",
color: "text-yellow-600",
icon: "cube",
},
{
label: "Total Vendor",
key: "vb_vendors",
key: "vb_vendor",
color: "text-orange-600",
icon: "building-storefront",
},

View File

@@ -143,13 +143,26 @@
unit_price: 0,
description: "",
};
let selectedYear: number | null = new Date().getFullYear(); // Current year
let selectedYear: number | null = null;
$: if (selectedYear) {
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}
export let data;
async function fetchInventory(
searchTerm: string | null = "",
searchedVillaId: string | null = null,
year: number | null = null,
sortBy = "created_at",
sortOrder = "desc",
offset = 0,
@@ -169,6 +182,10 @@
query = query.eq("villa_id", searchedVillaId);
}
if (year) {
query = query.or(`year.eq.${year},year.eq.0`);
}
const { data, error, count } = await query;
if (error) {
@@ -257,7 +274,17 @@
}
onMount(async () => {
await fetchInventory();
selectedYear = new Date().getFullYear();
await fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
// Fetch villa names for dropdown
const { data: villaData, error: villaError } = await supabase
@@ -309,7 +336,15 @@
} else {
alert("Item updated successfully!");
showModal = false;
await fetchInventory();
await fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}
} else {
// Insert new item
@@ -324,7 +359,15 @@
} else {
alert("New item added successfully!");
showModal = false;
await fetchInventory();
await fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}
}
}
@@ -335,6 +378,8 @@
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
(currentPage - 1) * rowsPerPage,
@@ -375,6 +420,8 @@
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
(currentPage - 1) * rowsPerPage,
@@ -394,10 +441,15 @@
<span>📋</span>
Inventory Management
</h2>
<p class="text-sm text-gray-600">
<p class="text-sm text-gray-600 mb-2">
Manage your inventory items, track stock levels, and ensure
everything is in order.
</p>
<p class="text-xs text-gray-500 italic mb-2">
Note : Tekan tombol Enter untuk menyimpan. <span
class="text-red-500">⚠️</span
>
</p>
</div>
<div class="flex flex-col sm:flex-row sm:items-center gap-2">
<input
@@ -408,7 +460,15 @@
const searchTerm = (
e.target as HTMLInputElement
).value.toLowerCase();
fetchInventory(null, "created_at", "desc", 0, 10);
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}}
/>
<!-- dropdown villa -->
@@ -419,14 +479,15 @@
fetchInventory(
searchTerm,
selectedVillaId,
"created_at",
"desc",
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}}
>
<option value="" disabled selected>Select Villa</option>
<option value="" selected>All Villa</option>
{#each villaItems as villa}
<option value={villa.id}>{villa.villa_name}</option>
{/each}
@@ -444,7 +505,15 @@
<button
class="bg-gray-200 text-gray-700 px-4 py-2 rounded-xl hover:bg-gray-300 text-sm transition"
on:click={() =>
fetchInventory(null, "created_at", "desc", 0, 10)}
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
)}
>
🔄 Reset
</button>
@@ -461,7 +530,7 @@
<thead class="bg-gray-100">
<tr>
{#each columns as col}
{#if col.key === "guest_name"}
{#if col.key === "item_name"}
<th
class="sticky left-0 px-4 py-3 text-left font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap"
style="background-color: #f0f8ff; z-index: 10;"
@@ -480,10 +549,10 @@
</thead>
<tbody class="bg-white divide-y divide-gray-200">
{#each inventoryItems as item}
<tr>
<tr class="hover:bg-gray-100 transition duration-150">
{#each columns as col}
<td class="px-4 py-3 whitespace-nowrap">
{#if +col.key >= 1 && +col.key <= 12}
{#if +col.key >= 1 && +col.key <= 12}
<td class="px-4 py-3 whitespace-nowrap">
<input
type="number"
min="0"
@@ -515,21 +584,33 @@
});
}}
/>
{:else if col.key === "created_at" || col.key === "updated_at"}
<td class="px-4 py-2">
{item[col.key] !== undefined
? new Date(
item[col.key] as
| string
| number
| Date,
).toLocaleString()
: "N/A"}
</td>
{:else if col.key === "actions"}
</td>
{:else if col.key === "item_name"}
<td
class="px-4 py-2 sticky left-0 whitespace-nowrap bg-gray-100 z-10"
>
<span
class="font-medium text-gray-800 hover:text-blue-600"
>
{item.item_name}
</span>
</td>
{:else if col.key === "created_at" || col.key === "updated_at"}
<td class="px-4 py-2">
{item[col.key] !== undefined
? new Date(
item[col.key] as
| string
| number
| Date,
).toLocaleString()
: "N/A"}
</td>
{:else if col.key === "actions"}
<td class="px-4 py-2">
<div class="flex space-x-2">
<button
class="text-blue-600 hover:underline"
class="inline-flex items-center gap-1 rounded bg-blue-600 px-3 py-1.5 text-white text-xs font-medium hover:bg-blue-700"
on:click={() => {
isEditing = true;
currentEditingId = item.id;
@@ -551,7 +632,7 @@
✏️ Edit
</button>
<button
class="text-red-600 hover:underline"
class="inline-flex items-center gap-1 rounded bg-red-600 px-3 py-1.5 text-white text-xs font-medium hover:bg-red-700"
on:click={async () => {
const { error } = await supabase
.from("vb_inventory")
@@ -566,17 +647,27 @@
alert(
"Item deleted successfully!",
);
await fetchInventory();
await fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
offset,
limit,
);
}
}}
>
🗑️ Delete
</button>
</div>
{:else}
</td>
{:else}
<td class="px-4 py-2">
{item[col.key as keyof Inventory]}
{/if}
</td>
</td>
{/if}
{/each}
</tr>
{:else}
@@ -609,9 +700,10 @@
fetchInventory(
searchTerm,
selectedVillaId,
selectedYear,
sortBy,
sortOrder,
0,
(currentPage - 1) * rowsPerPage,
rowsPerPage,
);
}}
@@ -652,9 +744,6 @@
</div>
</div>
</div>
<p class="text-xs text-gray-500 italic mb-2">
Tekan tombol Enter untuk menyimpan.
</p>
<!-- Modal -->
{#if showModal}