aboutsummaryrefslogtreecommitdiff
path: root/src/components/InvoiceHeaderEditor.vue
blob: c14be3fc403f32a71ba7306b9b1458487b7d9041 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<script setup lang="ts">
import { ref, toRaw, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'
import Customer from "./../classes/customer"
import User from "./../classes/user"

const toast = useToast({
  position: 'top-right'
})

const route = useRouter()

const gettingData = ref(true) // for when getting all/one customer(s)
const submitting = ref(false) // shows spinner on continue button
const allCustomers = ref([])

const customer = ref(new Customer())
const user = ref(new User())
const customerSelection = ref(null)
const invoiceDate = ref(new Date().toISOString().substr(0, 10))

const getAllCustomers = async () => {
  allCustomers.value = []
  gettingData.value = true

  try {
    const r = await axios.get('/customer')
    if (r.status === 200) {
      allCustomers.value = r.data.data
    }
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  gettingData.value = false
}

const getUser = async () => {
  user.value = new User()
  gettingData.value = true

  try {
    const r = await axios.get('/user')
    if (r.status === 200) {
      user.value = r.data.data
    }
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  gettingData.value = false
}

const submit = async (e: Event) => {
  e.preventDefault()
  submitting.value = true

  try {
    const c = toRaw(customer.value)
    const u = toRaw(user.value)

    const res = await axios.post('/invoice', {
      "invoicedate": new Date(toRaw(invoiceDate.value)).toISOString(),
      "isdraft": true,
      "BillingAddress": {
        // NOTE: just c.BillingAddress doesn't work for some reason
        "AddressText": c.BillingAddress.AddressText,
        "City": c.BillingAddress.City,
        "State": c.BillingAddress.State,
        "PostalCode": c.BillingAddress.PostalCode,
        "Country": c.BillingAddress.Country,
      },
      "ShippingAddress": {
        // TODO: don't use the billing address!
        "AddressText": c.BillingAddress.AddressText,
        "City": c.BillingAddress.City,
        "State": c.BillingAddress.State,
        "PostalCode": c.BillingAddress.PostalCode,
        "Country": c.BillingAddress.Country,
      },
      "customername": c.Name,
      "customergstin": c.Gstin,
      "customercontactname": c.ContactName,
      "customerphone": c.Phone,
      "customeremail": c.Email,
      "customerwebsite": c.Website,
      "issuerfirmname": u.FirmName,
      "issuerfirmaddress": u.AddressText,
      "issuerfirmgstin": u.Gstin,
      "issuerfirmphone": u.Phone,
      "issuerfirmemail": u.Email,
      "issuerfirmwebsite": u.Website,
      "issuerfirmdetails": u.Details,
      "note": u.DefaultInvoiceNote,
    })

    route.push({ name: "edit-invoice", params: { id: res.data.data.ID }})
  } catch (err: any) {
    const statusCode: any = err.request.status
    const res: any = JSON.parse(err.request.response)

    switch (statusCode) {
      case 400:
        toast.error(res.error)
        break
      case 409:
        toast.error(res.error)
        break
      default:
        console.error(err)
        toast.error('An unhandled exception occoured. Please check logs')
    }
  }

  submitting.value = false
}

// when customer is selected,
// get the whole customer object from server
const refreshCustomer = async () => {
  gettingData.value = true

  const c: any = toRaw(customerSelection.value)

  try {
    const r = await axios.get(`/customer/${c.ID}`)
    customer.value = r.data.data
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  gettingData.value = false
}

onMounted(() => {
  getUser()
  getAllCustomers()
})
</script>

<template>
  <form class="row g-3" v-on:submit="submit">
    <div class="col-md-3"><!-- spacer --></div>

    <div class="col-md-3">
      <label for="invoice-customer-input" class="form-label">Customer</label>
      <select
        v-model="customerSelection"
        @change="refreshCustomer()"
        class="form-select"
        aria-label="Select Customer"
        id="invoice-customer-input"
      >
        <option v-if="allCustomers.length === 0" selected disabled value="null">No customers added in OpenBills!</option>
        <option v-else 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-2">
      <label for="invoice-date" class="form-label">Invoice Date</label>
      <input
        type="date"
        id="invoice-date"
        class="form-control"
        v-model="invoiceDate"/>
    </div>

    <div class="col-md-1 d-flex align-items-end justify-content-start">
      <button
        type="submit"
        class="btn btn-primary btn-block"
        :class="{ disabled: submitting || gettingData || customerSelection === null }"
      >
        Continue
        <div v-if="submitting" class="spinner-border spinner-border-sm ms-1" role="status">
          <span class="sr-only"></span>
        </div>
      </button>
    </div>

    <div class="col-md-2"><!-- spacer --></div>
    <p>TODO: add address info / shipping address selector</p>

  </form>
</template>