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

class Test_Controller extends CI_Controller {

        public function __construct() {
        parent::__construct();
        $this->load->model('Product_model');
      
         
        }
        
        function get_time(){
            $this->db->from('mes_qto');
                $this->db->select("*");
                $detailtax = $this->db->get();
                $qto = $detailtax->result();
            
            for($i=9; $i<count($qto); $i++){
               $this->db->from('mes_inventry');
                $this->db->select("invt_cdate");
                 $this->db->where('invt_type','INV');
                 $this->db->where('invt_type_id',$qto[$i]->po_id);
                 $this->db->order_by('invt_id', 'desc');
                $this->db->limit(1);
                $detailtax = $this->db->get();
                $time = $detailtax->result();
                
                //echo $qto[$i]->po_id."-".$time[0]->invt_cdate."</br>";
                
                $this->db->set('po_time',$time[0]->invt_cdate);
            	            $this->db->where('po_id',$qto[$i]->po_id);
            	            $result=$this->db->update('mes_qto');
                echo $qto[$i]->po_id."-".$time[0]->invt_cdate."</br>";
                
            }
            
        }
        
        function trans(){
            $this->db->from('mes_trnfer_item');
                $this->db->select("*");
                $this->db->where('trf_trfid',2);
                $detailtax = $this->db->get();
                $data = $detailtax->result();
                
            for($i=0; $i<count($data); $i++){
                
                    $this->db->from('batch_depo');
                    $this->db->select("*");
                    $this->db->where('batch_id',$data[$i]->trf_batch);
                    $detailtax = $this->db->get();
                    $datab = $detailtax->result();
                    
                    $newb = $datab[0]->avl_qty - $data[$i]->trf_qty;
                    
                    $this->db->set('avl_qty',$newb);
    	            $this->db->where('batch_id',$data[$i]->trf_batch);
    	            $result=$this->db->update('batch_depo');
    	            
            	            $this->db->from('mes_depot_stock');
                            $this->db->select("*");
                            $this->db->where('dpstock_itemid',$data[$i]->trf_itmid);
                            $detailtax = $this->db->get();
                            $datast = $detailtax->result();
                            
                            $news = $datast[0]->dpstock_qty - $data[$i]->trf_qty;
                            
                            $this->db->set('dpstock_qty',$news);
            	            $this->db->where('dpstock_itemid',$data[$i]->trf_itmid);
            	            $result=$this->db->update('mes_depot_stock');     
                
                echo $data[$i]->trf_itmid." >";
                echo $data[$i]->trf_batch.">";
                echo $data[$i]->trf_qty."<br>";
                
            }
            
        }

          
        function rt(){
            
            $this->db->from('mes_user');
                $this->db->select("*");
                $this->db->where('user_departmentid',"Marketing");
                $this->db->where('user_status',1);
                $de = $this->db->get();
                $dtt = $de->result();
            
            for($k=0; $k<count($dtt); $k++){
            
                $this->db->from('mes_route');
                $this->db->select("*");
                $this->db->where('ref_id',$dtt[$k]->user_id);
                $detailtax = $this->db->get();
                $data = $detailtax->result();
                
                for($j=0; $j<count($data); $j++){
                    $all1 = explode(",",$data[$j]->cities);
                         $all = array_unique($all1);
                                 for($i=0; $i<count($all); $i++){
                                     $this->db->select("*");    
                                    $this->db->from('cities');
                                    $this->db->where('id',$all[$i]);
                                    $query = $this->db->get();
                                    $cty =$query->result();
                                     
                                    $this->db->from('mes_customer');
                                    $this->db->select("*");
                                    $this->db->where('cust_salseperson',$dtt[$k]->user_id);
                                    $this->db->where('cust_city',$all[$i]);
                                    $detailtax = $this->db->get();
                                    $cus = $detailtax->result();
                                    
                                    if(count($cus)>0){
                                        for($h=0; $h<count($cus); $h++){
                                            echo $cus[$h]->cust_id."--".$data[$j]->route_id."<br>";
                                         $dat = array(
                                       'cus_id' => $cus[$h]->cust_id, 
                                        'route_id'  => $data[$j]->route_id
                                        );
                                          $result = $this->db->insert('customer_route',$dat);
                                        }
                                        
                                           
                                    }
                                    
                                    
                                  
            
            }
            
        }
                
            }

    
        }     
        
        function view(){
            $this->load->view('pages/test');
        }
        
        function get_data(){
            if (isset($_POST['submit']))
                        {
                             
                            $fileMimes = array(
                                'text/x-comma-separated-values',
                                'text/comma-separated-values',
                                'application/octet-stream',
                                'application/vnd.ms-excel',
                                'application/x-csv',
                                'text/x-csv',
                                'text/csv',
                                'application/csv',
                                'application/excel',
                                'application/vnd.msexcel',
                                'text/plain'
                            );
                         
                            // Validate selected file is a CSV file or not
                            if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $fileMimes))
                            {
                         
                                // Open uploaded CSV file with read-only mode
                                $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
                        
                                // Skip the first line
                                fgetcsv($csvFile);
                        
                                // Parse data from CSV file line by line        
                                while (($getData = fgetcsv($csvFile, 10000, ",")) !== FALSE)
                                {
                                    // Get row data
                                    
                                if($getData[0]!=""){
                                    $name = $getData[0];
                                    $email = $getData[1];            
                        
                                    $this->db->set('avl_qty',$getData[2]);
                                    $this->db->set('man_date',$getData[3]);
                                    $this->db->set('exp_date',$getData[4]);
                                    $this->db->set('buying_price',$getData[5]);
                                    $this->db->set('selling_price',$getData[6]);
                                    $this->db->set('retail_price',$getData[7]);
                                        $this->db->where('batch_id', $getData[0]);
                                        $result=$this->db->update('batch');
                                        
                                    $this->db->from('batch');
                                    $this->db->select("batch");
                                    $this->db->where('batch_id', $getData[0]);
                                    $detailt = $this->db->get();
                                    $detail1 = $detailt->result();   
                                    //print_r($detail1[0]->batch);
                        
                                    $datart = array(
                                        'batch_id' => $getData[0], 
                                        'batch' => $detail1[0]->batch, 
                                        'type' => "adj", 
                                        'doc_id' => 1, 
                                        'cal' => "plus", 
                                        'qty' => $getData[2], 
                                        'old_qty' => 0, 
                                        'new_qty' => $getData[2], 
                                        'depo' => 0, 
                                        'date' => date('Y-m-d'), 
                                        'user'  => 0
                                    );
                                    $this->db->insert('batch_log',$datart);
                                    echo $getData[0]."<br>";
                                }    
                                    
                                
                                
                                }
                                
                                
                                    $this->db->from('batch');
                                    $this->db->select("SUM(avl_qty) as qty,product");
                                    $this->db->group_by('product');
                                    $detailtax = $this->db->get();
                                    $detail = $detailtax->result();
                                
                                    for($i=0; $i<count($detail); $i++){
                                        $this->db->set('stc_actqty',$detail[$i]->qty);
                                        $this->db->set('stc_qty',$detail[$i]->qty);
                                        $this->db->where('stc_itemid', $detail[$i]->product);
                                        $result=$this->db->update('mes_stock');
                                    }
                        
                                // Close opened CSV file
                                fclose($csvFile);
                        
                               // header("Location: index.php");         
                            }
                            else
                            {
                                echo "Please select valid file";
                            }
                        }
        }
}        