| 1 |
<?php |
| 2 |
/** |
| 3 |
* PDF Helper Class |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Helpers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Includes\Helpers; |
| 10 |
|
| 11 |
use EasyInvoice\Models\Invoice; |
| 12 |
|
| 13 |
/** |
| 14 |
* PDF Helper Class |
| 15 |
* |
| 16 |
* Handles PDF generation for invoices with styling consistent with the web view |
| 17 |
*/ |
| 18 |
class PdfHelper { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get invoice data for PDF generation |
| 22 |
* @param \EasyInvoice\Includes\Models\Invoice $invoice |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public static function getInvoiceDataForPdf($invoice) { |
| 26 |
$company = [ |
| 27 |
'name' => get_bloginfo('name'), |
| 28 |
'address' => get_option('easy_invoice_company_address'), |
| 29 |
'email' => get_option('easy_invoice_company_email'), |
| 30 |
'phone' => get_option('easy_invoice_company_phone') |
| 31 |
]; |
| 32 |
|
| 33 |
$customer = [ |
| 34 |
'name' => $invoice->getCustomerName(), |
| 35 |
'address' => $invoice->getCustomerAddress(), |
| 36 |
'email' => $invoice->getCustomerEmail() |
| 37 |
]; |
| 38 |
|
| 39 |
$items = []; |
| 40 |
foreach ($invoice->getItems() as $item) { |
| 41 |
$items[] = [ |
| 42 |
'name' => $item->getName(), |
| 43 |
'description' => $item->getDescription(), |
| 44 |
'quantity' => $item->getQuantity(), |
| 45 |
'price' => $item->getPrice(), |
| 46 |
'adjust' => $item->getAdjust(), |
| 47 |
'total' => $item->getAmount() |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
$totals = [ |
| 52 |
'subtotal' => $invoice->getSubtotal(), |
| 53 |
'tax' => $invoice->getTaxAmount(), |
| 54 |
'discount' => $invoice->getDiscountAmount(), |
| 55 |
'total' => $invoice->getTotal() |
| 56 |
]; |
| 57 |
|
| 58 |
return [ |
| 59 |
'number' => $invoice->getNumber(), |
| 60 |
'date' => $invoice->getIssueDate(), |
| 61 |
'dueDate' => $invoice->getDueDate(), |
| 62 |
'status' => $invoice->getStatus(), |
| 63 |
'company' => $company, |
| 64 |
'customer' => $customer, |
| 65 |
'items' => $items, |
| 66 |
'totals' => $totals, |
| 67 |
'notes' => $invoice->getNotes(), |
| 68 |
'currency_code' => $invoice->getCurrencyCode() ?: 'USD', |
| 69 |
'currency_symbol' => \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($invoice->getCurrencyCode() ?: 'USD'), |
| 70 |
'symbol_position' => get_option('easy_invoice_currency_symbol_position', 'before'), |
| 71 |
'template' => $invoice->getTemplate() ?: 'standard' |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Enqueue required scripts for PDF generation |
| 77 |
*/ |
| 78 |
public static function enqueuePdfScripts() { |
| 79 |
// Enqueue jsPDF |
| 80 |
wp_enqueue_script( |
| 81 |
'jspdf', |
| 82 |
'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', |
| 83 |
[], |
| 84 |
'2.5.1', |
| 85 |
true |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|