|
|
@@ -1,39 +1,63 @@
|
|
1
|
1
|
import { reactive } from 'vue'
|
|
2
|
|
-import FormValidationError from '@/utils/FormValidationError'
|
|
|
2
|
+import { FormValidationError } from '@/utils/helpers'
|
|
3
|
3
|
|
|
4
|
4
|
export function useProductCart(form, initialProducts = []) {
|
|
5
|
5
|
const productCart = reactive(initialProducts)
|
|
6
|
6
|
|
|
7
|
7
|
const productCartDeleted = reactive([])
|
|
8
|
8
|
|
|
|
9
|
+ const productErrors = reactive([])
|
|
|
10
|
+
|
|
9
|
11
|
const productValidation = () => {
|
|
10
|
|
- const existProduct = productCart.find(
|
|
11
|
|
- (product) => product.number === form.product.number
|
|
12
|
|
- )
|
|
|
12
|
+ onClearProductErrors()
|
|
|
13
|
+
|
|
|
14
|
+ productCart.find((product) => {
|
|
|
15
|
+ if (product.number === form.product.number) {
|
|
|
16
|
+ // productErrors.push({
|
|
|
17
|
+ // message: 'Produk sudah ada dikeranjang',
|
|
|
18
|
+ // field: 'product',
|
|
|
19
|
+ // })
|
|
|
20
|
+
|
|
|
21
|
+ if (form.qty + product.qty > form.product.qty) {
|
|
|
22
|
+ productErrors.push({
|
|
|
23
|
+ message: 'Stok tidak mencukupi',
|
|
|
24
|
+ field: 'qty',
|
|
|
25
|
+ })
|
|
|
26
|
+ }
|
|
|
27
|
+ }
|
|
|
28
|
+ })
|
|
13
|
29
|
|
|
14
|
|
- if (existProduct) {
|
|
15
|
|
- throw new FormValidationError('Produk sudah ada dikeranjang', 'product')
|
|
|
30
|
+ if (productErrors.length) {
|
|
|
31
|
+ throw new FormValidationError('form error', productErrors)
|
|
16
|
32
|
}
|
|
17
|
33
|
}
|
|
18
|
34
|
|
|
19
|
35
|
const onAddProduct = () => {
|
|
20
|
36
|
try {
|
|
21
|
|
- form.clearErrors('product', 'price', 'qty')
|
|
|
37
|
+ form.clearErrors('product', 'qty')
|
|
22
|
38
|
|
|
23
|
39
|
productValidation()
|
|
24
|
40
|
|
|
25
|
|
- productCart.push({
|
|
26
|
|
- label: 'add',
|
|
27
|
|
- number: form.product.number,
|
|
28
|
|
- name: form.product.name,
|
|
29
|
|
- price: form.price,
|
|
30
|
|
- qty: form.qty,
|
|
31
|
|
- unit: form.product.unit,
|
|
|
41
|
+ productCart.find((product) => {
|
|
|
42
|
+ if (product.number === form.product.number) {
|
|
|
43
|
+ product.qty += form.qty
|
|
|
44
|
+ } else {
|
|
|
45
|
+ productCart.push({
|
|
|
46
|
+ label: 'add',
|
|
|
47
|
+ number: form.product.number,
|
|
|
48
|
+ name: form.product.name,
|
|
|
49
|
+ price: form.price,
|
|
|
50
|
+ qty: form.qty,
|
|
|
51
|
+ unit: form.product.unit,
|
|
|
52
|
+ })
|
|
|
53
|
+ }
|
|
32
|
54
|
})
|
|
33
|
55
|
|
|
34
|
|
- form.reset('product', 'price', 'qty')
|
|
|
56
|
+ form.reset('product', 'qty')
|
|
35
|
57
|
} catch (e) {
|
|
36
|
|
- form.setError(e.field, e.message)
|
|
|
58
|
+ e.errors.forEach((error) => {
|
|
|
59
|
+ form.setError(error.field, error.message)
|
|
|
60
|
+ })
|
|
37
|
61
|
}
|
|
38
|
62
|
}
|
|
39
|
63
|
|
|
|
@@ -56,6 +80,10 @@ export function useProductCart(form, initialProducts = []) {
|
|
56
|
80
|
productCartDeleted.splice(0)
|
|
57
|
81
|
}
|
|
58
|
82
|
|
|
|
83
|
+ const onClearProductErrors = () => {
|
|
|
84
|
+ productErrors.splice(0)
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
59
|
87
|
const totalProductPrice = () => {
|
|
60
|
88
|
const productPrices = productCart.map((product) => {
|
|
61
|
89
|
if (form.checkedPpn) {
|
|
|
@@ -65,7 +93,10 @@ export function useProductCart(form, initialProducts = []) {
|
|
65
|
93
|
}
|
|
66
|
94
|
})
|
|
67
|
95
|
|
|
68
|
|
- return productPrices.reduce((prev, current) => prev + current, 0)
|
|
|
96
|
+ return productPrices.reduce(
|
|
|
97
|
+ (prevPrice, currentPrice) => prevPrice + currentPrice,
|
|
|
98
|
+ 0
|
|
|
99
|
+ )
|
|
69
|
100
|
}
|
|
70
|
101
|
|
|
71
|
102
|
const onEditProduct = (event) => {
|
|
|
@@ -80,6 +111,7 @@ export function useProductCart(form, initialProducts = []) {
|
|
80
|
111
|
return {
|
|
81
|
112
|
productCart,
|
|
82
|
113
|
productCartDeleted,
|
|
|
114
|
+ productErrors,
|
|
83
|
115
|
onClearProductCart,
|
|
84
|
116
|
onClearProductCartDelete,
|
|
85
|
117
|
onAddProduct,
|