| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikRentCar |
| 4 |
* @subpackage com_vikrentcar |
| 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 |
class vikRentCarXmlExport { |
| 14 |
|
| 15 |
private $orders; |
| 16 |
private $params; |
| 17 |
private $custom_fields; |
| 18 |
private $datetime_format; |
| 19 |
private $num_decimals; |
| 20 |
private $decimal_point; |
| 21 |
private $thousand_separator; |
| 22 |
|
| 23 |
private $anag_type; |
| 24 |
private $mappa_codici_sede; |
| 25 |
private $mappa_codici_gruppo_veicoli; |
| 26 |
private $targa_feature_key; |
| 27 |
private $mappa_codici_tariffe; |
| 28 |
|
| 29 |
static function getAdminParameters () { |
| 30 |
return array(); |
| 31 |
} |
| 32 |
|
| 33 |
public function __construct ($orders, $params = array()) { |
| 34 |
$this->orders = $orders; |
| 35 |
$this->params = $params; |
| 36 |
$this->custom_fields = $this->loadCustomFields(); |
| 37 |
$this->datetime_format = 'd/m/Y H:i'; |
| 38 |
$this->num_decimals = 2; |
| 39 |
$this->decimal_point = ','; |
| 40 |
$this->thousand_separator = ''; |
| 41 |
|
| 42 |
$this->anag_type = 'F'; //Persona Fisica |
| 43 |
//Codici Sede Gestionale (default empty) |
| 44 |
$this->mappa_codici_sede = array(); |
| 45 |
/* |
| 46 |
$this->mappa_codici_sede = array( |
| 47 |
'1' => 'Codice Sede Gestionale con ID 1 in VikRentCar', |
| 48 |
'2' => 'Codice Sede Gestionale con ID 2 in VikRentCar', |
| 49 |
'5' => 'Codice Sede Gestionale con ID 5 in VikRentCar', |
| 50 |
); |
| 51 |
*/ |
| 52 |
//Fine Codici Sede Gestionale |
| 53 |
//Codici Gruppo Veicoli Gestionale |
| 54 |
$this->mappa_codici_gruppo_veicoli = array(); |
| 55 |
/* |
| 56 |
$this->mappa_codici_gruppo_veicoli = array( |
| 57 |
'1' => 'Codice Gruppo Veicolo con ID 1 in VikRentCar', |
| 58 |
'3' => 'Codice Gruppo Veicolo con ID 3 in VikRentCar', |
| 59 |
'5' => 'Codice Gruppo Veicolo con ID 5 in VikRentCar', |
| 60 |
); |
| 61 |
*/ |
| 62 |
//Fine Codici Gruppo Veicoli Gestionale |
| 63 |
//Targa veicolo (di default License Plate corrisponde alla definizione lingua VRCDEFAULTDISTFEATUREONE nei files .ini) |
| 64 |
$this->targa_feature_key = 'VRCDEFAULTDISTFEATUREONE'; |
| 65 |
//Codici Tariffe Gestionale |
| 66 |
$this->mappa_codici_tariffe = array(); |
| 67 |
/* |
| 68 |
$this->mappa_codici_tariffe = array( |
| 69 |
'1' => 'Codice Tariffa per Type of Price con ID 1 in VikRentCar', |
| 70 |
'3' => 'Codice Tariffa per Type of Price con ID 3 in VikRentCar', |
| 71 |
'5' => 'Codice Tariffa per Type of Price con ID 5 in VikRentCar', |
| 72 |
); |
| 73 |
*/ |
| 74 |
} |
| 75 |
|
| 76 |
public function generateXml () { |
| 77 |
$xml = '<?xml version="1.0" encoding="utf-8"?>'."\n"; |
| 78 |
//Debug Array $this->orders |
| 79 |
//echo '<pre>'.print_r($this->orders, true).'</pre>';die; |
| 80 |
// |
| 81 |
foreach ($this->orders as $k => $order) { |
| 82 |
$xml .= '<nol>'."\n"; |
| 83 |
//Sede di Uscita |
| 84 |
if(!empty($order['pickup_location_name']) && array_key_exists($order['idplace'], $this->mappa_codici_sede)) { |
| 85 |
$sede_uscita = $this->mappa_codici_sede[$order['idplace']]; |
| 86 |
}else { |
| 87 |
$sede_uscita = $order['pickup_location_name']; |
| 88 |
} |
| 89 |
$xml .= "\t".'<sedeu>'.$sede_uscita.'</sedeu>'."\n"; |
| 90 |
// |
| 91 |
//Sede di Rientro |
| 92 |
if(!empty($order['dropoff_location_name']) && array_key_exists($order['idreturnplace'], $this->mappa_codici_sede)) { |
| 93 |
$sede_rientro = $this->mappa_codici_sede[$order['idreturnplace']]; |
| 94 |
}else { |
| 95 |
$sede_rientro = $order['dropoff_location_name']; |
| 96 |
} |
| 97 |
$xml .= "\t".'<seder>'.$sede_rientro.'</seder>'."\n"; |
| 98 |
// |
| 99 |
//Data inizio noleggio |
| 100 |
$xml .= "\t".'<datau>'.date($this->datetime_format, $order['ritiro']).'</datau>'."\n"; |
| 101 |
// |
| 102 |
//Data fine noleggio |
| 103 |
$xml .= "\t".'<datar>'.date($this->datetime_format, $order['consegna']).'</datar>'."\n"; |
| 104 |
// |
| 105 |
//Gruppo Veicolo |
| 106 |
if(array_key_exists($order['idcar'], $this->mappa_codici_gruppo_veicoli)) { |
| 107 |
$gruppo_veicolo = $this->mappa_codici_gruppo_veicoli[$order['idcar']]; |
| 108 |
}else { |
| 109 |
$gruppo_veicolo = $order['car_details']['name']; |
| 110 |
} |
| 111 |
$xml .= "\t".'<grpv>'.date($this->datetime_format, $order['consegna']).'</grpv>'."\n"; |
| 112 |
// |
| 113 |
//Targa Veicolo |
| 114 |
if(!empty($order['carindex']) && !empty($order['car_details']['params'])) { |
| 115 |
$car_params = json_decode($order['car_details']['params'], true); |
| 116 |
if(array_key_exists('features', $car_params) && array_key_exists($order['carindex'], $car_params['features'])) { |
| 117 |
if(array_key_exists($this->targa_feature_key, $car_params['features'][$order['carindex']])) { |
| 118 |
$targa = $car_params['features'][$order['carindex']][$this->targa_feature_key]; |
| 119 |
}else { |
| 120 |
foreach ($car_params['features'][$order['carindex']] as $v) { |
| 121 |
$targa = $v; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
$xml .= "\t".'<trgv>'.$targa.'</trgv>'."\n"; |
| 126 |
} |
| 127 |
} |
| 128 |
// |
| 129 |
//Codice Tariffa |
| 130 |
if(array_key_exists($order['price_info']['idprice'], $this->mappa_codici_tariffe)) { |
| 131 |
$tariffa = $this->mappa_codici_tariffe[$order['price_info']['idprice']]; |
| 132 |
}else { |
| 133 |
$tariffa = $order['price_info']['name']; |
| 134 |
} |
| 135 |
$xml .= "\t".'<tar>'.$tariffa.'</tar>'."\n"; |
| 136 |
// |
| 137 |
//Importo Tariffa IVA esclusa |
| 138 |
$tariffa_no_iva = VikRentCar::sayCostMinusIva($order['price_info']['cost'], $order['price_info']['idprice']); |
| 139 |
$xml .= "\t".'<tar_imp>'.number_format($tariffa_no_iva, $this->num_decimals, $this->decimal_point, $this->thousand_separator).'</tar_imp>'."\n"; |
| 140 |
// |
| 141 |
//Km Inclusi al giorno |
| 142 |
$km = -1; |
| 143 |
if(!empty($order['price_info']['attrdata'])) { |
| 144 |
$km = intval($order['price_info']['attrdata']); |
| 145 |
} |
| 146 |
$xml .= "\t".'<tar_km>'.$km.'</tar_km>'."\n"; |
| 147 |
// |
| 148 |
//Giorni di noleggio |
| 149 |
$xml .= "\t".'<giorni>'.$order['days'].'</giorni>'."\n"; |
| 150 |
// |
| 151 |
//Costo noleggio senza accessori IVA esclusa |
| 152 |
$xml .= "\t".'<cs_prev>'.number_format($tariffa_no_iva, $this->num_decimals, $this->decimal_point, $this->thousand_separator).'</cs_prev>'."\n"; |
| 153 |
// |
| 154 |
//Costo Totale con accessori IVA esclusa |
| 155 |
$costo_totale = VikRentCar::sayCostMinusIva($order['order_total'], $order['price_info']['idprice']); |
| 156 |
$xml .= "\t".'<cs_tot>'.number_format($costo_totale, $this->num_decimals, $this->decimal_point, $this->thousand_separator).'</cs_tot>'."\n"; |
| 157 |
// |
| 158 |
//Codice IVA |
| 159 |
$tax = $order['price_info']['aliq']; |
| 160 |
if(((float)$tax - abs($tax)) > 0) { |
| 161 |
$tax = number_format($tax, $this->num_decimals, $this->decimal_point, $this->thousand_separator); |
| 162 |
}else { |
| 163 |
$tax = intval($tax); |
| 164 |
} |
| 165 |
$xml .= "\t".'<codiva>'.$tax.'</codiva>'."\n"; |
| 166 |
// |
| 167 |
//Costo Pagato |
| 168 |
$xml .= "\t".'<cs_pag>'.number_format($order['totpaid'], $this->num_decimals, $this->decimal_point, $this->thousand_separator).'</cs_pag>'."\n"; |
| 169 |
// |
| 170 |
//Dati Anagrafici |
| 171 |
if(!empty($order['custdata'])) { |
| 172 |
$xml .= "\t".'<ana>'."\n"; |
| 173 |
//Tipo Anagrafica |
| 174 |
$xml .= "\t\t".'<tp>'.$this->anag_type.'</tp>'."\n"; |
| 175 |
// |
| 176 |
//Custom Fields (use the code $this->getCustomField(ID_CUSTOM_FIELD, $order['custdata']) to retrieve the desired custom field) |
| 177 |
//Cognome (ID 2 by default) |
| 178 |
$cognome = $this->getCustomField(2, $order['custdata']); |
| 179 |
$xml .= "\t\t".'<cognome>'.$cognome.'</cognome>'."\n"; |
| 180 |
//Nome (ID 1 by default) |
| 181 |
$nome = $this->getCustomField(1, $order['custdata']); |
| 182 |
$xml .= "\t\t".'<nome>'.$nome.'</nome>'."\n"; |
| 183 |
//Via |
| 184 |
$via = $this->getCustomField(3, $order['custdata']); |
| 185 |
$xml .= "\t\t".'<via>'.$via.'</via>'."\n"; |
| 186 |
//Numero |
| 187 |
$num = $this->getCustomField(4, $order['custdata']); |
| 188 |
$xml .= "\t\t".'<num>'.$num.'</num>'."\n"; |
| 189 |
//CAP |
| 190 |
$cap = $this->getCustomField(5, $order['custdata']); |
| 191 |
$xml .= "\t\t".'<cap>'.$cap.'</cap>'."\n"; |
| 192 |
//Località |
| 193 |
$loc = $this->getCustomField(6, $order['custdata']); |
| 194 |
$xml .= "\t\t".'<loc>'.$loc.'</loc>'."\n"; |
| 195 |
//Provincia |
| 196 |
$prv = $this->getCustomField(7, $order['custdata']); |
| 197 |
$xml .= "\t\t".'<prv>'.$prv.'</prv>'."\n"; |
| 198 |
//Nazione (Custom Field of type Country) |
| 199 |
$xml .= "\t\t".'<naz>'.$order['country'].'</naz>'."\n"; |
| 200 |
//Codice Fiscale |
| 201 |
$codfisc = $this->getCustomField(8, $order['custdata']); |
| 202 |
$xml .= "\t\t".'<codfisc>'.$codfisc.'</codfisc>'."\n"; |
| 203 |
//Data di Nascita |
| 204 |
$ndata = $this->getCustomField(9, $order['custdata']); |
| 205 |
$xml .= "\t\t".'<ndata>'.$ndata.'</ndata>'."\n"; |
| 206 |
//Località di Nascita |
| 207 |
$nloc = $this->getCustomField(10, $order['custdata']); |
| 208 |
$xml .= "\t\t".'<nloc>'.$nloc.'</nloc>'."\n"; |
| 209 |
//Nazione di Nascita |
| 210 |
$nnaz = $this->getCustomField(11, $order['custdata']); |
| 211 |
$xml .= "\t\t".'<nnaz>'.$nnaz.'</nnaz>'."\n"; |
| 212 |
//Nazionalità |
| 213 |
$nnal = $this->getCustomField(12, $order['custdata']); |
| 214 |
$xml .= "\t\t".'<nnal>'.$nnal.'</nnal>'."\n"; |
| 215 |
//Fine Anagrafica cliente |
| 216 |
$xml .= "\t".'</ana>'."\n"; |
| 217 |
} |
| 218 |
// |
| 219 |
//Fine Ordine |
| 220 |
$xml .= '</nol>'."\n"; |
| 221 |
} |
| 222 |
|
| 223 |
return $xml; |
| 224 |
} |
| 225 |
|
| 226 |
private function loadCustomFields() { |
| 227 |
$dbo = JFactory::getDbo(); |
| 228 |
$q = "SELECT * FROM `#__vikrentcar_custfields`;"; |
| 229 |
$dbo->setQuery($q); |
| 230 |
$dbo->execute(); |
| 231 |
$cfields = $dbo->getNumRows() > 0 ? $dbo->loadAssocList() : ""; |
| 232 |
$cfmap = array(); |
| 233 |
if (is_array($cfields)) { |
| 234 |
foreach($cfields as $cf) { |
| 235 |
$cfmap[trim(JText::translate($cf['name']))] = $cf['id']; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return $cfmap; |
| 240 |
} |
| 241 |
|
| 242 |
private function getCustomField($cfid, $custdata) { |
| 243 |
$cfmapreplace = array(); |
| 244 |
$cfmap = $this->custom_fields; |
| 245 |
$partsreceived = explode("\n", $custdata); |
| 246 |
if (count($partsreceived) > 0) { |
| 247 |
foreach($partsreceived as $pst) { |
| 248 |
if (!empty($pst)) { |
| 249 |
$tmpdata = explode(":", $pst); |
| 250 |
if (array_key_exists(trim($tmpdata[0]), $cfmap)) { |
| 251 |
$cfmapreplace[(int)$cfmap[trim($tmpdata[0])]] = trim($tmpdata[1]); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
if (array_key_exists($cfid, $cfmapreplace)) { |
| 257 |
return $cfmapreplace[$cfid]; |
| 258 |
} |
| 259 |
|
| 260 |
return ''; |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
?> |