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

require APPPATH . '/libraries/BaseController.php';

/**
 * Class : RawMaterial (PurchaseorderController)
 * User Class to control all user related operations.
 * @author : Gandhimathi
 * @version : 1.1
 * @since : 12 Apr 2017
 */
class rawmaterialdetails extends BaseController
{
    /**
     * This is default constructor of the class
     */
	 public function __construct()
    {
        parent::__construct();
        $this->load->model('rawmaterialdetails_model');
		 $this->load->library('pagination');
		  $this->load->library('form_validation');  
		  $this->load->library('Excel');  
        
		$this->isLoggedIn();   
    }
	/**
     * This function used to load the first screen of the user
     */
	public function index()
    {
        $this->global['pageTitle'] = 'Siddharth : RawMaterialDetails';
        
        $this->loadViews("rawmaterialListing", $this->global, NULL , NULL);
    }
	
	/**
     * This function is used to load the RawMaterialDetails list
     */
    function rawmaterialListing()
    {
        
            $this->load->model('rawmaterialdetails_model');
        
           
            
            $data['userRecords'] = $this->rawmaterialdetails_model->rawmaterialListing();
            
            $this->global['pageTitle'] = 'Siddharth : RawMaterial Listing';
            
            $this->loadViews("rawmaterialListing", $this->global, $data, NULL);
        
    }
	/**
     * This function is used to load the add new cost    */
    function addRawMaterial()
    {
        
            $this->load->model('rawmaterialdetails_model');
            $data['material'] = $this->rawmaterialdetails_model->getmaterialType();
            $data['UOM'] = $this->rawmaterialdetails_model->getUOM();            
            
            $this->global['pageTitle'] = 'siddharth : AddNew RawMaterial';

            $this->loadViews("addRawMaterial", $this->global, $data, NULL);
        
    }
	/*
	 Validation for Material Type Dropdown
	*/
	function  MaterialType_validate($selectValue)
{
	//echo 'select_validate  method called';
    // 'none' is the first option and the text says something like "-Choose one-"
    if($selectValue == '-1')
    {
        $this->form_validation->set_message('MaterialType_validate', 'Please Select Material Type.');
        return false;
    }
    else // user picked something
    {
        return true;
    }
}
/*
	 Validation for Material Type Dropdown
	*/
	function  UOM_validate($selectValue)
{
	//echo 'select_validate  method called';
    // 'none' is the first option and the text says something like "-Choose one-"
    if($selectValue == '-1')
    {
        $this->form_validation->set_message('UOM_validate', 'Please Select UOM.');
        return false;
    }
    else // user picked something
    {
        return true;
    }
}
     /* This function is used to add new Cost to the system
     */
    function addNewRawMaterial()
    {
		
            $this->form_validation->set_rules('MaterialName','Material Name','trim|required|');
	        $this->form_validation->set_rules('MaterialType','MaterialType','trim|callback_MaterialType_validate');
			$this->form_validation->set_rules('UOM','UOM','trim|callback_UOM_validate');
                       
            if($this->form_validation->run() == FALSE)
            {
                $this->addRawMaterial();
            }
            else
            {
                $MaterialName = $this->input->post('MaterialName');
                $MaterialType = $this->input->post('MaterialType');
                $UOM = $this->input->post('UOM');
                $CreatedBy = $this->session->userdata('userId');
				
                $chked =  $this->input->post('isactive');
				
					if( $chked != '')
					{
						$IsActive = '1';
					}
					else
					{
						$IsActive = '0';
					}
					
					$RawMaterial = array('MaterialName'=>$MaterialName, 'MaterialType'=>$MaterialType, 'UOM'=>$UOM,'CreatedBy'=>$CreatedBy,'IsActive' => $IsActive);    
                
                   $result = $this->rawmaterialdetails_model->addNewRawMaterial($RawMaterial);
                
                if($result > 0)
                {
                   
                                   redirect('rawmaterialListing','refresh');
					 echo "<script>alert('RawMaterial Created successfully!');</script>";
					
                }
                else
                {
                                    redirect('rawmaterialListing','refresh');
				    echo "<script>alert('RawMaterial Not Created!');</script>";
				   
                }       
               
            }        
    }
	function viewRawmaterial($RID= '')
    {
		$MaterialType = '';
		$UOM = '';
			if($RID == '')
			{
			 $RawMaterialID =  $_GET['RID']; 
			 $MaterialType = $_GET['MType'];
			 $UOM = $_GET['UOM'];
			}
			else
			{				
			  $RawMaterialID  = $RID;	
			}	
			
            $data['materialDetails'] = $this->rawmaterialdetails_model->getMaterialDetails($RawMaterialID);
			
			$data['materialType'] = $this->rawmaterialdetails_model->getmaterialType($MaterialType );
            $data['UOMList'] = $this->rawmaterialdetails_model->getUOM($UOM); 
            $this->global['pageTitle'] = 'Siddharth : Edit Material Master';
            $this->loadViews("editOldRawmaterial", $this->global,$data, NULL);
    }
	
	
	/**
     * This function is used to edit the user information
     */
    function editRawmaterial()
    {
		if($this->isAdmin() == TRUE)
        {
			$this->loadThis();			
        }
        else
        {
	    $RawmaterialID = $this->input->post('MaterialCode');
            $this->form_validation->set_rules('MaterialName','Material Name','trim|required|');
            $this->form_validation->set_rules('MaterialType','MaterialType','trim');
            $this->form_validation->set_rules('UOM','UOM','trim');
            
            if($this->form_validation->run() == FALSE)
            {
                $this->viewRawmaterial($RawmaterialID);
				
            }
            else
            {
				$MaterialCode = $this->input->post('MaterialCode');
                $MaterialName = $this->input->post('MaterialName');
                $MaterialType = $this->input->post('MaterialType');
                $UOM = $this->input->post('UOM');
                $UpdatedBy = $this->session->userdata ('userId');
				 $chked =  $this->input->post('isactive');
				
					if( $chked != '')
					{
						$IsActive = '1';
					}
					else
					{
						$IsActive = '0';
					}
					
                $RawMaterial = array();
				$RawMaterial = array('MaterialName'=>$MaterialName,'MaterialType'=>$MaterialType,'UpdatedBy'=>$UpdatedBy, 'UOM'=>$UOM,'IsActive' => $IsActive);    
				  
                $result = $this->rawmaterialdetails_model->editRawmaterial($RawMaterial, $MaterialCode);
            
                if($result == true)
                {
					redirect('rawmaterialListing','refresh');
                  
					 echo "<script>alert('RawMaterial Updated successfully!');</script>";
                }
                else
                {
					redirect('rawmaterialListing','refresh');
					echo "<script>alert('RawMaterial Record Not updated!');</script>";
                   
                }
                
               
            }
        }
    }
	/** this function can be use for excel report **/
	
	function export_material(){
		
		
		$this->load->library('excel');
        $this->excel->setActiveSheetIndex(0)->mergeCells('D3:J3');

        $this->excel->getActiveSheet()->setTitle('Material Master');									

        $this->excel->getActiveSheet()->setCellValue('D3', 'SIDDHARTH INDUSTRIES Raw Material');
        $this->excel->getActiveSheet()->setCellValue('C5', 'MaterialCode');
        $this->excel->getActiveSheet()->setCellValue('D5', 'MaterialName');
        $this->excel->getActiveSheet()->setCellValue('E5', 'MaterialType');
        $this->excel->getActiveSheet()->setCellValue('F5', 'UOM');
        $this->excel->getActiveSheet()->setCellValue('G5', 'Created by');
        $this->excel->getActiveSheet()->setCellValue('H5', 'Updated By');
        $this->excel->getActiveSheet()->setCellValue('I5', 'IsActive');
        
        


        $this->excel->getActiveSheet()->getStyle('D3')->getFont()->setSize(14);
        $this->excel->getActiveSheet()->getStyle('D3')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('C5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('C5')->getFont()->setBold(true);
        $this->excel->getActiveSheet()->getStyle('D5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('D5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('E5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('E5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('F5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('F5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('G5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('G5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('H5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('H5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('I5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('I5')->getFont()->setBold(true);
		
		
        

       $exportData = $this->rawmaterialdetails_model->export_excel();

        if ($exportData) {
            $i = 6;
            $s = 1;
            foreach ($exportData as $data) {
				
				$this->excel->getActiveSheet()->setCellValue('C' .  $i,$data['MaterialCode']);
				$this->excel->getActiveSheet()->setCellValue('D' .  $i,$data['MaterialName']);
				$this->excel->getActiveSheet()->setCellValue('E' .  $i,$data['MaterialType']);
				$this->excel->getActiveSheet()->setCellValue('F' .  $i,$data['UOM']);
				$this->excel->getActiveSheet()->setCellValue('G' .  $i,$data['firstname']);
				$this->excel->getActiveSheet()->setCellValue('H' .  $i,$data['Updated_By']);
				$this->excel->getActiveSheet()->setCellValue('I' .  $i,$data['IsActive']);
				
              
           

               

                $i++;
                $s++;
            }
        }

        $filename = 'RawMaterial' .' '.  '.xls'; //save our workbook as this file name
        header('Content-Type: application/vnd.ms-excel'); //mime type
        header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name
        header('Cache-Control: max-age=0'); //no cache
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        if (ob_get_contents()) ob_end_clean();
        ob_start();
        $objWriter->save('php://output');


    }
	
    function pageNotFound()
    {
        $this->global['pageTitle'] = 'Siddharth : 404 - Page Not Found';
        
        $this->loadViews("404", $this->global, NULL, NULL);
    }
}

?>+