mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange.git
synced 2026-08-19 04:14:04 +00:00
157 lines
4.5 KiB
PHP
157 lines
4.5 KiB
PHP
<?php
|
|
|
|
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;
|
|
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);
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// Write to database if does not exist
|
|
$exist = Invoice::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->format('Ymmdd').'-'.$id;
|
|
|
|
$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);
|
|
}
|
|
|
|
// 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);
|
|
|
|
$invoice->lines = $invoice_details;
|
|
|
|
|
|
if($invoice_details) {
|
|
return $this->show($id);
|
|
} else {
|
|
return response(500);
|
|
}
|
|
}
|
|
|
|
public function edit(Request $request, $id)
|
|
{
|
|
|
|
}
|
|
|
|
public function updateStatus(Request $request, $id)
|
|
{
|
|
|
|
}
|
|
|
|
public function getPO($id)
|
|
{
|
|
|
|
}
|
|
|
|
public function getDO($id)
|
|
{
|
|
|
|
}
|
|
|
|
public function getSupplierDO($id)
|
|
{
|
|
|
|
}
|
|
}
|