/**
  * Transfer cartridges of a printer
  *
  * @param $ID original ID of the printer
  * @param $newID new ID of the printer
  **/
 function transferPrinterCartridges($ID, $newID)
 {
     global $DB;
     // Get cartrdiges linked
     $query = "SELECT *\n                FROM `glpi_cartridges`\n                WHERE `glpi_cartridges`.`printers_id` = '{$ID}'";
     if ($result = $DB->query($query)) {
         if ($DB->numrows($result) > 0) {
             $cart = new Cartridge();
             $carttype = new CartridgeItem();
             while ($data = $DB->fetch_array($result)) {
                 $need_clean_process = false;
                 // Foreach cartridges
                 // if keep
                 if ($this->options['keep_cartridgeitem']) {
                     $newcartID = -1;
                     $newcarttypeID = -1;
                     // 1 - Search carttype destination ?
                     // Already transfer carttype :
                     if (isset($this->already_transfer['CartridgeItem'][$data['cartridgeitems_id']])) {
                         $newcarttypeID = $this->already_transfer['CartridgeItem'][$data['cartridgeitems_id']];
                     } else {
                         // Not already transfer cartype
                         $query = "SELECT count(*) AS CPT\n                               FROM `glpi_cartridges`\n                               WHERE `glpi_cartridges`.`cartridgeitems_id`\n                                          = '" . $data['cartridgeitems_id'] . "'\n                                     AND `glpi_cartridges`.`printers_id` > '0'\n                                     AND `glpi_cartridges`.`printers_id`\n                                          NOT IN " . $this->item_search['Printer'];
                         $result_search = $DB->query($query);
                         // Is the carttype will be completly transfer ?
                         if ($DB->result($result_search, 0, 'CPT') == 0) {
                             // Yes : transfer
                             $need_clean_process = false;
                             $this->transferItem('CartridgeItem', $data['cartridgeitems_id'], $data['cartridgeitems_id']);
                             $newcarttypeID = $data['cartridgeitems_id'];
                         } else {
                             // No : copy carttype
                             $need_clean_process = true;
                             $carttype->getFromDB($data['cartridgeitems_id']);
                             // Is existing carttype in the destination entity ?
                             $query = "SELECT *\n                                  FROM `glpi_cartridgeitems`\n                                  WHERE `entities_id` = '" . $this->to . "'\n                                        AND `name` = '" . addslashes($carttype->fields['name']) . "'";
                             if ($result_search = $DB->query($query)) {
                                 if ($DB->numrows($result_search) > 0) {
                                     $newcarttypeID = $DB->result($result_search, 0, 'id');
                                 }
                             }
                             // Not found -> transfer copy
                             if ($newcarttypeID < 0) {
                                 // 1 - create new item
                                 unset($carttype->fields['id']);
                                 $input = $carttype->fields;
                                 $input['entities_id'] = $this->to;
                                 unset($carttype->fields);
                                 $newcarttypeID = $carttype->add($input);
                                 // 2 - transfer as copy
                                 $this->transferItem('CartridgeItem', $data['cartridgeitems_id'], $newcarttypeID);
                             }
                             // Founded -> use to link : nothing to do
                         }
                     }
                     // Update cartridge if needed
                     if ($newcarttypeID > 0 && $newcarttypeID != $data['cartridgeitems_id']) {
                         $cart->update(array('id' => $data['id'], 'cartridgeitems_id' => $newcarttypeID));
                     }
                 } else {
                     // Do not keep
                     // If same printer : delete cartridges
                     if ($ID == $newID) {
                         $del_query = "DELETE\n                                   FROM `glpi_cartridges`\n                                   WHERE `printers_id` = '{$ID}'";
                         $DB->query($del_query);
                     }
                     $need_clean_process = true;
                 }
                 // CLean process
                 if ($need_clean_process && $this->options['clean_cartridgeitem']) {
                     // Clean carttype
                     $query2 = "SELECT COUNT(*) AS CPT\n                             FROM `glpi_cartridges`\n                             WHERE `cartridgeitems_id` = '" . $data['cartridgeitems_id'] . "'";
                     $result2 = $DB->query($query2);
                     if ($DB->result($result2, 0, 'CPT') == 0) {
                         if ($this->options['clean_cartridgeitem'] == 1) {
                             // delete
                             $carttype->delete(array('id' => $data['cartridgeitems_id']));
                         }
                         if ($this->options['clean_cartridgeitem'] == 2) {
                             // purge
                             $carttype->delete(array('id' => $data['cartridgeitems_id']), 1);
                         }
                     }
                 }
             }
         }
     }
 }
define('GLPI_ROOT', '..');
include GLPI_ROOT . "/inc/includes.php";
if (!isset($_GET["id"])) {
    $_GET["id"] = "";
}
$cartype = new CartridgeItem();
if (isset($_POST["add"])) {
    $cartype->check(-1, 'w', $_POST);
    if ($newID = $cartype->add($_POST)) {
        Event::log($newID, "cartridges", 4, "inventory", $_SESSION["glpiname"] . " " . $LANG['log'][20] . " " . $_POST["name"] . ".");
    }
    glpi_header($_SERVER['HTTP_REFERER']);
} else {
    if (isset($_POST["delete"])) {
        $cartype->check($_POST["id"], 'w');
        if ($cartype->delete($_POST)) {
            Event::log($_POST["id"], "cartridges", 4, "inventory", $_SESSION["glpiname"] . " " . $LANG['log'][22]);
        }
        $cartype->redirectToList();
    } else {
        if (isset($_POST["restore"])) {
            $cartype->check($_POST["id"], 'w');
            if ($cartype->restore($_POST)) {
                Event::log($_POST["id"], "cartridges", 4, "inventory", $_SESSION["glpiname"] . " " . $LANG['log'][23]);
            }
            $cartype->redirectToList();
        } else {
            if (isset($_POST["purge"])) {
                $cartype->check($_POST["id"], 'w');
                if ($cartype->delete($_POST, 1)) {
                    Event::log($_POST["id"], "cartridges", 4, "inventory", $_SESSION["glpiname"] . " " . $LANG['log'][24]);