<?php
/*
 * @version $Id: bill.tabs.php 530 2011-06-30 11:30:17Z walid $
 LICENSE

 This file is part of the order plugin.

 Order plugin is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Order plugin is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GLPI; along with Behaviors. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 @package   order
 @author    the order plugin team
 @copyright Copyright (c) 2010-2011 Order plugin team
 @license   GPLv2+
            http://www.gnu.org/licenses/gpl.txt
 @link      https://forge.indepnet.net/projects/order
 @link      http://www.glpi-project.org/
 @since     2009
 ---------------------------------------------------------------------- */

define('GLPI_ROOT', '../../..');
include (GLPI_ROOT . "/inc/includes.php");

if(!isset($_GET["id"])) {
   $_GET["id"] = "";
}
if(!isset($_GET["withtemplate"])) {
   $_GET["withtemplate"] = "";
}

$reception  = new PluginOrderReception();
$link       = new PluginOrderLink();
$order_item = new PluginOrderOrder_Item();
$plugin     = new Plugin();

if (isset ($_POST["generation"])) {
   if (isset ($_POST["item"])) {
      foreach ($_POST["item"] as $key => $val) {
         if ($val == 1) {
            $order_item->getFromDB($_POST["id"][$key]);
            if ($order_item->fields["states_id"] == 
                  PluginOrderOrder::ORDER_DEVICE_NOT_DELIVRED) {
               Session::addMessageAfterRedirect($LANG['plugin_order'][45], true, ERROR);
               Html::back();
            }
         }
      }
   }

   if (isset ($_POST["item"])) {
      Html::header($LANG['plugin_order']['title'][1], '', "plugins", "order", "order");
      $link->showItemGenerationForm($_POST);
      Html::footer();
      
   } else {
      Session::addMessageAfterRedirect($LANG['plugin_order']['detail'][29], false, ERROR);
      Html::back();
   }
} elseif (isset ($_POST["generate"])) {
   $link->generateNewItem($_POST);
   Html::redirect(Toolbox::getItemTypeFormURL('PluginOrderOrder')."?id=" . 
                                    $_POST["plugin_order_orders_id"] . "");
} elseif (isset ($_POST["deleteLinkWithItem"])) {
   foreach ($_POST["item"] as $key => $val) {
      if ($val == 1)
         $link->deleteLinkWithItem($key, $_POST["itemtype"][$key],
                                              $_POST["plugin_order_orders_id"]);
   }
   Html::redirect(Toolbox::getItemTypeFormURL('PluginOrderOrder')."?id=" . 
                                     $_POST["plugin_order_orders_id"] . "");
} elseif (isset ($_POST["createLinkWithItem"])) {
   if ($_POST["item"]) {
      $i    = 0;
      $doit = 1;
      if ($_POST["itemtype"] != 'SoftwareLicense'
         && $_POST["itemtype"] != 'ConsumableItem'
             && $_POST["itemtype"] != 'CartridgeItem') {
         if (count($_POST["item"]) > 1) {
            $doit = 0;
         }
      }

      if ($doit) {
         foreach ($_POST["item"] as $key => $val) {
            if ($val == 1) {
               $order_item->getFromDB($_POST["id"][$key]);
               if ($order_item->fields["states_id"] == 
                     PluginOrderOrder::ORDER_DEVICE_NOT_DELIVRED) {
                  Session::addMessageAfterRedirect($LANG['plugin_order'][46], true, ERROR);
                  Html::back();
               } else {
                  $link->createLinkWithItem($key, $_POST["items_id"], 
                                            $_POST["itemtype"], 
                                            $_POST["plugin_order_orders_id"]);
               }
            }
         }
      } else {
         Session::addMessageAfterRedirect($LANG['plugin_order'][42], true, ERROR);
      }
   }
   Html::redirect(Toolbox::getItemTypeFormURL('PluginOrderOrder')."?id=" . 
                                    $_POST["plugin_order_orders_id"] . "");
   
}

?>