1
1
<script setup>
2
- // import {ref, onMounted} from "vue";
3
- // import axios from "axios";
4
- // import router from "@/Router/index.js";
5
- //
6
- // const form = ref([]);
7
- // const allCustomers = ref([]);
8
- // let listCart = ref([]);
9
- // const showModal = ref(false);
10
- // let listProducts = ref([]);
11
- //
12
- //
13
- // onMounted(async () => {
14
- // await indexForm();
15
- // await getAllCustomers();
16
- // await getProducts();
17
- // })
18
- //
19
- //
20
- // const indexForm = async () => {
21
- // const response = await axios.get('/api/create_invoice');
22
- // form.value = response.data;
23
- // }
24
- //
25
-
26
- //
27
- // const addCart = (item) => {
28
- // const itemCart = {
29
- // id: item.id,
30
- // item_code: item.item_code,
31
- // description: item.description,
32
- // unit_price: item.unit_price,
33
- // quantity: form.value.items[0].quantity
34
- // }
35
- //
36
- // let existingItemId;
37
- //
38
- // listCart.value.map(data => {
39
- // if(data.id === item.id){
40
- // existingItemId = item.id;
41
- // }
42
- // })
43
- //
44
- // if(existingItemId !== item.id){
45
- // listCart.value.push(itemCart);
46
- // toggleModal();
47
- // } else{
48
- // alert('product already added')
49
- // }
50
- //
51
- // }
52
- //
53
-
54
- //
55
- // const getProducts = async () => {
56
- // let response = await axios.get('/api/products');
57
- // listProducts.value = response.data.products;
58
- // }
59
- //
60
- // const removeItem = (id) => {
61
- // listCart.value = listCart.value.filter(item => item.id !== id);
62
- // }
63
- //
64
-
65
- //
66
- // const onSave = () => {
67
- // if(listCart.value.length >= 1){
68
- // let subtotal = 0;
69
- // subtotal = subTotal();
70
- // let total = 0;
71
- // total = grandTotal();
72
- //
73
- // const formData = new FormData();
74
- //
75
- // formData.append('invoice_item', JSON.stringify(listCart.value));
76
- // formData.append('customer_id', form.value.customer_id);
77
- // formData.append('date', form.value.date);
78
- // formData.append('due_date', form.value.due_date);
79
- // formData.append('number', form.value.number);
80
- // formData.append('reference', form.value.reference);
81
- // formData.append('discount', form.value.discount);
82
- // formData.append('subtotal', total);
83
- // formData.append('total', subtotal);
84
- // formData.append('terms_and_conditions', form.value.terms_and_conditions);
85
- //
86
- // axios.post('/api/add_invoice', formData);
87
- //
88
- // listCart.value = [];
89
- //
90
- // router.push('/');
91
- //
92
- // }
93
- // }
2
+
94
3
import {onMounted , ref } from " vue" ;
95
4
import axios from " axios" ;
5
+ import { useRouter } from " vue-router" ;
96
6
97
7
const props = defineProps ({
98
8
id: {
@@ -104,7 +14,6 @@ const props = defineProps({
104
14
const allCustomers = ref ([]);
105
15
106
16
const form = ref ([]);
107
- let listCart = ref ([]);
108
17
const showModal = ref (false );
109
18
let listProducts = ref ([]);
110
19
@@ -131,13 +40,15 @@ const toggleModal = () => {
131
40
showModal .value = ! showModal .value ;
132
41
}
133
42
43
+ const router = useRouter ();
44
+
134
45
const addCart = (item ) => {
135
46
const itemCart = {
136
- product_id : item .id ,
47
+ id : item .id ,
137
48
item_code: item .item_code ,
138
49
description: item .description ,
139
50
unit_price: item .unit_price ,
140
- quantity: form . value . items [ 0 ] .quantity
51
+ quantity: item .quantity
141
52
}
142
53
143
54
let existingItemId;
@@ -166,26 +77,55 @@ const deleteInvoiceItems = (id, i) => {
166
77
}
167
78
}
168
79
169
- const removeItem = (id ) => {
170
- listCart .value = listCart .value .filter (item => item .id !== id);
171
- }
172
-
173
80
const getProducts = async () => {
174
81
let response = await axios .get (' /api/products' );
175
82
listProducts .value = response .data .products ;
176
83
}
177
84
178
85
const subTotal = () => {
179
86
let total = 0 ;
180
- listCart .value .map ((data ) => {
181
- total = total + (data .quantity * data .unit_price );
182
- })
87
+ if (form .value .invoice_items ){
88
+ form .value .invoice_items .map ((data ) => {
89
+ total = total + (data .quantity * data .unit_price );
90
+ })
91
+ }
183
92
184
93
return total;
185
94
}
186
95
187
96
const grandTotal = () => {
188
- return subTotal () - form .value .discount ;
97
+ if (form .value .invoice_items ){
98
+ return subTotal () - form .value .discount ;
99
+ }
100
+
101
+ }
102
+
103
+ const onEdit = (id ) => {
104
+ if (form .value .invoice_items .length >= 1 ){
105
+ let subtotal = 0 ;
106
+ subtotal = subTotal ();
107
+ let total = 0 ;
108
+ total = grandTotal ();
109
+
110
+ const formData = new FormData ();
111
+
112
+ formData .append (' invoice_item' , JSON .stringify (form .value .invoice_items ));
113
+ formData .append (' customer_id' , form .value .customer_id );
114
+ formData .append (' date' , form .value .date );
115
+ formData .append (' due_date' , form .value .due_date );
116
+ formData .append (' number' , form .value .number );
117
+ formData .append (' reference' , form .value .reference );
118
+ formData .append (' discount' , form .value .discount );
119
+ formData .append (' subtotal' , total);
120
+ formData .append (' total' , subtotal);
121
+ formData .append (' terms_and_conditions' , form .value .terms_and_conditions );
122
+
123
+ axios .post (` /api/update_invoice/${ form .value .id } ` , formData);
124
+
125
+ form .value .invoice_items = [];
126
+
127
+ router .push (' /' );
128
+ }
189
129
}
190
130
191
131
</script >
@@ -223,7 +163,7 @@ const grandTotal = () => {
223
163
</div >
224
164
<div >
225
165
<p class =" my-1" >Numero</p >
226
- <input type =" text" class =" input" v-model =" form.data " >
166
+ <input type =" text" class =" input" v-model =" form.number " >
227
167
<p class =" my-1" >Reference(Optional)</p >
228
168
<input type =" text" class =" input" v-model =" form.reference" >
229
169
</div >
@@ -244,6 +184,9 @@ const grandTotal = () => {
244
184
<p v-if =" itemCart.product" >
245
185
#{{ itemCart.product.item_code }} {{ itemCart.product.description }}
246
186
</p >
187
+ <p v-else >
188
+ #{{ itemCart.item_code }} {{ itemCart.description }}
189
+ </p >
247
190
<p >
248
191
<input type =" text" class =" input" v-model =" itemCart.unit_price" >
249
192
</p >
@@ -266,7 +209,7 @@ const grandTotal = () => {
266
209
<div class =" table__footer" >
267
210
<div class =" document-footer" >
268
211
<p >Terms and Conditions</p >
269
- <textarea cols =" 50" rows =" 7" class =" textarea" ></textarea >
212
+ <textarea cols =" 50" rows =" 7" class =" textarea" v-model = " form.terms_and_conditions " ></textarea >
270
213
</div >
271
214
<div >
272
215
<div class =" table__footer--subtotal" >
@@ -291,7 +234,7 @@ const grandTotal = () => {
291
234
292
235
</div >
293
236
<div >
294
- <a class =" btn btn-secondary" >
237
+ <a class =" btn btn-secondary" @click = " onEdit(form.id) " >
295
238
Save
296
239
</a >
297
240
</div >
0 commit comments