<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');?>
<?php 
if (isset($_POST['oinvoice'])) {
  $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 order_list   WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$oinvoice'") or die(mysqli_error($conn));
  mysqli_query($conn,"DELETE FROM imei_tracker WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$oinvoice'") or die(mysqli_error($conn));
  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,"DELETE FROM tran_cheque  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$oinvoice'") or die(mysqli_error($conn));

  foreach (array_unique($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'") or die(mysqli_error($conn));
  }

}
?>