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

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

function f_update_budget($site, $trx_date, $costcenter, $account, $trx_type, $ar_amount, $conn, $emp_id){
	$year;
	$count;
	$period;
	$charge_amt = array(12);
	$credit_amt = array(12);
	$valid = true;
	
	$costcenter = trim((string)($costcenter ?? ''));
	$account    = trim((string)($account ?? ''));

	// 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."'";

	$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 == "CHARGE") {
				$charge_amt[$li_cnt] = $ar_amount;
				$credit_amt[$li_cnt] = 0;
				
			}
			
			if ($trx_type == "CREDIT") {
				$charge_amt[$li_cnt] = 0;
				$credit_amt[$li_cnt] = $ar_amount;
			}
		}else{
			
			$charge_amt[$li_cnt] = 0;
			$credit_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 ( 
				?, 					?, 						?, 						?,					0, 				0, 				0, 
				0, 					0, 						0, 						0, 					0, 				0, 				0, 
				0, 					0, 						0, 						?,		GetDate() )";

		$params_cf_budget = array($site,$year,$costcenter,$account,$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget);				
						
		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 ( 
			?, 	 					?, 	 					?, 						?, 	 				?, 	 				?, 					?, 	  
			?, 	 					?, 	 					?, 	 					?, 					?, 					?, 	 				?, 	 
			?, 						?, 	 					0, 						?, 					GetDate() )";


		$params_cf_budget_charge = array($site,$year,$costcenter,$account,$charge_amt[1],$charge_amt[2],$charge_amt[3],$charge_amt[4],$charge_amt[5],$charge_amt[6],$charge_amt[7],$charge_amt[8],$charge_amt[9],$charge_amt[10],
		$charge_amt[11],$charge_amt[12],$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget_charge);				
						
		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 ( 
			?, 	 					?, 	 					?, 						?, 	 				?, 	 				?, 					?, 	  
			?, 	 					?, 	 					?, 	 					?, 					?, 	 				?, 	 				?, 	 
			?, 						?, 	 					0, 						?, 			GetDate() )";


		$params_cf_budget_credit = array($site,$year,$costcenter,$account,$credit_amt[1],$credit_amt[2],$credit_amt[3],$credit_amt[4],$credit_amt[5],$credit_amt[6],$credit_amt[7],$credit_amt[8],$credit_amt[9],$credit_amt[10],
		$credit_amt[11],$credit_amt[12],$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget_credit);				
						
		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 ( 
				?, 					?, 						?,						?,					0, 				0, 				0, 
				0, 					0, 						0, 						0, 					0, 				0, 				0, 
				0, 					0, 						0, 						?,	GetDate() )";


		$params_cf_budget_awa_pr = array($site,$year,$costcenter,$account,$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget_awa_pr);				
						
		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 ( 
				?, 					?, 						?, 						?,					0, 				0, 				0, 
				0, 					0, 						0, 						0, 					0, 				0, 				0, 
				0, 					0, 						0, 						?,		GetDate() )";

		$params_cf_budget_pr = array($site,$year,$costcenter,$account,$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget_pr);				
						
		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 ( 
				?, 					?,						?, 						?,					0, 				0, 				0, 
				0, 					0, 						0, 						0, 					0, 				0, 				0, 
				0, 					0, 						0, 						?,		GetDate() )";

		$params_cf_budget_po = array($site,$year,$costcenter,$account,$emp_id);
		$stmt = sqlsrv_query( $conn, $sql,$params_cf_budget_po);				
						
		if( !$stmt ) {
			 $error_message = "Error insert table (cf_budget_po)";
			 $valid = false;
			 die( print_r( sqlsrv_errors(), true));
		}
			
	}else{
		switch ($trx_type) {
		  case 'CHARGE':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_charge
			SET			per1 = per1 + ".$charge_amt[1].", 
						per2 = per2 + ".$charge_amt[2].", 
						per3 = per3 + ".$charge_amt[3].",  
						per4 = per4 + ".$charge_amt[4].", 
						per5 = per5 + ".$charge_amt[5].", 
						per6 = per6 + ".$charge_amt[6].", 
						per7 = per7 + ".$charge_amt[7].", 
						per8 = per8 + ".$charge_amt[8].", 
						per9 = per9 + ".$charge_amt[9].", 
						per10 = per10 + ".$charge_amt[10].", 
						per11 = per11 + ".$charge_amt[11].", 
						per12 = per12 + ".$charge_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 'CREDIT':
			//cf_budget_credit
			$sql = "UPDATE 	cf_budget_credit
			SET		per1 = per1 + ".$credit_amt[1].", 
						per2 = per2 + ".$credit_amt[2].", 
						per3 = per3 + ".$credit_amt[3].",  
						per4 = per4 + ".$credit_amt[4].", 
						per5 = per5 + ".$credit_amt[5].", 
						per6 = per6 + ".$credit_amt[6].", 
						per7 = per7 + ".$credit_amt[7].", 
						per8 = per8 + ".$credit_amt[8].", 
						per9 = per9 + ".$credit_amt[9].", 
						per10 = per10 + ".$credit_amt[10].", 
						per11 = per11 + ".$credit_amt[11].", 
						per12 = per12 + ".$credit_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;
}

?>