create and show controller

This commit is contained in:
Edmond Teh
2020-09-03 16:04:45 +08:00
parent 7692f45332
commit 7aa52e784a
+39 -19
View File
@@ -10,11 +10,24 @@ use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Booking;
use App\Invoice;
use App\InvoiceDetails;
use App\InvoiceStatuses;
class InvoiceController extends Controller
{
public function show($id)
{
// validate $id is number
$invoice = Invoice::where('booking_id', $id)->first();
if (!$invoice) {
return response().json(['message' => 'Invoice Not Found'], 404);
}
$lines = InvoiceDetails::where('invoice_id', $invoice['id'])->orderBy('order', 'asc')->get();
$invoice->lines = $lines;
return response()->json($invoice, 200);
}
@@ -59,17 +72,14 @@ class InvoiceController extends Controller
$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();
$exist = Invoice::where('booking_id', $id)->first();
if ($exist) {
return response()
->json(['message' => 'Invoice exist. Cannot create duplicate invoice. Try edit.'], 400);
@@ -77,33 +87,43 @@ class InvoiceController extends Controller
// Generate PO Number - po20200810-001(PO Format)
$dt = Carbon::now();
$po_number = 'po'.$dt->year.$dt->month.$dt->day.'-'.$id;
$po_number = 'po'.$dt->format('Ymmdd').'-'.$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
]);
$invoice = new Invoice;
$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)
->json(['message' => 'Unable to Save Data'], 500);
}
// Insert invoice_id into lines
foreach( $validatedData['lines'] as $key => $line) {
$validatedData['lines'][$key]['invoice_id'] = $invoice;
$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);
$lines = DB::table('invoice_details')->insert($validatedData['lines']);
$invoice->lines = $invoice_details;
if($lines) {
return response(201);
if($invoice_details) {
return $this->show($id);
} else {
return response(500);
}