<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
$branchid = $_SESSION['LOGIN_BRANCH'];
extract($_GET);
extract($_POST);
?>
<?php

if($operation=='rollback'){
	$chk_status = $obj->Details_By_Cond("_fee_master_invoice","_invoice_no='$invoice_no'");
	$status = $chk_status['_status'];
	$total_amount = $chk_status['_total_amount'];
	$discount_amount = $chk_status['_discount'];
	if($status=='P'){
		$form_data1 = array(
			'_due_amount'=>$total_amount,
			'_paid_amount'=>0,
			'_last_paid_amount'=>0,
			'_discount'=>0,
			'_payment_date'=>'0000-00-00',
			'_payment_batch_no'=>0,
			'_status'=>'U',
			'_update_by'=>$_SESSION['LOGIN_USERID']
		);
		
		foreach($obj->View_All_By_Cond("_fee_details_invoice","_invoice_no='$invoice_no' AND _type='C'") AS $fee_det_info){
			extract($fee_det_info);
			$fee_det_id = $fee_det_info['_id'];
			$form_data_to_reset_ad_am = array(
											'_adjust_amount'=>0,
											'_update_by'=>$_SESSION['LOGIN_USERID']
											);
			$status_of_reset_ad_am = $obj->Update_Data("_fee_details_invoice", $form_data_to_reset_ad_am,"WHERE _id='$fee_det_id'");
		}
		$update= $obj->Delete_Data("_fee_total_receive","_invoice_no='$invoice_no'");
		$update= $obj->Delete_Data("_fee_partial_pay_table","_invoice='$invoice_no'");
		$update= $obj->Update_Data("_fee_master_invoice", $form_data1,"WHERE _invoice_no='$invoice_no'");
	}
	if($status=='PR'){
		$form_data1 = array(
			'_due_amount'=>$total_amount,
			'_paid_amount'=>0,
			'_discount'=>0,
			'_last_paid_amount'=>0,
			'_payment_date'=>'0000-00-00',
			'_payment_batch_no'=>0,
			'_status'=>'U',
			'_update_by'=>$_SESSION['LOGIN_USERID']
		);
		foreach($obj->View_All_By_Cond("_fee_details_invoice","_invoice_no='$invoice_no' AND _type='C'") AS $fee_det_info){
			extract($fee_det_info);
			$fee_det_id = $fee_det_info['_id'];
			$form_data_to_reset_ad_am = array(
											'_adjust_amount'=>0,
											'_update_by'=>$_SESSION['LOGIN_USERID']
											);
			$status_of_reset_ad_am = $obj->Update_Data("_fee_details_invoice", $form_data_to_reset_ad_am,"WHERE _id='$fee_det_id'");
		}
		$update= $obj->Delete_Data("_fee_total_receive","_invoice_no='$invoice_no'");
		$update= $obj->Delete_Data("_fee_partial_pay_table","_invoice='$invoice_no'");
		$update= $obj->Update_Data("_fee_master_invoice", $form_data1,"WHERE _invoice_no='$invoice_no'");
	}
		
	if($update){
		$result = "Invoice Rollback Successfully!!!";
	}
	else{
		$result = "Rollback Failed. Please Try Again.";
	}
	echo $result;
}
if($operation=='delete_unpaid_invoice'){
	$result = '';
	$chk_status = $obj->Details_By_Cond("_fee_master_invoice","_invoice_no='$invoice_no'");
	$status = $chk_status['_status'];
	if($status=='U'){
		foreach($obj->View_colmn_By_Cond("_fee_details_invoice","*","_invoice_no='$invoice_no'") AS $inv_det_info){
			extract($inv_det_info);
			$fee_head 	 = $inv_det_info['_fee_head'];
			$head_amount = $inv_det_info['_amount'];
			$fee_type 	 = $inv_det_info['_fee_type'];
			$type 	 	 = $inv_det_info['_type'];
			$entry_by 	 = $inv_det_info['_entry_by'];
			$entry_date  = $inv_det_info['_entry_date'];
			$month 				= $inv_det_info['_month'];
			$year 				= $inv_det_info['_year'];
			$std_id 			= $inv_det_info['_std_id'];
			$last_payment_date 	= $inv_det_info['_last_payment_date'];
			$class_id 			= $inv_det_info['_class_id'];
			$section_id 		= $inv_det_info['_section_id'];
			$branch_id 			= $inv_det_info['_br_id'];
			$invoiceno 			= $inv_det_info['_invoice_no'];
			
			$form_data4=array(
				'_br_id'=>$branch_id,
				'_month'=>$month,
				'_year'=>$year,
				'_class_id'=>$class_id,
				'_section_id'=>$section_id,
				'_std_id'=>$std_id,
				'_fee_head'=>$fee_head,
				'_amount'=>$head_amount,
				'_invoice_no'=>$invoiceno,
				'_type'=>$type,
				'_last_payment_date'=>$last_payment_date,
				'_fee_type'=>$fee_type,
				'_entry_by'=>$entry_by,
				'_entry_date'=>$entry_date
			);
			$res1 = $obj->Insert_Data_By_Cond("_fee_details_invoice_del_log",$form_data4,"_br_id='$branch_id' AND _month='$month' AND _year='$year' AND _section_id='$section_id' AND _std_id='$std_id' AND _fee_head='$fee_head'");
		}
		$del_inv_det = $obj->Delete_Data("_fee_details_invoice","_invoice_no='$invoice_no'");
		if($del_inv_det){
			$del_inv_mas= $obj->Delete_Data("_fee_master_invoice","_invoice_no='$invoice_no'");
			if($del_inv_mas){
				$result = 'Invoice Deleted Successfully!!!';
			}
			else{
				$result = 'Invoice Could not be deleted. Please try again!';
			}
		}
		else{
			$result = 'Invoice Could not be deleted. Please try again!';
		}
	}else{
		$result = 'Only unpaid invoice can be deleted';
	}
	echo $result;
}
/*if($operation=='delete'){

 	$chk_status = $obj->Details_By_Cond("_fee_master_invoice","_invoice_no='$invoice_no'");
	$status = $chk_status['_status'];
	
	
	if($status=='U'){
		foreach($obj->View_All_By_Cond("_fee_details_invoice","_invoice_no='$invoice_no'") as $chk_status2){
		 extract($chk_status2);
		$pre_invoice = $chk_status2['_pre_invoice'];
		$fee_head 	 = $chk_status2['_fee_head'];
		$head_amount = $chk_status2['_amount'];
		$fee_type 	 = $chk_status2['_fee_type'];
		$type 	 	 = $chk_status2['_type'];
		$entry_by 	 = $chk_status2['_entry_by'];
		$entry_date  = $chk_status2['_entry_date'];
		$month 				= $chk_status2['_month'];
		$year 				= $chk_status2['_year'];
		$std_id 			= $chk_status2['_std_id'];
		$last_payment_date 	= $chk_status2['_last_payment_date'];
		$class_id 			= $chk_status2['_class_id'];
		$section_id 		= $chk_status2['_section_id'];
		$branch_id 			= $chk_status2['_br_id'];
		$invoiceno 			= $chk_status2['_invoice_no'];
		
		
		if($pre_invoice==''){
			$form_data = array(
			'_status'=>'DEL',
			'_update_by'=>$_SESSION['LOGIN_USERID']
			);
			$form_data3 = array(
			'_type'=>'DEL',
			'_update_by'=>$_SESSION['LOGIN_USERID']
			);
			
			$form_data4=array(
					'_br_id'=>$branch_id,
					'_month'=>$month,
					'_year'=>$year,
					'_class_id'=>$class_id,
					'_section_id'=>$section_id,
					'_std_id'=>$std_id,
					'_fee_head'=>$fee_head,
					'_amount'=>$head_amount,
					'_invoice_no'=>$invoiceno,
					'_type'=>$type,
					'_last_payment_date'=>$last_payment_date,
					'_fee_type'=>$fee_type,
					'_entry_by'=>$entry_by,
					'_entry_date'=>$entry_date
					);
			$res1 = $obj->Insert_Data_By_Cond("_fee_details_invoice_del_log",$form_data4,"_br_id='$branch_id' AND _month='$month' AND _year='$year' AND _section_id='$section_id' AND _std_id='$std_id' AND _fee_head='$fee_head'");
				
			$delete=$obj->Delete_Data("_fee_details_invoice","_invoice_no='$invoice_no'");
			
			$delete=$obj->Delete_Data("_fee_master_invoice","_invoice_no='$invoice_no'");
				//$update= $obj->Update_Data("_fee_master_invoice", $form_data,"WHERE _invoice_no='$invoice_no'");
				//$update= $obj->Update_Data("_fee_details_invoice", $form_data3,"WHERE _invoice_no='$invoice_no'");
			//echo $result =$pre_invoice;
		}else{
		
			$form_data1 = array(
			'_status'=>'U',
			'_update_by'=>$_SESSION['LOGIN_USERID']
			);
			
			$form_data = array(
			'_status'=>'DEL',
			'_update_by'=>$_SESSION['LOGIN_USERID']
			);
			$form_data3 = array(
			'_type'=>'DEL',
			'_update_by'=>$_SESSION['LOGIN_USERID']
			);
			$form_data4=array(
						'_br_id'=>$branch_id,
						'_month'=>$month,
						'_year'=>$year,
						'_class_id'=>$class_id,
						'_section_id'=>$section_id,
						'_std_id'=>$std_id,
						'_fee_head'=>$fee_head,
						'_amount'=>$head_amount,
						'_invoice_no'=>$invoice_no_master,
						'_type'=>$type,
						'_last_payment_date'=>$last_payment_date,
						'_fee_type'=>$fee_type,
						'_entry_by'=>$entry_by,
						'_entry_date'=>$entry_date
						);
			$res1 = $obj->Insert_Data_By_Cond("_fee_details_invoice_del_log",$form_data4,"_br_id='$branch_id' AND _month='$month' AND _year='$year' AND _section_id='$section_id' AND _std_id='$std_id' AND _fee_head='$fee_head'");
			
			$delete=$obj->Delete_Data("_fee_details_invoice","_invoice_no='$invoice_no'");
			
			$delete=$obj->Delete_Data("_fee_master_invoice","_invoice_no='$invoice_no'");
			
			$update= $obj->Update_Data("_fee_master_invoice", $form_data1,"WHERE _invoice_no='$pre_invoice'");
		}
	 }
	}
	if($delete){
		$result = "Invoice Deleted Successfully!!!";
			
	}
	else{
		$result = "Deleted Failed. Please Try Again.";
	}
	echo $result;
	
}*/
?>


<?php
if($oper=='update'){
	$form_data = array(
		'_br_id'=>$branchid,
		'_fee_head'=>$e_fee_head,
		'_amount'=>$e_amount,
		'_purpuse'=>$e_purpuse,
		'_update_by'=>$_SESSION['LOGIN_USERID']
		
		);

  $update= $obj->Update_Data("_fee_details_invoice", $form_data,"WHERE _id='$id'");

  if($update){
				echo "invoice Updated";
			}
			else{
				echo "Update Failed";
			}

}

?>
