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

$error_message;
$valid = true;


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


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


foreach ($result->Header as $Header)
{
	$site_cd = $Header->site_cd;
	$mst_RowID = $Header->mst_RowID;
	$wko_ls11_name = $Header->wko_ls11_name;
	$wko_ls11_sts_upd = $Header->wko_ls11_sts_upd;
	$audit_user = $Header->audit_user;
	$chatID = $Header->chatID;
	
	$sql_insert_wko_ls11 = "INSERT wko_ls11 
						(	site_cd ,	mst_RowID,		wko_ls11_name,		wko_ls11_sts_upd,	wko_ls11_category,
							status,		type,			file_name,			attachment,			audit_user,			
							audit_date,	column1,		column2,			column3,			column4,
							column5,	chatID) 
				VALUES 	(	?,			?,				?,					?,					NULL, 				
							NULL,		NULL,			NULL,				NULL,				?,
							GETDATE(),	NULL,			NULL,				NULL,				NULL,
							NULL, 		? )";

	$params_insert_wko_ls11 = array($site_cd,$mst_RowID,$wko_ls11_name,$wko_ls11_sts_upd,$audit_user,$chatID);
	
	$stmt_insert_wko_ls11 = sqlsrv_query( $conn, $sql_insert_wko_ls11,$params_insert_wko_ls11);	
	
	if( !$stmt_insert_wko_ls11 ) {
		
		$error_message = "Error (INSERT Table wko_ls11)";
		returnError($error_message);
	
		die( print_r( sqlsrv_errors(), true));	
	
	}
	
}


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

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

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