From 7692f45332d1c8230b75111dfd5e9172d786969f Mon Sep 17 00:00:00 2001 From: Edmond Teh Date: Thu, 3 Sep 2020 13:41:19 +0800 Subject: [PATCH] create invoice controller --- app/Http/Controllers/InvoiceController.php | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index f20e6914..a092a692 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -3,6 +3,13 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Routing\ResponseFactory; +use Illuminate\Support\MessageBag; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Validator; +use Carbon\Carbon; +use Illuminate\Database\Eloquent\Model; +use App\Booking; class InvoiceController extends Controller { @@ -13,7 +20,93 @@ class InvoiceController extends Controller public function create(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); + } + + // user_id in booking does not match + if ($booking->user_id !== $user->id) { + return response()->json([ + 'message' => 'Booking does not belongs to you' + ], 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); + } + + // Change Status to submitted + + // Write to database if does not exist + $exist = DB::table('invoices')->where('booking_id', '=', $id)->first(); + if ($exist) { + return response() + ->json(['message' => 'Invoice exist. Cannot create duplicate invoice. Try edit.'], 400); + } + + // Generate PO Number - po20200810-001(PO Format) + $dt = Carbon::now(); + $po_number = 'po'.$dt->year.$dt->month.$dt->day.'-'.$id; + + // Insert Invoice into database + $invoice = DB::table('invoices')->insertGetId([ + 'amount' => $total, + 'booking_id' => $id, + 'buyer_company' => $validatedData['buyer_company'], + 'address' => $validatedData['address'], + 'contact_no' => $validatedData['contact_no'], + 'po_number' => $po_number + ]); + + if (!$invoice) { + return response() + ->json(['message' => 'Unable to Save Data'], 500) + } + + // Insert invoice_id into lines + foreach( $validatedData['lines'] as $key => $line) { + $validatedData['lines'][$key]['invoice_id'] = $invoice; + } + unset($line); + + $lines = DB::table('invoice_details')->insert($validatedData['lines']); + + if($lines) { + return response(201); + } else { + return response(500); + } } public function edit(Request $request, $id)