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

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

//echo json_encode($result);

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;
	$LoginID= $Detail->LoginID;
	
	
	
	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,			mbl_audit_user,			mbl_audit_date )
					
		
		  VALUES		(	?,					?,						?,   				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,	
							NULL,				NULL,					NULL,				NULL,					?,
							getDate(),			NULL,					NULL,				NULL,					NULL,				
							NULL, 				?,			getDate())";
								
								
	$params_ast_ls1 = array($site_cd,$stock_no,$mst_RowID,$qty_needed,$ls1_desc,$part_number,$audit_user,$LoginID);
	$stmt = sqlsrv_query( $conn, $sql_insert_ast_ls1,$params_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 		=	?,		
							ast_ls1_varchar1	=	?,		
							ast_ls1_qty_needed	=	?,							
							mbl_audit_user		=	?,							
							mbl_audit_date		=	getDate()							
							WHERE 	site_cd = ? 
							AND 	Rowid = ? ;	";
	
				//echo $sql;
				$params1 = array($ls1_desc,$part_number,$qty_needed,$LoginID,$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();
}
 
?>