41 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
include_once( '../../header.php' );
|
|
|
4 |
include_once( 'csvImporter.php' );
|
|
|
5 |
|
|
|
6 |
$filename = 'machine_licenses.csv';
|
|
|
7 |
|
|
|
8 |
function getValue ( $table, $column, $match, $value, $add = false ) {
|
|
|
9 |
$return = getOneDBValue( "select $column from $table where $match = '$value'" );
|
|
|
10 |
if ( $return === null ) {
|
|
|
11 |
if ( $add ) {
|
|
|
12 |
$return = doSQL( "insert into $table ( $match ) values ( '$value' )" );
|
|
|
13 |
return $return['insert_id'];
|
|
|
14 |
} else {
|
|
|
15 |
$return = '';
|
|
|
16 |
}
|
|
|
17 |
}
|
|
|
18 |
return $return;
|
|
|
19 |
}
|
|
|
20 |
|
42 |
rodolico |
21 |
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
|
|
|
22 |
// see if the entry already exists
|
|
|
23 |
$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" );
|
|
|
24 |
//print "<pre>"; print_r( $results ); print "</pre>"; die;
|
|
|
25 |
$db_license_id = $results['data'][0]['license_id'];
|
|
|
26 |
$db_client_id = $results['data'][0]['client_id'];
|
|
|
27 |
$db_device_id = $results['data'][0]['device_id'];
|
|
|
28 |
$db_license_product_id = $results['data'][0]['license_product_id'];
|
|
|
29 |
if ( ! $results ) { # this was not found, so just add it
|
|
|
30 |
if ( $device_id === '' ) $device_id = 'null';
|
|
|
31 |
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
|
|
|
32 |
return "Added";
|
|
|
33 |
}
|
|
|
34 |
if ( $client_id == $db_client_id && $device_id == $db_device_id ) { // already done, so just leave alone
|
|
|
35 |
return "Already Set";
|
|
|
36 |
}
|
|
|
37 |
if ( ! $db_device_id ) { # key was not assigned before, so just assign it
|
|
|
38 |
doSQL( "update license set device_id = $db_device_id,added_date = now() where license_id = $db_license_id" );
|
|
|
39 |
return "Assigned";
|
|
|
40 |
}
|
|
|
41 |
// 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
|
|
|
42 |
doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
|
|
|
43 |
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
|
|
|
44 |
return "Reassigned";
|
|
|
45 |
}
|
41 |
rodolico |
46 |
|
42 |
rodolico |
47 |
|
41 |
rodolico |
48 |
$fileInfo = new CsvImporter( $filename, true );
|
|
|
49 |
while ( $line = $fileInfo->get(1) ) {
|
|
|
50 |
foreach ( $line as $index => $values ) {
|
|
|
51 |
$import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
|
|
|
52 |
}
|
|
|
53 |
};
|
42 |
rodolico |
54 |
|
41 |
rodolico |
55 |
//print '<pre>'; print_r( $import ); print '</pre>';
|
|
|
56 |
|
|
|
57 |
print "<table border='1'><tr></tr><th>Client</th><th>ID</th><th>Machine</th><th>ID</th><th>Product</th><th>ID</th><th>License</th></tr>\n";
|
|
|
58 |
foreach ( $import as $client => $data ) {
|
|
|
59 |
$client_id = getValue ( 'client', 'client_id', 'name', $client );
|
|
|
60 |
if ( $client_id === '' ) {
|
|
|
61 |
print "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
|
|
|
62 |
continue;
|
|
|
63 |
}
|
|
|
64 |
foreach ( $data as $machine => $info ) {
|
|
|
65 |
if ( $machine === '' ) {
|
|
|
66 |
$machine_id = '';
|
|
|
67 |
} else {
|
|
|
68 |
$machine_id = getValue( 'device', 'device_id', 'name', $machine );
|
|
|
69 |
if ( $machine_id === '' ) {
|
|
|
70 |
print "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
|
|
|
71 |
print "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
|
|
|
72 |
continue;
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
foreach ( $info as $product => $license ) {
|
|
|
76 |
$product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
|
42 |
rodolico |
77 |
$action = updateLicense( $client_id, $machine_id, $product_id, $license );
|
41 |
rodolico |
78 |
print "<tr>\n";
|
42 |
rodolico |
79 |
print "<td>$action</td><td>$client</td><td>$client_id</td>";
|
41 |
rodolico |
80 |
print "<td>$machine</td><td>$machine_id</td>";
|
|
|
81 |
print "<td>$product</td><td>$product_id</td><td>$license</td>\n";
|
|
|
82 |
print "</tr>\n";
|
42 |
rodolico |
83 |
|
41 |
rodolico |
84 |
}
|
|
|
85 |
} // for each machine
|
|
|
86 |
} // for each client
|
|
|
87 |
print "</table>\n";
|
|
|
88 |
|
|
|
89 |
?>
|