private function buildXMLOrder($id_order) { CertissimLogger::insertLog(__METHOD__ . ' : ' . __LINE__, 'construction du flux pour order ' . $id_order); $order = new Order($id_order); //gets back the delivery address $address_delivery = new Address((int) $order->id_address_delivery); //gets back the invoice address $address_invoice = new Address((int) $order->id_address_invoice); //gets back the customer $customer = new Customer((int) $order->id_customer); //initializatino of the XML root: <control> $xml_element_control = new CertissimControl(); //gets the lang used in the order $id_lang = $order->id_lang; //sets the gender, depends on PS version if (_PS_VERSION_ < '1.5') { $gender = $customer->id_gender == 2 ? $this->l('Ms.') : $this->l('Mr.'); } else { $customer_gender = new Gender($customer->id_gender); $lang_id = Language::getIdByIso('en'); if (empty($lang_id)) { $lang_id = Language::getIdByIso('fr'); } CertissimLogger::insertLog(__METHOD__ . ' : ' . __LINE__, "id_gender = " . $customer->id_gender . ", gender name =" . $customer_gender->name[$lang_id]); $gender = $this->l($customer_gender->name[$lang_id]); } //initialization of the element <utilisateur type='facturation'...> $xml_element_invoice_customer = new CertissimUtilisateur('facturation', $gender, $address_invoice->lastname, $address_invoice->firstname, $address_invoice->company, $address_invoice->phone, $address_invoice->phone_mobile, null, $customer->email); //gets customer stats $customer_stats = $customer->getStats(); //gets already existing orders for the customer $all_orders = Order::getCustomerOrders((int) $customer->id); //initialization of the element <siteconso> $xml_element_invoice_customer_stats = new CertissimSiteconso($customer_stats['total_orders'], $customer_stats['nb_orders'], $all_orders[count($all_orders) - 1]['date_add'], count($all_orders) > 1 ? $all_orders[1]['date_add'] : null); //gets back the invoice country $country = new Country((int) $address_invoice->id_country); //initialization of the element <adresse type="facturation" ...> $xml_element_invoice_address = new CertissimAdresse('facturation', $address_invoice->address1, $address_invoice->address2, $address_invoice->postcode, $address_invoice->city, $country->name[$id_lang]); //gets back the carrier used for this order $carrier = new Carrier((int) $order->id_carrier); //gets the carrier certissim type if (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { $carrier_type = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE', null, null, $order->id_shop); } else { $carrier_type = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE'); } //if the order is to be delivered at home: element <utilisateur type="livraison"...> has to be added if ($carrier_type == 4) { //initialization of the element <utilisateur type="livraison" ...> $xml_element_delivery_customer = new CertissimUtilisateur('livraison', $customer->id_gender == 2 ? $this->l('Miss') : $this->l('Mister'), $address_delivery->lastname, $address_delivery->firstname, $address_delivery->company, $address_delivery->phone, $address_delivery->phone_mobile, null, $customer->email); //gets back the delivery country $country = new Country((int) $address_delivery->id_country); //initialization of the element <adresse type="livraison" ...> $xml_element_delivery_address = new CertissimAdresse('livraison', $address_delivery->address1, $address_delivery->address2, $address_delivery->postcode, $address_delivery->city, $country->name[$id_lang], null); } //gets the used currency $currency = new Currency((int) $order->id_currency); if (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { $siteid = Configuration::get('CERTISSIM_SITEID', null, null, $order->id_shop); } else { $siteid = Configuration::get('CERTISSIM_SITEID'); } //initialize the element <infocommande> $xml_element_order_details = new CertissimInfocommande($siteid, $order->id, (string) $order->total_paid, self::getIpByOrder((int) $order->id), date('Y-m-d H:i:s'), $currency->iso_code); //gets the order products $products = $order->getProducts(); //define the default product type (depends on PS version) if (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { $default_product_type = Configuration::get('CERTISSIM_DEFAULT_PRODUCT_TYPE', null, null, $order->id_shop); } else { $default_product_type = Configuration::get('CERTISSIM_DEFAULT_PRODUCT_TYPE'); } //initialization of the element <list ...> $xml_element_products_list = new CertissimProductList(); //initialize the boolean that says if all the products in the order are downloadables $alldownloadables = true; foreach ($products as $product) { //check if the visited product is downloadable and update the boolean value $alldownloadables = $alldownloadables && strlen($product['download_hash']) > 0; //gets the main product category $product_categories = Product::getProductCategories((int) $product['product_id']); $product_category = array_pop($product_categories); //initilization of the element <produit ...> $xml_element_product = new CertissimXMLElement("<produit></produit>"); //gets the product certissim category (depends on PS version) if (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { $product_type = Configuration::get('CERTISSIM' . $product_category . '_PRODUCT_TYPE', null, null, $order->id_shop); } else { $product_type = Configuration::get('CERTISSIM' . $product_category . '_PRODUCT_TYPE'); } //if a certissim category is set: the type attribute takes the product certissim type value if ($product_type) { $xml_element_product->addAttribute('type', Configuration::get('CERTISSIM' . $product_category . '_PRODUCT_TYPE', null, null, $order->id_shop)); } else { //if certissim category not set: the type attribute takes the default value $xml_element_product->addAttribute('type', $default_product_type); } //sets the product reference that will be inserted into the XML stream //uses the product name by default $product_ref = $product['product_name']; //prefers ean13 if defined if (!empty($product['product_ean13'])) { $product_ref = $product['product_ean13']; } //prefers local reference if defined if (!empty($product['product_reference'])) { $product_ref = $product['product_reference']; } //adds attributes ref, nb, prixunit, and sets the value of the element <product> with the product name $xml_element_product->addAttribute('ref', CertissimTools::normalizeString($product_ref)); $xml_element_product->addAttribute('nb', $product['product_quantity']); $xml_element_product->addAttribute('prixunit', $product['total_price']); $xml_element_product->setValue($product['product_name']); //adds the element <product> to the element <list> $xml_element_products_list->addProduit($xml_element_product); } if ($alldownloadables) { $real_carrier_type = '5'; } elseif (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { //if selected carrier fianet type is defined, the type used will be the one got in the Configuration if (in_array(Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE', null, null, $order->id_shop), array_keys($this->_carrier_types))) { $real_carrier_type = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE', null, null, $order->id_shop); $real_carrier_speed = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_SPEED', null, null, $order->id_shop); } else { $real_carrier_type = Configuration::get('CERTISSIM_DEFAULT_CARRIER_TYPE', null, null, $order->id_shop); $real_carrier_speed = Configuration::get('CERTISSIM_DEFAULT_CARRIER_SPEED', null, null, $order->id_shop); } } elseif (in_array(Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE'), array_keys($this->_carrier_types))) { $real_carrier_type = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_TYPE'); $real_carrier_speed = Configuration::get('CERTISSIM_' . (string) $carrier->id . '_CARRIER_SPEED'); } else { $real_carrier_type = Configuration::get('CERTISSIM_DEFAULT_CARRIER_TYPE'); $real_carrier_speed = Configuration::get('CERTISSIM_DEFAULT_CARRIER_SPEED'); } //initialization of the element <transport> $xml_element_carrier = new CertissimTransport($real_carrier_type, $alldownloadables ? 'Téléchargement' : Tools::htmlentitiesUTF8($carrier->name), $alldownloadables ? '1' : $real_carrier_speed, null); //find the id of the payment module used (depends on the PS version) if (_PS_VERSION_ >= '1.5') { $id_payment_module = PaymentModule::getModuleIdByName($order->module); } else { $payment_module = Module::getInstanceByName($order->module); $id_payment_module = $payment_module->id; } //initialization of the element <paiement> if (_PS_VERSION_ >= '1.5' && Shop::isFeatureActive()) { $payment_type = $this->_payment_types[Configuration::get('CERTISSIM_' . $id_payment_module . '_PAYMENT_TYPE', null, null, $order->id_shop)]; } else { $payment_type = $this->_payment_types[Configuration::get('CERTISSIM_' . $id_payment_module . '_PAYMENT_TYPE')]; } $xml_element_payment = new CertissimPaiement($payment_type); //initialization of the element <stack> $stack = new CertissimXMLElement("<stack></stack>"); //agregates each elements in a main stream $xml_element_invoice_customer->childSiteconso($xml_element_invoice_customer_stats); $xml_element_control->childUtilisateur($xml_element_invoice_customer); $xml_element_control->childAdresse($xml_element_invoice_address); if (isset($xml_element_delivery_customer)) { $xml_element_control->childUtilisateur($xml_element_delivery_customer); } if (isset($xml_element_delivery_address)) { $xml_element_control->childAdresse($xml_element_delivery_address); } $xml_element_order_details->childTransport($xml_element_carrier); $xml_element_order_details->childList($xml_element_products_list); $xml_element_control->childInfocommande($xml_element_order_details); $xml_element_control->childPaiement($xml_element_payment); //add CDATA sections to protect against encoding issues $xml_element_control->addCdataSections(); //add the <control> element into <stack> $stack->childControl($xml_element_control); CertissimLogger::insertLog(__METHOD__ . ' : ' . __LINE__, "---- flux généré pour commande {$id_order} ----"); CertissimLogger::insertLog(__METHOD__ . ' : ' . __LINE__, $xml_element_control->getXML()); CertissimLogger::insertLog(__METHOD__ . ' : ' . __LINE__, "---------------------------------------"); return $stack; }