<?php
//user_action.php
include('db_con.php');
if (isset($_SESSION['user_id'])) {
		$user_type = $_SESSION['user_type'];
		$user_id = $_SESSION['user_id'];
		$user_name = $_SESSION['user_name'];
		$branch_id = $_SESSION['branch_id'];
		$branch_name = $_SESSION['branch_name'];
		$store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action']=='save')
	{
		 $new_dated = $_POST['dated'];
             if($user_type=='user')
             {
            $sql = "select app_value from app_options where app_name='datedown' and user_id = '".$user_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $datedown = $statement->fetchColumn();
            $sql = "select app_value from app_options where app_name='dateup' and user_id = '".$user_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $dateup = $statement->fetchColumn();
             $sql = "SELECT date_sub(curdate(),interval '".$datedown."' day)";
              $statement = $connect->prepare($sql);
              $statement->execute(); // no need to add `$sql` here, you can take that out
              $pre_dated = $statement->fetchColumn();
    if($new_dated < $pre_dated){echo 'Invoice Can not be save in Previous date pre_dated is : '.$pre_dated .' Post Date : '.$new_dated ;return false;}
     $above_dated = Date('Y-m-d', strtotime("+".$dateup .' days'));
    if($new_dated > $above_dated){echo 'Invoice Can not be save in Above date Above dated is :'.$above_dated .' New Date is '. $new_dated;return false;}
        }
    $due_day = 0;
   
    $due_date =Date('Y-m-d', strtotime("+".$due_day." days"));
   if($due_day==0){$due_date=$new_dated;}
   if($due_day==''){$due_date=$new_dated;}
    
    $statement = $connect->prepare("
     INSERT INTO pr
        (po_no,branch_id,inv_no,dated,due_date,v_id,sv_id,description,discount,discount_dsc,inv_amt,due_day,freight,freight_dsc,gst,gst_pr,user_id,status)
        VALUES (:po_no, :branch_id, :inv_no, :dated, :due_date, :v_id, :sv_id, :description, :discount, :discount_dsc, :inv_amt, :due_day, :freight, :freight_dsc, :gst, :gst_pr, :user_id, :status)
    ");
    $statement->execute(
      array(
         ':po_no'             =>  0,
          ':branch_id'             =>  $branch_id,
          ':inv_no'             =>  trim($_POST["inv_no"]),
          ':dated'             =>  $_POST["dated"],
       ':due_date'             =>  $due_date,
          ':v_id'          =>  trim($_POST["v_id"]),
          ':sv_id'          =>  0,
          ':description'       =>  $_POST["description"],
          ':discount'       =>  trim($_POST["discount"]),
          ':discount_dsc'       =>  trim($_POST["discount_dsc"]),
          ':inv_amt'       =>  0,
          ':due_day'       =>  0,          
          ':freight'       =>  trim($_POST["freight"]),
          ':freight_dsc'       =>  trim($_POST["freight_dsc"]),
          ':gst'       =>  trim($_POST["gst"]),
          ':gst_pr'       =>  trim($_POST["gst_pr"]),
          ':user_id'       =>  $user_id,          
          ':status'       =>  'Active'
          
      )
    );
      
        $statement = $connect->query("SELECT LAST_INSERT_ID()");
         $pr_id = $statement->fetchColumn();
          $sql = "delete from p_detail";
          $statement = $connect->prepare($sql);
          $statement->execute();
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO pr_detail
          (pr_id,item_no,store_id,ctn,qty,rate,ln_no)
          VALUES(:pr_id, :item_no, :store_id, :ctn, :qty, :rate, :ln_no)
        ");
        $statement->execute(
          array(
            ':pr_id'               =>  $pr_id,            
            ':item_no'              =>  trim($_POST["item_no"][$count]),
            ':store_id'              =>  1,
             ':ctn'          =>  1,
            ':qty'          =>  trim($_POST["qty"][$count]),
            ':rate'           =>  trim($_POST["rate"][$count]),
            
            ':ln_no'        => $count
        
          )
        );
      }  
        $sql ="
          select sum(qty*rate) from pr_detail where pr_id = '".$pr_id."'
          ";
        $statement = $connect->prepare($sql);
        $statement->execute();
        $inv_amt = $statement->fetchColumn();     
          $due_day = 0;
          $ref_id = $pr_id;
            $sql = "SELECT IFNULL(max(jv_no),0) FROM jv WHERE branch_id = '".$branch_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_no = $statement->fetchColumn();
            $jv_no++;        
            $inv_no = $_POST["inv_no"];
        $sql = "SELECT sum(qty*rate) FROM pr_detail WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $pr = $statement->fetchColumn();
             
             $sql = "SELECT gst_pr FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $gst_pr = $statement->fetchColumn();
             $sql = "SELECT freight FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $freight = $statement->fetchColumn();
           
           if($gst_pr > 0)
           {
           $gst_amount = $pr * $gst_pr / 100;
         }else {$gst_amount = 0;}
          
            $sql = "SELECT discount FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $discount = $statement->fetchColumn();
            
            if($discount > 0)
            {
              $pr = $pr - $discount;
            }
            $sql = "SELECT MAX(jv_no) FROM jv WHERE branch_id = '".$branch_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_no = $statement->fetchColumn();
            settype($jv_no, "integer");
            $jv_no++;            
            $v_id = $_POST['v_id'];
           
            
           
              $pr_account = $v_id;
           
            $source = 'AP InvR';
            $description = 'AP Credit Invoice Return' .$pr_id .', ' .$_POST['inv_no'] .', ' .$_POST['description'] ;
            $due_day = 0;
        
            
            $statement = $connect->prepare("
              INSERT INTO jv 
                (jv_no,branch_id,dated,source,ref_id,description,user_id)
                VALUES (:jv_no, :branch_id, :dated, :source, :ref_id, :description, :user_id)
    ");
    $statement->execute(
      array(
          ':jv_no'             =>  $jv_no,
          ':branch_id'             =>  $branch_id,
          ':dated'             =>  $_POST['dated'],
          ':source'          =>  $source,
          ':ref_id'          =>  $pr_id,
          ':description'       =>  $description,
       
          ':user_id'       =>  $user_id
      )
    );
     $statement = $connect->query("select max(jv_id) from jv");
      $jv_id = $statement->fetchColumn();
      
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  1031002,
            ':debit'          =>  0,
            ':credit'           =>  $pr
            
          )
        );
        
        
          $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  2012002,
            ':debit'          =>  0,
            ':credit'           =>  $gst_amount
                        
          )
        );
        
        
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  2012003,
            ':debit'          =>  0,
            ':credit'           =>  $freight
                        
          )
        );
        $payable = $pr + $gst_amount + $freight;
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $pr_account,
            ':debit'          =>  $payable,
            ':credit'           =>  0
                     
          )
        );
        echo 'success';
        /////End Jv/////
	}

  if($_POST['btn_action']=='update')
  {
    $pr_id = $_POST['pr_id'];
    $ref_id = $pr_id;
     $statement = $connect->prepare("
        UPDATE pr
        SET branch_id = :branch_id,
        po_no = :po_no,
        inv_no = :inv_no,
        due_day = :due_day,
        dated = :dated,
         due_date = :due_date,
        v_id = :v_id,
        description = :description,
        discount = :discount, 
        discount_dsc = :discount_dsc, 
        inv_amt = :inv_amt,       
        freight = :freight,
        freight_dsc = :freight_dsc,
        gst = :gst,
        gst_pr = :gst_pr,
        user_id= :user_id,    
        status = :status        
        WHERE pr_id = :pr_id
        ");
      $statement->execute(
        array(
          ':branch_id'             =>  $branch_id,
          ':inv_no'             =>  trim($_POST["inv_no"]),
          ':po_no'             =>  0,
          ':due_day'             =>  0,
          ':dated'             =>  $_POST["dated"],
          ':due_date'             =>  $_POST["dated"],
          ':v_id'          =>  trim($_POST["v_id"]),
          ':description'       =>  $_POST["description"],
          ':discount'             =>  trim($_POST["discount"]),
          ':discount_dsc'             =>  trim($_POST["discount_dsc"]),
          ':inv_amt'             =>  0,          
          ':freight'             =>  trim($_POST["freight"]),
          ':freight_dsc'             =>  trim($_POST["freight_dsc"]),
          ':gst'             =>  trim($_POST["gst"]),
          ':gst_pr'             =>  trim($_POST["gst_pr"]),
          ':user_id'             =>  $user_id,       
          ':status'             =>  'Active',          
          ':pr_id'               =>  $pr_id
          )
        );
      
         $sql = "delete from pr_detail 
        where pr_id = '".$pr_id."'";
        $statement = $connect->prepare($sql);
        $statement->execute();
       
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        $statement = $connect->prepare("
          INSERT INTO pr_detail
          (pr_id,item_no,store_id,ctn,qty,rate,ln_no)
          VALUES(:pr_id, :item_no, :store_id, :ctn, :qty, :rate, :ln_no)
        ");
        $statement->execute(
          array(
            ':pr_id'               =>  $pr_id,            
            ':item_no'              =>  trim($_POST["item_no"][$count]),
            ':store_id'              =>  1,
             ':ctn'          =>  1,
            ':qty'          =>  trim($_POST["qty"][$count]),
            ':rate'           =>  trim($_POST["rate"][$count]),
           
            ':ln_no'        => $count
        
          )
        );
      }    
    
         
        $sql ="
          select sum(qty*rate) from pr_detail where pr_id = '".$pr_id."'
          
          ";
        $statement = $connect->prepare($sql);
        $statement->execute();
        $inv_amt = $statement->fetchColumn();
     
      
      $sql = "update pr set inv_amt = '".$inv_amt."' where pr_id = '".$pr_id."'";
          $statement = $connect->prepare($sql);
          $statement->execute();
         
          $sql = "select inv_amt from pr where pr_id = '".$pr_id."'";
          
         
          $freight =  trim($_POST["freight"]);
          $gst = trim($_POST["gst"]); 
        
         
         
      
          //create jv
         
         
          $due_day = 0;
          $ref_id = $pr_id;
            $sql = "SELECT jv_id FROM jv WHERE source = 'AP InvR'  and  ref_id = '".$ref_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $jv_id = $statement->fetchColumn();
             $sql = "SELECT jv_no FROM jv WHERE jv_id = '".$jv_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $jv_no = $statement->fetchColumn();
        
            $inv_no = $_POST["inv_no"];
             $sql = "delete  FROM jv_detail WHERE jv_id = '".$jv_id."'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
        $sql = "SELECT sum(qty*rate) FROM pr_detail WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $pr = $statement->fetchColumn();
                
             $sql = "SELECT gst_pr FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $gst_pr = $statement->fetchColumn();
             $sql = "SELECT freight FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $freight = $statement->fetchColumn();
           
           if($gst_pr > 0)
           {
           $gst_amount = $pr * $gst_pr / 100;
         }else {$gst_amount = 0;}
            $sql = "SELECT discount FROM pr WHERE pr_id = '".$pr_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); 
            $discount = $statement->fetchColumn();         
            if($discount > 0)
            {
              $pr = $pr - $discount;
            }
                     
         
            $source = 'AP InvR';
            $v_id = $_POST['v_id'];
               
            
  
              $pr_account = $v_id;
  
             $description = 'AP Credit Invoice Return' .$pr_id .', ' .$inv_no .', ' .$_POST['description'] ;
           
            
            $statement = $connect->prepare("
        UPDATE jv 
        SET 
        dated = :dated,
        branch_id = :branch_id, 
        source = :source,
        ref_id = :ref_id, 
        description = :description,
             
        user_id = :user_id,
        status = :status
        WHERE jv_id = :jv_id
      ");
      
      $statement->execute(
        array(
          ':dated'             =>  $_POST["dated"],
          ':branch_id'          =>  $branch_id,
          ':source'          =>  $source,
          ':ref_id'          =>  $pr_id,
          ':description'       =>  $description,
                      
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active',
          ':jv_id'               =>  $jv_id
        )
      );
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  1031002,
            ':debit'          =>  0,
            ':credit'           =>  $pr
            
          )
        );
        
        
          $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  2012002,
            ':debit'          =>  0,
            ':credit'           =>  $gst_amount
                        
          )
        );
        
        
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  2012003,
            ':debit'          =>  0,
            ':credit'           =>  $freight
                        
          )
        );
        $payable = $pr + $gst_amount + $freight;
        $statement = $connect->prepare("
          INSERT INTO jv_detail 
          (jv_id,acc_no,debit,credit)
          VALUES(:jv_id, :acc_no, :debit, :credit)
        ");
        $statement->execute(
          array(
            ':jv_id'               =>  $jv_id,
            ':acc_no'              =>  $pr_account,
            ':debit'          =>  $payable,
            ':credit'           =>  0
                     
          )
        );
        
        /////End Jv/////
     echo 'success';
  }
	
	if($_POST['btn_action'] == 'delete')
	{
		$p_id = $_POST['p_id'];
		$sql = "DELETE FROM pr WHERE pr_id = '".$p_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
        
        $sql = "DELETE FROM pr_detail WHERE pr_id = '".$p_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no 
		
		$sql = "SELECT jv_id FROM jv WHERE source = 'AP InvR' and ref_id = '".$p_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $jv_id = $statement->fetchColumn();
            
             $sql = "DELETE FROM jv WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute();
            $sql = "DELETE FROM jv_detail WHERE jv_id = '".$jv_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute();
            
		
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Deleted ';
		}
		
	}
}
?>