From 511e5f3badb871a4407a1cfef2d17a8c99660d30 Mon Sep 17 00:00:00 2001
From: Vidhu Kant Sharma <vidhukant@vidhukant.com>
Date: Sat, 2 Dec 2023 15:54:44 +0530
Subject: added new invoice page

---
 src/components/invoice_header.vue | 118 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)
 create mode 100644 src/components/invoice_header.vue

(limited to 'src/components/invoice_header.vue')

diff --git a/src/components/invoice_header.vue b/src/components/invoice_header.vue
new file mode 100644
index 0000000..2c62f15
--- /dev/null
+++ b/src/components/invoice_header.vue
@@ -0,0 +1,118 @@
+<script setup lang="ts">
+import { ref, toRaw, onMounted } from 'vue'
+import axios from 'axios'
+import { useToast } from 'vue-toast-notification'
+
+const toast = useToast({
+  position: 'top-right'
+})
+
+const isLoading = ref(true)
+const allCustomers = ref([])
+
+const invoiceCustomer = ref(null)
+const invoiceNumber = ref(0)
+const invoiceDate = ref(new Date().toISOString().substr(0, 10))
+
+const handleDateChange = e => {
+  invoiceDate.value = e
+}
+
+const getAllCustomers = async () => {
+  allCustomers.value = []
+  isLoading.value = true
+
+  try {
+    const r = await axios.get('/customer')
+    if (r.status === 200) {
+      allCustomers.value = r.data.data
+    } else if (r.status === 204) {
+      toast.warning('No customers found')
+    }
+  } catch (err) {
+    toast.error('An unhandled exception occoured. Please check logs')
+    console.error(err)
+  }
+
+  isLoading.value = false
+}
+
+const submit = async (e) => {
+  e.preventDefault()
+  isLoading.value = true
+
+  try {
+    const c = toRaw(invoiceCustomer.value)
+
+    await axios.post('/invoice', {
+      "invoicenumber": toRaw(invoiceNumber.value),
+      "invoicedate": new Date(toRaw(invoiceDate.value)).toISOString(),
+      "isdraft": true,
+      "billingaddress": c.BillingAddress,
+      "shippingaddress": c.BillingAddress, // TODO
+      "customername": c.Name,
+      "customergstin": c.Gstin,
+      "customercontactname": c.ContactName,
+      "customerphone": c.Phone,
+      "customeremail": c.Email,
+      "customerwebsite": c.Website,
+    })
+  } catch (err) {
+    const statusCode = err.request.status
+    const res = JSON.parse(err.request.response)
+
+    switch (statusCode) {
+      case (400, 409):
+        toast.error(res.error)
+        break
+      default:
+        console.error(err)
+        toast.error('An unhandled exception occoured. Please check logs')
+    }
+  }
+
+  isLoading.value = false
+}
+
+onMounted(() => {
+  getAllCustomers()
+})
+</script>
+
+<template>
+  <form class="row g-12" v-on:submit="submit">
+    <div class="col-md-4">
+      <label for="item-brand-input" class="form-label">Customer</label>
+      <select
+        v-model="invoiceCustomer"
+        class="form-select"
+        aria-label="Select Brand"
+        id="item-brand-input"
+      >
+        <option selected disabled value="null">Select Customer</option>
+        <option v-for="customer in allCustomers" :value="customer" :key="customer.id">
+          {{ customer.Name }}
+        </option>
+      </select>
+    </div>
+
+    <div class="col-md-4">
+      <label for="invoice-number" class="form-label">Invoice Number</label>
+      <input id="invoice-number" class="form-control" type="number" v-model="invoiceNumber" min="0"/>
+    </div>
+
+    <div class="col-md-4">
+      <label for="invoice-date" class="form-label">Invoice Date</label>
+      <input
+        type="date"
+        id="invoice-date"
+        class="form-control"
+        :value="new Date().toISOString().substr(0, 10)"
+        @input="handleDateChange($event.target.value)" />
+    </div>
+
+    <div class="col-12">
+      <input type="submit" value="Continue" class="btn btn-primary" />
+    </div>
+  </form>
+</template>
-- 
cgit v1.2.3

