<?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 MoneyReceiptModel
 *
 * @author BusyMan
 */
class MoneyReceiptModel extends CI_Model {

    //put your code here
    private $_tbl_AC = 'tbl_land_account';
    private $_tbl_CREG='tbl_customer_reg';
    private $_tbl_PRG='tbl_project';
    private $_tbl_CHKIN= 'tbl_cheque_in';
    private $_tbl_CHKOUT= 'tbl_cheque_out';
    private $_tbl_MR= 'tbl_advance_mr';

    // public function insert_amount($data) {
    //     $data['date']=date('Y-m-d',strtotime($data['date']));
    //     $this->db->insert($this->_tbl_AC, $data);
    //     return $this->db->insert_id();
    // }

    public function insert_moneyreceipt($data) {
        $data['payment_date']=date('Y-m-d',strtotime($data['payment_date']));
        $this->db->insert($this->_tbl_MR, $data);
        return $this->db->insert_id();
             
        }

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

    public function markdonemrById($data)
    {
        $this->db->update($this->_tbl_MR,$data,array('id'=>$data['id']));
        return $this->db->affected_rows();
    }

    public function updatemrById($data)
    {
        $data['payment_date']=date('Y-m-d',strtotime($data['payment_date']));
        $this->db->update($this->_tbl_MR,$data,array('id'=>$data['id']));
        return $this->db->affected_rows();
    }

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


    public function getalldonemr(){ 
        $query=$this->db->select('*')->where('active', 1)->order_by('id','desc')->get($this->_tbl_MR);
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

    public function getallmrlist(){ 
        $query=$this->db->select('*')->where('active', 0)->order_by('id','desc')->get($this->_tbl_MR);
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

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


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

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

 // out cheques Models

    public function insert_chequeout($data) {
        $data['date']=date('Y-m-d',strtotime($data['date']));
        $this->db->insert($this->_tbl_CHKOUT, $data);
        return $this->db->insert_id();
             
        }

    public function getalloutcheques(){ 
        $query=$this->db->select('*')->where('active', 0)->order_by('id','desc')->get($this->_tbl_CHKOUT);
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

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

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

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

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


    public function getalldoneoutcheques(){ 
        $query=$this->db->select('*')->where('active', 1)->order_by('id','desc')->get($this->_tbl_CHKOUT);
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

        public function markdoneoutChequeById($data)
    {
        $this->db->update($this->_tbl_CHKOUT,$data,array('id'=>$data['id']));
        return $this->db->affected_rows();
    }

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











//previous codes below
    public function GetCustomerlist($data)
    {
        $query=$this->db->select('*')->get_where($this->_tbl_CREG,array('customerId'=>$data['customerId']));
        $result=$query->row_array();
        if (!empty($result)) {
             return $result;
        }
    }

    

    public function getallincome(){ 
    	$query=$this->db->select('*')->order_by('id','desc')->get($this->_tbl_AC);
    	$result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

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

    public function totalAmountByYear($current_year){
        $query  = $this->db->select_sum('amount')->get_where($this->_tbl_AC,array('year' =>$current_year));
        $result = $query->row_array();
        if(!empty($result)){
            return $result;
        }
    }

    public function getincomeByCustomerId($customerId){
        $query=$this->db->get_where($this->_tbl_AC,array('customerId'=>$customerId));
        $result=$query->result_array();
        if (!empty($result)) {
            return $result;
        }
    }

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

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

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

   /* public function getTotalAmountByCSId($customerId) {
        $query = $this->db->select_sum('amount')
                ->get_where($this->_tbl_AC, array('customerId' => $customerId));
        $result = $query->row_array();
        if (!empty($result)) {
            return $result;
        } else {
            return 0;
        }
    }*/

    public function checkCustomer($data){
        $query=$this->db->get_where($this->_tbl_AC,array('customerId' => $data['customerId']));
        $result = $query->row_array();
        if (!empty($result)) {
            return $result;
        }
    }
    public function updateAccountByCustomerId($data){
        $data['date']=date('Y-m-d',strtotime($data['date']));
        $this->db->update($this->_tbl_AC,$data,array('customerId'=>$data['customerId']));
         return $this->db->affected_rows();
    }
   /* public function saveamountIntoallrecord($data){
        $datae=array('customerId'=>$data['customerId'],'amount'=>$data['amount'],'date'=>$data['date'],
                     'payment_description'=>$data['payment_description'],
                     'incomeCategory'=>$data['incomeCategory'], 'receiver_id'=>$data['receiver_id']  
                    );
         $this->db->insert($this->_tbl_allAC, $datae);
        return $this->db->insert_id();
    }
    */

    public function getinvoiceInfoByid($id)
    {
        $this->db->select('crg.*,lac.*');
        $this->db->from('tbl_land_account lac');
        $this->db->join('tbl_customer_reg crg','lac.customerId=crg.customerId');
        $this->db->where('lac.id',$id);
        $query=$this->db->get();
        $result=$query->row_array();
        if(!empty($result))
        {
            return $result;
        }

    }


    public function getcustomerById($id){
        $this->db->select('tbl_customer_reg.*');
        $this->db->select('tbl_project.project_name');
        $this->db->join('tbl_project','tbl_project.project_id=tbl_customer_reg.projectcategory');    
        $query=$this->db->get_where('tbl_customer_reg', array('customerId' =>$id));
        $result=$query->row_array();
        if(!empty($result)){
            return $result;
        }
    }



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

}