<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
	if (isset($_POST['ins_invoice'])) {
	    $ins_invoice    = $_POST['ins_invoice'];
	    $ins_acid       = $_POST['cr_acid'];
        $m_acid         = $_POST['dr_acid'];
        $ins_uid        = $_POST['ins_uid'];
	    $details        = $_POST['details'];
	    $m_invoice      = $_POST['m_invoice'];
	    $email          = $_POST['email'];
	    $sms            = $_POST['sms'];
	    $tdate          = $_POST['tdate'];
	    $d_acid         = $_POST['d_acid'];
	    $ins_discount   = $_POST['ins_discount']  ? $_POST['ins_discount'] : 0;

	    // array data here 
	    $ins_payment    = $_POST['ins_payment'];
	    $ins_id         = $_POST['ins_id'];

        $tran_amount=0;

    for ($i = 0; $i < count($ins_id); $i++) {
            $array_ins_payment  = $ins_payment[$i];
            $array_ins_id       = $ins_id[$i];

        if ($array_ins_payment >0  ) {
               mysqli_query($conn,"UPDATE intallment SET payment= (payment+'$array_ins_payment'),due_ins=(per_ins-payment), re_invoice='$ins_invoice', status= IF(due_ins > 0 , 'unpaid','paid') WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND insid ='$array_ins_id'") or die(mysqli_error($conn));

        $tran_amount +=$array_ins_payment;

        }
    }

    //  transtion 
        $actual_payment=$tran_amount-$ins_discount;

    $dr_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$m_acid' LIMIT 1"))['gcatid'];

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

    mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,m_invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
	        VALUES ('$coid','$bid','$brid','$m_acid', '$ins_acid', '$uid', '$dr_gcatid', '$cr_gcatid', '$ins_invoice','$m_invoice', 'INSR','$details','installment','$actual_payment','$tdate','$time','$ins_uid','$uid','1')") or die(mysqli_error($conn));

    // discount insert 


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

        mysqli_query($conn,"INSERT INTO transaction(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,m_invoice,ttype,details,note,amount,date,ptime,userid,em_id,active) 
              VALUES ('$coid','$bid','$brid','$d_acid', '$ins_acid', '$uid', '$d_gcatid', '$cr_gcatid', '$ins_invoice','$m_invoice', 'INSD',
              	'$details','installment Discount','$ins_discount','$tdate','$time','$ins_uid','$uid','1')") or die(mysqli_error($conn));

        $dr_balance = balance($d_acid)['ac_balance'];
             mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid='$d_acid'") or die(mysqli_error($conn));
    }


    $dr_balance = balance($m_acid)['ac_balance'];
         mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance' WHERE coid='$coid' AND acid ='$m_acid'") 
         or die(mysqli_error($conn));

    $cr_balance = balance($ins_acid)['ac_balance'];
        mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance' WHERE coid='$coid' AND acid ='$ins_acid'") 
        or die(mysqli_error($conn));

	}

?>
