<?php

if (!defined('BASEPATH')

    )exit('No direct script access allowed');

class Materials extends MY_Controller {

   function __construct() {
        parent::__construct();
         $this->load->model('inventory/materials_model');
       
    }
    
    // GET CATEGORIES IN COMBO
    function getcategoriescombo($id = NULL) {
        $this->load->model('inventory/materials_model');
        $catarr = $this->materials_model->categorydropdown();
        $data = '<option value="">Select</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;
    }
    
    function index() {
        $this->load->model('inventory/materials_model');
          $data['category'] = $this->common->getAll('CATEGORIES');
          $data['taxcategory'] = $this->common->getAll('TAXCATEGORIES');
            $data['uom'] = $this->common->getAll('UNITS');
         $data['template'] = 'inventory/materials_view';
          $this->load->view('template',$data);
    }

    function getmaterials() {
        $this->load->model('inventory/materials_model');
        $data['data'] = $this->materials_model->getmaterials();
        echo json_encode($data);
    }

   
    private function getuom($id = NULL) {
        $this->load->model('settings/uom_model');
        $catarr = $this->uom_model->getuomcombo();
        $data = '<option value="">Select</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;
    }
    
    private function gettaxcategories($id = NULL) {
        $this->load->model('inventory/materials_model');
        $catarr = $this->materials_model->gettaxcat();
        $data ='';
        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 UOM AUTOCOMPLETE
    function getuomautocomplete(){
           $term = $this->input->post('term', TRUE);
            $this->load->model('settings/uom_model');
            $rows = $this->uom_model->getuomautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID
            ));
            echo json_encode($keywords);
     }
      private function gettaxcategoriesselect($id = NULL) {
        $this->load->model('inventory/materials_model');
        $catarr = $this->materials_model->gettaxcat();
      $data = '<option value="">Select</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;
    }
    
    // GET MATERIALREQUESTS BY CATEGORY
    function getlinesbycategory() {
        $this->load->model('inventory/materials_model');
        $datefrom = $this->input->post('datefrom');
        $dateto = $this->input->post('dateto');
        $product = $this->input->post('product');
        if ($product) {
            $this->load->model('inventory/products_model');
            $pid = $this->products_model->getpidbn($product);
        } else {
            $pid = null;
        }
        echo $this->materials_model->getlinesbycategory($datefrom, $dateto, $pid);
    }
    private function getcat($name) {
        $this->load->model('inventory/products_model');
        return $this->products_model->getcat($name);
    }
     private function gettaxcat1($name) {
        $this->load->model('inventory/products_model');
        return $this->products_model->gettaxcat1($name);
    }
    
        function exportcsv()
    {
            $file="materials.csv";
            header("Content-Disposition: attachment; filename=" . urlencode($file));
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header("Content-Description: File Transfer");
            header("Content-type: application/csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            
            $export= $this->input->post('exportdata');
            $this->load->helper('download');
            force_download($file, $export);
    }
         function addmaterial() {
            $id = $this->input->post('id');
            if($id == '') { 
            $form_data = array(
                'ID' => uniqid(),
                'REFERENCE' => $this->input->post('REFERENCE'),
                'CODE' => $this->input->post('REFERENCE'),    
                'NAME' => $this->input->post('NAME'),
                'CATEGORY' => $this->input->post('CATID'),
                'TAXCAT' => $this->input->post('TAXCATID'),
                'PRICEBUY' => $this->input->post('PRICEBUY'),                    
                'PRICESELL' => 0     
            );
            $existdata = $this->common->existdata('PRODUCTS',$form_data);
//            print_r( $existdata);die();
           if(!empty($existdata))
           {
             $this->session->set_flashdata('success_msg', 'The record is already exist');
             redirect('inventory/materials');
           }
           else
           {
            
            $productid = $this->common->insert('PRODUCTS',$form_data);              
           $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }

           
            $form_data2 = array(
                       'ID' => uniqid(),
                    'MATERIAL'=> $productid,
                    'AMOUNT' => $this->input->post('QUANTITY'),
                    'PRICEBUY' => $this->input->post('PRICEBUY'),
                    'UNIT' => $this->input->post('UOM'),
                );
            
             $form_data3 = array(
                    'ID' => uniqid(),
                    'LOCATION' => 0,
                    'PRODUCT' => $productid,
                    'UNITS' => $this->input->post('QUANTITY')
                );
            
         
      
             if ($productid) {
                    $this->materials_model->add_materials_units($form_data2);
                    $this->materials_model->add_stockcurrent($form_data3);
               $this->session->set_flashdata('success_msg', 'Product added successfuly.');
                //return true;
             }
         }

          else
       {
                    $form_data4 = array(
                    'REFERENCE' => $this->input->post('REFERENCE'),
                    'CODE' => $this->input->post('REFERENCE'),    
                    'NAME' => $this->input->post('NAME'),
                    'CATEGORY' => $this->input->post('CATID'),
                    'TAXCAT' => $this->input->post('TAXCATID'),
                    'PRICEBUY' => $this->input->post('PRICEBUY'),                    
                    'PRICESELL' => 0     
               );
            $form_data5 = array(
                    'AMOUNT' => $this->input->post('QUANTITY'),
                    'PRICEBUY' => $this->input->post('PRICEBUY'),
                    'UNIT' => $this->input->post('UOM'),
                );
                  $this->materials_model->update($id, $form_data4);
                  $this->materials_model->update_materials_units($id, $form_data5);
                   $this->session->set_flashdata('success_msg', 'Product updated successfuly.');
       }

              redirect('inventory/materials');
     

    }
    
       public function editmaterials($id) {
        $pid = $id;
        $data = $this->materials_model->editmaterials($pid);

        echo json_encode($data);
 
    }
    
    function deletepmaterial($id){
         $where = array('ID' => $id);  
        $this->common->delete('PRODUCTS',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/materials');
     }
  
}