JezK
Edit File: userInvoicepdf.php
<?php // Enable error reporting ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Include autoloader require_once __DIR__ . '/../dompdf/autoload.inc.php'; include('userObject.php'); use Dompdf\Dompdf; use Dompdf\Options; use Dompdf\FontMetrics; // Set options to enable embedded PHP $options = new Options(); $options->set('isPhpEnabled', true); // Instantiate dompdf class $dompdf = new Dompdf($options); try { // Check if allotmentId is set if (!isset($_GET['allotmentId'])) { throw new Exception('allotmentId parameter is missing.'); } $allotmentId = $_GET['allotmentId']; // Fetch allotment details $items = $user->allotmentDetails($allotmentId); if (empty($items)) { throw new Exception('No allotment details found for the given ID.'); } $type = $items[0]['type']; $userId = $items[0]['user_id']; $date = $items[0]['created_date']; $convertedDate = date('d-m-Y', strtotime($date)); $approveStatus = $items[0]['approve_status']; $invoiceId = $items[0]['invoice_id']; $couponId = $items[0]['coupon_id']; $loyaltyPoints = $items[0]['loyalty_points']; $referralPoints = $items[0]['referral_points']; $userDeatils = $user->userDeatils($userId); $details = unserialize($items[0]['details']); // Initialize variables for calculations $subTotal = 0; $couponAmount = 0; $taxRate = 18; $productEntries = ''; $serviceEntries = ''; $packageEntry = ''; foreach ($items as $item) { $details = unserialize($item['details']); if ($item['type'] == 3 || $item['type'] == 2) { // Products $productIds = isset($details['selectedProductIds']) ? json_decode($details['selectedProductIds']) : []; $productNames = isset($details['productNames']) ? json_decode($details['productNames']) : []; $productQuantity = isset($details['productQuantity']) ? json_decode($details['productQuantity']) : []; $productCosts = isset($details['productCosts']) ? json_decode($details['productCosts']) : []; for ($j = 0; $j < count($productIds); $j++) { $productEntries .= '<tr> <td style="padding:5px 0; border:1px solid; text-align:center">' . $productNames[$j] . ' (' . $productQuantity[$j] . ')</td> <td style="padding:5px 0; border:1px solid; text-align:center"> Product</td> <td style="padding:5px 0; border:1px solid; text-align:center" colspan="2">' . $productCosts[$j] . '</td> </tr>'; $subTotal += $productCosts[$j]; } $servicesNames = isset($details['servicesNames']) ? json_decode($details['servicesNames']) : []; $servicesCosts = isset($details['servicesCosts']) ? json_decode($details['servicesCosts']) : []; for ($j = 0; $j < count($servicesNames); $j++) { $serviceEntries .= '<tr> <td style="padding:5px 0; border:1px solid; text-align:center">' . $servicesNames[$j] . '</td> <td style="padding:5px 0; border:1px solid; text-align:center"> Service</td> <td style="padding:5px 0; border:1px solid; text-align:center" colspan="2">' . $servicesCosts[$j] . '</td> </tr>'; $subTotal += $servicesCosts[$j]; } } elseif ($item['type'] == 4) { // Care Club $careclubDetails = unserialize($item['care_club_details']); $careClubName = $careclubDetails['careClubName']; $serviceIds = json_decode($careclubDetails['serviceIds']); $serviceNames = json_decode($careclubDetails['serviceNames']); $serviceCount = json_decode($careclubDetails['serviceCount']); $totalServices = $careclubDetails['totalServices']; $careClubCost = $careclubDetails['careClubCost']; $branchId = $item['branch_id']; $branchDetails = $user->branchDetails($branchId); $careClubName .= ($branchId > 0) ? " ({$branchDetails['name']})" : ""; $serviceEntries .= '<tr> <td style="padding:5px 0; border:1px solid; text-align:center">' . $careClubName . '</td> <td style="padding:5px 0; border:1px solid; text-align:center"> Care Club</td> <td style="padding:5px 0; border:1px solid; text-align:center" colspan="2">' . $careClubCost . '</td> </tr>'; $subTotal += $careClubCost; } } $grandTotal = 0; $subTotalTax = $subTotal; // If couponDetails is greater than 0, fetch the coupon amount from the table and deduct it from the total amount $grandTotal = $subTotal; if ($couponId > 0) { $couponDetails = $user->getCouponDetails($couponId); if($couponDetails['cpn_type'] == 1) { $couponAmount = $couponDetails['amount']; } else { $couponAmount = $grandTotal * ($couponDetails['percent'] / 100); } $grandTotal = $grandTotal - $couponAmount; } if($loyaltyPoints > 0) { $grandTotal = $grandTotal - $loyaltyPoints; } if($referralPoints > 0){ $grandTotal = $grandTotal - $referralPoints; } $tax = $grandTotal * $taxRate / 100; $grandTotal = $grandTotal + $tax; $couponAmountEntry = ""; if($couponAmount > 0) { $couponAmountEntry = '<tr> <td style=" border:1px solid; text-align:center" colspan="2">Coupon Amount:</td> <td style=" border:1px solid; text-align:center" colspan="2" >- ' . $couponAmount . ' Rs</td> </tr>'; } $loyaltyPointsEntry = ""; if($loyaltyPoints > 0) { $loyaltyPointsEntry = '<tr> <td style=" border:1px solid; text-align:center" colspan="2">Loyalty Points:</td> <td style=" border:1px solid; text-align:center" colspan="2" >- ' . $loyaltyPoints . ' Rs</td> </tr>'; } $referralPointsEntry = ""; if($referralPoints > 0) { $referralPointsEntry = '<tr> <td style=" border:1px solid; text-align:center" colspan="2">Referral Points:</td> <td style=" border:1px solid; text-align:center" colspan="2" >- ' . $referralPoints . ' Rs</td> </tr>'; } // Construct HTML for the PDF $html = '<body> <table style="width: 100%; max-width: 600px; margin: 0 auto; font-family: Arial, sans-serif;"> <tr> <td style="width: 100px;"> </td> <td style="text-align: right;"> <h1>Invoice</h1> </td> </tr> <tr> <td style="width: 150px;"> <p>Detailing Edition</p> <p>Behind Neurostar hospital</p> <p>Nagamalli Thota, Clock Tower junction</p> <p>LB Nagar, Kakinada 533003</p> </td> <td style="text-align: right;"> <p>Invoice ID: #' . $invoiceId . '</p> <p>Date: ' . $convertedDate . '</p> </td> </tr> <tr> <td style="width: 300px;"> <h3 style="padding:4px 2px; font-weight: 400; text-transform: uppercase; font-size: 14px; font-weight:bold;">Customer Details:</h3> <p>' . $userDeatils["user_name"] . '</p> <p>' . $userDeatils["address"] . '</p> <p>' . $userDeatils["phone"] . '</p> </td> </tr> <tr> <td colspan="2" style="text-align: center; font-weight: bold;"> <h2>Invoice Details</h2> </td> </tr> <tr> <td colspan="2"> <table style="width: 100%; border-collapse: collapse; border:1px solid;"> <tr style="border:1px solid; background-color: rgb(214, 214, 214);"> <th style="padding:5px 0; border:1px solid; width: 60%;">Item</th> <th style="padding:5px 0; border:1px solid">Category</th> <th style="padding:5px 0; border:1px solid" colspan="2">Amount</th> </tr>' . $productEntries . $serviceEntries . $packageEntry . '<tr> <td style=" border:1px solid; text-align:center" colspan="2">Subtotal:</td> <td style=" border:1px solid; text-align:center" colspan="2" >' . $subTotalTax . ' Rs</td> </tr> <tr> <td style=" border:1px solid; text-align:center" colspan="2">Tax Rate:</td> <td style=" border:1px solid; text-align:center" colspan="2" >' . $taxRate . '%</td> </tr> <tr> <td style=" border:1px solid; text-align:center" colspan="2">Tax:</td> <td style=" border:1px solid; text-align:center" colspan="2" >' . $tax . ' Rs</td> </tr>'. $couponAmountEntry . $loyaltyPointsEntry . $referralPointsEntry . '<tr> <td style=" border:1px solid; text-align:center" colspan="2">Total:</td> <td style=" border:1px solid; text-align:center" colspan="2" >' . $grandTotal . ' Rs</td> </tr> </table> </td> </tr> <tr> <td colspan="2" style="text-align: center; padding-top: 15px; font-size: 16px; font-weight: bold;"> <p>Thank you! </p> </td> </tr> </table> </body>'; // Load the HTML content $dompdf->loadHtml($html); // Set paper size and orientation $dompdf->setPaper('A4', 'portrait'); // Render the HTML as PDF $dompdf->render(); // Instantiate canvas instance $canvas = $dompdf->getCanvas(); // Instantiate font metrics class $fontMetrics = new FontMetrics($canvas, $options); // Get height and width of page $w = $canvas->get_width(); $h = $canvas->get_height(); // Get font family file $font = $fontMetrics->getFont('times'); // Specify watermark text $text = ""; // Get height and width of text $txtHeight = $fontMetrics->getFontHeight($font, 75); $textWidth = $fontMetrics->getTextWidth($text, $font, 75); // Set text opacity $canvas->set_opacity(.2); // Specify horizontal and vertical position $x = (($w - $textWidth) / 2); $y = (($h - $txtHeight) / 3); // Writes text at the specified x and y coordinates //$canvas->text($x, $y, $text, $font, 75); //$canvas->text($x, $y, $text, $font, 75, array(255, 0, 0)); $canvas->text($x, $y, $text, $font, 75, array(255, 0, 0), '', '', 20); // Specify watermark image $imageURL = 'http://detailingedition.com/admin/images/logo.png'; $imgWidth = 106; $imgHeight = 86; // Specify horizontal and vertical position // $x = (($w-$imgWidth)-10); // $y = (($h-$imgHeight)-20); // Add an image to the pdf $canvas->image($imageURL, 30, 20, $imgWidth, $imgHeight); // Output the generated PDF (force download) $dompdf->stream('invoice_' . $invoiceId . '.pdf', ['Attachment' => 1]); } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); }