* @copyright 2007-2015 PrestaShop SA * @license http://addons.prestashop.com/en/content/12-terms-and-conditions-of-use * International Registered Trademark & Property of PrestaShop SA */ include_once('Expeditor.class.php'); class GenerateCsv { private static function formatItem($string) { return '"' . $string . '";'; } private static function changeGender($gender) { if ($gender == '1') { return '2'; } elseif ($gender == '2') { return '3'; } elseif ($gender == '3') { return '4'; } } private static function getWeight($value) { $multiplicator = Configuration::get('EXPEDITOR_MULTIPLY'); $weight = $value; if ($multiplicator == "1") { $weight = $weight*1000; } return self::formatItem($weight); } private static function getStandardSize($value) { $ret = 'N'; if ($value == "true") { $ret = 'O'; } return self::formatItem($ret); } public static function generate($array) { // ob_clean(); // header("Content-Transfer-Encoding: binary"); header('Content-Type: application/vnd.ms-excel; charset=ISO-8859-1'); // Should work for IE & Opera header("Content-Type: application/x-msexcel; charset=ISO-8859-1"); // Should work for the rest foreach ($array as $value) { ob_start(); ini_set('display_errors', 'On'); $order = new Order((int)($value['id_order'])); $customer = new Customer((int)($order->id_customer)); $address = new Address((int)($order->id_address_delivery)); $address_invoice = new Address((int)($order->id_address_invoice)); $country = new Country((int)($address->id_country)); $delivery_info = array(); if (Configuration::get('EXPEDITOR_CARRIER_CODES_'.(int)($value['id_carrier'])) == 'SO') { $delivery_info = Db::getInstance()->getRow( 'SELECT * FROM '._DB_PREFIX_.'socolissimo_delivery_info WHERE id_cart ='.(int)($order->id_cart).' AND id_customer = '.(int)($customer->id) ); } if (isset($delivery_info) && !empty($delivery_info)) { // Numbers comment match the position in the .fmt file echo utf8_decode(self::formatItem(trim($delivery_info['delivery_mode']))); // code produit = 1 echo self::formatItem('EXP'.(int)($order->id)); // ref EXP + id_order /*echo utf8_decode(self::formatItem('1')); // 1 echo utf8_decode(self::formatItem('2')); // 1*/ if (in_array($delivery_info['delivery_mode'], array('RDV', 'DOM', 'DOS'))) { echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $delivery_info['prname']), 0, 35)) ); // Nom = 3 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $delivery_info['pradress3']), 0, 35)) ); // Numéro de voie = 4 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $delivery_info['pradress1']), 0, 35)) ); // Etage, couloir = 5 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $delivery_info['pradress2']), 0, 35)) ); // entrée, batiment escalier = 6 echo utf8_decode( self::formatItem(Tools::substr($delivery_info['przipcode'], 0, 35)) ); // code postale 7 echo utf8_decode( self::formatItem(Tools::substr($delivery_info['prtown'], 0, 35)) ); // ville 8 /*echo utf8_decode(self::formatItem('3.1')); // 1 echo utf8_decode(self::formatItem('4.1')); // 1 echo utf8_decode(self::formatItem('5.1')); // 1 echo utf8_decode(self::formatItem('6.1')); // 1 echo utf8_decode(self::formatItem('7.1')); // 1 echo utf8_decode(self::formatItem('8.1')); // 1*/ } else { echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $address_invoice->lastname), 0, 35)) ); // Nom = 3 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $address_invoice->address1), 0, 35)) ); // = 4 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $address_invoice->address2), 0, 35)) ); // = 5 echo utf8_decode(self::formatItem('')); // = 6 echo self::formatItem(str_replace(' ', '', $address_invoice->postcode)); // code postale 7 echo utf8_decode(self::formatItem($address_invoice->city)); // ville 8 /*echo utf8_decode(self::formatItem('3.2')); // 1 echo utf8_decode(self::formatItem('4.2')); // 1 echo utf8_decode(self::formatItem('5.2')); // 1 echo utf8_decode(self::formatItem('6.2')); // 1 echo utf8_decode(self::formatItem('7.2')); // 1 echo utf8_decode(self::formatItem('8.2')); // 1*/ } } else { echo utf8_decode( self::formatItem(Configuration::get('EXPEDITOR_CARRIER_CODES_'.(int)($value['id_carrier']))) ); // code produit = 1 echo self::formatItem('EXP'.(int)($order->id)); // ref EXP + id_order echo utf8_decode(self::formatItem(trim($address->lastname))); // Nom = 3 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $address->address1), 0, 35)) ); // addresse1 = 4 echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $address->address2), 0, 35)) ); // = 5 echo self::formatItem(''); // addresse3 = 6 echo self::formatItem(str_replace(' ', '', $address->postcode)); // code postale 7 echo utf8_decode(self::formatItem($address->city)); // ville 8*/ /*echo utf8_decode(self::formatItem('1.3')); // 1 echo utf8_decode(self::formatItem('2.3')); // 1 echo utf8_decode(self::formatItem('3.3')); // 1 echo utf8_decode(self::formatItem('4.3')); // 1 echo utf8_decode(self::formatItem('5.3')); // 1 echo utf8_decode(self::formatItem('6.3')); // 1 echo utf8_decode(self::formatItem('7.3')); // 1 echo utf8_decode(self::formatItem('8.3')); // 1*/ } echo utf8_decode(self::formatItem($country->iso_code)); // Code pays iso 9 echo self::getWeight($value['weight']); // poids 10 echo utf8_decode(self::formatItem('0')); // montant 11 echo self::getStandardSize($value['standard_size']); // 12 /*echo utf8_decode(self::formatItem('9')); // 1 echo utf8_decode(self::formatItem('10')); // 10 echo utf8_decode(self::formatItem('11')); // 1 echo utf8_decode(self::formatItem('12')); // 1*/ if (Configuration::get('EXPEDITOR_CARRIER_CODES_'.(int)($value['id_carrier'])) == 'SO' && $delivery_info) { echo utf8_decode(self::formatItem(($delivery_info['cephonenumber']))); // 13 echo utf8_decode(self::formatItem($customer->email)); // 14 if (in_array($delivery_info['delivery_mode'], array('RDV', 'DOM', 'DOS'))) { echo utf8_decode( self::formatItem(Tools::substr(str_replace(',', '', $delivery_info['pradress4']), 0, 35)) ); // 15 } else { echo utf8_decode(self::formatItem('')); // 15 } echo utf8_decode(self::formatItem(self::changeGender($customer->id_gender))); // civilité 16 if (isset($delivery_info['prfirstname']) and !empty($delivery_info['prfirstname'])) { echo utf8_decode(self::formatItem($delivery_info['prfirstname'])); // prenom = 17 } else { if (Configuration::get('EXPEDITOR_CARRIER_CODES_'.(int)($value['id_carrier'])) == 'SO') { echo utf8_decode(self::formatItem($address_invoice->firstname)); } else { echo utf8_decode(self::formatItem($customer->firstname)); // prenom = 17 Fix BUG } } echo utf8_decode(self::formatItem($delivery_info['cecompanyname'])); // 18 echo utf8_decode(self::formatItem(($delivery_info['cephonenumber']))); // 19 echo utf8_decode(self::formatItem($delivery_info['cedoorcode1'])); // 20 echo utf8_decode(self::formatItem($delivery_info['cedoorcode2'])); // 21 /*echo utf8_decode(self::formatItem('13.1')); // 1 echo utf8_decode(self::formatItem('14.1')); // 1 echo utf8_decode(self::formatItem('15.1')); // 1 echo utf8_decode(self::formatItem('16.1')); // 1 echo utf8_decode(self::formatItem('17.1')); // 1 echo utf8_decode(self::formatItem('18.1')); // 1 echo utf8_decode(self::formatItem('19.1')); // 1 echo utf8_decode(self::formatItem('20.1')); // 20 echo utf8_decode(self::formatItem('21.1')); // 1*/ } else { $phone = ''; if (isset($address->phone)) { $phone = $address->phone; } elseif (isset($address->phone_mobile)) { $phone = $address->phone_mobile; } echo self::formatItem($phone); echo self::formatItem((($customer->email != '') ? $customer->email : '')); // 14` echo utf8_decode(self::formatItem('')); // addresse4 = 15 echo utf8_decode(self::formatItem(self::changeGender($customer->id_gender))); // civilité 16 echo utf8_decode(self::formatItem($address->firstname)); // Prenom > 17 echo utf8_decode(self::formatItem($address->company)); // 18 echo self::formatItem($address->phone_mobile); // 19 echo self::formatItem(''); // 20 echo self::formatItem(''); // 21 /*echo utf8_decode(self::formatItem('13.2')); // 1 echo utf8_decode(self::formatItem('14.2')); // 1 echo utf8_decode(self::formatItem('15.2')); // 1 echo utf8_decode(self::formatItem('16.2')); // 1 echo utf8_decode(self::formatItem('17.2')); // 1 echo utf8_decode(self::formatItem('18.2')); // 1 echo utf8_decode(self::formatItem('19.2')); // 1 echo utf8_decode(self::formatItem('20.2')); // 20 echo utf8_decode(self::formatItem('21.2')); // 1*/ } if ((isset($delivery_info) && !empty($delivery_info) && in_array($delivery_info['delivery_mode'], array('BPR', 'ACP', 'CIT', 'CDI', 'A2P', 'CMT', 'BDP')) ) or Configuration::get('EXPEDITOR_CARRIER_CODES_'.(int)($value['id_carrier'])) == 'SO') { echo utf8_decode(self::formatItem($delivery_info['prid'])); // 22 } else { echo self::formatItem(''); // other 22 } echo self::formatItem(Configuration::get('PS_SHOP_NAME')); // 23 $message = $order->getFirstMessage(); if (!$message) { $message = ""; } if (isset($delivery_info) && !empty($delivery_info) && in_array($delivery_info['delivery_mode'], array('RDV', 'DOM', 'BPR', 'A2P')) ) { if (isset($delivery_info['cedeliveryinformation'])) { echo utf8_decode(self::formatItem($delivery_info['cedeliveryinformation'])); // 24 } else { echo utf8_decode(self::formatItem($message)); // 24 } } else { echo self::formatItem(''); // 24 } if (isset($delivery_info) && !empty($delivery_info) && in_array($delivery_info['delivery_mode'], array('CMT', 'BDP')) ) { echo utf8_decode(self::formatItem($delivery_info['przipcode'])); // 25 } else { echo self::formatItem(''); // 25 } echo chr(13); // CR --> carriage return echo chr(10); // LF --> new line } exit(ob_get_clean()); } }