Esempio n. 1
0
 /**
  * Transfer contracts
  *
  * @param $itemtype original type of transfered item
  * @param $ID original ID of the contract
  * @param $newID new ID of the contract
  **/
 function transferContracts($itemtype, $ID, $newID)
 {
     global $DB;
     $need_clean_process = false;
     // if keep
     if ($this->options['keep_contract']) {
         $contract = new Contract();
         // Get contracts for the item
         $query = "SELECT *\n                   FROM `glpi_contracts_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'\n                         AND `contracts_id` NOT IN " . $this->item_recurs['Contract'];
         if ($result = $DB->query($query)) {
             if ($DB->numrows($result) > 0) {
                 // Foreach get item
                 while ($data = $DB->fetch_array($result)) {
                     $need_clean_process = false;
                     $item_ID = $data['contracts_id'];
                     $newcontractID = -1;
                     // is already transfer ?
                     if (isset($this->already_transfer['Contract'][$item_ID])) {
                         $newcontractID = $this->already_transfer['Contract'][$item_ID];
                         if ($newcontractID != $item_ID) {
                             $need_clean_process = true;
                         }
                     } else {
                         // No
                         // Can be transfer without copy ? = all linked items need to be transfer (so not copy)
                         $canbetransfer = true;
                         $query = "SELECT DISTINCT `itemtype`\n                               FROM `glpi_contracts_items`\n                               WHERE `contracts_id` = '{$item_ID}'";
                         if ($result_type = $DB->query($query)) {
                             if ($DB->numrows($result_type) > 0) {
                                 while (($data_type = $DB->fetch_array($result_type)) && $canbetransfer) {
                                     $dtype = $data_type['itemtype'];
                                     if (isset($this->item_search[$dtype])) {
                                         // No items to transfer -> exists links
                                         $query_search = "SELECT count(*) AS CPT\n                                                  FROM `glpi_contracts_items`\n                                                  WHERE `contracts_id` = '{$item_ID}'\n                                                        AND `itemtype` = '{$dtype}'\n                                                        AND `items_id`\n                                                             NOT IN " . $this->item_search[$dtype];
                                         $result_search = $DB->query($query_search);
                                         if ($DB->result($result_search, 0, 'CPT') > 0) {
                                             $canbetransfer = false;
                                         }
                                     } else {
                                         $canbetransfer = false;
                                     }
                                 }
                             }
                         }
                         // Yes : transfer
                         if ($canbetransfer) {
                             $this->transferItem('Contract', $item_ID, $item_ID);
                             $newcontractID = $item_ID;
                         } else {
                             $need_clean_process = true;
                             $contract->getFromDB($item_ID);
                             // No : search contract
                             $query = "SELECT *\n                                  FROM `glpi_contracts`\n                                  WHERE `entities_id` = '" . $this->to . "'\n                                        AND `name` = '" . addslashes($contract->fields['name']) . "'";
                             if ($result_search = $DB->query($query)) {
                                 if ($DB->numrows($result_search) > 0) {
                                     $newcontractID = $DB->result($result_search, 0, 'id');
                                     $this->addToAlreadyTransfer('Contract', $item_ID, $newcontractID);
                                 }
                             }
                             // found : use it
                             // not found : copy contract
                             if ($newcontractID < 0) {
                                 // 1 - create new item
                                 unset($contract->fields['id']);
                                 $input = $contract->fields;
                                 $input['entities_id'] = $this->to;
                                 unset($contract->fields);
                                 $newcontractID = $contract->add($input);
                                 // 2 - transfer as copy
                                 $this->transferItem('Contract', $item_ID, $newcontractID);
                             }
                         }
                     }
                     // Update links
                     if ($ID == $newID) {
                         if ($item_ID != $newcontractID) {
                             $query = "UPDATE `glpi_contracts_items`\n                                  SET `contracts_id` = '{$newcontractID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                             $DB->query($query);
                         }
                         // Same Item -> update links
                     } else {
                         // Copy Item -> copy links
                         if ($item_ID != $newcontractID) {
                             $query = "INSERT\n                                  INTO `glpi_contracts_items`\n                                  (`contracts_id`, `items_id`, `itemtype`)\n                                  VALUES ('{$newcontractID}', '{$newID}', '{$itemtype}')";
                             $DB->query($query);
                         } else {
                             // same contract for new item update link
                             $query = "UPDATE `glpi_contracts_items`\n                                  SET `items_id` = '{$newID}'\n                                  WHERE `id` = '" . $data['id'] . "'";
                             $DB->query($query);
                         }
                     }
                     // If clean and unused ->
                     if ($need_clean_process && $this->options['clean_contract']) {
                         $query = "SELECT COUNT(*) AS CPT\n                               FROM `glpi_contracts_items`\n                               WHERE `contracts_id` = '{$item_ID}'";
                         if ($result_remaining = $DB->query($query)) {
                             if ($DB->result($result_remaining, 0, 'CPT') == 0) {
                                 if ($this->options['clean_contract'] == 1) {
                                     $contract->delete(array('id' => $item_ID));
                                 }
                                 if ($this->options['clean_contract'] == 2) {
                                     // purge
                                     $contract->delete(array('id' => $item_ID), 1);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } else {
         // else unlink
         $query = "DELETE\n                   FROM `glpi_contracts_items`\n                   WHERE `items_id` = '{$ID}'\n                         AND `itemtype` = '{$itemtype}'";
         $DB->query($query);
     }
 }
Esempio n. 2
0
if (!isset($_GET["withtemplate"])) {
    $_GET["withtemplate"] = "";
}
$contract = new Contract();
$contractitem = new Contract_Item();
$contractsupplier = new Contract_Supplier();
if (isset($_POST["add"])) {
    $contract->check(-1, 'w', $_POST);
    if ($newID = $contract->add($_POST)) {
        Event::log($newID, "contracts", 4, "financial", sprintf(__('%1$s adds the item %2$s'), $_SESSION["glpiname"], $_POST["name"]));
    }
    Html::back();
} else {
    if (isset($_POST["delete"])) {
        $contract->check($_POST['id'], 'd');
        if ($contract->delete($_POST)) {
            Event::log($_POST["id"], "contracts", 4, "financial", sprintf(__('%s deletes an item'), $_SESSION["glpiname"]));
        }
        $contract->redirectToList();
    } else {
        if (isset($_POST["restore"])) {
            $contract->check($_POST['id'], 'd');
            if ($contract->restore($_POST)) {
                Event::log($_POST["id"], "contracts", 4, "financial", sprintf(__('%s restores an item'), $_SESSION["glpiname"]));
            }
            $contract->redirectToList();
        } else {
            if (isset($_POST["purge"])) {
                $contract->check($_POST['id'], 'd');
                if ($contract->delete($_POST, 1)) {
                    Event::log($_POST["id"], "contracts", 4, "financial", sprintf(__('%s purges an item'), $_SESSION["glpiname"]));