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

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

/**
 * Class : purchaseorder (PurchaseorderController)
 * User Class to control all user related operations.
 * @author : VenbaMail Mali
 * @version : 1.1
 * @since : 15 Feb 2016
 */
class assetdetails extends BaseController
{
    /**
     * This is default constructor of the class
     */
	 public function __construct()
    {
        parent::__construct();
        $this->load->model('assetdetails_model');
		 $this->load->library('form_validation');
        $this->isLoggedIn();   
    }
	/**
     * This function used to load the first screen of the user
     */
	public function index()
    {
        $this->global['pageTitle'] = 'Siddharth : Asset Details';
        
        $this->loadViews("assetListing", $this->global, NULL , NULL);
    }
	
	/**
     * This function is used to load the Supplier list
     */
    function assetListing()
    {
        
            $this->load->model('assetdetails_model');
        
           
            
            $data['userRecords'] = $this->assetdetails_model->assetListing();
            
            $this->global['pageTitle'] = 'Siddharth : Asset List';
            
            $this->loadViews("assetListing", $this->global, $data, NULL);
        
    }
	/**
     * This function is used to load the add new supplier    */
    function addasset()
    {
        
            $data['SupplierList'] = $this->assetdetails_model->getSupplierName();
			$data['DepartmentList'] = $this->assetdetails_model->getDepartment();   
			$data['AssetStatus'] = $this->assetdetails_model->getConfigValue('C015');  
            
            $this->global['pageTitle'] = 'siddharth : Add Asset';

            $this->loadViews("addasset", $this->global, $data, NULL);
        
    }
	/* Validation for Department  Dropdown
	*/
	function  Department_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('Department_validate', 'Please Select Department.');
        return false;
    }
    else // user picked something
    {
        return true;
    }
}
/* Validation for Supplier  Dropdown
	*/
	function  Supplier_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('Supplier_validate', 'Please Select Supplier.');
        return false;
    }
    else // user picked something
    {
        return true;
    }
}

/* Validation for Supplier  Dropdown
	*/
	function  Asset_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('Asset_validate', 'Please Select Asset Status.');
        return false;
    }
    else // user picked something
    {
        return true;
    }
}
	/**
     * This function is used to add new user to the system
     */
    function addNewasset()
    {
                
            $this->form_validation->set_rules('AssetName','AssetName','trim|required');
            $this->form_validation->set_rules('Description','Description','trim|required');
            $this->form_validation->set_rules('Department','Department','trim|callback_Department_validate');
            $this->form_validation->set_rules('Location','Location','trim|required');
            $this->form_validation->set_rules('User','User','trim|required');
            $this->form_validation->set_rules('SupplierName','SupplierName','trim|callback_Supplier_validate');
            $this->form_validation->set_rules('DateOfPurchase','DateOfPurchase','trim|required');
            $this->form_validation->set_rules('DateOfCommission','DateOfCommission','trim|required');
            $this->form_validation->set_rules('Assetvalue','Assetvalue','trim|required');
			 $this->form_validation->set_rules('AssetStatus','AssetStatus','trim|callback_Asset_validate');
           
                
            if($this->form_validation->run() == FALSE)
            {
                $this->addasset();
            }
            else
            {
               $AssetName = strtoupper($this->input->post('AssetName'));
                $Description = $this->input->post('Description');
                $Department = $this->input->post('Department');
                $Location = $this->input->post('Location');
                $User = $this->input->post('User');
                $SupplierName = $this->input->post('SupplierName');
                $DateOfPurchase = $this->input->post('DateOfPurchase');
                $DateOfCommission = $this->input->post('DateOfCommission');
                $CreatedBy = $this->session->userdata('userId');
                $AssetValue = $this->input->post('Assetvalue');
                $DOPurchase = $this->getDateformat($DateOfPurchase);
				$DOCommission=  $this->getDateformat($DateOfCommission);
				$AssetStatus =  $this->input->post('AssetStatus');
				$Remarks =  $this->input->post('Remarks');
				
				 $chked =  $this->input->post('isactive');
				
					if( $chked != '')
					{
						$IsActive = '1';
					}
					else
					{
						$IsActive = '0';
					}
					
					
					$asset = array();
				
				
					$asset = array('AssetName'=>$AssetName, 'Description'=>$Description,'DEPCode'=>$Department,'Location'=>$Location,'UserName'=>$User,'SupplierCode'=>$SupplierName,'DateOfPurchase'=>$DOPurchase,'DateOfCommison'=>$DOCommission,'AssetValue'=>$AssetValue,'AssetStatus'=>$AssetStatus,'IsActive'=>$IsActive,'Remarks'=>$Remarks,'CreatedBy'=>$CreatedBy);  
				
                $result = $this->assetdetails_model->addNewasset($asset);
                
                if($result > 0)
                {
                   // $this->session->set_flashdata('success', 'New Asset created successfully');
					
                     redirect('assetListing','refresh');
					 echo "<script>alert('Asset Created successfully!');</script>";
                }
                else
                {
                    redirect('assetListing','refresh');
					 echo "<script>alert('Asset Not Created!');</script>";
                }               
                
            }
        
    }
	/**
     * This function is used load user edit information
     * @param number $userId : Optional : This is user id
     
	 
	 function ech(){
		 
		 echo $this->input->post('Asset_Code');
	 }*/
	function editOldasset($AssetCode = '')
    {
         
            $SupplierName = $this->input->post('SupplierName');
			$AssetStatus =  $this->input->post('AssetStatus');
			$Department =  $this->input->post('Department');
			
            if($AssetCode == '')
			{
			 $AssetID =  $_GET['AssetID']; 	
             			 
			}
			else
			{				
			  $AssetID  = $AssetCode;	
			}	
			//echo  $AssetID;
			
            $data['assetList'] = $this->assetdetails_model->getAssetDetails($AssetID);
			$data['SupplierList'] = $this->assetdetails_model->getSupplierName($SupplierName);
			$data['DepartmentList'] = $this->assetdetails_model->getDepartment($Department);
			$data['AssetStatusList'] = $this->assetdetails_model->getConfigValue('C015');  			
            $this->global['pageTitle'] = 'Siddharth : Edit Asset';
            
            $this->loadViews("editOldasset", $this->global, $data,NULL);
        
    }
	
	/**
     * This function is used to edit the user information
     */
    function editasset()
    {
        
                       
            $Asset_Code = $this->input->post('AssetCode');
            
            $this->form_validation->set_rules('AssetName','AssetName','trim|required');
            $this->form_validation->set_rules('Description','Description','trim|required');
            $this->form_validation->set_rules('Department','Department','trim|callback_Department_validate');
            $this->form_validation->set_rules('Location','Location','trim|required');
            $this->form_validation->set_rules('User','User','trim|required');
            $this->form_validation->set_rules('SupplierName','SupplierName','trim|callback_Supplier_validate');
            $this->form_validation->set_rules('DateOfPurchase','DateOfPurchase','trim|required');
            $this->form_validation->set_rules('DateOfCommission','DateOfCommission','trim|required');
            $this->form_validation->set_rules('Assetvalue','Assetvalue','trim|required');
			 $this->form_validation->set_rules('AssetStatus','AssetStatus','trim|callback_Asset_validate');
            
            if($this->form_validation->run() == FALSE)
            {
				
                $this->editOldasset($Asset_Code);
            }
            else
            {
				$Asset_Code = $this->input->post('AssetCode');
                $AssetName = strtoupper( $this->input->post('AssetName'));
                $Description = $this->input->post('Description');
                $Department = $this->input->post('Department');
                $Location = $this->input->post('Location');
                $User = $this->input->post('User');
                $SupplierName = $this->input->post('SupplierName');
                $DateOfPurchase = $this->input->post('DateOfPurchase');
                $DateOfCommission = $this->input->post('DateOfCommission');
                $UpdatedBy = $this->session->userdata('userId');
                $AssetValue = $this->input->post('Assetvalue');
                $DOPurchase = $this->getDateformat($DateOfPurchase);
				$DOCommission=  $this->getDateformat($DateOfCommission);
				$AssetStatus =  $this->input->post('AssetStatus');
				$Remarks =  $this->input->post('Remarks');
				
				 $chked =  $this->input->post('isactive');
				
					if( $chked != '')
					{
						$IsActive = '1';
					}
					else
					{
						$IsActive = '0';
					}
					
					
					$asset = array();
				
				
					$asset = array('AssetName'=>$AssetName, 'Description'=>$Description,'DEPCode'=>$Department,'Location'=>$Location,'UserName'=>$User,'SupplierCode'=>$SupplierName,'DateOfPurchase'=>$DOPurchase,'DateOfCommison'=>$DOCommission,'AssetValue'=>$AssetValue,'AssetStatus'=>$AssetStatus,'IsActive'=>$IsActive,'Remarks'=>$Remarks,'UpdatedBy'=>$UpdatedBy);               
				
				
                
                $result = $this->assetdetails_model->editasset($asset, $Asset_Code);
                
                if($result == true)
                {
					redirect('assetListing','refresh');
                    echo "<script>alert('Asset updated successfully!');</script>";
                }
                else
                {
					redirect('assetListing','refresh');
                    echo "<script>alert('Asset Not updated !');</script>";
                
				}
            }
        
    }
	
	function getDateformat($Val)
	{
		       $date = new DateTime($Val);
				$retDate = $date->format('Y-m-d H:i:s');
				return $retDate;
	}
	
	
	function export_asset(){
		
		
		$this->load->library('excel');
        $this->excel->setActiveSheetIndex(0)->mergeCells('G3:M3');
		      $this->excel->setActiveSheetIndex(0)->getStyle('G3')->getAlignment()->applyFromArray(
    array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,)
);

        $this->excel->getActiveSheet()->setTitle('Asset Details');									











        $this->excel->getActiveSheet()->setCellValue('G3', 'SIDDHARTH INDUSTRIES Asset Details');
        $this->excel->getActiveSheet()->setCellValue('C5', 'AssetCode');
        $this->excel->getActiveSheet()->setCellValue('D5', 'AssetName');
        $this->excel->getActiveSheet()->setCellValue('E5', 'Description');
        $this->excel->getActiveSheet()->setCellValue('F5', 'DEPCode');
        $this->excel->getActiveSheet()->setCellValue('G5', 'Location');
        $this->excel->getActiveSheet()->setCellValue('H5', 'UserName');
        $this->excel->getActiveSheet()->setCellValue('I5', 'SupplierCode');
        $this->excel->getActiveSheet()->setCellValue('J5', 'DateOfPurchase');
        $this->excel->getActiveSheet()->setCellValue('K5', 'DateOfCommison');
        $this->excel->getActiveSheet()->setCellValue('L5', 'AssetValue');
        $this->excel->getActiveSheet()->setCellValue('M5', 'AssetStatus');
        $this->excel->getActiveSheet()->setCellValue('N5', 'IsActive');
        $this->excel->getActiveSheet()->setCellValue('O5', 'Remarks');
        $this->excel->getActiveSheet()->setCellValue('P5', 'CreatedBy');
        $this->excel->getActiveSheet()->setCellValue('Q5', 'UpdatedBy');
        
        


        $this->excel->getActiveSheet()->getStyle('G3')->getFont()->setSize(14);
        $this->excel->getActiveSheet()->getStyle('G3')->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);
		$this->excel->getActiveSheet()->getStyle('J5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('J5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('K5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('K5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('L5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('L5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('M5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('M5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('N5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('N5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('O5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('O5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('P5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('P5')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('Q5')->getFont()->setSize(12);
        $this->excel->getActiveSheet()->getStyle('Q5')->getFont()->setBold(true);
		
		
        

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

        if ($exportData) {
            $i = 6;
            $s = 1;
            foreach ($exportData as $data) {
				
				
              $this->excel->getActiveSheet()->setCellValue('C'.  $i,$data['AssetCode']);
        $this->excel->getActiveSheet()->setCellValue('D'.  $i,$data[ 'AssetName']);
        $this->excel->getActiveSheet()->setCellValue('E'.  $i,$data[ 'Description']);
        $this->excel->getActiveSheet()->setCellValue('F'.  $i,$data[ 'DEPCode']);
        $this->excel->getActiveSheet()->setCellValue('G'.  $i,$data[ 'Location']);
        $this->excel->getActiveSheet()->setCellValue('H'.  $i,$data[ 'UserName']);
        $this->excel->getActiveSheet()->setCellValue('I'.  $i,$data[ 'SupplierCode']);
        $this->excel->getActiveSheet()->setCellValue('J'.  $i,$data[ 'DateOfPurchase']);
        $this->excel->getActiveSheet()->setCellValue('K'.  $i,$data[ 'DateOfCommison']);
        $this->excel->getActiveSheet()->setCellValue('L'.  $i,$data[ 'AssetValue']);
        $this->excel->getActiveSheet()->setCellValue('M'.  $i,$data[ 'AssetStatus']);
        $this->excel->getActiveSheet()->setCellValue('N'.  $i,$data[ 'IsActive']);
        $this->excel->getActiveSheet()->setCellValue('O'.  $i,$data[ 'Remarks']);
        $this->excel->getActiveSheet()->setCellValue('P'.  $i,$data[ 'firstname']);
        $this->excel->getActiveSheet()->setCellValue('Q'.  $i,$data[ 'Updated_By']);
           

               

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

        $filename = 'Asset_details' .' '.  '.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'] = 'CodeInsect : 404 - Page Not Found';
        
        $this->loadViews("404", $this->global, NULL, NULL);
    }
}

?>