tinyqc/v1/inc/entry_monthly.php

192 lines
6.9 KiB
PHP
Raw Normal View History

<?php
include("config.php");
if(!isset($_POST['submit'])) {
if(isset($_GET['control'])) { $control = $_GET['control']; }
else { $control = $_POST['control']; }
if(isset($_GET['dates'])) { $date = $_GET['dates']; }
else { $date = $_POST['dates']; }
$date = substr($date,0,7);
if(isset($_GET['test'])) { $testid = $_GET['test']; }
else { $testid = $_POST['test']; }
// control data
$sql = "select name, lot from DICT_CONTROL where id='$control'";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC );
$cname = $row[0];
$lot = $row[1];
// test data
$sql = "select name from DICT_TEST where id='$testid'";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC );
$tname = $row[0];
// test result
$sql = "select resdate, resvalue, rescomment from DAILY_RESULT where controlid='$control' and testid='$testid' and convert( varchar(7), resdate, 126) ='$date' ";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ) ) {
$day = date_format($row[0],'j');
$res = $row[1];
$rescom = $row[2];
$data [$day] = $res;
$comment [$day] = $rescom;
}
#mean sd
$sql = "select mean, sd from CONTROL_TEST where controlid='$control' and testid='$testid'";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC );
$mean = $row[0];
$sd = $row[1];
$max = $mean + ($sd*2);
$min = $mean - ($sd*2);
#comtext
$sql = "select comtext from MONTHLY_COMMENT where controlid='$control' and testid='$testid' and commonth='$date'";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC );
$comtext = '';
if(isset($row[0])) { $comtext = $row[0]; }
$hari = date("t",strtotime("$date-1"));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>QC APP</title>
<link rel="stylesheet" href="../assets/styles.css?v=<?php echo time(); ?>">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div id='container'>
<div id='content'>
<b>Periode</b> <?=$date;?><br/>
<table border='1'>
<tr> <th colspan='2'><?php echo "$cname ($lot) - $tname";?></th> </tr>
<tr> <td>Mean</td> <td><?=$mean;?></td></tr>
<tr> <td>SD</td> <td><?=$sd;?></td></tr>
<tr> <td>Range</td> <td><?=$min;?> - <?=$max;?></td></tr>
</table>
<br/>
<form method='POST'>
<table border='1'>
<tr> <th>Day</th> <th>Result</th> <th>Comment</th> </tr>
<?php
for($i=1;$i<=$hari;$i++) {
echo "<tr> <td>$i</td>";
if(isset($data[$i])) { echo "<td><input type='text' name='data[$i]' value='$data[$i]' size='25' /></td>"; }
else { echo "<td><input type='text' name='data[$i]' size='25' /></td>"; }
if(isset($comment[$i])) { echo "<td><input type='text' name='comment[$i]' value='$comment[$i]' size='50' /></td>"; }
else { echo "<td><input type='text' name='comment[$i]' size='50' /></td>"; }
}
?>
<tr> <td colspan='2' valign='top'>Comment:</td> <td><textarea name='monthlycomment' cols='40'><?=$comtext;?></textarea> </td> </tr>
<tr> <td colspan='3' align='right'><input type='submit' name='submit' value='update' /></td> </tr>
</table>
<input type='hidden' name='control' value='<?=$control;?>' />
<input type='hidden' name='dates' value='<?=$date;?>' />
<input type='hidden' name='test' value='<?=$testid;?>' />
</form>
</div>
</div>
</body>
</html>
<?php
} else {
$control = $_POST['control'];
$date = $_POST['dates'];
$testid = $_POST['test'];
$data = $_POST['data'];
$comment = $_POST['comment'];
$i = 1;
foreach($data as $data1) {
if($data1>0) {$rdata[$i]=$data1;}
$i++;
}
$data = $rdata;
$hari = date("t",strtotime("$date-1"));
$sql = "SELECT id,resdate FROM DAILY_RESULT WHERE convert(varchar(7),resdate,126) ='$date' and controlid='$control' and testid='$testid'";
//echo $sql;
$stmt = sqlsrv_query( $conn1, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ) ) {
$id = $row[0];
$cday = date_format($row[1],"j");
$resid[$cday] = $id;
}
///*
for($i=1;$i<=$hari;$i++) {
if( isset($data[$i]) ) {
if( isset($resid[$i]) ) { $dataUpdate[$resid[$i]] = $data[$i]; $commentUpdate[$resid[$i]]= $comment[$i]; } // data update
else { $dataInsert[$i] = $data[$i]; } // data insert
}
elseif( isset($resid[$i]) ) { $dataDelete[] = $resid[$i]; } // data delete
}
//INSERT
if(isset($dataInsert)) {
$sql = "INSERT INTO DAILY_RESULT (controlid, testid, resdate, resvalue, rescomment ) VALUES ";
foreach($dataInsert as $day => $data1) {
if($day < 10) { $day="0$day"; }
$sql.= "('$control', '$testid', '$date-$day', '$data1','$comment[$day]'),";
}
$sql = substr($sql,0,-1);
$stmt = sqlsrv_query( $conn1, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
echo "inserting new control result done... <br/>";
}
//UPDATE
if(isset($dataUpdate)) {
foreach($dataUpdate as $drid => $data1) {
$sql = "update DAILY_RESULT set resvalue='$data1', rescomment='$commentUpdate[$drid]' where id='$drid'";
$stmt = sqlsrv_query( $conn1, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
//echo "update $drid <br/>";
}
echo "updating control result done... <br/>";
}
//DELETE
if(isset($dataDelete)) {
foreach($dataDelete as $drid ) {
$sql = "delete from DAILY_RESULT where id='$drid'";
$stmt = sqlsrv_query( $conn1, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
//echo "delete $drid $sql<br/>";
}
echo "deleting control result done... <br/>";
}
$comtext = $_POST['monthlycomment'];
if( $comtext != '' ) {
$sql = "if exists (select * from MONTHLY_COMMENT with (updlock,serializable) where commonth='$date' and controlid='$control' and testid='$testid')
begin
update MONTHLY_COMMENT set comtext='$comtext'
where commonth='$date' and controlid='$control' and testid='$testid'
end
else
begin
insert into MONTHLY_COMMENT (controlid, testid, commonth, comtext )
values ('$control', '$testid', '$date', '$comtext')
end";
echo $sql;
$stmt = sqlsrv_query( $conn1, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
echo "updating comment done<br/>";
} else {
$sql = "delete from MONTHLY_COMMENT where commonth='$date' and controlid='$control' and testid='$testid'";
$stmt = sqlsrv_query( $conn1, $sql);
if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); }
echo "deleting comment done<br/>";
}
//echo "<br/>redirecting ....<META http-equiv='refresh' content='2;url=./index.php?p=entry_1&control=$control&dates=$date&test=$testid'>";
echo "<br/>redirecting ....<META http-equiv='refresh' content='2;url=./index.php?p=entry'>";
//*/
}
?>