<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Invoice extends CI_Controller
{
    // Session Authoriesed
    public function __construct()
    {
        parent::__construct();

        $admin = $this
            ->session
            ->userdata('admin');
        $column['id'] = $admin['id'];
        $table = 'tbl_user';

        // loading common model
        $this->load ->model('Common_model');
        $result = $this->Common_model->Result($column, $table);
        
        if ($result['password'] == $admin['password'])
        {
            $login = true;
        }
        else
        {
            $login = false;
        }

        if (empty($admin) || $login == false)
        {
            $this
                ->session
                ->set_flashdata('msg', 'Your Session Has Been Expired');
            redirect(base_url() . 'master/login');
        }

        // loading common model
        $this
            ->load
            ->model('Common_model');
        $this
            ->load
            ->helper('common_helper');
           
    }






    public function index()
    {
      	$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Invoice';
		
		$table2 = 'tbl_user';
		$column2['id'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		
		$table21 = 'tbl_product';
	    $result21 = $this->Common_model->Results($table21);
	    
	    
	    $tables21 = 'tbl_product_size';
	    $results21 = $this->Common_model->Results($tables21);
	    
	    
	 
	    
	    $table211 = 'staff_stock';
		$column21['user_id'] = $_SESSION['admin']['id'];
        // we are getting results from common modal
		$result211 = $this->Common_model->ResultsWhere2($column21,$table211);
		
	    
	    
	    $table212 = ' tbl_size';
	    $result212 = $this->Common_model->Results($table212);
	    
	    
	    
	    $table2122 = 'tbl_customer';
	    $result2122 = $this->Common_model->Results($table2122);
		
	
		
	    $tablecolor = ' tbl_color';
	    $resultablecolor = $this->Common_model->Results($tablecolor);
	
		$data['user'] = $result2;
		$data['product'] = $result21;
		$data['customer'] = json_encode($result2122);
		$data['product_size'] = json_encode($result211);
		$data['product_size1'] = json_encode($results21);
	
		$data['color'] = json_encode($resultablecolor);
		$data['size_name'] = json_encode($result212);

        // we are loading admin dashboard
        $this
            ->load
            ->view('master/invoice/invoice', $data);
    }

 






 public function invoice() {
     
        	$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Invoice List';
		
		$table2 = 'tbl_user';
		$column2['id'] = $_SESSION['admin']['id'];
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		$data['user'] = $result2;
	
		
        // we are loading admin dashboard
		$this->load->view('master/invoice/invoice_list',$data);
    }





public function invoice_list($bill_no)
{
    
        	$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Invoice List';
		
		$table2 = 'tbl_user';
		$column2['id'] = $_SESSION['admin']['id'];;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		
	
		$data['user'] = $result2;
		$data['bill_no'] = $bill_no;
	
	
		
        // we are loading admin dashboard
		$this->load->view('master/master/invoice_list',$data);
    
  
}



public function payment()
{
    $bill_no=$_POST['bill_no'];
    $user_id=$_POST['user_id'];
    $payid=$_POST['payid'];
    $paid=$_POST['paid'];
    $user_id1=$_SESSION['admin']['id'];
    
       date_default_timezone_set('Asia/Kolkata');
        $addon=date('Y-m-d H:i:s');
    
    
     $queryd1 = $this->db->query("SELECT SUM(paid) AS rrt FROM customer_payment where bill_no='$bill_no' AND customer_id='$user_id'");
     $rowd1 = $queryd1->row(); 
     $all_paid=$rowd1->rrt+0;
     
     $queryd11 = $this->db->query("SELECT * FROM customer_invoice_list where user_id='$user_id1' AND bill_no='$bill_no' AND customer_id='$user_id'");
     $rowd11 = $queryd11->row(); 
     $dues=$rowd11->total;
     
     $due=$dues-$all_paid;
     $ndue=$due-$paid;
  
  if($payid=='')  
  {
            $this->session->set_flashdata('error','Something went Wrong.');
		    
  }
  else
  {
   $data=[
       'user_id'=>$_SESSION['admin']['id'],
       'customer_id'=>$user_id,
       'payid'=>$payid,
       'bill_no'=>$bill_no,
       'pre_amt'=>$due,
       'paid'=>$paid,
       'dues'=>$ndue,
       'addon'=>$addon,
       'status'=>1 
       ];
       
       
        $this->session->set_flashdata('success','SUCCESS!! Payment Successfully Completed.');
		   
    $this->db->insert('customer_payment',$data); 
  }
    
     redirect(base_url().'master/invoice/print_pay/'.$bill_no.'/'.$payid); 
}




  public function payment_list($bill_no)
  {
      
    
        	$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Payment List';
		
		$table2 = 'tbl_user';
		$column2['id'] = $_SESSION['admin']['id'];
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		
		$table21 = 'customer_invoice';
		$column21['bill_no'] = $bill_no;
		$result21 = $this->Common_model->Result($column21,$table21);
	

	
		$table211 = 'tbl_customer';
		$column211['cust_phone'] = $result21['customer_id'];
		$result211 = $this->Common_model->Result($column211,$table211);
		
		
		$data['user'] = $result2;
		$data['bill_no'] = $bill_no;
		$data['customers'] = $result211;
		$data['bill_date'] = $result21['addon'];
	
	

		
        // we are loading admin dashboard
	$this->load->view('master/invoice/payment_list',$data);
    
  

  }




  public function print_pay($id,$bill_no)
  {
      
    
        	$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Print Payment';
		
		$table2 = 'tbl_user';
		$column2['id'] = $_SESSION['admin']['id'];
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		
		$table21 = 'customer_invoice';
		$column21['bill_no'] = $id;
		$result21 = $this->Common_model->Result($column21,$table21);
	

	
		$table211 = 'tbl_customer';
		$column211['cust_phone'] = $result21['customer_id'];
		$result211 = $this->Common_model->Result($column211,$table211);
		
		
	
		$data['customers'] = $result211;
		
		
	
		$data['user'] = $result2;
		$data['payid'] = $bill_no;
	
	

		
        // we are loading admin dashboard
		$this->load->view('master/invoice/print_pay',$data);
    
  

  }





public function add_invoice()
{
       date_default_timezone_set('Asia/Kolkata');
        $addon=date('Y-m-d H:i:s');
        $pidp=$_POST['pid'];
        $bill_no=$_POST['bill_no'];
        
        $phone=$_POST['phone'];
        $full_name=$_POST['full_name'];
        $address=$_POST['address'];
       $uid=$_SESSION['admin']['id'];
          $tamt1=$_POST['tamt1'];
       $dis=$_POST['dis'];
       $bill1=date('YmdHi'); 
        
     $i=count($_POST['size']);
   
    $data121=['status'=>0];
    $this->db->where('bill_no',$bill_no);   
    $this->db->update('customer_invoice_list1',$data121); 
    
    
    
    
  $pass='12345';
   $pass1=md5($pass);
   
/*  $query =$this->db->where('cust_phone', $phone);
  $query = $this->db->get('tbl_customer');
  if ($query->num_rows()==0)  
  {
     $rpdata=['cust_name'=>$full_name,'cust_address'=>$address,'cust_phone'=>$phone,'user'=>$_SESSION['admin']['id'],'cust_datetime'=>$addon,'cust_status'=>1,'code'=>1,'pass'=>$pass,'cust_password'=>$pass1]; 
     $this->db->insert('tbl_customer',$rpdata);
      
  }*/
  
  
  $querysss = $this->db->query("SELECT * FROM tbl_customer where cust_phone='$phone'"); 
  $rowss1 = $querysss->row(); 
  $sps11=$rowss1->uid;
  
     
     for($x=0;$x<$i;$x++){
     
  
     
        $pid=$_POST['pid'][$x];
        
        $rid=$_POST['rid'][$x];
        $size_id=$_POST['size'][$x];
        $color_id=$_POST['color'][$x];
        $mrp=$_POST['mrp'][$x];
        $total=$_POST['totals'][$x];
        $color_id=$_POST['color'][$x];
        $oqty=$_POST['oqty'][$x];
        $qty=$_POST['qty'][$x];
        
     
 
     
   
if(empty($color_id))
{
 $querys = $this->db->query("SELECT * FROM tbl_product_size where p_id=$pid  AND size_id=$size_id"); 
  $row1 = $querys->row(); 
  $qqty=$row1->p_qty;
  $pv=$row1->p_pv+0;
}
else
{
  $querys = $this->db->query("SELECT * FROM tbl_product_size where p_id=$pid  AND size_id=$size_id AND color_id=$color_id"); 
  $row1 = $querys->row(); 
   $qqty=$row1->p_qty;
   $pv=$row1->p_pv+0;
 
}
  
  
  
   $data=[
       'user_id'=>$uid,
       'customer_id'=>$phone,
       'bill_no'=>$bill_no,
       'pid'=>$pid,
       'size_id'=>$size_id,
       'color_id'=>$color_id,
       'mrp'=>$mrp,
       'qty'=>$qty,
       'total'=>$total,
       'tps'=>$rid,
       'status'=>1,
       'addon'=>$addon,
       'pv'=>$pv
       ];


  
  
  
 if($qqty>=$qty)
   { 




       
  $this->db->insert('customer_invoice',$data);
  
  

  

$data12=['p_qty'=>$qqty-$qty];
if(!empty($color_id))
{ 
    $this->db->where('color_id',$color_id);  
}
    $this->db->where('p_id',$pid);   
    $this->db->where('size_id',$size_id);   
    $this->db->update('tbl_product_size',$data12); 
    
  }
  
  
   
    } 
  

$queryd1 = $this->db->query("SELECT SUM(total) AS rrt FROM customer_invoice where bill_no='$bill_no'");
$rowd1 = $queryd1->row(); 
$total=$rowd1->rrt;


$queryd111 = $this->db->query("SELECT SUM(pv) AS rrts FROM customer_invoice where bill_no='$bill_no'");
$rowd111 = $queryd111->row(); 
$tpv=$rowd111->rrts;

if($total>0)
{
 $ts=[
  'user_id'=>$uid,
  'customer_id'=>$phone,
  'bill_no'=>$bill_no,
  'tamt1'=>$tamt1,
  'dis'=>$dis,
  'total'=>$total-$dis,
  'addon'=>$addon,
  'status'=>1 
  ];
  
 $this->db->insert('customer_invoice_list',$ts);
 

 $ts1=[
  'user_id'=>$uid,
  'customer_id'=>$phone,
  'bill_no'=>$bill_no,
  'payid'=>rand(111111,999999),
  'pre_amt'=>$total,
  'paid'=>$_POST['pamt'],
  'dues'=>$total-$_POST['pamt'],
  'addon'=>$addon,
  'status'=>1 
  ];
  
 $this->db->insert('customer_payment',$ts1);
 
 
 
 
   
 
 
}
  
   
redirect(base_url('master/invoice/order_list/'.$bill_no));


     

   
        
    
    
    
    
}



public function order_list($bill_no) {
    
    $column['id'] = '1';
    $table = 'tbl_settings';
    
    // we are getting results from common modal
    $result = $this->Common_model->Result($column,$table);
    
    // we are setting result in data array as $data
    $data['data'] = $result;
    $data['page'] = 'Print Bill';
    
    $table2 = 'tbl_user';
    $column2['id'] = $_SESSION['admin']['id'];
    // we are getting results from common modal
    $result2 = $this->Common_model->Result($column2,$table2);
    
    $data['user'] = $result2;
    $data['payid'] = $bill_no;
    
    // we are loading admin dashboard
    $this->load->view('master/invoice/order_list',$data);
    
}



public function bill_print($bill_no) {
    
    $column['id'] = '1';
    $table = 'tbl_settings';
    
    // we are getting results from common modal
    $result = $this->Common_model->Result($column,$table);
    
    // we are setting result in data array as $data
    $data['data'] = $result;
    $data['page'] = 'Print Bill';
    
    $table2 = 'tbl_user';
    $column2['id'] = $_SESSION['admin']['id'];
    // we are getting results from common modal
    $result2 = $this->Common_model->Result($column2,$table2);
    
    $data['user'] = $result2;
    $data['payid'] = $bill_no;
    
    // we are loading admin dashboard
    $this->load->view('master/invoice/bill_print',$data);
    
}



}