<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['itar_invoice'])){
   $invoice    = $_POST['itar_invoice'];
   $tdate      = $_POST['itardate'].' '.$time;
   $details    = $_POST['itransdetails'];
   $out_wh_id      = $_POST['out_wh_id'];
   $load_wh_id      = $_POST['load_wh_id'];


  $chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE coid='$coid' AND invoice='$invoice' LIMIT 1");
  if (mysqli_num_rows($chinvoice)>0) {}else {

    mysqli_query($conn,"INSERT INTO `order_list`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice` , `discount`, `dis_amount`, `amount`, `net_amount`, `t_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, `datetime`, `active`) SELECT `coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, '$out_wh_id', `pro_id`, `item_code`, `pro_type`, '-', (qty*unit_val), `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `discount`, `dis_amount`, `amount`, `net_amount`, `t_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, '$tdate', 1 FROM `order_list_edit` WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice'") or die(mysqli_error($conn));

        mysqli_query($conn,"INSERT INTO `order_list`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `item_code`, `pro_type`, `plus`, `qty`, `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice` , `discount`, `dis_amount`, `amount`, `net_amount`, `t_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, `datetime`, `active`) SELECT `coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, '$load_wh_id', `pro_id`, `item_code`, `pro_type`, '+', (qty*unit_val), `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `discount`, `dis_amount`, `amount`, `net_amount`, `t_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, '$tdate', 1 FROM `order_list_edit` WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice'") or die(mysqli_error($conn));

      mysqli_query($conn,"DELETE FROM order_list_edit WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice'") or die(mysqli_error($conn));


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

     $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"));
     $op_acid   = $op_row['acid'];
     $op_gcatid = $op_row['gcatid'];


      $stck= mysqli_query($conn,"SELECT pro_id,net_amount,plus FROM order_list WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice'") or die(mysqli_error($conn));
          while ($stc = mysqli_fetch_assoc($stck)) {
            $item_id  = $stc['pro_id'];
            $plus     = $stc['plus'];
            $item_net = $stc['net_amount'];

          if ($plus=='+') {

            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','$inv_acid', '$op_acid', '$uid', '$inv_gcatid', '$op_gcatid', '$invoice', 'ITRA','$details','eitransfer','$item_net','$tdate','$time','$uid','$uid','1')") or die(mysqli_error($conn));

          } elseif ($plus=='-') {

              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','$op_acid', '$inv_acid', '$uid', '$op_gcatid', '$inv_gcatid', '$invoice', 'ITRA','$details','eitransfer','$item_net','$tdate','$time','$uid','$uid','1')") or die(mysqli_error($conn));

          }

      }
      


        $tmp_inv = balance($inv_acid)['ac_balance'];
        $inv_acid_balance = (@$tmp_inv)?$tmp_inv:0;
        
        $tmp_op = balance($op_acid)['ac_balance'];
        $op_acid_balance = (@$tmp_op)?$tmp_op:0;
        
        mysqli_query($conn,"UPDATE accounts SET balance='$inv_acid_balance' WHERE coid='$coid' AND acid ='$inv_acid'") or die(mysqli_error($conn));
        mysqli_query($conn,"UPDATE accounts SET balance='$op_acid_balance' WHERE coid='$coid' AND acid ='$op_acid'") or die(mysqli_error($conn));        
            

    }
}
?>
