<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['edit_tid'])){
   $edit_tid       = $_POST['edit_tid'];
   $dr_old_acid    = $_POST['dr_old_acid'];
   $cr_old_acid    = $_POST['cr_old_acid'];
   $old_amount     = $_POST['old_amount'];
   $edit_acid      = $_POST['edit_acid'];
   $ttype          = $_POST['ttype'];
   $edit_amount    = $_POST['edit_amount'];
   $edit_details   = $_POST['edit_details'];
   $edit_tdate     = $_POST['edit_tdate'].' '.$time;

	if ($edit_amount > 0.01) {

      $op_row= 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"));

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

      if ($ttype == '+') {
        $dr_edit_acid = $edit_acid;
        $cr_edit_acid = $op_row['acid'];

        $dr_gcatid = $gcatid;
        $cr_gcatid = $op_row['gcatid'];
      } else {
        $dr_edit_acid = $op_row['acid'];
        $cr_edit_acid = $edit_acid;

        $dr_gcatid = $op_row['gcatid'];
        $cr_gcatid = $gcatid;
      } 

    mysqli_query($conn,"INSERT INTO transaction_edit(coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active,edit,e_userid) 
          SELECT coid,bid,brid,dr_acid,cr_acid,e_acid,dr_gcatid,cr_gcatid,invoice,ttype,details,note,amount,date,ptime,userid,em_id,active,edit+1,'$uid' FROM transaction WHERE coid='$coid' AND tid='$edit_tid'") or die(mysqli_error($conn));

     mysqli_query($conn,"UPDATE transaction SET dr_acid='$dr_edit_acid',cr_acid='$cr_edit_acid',dr_gcatid='$dr_gcatid',cr_gcatid='$cr_gcatid',details='$edit_details',amount='$edit_amount',date='$edit_tdate',ptime='$time',edit=edit+1 WHERE coid='$coid' AND tid ='$edit_tid'") or die(mysqli_error($conn));  

    if ($dr_old_acid !== $dr_edit_acid || $cr_old_acid !== $cr_edit_acid || $old_amount !== $edit_amount) {

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

        if ($dr_old_acid !== $dr_edit_acid) {
            $dr_old_balance = balance($dr_old_acid)['ac_balance'];
            mysqli_query($conn,"UPDATE accounts SET balance='$dr_old_balance' WHERE coid='$coid' AND acid ='$dr_old_acid'") or die(mysqli_error($conn));
        }

        if ($cr_old_acid !== $cr_edit_acid) {
            $cr_old_balance = balance($cr_old_acid)['ac_balance'];
            mysqli_query($conn,"UPDATE accounts SET balance='$cr_old_balance' WHERE coid='$coid' AND acid ='$cr_old_acid'") or die(mysqli_error($conn));
        }        
			}

        if (isset($_POST['edit_email'])) {
        	// Email Part Here
        }

        if (isset($_POST['edit_sms'])) {
        	// SMS Part Here
        }
    }


	}
?>