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

//f_get_update_budget('UZMA', '20200907', 'CC01', 'STORE', 'CHARGE', 1000, $conn);

function f_update_purchase_budget($site, $trx_date, $costcenter, $account, $trx_type, $ar_amount, $conn, $emp_id){
	

	
	$year;
	$count;
	$period;
	$ldc_awa_pr_amt = array(12);
	$ldc_pr_amt = array(12);
	$ldc_po_amt = array(12);
	$valid = true;
	
	
	if (is_string($trx_date)) {
    // Handle string input
		$dateTime = new DateTime($trx_date);
		
		$trx_date = $dateTime->format('Y-m-d');
		
		//echo $trx_date;
		//die();
		
	} elseif ($trx_date instanceof DateTimeInterface) {
		// Handle DateTimeInterface object
		//$dateTime = new DateTime($trx_date->format('Y-m-d H:i:s')); // Convert object to string
		$trx_date = $trx_date->format('Y-m-d');
		
		
		
	} 
	
	


	//$trx_date = $trx_date->format('Y-m-d H:i:s'); // Format the DateTime object to the desired format
	
	// Check if $account is null or $costcenter is empty
    if (is_null($account) || strlen($costcenter) == 0 || is_null($account) || strlen($account) == 0) {
        return false;
    }

    // Check if $trx_type is null or empty
    if (is_null($trx_type) || strlen($trx_type) == 0) {
        return false;
    }

	
	$sql = "SELECT 	prod_year
			FROM	cf_acct_period 
			WHERE 	site_cd = '".$site."'
			AND		p1beg <= '".$trx_date."'
			AND 	end_of_year >= '".$trx_date."'";
			
		//file_put_contents('f_update_purchase_budget.json',$sql);

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_acct_period)";
		 $valid = false;
		 die( print_r( sqlsrv_errors(), true));
	}

	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
			$year = $row['prod_year'];
		 }
	} while ( sqlsrv_next_result($stmt) );	
	
	$period = f_get_fin_period($site, $year, $trx_date, $conn);
	//echo f_get_fin_period($site, $year, $trx_date, $conn).' </b>'; 
	
	//echo $period;
	
	for ($li_cnt = 1; $li_cnt <= 12; $li_cnt++) {
		if ($li_cnt == $period){
			if ($trx_type == "ADD_AWA_PR") {
				$ldc_awa_pr_amt[$li_cnt] = $ar_amount;
				$ldc_pr_amt[$li_cnt] = 0;
				$ldc_po_amt[$li_cnt] = 0;
			}else if($trx_type == "MINUS_AWA_PR"){
				
				$ldc_awa_pr_amt[$li_cnt] = ($ar_amount * -1);
				$ldc_pr_amt[$li_cnt] = 0;
				$ldc_po_amt[$li_cnt] = 0;
				
			}else if($trx_type == "ADD_PR"){
				
				$ldc_awa_pr_amt[$li_cnt] = 0;
				$ldc_pr_amt[$li_cnt] = $ar_amount;
				$ldc_po_amt[$li_cnt] = 0;
				
			}else if($trx_type == "MINUS_PR"){
				
				$ldc_awa_pr_amt[$li_cnt] = 0;
				$ldc_pr_amt[$li_cnt] = ($ar_amount * -1);
				$ldc_po_amt[$li_cnt] = 0;
				
			}else if($trx_type == "ADD_PO"){
				
				$ldc_awa_pr_amt[$li_cnt] = 0;
				$ldc_pr_amt[$li_cnt] = 0;
				$ldc_po_amt[$li_cnt] = $ar_amount;
				
			}else if($trx_type == "MINUS_PO"){
				
				$ldc_awa_pr_amt[$li_cnt] = 0;
				$ldc_pr_amt[$li_cnt] = 0;
				$ldc_po_amt[$li_cnt] = ($ar_amount * -1);
				
			}
			
			
		}else{
			
			$ldc_awa_pr_amt[$li_cnt] = 0;
			$ldc_pr_amt[$li_cnt] = 0;
			$ldc_po_amt[$li_cnt] = 0;
		}
 
	}
	
	//Count
	$sql = "Select 	'count' = Count(*)
			from 	cf_budget
			Where 	cf_budget.site_cd = '".$site."'
			And		budget_year = ".$year."
			And		costcenter = '".$costcenter."'
			And		account = '".$account."'";

	$stmt = sqlsrv_query( $conn, $sql);				
					
	if( !$stmt ) {
		 $error_message = "Error select table (cf_budget)";
		 $valid = false;
		 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) {
		
		//cf_budget
		$sql = "INSERT INTO cf_budget ( 
				site_cd, 			budget_year, 			costcenter, 			account, 			per1, 			per2, 			per3, 
				per4, 				per5, 					per6, 					per7, 				per8, 			per9, 			per10, 
				per11, 				per12, 					per13, 					audit_user, 		audit_date )
		VALUES ( 
				'".$site."', 		".$year.", 				'".$costcenter."', 		'".$account."',		0, 				0, 				0, 
				0, 					0, 						0, 						0, 					0, 				0, 				0, 
				0, 					0, 						0, 						'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget)";
			 die( print_r( sqlsrv_errors(), true));
		}

		//cf_budget_charge
		$sql = "INSERT INTO cf_budget_charge ( 
			site_cd, 				budget_year, 			costcenter, 			account, 			per1, 				per2, 				per3, 
			per4, 					per5, 					per6, 					per7, 				per8, 				per9, 				per10, 
			per11, 					per12, 					per13, 					audit_user, 		audit_date )
		VALUES ( 
			'".$site."', 			".$year.", 				'".$costcenter."', 		'".$account."', 	0, 					0, 					0,  
			0, 						0, 						0, 						0, 					0, 					0, 					0, 
			0,						0, 						0, 						'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_charge)";
			 $valid = false;
			 die( print_r( sqlsrv_errors(), true));
		}
		
		//cf_budget_credit
		$sql = "INSERT INTO cf_budget_credit ( 
			site_cd, 				budget_year, 			costcenter, 			account, 			per1, 				per2, 				per3, 
			per4, 					per5, 					per6, 					per7, 				per8, 				per9, 				per10, 
			per11, 					per12, 					per13, 					audit_user, 		audit_date )
		VALUES ( 
			'".$site."', 			".$year.", 				'".$costcenter."', 		'".$account."', 	0, 					0,					 0,  
			0, 						0, 						0, 						0, 					0, 					0, 					 0, 
			0,						0, 						0, 						'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_credit)";
			 die( print_r( sqlsrv_errors(), true));
		}
		
		//cf_budget_awa_pr
		$sql = "INSERT INTO cf_budget_awa_pr ( 
				site_cd, 					budget_year, 			costcenter, 			account, 				per1, 					per2, 					per3, 
				per4, 						per5, 					per6, 					per7, 					per8, 					per9, 					per10, 
				per11, 						per12, 					per13, 					audit_user, 			audit_date )
		VALUES ( 
				'".$site."', 				".$year.", 				'".$costcenter."', 		'".$account."',			".$ldc_awa_pr_amt[1].", ".$ldc_awa_pr_amt[2].", ".$ldc_awa_pr_amt[3].", 
				".$ldc_awa_pr_amt[4].", 	".$ldc_awa_pr_amt[5].", ".$ldc_awa_pr_amt[6].", ".$ldc_awa_pr_amt[7].", ".$ldc_awa_pr_amt[8].", ".$ldc_awa_pr_amt[9].", ".$ldc_awa_pr_amt[10].", 
				".$ldc_awa_pr_amt[11].", 	".$ldc_awa_pr_amt[12].", 0, 					'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_awa_pr)";
			 $valid = false;
			 die( print_r( sqlsrv_errors(), true));
		}
		
		//cf_budget_pr
		$sql = "INSERT INTO cf_budget_pr ( 
				site_cd, 					budget_year, 			costcenter, 			account, 				per1, 					per2, 					per3, 
				per4, 						per5, 					per6, 					per7, 					per8, 					per9, 					per10, 
				per11, 						per12, 					per13, 					audit_user, 			audit_date )
		VALUES ( 
				'".$site."', 				".$year.", 				'".$costcenter."', 		'".$account."',			".$ldc_pr_amt[1].", 	".$ldc_pr_amt[2].", 	".$ldc_pr_amt[3].", 
				".$ldc_pr_amt[4].", 		".$ldc_pr_amt[5].", 	".$ldc_pr_amt[6].", 	".$ldc_pr_amt[7].",	 	".$ldc_pr_amt[8].", 	".$ldc_pr_amt[9].", 	".$ldc_pr_amt[10].", 
				".$ldc_pr_amt[11].", 		".$ldc_pr_amt[12].", 	0, 						'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_pr)";
			 $valid = false;
			 die( print_r( sqlsrv_errors(), true));
		}
		
		//cf_budget_po
		$sql = "INSERT INTO cf_budget_po ( 
				site_cd, 					budget_year, 			costcenter, 			account, 				per1, 					per2, 					per3, 
				per4, 						per5, 					per6, 					per7, 					per8, 					per9, 					per10, 
				per11, 						per12, 					per13, 					audit_user, 			audit_date )
		VALUES ( 
				'".$site."', 				".$year.", 				'".$costcenter."', 		'".$account."',			".$ldc_po_amt[1].", 	".$ldc_po_amt[2].", 	".$ldc_po_amt[3].", 
				".$ldc_po_amt[4].", 		".$ldc_po_amt[5].", 	".$ldc_po_amt[6].", 	".$ldc_po_amt[7].",	 	".$ldc_po_amt[8].", 	".$ldc_po_amt[9].", 	".$ldc_po_amt[10].", 
				".$ldc_po_amt[11].", 		".$ldc_po_amt[12].", 	0, 						'".$emp_id."',		GetDate() )";

		$stmt = sqlsrv_query( $conn, $sql);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_po)";
			 $valid = false;
			 die( print_r( sqlsrv_errors(), true));
		}
			
	}else{
		switch ($trx_type) {
		  case 'ADD_AWA_PR':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_charge
			SET			per1 = per1 + ".$ldc_awa_pr_amt[1].", 
						per2 = per2 + ".$ldc_awa_pr_amt[2].", 
						per3 = per3 + ".$ldc_awa_pr_amt[3].",  
						per4 = per4 + ".$ldc_awa_pr_amt[4].", 
						per5 = per5 + ".$ldc_awa_pr_amt[5].", 
						per6 = per6 + ".$ldc_awa_pr_amt[6].", 
						per7 = per7 + ".$ldc_awa_pr_amt[7].", 
						per8 = per8 + ".$ldc_awa_pr_amt[8].", 
						per9 = per9 + ".$ldc_awa_pr_amt[9].", 
						per10 = per10 + ".$ldc_awa_pr_amt[10].", 
						per11 = per11 + ".$ldc_awa_pr_amt[11].", 
						per12 = per12 + ".$ldc_awa_pr_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_charge)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		  case 'MINUS_AWA_PR':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$ldc_awa_pr_amt[1].", 
						per2 = per2 + ".$ldc_awa_pr_amt[2].", 
						per3 = per3 + ".$ldc_awa_pr_amt[3].",  
						per4 = per4 + ".$ldc_awa_pr_amt[4].", 
						per5 = per5 + ".$ldc_awa_pr_amt[5].", 
						per6 = per6 + ".$ldc_awa_pr_amt[6].", 
						per7 = per7 + ".$ldc_awa_pr_amt[7].", 
						per8 = per8 + ".$ldc_awa_pr_amt[8].", 
						per9 = per9 + ".$ldc_awa_pr_amt[9].", 
						per10 = per10 + ".$ldc_awa_pr_amt[10].", 
						per11 = per11 + ".$ldc_awa_pr_amt[11].", 
						per12 = per12 + ".$ldc_awa_pr_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_credit)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		 case 'ADD_PR':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$ldc_pr_amt[1].", 
						per2 = per2 + ".$ldc_pr_amt[2].", 
						per3 = per3 + ".$ldc_pr_amt[3].",  
						per4 = per4 + ".$ldc_pr_amt[4].", 
						per5 = per5 + ".$ldc_pr_amt[5].", 
						per6 = per6 + ".$ldc_pr_amt[6].", 
						per7 = per7 + ".$ldc_pr_amt[7].", 
						per8 = per8 + ".$ldc_pr_amt[8].", 
						per9 = per9 + ".$ldc_pr_amt[9].", 
						per10 = per10 + ".$ldc_pr_amt[10].", 
						per11 = per11 + ".$ldc_pr_amt[11].", 
						per12 = per12 + ".$ldc_pr_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_credit)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		 case 'MINUS_PR':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$ldc_pr_amt[1].", 
						per2 = per2 + ".$ldc_pr_amt[2].", 
						per3 = per3 + ".$ldc_pr_amt[3].",  
						per4 = per4 + ".$ldc_pr_amt[4].", 
						per5 = per5 + ".$ldc_pr_amt[5].", 
						per6 = per6 + ".$ldc_pr_amt[6].", 
						per7 = per7 + ".$ldc_pr_amt[7].", 
						per8 = per8 + ".$ldc_pr_amt[8].", 
						per9 = per9 + ".$ldc_pr_amt[9].", 
						per10 = per10 + ".$ldc_pr_amt[10].", 
						per11 = per11 + ".$ldc_pr_amt[11].", 
						per12 = per12 + ".$ldc_pr_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_credit)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		 case 'ADD_PO':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$ldc_po_amt[1].", 
						per2 = per2 + ".$ldc_po_amt[2].", 
						per3 = per3 + ".$ldc_po_amt[3].",  
						per4 = per4 + ".$ldc_po_amt[4].", 
						per5 = per5 + ".$ldc_po_amt[5].", 
						per6 = per6 + ".$ldc_po_amt[6].", 
						per7 = per7 + ".$ldc_po_amt[7].", 
						per8 = per8 + ".$ldc_po_amt[8].", 
						per9 = per9 + ".$ldc_po_amt[9].", 
						per10 = per10 + ".$ldc_po_amt[10].", 
						per11 = per11 + ".$ldc_po_amt[11].", 
						per12 = per12 + ".$ldc_po_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_credit)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		 case 'MINUS_PO':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$ldc_po_amt[1].", 
						per2 = per2 + ".$ldc_po_amt[2].", 
						per3 = per3 + ".$ldc_po_amt[3].",  
						per4 = per4 + ".$ldc_po_amt[4].", 
						per5 = per5 + ".$ldc_po_amt[5].", 
						per6 = per6 + ".$ldc_po_amt[6].", 
						per7 = per7 + ".$ldc_po_amt[7].", 
						per8 = per8 + ".$ldc_po_amt[8].", 
						per9 = per9 + ".$ldc_po_amt[9].", 
						per10 = per10 + ".$ldc_po_amt[10].", 
						per11 = per11 + ".$ldc_po_amt[11].", 
						per12 = per12 + ".$ldc_po_amt[12]."
			WHERE	site_cd = '".$site."'
			AND		budget_year = ".$year."
			AND		costcenter = '".$costcenter."'
			AND		account = '".$account."'";

			$stmt = sqlsrv_query( $conn, $sql);				
							
			if( !$stmt ) {
				 $error_message = "Error update table (cf_budget_credit)";
				 $valid = false;
				 die( print_r( sqlsrv_errors(), true));
			}
			break;
		  default:

		}
	}	
	return $valid;
}

?>