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

class Staff 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()
    {
        // we are setting data for get site data
        $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'] = 'Staff';

        // 		getting product data
       $table6='tbl_user';
        $result6 = $this
            ->Common_model
            ->Resultss($table6);


        $data['user'] = $result6;
      

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

    public function edit($id) {
        	$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'] = 'Edit Staff';
		
		$table2 = 'tbl_user';
		$column2['id'] = $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/staff/edit',$data);
    }

    public function create()
    {
        // we are setting data for get site data
        $column['id'] = '1';
        $table = 'tbl_settings';
        $data['page'] = 'Staff Add';

        // we are getting results from common modal
        $result0 = $this
            ->Common_model
            ->Result($column, $table);


        $data['data'] = $result0;
     
        $this
            ->load
            ->view('master/staff/create', $data);
    }



    public function add()
    {

      
   
        if (!empty($_FILES['photo']['name']))
        {
            $path = $_FILES['photo']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);

            // $rand = mt_rand(10000,999999);
            $rand = $p_id001;
            $product_name = 'Product_' . $rand . '.' . $ext;
            $product_name2 = 'Product_Temp_' . $rand . '.' . $ext;

            $config['upload_path'] = 'uploads/master/user/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $product_name2;

            $this->load->library('upload', $config);

            if ($this ->upload->do_upload('photo')){}
           

        }
      
       $sponsor='8084940179'; 
       $sponsor1='8084940179'; 
       $upid=$sponsor;
       $uid=$_POST['phone'];
       $mob=$_POST['phone'];
       $reg_password=$_POST['password'];
       $pass=$reg_password;
       $name=$_POST['full_name'];
       $email=$_POST['email'];
       $addon=date('Y-m-d H:i:s');
        $data=[
            'full_name'=>$_POST['full_name'],
            'email'=>$_POST['email'],
            'phone'=>$_POST['phone'],
            'password'=>md5($_POST['password']),
            'passwords'=>$_POST['password'],
            'dob'=>$_POST['dob'],
            'fname'=>$_POST['fname'],
            'address'=>$_POST['address'],
            'photo'=>$product_name2,
            'role'=>'Staff',
            'status'=>'Active',
            'vender'=>0,
            'aadhar'=>$_POST['aadhar'],
            'pan'=>$_POST['pan'],
            'gst'=>$_POST['gst'],
            'acc'=>$_POST['acc'],
            'acc_name'=>$_POST['acc_name'],
            'ifsc'=>$_POST['ifsc'],
            'date'=>date('Y-m-d H:i:s')
            ];
       
         
            
            
           
        $table='tbl_user';
        $result = $this ->Common_model->Create($data, $table);
        
        
        
        



 if($side=='1')
    {
        $sides='luid';
       
      
    }
    else
    {
       $sides='ruid';
     
      
    }   
     
     
  for($x1=0;$x1<1000;$x1++)
   {

 $res= $this->db->query("select * from details where uid='$upid'");
     foreach ($res->result() as $row9)
        {
          
           $upid_side=$row9->$sides;
           
        if(empty($upid_side)){
              $upid=$row9->uid;
             $root1=$row9->root;
             $root=$root1.','.$upid; 
           }
         else
         {
            $upid=$row9->$sides;
         }
           
        }
}  


       
     
$datakr=[
   'sponsor'=>$sponsor,
   'uid'=>$uid,
   'pass'=>$pass, 
   'name'=>$name, 
   'mob'=>$mob, 
   'smsd'=>0,
   'email'=>$email,
   'branch'=>0,
   'tlm'=>0,
   'root'=>$root,
   'tlm1'=>0,
   'tlm2'=>0,
   'level'=>0,
   'level1'=>0,
   'level2'=>0,
   'wallet'=>0,
   'wallet1'=>0,
   'wallet2'=>0,
   'role'=>0,
   'status'=>1,
   'status1'=>1,
   'status2'=>1,
   'kyc'=>0,
   'pans'=>0,
   'addon'=>$addon
    ];




$sql = 'update details set tlm2=tlm2+1';
$this->db->query($sql, array());


$this->db->insert('details',$datakr); 

$query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=1 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',1);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>1];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
      
      
      
      $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=2 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',2);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>2];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
  $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=3 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',3);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>3];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
  
  $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=4 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',4);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>4];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=5 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',5);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>5];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=6 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',6);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>6];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=7 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',7);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>7];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=8 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',8);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>8];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=9 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',9);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>9];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        
        $query11 = $this->db->where('uid',$sponsor);
    $query11 = $this->db->get('details');
	if ($query11->num_rows() > 0)  
        {
    foreach ($query11->result() as $row11)
        {
        
         $uids=$row11->uid;
         $sponsor=$row11->sponsor;
         $querysk11 = $this->db->query("SELECT * FROM tree where uid='$uids' AND level=10 ");
         if ($querysk11->num_rows() > 0)  
        {
         $rowfa111 = $querysk11->row();
         $mroot=$rowfa111->root;
         $roots=$mroot.','.$uid;
         $xdata=['root'=>$roots];
         $this->db->where('uid',$uids);
         $this->db->where('level',10);
         $this->db->update('tree',$xdata);
         
        }
        else
        {
         $roots=$uid;
         $xdata=['root'=>$roots,'uid'=>$uids,'level'=>10];
         $this->db->where('uid',$uids);
         $this->db->insert('tree',$xdata); 
        }
        }}
        }}
        }}
        }}
        }}
        }}
        }}
        
        
        }}
        
        
        }}  
        
        
  
        }}
        
     
      
                    $token = md5(time());
                    $cust_datetime = date('Y-m-d h:i:s');
                    $cust_timestamp = time();
    
                    $formArray['cust_name'] =  strip_tags($name);
                    $formArray['sponsor_id'] =  strip_tags($sponsor1);
                    $formArray['cust_email'] = strip_tags($email);
                    $formArray['cust_phone'] = strip_tags($mob);
                    $formArray['cust_password'] = md5($reg_password);
                    $formArray['cust_token'] = $token;
                    $formArray['cust_datetime'] = $cust_datetime;
                    $formArray['cust_timestamp'] = $cust_timestamp;
                    $formArray['cust_status'] = 1;
                    $formArray['user'] = 1;
                    $formArray['code'] = rand(1111,9999);
                    $formArray['uid'] = $mob;
                    $formArray['pass'] = $reg_password;
                        
                    $this->Common_model->Create($formArray,'tbl_customer');
                    
                    $column1['cust_phone'] = $mob;
                    $table1 = 'tbl_customer';
                    $result2 = $this->Common_model->ResultsWhere2($column1,$table1);
                    foreach ($result2 as $row2)
                    {
    
                        $code1 = $row2['code'];
                        $pass = $row2['pass'];
                        $user = $row2['cust_phone'];
                        $cust_email = $row2['cust_email'];
                        $name = $cust_name;
    
                        $to = $cust_email;
                        $subject = "Welcome Message !!";
                        $message = "<h3><strong>Dear $name .</strong>Your Registration is successful.  User Id: $user And Password: $pass !! Thank You For Visiting ".$data['base_name'].".</h3>";
                        $this->email->from($data['contact_email'], $data['base_name']);
                        $this->email->to($to);
                        $this->email->cc($data['base_name']);
                        $this->email->bcc($data['web_url']);
                        $this->email->subject($subject);
                        $this->email->message($message);
                        $this->email->send();
    
                        $sender = $data['sms_sender'];
                        $username = $data['sms_user'];
                        $password2 = $data['sms_pass'];
                        $number = $mob;
                        $message="Welcome to tryit india Your User $uid and Password is- $pass . Visit Website- tryitindia.com";
                       // $message = "Dear $name ,Your Welcome. User Id: $user And Password: $pass !! Thank You For Visiting ".$data['base_name'].".";
                        $url = "http://buzz.azmarq.com/http-api.php?username=" . urlencode($username) . "&password=" . urlencode($password2) . "&senderid=" . urlencode($sender) . "&route=1&number=" . urlencode($number) . "&message=" . urlencode($message);
                        
                        
                        


                        $ch = curl_init();
                        $timeout = 90; // set to zero for no timeout
                        $ch = curl_init($url);
                        curl_setopt ($ch, CURLOPT_URL, $url);
                        curl_setopt ($ch, CURLOPT_HEADER, 0);
                        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                        $res_data = curl_exec($ch);
                        $curl_error = curl_errno($ch);
                        curl_close($ch);
                        $rj = (json_decode($res_data, true));
    
                      
                    }
    
               
        
        
        
        

        if ($result == true)
        {
            $this
                ->session
                ->set_flashdata('success', 'SUCCESS!! Staff is Added Successfully.');
        }
        else
        {
            $this
                ->session
                ->set_flashdata('error', 'Something went Wrong .');
        }

     
     
 
    redirect(base_url('master/Staff')); 

        	
        
    }

  
     public function edit_staff()
    {

      
        // 		here we are uploading product featured photo
        if (!empty($_FILES['photo']['name']))
        {
            $path = $_FILES['photo']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);

            // $rand = mt_rand(10000,999999);
            $rand = $p_id001;
            $product_name = 'Product_' . $rand . '.' . $ext;
            $product_name2 = 'Product_Temp_' . $rand . '.' . $ext;

            $config['upload_path'] = 'uploads/master/user/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $product_name2;

            $this->load->library('upload', $config);

            if ($this->upload->do_upload('photo')){}
           

        }
        else
        {
        $product_name2='user.png';
       }
        
       
        
        $data=[
            'full_name'=>$_POST['full_name'],
            'fname'=>$_POST['fname'],
             'address'=>$_POST['address'],
             'dob'=>$_POST['dob'],
            'email'=>$_POST['email'],
            'phone'=>$_POST['phone'],
            'password'=>md5($_POST['password']),
            'passwords'=>$_POST['password'],
            'photo'=>$product_name2,
            'role'=>'Staff',
            'status'=>'Active',
            'vender'=>0,
            'aadhar'=>$_POST['aadhar'],
            'pan'=>$_POST['pan'],
            'gst'=>$_POST['gst'],
            'acc'=>$_POST['acc'],
            'acc_name'=>$_POST['acc_name'],
            'ifsc'=>$_POST['ifsc'],
            'date'=>date('Y-m-d H:i:s'),
            'a'=>$_POST['a'],
            'b'=>$_POST['b'],
            'c'=>$_POST['c'],
            'd'=>$_POST['d'],
            'e'=>$_POST['e'],
            'f'=>$_POST['f'],
            'g'=>$_POST['g'],
            'h'=>$_POST['h'],
            'i'=>$_POST['i'],
            'j'=>$_POST['j'],
            'k'=>$_POST['k'],
            'l'=>$_POST['l'],
            'm'=>$_POST['m'],
            'n'=>$_POST['n'],
            'o'=>$_POST['o'],
            'p'=>$_POST['p'],
            'q'=>$_POST['q'],
            'r'=>$_POST['r'],
            's'=>$_POST['s'],
            't'=>$_POST['t'],
            ];
       
         
            
          $column['id']=$_POST['id'];  
          $table='tbl_user';
          $id=$_POST['id'];
            
          $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Staff is Updated Successfully.');
		    redirect(base_url('master/staff/edit/'.$id));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong.');
		    redirect(base_url('master/staff/edit/'.$id));
		    exit();
        }

     
     
 
     redirect(base_url('master/Staff')); 

        	
        
    }
    
    
    public function delete() {
        $p_id = $_POST['pid'];
        $column['id'] = $p_id;
        
         $this->load->model('Common_model');
        
        $result = $this->Common_model->Result($column,'tbl_user');
        $product_pic_name2 = $result['photo'];
        $uploadPath = 'uploads/shop/product/';
        
        if (file_exists($uploadPath . $product_pic_name2))
        {
            unlink($uploadPath . $product_pic_name2);
        }
        
        $this->Common_model->Del($column,'tbl_user');
      
    
        $this->session->set_flashdata('success', 'SUCCESS!! Staff is Deleted Successfully.');

        redirect(base_url('master/staff'));
        
    }

 public function stock($id) {
        	$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'] = 'Staff Stock';
		
		$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);
	    
	    
	    $table211 = 'tbl_product_size';
	    $result211 = $this->Common_model->Results($table211);
	    
	    
	    $table212 = ' tbl_size';
	    $result212 = $this->Common_model->Results($table212);
		
	
		
	    $tablecolor = ' tbl_color';
	    $resultablecolor = $this->Common_model->Results($tablecolor);
	
		$data['user'] = $result2;
		$data['product'] = $result21;
		$data['product_size'] = json_encode($result211);
	
		$data['color'] = json_encode($resultablecolor);
		$data['size_name'] = json_encode($result212);
		
        // we are loading admin dashboard
		$this->load->view('master/staff/stock',$data);
    }
    
    
    public function add_product()
    {
        
        
        
        
        
        
        date_default_timezone_set('Asia/Kolkata');
        $addon=date('Y-m-d H:i:s');
        $pidp=$_POST['pid'];
        $bill_no=$_POST['bill_no'];
     
        
       $bill1=date('YmdHi'); 
        
     $i=count($pidp);
     
     for($x=0;$x<$i;$x++){
     
  
     
        $pid=$_POST['pid'][$x];
        $uid=$_POST['uid'][$x];
        $rid=$_POST['rid'][$x];
        $size_id=$_POST['size'][$x];
        $color_id=$_POST['color'][$x];
        $oqty=$_POST['oqty'][$x];
        $qty=$_POST['qty'][$x];
        
     
     
     
      $data=[
       'user_id'=>$uid,
       'pid'=>$pid,
       'size_id'=>$size_id,
       'color_id'=>$color_id,
       'qty'=>$qty,
       'tps'=>$rid,
       'status'=>1,
       'addon'=>$addon
       ];





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;
}
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;
}
  
 if($qqty>=$qty)
   { 

if(empty($color_id))
{       
  $querysk11 = $this->db->query("SELECT * FROM staff_stock where pid='$pid' AND size_id='$size_id' ");
}
else
{
    $querysk11 = $this->db->query("SELECT * FROM staff_stock where pid='$pid' AND size_id='$size_id' AND color_id='$color_id'");
}
  if ($querysk11->num_rows() ==0)  
  { 
       
  $this->db->insert('staff_stock',$data);
  
  
  }
  else
  {
   $row = $querysk11->row(); 
   $xqty=$row->qty;
   $tt=$xqty+$qty;
   
   $data1=['qty'=>$tt];
   
if(!empty($color_id))
{ 
    $this->db->where('color_id',$color_id);  
}
    $this->db->where('pid',$pid);   
    $this->db->where('size_id',$size_id);   
    $this->db->update('staff_stock',$data1);   
      
  }
  

    $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); 
    
 
$queryd = $this->db->query("SELECT * FROM tbl_product_size where id='$rid'");
$rowd = $queryd->row(); 
$mrp=$rowd->p_current_price;
$total=$qty*$mrp;

 $rdata=[
       'bill_no'=>$bill_no,
       'user_id'=>$uid,
       'pid'=>$pid,
       'size_id'=>$size_id,
       'color_id'=>$color_id,
       'mrp'=>$mrp,
       'qty'=>$qty,
       'total'=>$total,
       'tps'=>$rid,
       'status'=>1,
       'addon'=>$addon
       ];    
    
$this->db->insert('staff_stock_list',$rdata);





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

 $ts=[
  'user_id'=>$uid,
  'bill_no'=>$bill_no,
  'total'=>$total,
  'addon'=>$addon,
  'status'=>1 
  ];
  
  $this->db->insert('staff_bill_list',$ts);
   
redirect(base_url('master/staff/invoice_list/'.$uid));

      

   
        
    }
    
    
    
    
   public function upd_stock()
    {
    
     
      $id=$_POST['id'];
       $prestock=$_POST['prestock'];
      $newstock=$_POST['newstock'];
        
      $querysk11 = $this->db->query("SELECT * FROM staff_stock where id='$id' ");
      $row = $querysk11->row(); 
      $tps=$row->tps;
      
      $querysk111 = $this->db->query("SELECT * FROM tbl_product_size where id='$tps' ");
      $row1 = $querysk111->row(); 
      $p_qty=$row1->p_qty;
      
      
     if($newstock>$prestock)
     {
         $a=$newstock-$prestock;
          
          
          if($p_qty>=$a)
          {
              echo 1;
         $newstock1=$p_qty-$a;
             $data=['qty'=>$newstock];
             $data1=['p_qty'=>$newstock1];
           
        
             $this->db->where('id',$tps);
             $this->db->update('tbl_product_size',$data1);
            
             
              $this->db->where('id',$id);
             $this->db->update('staff_stock',$data);
          }
          else
          {
              echo 0;
          }
     }
     else
     {
        
        $a=$prestock-$newstock;
        
              echo 1;
              $newstock1=$p_qty+$a;
             $data=['qty'=>$newstock];
             $data1=['p_qty'=>$newstock1];
             
             $this->db->where('id',$tps);
             $this->db->update('tbl_product_size',$data1);
             
             
              $this->db->where('id',$id);
             $this->db->update('staff_stock',$data); 
     }
  

    }
  





 public function invoice($id) {
        	$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'] = 'Staff Stock';
		
		$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);
	    
	    
	    $table211 = 'tbl_product_size';
	    $result211 = $this->Common_model->Results($table211);
	    
	    
	    $table212 = ' tbl_size';
	    $result212 = $this->Common_model->Results($table212);
		
	
		
	    $tablecolor = ' tbl_color';
	    $resultablecolor = $this->Common_model->Results($tablecolor);
	
		$data['user'] = $result2;
		$data['product'] = $result21;
		$data['product_size'] = json_encode($result211);
	
		$data['color'] = json_encode($resultablecolor);
		$data['size_name'] = json_encode($result212);
		
        // we are loading admin dashboard
		$this->load->view('master/staff/invoice',$data);
    }





public function invoice_list($id)
{
    
        	$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'] = $id;
        // we are getting results from common modal
		$result2 = $this->Common_model->Result($column2,$table2);
		
		
		
		$table21 = 'tbl_product';
	    $result21 = $this->Common_model->Results($table21);
	    
	    
	    $table211 = 'tbl_product_size';
	    $result211 = $this->Common_model->Results($table211);
	    
	    
	    $table212 = ' tbl_size';
	    $result212 = $this->Common_model->Results($table212);
		
	
		
	    $tablecolor = ' tbl_color';
	    $resultablecolor = $this->Common_model->Results($tablecolor);
	
		$data['user'] = $result2;
		$data['product'] = $result21;
		$data['product_size'] = json_encode($result211);
	
		$data['color'] = json_encode($resultablecolor);
		$data['size_name'] = json_encode($result212);
		
        // we are loading admin dashboard
		$this->load->view('master/staff/invoice_list',$data);
    
  
}



public function payment()
{
    $bill_no=$_POST['bill_no'];
    $user_id=$_POST['user_id'];
    $payid=$_POST['payid'];
    $paid=$_POST['paid'];
    
       date_default_timezone_set('Asia/Kolkata');
        $addon=date('Y-m-d H:i:s');
    
    
     $queryd1 = $this->db->query("SELECT SUM(paid) AS rrt FROM staff_payment where bill_no='$bill_no'");
     $rowd1 = $queryd1->row(); 
     $all_paid=$rowd1->rrt+0;
     
     $queryd11 = $this->db->query("SELECT * FROM staff_bill_list where user_id='$user_id' AND bill_no='$bill_no'");
     $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'=>$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('staff_payment',$data); 
  }
    
     redirect(base_url('master/staff/payment_list/'.$user_id.'/'.$bill_no)); 
}




  public function payment_list($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'] = 'Payment List';
		
		$table2 = 'tbl_user';
		$column2['id'] = $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/staff/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'] = $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/staff/print_pay',$data);
    
  

  }

public function order_list($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 Bill';
		
		$table2 = 'tbl_user';
		$column2['id'] = $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/staff/order_list',$data);
    
  

    
}


}