Skip to content

Commit 7347a17

Browse files
author
muendokennedy
committed
Finished making the update logic invoice items logic in the database
1 parent 028fbb2 commit 7347a17

File tree

5 files changed

+89
-114
lines changed

5 files changed

+89
-114
lines changed

app/Http/Controllers/InvoiceController.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function createInvoice()
7373

7474
public function addInvoice(Request $request)
7575
{
76-
$invoiceItem = $request->input('invoiceItem');
76+
$invoiceItem = $request->input('invoice_item');
7777

7878
$invoiceData['sub_total'] = $request->input('subtotal');
7979
$invoiceData['total'] = $request->input('total');
@@ -116,11 +116,45 @@ public function editInvoice (String $id)
116116
], 200);
117117
}
118118

119-
public function deleteInvoice($id)
119+
public function deleteInvoice(String $id)
120120
{
121121
$invoiceItem = InvoiceItem::findOrFail($id);
122122

123123
$invoiceItem->delete();
124124

125125
}
126+
127+
public function updateInvoice(String $id, Request $request)
128+
{
129+
$invoice = Invoice::where('id', $id)->first();
130+
131+
$invoice->sub_total = $request->subtotal;
132+
$invoice->total = $request->total;
133+
$invoice->customer_id = $request->customer_id;
134+
$invoice->number = $request->number;
135+
$invoice->date = $request->date;
136+
$invoice->due_date = $request->due_date;
137+
$invoice->discount = $request->discount;
138+
$invoice->reference = $request->reference;
139+
$invoice->terms_and_conditions = $request->terms_and_conditions;
140+
141+
// $invoice->update($request->all());
142+
143+
// I will use this instead because it makes much more sense to me
144+
145+
$invoice->save();
146+
147+
$invoiceItem = $request->input('invoice_item');
148+
149+
$invoice->invoice_items()->delete();
150+
151+
foreach (json_decode($invoiceItem) as $item){
152+
$itemData['product_id'] = $item->id;
153+
$itemData['invoice_id'] = $invoice->id;
154+
$itemData['quantity'] = $item->quantity;
155+
$itemData['unit_price'] = $item->unit_price;
156+
157+
InvoiceItem::create($itemData);
158+
}
159+
}
126160
}

resources/js/components/invoices/edit.vue

Lines changed: 49 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,8 @@
11
<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+
943
import {onMounted, ref} from "vue";
954
import axios from "axios";
5+
import { useRouter } from "vue-router";
966
977
const props = defineProps({
988
id: {
@@ -104,7 +14,6 @@ const props = defineProps({
10414
const allCustomers = ref([]);
10515
10616
const form = ref([]);
107-
let listCart = ref([]);
10817
const showModal = ref(false);
10918
let listProducts = ref([]);
11019
@@ -131,13 +40,15 @@ const toggleModal = () => {
13140
showModal.value = !showModal.value;
13241
}
13342
43+
const router = useRouter();
44+
13445
const addCart = (item) => {
13546
const itemCart = {
136-
product_id: item.id,
47+
id: item.id,
13748
item_code: item.item_code,
13849
description: item.description,
13950
unit_price: item.unit_price,
140-
quantity: form.value.items[0].quantity
51+
quantity: item.quantity
14152
}
14253
14354
let existingItemId;
@@ -166,26 +77,55 @@ const deleteInvoiceItems = (id, i) => {
16677
}
16778
}
16879
169-
const removeItem = (id) => {
170-
listCart.value = listCart.value.filter(item => item.id !== id);
171-
}
172-
17380
const getProducts = async () => {
17481
let response = await axios.get('/api/products');
17582
listProducts.value = response.data.products;
17683
}
17784
17885
const subTotal = () => {
17986
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+
}
18392
18493
return total;
18594
}
18695
18796
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+
}
189129
}
190130
191131
</script>
@@ -223,7 +163,7 @@ const grandTotal = () => {
223163
</div>
224164
<div>
225165
<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">
227167
<p class="my-1">Reference(Optional)</p>
228168
<input type="text" class="input" v-model="form.reference">
229169
</div>
@@ -244,6 +184,9 @@ const grandTotal = () => {
244184
<p v-if="itemCart.product">
245185
#{{ itemCart.product.item_code }} {{ itemCart.product.description }}
246186
</p>
187+
<p v-else>
188+
#{{ itemCart.item_code }} {{ itemCart.description }}
189+
</p>
247190
<p>
248191
<input type="text" class="input" v-model="itemCart.unit_price">
249192
</p>
@@ -266,7 +209,7 @@ const grandTotal = () => {
266209
<div class="table__footer">
267210
<div class="document-footer" >
268211
<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>
270213
</div>
271214
<div>
272215
<div class="table__footer--subtotal">
@@ -291,7 +234,7 @@ const grandTotal = () => {
291234

292235
</div>
293236
<div>
294-
<a class="btn btn-secondary">
237+
<a class="btn btn-secondary" @click="onEdit(form.id)">
295238
Save
296239
</a>
297240
</div>

resources/js/components/invoices/index.vue

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ const onShow = (id) => {
108108
</div>
109109

110110
<br><br><br>
111-
112-
113-
114-
115-
<!-- <br><br><br>-->
116111
</div>
117112
</template>
118113

resources/js/components/invoices/newInvoice.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import {ref, onMounted} from "vue";
33
import axios from "axios";
4-
import router from "@/Router/index.js";
4+
import { useRouter } from "vue-router";
55
66
const form = ref([]);
77
const allCustomers = ref([]);
@@ -22,6 +22,8 @@ const indexForm = async () => {
2222
form.value = response.data;
2323
}
2424
25+
const router = useRouter();
26+
2527
const getAllCustomers = async () => {
2628
const response = await axios.get('/api/customers');
2729

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
Route::get('/show_invoice/{id}', [InvoiceController::class, 'showInvoice']);
2020
Route::get('/edit_invoice/{id}', [InvoiceController::class, 'editInvoice']);
2121
Route::get('/delete_invoice_item/{id}', [InvoiceController::class, 'deleteInvoice']);
22+
Route::post('/update_invoice/{id}', [InvoiceController::class, 'updateInvoice']);

0 commit comments

Comments
 (0)