<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Categories extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    //PRODUCT CATEGORIES HOME
    public function products() {

        $data['categories'] = $this->common->getAll('CATEGORIES');
         $data['template'] = 'inventory/categories_products_view';
        $this->load->view('template',$data);
    }

    //MATERIAL CATEGORIES HOME
    public function materials() {
     
          $data['template'] = 'inventory/categories_materials_view';
          $this->load->view('template',$data);
    }
    
     //GET RESOURCES
    public function getmaterialscat() {
        $where = array('PARENTID' => '-1');
        $data['data'] = $this->common->getWhere('CATEGORIES',$where);
        //$data['data'] = $this->common->getAll('CATEGORIES');
        echo json_encode($data);
    }


       public function addmaterialscat() {
        $id = $this->input->post('id');
        $data = array(    
            'ID' => uniqid(),
            'NAME' => $this->input->post('NAME'),
            'PARENTID' => '-1'
        );
       if($id == '')
       { 
          $existdata = $this->common->existdata('CATEGORIES',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

              $this->common->insert('CATEGORIES',$data);
              $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
         
       }
        else
       {
          $where = array('ID' => $id);
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('CATEGORIES',$where,$data);
       }
        redirect('inventory/categories/materials');
 
    }
    
      public function editmaterialscat($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('CATEGORIES',$where);

        echo json_encode($data);
 
    }
       public function deletematerialscat($id) {

        $where = array('ID' => $id);  
        $this->common->delete('CATEGORIES',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/categories/materials');
 
    }


    //GET PRODUCT CATEGORIES
    public function getproductcat() {
          //$where = array('PARENTID' => 'NULL');
//          $sql = 'SELECT C.ID, C.NAME, C.PARENTID, C2.NAME AS PARENTNAME FROM CATEGORIES C LEFT JOIN CATEGORIES C2 ON C.PARENTID=C2.ID WHERE (C.PARENTID IS NULL AND C.ID <> "-1" AND C.ID <> "-2") OR (C.PARENTID <> "-1" AND C.PARENTID <> "-2")';
          $sql  =   'SELECT C.ID, C.NAME, C.PARENTID, C2.NAME AS PARENTNAME FROM CATEGORIES C LEFT JOIN CATEGORIES C2 ON C.PARENTID=C2.ID';
//                  . ' WHERE (C.PARENTID IS NULL AND C.ID <> "-1" AND C.ID <> "-2") OR (C.PARENTID <> "-1" AND C.PARENTID <> "-2")';
//          echo $sql;exit;
          $data['data'] = $this->common->customquery($sql);
         //$data['data'] = $this->common->getAll('CATEGORIES');
          echo json_encode($data);
    }

 public function addproductcat() {
        
        $data = array( 

            'NAME' => $this->input->post('NAME'),
            'PARENTID' => ($this->input->post('PARENTID')) ? $this->input->post('PARENTID') : NULL
        );
       if($this->input->post('id')==NULL)
       {  

                 $data['ID'] = uniqid();
           $existdata = $this->common->existdata('CATEGORIES',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

              $this->common->insert('CATEGORIES',$data);
              $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
         
       
       }
        else
       {
          $where = array('ID' => $this->input->post('id'));
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('CATEGORIES',$where,$data);
       }
        redirect('inventory/categories/products');
 
    }
    
      public function editproductcat($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('CATEGORIES',$where);

        echo json_encode($data);
 
    }
       public function deleteproductcat($id) {

        $where = array('ID' => $id);  
        $this->common->delete('CATEGORIES',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/categories/products');
 
    }



    //GET MATERIAL CATEGORIES FOR GRID
    public function getmaterialcategoriesgrid() {
        $this->load->model('inventory/categories_model');
        echo $this->categories_model->getmaterialcategoriesgrid();
    }
   
    //ALL CATEGORIES DROPDOWN LIST
    private function getallcategoriescombo($id = NULL) {
        $this->load->model('inventory/categories_model');
        $catarr = $this->categories_model->getallcategories();
        $data = '<option value="">No parent category</option>';
        for ($i = 0; $i < count($catarr); $i++) {
            if ($id != NULL && $id == $catarr[$i]->ID)
                $data .= "<option id=\"option_custom\" selected=\"yes\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
            else
                $data .= "<option id=\"option_custom\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
        }
        return $data;
    }

    //PRODUCT CATEGORY DROPDOWN LIST
    private function getproductcategoriescombo($id = NULL) {
        $this->load->model('inventory/categories_model');
        $catarr = $this->categories_model->getproductcategories();
        $data = '<option value="">No parent category</option>';
        for ($i = 0; $i < count($catarr); $i++) {
            if ($id != NULL && $id == $catarr[$i]->ID)
                $data .= "<option id=\"option_custom\" selected=\"yes\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
            else
                $data .= "<option id=\"option_custom\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
        }
        return $data;
    }

    //PRODUCT CATEGORY DROPDOWN LIST
    private function getmaterialcategoriescombo($id = NULL) {
        $this->load->model('inventory/categories_model');
        $catarr = $this->categories_model->getmaterialcategories();
        $data = '<option value="">None</option>';
        for ($i = 0; $i < count($catarr); $i++) {
            if ($id != NULL && $id == $catarr[$i]->ID)
                $data .= "<option id=\"option_custom\" selected=\"yes\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
            else
                $data .= "<option id=\"option_custom\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
        }
        return $data;
     }

    //FOR PRODUCT CATEGORIES AUTOCOMPLETE
    function getcategoriesautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/categories_model');
        $rows = $this->categories_model->getcategoriesautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
    
   
    
    function getparentcategoriesautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/categories_model');
        $rows = $this->categories_model->getparentcategoriesautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
    
    //MATERIALCATEGORY AUTOCOMPLETE
    function getmatcategoriesautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/categories_model');
        $rows = $this->categories_model->getmatcategoriesautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
    
    //PRODUCTCATEGORY AUTOCOMPLETE WITH ID
    function getprodcategoriesautocomplete() {
         $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/categories_model');
            $rows = $this->categories_model->getprodcategoriesautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID
            ));

            echo json_encode($keywords);
      }
      
      //PRODUCTCATEGORY AUTOCOMPLETE WITH ID
    function getprodcategoriesautocompletewithnextbarcode() {
         $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/categories_model');
            $rows = $this->categories_model->getprodcategoriesautocompletewithnextbarcode($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID,
                'nextbarcode' => $row->CODE
            ));

            echo json_encode($keywords);
      }
      
             
    //TAXCATEGORY AUTOCOMPLETE
    function gettaxcategoriesautocomplete() {
         $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/categories_model');
            $rows = $this->categories_model->gettaxcategoriesautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID
            ));

            echo json_encode($keywords);
      
    }
      function getmaterialcatautocomplete() {
         $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/categories_model');
            $rows = $this->categories_model->getmaterialcatautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID
            ));

            echo json_encode($keywords);
      
    }
    
    private function getparentcat($name) {
        $this->load->model('inventory/categories_model');
        return $this->categories_model->getparentcat($name);
    }
    function getcategoryautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/categories_model');
        $rows = $this->categories_model->getcategoryautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
     //ADD-EDIT-DELETE PRODUCT CATEGORY
    function record()
    {
        $this->load->model('inventory/categories_model');
        $oper = $this->input->post('oper');
        $parentcategory = $this->getparentcat($this->input->post('PARENTID'));
        $id = uniqid();
        /*if($oper=='del')
        {
            $id = $this->input->post('id');
            if($id)
            {
                $this->categories_model->delete($id);
                $fdata = array('msgt' => 'S', 'msg' => 'Product deleted successfully.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/categories/products');
            }
            else {
                $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete product.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/categories/products');
            }
        }
        else
        {*/
            $form_data =  array(
                            'ID' => $id,
                            'NAME' => $this->input->post('NAME'),
                            'PARENTID' => $parentcategory
                          );
            var_dump($form_data);
        
            if($oper=='add'){
                if ($this->categories_model->add($form_data) == TRUE) {
                    $fdata = array('msgt' => 'S', 'msg' => 'Product added successfully.');
                    $this->session->set_flashdata($fdata);
                    return true;
                }
            }
            elseif($oper=='edit'){
                $id = $this->input->post('ID');
                if($id){
                    $this->categories_model->update($id, $form_data);   
                }
            }
        
    }
    
    //ADD-EDIT-DELETE MATERIAL CATEGORY
    function materialcat()
    {
        $this->load->model('inventory/categories_model');
        $oper = $this->input->post('oper');
        $id = uniqid();
        /*if($oper=='del')
        {
            $id = $this->input->post('id');
            if($id){
                $this->categories_model->deletematerialcat($id);
                $fdata = array('msgt' => 'S', 'msg' => 'Product deleted successfully.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/categories/products');
            }
            else {
                $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete product.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/categories/products');
            }
        }
        else{*/
            $form_data =  array(
                            'ID' => $id,
                            'NAME' => $this->input->post('NAME'),
                            'PARENTID' => -1
                         );
            var_dump($form_data);
                
            if($oper=='add'){
                if ($this->categories_model->addmaterialcat($form_data) == TRUE) {
                    $fdata = array('msgt' => 'S', 'msg' => 'Product added successfully.');
                    $this->session->set_flashdata($fdata);
                    return true;
                }
            }
            elseif($oper=='edit'){
                $id = $this->input->post('ID');
                if($id){
                     $this->categories_model->updatematerialcat($id, $form_data);   
                }   
            }
        
    }
    
   
    //to check
     function getprodcat() {
        $this->load->model('inventory/products_model');
        $rows = $this->products_model->getprodcat();
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
   
    function deletecategories()
    { $this->load->model('inventory/categories_model');
        if ($this->uri->segment(4)) 
            {
           
           if( $this->categories_model->delete($this->uri->segment(4))==true){
            $fdata = array('msgt' => 'S', 'msg' => 'Category deleted successfully.');
            $this->session->set_flashdata($fdata);
            redirect(site_url() . 'inventory/categories/products');
        } else{
            $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete Category.');
            $this->session->set_flashdata($fdata);

            redirect(site_url() . 'inventory/categories/products');
        }
    }}
    function deletematerialcat()
    { $this->load->model('inventory/categories_model');
        if ($this->uri->segment(4)) 
            {
           
           if( $this->categories_model->deletematerialcat($this->uri->segment(4))==true){
            $fdata = array('msgt' => 'S', 'msg' => 'Material Category deleted successfully.');
            $this->session->set_flashdata($fdata);
            redirect(site_url() . 'inventory/categories/materials');
        } else{
            $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete Material Category.');
            $this->session->set_flashdata($fdata);

            redirect(site_url() . 'inventory/categories/materials');
        }
    }}
}
