mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange.git
synced 2026-08-19 04:14:04 +00:00
543 lines
18 KiB
PHP
543 lines
18 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
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;
|
|
use App\SupplierBooking;
|
|
use App\SettingSupplier;
|
|
use PDF;
|
|
|
|
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();
|
|
|
|
$status = InvoiceStatuses::where('invoice_id', $invoice['id'])->orderBy('created_at', 'asc')->get();
|
|
|
|
$invoice->lines = $lines;
|
|
$invoice->status = $status;
|
|
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',
|
|
'reg_no' => '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->reg_no = $validatedData['reg_no'];
|
|
$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;
|
|
|
|
// Post status as pending
|
|
$status = new InvoiceStatuses;
|
|
$status->status = 'pending';
|
|
$status->comment = '';
|
|
$status->invoice_id = $invoice->id;
|
|
$status->save();
|
|
|
|
if($invoice_details) {
|
|
return $this->show($id);
|
|
} else {
|
|
return response(500);
|
|
}
|
|
}
|
|
|
|
public function edit(Request $request, $id)
|
|
{
|
|
$user = $request->user();
|
|
|
|
// validate $id
|
|
|
|
$validatedData = $request->validate([
|
|
'address' => 'required',
|
|
'buyer_company' => 'required',
|
|
'contact_no' => 'required',
|
|
'marking' => 'required',
|
|
'reg_no' => '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->reg_no = $validatedData['reg_no'];
|
|
$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)
|
|
{
|
|
|
|
// validate $id is integer
|
|
|
|
$validatedData = $request->validate([
|
|
'status' => ['required', Rule::in('request_change', 'approve')],
|
|
'comment' => Rule::requiredIf($request->status === 'request_change')
|
|
]);
|
|
|
|
// Save status to database
|
|
|
|
$status = new InvoiceStatuses;
|
|
$status->status = $validatedData['status'];
|
|
$status->comment = $validatedData['comment'];
|
|
$status->invoice_id = Invoice::where('booking_id', $id)->first()['id'];
|
|
$status->save();
|
|
|
|
if(!$status) {
|
|
return response()->json(['message' => 'Unable to Update Status'], 500);
|
|
}
|
|
|
|
// Change Status to 7 if approve
|
|
// Add email event here
|
|
if ($validatedData['status'] === 'approve') {
|
|
$booking = Booking::find($id);
|
|
$booking->status = 7;
|
|
$booking->admin_status = 7;
|
|
$booking->save();
|
|
|
|
if(!$booking) {
|
|
return response()->json(['message' => 'Unable to Complete Booking. Please Approve Again'], 500);
|
|
}
|
|
}
|
|
|
|
// Response
|
|
return response()->json($status, 201);
|
|
|
|
|
|
}
|
|
|
|
public function getPO(Request $request, $id)
|
|
{
|
|
// Booking Exist and Belongs to User or is Admin
|
|
if ($request->user()->role === 'member') {
|
|
if (!$this->isUser($id, $request->user()->id)) {
|
|
return response()->json(['message' => 'The PO Does not belongs to you.'], 403);
|
|
}
|
|
}
|
|
|
|
// Ensure Invoice Status is Approve
|
|
// if ($this->invoiceStatus($id) !== 'approve') {
|
|
// return response()->json(['message' => 'Invoice must and exist and approve status.'], 403);
|
|
// }
|
|
|
|
// Generate PO
|
|
|
|
$booking = Booking::where('id', $id)->first();
|
|
$invoice = Invoice::where('booking_id', $booking['id'])->first();
|
|
$lines = InvoiceDetails::where('invoice_id', $invoice->id)->get();
|
|
|
|
$data = [
|
|
'title' => 'Purchase Order',
|
|
'buyer' => [
|
|
'buyer_company' => $invoice->buyer_company,
|
|
'reg_no' => $invoice->reg_no,
|
|
'address' => $invoice->address,
|
|
'gst' => '',
|
|
'phone' => $invoice->contact_no
|
|
],
|
|
'seller' => [
|
|
'seller_company' => 'CIEF Worldwide Sdn Bhd',
|
|
'address' => 'Malaysia Global Innovation & Creativity Centre, Level
|
|
1 CWS, Block 3730, Persiaran APEC 63000
|
|
Cyberjaya.',
|
|
'phone' => '018 2909252',
|
|
'date' => $invoice->updated_at,
|
|
'po_number' => $invoice->po_number,
|
|
'order_number' => $booking->order_no
|
|
],
|
|
'lines' => $lines,
|
|
'amount' => $invoice->amount
|
|
];
|
|
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'courier']);
|
|
$pdf = PDF::loadView('pdf/po', $data);
|
|
|
|
|
|
// Send
|
|
return $pdf->download('po.pdf');
|
|
|
|
}
|
|
|
|
public function showpo() {
|
|
|
|
$booking = Booking::where('id', 2018)->first();
|
|
$invoice = Invoice::where('booking_id', $booking['id'])->first();
|
|
$lines = InvoiceDetails::where('invoice_id', $invoice->id)->get();
|
|
|
|
$data = [
|
|
'title' => 'Delivery Order',
|
|
'buyer' => [
|
|
'buyer_company' => $invoice->buyer_company,
|
|
'reg_no' => $invoice->reg_no,
|
|
'address' => $invoice->address,
|
|
'gst' => '',
|
|
'phone' => $invoice->contact_no
|
|
],
|
|
'seller' => [
|
|
'seller_company' => 'CIEF Worldwide Sdn Bhd',
|
|
'address' => 'Malaysia Global Innovation & Creativity Centre,
|
|
Level 1 CWS, Block 3730,
|
|
Persiaran APEC 63000 Cyberjaya.',
|
|
'phone' => '018 2909252',
|
|
'date' => $invoice->updated_at,
|
|
'po_number' => $invoice->po_number,
|
|
'order_number' => $booking->order_no
|
|
],
|
|
'lines' => $lines,
|
|
'amount' => $invoice->amount
|
|
];
|
|
|
|
// Send
|
|
return view('pdf/po', $data);
|
|
|
|
}
|
|
|
|
public function showdo() {
|
|
|
|
$booking = Booking::where('id', 2018)->first();
|
|
$invoice = Invoice::where('booking_id', $booking['id'])->first();
|
|
$lines = InvoiceDetails::where('invoice_id', $invoice->id)->get();
|
|
|
|
$data = [
|
|
'title' => 'Delivery Order',
|
|
'buyer' => [
|
|
'buyer_company' => $invoice->buyer_company,
|
|
'reg_no' => $invoice->reg_no,
|
|
'address' => $invoice->address,
|
|
'gst' => '',
|
|
'phone' => $invoice->contact_no
|
|
],
|
|
'seller' => [
|
|
'seller_company' => 'CIEF Worldwide Sdn Bhd',
|
|
'address' => 'Malaysia Global Innovation & Creativity Centre, Level
|
|
1 CWS, Block 3730, Persiaran APEC 63000 Cyberjaya.',
|
|
'phone' => '018 2909252',
|
|
'date' => $invoice->updated_at,
|
|
'po_number' => $invoice->po_number,
|
|
'order_number' => $booking->order_no
|
|
],
|
|
'lines' => $lines,
|
|
'amount' => $invoice->amount
|
|
];
|
|
|
|
// Send
|
|
return view('pdf/do', $data);
|
|
|
|
}
|
|
|
|
public function getDO(Request $request, $id)
|
|
{
|
|
// Booking Exist and Belongs to User or is Admin
|
|
if ($request->user()->role === 'member') {
|
|
if (!$this->isUser($id, $request->user()->id)) {
|
|
return response()->json(['message' => 'The PO Does not belongs to you.'], 403);
|
|
}
|
|
}
|
|
|
|
// Ensure Invoice Status is Approve
|
|
// if ($this->invoiceStatus($id) !== 'approve') {
|
|
// return response()->json(['message' => 'Invoice must and exist and approve status.'], 403);
|
|
// }
|
|
|
|
// Generate PO
|
|
|
|
$booking = Booking::where('id', $id)->first();
|
|
$invoice = Invoice::where('booking_id', $booking['id'])->first();
|
|
$lines = InvoiceDetails::where('invoice_id', $invoice->id)->get();
|
|
|
|
$data = [
|
|
'title' => 'Delivery Order',
|
|
'buyer' => [
|
|
'buyer_company' => $invoice->buyer_company,
|
|
'reg_no' => $invoice->reg_no,
|
|
'address' => $invoice->address,
|
|
'gst' => '',
|
|
'phone' => $invoice->contact_no
|
|
],
|
|
'seller' => [
|
|
'seller_company' => 'CIEF Worldwide Sdn Bhd',
|
|
'address' => 'Malaysia Global Innovation & Creativity Centre, Level
|
|
1 CWS, Block 3730, Persiaran APEC 63000
|
|
Cyberjaya.',
|
|
'phone' => '018 2909252',
|
|
'date' => $invoice->updated_at,
|
|
'po_number' => $invoice->po_number,
|
|
'order_number' => $booking->order_no
|
|
],
|
|
'lines' => $lines,
|
|
'amount' => $invoice->amount
|
|
];
|
|
$pdf = PDF::loadView('pdf/po', $data);
|
|
|
|
|
|
// Send
|
|
return $pdf->download('do.pdf');
|
|
|
|
}
|
|
|
|
public function getSupplierDO($id)
|
|
{
|
|
// Generate PO
|
|
|
|
$booking = Booking::where('id', $id)->first();
|
|
$invoice = Invoice::where('booking_id', $booking['id'])->first();
|
|
$lines = InvoiceDetails::where('invoice_id', $invoice->id)->get();
|
|
$supplierbooking = SupplierBooking::where('booking_id', $id)->first();
|
|
$supplier = SettingSupplier::where('id', $supplierbooking->supplier_id)->first();
|
|
|
|
$data = [
|
|
'title' => 'Delivery Order',
|
|
'buyer' => [
|
|
'buyer_company' => 'CIEF Worldwide Sdn Bhd',
|
|
'reg_no' => '',
|
|
'address' => 'Malaysia Global Innovation & Creativity Centre, Level
|
|
1 CWS, Block 3730, Persiaran APEC 63000
|
|
Cyberjaya.',
|
|
'gst' => '',
|
|
'phone' => '018 2909252'
|
|
],
|
|
'seller' => [
|
|
'seller_company' => $supplier->company_name,
|
|
'address' => '',
|
|
'phone' => '',
|
|
'date' => '',
|
|
'po_number' => $invoice->po_number,
|
|
'order_number' => $booking->order_no
|
|
],
|
|
'lines' => $lines,
|
|
'amount' => $invoice->amount
|
|
];
|
|
$pdf = PDF::loadView('pdf/po', $data);
|
|
|
|
|
|
// Send
|
|
return $pdf->download('do.pdf');
|
|
}
|
|
|
|
// private invoiceExist($id)
|
|
// {
|
|
|
|
// }
|
|
|
|
// private invoiceStatus($id) {
|
|
|
|
// }
|
|
|
|
private function isUser($booking_id, $user_id) {
|
|
|
|
$booking = Booking::where([
|
|
['id', '=', $booking_id],
|
|
['user_id', '=', $user_id]
|
|
])->first();
|
|
|
|
if ($booking) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function invoiceStatus($booking_id) {
|
|
$invoice = Invoice::where('booking_id', $booking_id)->first();
|
|
if (!$invoice) {
|
|
return;
|
|
}
|
|
$currentstatus = InvoiceStatuses::where('invoice_id', $invoice['id'])->latest('created_at')->first()->status;
|
|
return $currentstatus;
|
|
}
|
|
}
|