<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
 if(isset($_POST['tid'])){
 	$tid     = $_POST['tid'];
 	$dr_acid = $_POST['dr_acid'];
 	$cr_acid = $_POST['cr_acid'];

    mysqli_query($conn,"UPDATE transaction SET active=0 WHERE coid ='$coid' AND tid='$tid' LIMIT 1") or die(mysqli_error($conn));

    $tmp_dr = balance($dr_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 ='$dr_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));
 }
?>



<?php 
 if(isset($_POST['opins_invoice'])){
	$opins_invoice     = $_POST['opins_invoice'];
	$installment_query = mysqli_query($conn, "SELECT insid FROM intallment WHERE coid='$coid' AND invoice='$opins_invoice' AND payment >0 LIMIT 1");
	$count = mysqli_num_rows($installment_query);
	if ( $count < 1 ) {
        $acqur = mysqli_query($conn, "SELECT dr_acid,cr_acid FROM transaction WHERE coid='$coid' AND invoice='$opins_invoice' LIMIT 1") or die(mysqli_error($conn));
        $acrr = mysqli_fetch_assoc($acqur);
        $dr_acid = $acrr['dr_acid'];
 	    $cr_acid = $acrr['cr_acid'];

	    mysqli_query($conn,"DELETE FROM transaction WHERE invoice='$opins_invoice' AND coid='$coid'") or die(mysqli_error($conn));
	    mysqli_query($conn,"DELETE FROM intallment WHERE invoice='$opins_invoice' AND coid='$coid'") or die(mysqli_error($conn));
	                 
	    $tmp_dr = balance($dr_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 ='$dr_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));
    }
 }
 ?>
<?php 
 if(isset($_POST['rece_invoice'])){
	$rece_invoice     = $_POST['rece_invoice'];

        $acRECqur = mysqli_query($conn, "SELECT dr_acid,cr_acid FROM transaction WHERE coid='$coid' AND ttype='R' AND invoice='$rece_invoice'") or die(mysqli_error($conn));

        $ac_rec = mysqli_fetch_assoc($acRECqur);
        $dr_acid = $ac_rec['dr_acid'];
 	    $cr_acid = $ac_rec['cr_acid'];

      $acDISqur = mysqli_query($conn, "SELECT dr_acid,cr_acid FROM transaction WHERE coid='$coid' AND ttype='RD' AND invoice='$rece_invoice'") or die(mysqli_error($conn));
        $ac_dis = mysqli_fetch_assoc($acDISqur);
        $dr_acid_dis = $ac_dis['dr_acid'];
 	     $cr_acid_dis = $ac_dis['cr_acid'];


	    mysqli_query($conn,"DELETE FROM transaction WHERE invoice='$rece_invoice' AND ttype='R' AND coid='$coid'") or die(mysqli_error($conn));

	    mysqli_query($conn,"DELETE FROM transaction WHERE invoice='$rece_invoice' AND ttype='RD' AND coid='$coid'") or die(mysqli_error($conn));

	                 
	    $tmp_dr = balance($dr_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 ='$dr_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));


      $tmp_dr_dis = balance($dr_acid_dis)['ac_balance'];
	    $tmp_cr_dis = balance($cr_acid_dis)['ac_balance'];

	    $dr_balance_dis  = (@$tmp_dr_dis)?$tmp_dr_dis:0;
	    $cr_balance_dis  = (@$tmp_cr_dis)?$tmp_cr_dis:0;
	    
	    mysqli_query($conn,"UPDATE accounts SET balance='$dr_balance_dis' WHERE coid ='$coid' AND acid ='$dr_acid_dis'") or die(mysqli_error($conn));
	    mysqli_query($conn,"UPDATE accounts SET balance='$cr_balance_dis' WHERE coid ='$coid' AND acid ='$cr_acid_dis'") or die(mysqli_error($conn));

    }
 
 ?>


<?php
$oinvoice = $_POST['oinvoice'];

$puinv = mysqli_query($conn, "SELECT dr_acid,cr_acid FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$oinvoice'") or die(mysqli_error($conn));
$stacid = [];
while ($stc = mysqli_fetch_assoc($puinv)) {
	array_push($stacid, $stc['dr_acid'], $stc['cr_acid']);
}

mysqli_query($conn, "DELETE FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$oinvoice'") or die(mysqli_error($conn));
mysqli_query($conn, "UPDATE intallment SET re_invoice=' ', due_ins=payment,payment=0 , status='unpaid' WHERE  re_invoice='$oinvoice'  AND coid='$coid' ") or die(mysqli_error($conn));



foreach (array_filter($stacid) as $acid) {
	$tmp_bal = balance($acid)['ac_balance'];
	$acid_balance = (@$tmp_bal) ? $tmp_bal : 0;
	mysqli_query($conn, "UPDATE accounts SET balance='$acid_balance' WHERE coid='$coid' AND acid ='$acid' LIMIT 1") or die(mysqli_error($conn));
}

?>



