diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 30155f41..94356c43 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -14,6 +14,7 @@ use App\Booking; use App\Invoice; use App\InvoiceDetails; use App\InvoiceStatuses; +use PDF; class InvoiceController extends Controller { @@ -309,17 +310,87 @@ class InvoiceController extends Controller // 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' => '', + '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 response()->json(['message' => 'successful'], 200); + return $pdf->download('po.pdf'); + } - public function getDO($id) + public function getDO(Request $request, $id) { - // Ensure Invoice Exist and Completed + // 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' => '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' => $invoice->buyer_company, + 'address' => $invoice->address, + 'phone' => $invoice->contact_no, + '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'); } @@ -358,9 +429,9 @@ class InvoiceController extends Controller } private function invoiceStatus($booking_id) { - $invoice = Invoice::where('booking_id', $booking_id).first(); + $invoice = Invoice::where('booking_id', $booking_id)->first(); if (!$invoice) { - return + return; } $currentstatus = InvoiceStatuses::where('invoice_id', $invoice['id'])->latest('created_at')->first()->status; return $currentstatus; diff --git a/resources/assets/js/pages/booking/completed.vue b/resources/assets/js/pages/booking/completed.vue index edc9912d..b36c3c3d 100644 --- a/resources/assets/js/pages/booking/completed.vue +++ b/resources/assets/js/pages/booking/completed.vue @@ -223,13 +223,35 @@ export default { }, onDownloadPO () { const url = '/api/invoice/' + this.booking_details.id + '/po' - axios - .get(url) - .then(resp => console.log(resp)) + axios({ + url: url, + method: 'GET', + responseType: 'blob' // important + }).then(resp => { + const url = window.URL.createObjectURL(new Blob([resp.data])) + const link = document.createElement('a') + link.href = url + link.setAttribute('download', 'po.pdf') + document.body.appendChild(link) + link.click() + }) .catch(err => console.error(JSON.stringify(err))) }, onDownloadDO () { - alert('Downloading DO') + const url = '/api/invoice/' + this.booking_details.id + '/do' + axios({ + url: url, + method: 'GET', + responseType: 'blob' // important + }).then(resp => { + const url = window.URL.createObjectURL(new Blob([resp.data])) + const link = document.createElement('a') + link.href = url + link.setAttribute('download', 'po.pdf') + document.body.appendChild(link) + link.click() + }) + .catch(err => console.error(JSON.stringify(err))) } } } diff --git a/resources/views/myPDF.blade.php b/resources/views/myPDF.blade.php new file mode 100644 index 00000000..1897818c --- /dev/null +++ b/resources/views/myPDF.blade.php @@ -0,0 +1,15 @@ + + + + Hi + + +

Welcome to ItSolutionStuff.com - {{ $title }}

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, + quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo + consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse + cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non + proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ + \ No newline at end of file diff --git a/resources/views/pdf/po.blade.php b/resources/views/pdf/po.blade.php new file mode 100644 index 00000000..08f1b6e2 --- /dev/null +++ b/resources/views/pdf/po.blade.php @@ -0,0 +1,164 @@ + + + + + + + +
+

{{ $title }}

+
+
+
+ + {{ $buyer['buyer_company'] }} + + + X-123456 + + + {{ $buyer['address'] }} + + + + Phone: {{ $buyer['phone'] }} + +
+
+ + {{ $seller['seller_company'] }} + +
+ {{ $seller['address'] }} +
+
+ Phone: {{ $seller['phone'] }} +
+
+ Date: {{ $seller['date'] }} +
+
+ PO#: {{ $seller['po_number'] }} +
+
+ Order#: {{ $seller['order_number'] }} +
+
+
+
+
+
+ + + + + + + + + + + + + @foreach ($lines as $line) + + + + + + + + @endforeach + + + + + + + + + + + + + + + + + + +
+

Billing Detail

+
NoDescriptionQuantityUnit Price (RM)Total Amount
{{$line->order}}{{$line->description}}{{$line->quantity}}{{number_format($line->unit_price_rm, 2)}}{{number_format($line->total, 2)}}
Subtotal{{ number_format($amount, 2) }}
GST0
Total{{ number_format($amount, 2) }}
+ + + \ No newline at end of file