<?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 expencecategorymodel
 *
 * @author BusyMan
 */
class SalaryExpenseSetupModel extends CI_Model {
    //put your code here
    private $_tbl_SAEXP = 'tbl_salary_expense';
    //private $_tbl_admin = 'tbl_admin';
    public function saveSalaryExpense($data)
    {
         $data['date']=date('Y-m-d',strtotime($data['date']));
    	$this->db->insert($this->_tbl_SAEXP,$data);
    	return $this->db->insert_id();
    }

    public function checkduplicat($data)
    {
         $data['date']=date('Y-m-d',strtotime($data['date']));
    	$query=$this->db->get_where($this->_tbl_SAEXP,array(
		    									'employee_id'=>$data['employee_id'],
		    									'salaryAmount'=>$data['salaryAmount'],
		    									'date'=>$data['date']
    										)
    									);
    	$result=$query->row_array();
    	if(!empty($result)){return $result;}
    }

    public function getallsalaryExpense()
    {
    	$query=$this->db->get($this->_tbl_SAEXP);
    	$result=$query->result_array();
    	if(!empty($result)){return $result;}
    }

    public function deleteSalaryExpense($id)
    {
    	$query=$this->db->delete($this->_tbl_SAEXP,array('expenseId'=>$id));
    	return $this->db->affected_rows();
    }

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

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

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

    public function getinvoiceInfoByid($expenseId) 
      {
          // $this->db->select('TU.*,TXA.*');
          // $this->db->from('tbl_extra_account TXA');
          // $this->db->join('tbl_user TU','TXA.customerId=TU.userId');
          // $this->db->where('TXA.id',$id);
          // $query=$this->db->get();
        $query=$this->db->get_where($this->_tbl_SAEXP,array('expenseId'=>$expenseId));
          $result=$query->row_array();
          if(!empty($result))
          {
            return $result;
          }

      }

      public function getTotalSalaryExpense(){
        $this->db->select_sum('salaryAmount')->from($this->_tbl_SAEXP);
        $query  = $this->db->get();
        $result = $query->row_array();
        if(!empty($result)){
            return $result;
        }
      }


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

    public function getSalaryExpenseByYear($year){
        $query  = $this->db->select_sum('salaryAmount')->get_where($this->_tbl_SAEXP,array('year' =>$year));
        $result = $query->row_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function getTotalSalaryInDateRange($data){
        $data['fromdate'] = date('Y-m-d', strtotime($data['fromdate']));
        $data['todate']   = date('Y-m-d', strtotime($data['todate']));
        $query            = $this->db->select_sum('salaryAmount')->where('date BETWEEN "' . $data['fromdate'] . '" and "' . $data['todate'] . '"')->get($this->_tbl_SAEXP);
        $result           = $query->row_array();
        if (!empty($result)) {
            return $result;
        }
    }

}