Пример #1
0
 public function generateOrder($params)
 {
     global $DB;
     $ID = $params['id'];
     $template = $params['template'];
     $signature = $params['sign'];
     if ($template) {
         $config = array('PATH_TO_TMP' => GLPI_DOC_DIR . '/_tmp');
         $odf = new Odtphp\Odf(PLUGIN_ORDER_TEMPLATE_DIR . "{$template}", $config);
         $this->getFromDB($ID);
         if (file_exists(PLUGIN_ORDER_TEMPLATE_CUSTOM_DIR . "custom.php")) {
             include_once PLUGIN_ORDER_TEMPLATE_CUSTOM_DIR . "custom.php";
         }
         if (function_exists("plugin_order_getCustomFieldsForODT")) {
             plugin_order_getCustomFieldsForODT($ID, $template, $odf, $signature);
         } else {
             $PluginOrderOrder_Item = new PluginOrderOrder_Item();
             $PluginOrderReference_Supplier = new PluginOrderReference_Supplier();
             try {
                 $odf->setImage('logo', PLUGIN_ORDER_TEMPLATE_LOGO_DIR . '/logo.jpg');
             } catch (\Odtphp\Exceptions\OdfException $e) {
             }
             $values = array();
             $values['title_order'] = __("Order number", "order");
             $values['num_order'] = $this->fields["num_order"];
             $values['title_invoice_address'] = __("Invoice address", "order");
             $values['comment_order'] = $this->fields["comment"];
             $entity = new Entity();
             $entity->getFromDB($this->fields["entities_id"]);
             $town = '';
             if ($this->fields["entities_id"] != 0) {
                 $name_entity = $entity->fields["name"];
             } else {
                 $name_entity = __("Root entity");
             }
             $values['entity_name'] = $name_entity;
             if ($entity->getFromDB($this->fields["entities_id"])) {
                 $town = $entity->fields["town"];
                 $values['entity_address'] = $entity->fields["address"];
                 $values['entity_postcode'] = $entity->fields["postcode"];
                 $values['entity_town'] = $entity->fields["town"];
                 $values['entity_country'] = $entity->fields["country"];
             }
             $supplier = new Supplier();
             if ($supplier->getFromDB($this->fields["suppliers_id"])) {
                 $values['supplier_name'] = $supplier->fields["name"];
                 $values['supplier_address'] = $supplier->fields["address"];
                 $values['supplier_postcode'] = $supplier->fields["postcode"];
                 $values['supplier_town'] = $supplier->fields["town"];
                 $values['supplier_country'] = $supplier->fields["country"];
             }
             $location = new Location();
             if ($location->getFromDB($this->fields["locations_id"])) {
                 $values['title_delivery_address'] = __("Delivery address", "order");
                 $values['comment_delivery_address'] = $location->fields['comment'];
             }
             if ($town) {
                 $town = $town . ", ";
             }
             $order_date = Html::convDate($this->fields["order_date"]);
             $username = Html::clean(getUserName(Session::getLoginUserID()));
             $values['title_date_order'] = $town . __("The", "order") . " ";
             $values['date_order'] = $order_date;
             $values['title_sender'] = __("Issuer order", "order");
             $values['sender'] = $username;
             $values['title_budget'] = __("Budget");
             $budget = new Budget();
             if ($budget->getFromDB($this->fields["budgets_id"])) {
                 $values['budget'] = $budget->fields['name'];
             } else {
                 $values['budget'] = '';
             }
             $output = '';
             $contact = new Contact();
             if ($contact->getFromDB($this->fields["contacts_id"])) {
                 $output = formatUserName($contact->fields["id"], "", $contact->fields["name"], $contact->fields["firstname"]);
             }
             $values['title_recipient'] = __("Recipient", "order");
             $values['recipient'] = Html::clean($output);
             $values['nb'] = __("Quantity", "order");
             $values['title_item'] = __("Designation", "order");
             $values['title_ref'] = __("Reference");
             $values['HTPrice_item'] = __("Unit price", "order");
             $values['TVA_item'] = __("VAT", "order");
             $values['title_discount'] = __("Discount rate", "order");
             $values['HTPriceTotal_item'] = __("Sum tax free", "order");
             $values['ATIPriceTotal_item'] = __("Price ATI", "order");
             $listeArticles = array();
             $result = $PluginOrderOrder_Item->queryDetail($ID);
             $num = $DB->numrows($result);
             while ($data = $DB->fetch_array($result)) {
                 $quantity = $PluginOrderOrder_Item->getTotalQuantityByRefAndDiscount($ID, $data["id"], $data["price_taxfree"], $data["discount"]);
                 $listeArticles[] = array('quantity' => $quantity, 'ref' => utf8_decode($data["name"]), 'taxe' => Dropdown::getDropdownName(getTableForItemType("PluginOrderOrderTax"), $data["plugin_order_ordertaxes_id"]), 'refnumber' => $PluginOrderReference_Supplier->getReferenceCodeByReferenceAndSupplier($data["id"], $this->fields["suppliers_id"]), 'price_taxfree' => $data["price_taxfree"], 'discount' => $data["discount"], false, 0, 'price_discounted' => $data["price_discounted"] * $quantity, 'price_ati' => $data["price_ati"]);
             }
             $article = $odf->setSegment('articles');
             foreach ($listeArticles as $element) {
                 $article->nbA($element['quantity']);
                 $article->titleArticle($element['ref']);
                 $article->refArticle($element['refnumber']);
                 $article->TVAArticle($element['taxe']);
                 $article->HTPriceArticle(Html::clean(Html::formatNumber($element['price_taxfree'])));
                 if ($element['discount'] != 0) {
                     $article->discount(Html::clean(Html::formatNumber($element['discount'])) . " %");
                 } else {
                     $article->discount("");
                 }
                 $article->HTPriceTotalArticle(Html::clean(Html::formatNumber($element['price_discounted'])));
                 $total_TTC_Article = $element['price_discounted'] * (1 + $element['taxe'] / 100);
                 $article->ATIPriceTotalArticle(Html::clean(Html::formatNumber($total_TTC_Article)));
                 $article->merge();
             }
             $odf->mergeSegment($article);
             $prices = $PluginOrderOrder_Item->getAllPrices($ID);
             // total price (with postage)
             $postagewithTVA = $PluginOrderOrder_Item->getPricesATI($this->fields["port_price"], Dropdown::getDropdownName("glpi_plugin_order_ordertaxes", $this->fields["plugin_order_ordertaxes_id"]));
             $total_HT = $prices["priceHT"] + $this->fields["port_price"];
             $total_TVA = $prices["priceTVA"] + $postagewithTVA - $this->fields["port_price"];
             $total_TTC = $prices["priceTTC"] + $postagewithTVA;
             if ($signature) {
                 try {
                     $odf->setImage('sign', PLUGIN_ORDER_SIGNATURE_DIR . $signature);
                 } catch (\Odtphp\Exceptions\OdfException $e) {
                 }
             } else {
                 try {
                     $odf->setImage('sign', '../pics/nothing.gif');
                 } catch (\Odtphp\Exceptions\OdfException $e) {
                 }
             }
             $name = Dropdown::getDropdownName("glpi_plugin_order_orderpayments", $this->fields["plugin_order_orderpayments_id"]);
             $values['title_totalht'] = __("Price tax free", "order");
             $values['totalht'] = Html::clean(Html::formatNumber($prices['priceHT']));
             $values['title_port'] = __("Price tax free with postage", "order");
             $values['totalht_port_price'] = Html::clean(Html::formatNumber($total_HT));
             $values['title_price_port'] = __("Postage", "order");
             $values['price_port_tva'] = " (" . Html::clean(Dropdown::getDropdownName("glpi_plugin_order_ordertaxes", $this->fields["plugin_order_ordertaxes_id"])) . "%)";
             $values['port_price'] = Html::clean(Html::formatNumber($postagewithTVA));
             $values['title_tva'] = __("VAT", "order");
             $values['totaltva'] = Html::clean(Html::formatNumber($total_TVA));
             $values['title_totalttc'] = __("Price ATI", "order");
             $values['totalttc'] = Html::clean(Html::formatNumber($total_TTC));
             $values['title_money'] = __("€", "order");
             $values['title_sign'] = __("Signature of issuing order", "order");
             $values['title_conditions'] = __("Payment conditions", "order");
             $values['payment_conditions'] = $name;
             // Set variables in odt template
             foreach ($values as $field => $val) {
                 try {
                     $odf->setVars($field, $val, true, 'UTF-8');
                 } catch (\Odtphp\Exceptions\OdfException $e) {
                 }
             }
         }
         $message = "_";
         if (Session::isMultiEntitiesMode()) {
             $entity = new Entity();
             $entity->getFromDB($this->fields['entities_id']);
             $message .= $entity->getName();
         }
         $message .= "_" . $this->fields['num_order'] . "_";
         $message .= Html::convDateTime($_SESSION['glpi_currenttime']);
         $message = str_replace(" ", "_", $message);
         $outputfile = str_replace(".odt", $message . ".odt", $template);
         // We export the file
         $odf->exportAsAttachedFile($outputfile);
     }
 }