/**
  *
  * Update config of a new software office
  *
  * This function create a officepack in GLPI with some general data.
  *
  * @param type $computers_id
  * @param $entity
  * @param type $ocsComputer
  * @param type $cfg_ocs
  *
  * @internal param \type $ocsservers_id
  */
 static function updateOfficePack($computers_id, $entity, $ocsComputer, $cfg_ocs)
 {
     global $DB;
     $software = new Software();
     $ocsOfficePacks = $ocsComputer;
     // Read imported software in last sync
     $query = "SELECT `glpi_computers_softwarelicenses`.`id` as id,\n                          `glpi_softwares`.`name` as sname,\n                          `glpi_softwarelicenses`.`name` as lname,\n                          `glpi_softwareversions`.`name` as vname\n                   FROM `glpi_computers_softwarelicenses`\n                   INNER JOIN `glpi_softwarelicenses`\n                           ON `glpi_softwarelicenses`.`id`= `glpi_computers_softwarelicenses`.`softwarelicenses_id`\n                   INNER JOIN `glpi_softwares`\n                           ON `glpi_softwares`.`id`= `glpi_softwarelicenses`.`softwares_id`\n                   INNER JOIN `glpi_softwareversions`\n                           ON `glpi_softwarelicenses`.`softwareversions_id_use` = `glpi_softwareversions`.`id`\n                   WHERE `glpi_computers_softwarelicenses`.`computers_id`='{$computers_id}'\n                         AND `is_dynamic`";
     $imported = array();
     foreach ($DB->request($query) as $data) {
         $imported[$data['id']] = strtolower($data['vname']);
     }
     if (count($ocsOfficePacks) > 0) {
         foreach ($ocsOfficePacks as $ocsOfficePack) {
             $ocsOfficePack = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($ocsOfficePack));
             $query1 = "SELECT `glpi_softwares`.`id` AS softwares_id,\n                              `glpi_softwareversions`.`id` AS softwareversions_id,\n                              `glpi_softwareversions`.`name` AS softwareversions_name\n                     FROM `glpi_softwares`\n                     LEFT JOIN `glpi_softwareversions` ON `glpi_softwareversions`.`softwares_id` = `glpi_softwares`.`id`\n                     LEFT JOIN `glpi_computers_softwareversions` ON `glpi_computers_softwareversions`.softwareversions_id = `glpi_softwareversions`.`id`\n                     WHERE `glpi_computers_softwareversions`.`computers_id` = '" . $computers_id . "'\n                      AND `is_dynamic` \n                      AND `glpi_softwares`.`name` = '" . $ocsOfficePack['PRODUCT'] . "'\n                      AND `glpi_softwareversions`.`name` = '" . $ocsOfficePack['OFFICEVERSION'] . "'";
             $result = $DB->query($query1);
             if ($DB->numrows($result) > 0) {
                 $softwares_id = $DB->result($result, 0, 'softwares_id');
                 $softwareversions_id = $DB->result($result, 0, 'softwareversions_id');
                 $soft_l['softwares_id'] = $softwares_id;
                 $soft_l['softwareversions_id_use'] = $softwareversions_id;
                 $soft_l['entities_id'] = $entity;
                 $soft_l['name'] = $ocsOfficePack['OFFICEKEY'];
                 $soft_l['serial'] = $ocsOfficePack['OFFICEKEY'];
                 $soft_l['comment'] = $ocsOfficePack['NOTE'];
                 $id = array_search(strtolower(stripslashes($ocsOfficePack['OFFICEVERSION'])), $imported);
                 $software_licenses = new SoftwareLicense();
                 $computer_softwarelicenses = new Computer_SoftwareLicense();
                 if ($id) {
                     //-------------------------------------------------------------------------//
                     //---- The software exists in this license for this computer --------------//
                     //---------------------------- Update comments ----------------------------//
                     //---------------------------------------------------- --------------------//
                     if ($software_licenses->getFromDBByQuery("WHERE `softwares_id` = " . $softwares_id . " \n                                                         AND `serial` = '" . $ocsOfficePack['OFFICEKEY'] . "'\n                                                         AND `softwareversions_id_use` = " . $softwareversions_id)) {
                         $software_licenses->update(array('id' => $software_licenses->getID(), 'comment' => $ocsOfficePack['NOTE']));
                         if (!$computer_softwarelicenses->getFromDBByQuery("WHERE `computers_id` = " . $computers_id . "\n                           AND `softwarelicenses_id` = " . $software_licenses->getID())) {
                             $computer_soft_l['computers_id'] = $computers_id;
                             $computer_soft_l['softwarelicenses_id'] = $software_licenses->getID();
                             $computer_soft_l['is_dynamic'] = -1;
                             $computer_softwarelicenses->add($computer_soft_l);
                         }
                     }
                     unset($imported[$id]);
                 } else {
                     //------------------------------------------------------------------------//
                     //---- The software doesn't exists in this license for this computer -----//
                     //------------------------------------------------------------------------//
                     if ($software_licenses->getFromDBByQuery("WHERE `softwares_id` = " . $softwares_id . " \n                                                         AND `serial` = '" . $ocsOfficePack['OFFICEKEY'] . "'\n                                                         AND `softwareversions_id_use` = " . $softwareversions_id)) {
                         $id_software_licenses = $software_licenses->getID();
                     } else {
                         $software_licenses->fields['softwares_id'] = $softwares_id;
                         $id_software_licenses = $software_licenses->add($soft_l, array(), $cfg_ocs['history_software']);
                     }
                     if ($id_software_licenses) {
                         $computer_soft_l['computers_id'] = $computers_id;
                         $computer_soft_l['softwarelicenses_id'] = $id_software_licenses;
                         $computer_soft_l['is_dynamic'] = 1;
                         $computer_soft_l['number'] = -1;
                         $computer_softwarelicenses->add($computer_soft_l);
                     }
                 }
             }
         }
     }
     foreach ($imported as $id => $unused) {
         $computer_softwarelicenses->delete(array('id' => $id), true, $cfg_ocs['history_software']);
         // delete cause a getFromDB, so fields contains values
         $verid = $computer_softwarelicenses->getField('softwareversions_id');
         if (countElementsInTable('glpi_computers_softwarelicenses', "softwarelicenses_id = '{$verid}'") == 0) {
             $vers = new SoftwareVersion();
             if ($vers->getFromDB($verid) && countElementsInTable('glpi_softwarelicenses', "softwares_id = '" . $vers->fields['softwares_id'] . "'") == 0) {
                 $soft = new Software();
                 $soft->delete(array('id' => $vers->fields['softwares_id']), 1);
             }
             $vers->delete(array("id" => $verid));
         }
     }
 }