Subversion Repositories computer_asset_manager_v1

Rev

Rev 47 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

   define(VERSION,'1.0.0');
   define(BUILD_DATE,'20170709');

   include_once("database.php");
   
   include_once("library.php");
   include_once('reports.php');
   
   define (SQL_SHOW_LICENSE,
      "select 
                  client.name 'Client',
                  concat('<a href=\"index.html?action=edit&license_id=',license.license_id,'\">', license.license,'</a>') 'License',
                  device.name 'Installed On',
                  license_product.name 'Product',
                  license.added_date 'Installed',
                  concat('<a href=\"index.html?action=move&license_id=',license.license_id,'\">', 'Move','</a>') 'Action'
       from
                  license join license_product using (license_product_id)
                  left outer join device using (device_id)
                  join client using ( client_id )
       where <whereClause>
       order by 
          client.name,
          device.name,
          license_product.name,
          license.license"
      );

   define (SQL_SHOW_LICENSE_HISTORY,
      "select 
                  client.name 'Client',
                  concat('<a href=\"index.html?license_id=',license.license_id,'\">', license.license,'</a>') 'License',
                  device.name 'Installed On',
                  license_product.name 'Product',
                  license.added_date 'Installed',
                  license.removed_date 'Removed'
       from
                  license join license_product using (license_product_id)
                  left outer join device using (device_id)
                  join client using ( client_id )
       where <whereClause>
       order by 
          client.name,
          device.name,
          license_product.name,
          license.license"
        );
        
/*
 * Adds/updates license in license table
 * if already licensed for this machine, returns 'Already Set'
 * if license exists and is for a different machine
 *    mark it as removed
 *    adds key to new machine
 *    returns "Assigned"
 * Othewise, Adds key and assigns to machine
 * returns "Added"
 * NOTE: $device_id may be null which indicates license owned by client but unassigned
 */
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
   // see if the entry already exists
   $results = queryDatabaseExtended( "select license_id,client_id,device_id from license where license_product_id = $license_product_id and license = '$license' and removed_date is null" );
   //print "<pre>"; print_r( $results ); print "</pre>"; die;
   $db_license_id = $results['data'][0]['license_id'];
   $db_client_id = $results['data'][0]['client_id'];
   $db_device_id = $results['data'][0]['device_id'];
   // SQL does not understand an empty string, so we replace it with the keyword null for queries
   $queryDeviceID = $device_id ? $device_id : 'null';
   if ( ! $results ) { # this was not found, so just add it
      doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
      return "Added";
   }
   if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id  ) { // already done, so just leave alone
      return "Already Set";
   }
   if ( ! $db_device_id ) { # key was not assigned before, so just assign it
      doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
      return "Assigned";
   }
   // at this point, there is already an entry, but it is for a different machine, so we need to update it, ie remove the old, add the new
   doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
   doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
   return "Reassigned";
}

/* 
 * select $column from $table where $match = '$value'
 * if $add is true, will add row if it does not exist.
 * returns $column from the result (or '' if it does not exist)
 * Used mainly to get an index from a table with matching value
 */

function getValue ( $table, $column, $match, $value, $add = false ) {
   $return = getOneDBValue( "select $column from $table where $match = '$value'" );
   if ( $return === null ) {
      if ( $add ) {
         $return = doSQL( "insert into $table ( $match ) values ( '$value' )" );
         return $return['insert_id'];
      } else {
         $return = '';
      } // if..else
   } // if
   return $return;
} // function getValue

function getDescription( $license_id ) {
   $result = queryDatabaseExtended( 
           "select 
               license_product.name 'Product',
               license.license 'License',
               device.name 'Machine',
               client.name 'Client'
            from
               license join license_product using (license_product_id)
               left outer join device using (device_id)
               join client using ( client_id )
            where 
               license.removed_date is null
               and license.license_id = $license_id"
            );
   if ( $result['count'] ) {
      $return = 'Product: ' . $result['data'][0]['Product'] . "\n";
      $return .= 'Client:  ' . $result['data'][0]['Client'] . "\n";
      $return .= 'Machine:  ' . ( $result['data'][0]['Machine'] ? $result['data'][0]['Machine'] : 'Not Currently Assigned' ) . "\n";
      $return .= 'License:  ' . $result['data'][0]['License'] . "\n";
      return $return;
   } else {
      return "No License found for license_id $license_id";
   }
} // function getDescription

?>