aboutsummaryrefslogtreecommitdiff
path: root/src/components/item_selector.vue
blob: 9cdbeedd1c3ded79f9cde0a51d6f65ec0553183d (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
<script setup lang="ts">
import { ref, toRaw, onMounted } from 'vue'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'
import InvoiceItem from "./../classes/invoice_item"

const props = defineProps(["invoice"])
const emit = defineEmits(["added"])

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

const gettingData = ref(true) // for when getting all item(s)
const submitting = ref(false) // shows spinner on add button
const allItems = ref([])

const item = ref(new InvoiceItem())
const itemSelection = ref(null)

const getAllItems = async () => {
  allItems.value = []
  gettingData.value = true

  try {
    const r = await axios.get('/item')
    if (r.status === 200) {
      allItems.value = r.data.data
    } else if (r.status === 204) {
      toast.warning('No items found')
    }
  } 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 {
    await axios.post(`/invoice/${props.invoice.ID}/item`, toRaw(item.value))
    itemSelection.value = null
    item.value = new InvoiceItem()
    emit("added")
  } 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 item is selected,
// set item to the newly selected item
const itemSelected = async () => {
  const is: any = toRaw(itemSelection.value)
  const i = new InvoiceItem()
  i.Name = is.Name
  i.Description = is.Description
  i.HSN = is.HSN
  i.UnitPrice = is.UnitPrice
  i.GSTPercentage = is.GSTPercentage
  i.UnitOfMeasure = is.UnitOfMeasure
  i.BrandName = is.Brand.Name
  i.Quantity = "1"

  item.value = i
}

onMounted(() => {
  getAllItems()
})
</script>

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

    <div class="col-md-3">
      <label for="item-brand-input" class="form-label">Item</label>
      <select
        v-model="itemSelection"
        @change="itemSelected()"
        class="form-select"
        aria-label="Select Item"
        id="item-input"
      >
        <option selected disabled value="null">Select Item</option>
        <option v-for="item in allItems" :value="item" :key="item['id']">
          {{ item["Name"] }} ({{ item["Brand"]["Name"] }})
        </option>
      </select>
    </div>

    <div class="col-md-3">
      <label for="inputDescription" class="form-label">Description</label>
      <input
        type="text"
        class="form-control"
        id="inputDescription"
        v-model="item.Description"
      />
    </div>

    <div class="col-md-1">
      <label for="inputHSN" class="form-label">HSN</label>
      <input
        type="text"
        class="form-control"
        id="inputHSN"
        v-model="item.HSN"
      />
    </div>

    <div class="col-md-1">
      <label for="inputQty" class="form-label">Quantity {{ item.UnitOfMeasure === "" ? "" : `(${item.UnitOfMeasure})` }}</label>
      <input
        type="text"
        class="form-control"
        id="inputQty"
        v-model="item.Quantity"
      />
    </div>

    <div class="col-md-1">
      <label for="inputUnitPrice" class="form-label">Unit Price</label>
      <input
        type="text"
        class="form-control"
        id="inputUnitPrice"
        v-model="item.UnitPrice"
      />
    </div>

    <div class="col-md-1">
      <label for="inputGST" class="form-label">GST (%)</label>
      <input
        type="text"
        class="form-control"
        id="inputGST"
        v-model="item.GSTPercentage"
      />
    </div>

    <div class="col-md-10"><!-- spacer --></div>

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