PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / invoice / invoice.php
vikappointments / site / helpers / libraries / invoice Last commit date
classes 3 days ago constraints.php 3 days ago factory.php 3 days ago generator.php 3 days ago index.html 3 days ago invoice.php 3 days ago
invoice.php
341 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Abstract class used to implement the common functions
16 * that will be invoked to generate and send invoices.
17 *
18 * @since 1.6
19 */
20 abstract class VAPInvoice
21 {
22 /**
23 * The order details.
24 *
25 * @var object
26 */
27 protected $order;
28
29 /**
30 * The invoice arguments (e.g. increment number or legal info).
31 *
32 * @var object
33 */
34 protected $params;
35
36 /**
37 * The invoice properties (e.g. page margins or units).
38 *
39 * @var object
40 */
41 protected $constraints;
42
43 /**
44 * Class constructor.
45 *
46 * @param object The order details.
47 */
48 public function __construct($order)
49 {
50 $this->order = $order;
51 }
52
53 /**
54 * Overwrites the invoice parameters.
55 *
56 * @param object $data The parameters to set.
57 *
58 * @return self This object to support chaining.
59 *
60 * @since 1.7
61 */
62 public function setParams($data)
63 {
64 $this->params = $data->params;
65
66 $this->constraints = $data->constraints;
67
68 return $this;
69 }
70
71 /**
72 * Generates the invoices related to the specified order.
73 *
74 * @return mixed The invoice array data on success, otherwise false.
75 *
76 * @uses getInvoicePath()
77 * @uses getPageTemplate()
78 * @uses parseTemplate()
79 * @uses isFontSupported()
80 */
81 public function generate($increase = true)
82 {
83 if (!$this->order)
84 {
85 return false;
86 }
87
88 // prepare resulting data array
89 $data = array();
90
91 // get invoice path
92 $data['path'] = $this->getInvoicePath();
93
94 if (is_file($data['path']))
95 {
96 // unlink pdf if already exists
97 @unlink($data['path']);
98 }
99
100 if (!empty($this->constraints->font))
101 {
102 // use specified font
103 $font = $this->constraints->font;
104 }
105 else
106 {
107 // use DejavuSans font by default for UTF-8 compliance
108 $font = 'dejavusans';
109 }
110
111 // check if the selected font is supported
112 if (!$this->isFontSupported($font))
113 {
114 // fallback to Courier default font
115 $font = 'courier';
116 }
117
118 // load TCPDF only if missing, because it might have been already
119 // loaded by a different plugin
120 if (!class_exists('TCPDF'))
121 {
122 VAPLoader::import('pdf.tcpdf.tcpdf');
123 }
124
125 $pdf = new TCPDF($this->constraints->pageOrientation, $this->constraints->unit, $this->constraints->pageFormat, true, 'UTF-8', false);
126
127 // get title from constraints
128 $title = !empty($this->constraints->headerTitle) ? $this->constraints->headerTitle : null;
129
130 if ($title)
131 {
132 // set page title
133 $pdf->SetTitle($title);
134
135 // show header
136 $pdf->SetHeaderData('', 0, $title, '');
137
138 // set header font
139 $pdf->setHeaderFont(array($font, '', $this->constraints->fontSizes->header));
140
141 // set header margin
142 $pdf->SetHeaderMargin((int) $this->constraints->margins->header);
143 }
144 else
145 {
146 // nothing to display in header, hide it
147 $pdf->SetPrintHeader(false);
148 }
149
150 // default monospaced font
151 // $pdf->SetDefaultMonospacedFont('courier');
152
153 // margins
154 $pdf->SetMargins($this->constraints->margins->left, $this->constraints->margins->top, $this->constraints->margins->right);
155
156 $pdf->SetAutoPageBreak(true, $this->constraints->margins->bottom);
157 $pdf->setImageScale($this->constraints->imageScaleRatio);
158 $pdf->SetFont($font, '', $this->constraints->fontSizes->body);
159
160 // check if we should display the footer
161 if (!empty($this->constraints->showFooter))
162 {
163 // show footer
164 $pdf->SetPrintFooter(true);
165
166 // set footer font
167 $pdf->setFooterFont(array($font, '', $this->constraints->fontSizes->footer));
168
169 // set footer margin
170 $pdf->SetFooterMargin($this->constraints->margins->footer);
171 }
172 else
173 {
174 // hide footer otherwise
175 $pdf->SetPrintFooter(false);
176 }
177
178 // get invoice template
179 $tmpl = $this->getPageTemplate($this->order);
180
181 /**
182 * Parse the invoice template.
183 *
184 * @since 1.7 Pass the array data to let the invoice
185 * handler fill it with the resulting info.
186 */
187 $pages = $this->parseTemplate($tmpl, $data);
188
189 if (!is_array($pages))
190 {
191 $pages = array($pages);
192 }
193
194 // add pages
195 foreach ($pages as $page)
196 {
197 $pdf->addPage();
198 $pdf->writeHTML($page, true, false, true, false, '');
199 }
200
201 // write file
202 $pdf->Output($data['path'], 'F');
203
204 // check if the file has been created
205 if (!is_file($data['path']))
206 {
207 return false;
208 }
209
210 return $data;
211 }
212
213 /**
214 * Checks if the specified font is supported.
215 *
216 * @param string $font The font family name.
217 *
218 * @return boolean True if the font is supported, false otherwise.
219 *
220 * @since 1.7
221 */
222 public function isFontSupported($font)
223 {
224 $font = strtolower($font);
225
226 // font system supported by default
227 switch ($font)
228 {
229 case 'courier':
230 case 'helvetica':
231 return true;
232 }
233
234 // create font driver path under TCPDF "fonts" folder
235 $path = implode(DIRECTORY_SEPARATOR, array(VAPHELPERS, 'pdf', 'tcpdf', 'fonts', $font . '.php'));
236
237 // check if a the font is installed
238 return is_file($path);
239 }
240
241 /**
242 * Parses the given template to replace the placeholders
243 * with the values contained in the order details.
244 *
245 * @param string $tmpl The template to parse.
246 * @param array &$data An array data to fill.
247 *
248 * @return mixed The invoice page or an array of pages.
249 */
250 protected function parseTemplate($tmpl, &$data)
251 {
252 $config = VAPFactory::getConfig();
253
254 $logo_name = $config->get('companylogo');
255
256 // company logo
257 if ($logo_name)
258 {
259 $logo_str = '<img src="' . VAPMEDIA_URI . $logo_name . '" />';
260 }
261 else
262 {
263 $logo_str = '';
264 }
265
266 $tmpl = str_replace('{company_logo}', $logo_str, $tmpl);
267
268 // company info
269 $tmpl = str_replace('{company_info}', nl2br($this->params->legalinfo), $tmpl);
270
271 // invoice details
272 $suffix = '';
273
274 if (!empty($this->params->suffix))
275 {
276 $suffix = '/' . $this->params->suffix;
277 }
278
279 $tmpl = str_replace('{invoice_number}', $this->params->number, $tmpl);
280 $tmpl = str_replace('{invoice_suffix}', $suffix , $tmpl);
281
282 // register invoice number
283 $data['inv_number'] = $this->params->number . $suffix;
284
285 return $tmpl;
286 }
287
288 /**
289 * Returns the destination absolute path of the invoices folder.
290 * Inherit in children methods in case the path needs further
291 * subfolders.
292 *
293 * @return string The invoice folder path.
294 *
295 * @since 1.7
296 */
297 public function getInvoiceFolderPath()
298 {
299 // use default path
300 return VAPINVOICE;
301 }
302
303 /**
304 * Returns the destination URI of the invoices folder.
305 * Inherit in children methods in case the path needs further
306 * subfolders.
307 *
308 * @return string The invoice folder URI.
309 *
310 * @since 1.7
311 */
312 public function getInvoiceFolderURI()
313 {
314 // use default path
315 return VAPINVOICE_URI;
316 }
317
318 /**
319 * Returns the destination path of the invoice.
320 *
321 * @return string The invoice path.
322 */
323 abstract protected function getInvoicePath();
324
325 /**
326 * Returns the page template that will be used to
327 * generate the invoice.
328 *
329 * @return string The base HTML.
330 */
331 abstract protected function getPageTemplate();
332
333 /**
334 * Returns the e-mail address of the user that should
335 * receive the invoice via mail.
336 *
337 * @return string The customer e-mail.
338 */
339 abstract public function getRecipient();
340 }
341