<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');?>
<?php 
if(isset($_POST['pur_invoice'])){
   $invoice    = $_POST['pur_invoice'];
   $tdate      = $_POST['tdate'].' '.$time;
   $s_acid     = $_POST['s_acid'];
   $f_acid     = $_POST['f_acid'];
   $d_acid     = $_POST['d_acid'];
   $discount   = $_POST['discount'];
   $f_value    = $_POST['f_value'];
   $m_acid     = $_POST['m_acid'];
   $Bank_check = $_POST['Bank_check'];
   $payment    = $_POST['payment'];
   $details    = $_POST['details'];
   $m_invoice  = $_POST['m_invoice'];
   $d_man      = $_POST['d_man'];
   $wh_id      = $_POST['wh_id'];
   $net_amount = $_POST['sm_totalc'];
   


$count_zero_qty_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM order_list_edit 
       WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' AND qty=0 "))['count_row'];
      if ($count_zero_qty_row=='0') {


        $chinvoice=mysqli_query($conn,"SELECT invoice FROM transaction WHERE coid='$coid' AND invoice='$invoice' LIMIT 1");
        if (mysqli_num_rows($chinvoice)>0 || $net_amount < 0.01) {}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`, `t_amount`, `net_amount`, `tax`, `profit`, `details`, `d_man`, `userid`, `datetime`, `active`) SELECT `coid`, `bid`, `brid`, '$s_acid', `invoice`, 'EPUR', '$wh_id', `pro_id`, `item_code`, `pro_type`, `plus`, (qty*unit_val), `pro_unit`, `unit_val`, `out_qty`, `run_qty`, `pu_price`, `avg_price`, `sale_price`, `change_slprice`, `discount`, `dis_amount`, `amount`, `t_amount`, `net_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 `imei_tracker`(`coid`, `bid`, `brid`, `acid`, `invoice`, `order_type`, `wh_id`, `pro_id`, `imei_code`, `imei_color`, `mfg_date`, `exp_date`, `plus`, `pu_price`, `sale_price`, `date`, `active`) SELECT `coid`, `bid`, `brid`, '$s_acid', `invoice`, 'EPUR', '$wh_id', `pro_id`, `imei_code`, `imei_color`,`mfg_date`,`exp_date`, `plus`, `pu_price`, `sale_price`, `date`, 1 FROM `imei_tracker_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'];
      $m_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'];

       $s_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$s_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','$inv_acid', '$s_acid', '$uid', '$inv_gcatid', '$s_gcatid', '$invoice', '$m_invoice', 'EPUR','$details','epurchase','$net_amount','$tdate','$time','$uid','$uid','1')") 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' AND active=0") or die(mysqli_error($conn));
      mysqli_query($conn,"ALTER TABLE order_list_edit AUTO_INCREMENT = 1") or die(mysqli_error($conn));

      
     if ($f_acid>0 && $f_value>0) {

          $f_gcatid= mysqli_fetch_assoc(mysqli_query($conn,"SELECT gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND acid='$f_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', '$f_acid','$m_acid', '$uid', '$f_gcatid','$m_gcatid', '$invoice', '$m_invoice', 'P','Freight Charge','epur_freight','$f_value','$tdate','$time','$uid','$uid','1')") or die(mysqli_error($conn));

        $tmp_f = balance($f_acid)['ac_balance'];
        $f_acid_balance = (@$tmp_f)?$tmp_f:0;
        mysqli_query($conn,"UPDATE accounts SET balance='$f_acid_balance' WHERE coid='$coid' AND acid ='$f_acid'") or die(mysqli_error($conn));
        }


        if ($d_acid>0 && $discount>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','$s_acid', '$d_acid', '$uid', '$s_gcatid', '$d_gcatid', '$invoice', '$m_invoice', 'P','Purchase Discount','epur_discount','$discount','$tdate','$time','$uid','$uid','1')") or die(mysqli_error($conn));

        $tmp_d = balance($d_acid)['ac_balance'];
        $d_acid_balance = (@$tmp_d)?$tmp_d:0;
        
        mysqli_query($conn,"UPDATE accounts SET balance='$d_acid_balance' WHERE coid='$coid' AND acid ='$d_acid'") or die(mysqli_error($conn));
        }


       if ($m_acid !=='NaN' && $payment>0  ) {
  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','$s_acid', '$m_acid', '$uid', '$s_gcatid', '$m_gcatid', '$invoice', '$m_invoice', 'P','Purchase Payment','epur_payment','$payment','$tdate','$time','$uid','$uid','1')") or die(mysqli_error($conn));

         $tmp_m = balance($m_acid)['ac_balance'];
        $m_acid_balance = (@$tmp_m)?$tmp_m:0;
        mysqli_query($conn,"UPDATE accounts SET balance='$m_acid_balance' WHERE coid='$coid' AND acid ='$m_acid'") or die(mysqli_error($conn));
}else{

mysqli_query($conn,"INSERT INTO tran_cheque(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','$s_acid', '', '$uid', '$s_gcatid', '', '$invoice', '$m_invoice', 'P','Purchase Cheque Payment','$Bank_check','$payment','$tdate','$time','$uid','$uid','0')") or die(mysqli_error($conn));
}


    


        $tmp_inv = balance($inv_acid)['ac_balance'];
        $inv_acid_balance = (@$tmp_inv)?$tmp_inv:0;
        
        $tmp_s = balance($s_acid)['ac_balance'];
        $s_acid_balance = (@$tmp_s)?$tmp_s: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='$s_acid_balance' WHERE coid='$coid' AND acid ='$s_acid'") or die(mysqli_error($conn));        

        $year = date('Y');
        $invoice_parts = explode('-', $invoice);
        $prefix = $invoice_parts[0];
        mysqli_query($conn,"UPDATE voucher_no SET v_no=(v_no + 1) WHERE coid='$coid' AND v_type ='$prefix' AND v_year='$year'") or die(mysqli_error($conn));        

        if (isset($_POST['email'])) {
            // Email Part Here
        }
        
        //
       $prebalance= $s_acid_balance-$payment;
     $PhoneNumber = mysqli_fetch_array(mysqli_query($conn,"SELECT ac_number FROM accounts WHERE  acid='$s_acid' LIMIT 1 "))['ac_number'];
     $smstext= mysqli_fetch_assoc(mysqli_query($conn,"SELECT smstext FROM sms_tamp_all WHERE  status=1 AND type='EPUR' AND coid='$coid' AND bid='$bid' AND brid='$brid' LIMIT 1 "))['smstext'];  
   
          if (isset($_POST['sms'])){
            $smssend=   $_POST['sms'];
            if ($smssend =='yes') {
                $content_val   = [$net_amount,$discount,$prebalance,$payment,$s_acid_balance];
                $content_var   = ["%invoice%","%discount%","%prebalance%","%payment%","%lastbalance%"]; 
                $newmsg = str_replace($content_var, $content_val, $smstext);
                $msgmsg = urlencode($newmsg);
                require_once('../sms/api_key_access.php');
               } else { echo "Not chexkbox"; }
           }

     //


    }
    
   }
}
?>
