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

class Fixed_Assets  extends CI_Controller {
	var $main_menu_name = "settings";
	var $sub_menu_name = 'fixed_assets_list';
	
	public function __construct()
	{
	  parent::__construct();
	  $this->load->model('Fixed_Assets_Model');	
	  $this->load->model('Common_Model');
    }
	public function index()
	{
	$data['main_menu_name'] = $this->main_menu_name;
    $data['sub_menu_name'] = 'fixed_assets_list';	
	$this->load->view('fixed_assets_list',$data);
	}
	public function create_fixed_assets()
	{
        
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = 'create_fixed_assets';
		
        if (isset($_GET['fxd_ass_id'])) {
			$fxd_ass_id=$_GET['fxd_ass_id'];
		}
		else {
			$fxd_ass_id='';
		}
		if($fxd_ass_id){
			$data['fxd_ass_id']=$fxd_ass_id;
			$data['type']='E';
			$data['pageName']='UPDATE ASSETS/LIABILITIES';
			$data['btnText']='Update';
			$data['fixed_assets']= $this->Fixed_Assets_Model->get_fixed_asset_details($fxd_ass_id);	
		}
		else {
			$data['fxd_ass_id']='';
			$data['type']='A';
			$data['pageName']='ADD ASSETS/LIABILITIES';
			$data['btnText']='Add';
			$data['fixed_assets']=array();
		}
		$data['fixed_assets_type']=$this->Fixed_Assets_Model->fixed_assets_type_list();
        $this->load->view('models/create_fixed_assets',$data);	
	}

public function save_fixed_assets()
	{
		$fxd_ass_id=$this->input->post('fxd_ass_id');
		$type=$this->input->post('type');
		$fxd_ass_serial_no=$this->input->post('fxd_ass_serial_no');
		$fxd_ass_name=$this->input->post('fxd_ass_name');
		$fa_type_id=$this->input->post('fa_type_id');
		$fxd_ass_amount=$this->input->post('fxd_ass_amount');
		$fxd_ass_details=$this->input->post('fxd_ass_details');
		$added_date=$this->input->post('fxd_ass_added_date');
		$fxd_ass_added_date=date('Y-m-d',strtotime($added_date));
		$user_id=$this->session->userdata('ss_user_id');
		$fxd_ass_status=$this->input->post('fxd_ass_status');
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('fxd_ass_serial_no','fxd_ass_name','fa_type_id','fxd_ass_amount','fxd_ass_added_date','fxd_ass_status','required');
		}
		else if($type=='E')
		{
			$this->form_validation->set_rules('fxd_ass_serial_no','fxd_ass_name','fa_type_id','fxd_ass_amount','fxd_ass_added_date','fxd_ass_status','required');
		}
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
		$data=array(
			'fxd_ass_serial_no'=>$fxd_ass_serial_no,
			'fxd_ass_name'=>$fxd_ass_name,	
			'fa_type_id'=>$fa_type_id,
			'fxd_ass_amount'=>$fxd_ass_amount,
			'user_id'=>$user_id,
			'fxd_ass_details'=>$fxd_ass_details,
			'fxd_ass_added_date'=>$fxd_ass_added_date,	
			'fxd_ass_status'=>$fxd_ass_status,
			'debit_credit'=>$this->input->post('debit_credit'),
		);
		
		$_insert=$this->Fixed_Assets_Model->save_fixed_assets($data,$fxd_ass_id);
		$lastid=$this->db->insert_id();

		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type,'status' =>1));
			} else {
				echo json_encode(array('status'=>'0'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type,'status' =>1));
		}	
			
		}
	}
public function fixed_assets_load()
{
$fixed_asset_list=$this->Fixed_Assets_Model->get_fixed_asset_list();
$data=array();
$x=0;
foreach($fixed_asset_list as $row){
	$x++;
	$nestedData=array();
	$nestedData[]=$x;
	$nestedData[]=$row['fxd_ass_name'];
	$nestedData[]=$row['fa_type_name'];
	$nestedData[]=$row['fam_name'];
	//$nestedData[]=$row['fxd_ass_amount'];
	$status='';
	if($row['fxd_ass_status']==1)
	{
	$status='<center><span class="label label-success">Enable</span></center>';
	}
	else if($row['fxd_ass_status']==0)
	{
	$status='<center><span class="label label-warning">Disable</span></center>';
	}
	$nestedData[]=$status;
	$action='<center><div class="btn-group text-left">
                       
					        <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button">Actions <span class="caret"></span></button>
                            <ul role="menu" class="dropdown-menu pull-right">
                            <li><a href="#" onclick="update_fix_assets('.$row['fxd_ass_id'].'); return false;"><i class="fa fa-file-text-o"></i>Update Fixed Assets</a></li>
							</ul></div></center>';
	$nestedData[]=$action;
	$data[]=$nestedData;
	}
	$json_data = array(
	"data"=>$data
	);
	
	echo json_encode($json_data);	
}	
}