<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of incomecategorymodel
 *
 * @author BusyMan
 */
class ExpenseSetupModel extends CI_Model {
    //put your code here
    private $_tbl_EX = 'tbl_expense';
    private $_tbl_SAEX = 'tbl_salary_expense';
    public function saveExpense($data) {
        $data['date']=date('Y-m-d',strtotime($data['date']));
        $this->db->insert($this->_tbl_EX, $data);
        return $this->db->insert_id();
    }
    public function getexpenseInfo(){
    	$query=$this->db->get($this->_tbl_EX);
    	$result=$query->result_array();
    	if(!empty($result)){
    		return $result;
    	}
    }

    public function getexpenseInfoById($expenseId)
    {
        $query=$this->db->get_where($this->_tbl_EX,array('expenseId' =>$expenseId));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function updateExpenseByid($data,$expenseId)
    {
         $data['date']=date('Y-m-d',strtotime($data['date']));
        $this->db->update($this->_tbl_EX,$data,array('expenseId'=>$expenseId));
        return $this->db->affected_rows();
    }

    public function deleteExpenseByid($expenseId)
    {
        $this->db->delete($this->_tbl_EX,array('expenseId'=>$expenseId));
        return $this->db->affected_rows();
    }

    public function getexpenseBydate($data)
    {
         if(!empty($data['date'])){$data['date']=date('Y-m-d',strtotime($data['date']));}
        if(empty($data['expenseCategory'])){
            $query=$this->db->get_where($this->_tbl_EX,array('date' =>$data['date']));
        }elseif(empty($data['date'])){
            $query=$this->db->get_where($this->_tbl_EX,array('expenseCategory' =>$data['expenseCategory']));
        }else{
            $query=$this->db->get_where($this->_tbl_EX,array('date' =>$data['date'],'expenseCategory'=>$data['expenseCategory']));
        }
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        }  
    }

    public function getsalaryexpenseBydate($data)
    {
         $data['date']=date('Y-m-d',strtotime($data['date']));
        $query=$this->db->get_where($this->_tbl_SAEX,array('date' =>$data['date']));
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function getBydaterange($data)
    {
         $data['fromdate']=date('Y-m-d',strtotime($data['fromdate']));
         $data['todate']=date('Y-m-d',strtotime($data['todate']));
         if(empty($data['expenseCategory']) || $data['expenseCategory']==""){
            $query=$this->db->get_where($this->_tbl_EX,'date BETWEEN "'. $data['fromdate']. '" and "'. $data['todate'].'"');
         }  else {
             $query =$this->db->where('date BETWEEN "' . $data['fromdate'] . '" and "' . $data['todate'] . '"')
                             ->where('expenseCategory',$data['expenseCategory'])->get($this->_tbl_EX);
         }
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        } 
    }

    public function getsalaryBydaterange($data)
    {
         $data['fromdate']=date('Y-m-d',strtotime($data['fromdate']));
         $data['todate']=date('Y-m-d',strtotime($data['todate']));
        $query=$this->db->get_where($this->_tbl_SAEX,'date BETWEEN "'. $data['fromdate']. '" and "'. $data['todate'].'"');
        $result=$query->result_array();
        if(!empty($result)){
            return $result;
        }
    }

    /*public function getTotalExpense()
    {
        $query=$this->db->select_sum('expenseAmount')->get_where($this->_tbl_EX,array('year'=>date('Y')));        
        $result=$query->row_array();
        if(!empty($result)){return $result;}
    }*/

}