<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
$institute_id = $_SESSION['LOGIN_INSTITUTE_ID'];
$branchid = $_SESSION['LOGIN_BRANCH'];
$entry_by=$_SESSION['LOGIN_USERID'];
$date_time=date("Y:m:d h:i:s");

extract($_GET);
$invoice = trim($invoice);
$discount = trim($discount);

//===================================================
$total_amount;
$amount;
$names;
$values;
$arr_names = explode(",", $names);
$arr_values = explode(",", $values);
$arr = count($arr_values);
$payment_date;
$payment_date = date("Y-m-d",strtotime($payment_date));
//exit();

//===================================================
$det= $obj->Details_By_Cond("_fee_details_invoice","_invoice_no='$invoice'");

// adjust Calculation
for($j=0;$j<$arr;$j++){
if($arr_values[$j]>0){
	$form_adjust=array(
		'_adjust_amount'=>$arr_values[$j],
		'_update_by'=>$_SESSION['LOGIN_USERID']
		);
	$fee_head = $arr_names[$j];
	$up_res = $obj->Update_Data("_fee_details_invoice",$form_adjust,"WHERE _fee_head='$fee_head' and  _invoice_no='$invoice'");
}
}


/*if($discount>0){
	$form_ins=array(
	'_br_id'=>$det['_br_id'],
    '_invoice_no'=>$invoice,
    '_month'=>$det['_month'],
    '_year'=>$det['_year'],
    '_class_id'=>$det['_class_id'],
    '_section_id'=>$det['_section_id'],
    '_std_id'=>$det['_std_id'],
    '_fee_head'=>'Discount',
    '_amount'=>$discount,
    '_type'=>'D',
    '_entry_by'=>$_SESSION['LOGIN_USERID'],
	'_entry_date'=>date("Y:m:d H:i:s")
	);
	$ins_res = $obj->Insert_Data("_fee_details_invoice", $form_ins);
}*/

//================================================
//if($names!='' AND $values>0){
/*for($i=0;$i<$arr;$i++){
if($arr_names[$i]!='' AND $arr_values[$i]>0){
$form_head=array(
	
	'_br_id'=>$det['_br_id'],
    '_invoice_no'=>$invoice,
    '_month'=>$det['_month'],
    '_year'=>$det['_year'],
    '_class_id'=>$det['_class_id'],
    '_section_id'=>$det['_section_id'],
    '_std_id'=>$det['_std_id'],
    '_fee_head'=>$arr_names[$i],
    '_amount'=>$arr_values[$i],
    '_type'=>'C',
    '_entry_by'=>$_SESSION['LOGIN_USERID'],
	'_entry_date'=>date("Y:m:d H:i:s")
	);
	$ins_head = $obj->Insert_Data("_fee_details_invoice", $form_head);
}
}*/

//================================================
$sec = $obj->Details_By_Cond("_fee_master_invoice","_invoice_no='$invoice'");
//$new =$amount - $sec['_due_amount'];
//$receivetk = $sec['_due_amount']-$discount+$new;
$receivetk = $amount-$discount;
$total_recv_tk = $sec['_paid_amount']+$receivetk;

$form_update=array(
    '_total_amount'=>$total_amount,
    '_due_amount'=>'0',
	'_discount'=>$discount,
    '_paid_amount'=>$total_recv_tk,
	'_payment_date'=>$payment_date,
    '_last_paid_amount'=>$receivetk,
	'_status'=>'P',
	'_update_by'=>$_SESSION['LOGIN_USERID']
	);

$up_res = $obj->Update_Data("_fee_master_invoice",$form_update,"WHERE _invoice_no='$invoice'");

if($up_res){
    $form_data_rec_total=array(
        '_br_id'=>$det['_br_id'],
        '_invoice_no'=>$invoice,
        '_month'=>$det['_month'],
        '_year'=>$det['_year'],
        '_class_id'=>$det['_class_id'],
        '_section_id'=>$det['_section_id'],
        '_std_id'=>$det['_std_id'],
        '_entry_by'=>$_SESSION['LOGIN_USERID'],
        '_entry_date'=>date("Y:m:d H:i:s"),   
        '_receive_amount'=>$receivetk,
		'_payment_date'=>$payment_date,
        '_update_by'=>$_SESSION['LOGIN_USERID']
        );

    $ins_resqqq = $obj->Insert_Data("_fee_total_receive", $form_data_rec_total);
	$det23 = $obj->View_column_details_By_Cond("_pf_std_personal_info","_contact_mobile","_id = '".$det['_std_id']."'");
	
	$mon_num = $det['_month'];
	$mon_name = $obj->Month($mon_num);
    $dis_mon_name = substr($mon_name, 0, 3);
	$dis_year = substr($det['_year'],-2);
	$dis_mon_name = $dis_mon_name.'-'.$dis_year;
	$smsbody = $_SESSION["LOGIN_INSTITUTE_NAME"].' - Dear guardian, TK. '.$receivetk.' ('.$dis_mon_name.') tuition fee has been receipt. Thank you. Details: smscu.edu.bd'; 
   
	//echo $smsbody;
   $send = $obj->Sms_Send($institute_id,$mod_id,"A",$det23['_contact_mobile'],$smsbody,"H",$entry_by,$date_time);


}



?>
