<?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']))
{
	$dated = date('Y-m-d');
	if($_POST['btn_action']=='save')
	{

date_default_timezone_set('Asia/Karachi');
$curstamp = date('Y-m-d h:i:s a');
  
      if($_POST["valid_date"] !=='')
      {
        $description = "This Quotation is Valid till " .$_POST['valid_date'];
      }   else {$description = '';} 
  
    $statement = $connect->prepare("
      INSERT INTO quotation 
        (po_no,bilty_no,chal_no,gd_no,emp_id,title_id,dated,due_dated,inv_no,inv_type,inv_of,gst,gst_pr,sm_id,branch_id,cust_id,cust_name,contact,description,discount, discount_dsc, due_day, freight, freight_dsc, pre_balance,over_dues,user_id,status,cur_stamp)
        VALUES (:po_no, :bilty_no, :chal_no, :gd_no, :emp_id, :title_id, :dated, :due_dated, :inv_no, :inv_type, :inv_of, :gst,  :gst_pr, :sm_id, :branch_id, :cust_id, :cust_name, :contact, :description, :discount, :discount_dsc, :due_day, :freight, :freight_dsc, :pre_balance, :over_dues, :user_id, :status, :cur_stamp)
    ");
    $statement->execute(
      array(
          ':po_no'               =>  0,
          ':bilty_no'               =>  0,
          ':chal_no'             =>  0,
          ':gd_no'             =>  0,
          ':emp_id'             => 1,
          ':title_id'             =>  1,
          ':dated'             =>  $_POST["dated"],
          ':due_dated'             =>  $_POST["valid_date"],
          ':inv_no'             =>  0,
          ':inv_type'             =>  'Credit',
          ':inv_of'             =>  'Wholesale',
           ':gst'             =>  'OFF',
           ':gst_pr'             =>  0,
          ':sm_id'             =>  1,
          ':branch_id'             =>  $branch_id,         
          ':cust_id'       =>  trim($_POST["cust_id"]),
          ':cust_name'       =>  trim($_POST["cust_name"]),
          ':contact'       =>  '',
          ':description'       =>  $description,
          ':discount'       =>  trim($_POST["discount"]),
          ':discount_dsc'       =>  trim($_POST["discount_dsc"]),
          ':due_day'       =>  0,
          ':freight'       =>  trim($_POST["freight"]),
          ':freight_dsc'       =>  trim($_POST["freight_dsc"]),
           ':pre_balance'       => 0,
            ':over_dues'       =>  0,
          ':user_id'       =>  $user_id,
          ':status'       =>  'Active',
          ':cur_stamp'       =>  $curstamp
          
      )
    );
      $statement = $connect->query("SELECT LAST_INSERT_ID()");
      $s_id = $statement->fetchColumn();
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
        
        $statement = $connect->prepare("
          INSERT INTO quotation_detail 
          (s_id,item_no,store_id,ctn,qty,rate,dis_pr,item_status)
          VALUES (:s_id, :item_no, :store_id, :ctn, :qty, :rate, :dis_pr, :item_status)
        ");
        $statement->execute(
          array(
            ':s_id'               =>  $s_id,
            ':item_no'              =>  trim($_POST["item_no"][$count]),
            ':store_id'              => 1,
            ':ctn'          =>  1,
            ':qty'          =>  trim($_POST["qty"][$count]),
            ':rate'           =>  trim($_POST["rate"][$count]),
            ':dis_pr'           =>  trim($_POST["dis_pr"][$count]),
              ':item_status'           =>  trim($_POST["item_status"][$count])
            
                       
          )
        );
      }
 $sql = "update quotation_detail d, items i set d.cost = i.item_cost where d.s_id = '".$s_id."' and i.item_id=d.item_no";
           $statement = $connect->prepare($sql);
           $statement->execute();
         
        
        echo 'success';
    
	}

  if($_POST['btn_action']=='update')
  {
    $s_id = $_POST['s_id'];
   
     if($_POST["valid_date"] !=='')
      {
        $description = "This Quotation is Valid till " .$_POST['valid_date'];
      }   else {$description = '';} 
   
     
      $statement = $connect->prepare("
                DELETE FROM quotation_detail WHERE s_id = :s_id
            ");
            $statement->execute(
                array(
                    ':s_id'       =>      $s_id
                )
            );
      for($count=0; $count<$_POST["total_item"]; $count++)
      {
       $statement = $connect->prepare("
          INSERT INTO quotation_detail 
          (s_id,item_no,store_id,ctn,qty,rate,dis_pr, item_status)
          VALUES (:s_id, :item_no, :store_id, :ctn, :qty, :rate, :dis_pr, :item_status)
        ");
        $statement->execute(
          array(
            ':s_id'               =>  $s_id,
            ':item_no'              =>  trim($_POST["item_no"][$count]),
            ':store_id'              =>  1,
            ':ctn'          =>  1,
            ':qty'          =>  trim($_POST["qty"][$count]),
            ':rate'           =>  trim($_POST["rate"][$count]),
            ':dis_pr'           =>  trim($_POST["dis_pr"][$count]),
              ':item_status'           =>  trim($_POST["item_status"][$count])
                       
          )
        );
        
        
        //$result = $statement->fetchAll();
      }
      $sql = "update quotation_detail d, items i set d.cost = i.item_cost where d.s_id = '".$s_id."' and i.item_id=d.item_no";
           $statement = $connect->prepare($sql);
           $statement->execute();
      $statement = $connect->prepare("
        UPDATE quotation set
        po_no = :po_no,
        bilty_no = :bilty_no,
        chal_no = :chal_no,
        gd_no = :gd_no,
        emp_id = :emp_id,
        title_id = :title_id,
        dated = :dated,
        due_dated = :due_dated,
        inv_no = :inv_no,
        inv_type = :inv_type,
        inv_of = :inv_of,
        sm_id = :sm_id,
        branch_id = :branch_id,
        cust_id = :cust_id,
        cust_name = :cust_name,
        contact = :contact,
        description = :description,
        gst = :gst,
        gst_pr = :gst_pr,
        discount = :discount,
        discount_dsc = :discount_dsc,  
        freight = :freight,
        freight_dsc = :freight_dsc,
        due_day = :due_day,
        pre_balance = :pre_balance,
        over_dues = :over_dues,        
            
        status = :status
        where s_id = :s_id         
      ");
      $statement->execute(
        array(
          ':po_no'             =>  0,
          ':bilty_no'             =>  0,
          ':chal_no'             => 0,
          ':gd_no'             => 0,
          ':emp_id'             =>  1,
          ':title_id'             =>  1,
          ':dated'             =>  $_POST["dated"],          
          ':due_dated'             =>  $_POST["valid_date"],          
          ':inv_no'             =>  0,
          ':inv_type'             =>  'Credit',
          ':inv_of'          =>  'Wholesale',
          ':sm_id'       =>  1,
          ':branch_id'             =>  $branch_id,
          ':cust_id'             =>  trim($_POST["cust_id"]),          
          ':cust_name'             =>  trim($_POST["cust_name"]),
          ':contact'             =>  '',
          ':description'             =>  $description,
          ':gst'             =>  'OFF',
          ':gst_pr'             =>  0,
          ':discount'             =>  trim($_POST["discount"]),  
          ':discount_dsc'             =>  trim($_POST["discount_dsc"]),  
          ':freight'             =>  trim($_POST["freight"]),  
          ':freight_dsc'             =>  trim($_POST["freight_dsc"]),  
          ':due_day'       =>  0,  
           ':pre_balance'       =>  0,  
            ':over_dues'       =>  0,                
          ':status'               =>  'Active',
          ':s_id'               =>  $s_id
        )
      );

        
        echo 'success';
  }	
	
	if($_POST['btn_action'] == 'delete')
	{
		$s_id = $_POST['s_id'];
			
		$sql = "DELETE FROM quotaion WHERE s_id = '".$s_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
        
        $sql = "DELETE FROM quotation_detail WHERE s_id = '".$s_id."' ";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no 
		
		
		
		//$result = $statement->fetchAll();	
		
	}
}
?>