diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 29c72492..a5d69333 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -78,7 +78,7 @@ class InvoiceController extends Controller }); if ($total !== $booking->amount) { return response()->json([ - 'message' => 'Booking amount expected is '.$booking->amount.'. Your Invoice amount is $total.' + 'message' => 'Booking amount expected is '.$booking->amount.'. Your Invoice amount is '.$total ], 400); } @@ -141,7 +141,116 @@ class InvoiceController extends Controller public function edit(Request $request, $id) { + $user = $request->user(); + // validate $id + + $validatedData = $request->validate([ + 'address' => 'required', + 'buyer_company' => 'required', + 'contact_no' => 'required', + 'marking' => 'required', + 'lines.*.order' => 'required|numeric', + 'lines.*.stock_code' => 'required', + 'lines.*.description' => 'required', + 'lines.*.quantity' => 'required|numeric', + 'lines.*.unit_price_rm' => 'required|numeric', + 'lines.*.unit_price_rmb' => '', + 'lines.*.total' => 'required|numeric', + ]); + + // booking_id belongs to user + $booking = Booking::where('id', $id)->first(); + if (!$booking) { + return response()->json(['message' => 'Unable to find booking'], 403); + } + + $invoice = Invoice::where('booking_id', $id)->first(); + if (!$invoice) { + return response()->json(['message' => 'Invoice Not Found'], 404); + } + + // role === 'member' && $booking->user_id !== user()->id && $currentstatus !== 'request_change') + if ($request->user()->role === 'member') { + if ($booking->user_id !== $request->user()->id) { + return response()->json([ + 'message' => 'Booking does not belongs to you' + ], 403); + } + + $currentstatus = InvoiceStatuses::where('invoice_id', $invoice['id'])->latest('created_at')->first()->status; + if ($currentstatus !== 'request_change') { + return response()->json(['message' => 'Submitted PO can only be changed when status is request_change'], 403); + } + } + + // Amount match with total + $subtotalArray = array_map( + function ($line) { return $line['total']; }, + $validatedData['lines'] + ); + $total = array_reduce($subtotalArray, function ($v1, $v2) { + return $v1 + $v2; + }); + if ($total != $booking->amount) { + return response()->json([ + 'message' => 'Booking amount expected is '.$booking->amount.'. Your Invoice amount is '.$total + ], 400); + } + + // Generate PO Number - po20200810-001(PO Format) + $dt = Carbon::now(); + $po_number = 'po'.$dt->format('Ymmdd').'-'.$id; + + $invoice->amount = $total; + $invoice->booking_id = (int)$id; + $invoice->buyer_company = $validatedData['buyer_company']; + $invoice->address = $validatedData['address']; + $invoice->contact_no = $validatedData['contact_no']; + $invoice->po_number = $po_number; + $invoice->save(); + + if (!$invoice) { + return response() + ->json(['message' => 'Unable to Save Data'], 500); + } + + // Remove all invoice_details corresponding to invoice_id + $invoice_details = InvoiceDetails::where('invoice_id', $invoice->id); + $invoice_details->delete(); + + // Insert invoice_id into lines + foreach( $validatedData['lines'] as $key => $line) { + $invoice_details = new InvoiceDetails; + $invoice_details->order = $line['order']; + $invoice_details->description = $line['description']; + $invoice_details->quantity = $line['quantity']; + $invoice_details->stock_code = $line['stock_code']; + $invoice_details->invoice_id = $invoice->id; + $invoice_details->unit_price_rmb = $line['unit_price_rmb']; + $invoice_details->unit_price_rm = $line['unit_price_rm']; + $invoice_details->total = $line['total']; + + $invoice_details->save(); + } + unset($line); + + // Post status as pending + $status = new InvoiceStatuses; + $status->status = 'pending'; + if ($request->user()->role === 'admin') { + $status->comment = 'Edited by Admin.'; + } else { + $status->comment = 'Edited by Member.'; + } + $status->invoice_id = $invoice->id; + $status->save(); + + if($invoice_details) { + return $this->show($id); + } else { + return response(500); + } } public function postStatus(Request $request, $id) diff --git a/resources/assets/js/components/NewInvoice.vue b/resources/assets/js/components/NewInvoice.vue index 93caead6..4608c127 100644 --- a/resources/assets/js/components/NewInvoice.vue +++ b/resources/assets/js/components/NewInvoice.vue @@ -244,7 +244,7 @@ {{ line.total }} - @@ -624,10 +624,16 @@ export default { this.form.addLine.total = (+this.form.addLine.unit_price_rm * +this.form.addLine.quantity).toFixed(2) }, onRemoveLine: function (line) { - this.form.formData.lines.splice(line) + console.log('line', line) + this.form.formData.lines.splice(line, 1) this.setTotal() }, setTotal: function () { + if (this.form.formData.lines.length < 1) { + this.subtotal = 0 + this.total = 0 + return + } this.subtotal = parseFloat(this.form.formData.lines.map(line => line.total).reduce((a, b) => parseFloat(a) + parseFloat(b))).toFixed(2) this.total = parseFloat(+this.subtotal + (+this.subtotal * (+this.tax || 0) / 100)).toFixed(2) }, @@ -704,6 +710,7 @@ export default { this.form.formData.address = resp.data.address this.form.formData.lines = [] + this.statuses = [] resp.data.lines.forEach((el, i) => { this.form.formData.lines.push(el)