<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');?>
<?php
if (isset($_POST['ins_invoice'])) {
	$c_acid             = $_POST['c_acid'];
	$ins_invoice        = $_POST['ins_invoice'];
	$ins_qty            = $_POST['ins_qty'];
	$ins_type           = $_POST['ins_type'];
	$service_name       = $_POST['service_name'];
	$reference_name     = $_POST['reference_name'];
	$reference_number   = $_POST['reference_number'];
	$reference_address  = $_POST['reference_address'];
	$ac_op_date         = $_POST['ac_op_date'];
	$first_ins_date     = $_POST['first_ins_date'];

	$custodmer_balance  = $_POST['ins_due_amount'];
	$moduls             =  ($custodmer_balance / $ins_qty);
	$perins_set         = floor($moduls);


if ($custodmer_balance >0  ) {
      $gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$c_acid' LIMIT 1"))['gcatid'];

      $op_rowget= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='opening' LIMIT 1"));
                    $cr_acid = $op_rowget['acid'];
                    $cr_gcatid = $op_rowget['gcatid'];

	  mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
	        VALUES ('$coid','$bid','$brid','$c_acid', '$cr_acid', '$uid', '$gcatid', '$cr_gcatid', '$ins_invoice', 'OPINS','$service_name','opening_INS','$custodmer_balance','$ac_op_date','$time','$uid','$us_em_id','1')") or die(mysqli_error($conn));


      $lastins =  $custodmer_balance - ($perins_set * $ins_qty);
      

		if ($ins_type =='Daily') {
			$incr = 'day';
		} else if($ins_type =='Weekly'){
	        $incr = 'week';
		} else if($ins_type =='Monthly'){
			$incr = 'month';
		}
      	
		for ($i=1; $i <= $ins_qty -1; $i++) { 
            $in = $i-1;
			$finsdate = date('Y-m-d', strtotime(+$in.$incr, strtotime($first_ins_date)));

      	    mysqli_query($conn,"INSERT INTO `intallment`(`coid`, `bid`, `brid`,`acid`,`ins_active_date`, `invoice`,`in_type`, `ins_qty`,`ins_qty_total`, `per_ins`,`due_ins`, `ins_type`, `ins_date`, `status`, `service_name`, `reference_name`, `reference_number`, `reference_address`)  VALUES ('$coid','$bid','$brid','$c_acid','$ac_op_date','$ins_invoice','OPINS', '$i','$ins_qty', '$perins_set','$perins_set', '$ins_type',(IF((1 < $i),'$finsdate','$first_ins_date')), 'unpaid','$service_name','$reference_name','$reference_number','$reference_address')") or die(mysqli_error($conn));
	  }

	  $date_set=date('Y-m-d', strtotime('+1'.$incr, strtotime($finsdate)));
	  $lastinsbalance= $lastins+$perins_set;

	  mysqli_query($conn,"INSERT INTO `intallment`(`coid`, `bid`, `brid`,`acid`,`ins_active_date`, `invoice`,`in_type`, `ins_qty`,`ins_qty_total`, `per_ins`,`due_ins`, `ins_type`, `ins_date`, `status`, `service_name`, `reference_name`, `reference_number`, `reference_address`)  VALUES ('$coid','$bid','$brid','$c_acid','$ac_op_date', '$ins_invoice','OPINS', '$i+1','$ins_qty', '$lastinsbalance','$lastinsbalance', '$ins_type', '$date_set', 'unpaid','$service_name','$reference_name','$reference_number','$reference_address')") or die(mysqli_error($conn));



	  // balance update

	    $tmp_dr = balance($c_acid)['ac_balance'];
	    $tmp_cr = balance($cr_acid)['ac_balance'];

	    $dr_balance  = (@$tmp_dr)?$tmp_dr:0;
	    $cr_balance  = (@$tmp_cr)?$tmp_cr:0;
	    
	    mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid ='$coid' AND acid ='$c_acid'") or die(mysqli_error($conn));
	    mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance' WHERE coid ='$coid' AND acid ='$cr_acid'") or die(mysqli_error($conn));

		$year = date('Y');
        $invoice_parts = explode('-', $ins_invoice);
        $prefix = $invoice_parts[0];
        mysqli_query($conn,"UPDATE voucher_no SET v_no=(v_no + 1) WHERE coid='$coid' AND v_type ='$prefix' AND v_year='$year'") or die(mysqli_error($conn));
	}
}
?>