PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / conditionalrules / mydata_aade.php

mydata_aade.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/conditionalrules/mydata_aade.php

315 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Class handler for conditional rule "myDATA AADE" (electronic invoices driver for Greece).
15 * This Conditional Text Rule will include a QR Code image and the Invoice Mark number.
16 *
17 * @since 1.16.7 (J) - 1.6.7 (WP)
18 */
19 class VikBookingConditionalRuleMydataAade extends VikBookingConditionalRule
20 {
21 /**
22 * @var object
23 */
24 protected $transmission_data = null;
25
26 /**
27 * @var array
28 */
29 protected $correlated_invoice_data = [];
30
31 /**
32 * Class constructor will define the rule name, description and identifier.
33 */
34 public function __construct()
35 {
36 // call parent constructor
37 parent::__construct();
38
39 $this->ruleName = 'myDATA - ΑΑΔΕ Greece';
40 $this->ruleDescr = 'Include QR Code and Mark for the e-invoices generated through ΑΑΔΕ (Greece).';
41 $this->ruleId = basename(__FILE__);
42 }
43
44 /**
45 * Displays the rule parameters.
46 *
47 * @return void
48 */
49 public function renderParams()
50 {
51 ?>
52 <div class="vbo-param-container">
53 <div class="vbo-param-label">Choose the type of invoice</div>
54 <div class="vbo-param-setting">
55 <select name="<?php echo $this->inputName('invoice_type'); ?>">
56 <option value=""></option>
57 <option value="main"<?php echo $this->getParam('invoice_type', '') == 'main' ? ' selected="selected"' : ''; ?>>Reservation main invoice</option>
58 <option value="envfee"<?php echo $this->getParam('invoice_type', '') == 'envfee' ? ' selected="selected"' : ''; ?>>Environmental fee invoice</option>
59 </select>
60 <span class="vbo-param-setting-comment">Choose for which type of invoice the elements should be included. It is necessary to create a different Conditional Text Rule to cover both invoice types.</span>
61 </div>
62 </div>
63 <div class="vbo-param-container">
64 <div class="vbo-param-label">QR Code</div>
65 <div class="vbo-param-setting">
66 <?php echo $this->vbo_app->printYesNoButtons($this->inputName('qr_code'), JText::translate('VBYES'), JText::translate('VBNO'), (int)$this->getParam('qr_code', 0), 1, 0); ?>
67 <span class="vbo-param-setting-comment">You can optionally use the tag <strong onclick="vboMydataAadeAddContentEditor('{mydata_aade_qrcode_img}');" style="cursor: pointer;">{mydata_aade_qrcode_img}</strong> if you would like to place the QR Code PNG image on a specific section of the message.</span>
68 </div>
69 </div>
70 <div class="vbo-param-container">
71 <div class="vbo-param-label">QR Code width (px)</div>
72 <div class="vbo-param-setting">
73 <input type="number" name="<?php echo $this->inputName('qr_code_width'); ?>" value="<?php echo $this->getParam('qr_code_width', ''); ?>" min="1" max="999" />
74 </div>
75 </div>
76 <div class="vbo-param-container">
77 <div class="vbo-param-label">Invoice Mark</div>
78 <div class="vbo-param-setting">
79 <?php echo $this->vbo_app->printYesNoButtons($this->inputName('invoice_mark'), JText::translate('VBYES'), JText::translate('VBNO'), (int)$this->getParam('invoice_mark', 0), 1, 0); ?>
80 <span class="vbo-param-setting-comment">You can optionally use the tag <strong onclick="vboMydataAadeAddContentEditor('{mydata_aade_invmark}');" style="cursor: pointer;">{mydata_aade_invmark}</strong> if you would like to place the <i>invoice mark</i> on a specific section of the message.</span>
81 </div>
82 </div>
83
84 <script type="text/javascript">
85 function vboMydataAadeAddContentEditor(str) {
86 if (!str) {
87 return;
88 }
89
90 try {
91 // "msg" is the name of the WYSIWYG editor of the conditional text
92 Joomla.editors.instances.msg.replaceSelection(str);
93 } catch(e) {
94 // do nothing
95 }
96 }
97 </script>
98 <?php
99 }
100
101 /**
102 * Tells whether the rule is compliant.
103 *
104 * @return bool True on success, false otherwise.
105 */
106 public function isCompliant()
107 {
108 $booking_id = (int)$this->getPropVal('booking', 'id', 0);
109
110 // ensure the myDATA - AADE driver is configured
111 $mydata_driver_id = $this->getDriverId();
112
113 if (!$mydata_driver_id) {
114 return false;
115 }
116
117 // check if an electronic invoice was generated and transmitted for this booking
118 $this->transmission_data = $this->getBookingTransmissionData($mydata_driver_id, $booking_id);
119
120 if (!$this->transmission_data) {
121 return false;
122 }
123
124 if (!strcasecmp($this->getParam('invoice_type', 'main'), 'envfee')) {
125 // make sure data is available for the environmental fee invoice
126
127 $last_einv_id = $this->getLastEinvoiceId($mydata_driver_id, $booking_id);
128
129 $this->correlated_invoice_data = VBOFactory::getConfig()->getArray("envfee_invoice_{$mydata_driver_id}_{$last_einv_id}_{$booking_id}", []);
130
131 if (!$this->correlated_invoice_data || empty($this->correlated_invoice_data['transmission'])) {
132 return false;
133 }
134 }
135
136 return true;
137 }
138
139 /**
140 * Allows to manipulate the message of the conditional text with dynamic contents.
141 *
142 * @override
143 *
144 * @param string $msg the current conditional text message.
145 *
146 * @return string the manipulated conditional text message.
147 */
148 public function manipulateMessage($msg)
149 {
150 if (!is_object($this->transmission_data)) {
151 // reservation main invoice transmission data is always mandatory
152 return $msg;
153 }
154
155 $invoice_type = $this->getParam('invoice_type', 'main');
156
157 if (!strcasecmp($invoice_type, 'envfee') && !$this->correlated_invoice_data) {
158 // missing transmission data for the environmental fee invoice
159 return $msg;
160 }
161
162 $add_qr = (bool)$this->getParam('qr_code', 0);
163 $add_mark = (bool)$this->getParam('invoice_mark', 0);
164 $qr_width = (int)$this->getParam('qr_code_width', 128);
165
166 if (!$add_qr && !$add_mark) {
167 return $msg;
168 }
169
170 if (!strcasecmp($invoice_type, 'envfee')) {
171 // correlated invoice
172 $invoice_qrcode = !empty($this->correlated_invoice_data['transmission']['qrcode_img']) ? $this->correlated_invoice_data['transmission']['qrcode_img'] : '';
173 $invoice_mark = !empty($this->correlated_invoice_data['transmission']['mark']) ? $this->correlated_invoice_data['transmission']['mark'] : '';
174 } else {
175 // reservation main invoice
176 $invoice_qrcode = !empty($this->transmission_data->qrcode_img) ? $this->transmission_data->qrcode_img : '';
177 $invoice_mark = !empty($this->transmission_data->invoice_mark) ? $this->transmission_data->invoice_mark : '';
178 }
179
180 // build the HTML content for the QR Code PNG image
181 $qrcode_html = '';
182 if ($add_qr && !empty($invoice_qrcode)) {
183 // build the image tag
184 $qrcode_html = '<img src="' . $this->getQRCodeUri($invoice_qrcode) . '" width="' . $qr_width . '" alt="Invoice QR Code" />';
185
186 if (strpos($msg, '{mydata_aade_qrcode_img}') !== false) {
187 // exact placeholder tag found
188 $msg = str_replace('{mydata_aade_qrcode_img}', $qrcode_html, $msg);
189 } else {
190 // append image tag to message
191 $msg .= $qrcode_html;
192 }
193 }
194
195 // build the HTML content for the invoice mark (number)
196 if ($add_mark && !empty($invoice_mark)) {
197 if (strpos($msg, '{mydata_aade_invmark}') !== false) {
198 // exact placeholder tag found, so use the plain mark string
199 $msg = str_replace('{mydata_aade_invmark}', $invoice_mark, $msg);
200 } else {
201 // append P tag to message
202 $msg .= '<p>' . $invoice_mark . '</p>';
203 }
204 }
205
206 // return the manipulated message
207 return $msg;
208 }
209
210 /**
211 * Tells if the myDATA - AADE driver is configured by returning its ID.
212 *
213 * @return mixed null or integer ID, if configured.
214 */
215 protected function getDriverId()
216 {
217 $dbo = JFactory::getDbo();
218
219 $dbo->setQuery(
220 $dbo->getQuery(true)
221 ->select($dbo->qn('id'))
222 ->from($dbo->qn('#__vikbooking_einvoicing_config'))
223 ->where($dbo->qn('driver') . ' = ' . $dbo->q('mydata_aade'))
224 );
225
226 return $dbo->loadResult();
227 }
228
229 /**
230 * Tells if the myDATA - AADE driver transmitted an invoice for the given booking ID.
231 *
232 * @param int $driver_id the myDATA - AADE driver ID.
233 * @param int $bid the VikBooking reservation ID.
234 *
235 * @return mixed null or transmission result object, if invoice is available.
236 */
237 protected function getBookingTransmissionData($driver_id, $bid)
238 {
239 $dbo = JFactory::getDbo();
240
241 $dbo->setQuery(
242 $dbo->getQuery(true)
243 ->select($dbo->qn('trans_data'))
244 ->from($dbo->qn('#__vikbooking_einvoicing_data'))
245 ->where($dbo->qn('driverid') . ' = ' . (int)$driver_id)
246 ->where($dbo->qn('idorder') . ' = ' . (int)$bid)
247 ->where($dbo->qn('transmitted') . ' = 1')
248 ->where($dbo->qn('obliterated') . ' = 0')
249 ->order($dbo->qn('id') . ' DESC')
250 );
251
252 $transmission_data = $dbo->loadResult();
253
254 if (!$transmission_data) {
255 return null;
256 }
257
258 $transmission_data = json_decode($transmission_data);
259
260 if (!is_object($transmission_data) || (empty($transmission_data->invoice_mark) && empty($transmission_data->qrcode_img))) {
261 return null;
262 }
263
264 return $transmission_data;
265 }
266
267 /**
268 * Returns the last e-invoice record ID for the given reservation ID.
269 *
270 * @param int $driver_id the myDATA - AADE driver ID.
271 * @param int $bid the VikBooking reservation ID.
272 *
273 * @return int the last e-invoice ID generated, or 0.
274 */
275 protected function getLastEinvoiceId($driver_id, $bid)
276 {
277 $dbo = JFactory::getDbo();
278
279 $dbo->setQuery(
280 $dbo->getQuery(true)
281 ->select($dbo->qn('id'))
282 ->from($dbo->qn('#__vikbooking_einvoicing_data'))
283 ->where($dbo->qn('driverid') . ' = ' . (int)$driver_id)
284 ->where($dbo->qn('idorder') . ' = ' . (int)$bid)
285 ->where($dbo->qn('transmitted') . ' = 1')
286 ->where($dbo->qn('obliterated') . ' = 0')
287 ->order($dbo->qn('id') . ' DESC')
288 );
289
290 $einv_id = $dbo->loadResult();
291
292 return $einv_id ?: 0;
293 }
294
295 /**
296 * Returns the full URI to the given PNG filename for the QR Code.
297 * Rather than using static paths or URIs, we use the apposite helper.
298 *
299 * @param string $qrcode_fname the QR Code PNG filename.
300 *
301 * @return string
302 */
303 protected function getQRCodeUri($qrcode_fname)
304 {
305 // load the helper for the myDATA - AADE driver
306 require_once implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'einvoicing', 'drivers', 'MydataAade', 'constants.php']);
307
308 if (!class_exists('VikBookingMydataAadeConstants')) {
309 return $qrcode_fname;
310 }
311
312 return VikBookingMydataAadeConstants::getQRCodeBase('uri', $qrcode_fname);
313 }
314 }
315