-
Notifications
You must be signed in to change notification settings - Fork 3
/
invoice-pdf.php
231 lines (174 loc) · 8.52 KB
/
invoice-pdf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace WCInvoicePdf\Model;
class InvoicePdf
{
public function __construct()
{
// only load rospdf library when its necessary to avoid class conflicts
include_once WCINVOICEPDF_PLUGIN_DIR . 'vendor/rospdf/pdf-php/src/Cezpdf.php';
}
/**
* Used to build a pdf invoice using the WC_Order object
* @param {WC_Order} $order - the woocommerce order object
* @param {Array} $invoice-> list of extra data passed as array (E.g. invoice_number, created, due date, ...)
*/
public function BuildInvoice($invoice, $isOffer = false, $stream = false)
{
setlocale(LC_ALL, get_locale());
$order = $invoice->Order();
$formatStyle = \NumberFormatter::DECIMAL;
$formatter = new \NumberFormatter(get_locale(), $formatStyle);
$formatter->setPattern("#0.00 " . $order->get_currency());
$items = $order->get_items();
// if its first invoice, use shipping item as one-time fee
if ($invoice->isFirst) {
$items = array_merge($items, $order->get_items('shipping'));
}
//error_log(print_r($items, true));
$billing_info = str_replace('<br/>', "\n", $order->get_formatted_billing_address());
if ($isOffer) {
$headlineText = __('Offer', 'wc-invoice-pdf') . ' ' . $invoice->offer_number;
} else {
$headlineText = __('Invoice', 'wc-invoice-pdf') . ' ' . $invoice->invoice_number;
}
$pdf = new \Cezpdf('a4');
$pdf->ezSetMargins(50, 110, 50, 50);
$mediaId = intval(\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_logo']);
if ($mediaId > 0) {
$mediaUrl = wp_get_attachment_url($mediaId);
if ($mediaUrl !== false) {
$pdf->ezImage($mediaUrl, 0, 250, 'none', 'right');
}
}
$all = $pdf->openObject();
$pdf->saveState();
$pdf->setStrokeColor(0, 0, 0, 1);
$pdf->line(50, 100, 550, 100);
$pdf->addTextWrap(50, 90, 8, \WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_block1']);
$pdf->addTextWrap(250, 90, 8, \WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_block2'], 0);
$pdf->addTextWrap(550, 90, 8, \WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_block3'], 0, 'right');
$pdf->restoreState();
$pdf->closeObject();
$pdf->addObject($all, 'all');
$pdf->ezSetDy(-60);
$y = $pdf->y;
$pdf->ezText(sprintf(\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_info'], strftime('%x', strtotime($invoice->created))), 0, ['justification' => 'right']);
if ($order->get_date_paid() && !$isOffer) {
$pdf->saveState();
$pdf->setColor(1, 0, 0);
$pdf->ezText(sprintf(__('Paid at', 'wc-invoice-pdf') . ' %s', strftime('%x', strtotime($order->get_date_paid()))), 0, ['justification' => 'right']);
$pdf->restoreState();
} else {
$pdf->ezText('');
}
$pdf->y = $y;
$pdf->ezText("<strong>" . \WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_addressline'] . "</strong>\n", 8);
$pdf->line($pdf->ez['leftMargin'], $pdf->y + 5, 200, $pdf->y + 5);
$pdf->ezText($billing_info, 10);
$pdf->ezSetDy(-60);
$pdf->ezText("$headlineText", 14);
$cols = array('num' => __('No#', 'wc-invoice-pdf'), 'desc' => __('Description', 'wc-invoice-pdf'), 'qty' => __('Qty', 'wc-invoice-pdf'), 'price' => __('Unit Price', 'wc-invoice-pdf'), 'total' => __('Amount', 'wc-invoice-pdf'));
$colOptions = [
'num' => ['width' => 30],
'desc' => [],
'qty' => ['justification' => 'right', 'width' => 60],
'price' => ['justification' => 'right', 'width' => 70],
'total' => ['justification' => 'right', 'width' => 80],
];
$data = [];
$i = 1;
$summary = 0;
$fees = $order->get_fees();
$items = array_merge($items, $fees);
foreach ($items as $v) {
$row = [];
$product_name = $v['name'];
//error_log(print_r($v,true));
$product = null;
// check if product id is available and fetch the ISPCONFIG tempalte ID
if (!empty($v['product_id'])) {
$product = wc_get_product($v['product_id']);
}
if (!isset($v['qty'])) {
$v['qty'] = 1;
}
if (\is_subclass_of($product, 'WC_ISPConfigProduct')) {
// if its an ISPCONFIG Template product
$current = new \DateTime($invoice->created);
$next = clone $current;
if ($v['qty'] == 1) {
$next->add(new \DateInterval('P1M'));
} elseif ($v['qty'] == 12) {
// overwrite the QTY to be 1 MONTH
$next->add(new \DateInterval('P12M'));
}
$qtyStr = number_format($v['qty'], 0, ',', ' ') . ' ' . $product->get_price_suffix('', $v['qty']);
$product_name .= "\n<strong>" . __('Period', 'wc-invoice-pdf') . ": " . \strftime('%x', $current->getTimestamp()) ." - ". \strftime('%x', $next->getTimestamp()) . '</strong>';
} elseif ($product instanceof \WC_Product_Hour) {
// check if product type is "hour" to output hours instead of Qty
$qtyStr = number_format($v['qty'], 1, ',', ' ');
$qtyStr.= '' . $product->get_price_suffix('', $v['qty'], true);
} else {
$qtyStr = number_format($v['qty'], 2, ',', ' ');
}
$total = round($v['total'], 2);
$tax = round($v['total_tax'], 2);
if (\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_b2c']) {
$total += $tax;
}
$unitprice = $total / intval($v['qty']);
$mdcontent = '';
if ($v instanceof \WC_Order_Item_Product) {
$meta = $v->get_meta_data();
if (!empty($meta)) {
$mdcontent.= implode('', array_map(function ($m) {
return "\n<strong>".$m->key.":</strong> ".$m->value;
}, $meta));
}
}
$row['num'] = "$i";
$row['desc'] = $product_name . "\n" . $mdcontent;
$row['qty'] = $qtyStr;
$row['price'] = $formatter->format($unitprice);
$row['total'] = $formatter->format($total);
$summary += $total;
$data[] = $row;
$i++;
}
$pdf->ezSetDy(-30);
$pdf->ezTable($data, $cols, '', ['width' => '500','splitRows' => !boolval(\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_keeprows']),'gridlines' => EZ_GRIDLINE_HEADERONLY, 'cols' => $colOptions]);
$colOptions = [
['justification' => 'right'],
['justification' => 'right']
];
$summaryData = [
[
"<strong>".__('Summary', 'wc-invoice-pdf')."</strong>",
"<strong>".$formatter->format($summary)."</strong>"
]
];
$summaryTax = 0;
foreach ($order->get_tax_totals() as $tax) {
$summaryTax += $tax->amount;
$tax_rate = \WC_Tax::get_rate_percent($tax->rate_id);
if (\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_b2c']) {
$taxStr = __(sprintf("includes %s %s", $tax_rate, $tax->label), 'wc-invice-pdf');
} else {
$taxStr = __(sprintf("plus %s %s", $tax_rate, $tax->label), 'wc-invice-pdf');
}
$summaryData[] = ['<strong>' . $taxStr . '</strong>', '<strong>' . $formatter->format($tax->amount) . '</strong>'];
}
if (empty(\WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_b2c'])) {
$summaryData[] = ["<strong>".__('Total', 'wc-invoice-pdf')."</strong>", "<strong>".$formatter->format($summary + $summaryTax)."</strong>"];
}
$pdf->ezSetDy(-20);
$pdf->ezTable($summaryData, null, '', ['width' => 200, 'gridlines' => 0, 'showHeadings' => 0,'shaded' => 0 ,'xPos' => 'right', 'xOrientation' => 'left', 'cols' => $colOptions ]);
$pdf->ezSetDy(-20);
$pdf->ezText("<strong>" . \WCInvoicePdf\WCInvoicePdf::$OPTIONS['wc_pdf_condition'] . "</strong>", 8, ['justification' => 'center']);
if ($stream) {
$pdf->ezStream(['Content-Disposition' => ($isOffer ? $invoice->offer_number : $invoice->invoice_number) . '.pdf' ]);
} else {
return $pdf->ezOutput();
}
}
}