<?php
require_once('config.php');
$error_message;
$valid = true;

$json = file_get_contents('php://input');
$result = json_decode($json);


foreach($result->Details as $Detail){
	
	
	$site_cd= $Detail->site_cd;
	$stock_no= $Detail->stock_no;
	$mst_RowID= $Detail->mst_RowID;
	$part_number= $Detail->part_number;
	$qty_needed= $Detail->qty_needed;
	$ls1_desc= $Detail->ls1_desc;
	
	$audit_user= $Detail->audit_user;
	$RowID= $Detail->RowID;
	
	
	
	if(empty($RowID)){
		
		$sql_insert_ast_ls1 ="INSERT INTO ast_ls1
						(	site_cd,			ast_ls1_stock_no,		mst_RowID,			ast_ls1_position_no,	ast_ls1_qty_needed,	
							ast_ls1_desc,		ast_ls1_serial_no,		ast_ls1_mfg_cd,		ast_ls1_modelno,		ast_ls1_varchar1,	
							ast_ls1_varchar2,	ast_ls1_varchar3,		ast_ls1_varchar4,	ast_ls1_varchar5,		ast_ls1_varchar6,	
							ast_ls1_varchar7,	ast_ls1_varchar8,		ast_ls1_varchar9,	ast_ls1_varchar10,		ast_ls1_numeric1,		
							ast_ls1_numeric2,	ast_ls1_numeric3,		ast_ls1_numeric4,	ast_ls1_numeric5,		ast_ls1_numeric6,
							ast_ls1_numeric7,	ast_ls1_numeric8,		ast_ls1_numeric9,	ast_ls1_numeric10,		ast_ls1_datetime1,	
							ast_ls1_datetime2,	ast_ls1_datetime3,		ast_ls1_datetime4, 	ast_ls1_datetime5,		ast_ls1_datetime6,	
							ast_ls1_datetime7,	ast_ls1_datetime8,		ast_ls1_datetime9,	ast_ls1_datetime10, 	audit_user,			
							audit_date, 		column1, 				column2,			column3,				column4,			column5 )
					
		
		  VALUES		(	'".$site_cd."',		'".$stock_no."',		'".$mst_RowID."',   NULL,					'".$qty_needed."',				
							'".$ls1_desc."',	NULL,					NULL,				NULL,					'".$part_number."',				
							NULL,				NULL,					NULL,				NULL,					NULL,	
							NULL,				NULL,					NULL,				NULL,					NULL,		
							NULL,				NULL,					NULL,				NULL,					NULL,				
							NULL,				NULL,					NULL,				NULL,					NULL,
							NULL,				NULL,					NULL,				NULL,					NULL,	
							NULL,				NULL,					NULL,				NULL,					'".$audit_user."',
							getDate(),			NULL,					NULL,				NULL,					NULL,				NULL)";
								
								
	
	$stmt = sqlsrv_query( $conn, $sql_insert_ast_ls1);
	
	//echo 	$sql_insert_ast_ls1 ;

	if( !$stmt ) {
		$error_message = "Error insert table (INSERT Table mtr_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt);
		
	}else{
		//echo $RowID;
		$sql1 = "UPDATE 	ast_ls1 SET
							
							ast_ls1_desc ='".$ls1_desc."',		
							ast_ls1_varchar1='".$part_number."',		
							ast_ls1_qty_needed='".$qty_needed."'							
							WHERE 	site_cd = ? 
							AND 	Rowid = ? ;	";
	
				//echo $sql;
				$params1 = array($site_cd, $RowID);	
				$stmt1 = sqlsrv_query( $conn, $sql1, $params1);	
					
			if( !$stmt1 ) {
				$error_message = "Error -1 update table (UPDATE ast_ls1)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}sqlsrv_free_stmt( $stmt1);
		
	}
			
	
}/* 1st lopp */


if ($valid) {	
	returnData();	
}

//sqlsrv_free_stmt( $stmt);
sqlsrv_commit( $conn );
sqlsrv_close($conn);



function returnData(){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Asset Spare List Updated Successfully ');
	
	echo json_encode($returnData);
}

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