diff --git a/pocketbase/pb_hooks/main.pb.js b/pocketbase/pb_hooks/main.pb.js
index 24a0cfa..07a993a 100644
--- a/pocketbase/pb_hooks/main.pb.js
+++ b/pocketbase/pb_hooks/main.pb.js
@@ -1,77 +1,62 @@
///
-onRecordAfterCreateRequest((e) => {
- if (e.collection.name !== "book_out_records") {
- return;
- }
-
- // It's generally safer to use onRecordAfterCreateSuccess to ensure the record is actually persisted
- // However, for immediate feedback or if you need to modify the response, onRecordAfterCreateRequest can be used.
- // For this example, we'll use onRecordAfterCreateSuccess to ensure data consistency.
- // The actual email sending logic will be in onRecordAfterCreateSuccess.
- // This hook is just to demonstrate where you might intercept the request.
-}, "book_out_records");
+// Existing hooks...
onRecordAfterCreateSuccess((e) => {
- if (e.collection.name !== "book_out_records") {
- return;
- }
+ if (e.collection.name !== "purchase_orders") {
+ return;
+ }
- const record = e.record;
+ const record = e.record;
- // Fetch related customer and item details
- $app.dao().findRecordById("customers", record.get("customer"))
- .then((customer) => {
- return $app.dao().findRecordById("stock_items", record.get("items"))
- .then((item) => {
- const customerName = customer.get("comapny_name");
- const itemName = item.get("name");
- const itemSku = item.get("sku");
- const quantity = record.get("quantity");
- const dateBookedOut = new Date(record.get("date_booked_out")).toLocaleString();
+ // Define the recipient email for approval (replace with actual approver email)
+ const approverEmail = "approver@example.com";
- // Define the recipient email (e.g., an admin email or the customer's email if available)
- // IMPORTANT: Replace 'your-admin-email@example.com' with your actual admin email or a dynamic customer email
- const recipientEmail = 'your-admin-email@example.com';
+ // Prepare order details for email
+ const supplierName = record.get("supplierName");
+ const orderDate = new Date(record.get("orderDate")).toLocaleString();
+ const status = record.get("status");
+ const items = record.get("items") || [];
+ const quantities = record.get("quantities") || [];
- const message = new MailerMessage({
- from: {
- address: $app.settings().meta.senderAddress,
- name: $app.settings().meta.senderName,
- },
- to: [{address: recipientEmail}],
- subject: `Stock Booked Out: ${itemName} (${itemSku})`,
- html: `
-
Dear Admin,
- The following item has been booked out:
-
- - Customer: ${customerName}
- - Item Name: ${itemName}
- - SKU: ${itemSku}
- - Quantity: ${quantity}
- - Date Booked Out: ${dateBookedOut}
-
- Thank you.
- `,
- text: `
- Dear Admin,
- The following item has been booked out:
- Customer: ${customerName}
- Item Name: ${itemName}
- SKU: ${itemSku}
- Quantity: ${quantity}
- Date Booked Out: ${dateBookedOut}
- Thank you.
- `,
- });
+ // Build HTML list of items and quantities
+ let itemsListHtml = "";
+ for (let i = 0; i < items.length; i++) {
+ itemsListHtml += `- Item ID: ${items[i]}, Quantity: ${quantities[i]}
`;
+ }
+ itemsListHtml += "
";
- return $app.newMailClient().send(message);
- });
- })
- .then(() => {
- console.log("Book-out notification email sent successfully!");
- })
- .catch((err) => {
- console.error("Failed to send book-out notification email:", err);
- });
-}, "book_out_records");
\ No newline at end of file
+ const message = new MailerMessage({
+ from: {
+ address: $app.settings().meta.senderAddress,
+ name: $app.settings().meta.senderName,
+ },
+ to: [{ address: approverEmail }],
+ subject: `Purchase Order Approval Request from ${supplierName}`,
+ html: `
+ Dear Approver,
+ A new purchase order has been created and requires your approval:
+ Supplier: ${supplierName}
+ Order Date: ${orderDate}
+ Status: ${status}
+ Items:
+ ${itemsListHtml}
+ Please review and approve or reject the order in the system.
+ Thank you.
+ `,
+ text: `
+ Dear Approver,
+ A new purchase order has been created and requires your approval:
+ Supplier: ${supplierName}
+ Order Date: ${orderDate}
+ Status: ${status}
+ Items:
+ ${itemsListHtml.replace(/<[^>]+>/g, '')}
+ Please review and approve or reject the order in the system.
+ Thank you.
+ `,
+ });
+
+ $app.newMailClient().send(message);
+ console.log("Purchase order approval email sent.");
+});
\ No newline at end of file
diff --git a/src/components/purchase-orders/PurchaseOrderForm.tsx b/src/components/purchase-orders/PurchaseOrderForm.tsx
index 3028cc5..18dc233 100644
--- a/src/components/purchase-orders/PurchaseOrderForm.tsx
+++ b/src/components/purchase-orders/PurchaseOrderForm.tsx
@@ -20,7 +20,7 @@ import { showSuccess, showError } from "@/utils/toast"
import pb from "@/lib/pocketbase"
import { StockItem } from "@/components/stock/columns"
-const statusOptions = ["Pending", "Ordered", "Received"] as const
+const statusOptions = ["Pending Approval", "Approved", "Rejected"] as const
const formSchema = z.object({
supplierName: z.string().min(2, { message: "Supplier name must be at least 2 characters." }),
@@ -67,7 +67,7 @@ export function PurchaseOrderForm({ setOpen, refreshData }: PurchaseOrderFormPro
resolver: zodResolver(formSchema),
defaultValues: {
supplierName: "",
- status: "Pending",
+ status: "Pending Approval",
items: [{ stockItemId: "", quantity: 1 }],
},
})