aboutsummaryrefslogtreecommitdiff
path: root/src/components/invoice_header.vue
blob: 2c62f15d6d7f2d729d3338fc24e36b034a3cff2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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>