| 46 | rodolico | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 |    define(VERSION,'1.0.0');
 | 
        
           |  |  | 4 |    define(BUILD_DATE,'20170709');
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 |    include_once("database.php");
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 |    include_once("library.php");
 | 
        
           |  |  | 9 |    include_once('reports.php');
 | 
        
           |  |  | 10 |   | 
        
           |  |  | 11 |    define (SQL_SHOW_LICENSE,
 | 
        
           |  |  | 12 |       "select 
 | 
        
           |  |  | 13 |                   client.name 'Client',
 | 
        
           |  |  | 14 |                   concat('<a href=\"index.html?action=edit&license_id=',license.license_id,'\">', license.license,'</a>') 'License',
 | 
        
           |  |  | 15 |                   device.name 'Installed On',
 | 
        
           |  |  | 16 |                   license_product.name 'Product',
 | 
        
           |  |  | 17 |                   license.added_date 'Installed',
 | 
        
           |  |  | 18 |                   concat('<a href=\"index.html?action=move&license_id=',license.license_id,'\">', 'Move','</a>') 'Action'
 | 
        
           |  |  | 19 |        from
 | 
        
           |  |  | 20 |                   license join license_product using (license_product_id)
 | 
        
           |  |  | 21 |                   left outer join device using (device_id)
 | 
        
           |  |  | 22 |                   join client using ( client_id )
 | 
        
           |  |  | 23 |        where <whereClause>
 | 
        
           |  |  | 24 |        order by 
 | 
        
           |  |  | 25 |           client.name,
 | 
        
           |  |  | 26 |           device.name,
 | 
        
           |  |  | 27 |           license_product.name,
 | 
        
           |  |  | 28 |           license.license"
 | 
        
           |  |  | 29 |       );
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |    define (SQL_SHOW_LICENSE_HISTORY,
 | 
        
           |  |  | 32 |       "select 
 | 
        
           |  |  | 33 |                   client.name 'Client',
 | 
        
           |  |  | 34 |                   concat('<a href=\"index.html?license_id=',license.license_id,'\">', license.license,'</a>') 'License',
 | 
        
           |  |  | 35 |                   device.name 'Installed On',
 | 
        
           |  |  | 36 |                   license_product.name 'Product',
 | 
        
           |  |  | 37 |                   license.added_date 'Installed',
 | 
        
           |  |  | 38 |                   license.removed_date 'Removed'
 | 
        
           |  |  | 39 |        from
 | 
        
           |  |  | 40 |                   license join license_product using (license_product_id)
 | 
        
           |  |  | 41 |                   left outer join device using (device_id)
 | 
        
           |  |  | 42 |                   join client using ( client_id )
 | 
        
           |  |  | 43 |        where <whereClause>
 | 
        
           |  |  | 44 |        order by 
 | 
        
           |  |  | 45 |           client.name,
 | 
        
           |  |  | 46 |           device.name,
 | 
        
           |  |  | 47 |           license_product.name,
 | 
        
           |  |  | 48 |           license.license"
 | 
        
           |  |  | 49 |         );
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | /*
 | 
        
           |  |  | 52 |  * Adds/updates license in license table
 | 
        
           |  |  | 53 |  * if already licensed for this machine, returns 'Already Set'
 | 
        
           |  |  | 54 |  * if license exists and is for a different machine
 | 
        
           |  |  | 55 |  *    mark it as removed
 | 
        
           |  |  | 56 |  *    adds key to new machine
 | 
        
           |  |  | 57 |  *    returns "Assigned"
 | 
        
           |  |  | 58 |  * Othewise, Adds key and assigns to machine
 | 
        
           |  |  | 59 |  * returns "Added"
 | 
        
           |  |  | 60 |  * NOTE: $device_id may be null which indicates license owned by client but unassigned
 | 
        
           |  |  | 61 |  */
 | 
        
           |  |  | 62 | function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
 | 
        
           |  |  | 63 |    // see if the entry already exists
 | 
        
           |  |  | 64 |    $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" );
 | 
        
           |  |  | 65 |    //print "<pre>"; print_r( $results ); print "</pre>"; die;
 | 
        
           |  |  | 66 |    $db_license_id = $results['data'][0]['license_id'];
 | 
        
           |  |  | 67 |    $db_client_id = $results['data'][0]['client_id'];
 | 
        
           |  |  | 68 |    $db_device_id = $results['data'][0]['device_id'];
 | 
        
           |  |  | 69 |    // SQL does not understand an empty string, so we replace it with the keyword null for queries
 | 
        
           |  |  | 70 |    $queryDeviceID = $device_id ? $device_id : 'null';
 | 
        
           |  |  | 71 |    if ( ! $results ) { # this was not found, so just add it
 | 
        
           |  |  | 72 |       doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
 | 
        
           |  |  | 73 |       return "Added";
 | 
        
           |  |  | 74 |    }
 | 
        
           |  |  | 75 |    if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id  ) { // already done, so just leave alone
 | 
        
           |  |  | 76 |       return "Already Set";
 | 
        
           |  |  | 77 |    }
 | 
        
           |  |  | 78 |    if ( ! $db_device_id ) { # key was not assigned before, so just assign it
 | 
        
           |  |  | 79 |       doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
 | 
        
           |  |  | 80 |       return "Assigned";
 | 
        
           |  |  | 81 |    }
 | 
        
           |  |  | 82 |    // 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
 | 
        
           |  |  | 83 |    doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
 | 
        
           |  |  | 84 |    doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
 | 
        
           |  |  | 85 |    return "Reassigned";
 | 
        
           |  |  | 86 | }
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | /* 
 | 
        
           |  |  | 89 |  * select $column from $table where $match = '$value'
 | 
        
           |  |  | 90 |  * if $add is true, will add row if it does not exist.
 | 
        
           |  |  | 91 |  * returns $column from the result (or '' if it does not exist)
 | 
        
           |  |  | 92 |  * Used mainly to get an index from a table with matching value
 | 
        
           |  |  | 93 |  */
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 | function getValue ( $table, $column, $match, $value, $add = false ) {
 | 
        
           |  |  | 96 |    $return = getOneDBValue( "select $column from $table where $match = '$value'" );
 | 
        
           |  |  | 97 |    if ( $return === null ) {
 | 
        
           |  |  | 98 |       if ( $add ) {
 | 
        
           |  |  | 99 |          $return = doSQL( "insert into $table ( $match ) values ( '$value' )" );
 | 
        
           |  |  | 100 |          return $return['insert_id'];
 | 
        
           |  |  | 101 |       } else {
 | 
        
           |  |  | 102 |          $return = '';
 | 
        
           |  |  | 103 |       } // if..else
 | 
        
           |  |  | 104 |    } // if
 | 
        
           |  |  | 105 |    return $return;
 | 
        
           |  |  | 106 | } // function getValue
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | function getDescription( $license_id ) {
 | 
        
           |  |  | 109 |    $result = queryDatabaseExtended( 
 | 
        
           |  |  | 110 |            "select 
 | 
        
           |  |  | 111 |                license_product.name 'Product',
 | 
        
           |  |  | 112 |                license.license 'License',
 | 
        
           |  |  | 113 |                device.name 'Machine',
 | 
        
           |  |  | 114 |                client.name 'Client'
 | 
        
           |  |  | 115 |             from
 | 
        
           |  |  | 116 |                license join license_product using (license_product_id)
 | 
        
           |  |  | 117 |                left outer join device using (device_id)
 | 
        
           |  |  | 118 |                join client using ( client_id )
 | 
        
           |  |  | 119 |             where 
 | 
        
           |  |  | 120 |                license.removed_date is null
 | 
        
           |  |  | 121 |                and license.license_id = $license_id"
 | 
        
           |  |  | 122 |             );
 | 
        
           |  |  | 123 |    if ( $result['count'] ) {
 | 
        
           |  |  | 124 |       $return = 'Product: ' . $result['data'][0]['Product'] . "\n";
 | 
        
           |  |  | 125 |       $return .= 'Client:  ' . $result['data'][0]['Client'] . "\n";
 | 
        
           |  |  | 126 |       $return .= 'Machine:  ' . ( $result['data'][0]['Machine'] ? $result['data'][0]['Machine'] : 'Not Currently Assigned' ) . "\n";
 | 
        
           |  |  | 127 |       $return .= 'License:  ' . $result['data'][0]['License'] . "\n";
 | 
        
           |  |  | 128 |       return $return;
 | 
        
           |  |  | 129 |    } else {
 | 
        
           |  |  | 130 |       return "No License found for license_id $license_id";
 | 
        
           |  |  | 131 |    }
 | 
        
           |  |  | 132 | } // function getDescription
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 | ?>
 |