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 / generator.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
generator.php
356 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 VAPLoader::import('libraries.invoice.invoice');
15 VAPLoader::import('libraries.invoice.constraints');
16
17 /**
18 * Invoices generator class.
19 *
20 * @since 1.7
21 */
22 class VAPInvoiceGenerator
23 {
24 /**
25 * The invoice instance.
26 *
27 * @var VAPInvoice
28 */
29 protected $invoice;
30
31 /**
32 * The invoice parameters and settings.
33 *
34 * @var object
35 */
36 protected $data;
37
38 /**
39 * Class constructor.
40 *
41 * @param VAPInvoice $invoice The invoice instance to generate. If not
42 * specified, it will be possible to set it
43 * in a second time.
44 * @param object $data An object containing the invoice arguments.
45 * Leave empty to autoload them.
46 */
47 public function __construct(?VAPInvoice $invoice = null, $data = null)
48 {
49 if ($data)
50 {
51 $this->data = $data;
52 }
53 else
54 {
55 // loads internal data
56 $this->load();
57 }
58
59 if ($invoice)
60 {
61 $this->setInvoice($invoice);
62 }
63 }
64
65 /**
66 * Loads the invoice settings.
67 *
68 * @return self This object to support chaining.
69 */
70 protected function load()
71 {
72 $data = VAPFactory::getConfig()->getObject('invoiceobj', null);
73
74 if (!isset($data->params) || !is_object($data->params) || !get_object_vars($data->params))
75 {
76 // get system timezone
77 $tz = JFactory::getApplication()->get('offset', 'UTC');
78
79 $this->data = new stdClass;
80
81 // create parameters for the first time
82 $this->data->params = new stdClass;
83 $this->data->params->number = 1;
84 $this->data->params->suffix = (int) JHtml::fetch('date', 'now', 'Y', $tz);
85 $this->data->params->datetype = 1; // 1: today, 2: booking date, 3: check-in date
86 $this->data->params->date = null;
87 $this->data->params->legalinfo = '';
88 $this->data->params->sendinvoice = 0;
89
90 $this->data->constraints = null;
91 }
92 else
93 {
94 $this->data = $data;
95
96 // always unset last stored date
97 $this->data->params->date = null;
98 }
99
100 // check if the constraints was set in the stored JSON
101 if (!isset($this->data->constraints) || !is_object($this->data->constraints))
102 {
103 // no constraints, use empty array to load default settings
104 $this->data->constraints = array();
105 }
106
107 // create new constraints instance with stored data
108 $this->data->constraints = new VAPInvoiceConstraints($this->data->constraints);
109
110 return $this;
111 }
112
113 /**
114 * Sets the invoice into the internal state.
115 *
116 * @param VAPInvoice $invoice The invoice instance.
117 *
118 * @return self This object to support chaining.
119 */
120 public function setInvoice(VAPInvoice $invoice)
121 {
122 // set invoice and force it to use our internal parameters
123 $this->invoice = $invoice;
124 $this->invoice->setParams($this->data);
125
126 return $this;
127 }
128
129 /**
130 * Returns an array containing the invoice arguments.
131 *
132 * @return object The invoice arguments.
133 */
134 public function getParams()
135 {
136 return $this->data->params;
137 }
138
139 /**
140 * Overwrites the invoice parameters.
141 *
142 * @param object $params The parameters to set.
143 *
144 * @return self This object to support chaining.
145 */
146 public function setParams($params)
147 {
148 $params = (object) $params;
149
150 foreach ($params as $k => $v)
151 {
152 if (property_exists($this->data->params, $k))
153 {
154 $this->data->params->{$k} = $v;
155 }
156 }
157
158 if (!empty($params->inv_number))
159 {
160 list($this->data->params->number, $this->data->params->suffix) = explode('/', $params->inv_number);
161 }
162
163 return $this;
164 }
165
166 /**
167 * Returns an object containing the invoice properties.
168 *
169 * @return object The invoice properties.
170 */
171 public function getConstraints()
172 {
173 return $this->data->constraints;
174 }
175
176 /**
177 * Overwrites the invoice constraints.
178 *
179 * @param object $settings The constraints to set.
180 *
181 * @return self This object to support chaining.
182 */
183 public function setConstraints($settings)
184 {
185 // DO NOT cast the settings to array/object because
186 // the VAPInvoiceConstraints might be passed.
187 // In that case, an iterator should be returned.
188
189 foreach ($settings as $k => $v)
190 {
191 $this->data->constraints->{$k} = $v;
192 }
193
194 return $this;
195 }
196
197 /**
198 * Returns an object containing the invoice parameters and constraints.
199 *
200 * @param boolean $array True to return the data as array.
201 *
202 * @return mixed
203 */
204 public function getData($array = false)
205 {
206 if (!$array)
207 {
208 return clone $this->data;
209 }
210 else
211 {
212 $data = array(
213 'params' => (array) $this->data->params,
214 'constraints' => $this->data->constraints->toArray(),
215 );
216 }
217
218 return $data;
219 }
220
221 /**
222 * Generates the invoices related to the specified order.
223 *
224 * @param boolean $increase True to increase the invoice number
225 * by one step after generation.
226 *
227 * @return mixed The invoice array data on success, otherwise false.
228 */
229 public function generate($increase = true)
230 {
231 if (!$this->invoice)
232 {
233 // invoice not yet set
234 return false;
235 }
236
237 // get current language tag
238 $lang = JFactory::getLanguage()->getTag();
239
240 // always load the template by using the default language of the website
241 VikAppointments::loadLanguage(VikAppointments::getDefaultLanguage(), JPATH_SITE);
242
243 // generate PDF
244 $path = $this->invoice->generate();
245
246 // restore previous language
247 VikAppointments::loadLanguage($lang);
248
249 if (!$path)
250 {
251 // something went wrong
252 return false;
253 }
254
255 if ($increase)
256 {
257 // increase invoice number in case we are generating progressively
258 $this->increaseNumber();
259 }
260
261 return $path;
262 }
263
264 /**
265 * Sends the invoice via e-mail to the customer.
266 *
267 * @param string $path The invoice path, which will be
268 * included as attachment within the e-mail.
269 *
270 * @return boolean True on success, otherwise false.
271 */
272 public function send($path)
273 {
274 if (!$this->invoice)
275 {
276 // invoice not yet set
277 return false;
278 }
279
280 $to = $this->invoice->getRecipient();
281
282 if (!$to)
283 {
284 return false;
285 }
286
287 $sendermail = VikAppointments::getSenderMail();
288 $fromname = VAPFactory::getConfig()->get('agencyname');
289
290 $id = basename($path);
291 $id = substr($id, 0, strrpos($id, '.'));
292
293 // get current language tag
294 $lang = JFactory::getLanguage()->getTag();
295
296 // always load the mail contents by using the default language of the website
297 VikAppointments::loadLanguage(VikAppointments::getDefaultLanguage(), JPATH_SITE);
298
299 // fetch mail subject
300 $subject = JText::sprintf('VAPINVMAILSUBJECT', $fromname, $id);
301
302 /**
303 * Added message to e-mail.
304 *
305 * @since 1.7
306 */
307 $content = JText::sprintf('VAPINVMAILCONTENT', $fromname, $id);
308
309 // restore previous language
310 VikAppointments::loadLanguage($lang);
311
312 $vik = VAPApplication::getInstance();
313
314 return $vik->sendMail(
315 $sendermail,
316 $fromname,
317 $to,
318 null,
319 $subject,
320 $content,
321 array($path),
322 $isHtml = false
323 );
324 }
325
326 /**
327 * Method used to save the current parameters and settings.
328 *
329 * @return self This object to support chaining.
330 *
331 * @uses getData()
332 */
333 public function save()
334 {
335 // update invoice data
336 VAPFactory::getConfig()->set('invoiceobj', $this->getData());
337
338 return $this;
339 }
340
341 /**
342 * Increase the invoice number after a successful generation.
343 *
344 * @return void
345 *
346 * @uses save()
347 */
348 protected function increaseNumber()
349 {
350 // increase number by one
351 $this->data->params->number++;
352 // store parameters
353 $this->save();
354 }
355 }
356