| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2024 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 |
* Factory class for the e-invoicing framework. |
| 16 |
* |
| 17 |
* @since 1.16.7 (J) - 1.6.7 (WP) |
| 18 |
*/ |
| 19 |
final class VBOEinvoicingFactory |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The singleton instance of the class. |
| 23 |
* |
| 24 |
* @var VBOEinvoicingFactory |
| 25 |
*/ |
| 26 |
private static $instance = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* List of loaded driver objects. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $drivers = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns an instance of the class. |
| 37 |
* |
| 38 |
* @return VBOEinvoicingFactory |
| 39 |
*/ |
| 40 |
public static function getInstance() |
| 41 |
{ |
| 42 |
if (is_null(static::$instance)) { |
| 43 |
static::$instance = new static(); |
| 44 |
} |
| 45 |
|
| 46 |
return static::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Class constructor. |
| 51 |
*/ |
| 52 |
public function __construct() |
| 53 |
{ |
| 54 |
// load dependencies |
| 55 |
$driver_base = VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'einvoicing' . DIRECTORY_SEPARATOR; |
| 56 |
|
| 57 |
require_once $driver_base . 'einvoicing.php'; |
| 58 |
|
| 59 |
// load drivers |
| 60 |
$this->drivers = $this->loadDrivers(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the list of loaded drivers. |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function getDrivers() |
| 69 |
{ |
| 70 |
return $this->drivers; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Loads the list of available e-invoicing drivers. |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function loadDrivers() |
| 79 |
{ |
| 80 |
$loaded = []; |
| 81 |
|
| 82 |
$dbo = JFactory::getDbo(); |
| 83 |
|
| 84 |
// always load all drivers, db and plugin based ones |
| 85 |
$dbo->setQuery( |
| 86 |
$dbo->getQuery(true) |
| 87 |
->select('*') |
| 88 |
->from($dbo->qn('#__vikbooking_einvoicing_config')) |
| 89 |
->where($dbo->qn('automatic') . ' = 1') |
| 90 |
); |
| 91 |
$drivers = $dbo->loadAssocList(); |
| 92 |
|
| 93 |
/** |
| 94 |
* Trigger event to let other plugins register additional drivers. |
| 95 |
* |
| 96 |
* @return array A list of supported drivers. |
| 97 |
*/ |
| 98 |
$list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadEinvoicingDrivers'); |
| 99 |
foreach ($list as $chunk) { |
| 100 |
// merge default driver files with the returned ones |
| 101 |
$drivers = array_merge($drivers, (array)$chunk); |
| 102 |
} |
| 103 |
|
| 104 |
// drivers base path |
| 105 |
$driver_base = implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'helpers', 'einvoicing', 'drivers']) . DIRECTORY_SEPARATOR; |
| 106 |
|
| 107 |
// invoke all drivers that should run automatically, or that were loaded by third-party plugins |
| 108 |
foreach ($drivers as $driver) { |
| 109 |
$driver_file = is_string($driver) ? $driver : $driver['driver'] . '.php'; |
| 110 |
$driver_path = $driver_base . $driver_file; |
| 111 |
|
| 112 |
if (is_file($driver_path)) { |
| 113 |
// require the driver sub-class |
| 114 |
require_once $driver_path; |
| 115 |
} elseif (is_file($driver_file)) { |
| 116 |
// require the plugin-loaded driver |
| 117 |
require_once $driver_file; |
| 118 |
} |
| 119 |
|
| 120 |
// build driver class name |
| 121 |
$classname = 'VikBookingEInvoicing' . str_replace(' ', '', ucwords(str_replace('.php', '', str_replace('_', ' ', $driver_file)))); |
| 122 |
if (!class_exists($classname)) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
// instantiate the object |
| 127 |
$loaded[] = new $classname; |
| 128 |
} |
| 129 |
|
| 130 |
return $loaded; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns a list of additional invoices available for a specific booking. |
| 135 |
* For example, one e-invoicing driver like myDATA could generate one |
| 136 |
* correlated invoice for the same reservation ID. |
| 137 |
* |
| 138 |
* @param int $bid the VikBooking reservation ID. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function getBookingExtraInvoices($bid) |
| 143 |
{ |
| 144 |
$extra_invoices = []; |
| 145 |
|
| 146 |
foreach ($this->drivers as $driver) { |
| 147 |
$extras = $driver->getBookingExtraInvoices($bid); |
| 148 |
if ($extras) { |
| 149 |
$extra_invoices = array_merge($extra_invoices, $extras); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $extra_invoices; |
| 154 |
} |
| 155 |
} |
| 156 |
|