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

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

$ll_row = 0;
$ldc_itm_cost = 0;
$ldc_pr_tot_cost =0;
$ldc_line_cost =0;
$ldc_line_tax =0;
$ldc_total=0;
$first_row = true;

foreach($result->Header as $Heade){
	
	
	$site_cd= $Heade->site_cd;
	$wko_RowID= $Heade->wko_RowID;
	$wko_mst_wo_no= $Heade->wko_mst_wo_no;
	$wko_mst_assetno= $Heade->wko_mst_assetno;
	$wko_mst_chg_costcenter= $Heade->chargecostcenter;
	
	$wko_det_laccount= $Heade->wko_det_laccount;
	$wko_det_caccount= $Heade->wko_det_caccount;
	$wko_det_maccount= $Heade->wko_det_maccount;
	
	$audit_user= $Heade->audit_user;
	$audit_date= $Heade->audit_date;
	$LogingID= $Heade->LOGINID;
	
	
	$sql = "Select cnt_mst_prefix + SUBSTRING(CONVERT(VARCHAR(7), cnt_mst_counter + 1000000), 2, 6)  ls_doc_no
			From 	cnt_mst WITH (UPDLOCK)
			Where 	site_cd ='".$site_cd."'
			And 	cnt_mst_module_cd =  'PR'
			UPDATE 	cnt_mst WITH (UPDLOCK)
			SET 	cnt_mst_counter = cnt_mst_counter + 1
			WHERE 	site_cd ='".$site_cd."' 
			AND 	cnt_mst_module_cd = 'PR'";			
			
			
			$stmt = sqlsrv_query( $conn, $sql);			
			if( !$stmt ) {
				$error_message = "Error update table (itm_mst)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			do {
				while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
				
				$pur_mst_porqnnum = $row['ls_doc_no'];	
				
					 	 
			}
		} while ( sqlsrv_next_result($stmt));
	
	
	$sql = "SELECT 	dft_mst_pur_sts,dft_mst_pur_email_requestor,dft_mst_pr_release_for_app
			FROM	dft_mst (NOLOCK)
			WHERE	( dft_mst.site_cd = '".$site_cd."' )";


		$stmt = sqlsrv_query( $conn, $sql);				

		if( !$stmt ) {
			 $error_message = "Error select table (dft_mst)";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
		}

		do {
			 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
				$ls_mr_status = $row['dft_mst_pur_sts'];	
				$dft_mst_mr_release_for_app = $row['dft_mst_pr_release_for_app'];	
				$dft_mst_mtr_email_requestor = $row['dft_mst_pur_email_requestor'];	
				
			 }
		} while ( sqlsrv_next_result($stmt) );		
		
		
		
	
	$sql_insert_pur_mst = "INSERT pur_mst 
			(	site_cd ,						pur_mst_status,			pur_mst_porqnnum,				pur_mst_rqn_date,		pur_mst_requested_by, 
				pur_mst_dept,					pur_mst_req_date,		pur_mst_chg_costcenter,			pur_mst_chg_account,	pur_mst_crd_costcenter, 
				pur_mst_crd_account,			pur_mst_op_flag,		pur_mst_entered_by ,			pur_mst_tot_cost ,		pur_mst_purq_approve ,
				pur_mst_notes,					pur_mst_app_route_id,	pur_mst_projectid ,				pur_mst_buyer ,			pur_mst_release_for_app , 
				pur_mst_pur_email_requestor ,	pur_mst_create_by ,		pur_mst_create_date ,			audit_user ,			audit_date,
				mbl_audit_user,					mbl_audit_date) 
	VALUES 	(	?,								?,						?,								getDate(),				?,
				NUll,							getDate(),				?,								'',						Null,							
				'',								'0',					?,								'0',					'W',						
				NULL,							NULL,					Null,							NULL,					?,
				?,								?,						getDate(),						?,						getDate(),
				?,								getDate())";
								
	
	If ($ls_mr_status == "" || empty($ls_mr_status)){
		$ls_mr_status= "NEW";
	}	
	$params_pur_mst = array($site_cd,$ls_mr_status,$pur_mst_porqnnum,$audit_user,$wko_mst_chg_costcenter,$LogingID,$dft_mst_mr_release_for_app,$dft_mst_mtr_email_requestor,$LogingID,$LogingID,$LogingID);				
	$stmt_pur_mst = sqlsrv_query( $conn, $sql_insert_pur_mst,$params_pur_mst);

	if( !$stmt_pur_mst ) {
		$error_message = "Error insert table (INSERT Table pur_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_pur_mst);	
		
	
	
	$sql = "SELECT 	RowID FROM 	pur_mst (NOLOCK)
			WHERE	site_cd = '".$site_cd."'
			AND		pur_mst_porqnnum = '".$pur_mst_porqnnum."'";


		$stmt = sqlsrv_query( $conn, $sql);				

		if( !$stmt ) {
			 $error_message = "Error select table (pur_mst)";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
		}

		do {
			 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
				 
				$ll_RowID = $row['RowID'];	
				
			 }
		} while ( sqlsrv_next_result($stmt) );	
		
		
		
	$sql_insert_pur_det = "INSERT pur_det 
									(	 site_cd ,		mst_RowID , 		audit_user , 				audit_date ,	mbl_audit_user,		mbl_audit_date)
							VALUES	(	 ?,				?,					?,							GETDATE(),		?,					GETDATE());";
								
								
	$params_pur_det = array($site_cd,$ll_RowID,$LogingID,$LogingID);			
	$stmt_pur_det = sqlsrv_query( $conn, $sql_insert_pur_det,$params_pur_det);


	if( !$stmt_pur_det ) {
		$error_message = "Error insert table (INSERT Table pur_det)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_pur_det);	
		
	
	
	$sql_insert_pur_ls2 = "INSERT pur_ls2 
					(		site_cd,				mst_RowID ,				pur_ls2_varchar1,		pur_ls2_varchar2,		
							pur_ls2_varchar3,		pur_ls2_datetime1, 		pur_ls2_datetime2,		pur_ls2_numeric1,		
							audit_user,				audit_date,				mbl_audit_user,			mbl_audit_date ) 
			VALUES  (		?,						?,						?,						?,					
							?,						GETDATE (), 			NULL,					NULL,					
							?,						GETDATE (),				?,						GETDATE ())";
								
								
	$params_pur_ls2 = array($site_cd,$ll_RowID,$pur_mst_porqnnum,$ls_mr_status,$audit_user,$LogingID,$LogingID);				
	$stmt_pur_ls2 = sqlsrv_query( $conn, $sql_insert_pur_ls2,$params_pur_ls2);

	if( !$stmt_pur_ls2 ) {
		$error_message = "Error insert table (INSERT Table pur_ls2)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_pur_ls2);
	
	
	
foreach($result->Details as $Detail){
		
		$assestno = $Detail->assestno;
		$supplier = $Detail->supplier;
		$description = $Detail->description;
		$uom = $Detail->uom;
		$issueqty = $Detail->issueqty;
		$quantity = $Detail->quantity;
		$chargecostcenter = $Detail->chargecostcenter;
		$chargeaccount = $Detail->chargeaccount;
		$itemcost = $Detail->itemcost;
		$tax_code = $Detail->tax_code;
		
		if (!empty($tax_code)){
	
		   $tax_code = $tax_code;	
			
		}else{
			
			$tax_code = Null;
			
		}
		
		
		$ll_row++;
		
		$ldc_itm_cost = $itemcost / $quantity;
		$ldc_pr_tot_cost =  $ldc_pr_tot_cost + $itemcost;
	
	
		if(empty($supplier)){
			
			$sldc_rate = "1";
			
			
			$sql = "SELECT 	 cur_mst.cur_mst_base_cur FROM 	cur_mst    WHERE cur_mst.site_cd = '".$site_cd."' and cur_mst_base_cur= '1'  ";


			$stmt = sqlsrv_query( $conn, $sql);				

			if( !$stmt ) {
				 $error_message = "Error select table (cur_mst)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
					 
					$sup_mst_curr_code = $row['cur_mst_base_cur'];	
					
				 }
			} while ( sqlsrv_next_result($stmt) );	
			
			
			
			
		}else{
			
			$sql = "SELECT 	sup_mst_curr_code FROM 	sup_mst (NOLOCK)WHERE	sup_mst.site_cd = '".$site_cd."' AND sup_mst.sup_mst_supplier_cd  = '".$supplier."'";


			$stmt = sqlsrv_query( $conn, $sql);				

			if( !$stmt ) {
				 $error_message = "Error select table (sup_mst)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
					 
					$sup_mst_curr_code = $row['sup_mst_curr_code'];	
					
				 }
			} while ( sqlsrv_next_result($stmt) );	
			
			
			$sql = "SELECT 	sldc_rate = COALESCE ( cur_mst_exchange_rate , 0 ) FROM 	cur_mst (NOLOCK)WHERE	cur_mst.site_cd = '".$site_cd."' AND 
			cur_mst_cur_code = '".$sup_mst_curr_code."'";


			$stmt = sqlsrv_query( $conn, $sql);				

			if( !$stmt ) {
				 $error_message = "Error select table (cur_mst)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
					 
					$sldc_rate = $row['sldc_rate'];	
					
				 }
			} while ( sqlsrv_next_result($stmt) );		

		}
		
		
		if(empty($tax_code)){
			$ldc_tax_rate = "0";
		}else{
			
			$sql = "SELECT 	ldc_tax_rate = COALESCE ( tax_mst_tax_rate , 0 ) FROM 	tax_mst (NOLOCK) WHERE	tax_mst.site_cd = '".$site_cd."' AND tax_mst_tax_cd = '".$tax_code."'";


			$stmt = sqlsrv_query( $conn, $sql);				

			if( !$stmt ) {
				 $error_message = "Error select table (tax_mst)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}

			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
					 
					$ldc_tax_rate = $row['ldc_tax_rate'];	
					
				 }
			} while ( sqlsrv_next_result($stmt) );	
		}		
		
		
		
		
		if($ldc_tax_rate > 0){
			$ldc_tax_amt = ($ldc_itm_cost *($ldc_tax_rate/100))*$quantity;
		}else{
			$ldc_tax_amt =0;
		}
		
			$ldc_retail_price = $itemcost * $sldc_rate;
			$ldc_line_cost 	= $ldc_line_cost + ($quantity * $ldc_itm_cost);
			$ldc_line_tax 	= $ldc_line_tax + $ldc_tax_amt;
			$ldc_total = $ldc_line_cost + $ldc_line_tax;
			/* ldc_line_cost 	= ldc_line_cost + (ldc_req_qty * ldc_itm_cost)
			ldc_line_tax 	= ldc_line_tax + ldc_tax_amt
			
			ldc_total = ldc_line_cost + ldc_line_tax
			
			iu_dw.SetItem(ll_find, "wko_ls4_pr_no", ls_doc)
			iu_dw.SetItem(ll_find, "wko_ls4_pr_lineno", ll_row) */
						
		
		
		$sql_insert_pur_ls1 = "INSERT pur_ls1 
			(		site_cd ,					mst_RowID ,					pur_ls1_pr_lineno ,			pur_ls1_wo_no ,				pur_ls1_mr_no , 
					pur_ls1_mr_lineno ,			pur_ls1_quo_no ,			pur_ls1_contract_id ,		pur_ls1_contract_lineno ,	pur_ls1_po_no , 
					pur_ls1_po_lineno ,			pur_ls1_op_refno ,			pur_ls1_item_category ,		pur_ls1_stockno ,			pur_ls1_stk_locn , 
					pur_ls1_desc ,				pur_ls1_ord_uom ,			pur_ls1_ord_qty ,			pur_ls1_supplier ,			pur_ls1_rec_supplier , 
					pur_ls1_item_cost ,			pur_ls1_last_item_cost ,	pur_ls1_chg_costcenter ,	pur_ls1_chg_account ,		pur_ls1_taxable ,			
					pur_ls1_activity_code ,		pur_ls1_projectid ,			pur_ls1_rcv_in_full ,		pur_ls1_cur_code ,			pur_ls1_cur_exchange_rate , 
					pur_ls1_retail_price ,		pur_ls1_tax_code ,			pur_ls1_tax_rate ,			pur_ls1_tax_value ,			audit_user , 
					audit_date,					mbl_audit_user,				mbl_audit_date  )
	VALUES (		?,							?,							?,							?, 							NULL,
					NULL,						NULL,						NULL,						NULL, 						NULL,
					NULL,						NULL,						'Service',					NULL, 						NULL,
					?,							?,							?,							?, 							NULL, 
					?,							'0',						?,							?,							'0', 
					Null,						Null,						'0',						?,							?,
					?,							?,							?,							?,							?,
					getDate(),					?,							getDate())";
								
								
				//echo $sql_insert_pur_ls1;
	$params_pur_ls1 = array($site_cd,$ll_RowID,$ll_row,$wko_mst_wo_no,$description,$uom,$quantity,$supplier,$ldc_itm_cost,$chargecostcenter,$chargeaccount,$sup_mst_curr_code,$sldc_rate,$ldc_retail_price,$tax_code,$ldc_tax_rate,$ldc_tax_amt,$LogingID,$LogingID);			
	$stmt = sqlsrv_query( $conn, $sql_insert_pur_ls1,$params_pur_ls1);

	if( !$stmt ) {
		$error_message = "Error insert table (INSERT Table pur_ls1)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt);
	
	
	$sql_insert_wko_ls2 = "INSERT INTO wko_ls3 
									(	site_cd,			wko_ls3_assetno,			wko_ls3_activity_code,				 wko_ls3_rec_supplier,			 
										wko_ls3_po_no,		wko_ls3_po_lineno,							wko_ls3_item_cost,			wko_ls3_act_cost, 
										wko_ls3_descr,		wko_ls3_qty_needed, 		wko_ls3_mtl_uom,				wko_ls3_pr_no,				wko_ls3_pr_lineno, 
										wko_ls3_nonrefpono, wko_ls3_com_code,			wko_ls3_taxable,				wko_ls3_chg_costcenter, 	wko_ls3_chg_account, 
										wko_ls3_inv_no,		wko_ls3_inv_lineno, 		wko_ls3_varchar1,				wko_ls3_varchar2,			wko_ls3_varchar3, 
										wko_ls3_varchar4,	wko_ls3_varchar5,			wko_ls3_numeric1,				wko_ls3_numeric2,			wko_ls3_numeric3, 
										wko_ls3_numeric4,	wko_ls3_numeric5,			wko_ls3_datetime1,				wko_ls3_datetime2,			wko_ls3_datetime3, 
										wko_ls3_datetime4,	wko_ls3_datetime5,			wko_ls3_tax_cd,					wko_ls3_tax_rate,			wko_ls3_tax_value, 
										audit_user,			audit_date,					column1,						column2,					column3, 
										column4,			column5,					mst_RowID,  wko_ls3_mr_no										 )
							VALUES (	?,					?,							NULL,							?,							
										NULL,				NULL,													?,							'0.0',
										?,					?,							?,								?,							?,
										NULL,				NULL,						NULL,							?,							?,
										NULL,				NULL,						NULL,							NULL,						NULL,
										NULL,				NULL,						NULL,							NULL,						NULL,
										NULL,				NULL,						NULL,							NULL,						NULL,
										NULL,				NULL,						?,								?,							'0.0',
										?,					GETDATE( ),					NULL,							NULL,						NULL,						
										NULL,				NULL,						?, '-')";
														
								
	$params_wko_ls4 = array($site_cd,$assestno,$supplier,$itemcost,$description,$quantity,$uom,$pur_mst_porqnnum,$ll_row,$chargecostcenter,$chargeaccount,$tax_code,$sldc_rate,$LogingID,$wko_RowID);				
	$stmt = sqlsrv_query( $conn, $sql_insert_wko_ls2,$params_wko_ls4);

	if( !$stmt ) {
		$error_message = "Error insert table (INSERT Table wko_ls2)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt);
	
	
	
		
}/* 2nd lopp */	
	
	
	$sql = "SELECt 'count' = COUNT (*) 
			FROM	cf_cost_center (NOLOCK),emp_ls2 (NOLOCK)
			WHERE	cf_cost_center.site_cd = emp_ls2.site_cd
			AND		cf_cost_center.costcenter = emp_ls2.emp_ls2_costcenter
			AND		emp_ls2.emp_ls2_approver = '1'
			AND		cf_cost_center.site_cd = '".$site_cd."'
			AND		cf_cost_center.costcenter = '".$wko_mst_chg_costcenter."'";


		$stmt = sqlsrv_query( $conn, $sql);				

		if( !$stmt ) {
			 $error_message = "Error select table (cf_cost_center)";
			 returnError($error_message);
			 die( print_r( sqlsrv_errors(), true));
		}

		do {
			 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
				 
				$count = $row['count'];	
				
			 }
		} while ( sqlsrv_next_result($stmt) );
		
		if($count >0){
			
			
			$sql = "SELECT cf_cost_center.costcenter,
							cf_cost_center.descs,
							emp_ls2.emp_ls2_empl_id,   
							emp_ls2.emp_ls2_pr_approval_limit,
							emp_ls2.emp_ls2_approver,   
							emp_ls2.emp_ls2_level,
							emp_ls2.RowID 
			FROM		cf_cost_center (NOLOCK),emp_ls2 (NOLOCK)
			WHERE	cf_cost_center.site_cd = emp_ls2.site_cd
			AND		cf_cost_center.costcenter = emp_ls2.emp_ls2_costcenter
			AND		emp_ls2.emp_ls2_approver = '1'
			AND		cf_cost_center.site_cd = '".$site_cd."'
			AND		cf_cost_center.costcenter = '".$wko_mst_chg_costcenter."'
			ORDER BY emp_ls2.emp_ls2_level "	;


			$stmt = sqlsrv_query( $conn, $sql);				

			if( !$stmt ) {
				 $error_message = "Error select table (cf_cost_center)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}

			$json = array();
			
			
			do {
				 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
					 
					$costcenter = $row['costcenter'];	
					$descs = $row['descs'];	
					$emp_ls2_empl_id = $row['emp_ls2_empl_id'];	
					$emp_ls2_pr_approval_limit = $row['emp_ls2_pr_approval_limit'];	
					$emp_ls2_approver = $row['emp_ls2_approver'];	
					$emp_ls2_level = $row['emp_ls2_level'];	
					$RowID = $row['RowID'];			
					
					
					if ($first_row) {
						//echo $first_row;
					 $next_approver = $emp_ls2_empl_id;
					 $first_row = false;
					}
					
					//$next_approver = $emp_ls3_empl_id
					
					$sql_insert_pur_app = "Insert pur_app 
									(	site_cd ,				mst_RowID ,							pur_app_level,				pur_app_cost_center, 
										pur_app_empl_id,		pur_app_pr_limit,					pur_app_date,				pur_app_status, 
										pur_app_approval,		audit_user ,						audit_date,					mbl_audit_user,		mbl_audit_date )
						Values 		(	?,						?,									?,							?, 
										?,						?,									NULL ,						'P', 
										NULL,					?,									GetDate(),					?,					GetDate ()) ";								
											
					
					$params_pur_app = array($site_cd,$ll_RowID,$emp_ls2_level,$wko_mst_chg_costcenter,$emp_ls2_empl_id,$emp_ls2_pr_approval_limit,$LogingID,$LogingID);						
					$stmt2 = sqlsrv_query( $conn, $sql_insert_pur_app,$params_pur_app);

					if( !$stmt2 ) {
						$error_message = "Error insert table (INSERT Table pur_app)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}	
					sqlsrv_free_stmt( $stmt2);
					
					//$json[]= $row;	
					
					
				 }			 
				 
			} while ( sqlsrv_next_result($stmt) );
			sqlsrv_free_stmt( $stmt);
			
					
			
			
			
			$sql = "SELECt 'Max' =MAX(emp_ls2_level) 
			FROM	cf_cost_center (NOLOCK),emp_ls2 (NOLOCK)
			WHERE	cf_cost_center.site_cd = emp_ls2.site_cd
			AND		cf_cost_center.costcenter = emp_ls2.emp_ls2_costcenter
			AND		emp_ls2.emp_ls2_approver = '1'
			AND		cf_cost_center.site_cd = '".$site_cd."'
			AND		cf_cost_center.costcenter = '".$wko_mst_chg_costcenter."'";


				$stmt = sqlsrv_query( $conn, $sql);				

				if( !$stmt ) {
					 $error_message = "Error select table (cf_cost_center)";
					 returnError($error_message);
					 die( print_r( sqlsrv_errors(), true));
				}

				do {
					 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
						 
						$Max = $row['Max'];	
						
					 }
				} while ( sqlsrv_next_result($stmt) );
				sqlsrv_free_stmt( $stmt);
					
					$sql = "UPDATE pur_mst 
							SET pur_mst_app_level = ? , 
								pur_mst_cur_app_level = ?, 
								pur_mst_approver = ? , 
								pur_mst_sub_tot_cost = ? ,
								pur_mst_tax= ?,
								pur_mst_tot_cost = ?,
								mbl_audit_user= ?,
								mbl_audit_date=getDate()
						
						WHERE 	site_cd = ? 
						AND 	RowID = ?";	

					$params_pur_mst = array($Max,0,$next_approver,$ldc_line_cost,$ldc_line_tax,$ldc_total,$LogingID,$site_cd,$ll_RowID);								
					$stmt = sqlsrv_query( $conn, $sql,$params_pur_mst);				
									
					if( !$stmt ) {
						 $error_message = "Error update table (pur_mst)";
						 returnError($error_message);
						 die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt);


		}else{
			
			
			$sql = "UPDATE pur_mst 
					SET pur_mst_app_level = ?, 
						pur_mst_cur_app_level = ?, 
						pur_mst_approver =Null, 
						pur_mst_sub_tot_cost = ? ,
						pur_mst_tax= ?,
						pur_mst_tot_cost = ?,
						mbl_audit_user= ?,
						mbl_audit_date=getDate()
				WHERE 	site_cd = ?
				AND 	RowID = ?";	

			$params_pur_mst = array(0,0,$ldc_line_cost,$ldc_line_tax,$ldc_total,$LogingID,$site_cd,$ll_RowID);				
			$stmt = sqlsrv_query( $conn, $sql,$params_pur_mst);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (pur_mst)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}sqlsrv_free_stmt( $stmt);

		}
		
			
		$sql = "UPDATE wko_det 
				SET wko_det_est_con_cost = wko_det_est_con_cost + '".$ldc_total."', 
				audit_user = '".$LogingID."', 
				audit_date = GetDate (),
				mbl_audit_user='".$LogingID."',
				mbl_audit_date=getDate()				
				WHERE mst_RowID = '".$wko_RowID."'";	

					
			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (wko_det)";
				 returnError($error_message);
				 die( print_r( sqlsrv_errors(), true));
			}sqlsrv_free_stmt( $stmt);
	
	
	
}/* 1st lopp */


if ($valid) {
	
	sqlsrv_close($conn);	
	returnData($pur_mst_porqnnum,$json);	
}

//sqlsrv_free_stmt( $stmt);




function returnData($pur_mst_porqnnum,$json){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'The purchase request no ('.$pur_mst_porqnnum .') created successfully) ',
	'data' => $json);
	
	echo json_encode($returnData);
}

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