<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php'); ?>
<?php 
if(isset($_POST['gpur_invoice'])){
   $invoice    = $_POST['gpur_invoice'];
   $tdate      = $_POST['tdate'].' '.$time;

   $s_acid     = $_POST['s_acid'];
   $d_acid     = $_POST['d_acid'];
   $d_amount   = $_POST['d_amount'];
   $m_acid     = $_POST['m_acid'];
   $m_amount   = $_POST['m_amount'];

   $project    = isset($_POST['project'])?$_POST['project']:0;
   $p_discount = $_POST['p_discount'];
   $p_price    = $_POST['p_price'];
   $amount     = array_sum($p_price);
   $p_details  = $_POST['p_details'];


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

    for ($i = 0; $i < count($p_price); $i++)  {
       $inp_discount = $p_discount[$i];
       $inp_price    = $p_price[$i];
       $inp_details  = $p_details[$i];

      mysqli_query($conn,"INSERT INTO `g_order_list`(`coid`, `bid`, `brid`, `g_invoice`, `g_type`, `g_details`, `g_discount`, `g_amount`, `g_project`, `g_date`, `g_active`) VALUES ('$coid','$bid','$brid','$invoice','GPUR','$inp_details','$inp_discount','$inp_price','$project','$tdate','1')") or die(mysqli_error($conn));
     }

      $pu_row= mysqli_fetch_assoc(mysqli_query($conn,"SELECT acid,gcatid FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='purchase' LIMIT 1"));
      $p_acid   = $pu_row['acid'];
      $p_gcatid = $pu_row['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,ttype,details,note,amount,date,ptime,userid,em_id,project,active) 
          VALUES ('$coid','$bid','$brid','$p_acid', '$s_acid', '$uid', '$p_gcatid', '$s_gcatid', '$invoice', 'GPUR','General Purchase','gpurchase','$amount','$tdate','$time','$uid','$us_em_id','$project','1')") or die(mysqli_error($conn));


        if ($d_acid>0 && $d_amount>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,ttype,details,note,amount,date,ptime,userid,em_id,project,active) 
          VALUES ('$coid','$bid','$brid','$s_acid', '$d_acid', '$uid', '$s_gcatid', '$d_gcatid', '$invoice', 'P','Purchase Discount','gpurchase','$d_amount','$tdate','$time','$uid','$us_em_id','$project','1')") or die(mysqli_error($conn));

        $d_acid_balance = balance($d_acid)['ac_balance'];
        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>0 && $m_amount>0) {

          $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'];

           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,project,active) 
          VALUES ('$coid','$bid','$brid','$s_acid', '$m_acid', '$uid', '$s_gcatid', '$m_gcatid', '$invoice', 'P','Purchase Payment','gpurchase','$m_amount','$tdate','$time','$uid','$us_em_id','$project','1')") or die(mysqli_error($conn));

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

        }

        $p_acid_balance = balance($p_acid)['ac_balance'];
        $s_acid_balance = balance($s_acid)['ac_balance'];
        mysqli_query($conn,"UPDATE accounts SET balance='$p_acid_balance' WHERE coid='$coid' AND acid ='$p_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));        
			
        if (isset($_POST['email'])) {
        	// Email Part Here
        }

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


	}
}
?>
