<?php
require_once('config.php');


$error_message;
$valid = true;

$site_cd = $_REQUEST['site_cd'];
$wko_mst_wo_no = $_REQUEST['wko_mst_wo_no'];

/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$sql_wko_ls1="select	wko_ls1= count(wko_ls1.rowid), 
						wko_ls2 = count(wko_ls2.rowid),
						wko_ls3 = count(wko_ls3.rowid),
						wko_ls4 = count(wko_ls4.rowid),
						wko_ls5 = count(wko_ls5.rowid),
						wko_ls6 = count(wko_ls6.rowid),
						wko_ls8 = count(wko_ls8.rowid)

			from wko_mst (Nolock)

			LEFT JOIN wko_ls1 (Nolock) ON wko_mst.site_cd = wko_ls1.site_cd and wko_mst.RowID = wko_ls1.mst_RowID
			LEFT JOIN wko_ls2 (Nolock) ON wko_mst.site_cd = wko_ls2.site_cd and wko_mst.RowID = wko_ls2.mst_RowID
			LEFT JOIN wko_ls3 (Nolock) ON wko_mst.site_cd = wko_ls3.site_cd and wko_mst.RowID = wko_ls3.mst_RowID
			LEFT JOIN wko_ls4 (Nolock) ON wko_mst.site_cd = wko_ls4.site_cd and wko_mst.RowID = wko_ls4.mst_RowID
			LEFT JOIN wko_ls5 (Nolock) ON wko_mst.site_cd = wko_ls5.site_cd and wko_mst.RowID = wko_ls5.mst_RowID
			LEFT JOIN wko_ls6 (Nolock) ON wko_mst.site_cd = wko_ls6.site_cd and wko_mst.RowID = wko_ls6.mst_RowID
			LEFT JOIN wko_ls8 (Nolock) ON wko_mst.site_cd = wko_ls8.site_cd and wko_mst.RowID = wko_ls8.mst_RowID

			WHERE  wko_mst.wko_mst_wo_no=?
			and wko_mst.site_cd=?";	
					
	//echo $sql;
	$params_wko_ls1 = array($wko_mst_wo_no,$site_cd);					
						
	$stmt_wko_ls1 = sqlsrv_query( $conn, $sql_wko_ls1,$params_wko_ls1);

			if( !$stmt_wko_ls1 ) {
				$error_message = "Error SELECT Table (wko_ls1)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}

			

			do {
				 while ($row = sqlsrv_fetch_array($stmt_wko_ls1, SQLSRV_FETCH_ASSOC)) {	
				 
					$wko_ls1 = $row['wko_ls1'];	
					$wko_ls2 = $row['wko_ls2'];
					$wko_ls3 = $row['wko_ls3'];
					$wko_ls4 = $row['wko_ls4'];
					$wko_ls5 = $row['wko_ls5'];
					$wko_ls6 = $row['wko_ls6'];
					$wko_ls8 = $row['wko_ls8'];
					
					$sum =($wko_ls1 + $wko_ls2+$wko_ls3+$wko_ls4+$wko_ls5+$wko_ls6+$wko_ls8);
					
				
				 }
			} while ( sqlsrv_next_result($stmt_wko_ls1) );


if( $stmt_wko_ls1) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData($sum);		
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}

function returnData($sum){
	
	 $json1 = json_encode($sum);
	  
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',	
	'data' => $sum);

	echo json_encode($returnData);
}

function returnError($error_message){	
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}

 
?>