diff --git a/api/v1/get/cartests.php b/api/v1/get/cartests.php index a08e174..76aa80b 100644 --- a/api/v1/get/cartests.php +++ b/api/v1/get/cartests.php @@ -2,15 +2,37 @@ defined($security_key) or exit; //------------------------------------------ -// Products +// cartests //------------------------------------------ //Connect to DB $pdo = dbConnect($dbname); +//SoldTo is empty +if (empty($partner->soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';} + +//default whereclause +$whereclause = ''; + +switch ($permission) { + case '4': + $whereclause = ''; + break; + case '3': + $whereclause = ''; + break; + case '2': + $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search; + $whereclause = 'WHERE accounthierarchy like :condition '; + break; + default: + $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search.'___shipto___'.substr($partner->shipto, 0, strpos($partner->shipto, "-")).'%___location___'.substr($partner->location, 0, strpos($partner->location, "-")).'%'; + $whereclause = 'WHERE accounthierarchy like :condition '; + break; +} + //NEW ARRAY $criterias = []; -$whereclause = ''; $clause = ''; //Check for $_GET variables and build up clause diff --git a/api/v1/post/cartests.php b/api/v1/post/cartests.php index 3b5e984..c0723ff 100644 --- a/api/v1/post/cartests.php +++ b/api/v1/post/cartests.php @@ -24,6 +24,20 @@ $clause = ''; $clause_insert =''; $input_insert = ''; +//INCLUDE ACCOUNTHIERARCHY +$account = array( + "salesid"=>$partner->salesid, + "soldto"=>$partner->soldto, + "shipto"=>$partner->shipto, + "location"=>$partner->location, + "section"=>$partner->section +); + +// CREATE ACCOUNTHIERARCHY JSON FROM ACCOUNT ARRAY +$accounthierarchy = json_encode($account, JSON_UNESCAPED_UNICODE); +// add to post_content +$post_content['accounthierarchy'] = $accounthierarchy; + //ADD STANDARD PARAMETERS TO ARRAY BASED ON INSERT OR UPDATE if ($command == 'insert'){ $post_content['created'] = $date; diff --git a/api/v2/get/cartests.php b/api/v2/get/cartests.php new file mode 100644 index 0000000..2fbbdb4 --- /dev/null +++ b/api/v2/get/cartests.php @@ -0,0 +1,149 @@ +soldto) || $partner->soldto == ''){$soldto_search = '%';} else {$soldto_search = '-%';} + +//default whereclause +$whereclause = ''; + +switch ($permission) { + case '4': + $whereclause = ''; + break; + case '3': + $whereclause = ''; + break; + case '2': + $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search; + $whereclause = 'WHERE accounthierarchy like :condition '; + break; + default: + $condition = '__salesid___'.$partner->salesid.'___soldto___'.substr($partner->soldto, 0, strpos($partner->soldto, "-")).$soldto_search.'___shipto___'.substr($partner->shipto, 0, strpos($partner->shipto, "-")).'%___location___'.substr($partner->location, 0, strpos($partner->location, "-")).'%'; + $whereclause = 'WHERE accounthierarchy like :condition '; + break; +} + +//NEW ARRAY +$criterias = []; +$clause = ''; + +//Check for $_GET variables and build up clause +if(isset($get_content) && $get_content!=''){ + //GET VARIABLES FROM URL + $requests = explode("&", $get_content); + //Check for keys and values + foreach ($requests as $y){ + $v = explode("=", $y); + //INCLUDE VARIABLES IN ARRAY + $criterias[$v[0]] = $v[1]; + + if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='download' || $v[0] =='list'|| $v[0] =='success_msg'){ + //do nothing + } + elseif ($v[0] == 'search') { + //build up search + $clause .= ' AND (carbrand like :'.$v[0].' OR createdby like :'.$v[0].' OR rowID like :'.$v[0].')'; + } + else {//create clause + $clause .= ' AND '.$v[0].' = :'.$v[0]; + } + } + if ($whereclause == '' && $clause !=''){ + $whereclause = 'WHERE '.substr($clause, 4); + } else { + $whereclause .= $clause; + } +} +//Define Query +if(isset($criterias['totals']) && $criterias['totals'] ==''){ +//Request for total rows + $sql = 'SELECT count(*) as count FROM cartest '.$whereclause; +} +elseif(isset($criterias['list']) && $criterias['list'] =='brand'){ + //Request for total rows + $sql = 'SELECT distinct(carbrand) FROM cartest '.$whereclause; +} +elseif(isset($criterias['list']) && $criterias['list'] =='type'){ + //Request for total rows + $sql = 'SELECT distinct(cartype) FROM cartest '.$whereclause; +} +elseif(isset($criterias['download']) && $criterias['download'] ==''){ + //Request for total rows + $sql = 'SELECT * FROM cartest '.$whereclause; +} +else { + //SQL for Paging + $sql = "SELECT * from cartest $whereclause ORDER BY carbrand, cartype DESC LIMIT :page,:num_products"; +} + +$stmt = $pdo->prepare($sql); + +//Bind to query +if (str_contains($whereclause, ':condition')){ + $stmt->bindValue('condition', $condition, PDO::PARAM_STR); +} + +if (!empty($criterias)){ + foreach ($criterias as $key => $value){ + $key_condition = ':'.$key; + if (str_contains($whereclause, $key_condition)){ + if ($key == 'search'){ + $search_value = '%'.$value.'%'; + $stmt->bindValue($key, $search_value, PDO::PARAM_STR); + } + else { + $stmt->bindValue($key, $value, PDO::PARAM_STR); + } + } + } +} + +//------------------------------------------ +// Debuglog +//------------------------------------------ +if (debug){ + $message = $date.';'.$sql.';'.$username; + debuglog($message); +} + +//Add paging details +if(isset($criterias['totals']) && $criterias['totals']==''){ + $stmt->execute(); + $messages = $stmt->fetch(); + $messages = $messages[0]; +} +elseif ((isset($criterias['list']) && $criterias['list']!='') || (isset($criterias['download']) && $criterias['download'] =='')){ + //Excute Query + $stmt->execute(); + //Get results + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +} +else { + $current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1; + $stmt->bindValue('page', ($current_page - 1) * $page_rows_cartest, PDO::PARAM_INT); + $stmt->bindValue('num_products', $page_rows_cartest, PDO::PARAM_INT); + + //Excute Query + $stmt->execute(); + //Get results + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +} + +//------------------------------------------ +//JSON_EnCODE +//------------------------------------------ +$messages = json_encode($messages, JSON_UNESCAPED_UNICODE); +//------------------------------------------ +//Send results +//------------------------------------------ +echo $messages; + +?> \ No newline at end of file diff --git a/api/v2/post/cartests.php b/api/v2/post/cartests.php new file mode 100644 index 0000000..4732a44 --- /dev/null +++ b/api/v2/post/cartests.php @@ -0,0 +1,90 @@ +$partner->salesid, + "soldto"=>$partner->soldto, + "shipto"=>$partner->shipto, + "location"=>$partner->location, + "section"=>$partner->section +); + +// CREATE ACCOUNTHIERARCHY JSON FROM ACCOUNT ARRAY +$accounthierarchy = json_encode($account, JSON_UNESCAPED_UNICODE); +// add to post_content +$post_content['accounthierarchy'] = $accounthierarchy; + +//ADD STANDARD PARAMETERS TO ARRAY BASED ON INSERT OR UPDATE +if ($command == 'insert'){ + $post_content['created'] = $date; + $post_content['createdby'] = $username; +} +//CREAT NEW ARRAY AND MAP TO CLAUSE +if(isset($post_content) && $post_content!=''){ + foreach ($post_content as $key => $var){ + if ($key == 'submit' || $key == 'delete' || $key == 'rowID'){ + //do nothing + } + else { + $criterias[$key] = $var; + $clause .= ' , '.$key.' = ?'; + $clause_insert .= ' , '.$key.''; + $input_insert .= ', ?'; // ? for each insert item + $execute_input[]= $var; // Build array for input + } + } +} + +//CLEAN UP INPUT +$clause = substr($clause, 2); //Clean clause - remove first comma +$clause_insert = substr($clause_insert, 2); //Clean clause - remove first comma +$input_insert = substr($input_insert, 1); //Clean clause - remove first comma + +//QUERY AND VERIFY ALLOWED +if ($command == 'update' && isAllowed('cartest_manage',$profile,$permission,'U') === 1){ + $sql = 'UPDATE cartest SET '.$clause.' WHERE rowID = ? '.$whereclause.''; + $execute_input[] = $id; + $stmt = $pdo->prepare($sql); + $stmt->execute($execute_input); +} +elseif ($command == 'insert' && isAllowed('cartest_manage',$profile,$permission,'C') === 1){ + $sql = 'INSERT INTO cartest ('.$clause_insert.') VALUES ('.$input_insert.')'; + $stmt = $pdo->prepare($sql); + $stmt->execute($execute_input); +} +elseif ($command == 'delete' && isAllowed('cartest_manage',$profile,$permission,'D') === 1){ + $stmt = $pdo->prepare('DELETE FROM cartest WHERE rowID = ? '.$whereclause.''); + $stmt->execute([ $id ]); + + //Add deletion to changelog + changelog($dbname,'cartest',$id,'Delete','Delete',$username); +} else +{ + //do nothing +} + +?> \ No newline at end of file diff --git a/assets/functions.php b/assets/functions.php index 43d1df0..f5de586 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -2262,17 +2262,30 @@ $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); $questions = json_encode($cartest["Questions"] ??''); $datapoints = json_encode($cartest["plugDataPoints"] ?? ''); $nametester = $cartest["NameTester"] ?? 'Unknown'; + $plug_sn = $cartest["SN"] ?? 0; + $accounthierarchy = ''; //get header data only unset($cartest["Questions"]); unset($cartest["plugDataPoints"]); $header = json_encode($cartest); + //GET ACCOUNTHIERARCHY FROM SN OF + if ($plug_sn !=0){ + $sql ='SELECT accounthierarchy FROM equipment WHERE serialnumber = ?'; + $stmt = $pdo->prepare($sql); + //Excute Query + $stmt->execute([$plug_sn]); + //Get results + $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); + $accounthierarchy = $messages[0]['accounthierarchy']; + } + //INSERT INTO CARTEST - $sql = 'INSERT INTO cartest (carbrand, cartype,header,questions,datapoints,createdby) VALUES (?,?,?,?,?,?)'; + $sql = 'INSERT INTO cartest (carbrand, cartype,header,questions,datapoints,createdby,accounthierarchy) VALUES (?,?,?,?,?,?,?)'; $stmt = $pdo->prepare($sql); //Excute Query - $stmt->execute([$carbrand,$cartype,$header,$questions,$datapoints,$nametester]); + $stmt->execute([$carbrand,$cartype,$header,$questions,$datapoints,$nametester,$accounthierarchy]); //MARK HISTORY ITEM FOR DELETATION $sql = 'UPDATE history SET type = "delete" WHERE rowID = '.$message['rowID']; diff --git a/cartest_manage.php b/cartest_manage.php index e80c747..e536220 100644 --- a/cartest_manage.php +++ b/cartest_manage.php @@ -44,7 +44,11 @@ $cartest = [ 'HW' =>'', 'FW' =>'' ], - 'questions' => [], + 'questions' => [ + 'cartest_19' => '', + 'cartest_20' => '', + 'cartest_22' => '' + ], 'datapoints' => [], 'created' => '' ]; diff --git a/dev.php b/dev.php index 148f742..0955e94 100644 --- a/dev.php +++ b/dev.php @@ -7,121 +7,19 @@ include './assets/functions.php'; include './settings/settings.php'; include './settings/config.php'; - -$tes= '40'; - -$test_r = (isset($test))? $test : (isset($test2)? $test2 : 'none'); - -echo $test_r; //Connect to DB $pdo = dbConnect($dbname); - - +$plug_sn ='22050695'; //GET -$sql = 'SELECT rowID, description, equipmentid FROM history where type="Maintenance_Test" and description like "%doubletestvalues%"'; +$sql ='SELECT accounthierarchy FROM equipment WHERE serialnumber = ?'; $stmt = $pdo->prepare($sql); -$stmt->execute(); +//Excute Query +$stmt->execute([$plug_sn]); +//Get results $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); -$watchlist_byproduct = []; -$watchlist_bytest = []; -$watchlist_totals = []; - -//train the model -$total_measurement = traintotalMeasurement($messages); - -//get statics on results -$total_results = statisticalAnalyses($total_measurement); - -//COMPARISON -- CHECK DEVIATIONS FROM STANDARD -foreach ($total_measurement as $measurement => $values){ - foreach($total_results as $total_result => $measured_values){ - if ($measurement == $total_result){ - foreach ($values as $id => $measured_value){ - if (($measured_value <= $total_results[$total_result]['stdev-3']) && ($measured_value >= $total_results[$total_result]['stdev+3'])){ - $watchlist_byproduct[$id][] = array( - "measurement" => $measurement, - "value" => $measured_value, - "deviation" => 3 - ); - $watchlist_bytest[$measurement][] = array( - "equipmentid" => $id, - "value" => $measured_value, - "deviation" => 3 - ); - } - elseif ((($measured_value <= $total_results[$total_result]['stdev-2']) && ($measured_value >= $total_results[$total_result]['stdev-3'])) || (($measured_value >= $total_results[$total_result]['stdev+2']) && ($measured_value <= $total_results[$total_result]['stdev+3']))){ - $watchlist_byproduct[$id][] = array( - "measurement" => $measurement, - "value" => $measured_value, - "deviation" => 2 - ); - $watchlist_bytest[$measurement][] = array( - "equipmentid" => $id, - "value" => $measured_value, - "deviation" => 2 - ); - } - elseif ((($measured_value <= $total_results[$total_result]['stdev-1']) && ($measured_value >= $total_results[$total_result]['stdev-2'])) || (($measured_value >= $total_results[$total_result]['stdev+1']) && ($measured_value <= $total_results[$total_result]['stdev+2']))){ - /*$watchlist_byproduct[$id][] = array( - "measurement" => $measurement, - "value" => $measured_value, - "deviation" => 1 - ); - $watchlist_bytest[$measurement][] = array( - "equipmentid" => $id, - "value" => $measured_value, - "deviation" => 1 - ); - */ - } - } - } - } -} - -//GET WATCHLIST SUMMARY -foreach ($watchlist_bytest as $test => $value){ - $watchlist_totals[$test]['n_deviation'] = count($value); - //calculate percentage - //$watchlist_totals[$test]['percentage'] = ($watchlist_totals[$test]['n_deviation'] / $watchlist_totals[$test]['n_total'])*100; -} - -print "
";
-print_r($total_results);
-print "
"; +var_dump($messages[0]['accounthierarchy']); -/*MAIN STATISCAL FUNCTION -function standDeviation($arr) -{ - $num_of_elements = count($arr); - $variance = 0.0; - // Calculate mean using array_sum() method - $average = array_sum($arr) / $num_of_elements; - foreach($arr as $i) - { - // Sum of squares of differences between all numbers and means. - $variance += pow(($i - $average), 2); - } - return (float)sqrt($variance / $num_of_elements); -} -function average($arr) -{ - $num_of_elements = count($arr); - $average = array_sum($arr) / $num_of_elements; - return $average; -} -function calculateMedian($array) { - if (empty($array)) { - return null; - } else { - sort($array); - $lowMiddle = $array[floor((count($array) - 1) / 2)]; - $highMiddle = $array[ceil((count($array) - 1) / 2)]; - return ($lowMiddle + $highMiddle) / 2; - } -} -*/ ?> \ No newline at end of file