/**
  * adds the element <crypt> into the stream if it has no element <crypt> already, do nothing otherwise
  *
  * @param XMLElement $order 
  */
 public function addCrypt(SceauControl &$order)
 {
     $crypt = $order->getOneElementByTagName('crypt');
     if (is_null($crypt)) {
         $order->createChild('crypt', $this->generateCrypt($order), array());
     } else {
         $crypt->nodeValue = $this->generateCrypt($order);
     }
 }
 /**
  * build xml with customer's and order's data et send it to FIA-NET
  * 
  * @param int $id_order
  * @return boolean 
  */
 public function sendXML($id_order)
 {
     $order = new Order($id_order);
     $montant = $order->total_paid;
     $date = $order->date_add;
     $customer = new Customer($order->id_customer);
     $lastname = $customer->lastname;
     $firstname = $customer->firstname;
     $id_currency = $order->id_currency;
     $currency = new Currency($id_currency);
     if (_PS_VERSION_ < '1.5') {
         $civility = $customer->id_gender;
         $accepted_civility = array("1", "2", "3");
         if (!in_array($civility, $accepted_civility)) {
             $civility = '1';
         }
     } else {
         $gender = new Gender($customer->id_gender);
         $id_lang = Language::getIdByIso($this->context->language->iso_code);
         $civility = $gender->name[$id_lang] == 'Mr.' ? 1 : 2;
     }
     //xml construction
     if (_PS_VERSION_ < '1.5') {
         $fianetsceau = new Sceau();
     } else {
         $fianetsceau = new Sceau((int) $order->id_shop);
     }
     $utilisateur = new SceauXMLElement('<utilisateur></utilisateur>');
     $nom = $utilisateur->childNom($customer->lastname);
     $nom->addAttribute('titre', $civility);
     $utilisateur->childPrenom($customer->firstname);
     $utilisateur->childEmail(strtolower($customer->email));
     $infocommande = new SceauXMLElement('<infocommande></infocommande>');
     $infocommande->childSiteid($fianetsceau->getSiteid());
     $infocommande->childRefid($id_order);
     $montant = $infocommande->childMontant($order->total_paid);
     $montant->addAttribute('devise', $currency->iso_code);
     $ip = new SceauXMLElement('<ip>' . $this->getCustomerIP($id_order) . '</ip>');
     $ip->addAttribute('timestamp', $order->date_add);
     $infocommande->childIp($ip);
     $paiement = new SceauXMLElement('<paiement></paiement>');
     $paiement->childType($this->getPaymentFianetType($id_order));
     $lang = Language::getIsoById($order->id_lang);
     $langue = $infocommande->childLangue($lang);
     $xml_order = new SceauControl();
     $xml_order->childUtilisateur($utilisateur);
     $xml_order->childInfocommande($infocommande);
     $xml_order->childPaiement($paiement);
     $fianetsceau->addCrypt($xml_order);
     $result = $fianetsceau->sendSendrating($xml_order);
     if (!($result === false)) {
         $resxml = new SceauXMLElement($result);
         if ($resxml->getAttribute('type') != "OK") {
             //update fianetsceau_state 3:error
             $this->updateOrder($id_order, array('id_fianetsceau_state' => '3'));
             SceauLogger::insertLogSceau(__METHOD__ . " : " . __LINE__, 'Order ' . $id_order . ' XML Send error : ' . $resxml->getChildByName('detail')->getValue());
             return false;
         } else {
             //update fianetsceau_state 2:sent
             $this->updateOrder($id_order, array('id_fianetsceau_state' => '2'));
         }
         return true;
     } else {
         //update fianetsceau_state 3:error
         $this->updateOrder($id_order, array('id_fianetsceau_state' => '3'));
         SceauLogger::insertLogSceau(__METHOD__ . " : " . __LINE__, 'Order ' . $id_order . ' XML Send error : ' . $resxml->getChildByName('detail')->getValue());
         return false;
     }
 }
 /**
  * build xml with customer's and order's data et send it to FIA-NET
  * 
  * @param int $id_order
  * @return boolean 
  */
 public function sendXML($id_order)
 {
     $order = new Order($id_order);
     $montant = $order->total_paid;
     $date = $order->date_add;
     $customer = new Customer($order->id_customer);
     $lastname = $customer->lastname;
     $firstname = $customer->firstname;
     $id_currency = $order->id_currency;
     $currency = new Currency($id_currency);
     if (_PS_VERSION_ < '1.5') {
         $civility = $customer->id_gender;
         $accepted_civility = array("1", "2", "3");
         if (!in_array($civility, $accepted_civility)) {
             $civility = '1';
         }
         $id_shop = 1;
         $fianetsceau = new Sceau();
     } else {
         $gender = new Gender($customer->id_gender);
         $id_lang = Language::getIdByIso($this->context->language->iso_code);
         $civility = $gender->name[$id_lang] == 'Mr.' ? 1 : 2;
         $id_shop = (int) $order->id_shop;
         $fianetsceau = new Sceau($id_shop);
     }
     $lang = Language::getIsoById($order->id_lang) == 'fr' ? 'fr' : 'uk';
     $sceaucontrol = new SceauControl();
     $sceaucontrol->createCustomer('', $civility, $customer->lastname, $customer->firstname, strtolower($customer->email));
     $sceaucontrol->createOrderDetails($id_order, $fianetsceau->getSiteid(), $order->total_paid, 'EUR', $this->getCustomerIP($id_order), $order->date_add, $lang);
     //get default FIA-NET category
     $default_product_type = $this->getFianetSubCategoryId(0, $id_shop);
     $products = $order->getProducts();
     $productsceau = $sceaucontrol->createOrderProducts();
     foreach ($products as $product) {
         $product_categories = Product::getProductCategories((int) $product['product_id']);
         $product_category = array_pop($product_categories);
         $prod = new Product($product['product_id']);
         //gets the product FIA-NET category
         $product_type = $this->getFianetSubCategoryId($product_category, $id_shop);
         if (!empty($prod->ean13) && ($prod->ean13 != 0 || $prod->ean13 != '')) {
             $codeean = $prod->ean13;
         } else {
             $codeean = null;
         }
         $reference = (isset($prod->reference) and !empty($prod->reference)) ? $prod->reference : $product['product_id'];
         if ($product_type) {
             //if a FIA-NET category is set: the type attribute takes the product FIA-NET type value
             $fianet_type = $product_type;
         } else {
             //if FIA-NET category not set: the type attribute takes the default value
             $fianet_type = $default_product_type;
         }
         //get product image
         $images = Product::getCover($product['product_id']);
         if (!empty($images)) {
             $image = new Image($images['id_image']);
             $image_url = _PS_BASE_URL_ . _THEME_PROD_DIR_ . $image->getExistingImgPath() . "." . $image->image_format;
         }
         $productsceau->createProduct($codeean, str_replace("'", "", $reference), $fianet_type, str_replace("'", "", $product['product_name']), (string) $product['product_price_wt'], $image_url);
     }
     $sceaucontrol->createPayment($this->getPaymentFianetType($id_order));
     $fianetsceau->addCrypt($sceaucontrol);
     $result = $fianetsceau->sendSendrating($sceaucontrol);
     if (isXMLstringSceau($result)) {
         $resxml = new SceauSendratingResponse($result);
         if ($resxml->isValid()) {
             //update fianetsceau_state 2:sent
             $this->updateOrder($id_order, array('id_fianetsceau_state' => '2'));
             SceauLogger::insertLogSceau(__METHOD__ . " : " . __LINE__, 'Order ' . $id_order . ' sended');
             return true;
         } else {
             //update fianetsceau_state 3:error
             $this->updateOrder($id_order, array('id_fianetsceau_state' => '3'));
             SceauLogger::insertLogSceau(__METHOD__ . " : " . __LINE__, 'Order ' . $id_order . ' XML send error : ' . $resxml->getDetail());
             return false;
         }
     } else {
         //update fianetsceau_state 3:error
         $this->updateOrder($id_order, array('id_fianetsceau_state' => '3'));
         SceauLogger::insertLogSceau(__METHOD__ . " : " . __LINE__, 'Order ' . $id_order . ' XML send error : ' . $resxml->getDetail());
         return false;
     }
 }