From 5699903d148c16fc5dd1dc550e42b97527c52049 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Mon, 21 May 2018 20:55:00 +0800 Subject: [PATCH] Add pdf generation for job orders #95 --- src/Controller/JobOrderController.php | 304 +++++++++++++++++++++++++- 1 file changed, 292 insertions(+), 12 deletions(-) diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index e25be13c..2bb55a8c 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -44,6 +44,8 @@ use Mosquitto\Client as MosquittoClient; use DateTime; use DateInterval; +use FPDF; + class JobOrderController extends BaseController { public function getJobOrders(Request $req) @@ -1717,15 +1719,12 @@ class JobOrderController extends BaseController } - public function pdfForm($id) + public function pdfForm(Request $req, $id) { $this->denyAccessUnlessGranted('jo_pdf.list', null, 'No access.'); $em = $this->getDoctrine()->getManager(); - $params = $this->initParameters('jo_all'); - $params['mode'] = 'update-all'; - // get row data $obj = $em->getRepository(JobOrder::class)->find($id); @@ -1733,16 +1732,297 @@ class JobOrderController extends BaseController if (empty($obj)) throw $this->createNotFoundException('The job order does not exist'); - $this->fillDropdownParameters($params); - $this->fillFormTags($params); + // set output filename + $filename = 'job-order-' . $obj->getID() . '.pdf'; - $params['obj'] = $obj; - $params['status_cancelled'] = JOStatus::CANCELLED; - $params['return_url'] = $this->generateUrl('jo_all'); - $params['submit_url'] = ''; + // generate the pdf + $pdf = new FPDF('P', 'mm', 'letter'); + $pdf->AddPage(); + $pdf->SetTitle('Motolite Res-Q Job Order #' . $obj->getID()); + $pdf->SetFillColor(211, 211, 211); - // response - return $this->render('job-order/form.pdf.html.twig', $params); + // style defaults + $margin = 10; + $page_width = $pdf->GetPageWidth() - ($margin * 2); + $table_col_width = $page_width / 12; + $line_height = 5; + $jo_line_height = 10; + $table_line_height = 7; + $font_face = 'Arial'; + $body_font_size = 9; + $header_font_size = 9; + $jo_font_size = 16; + $col1_x = $margin; + $col2_x = 120; + $label_width = 35; + $val_width = 60; + + // insert the logo + $image_path = $this->get('kernel')->getProjectDir() . '/public/assets/images/logo-resq.png'; + $pdf->Image($image_path, $col1_x, 10); + + // insert JO number + $pdf->SetFont($font_face, 'B', $jo_font_size); + $pdf->SetX($col2_x); + $pdf->Cell($label_width, $jo_line_height, 'JO Number:'); + $pdf->SetTextColor(9, 65, 150); + $pdf->Cell(0, $jo_line_height, $obj->getID()); + + // insert customer info + $customer = $obj->getCustomer(); + $pdf->SetFont($font_face, '', $body_font_size); + $pdf->SetTextColor(0, 0, 0); + + $pdf->Ln($line_height * 7); + + // get current Y + $y = $pdf->GetY(); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Customer Name:'); + $pdf->MultiCell($val_width, $line_height, $customer->getFirstName() . ' ' . $customer->getLastName(), 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Mobile Phone:'); + $pdf->MultiCell(0, $line_height, $customer->getPhoneMobile() ? '+63' . $customer->getPhoneMobile() : '', 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Delivery Date:'); + $pdf->MultiCell($val_width, $line_height, $obj->getDateSchedule() ? $obj->getDateSchedule()->format("m/d/Y") : '', 0, 'left'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Landline:'); + $pdf->MultiCell(0, $line_height, $customer->getPhoneLandline() ? '+63' . $customer->getPhoneLandline() : '', 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Office Phone:'); + $pdf->MultiCell(0, $line_height, $customer->getPhoneOffice() ? '+63' . $customer->getPhoneOffice() : '', 0, 'L'); + + $pdf->SetX($col2_x); + $pdf->Cell($label_width, $line_height, 'Fax:'); + $pdf->MultiCell($val_width, $line_height, $customer->getPhoneFax() ? '+63' . $customer->getPhoneFax() : '', 0, 'L'); + + // insert vehicle info + $cv = $obj->getCustomerVehicle(); + $vehicle = $cv->getVehicle(); + $pdf->Ln(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell($label_width, $line_height, 'Vehicle Details'); + $pdf->Ln($line_height * 2); + + // get current Y + $y = $pdf->GetY(); + + $pdf->SetFont($font_face, '', $body_font_size); + $pdf->Cell($label_width, $line_height, 'Plate Number:'); + $pdf->MultiCell($val_width, $line_height, $cv ? $cv->getPlateNumber() : '', 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Vehicle Color:'); + $pdf->MultiCell(0, $line_height, $cv ? $cv->getColor() : '', 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Brand:'); + $pdf->MultiCell($val_width, $line_height, $vehicle ? $vehicle->getManufacturer()->getName() : '', 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Model / Year:'); + $pdf->MultiCell(0, $line_height, $cv ? $cv->getModelYear() : '', 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Make:'); + $pdf->MultiCell($val_width, $line_height, $vehicle ? $vehicle->getMake() : '', 0, 'L'); + + // insert battery info + $battery = $cv->getCurrentBattery(); + $pdf->Ln(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell($label_width, $line_height, 'Battery Details'); + $pdf->Ln($line_height * 2); + + $pdf->SetFont($font_face, '', $body_font_size); + + // get current Y + $y = $pdf->GetY(); + + $pdf->Cell($label_width, $line_height, 'Current Battery:'); + $pdf->MultiCell($val_width, $line_height, $battery ? $battery->getManufacturer()->getName() . ' ' . $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' (' . $battery->getProductCode() . ')' : '', 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Serial Number:'); + $pdf->MultiCell(0, $line_height, $cv ? $cv->getWarrantyCode() : '', 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Wty. Exp. Date:'); + $pdf->MultiCell($val_width, $line_height, $cv && $cv->getWarrantyExpiration() ? $cv->getWarrantyExpiration()->format("d/m/Y") : '', 0, 'L'); + + // insert address info + $pdf->Ln(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell($label_width, $line_height, 'Transaction Details'); + $pdf->Ln($line_height * 2); + + $pdf->SetFont($font_face, '', $body_font_size); + + // get current Y + $y = $pdf->GetY(); + + $pdf->Cell($label_width, $line_height, 'Delivery Address:'); + $pdf->MultiCell($val_width, $line_height, $obj->getDeliveryAddress(), 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Landmark:'); + $pdf->MultiCell(0, $line_height, $obj->getLandMark(), 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + $pdf->SetXY($col1_x, $y); + $pdf->Cell($label_width, $line_height, 'Dispatch Time:'); + $pdf->MultiCell($val_width, $line_height, $obj->getDateSchedule()->format("g:i A"), 0, 'L'); + + // get Y after left cell + $y1 = $pdf->GetY(); + + $pdf->SetXY($col2_x, $y); + $pdf->Cell($label_width, $line_height, 'Dispatched By:'); + $pdf->MultiCell(0, $line_height, $obj->getAssignedBy()->getFullName(), 0, 'L'); + + // get Y after right cell + $y2 = $pdf->GetY(); + + // get row height + $y = max($y1, $y2); + + // insert delivery instructions + $pdf->SetY($y); + $pdf->Ln(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell(0, $line_height, 'Delivery Instructions'); + $pdf->Ln(); + + $pdf->SetFont($font_face, '', $body_font_size); + $pdf->MultiCell(0, $line_height, $obj->getDeliveryInstructions(), 1, 'L'); + + // insert invoice details + $pdf->Ln(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell($label_width, $line_height, 'Invoice Details'); + $pdf->Ln(); + + // invoice table headers + $invoice = $obj->getInvoice(); + $pdf->SetFont($font_face, 'B', $header_font_size); + $pdf->Cell($table_col_width * 6, $table_line_height, 'Item', 1, 0, 'L', 1); + $pdf->Cell($table_col_width * 2, $table_line_height, 'Quantity', 1, 0, 'R', 1); + $pdf->Cell($table_col_width * 2, $table_line_height, 'Unit Price', 1, 0, 'R', 1); + $pdf->Cell($table_col_width * 2, $table_line_height, 'Amount', 1, 1, 'R', 1); + $pdf->SetFont($font_face, '', $body_font_size); + + // build invoice items table + if ($invoice && !empty($invoice->getItems())) + { + foreach ($invoice->getItems() as $item) + { + $pdf->Cell($table_col_width * 6, $table_line_height, $item->getTitle(), 1); + $pdf->Cell($table_col_width * 2, $table_line_height, number_format($item->getQuantity()), 1, 0, 'R'); + $pdf->Cell($table_col_width * 2, $table_line_height, number_format($item->getPrice(), 2), 1, 0, 'R'); + $pdf->Cell($table_col_width * 2, $table_line_height, number_format($item->getPrice() * $item->getQuantity(), 2), 1, 1, 'R'); + } + } + else + { + $pdf->Cell($table_col_width * 12, 7, 'No items', 1, 1); + } + + $pdf->Ln($line_height * 2); + + // get current Y + $y = $pdf->GetY(); + + $pdf->Cell($label_width, $line_height, 'Transaction Type:'); + $pdf->MultiCell($val_width, $line_height, ServiceType::getName($obj->getServiceType()), 0, 'L'); + + // insert footer totals + $pdf->SetXY($col2_x, $y); + $pdf->SetFont($font_face, 'B'); + $pdf->Cell($label_width, $line_height, 'SUBTOTAL:'); + $pdf->SetFont($font_face, ''); + $pdf->MultiCell(0, $line_height, number_format($invoice->getVATExclusivePrice(), 2), 0, 'R'); + + $pdf->SetX($col2_x); + $pdf->SetFont($font_face, 'B'); + $pdf->Cell($label_width, $line_height, 'TAX:'); + $pdf->SetFont($font_face, ''); + $pdf->MultiCell(0, $line_height, number_format($invoice->getVAT(), 2), 0, 'R'); + + $pdf->SetX($col2_x); + $pdf->SetFont($font_face, 'B'); + $pdf->Cell($label_width, $line_height, 'DISCOUNT:'); + $pdf->SetFont($font_face, ''); + $pdf->MultiCell(0, $line_height, number_format($invoice->getDiscount(), 2), 0, 'R'); + + $pdf->SetX($col2_x); + $pdf->SetFont($font_face, 'B'); + $pdf->Cell($label_width, $line_height, 'FINAL AMOUNT:'); + $pdf->MultiCell(0, $line_height, number_format($invoice->getTotalPrice(), 2), 0, 'R'); + $pdf->SetFont($font_face, ''); + + // return response + return new Response($pdf->Output('I', $filename), 200, [ + 'Content-Type' => 'application/pdf' + ]); } public function cancelJobOrder(Request $req, $id)