<?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 incomesetup
 *
 * @author BusyMan
 */
class ExtraIncomeSetup extends OWN_Controller {

    //put your code here 
    function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('incomecategorymodel/incomecategorymodel', 'incomecategorymodel');
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
        $this->load->model('extraincomesetupmodel/extraincomesetupmodel', 'extraincomesetupmodel');
        $this->load->model('notificationsetupmodel/notificationsetupmodel', 'notificationsetupmodel');
        $this->load->model('balancesetupmodel/balancesetupmodel','balancesetupmodel');
        $this->load->model('employeemodel/employeemodel','employeemodel');

    }

    public function index() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            //$data['allcustomer']=$this->generalcustomerManagmodel->getallCustomer();
            $data['incomeCategory'] = getCategoryInfo();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/extraincome/incomeEntryExtra', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function insert_income() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'data[incomeCategory]',
                'label' => 'incomeCategory',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[amount]',
                'label' => 'Amount',
                'rules' => 'required|xss_clean|trim|numeric'
            ),
            array(
                'field' => 'data[paymentProcess]',
                'label' => 'Payment Process',
                'rules' => 'required|xss_clean|trim|numeric'
            ),
            array(
                'field' => 'data[date]',
                'label' => 'Date',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[payment_description]',
                'label' => 'Payment Description',
                'rules' => 'xss_clean|trim'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == false) {
            $errormessage['errormessage'] = "Payment Information Missing......!";
            $this->session->set_userdata($errormessage);
            //$data['allcustomer']=$this->generalcustomerManagmodel->getallCustomer();
            $data['incomeCategory'] = getCategoryInfo();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/extraincome/incomeEntryExtra', $data);
            $this->load->view('common/footer');
        } else {
            $data = $this->input->post('data', true);
            $data['pay_order_ddNo'] = $this->input->post('pay_order_ddNo', true);
            $data['receiver_id'] = $this->session->userdata('employeeId');
            $data['year'] = date('Y');
            //$data['paymentProcess'] = implode(',', $data['paymentProcess']) . ',';
            //print_r($data);die();
            if (!empty($data)) {
                $execution = $this->extraincomesetupmodel->insert_amount($data);
                if (!empty($execution) && $data['incomeCategory'] != null) {
                    $categoryname = getincomeCategoryById($data['incomeCategory']);
                    //print_r($categoryname);die();
                    $datan = array(
                        'sender' => $data['receiver_id'],
                        'date' => date('Y-m-d', strtotime($data['date'])),
                        'notification' => $data['amount'] . ' taka was deposited of (' . $categoryname['category_name'] . ') category.'
                    );
                    if (!empty($datan)) {
                        $notification_id = $this->notificationsetupmodel->saveNotification($datan);
                        $receiver_id1 = $this->notificationsetupmodel->getReceiver();
                        if (!empty($notification_id) && !empty($receiver_id1)) {
                            $count = count($receiver_id1);
                            for ($i = 0; $i < $count; $i++) {
                                $datar = array('notification_id' => $notification_id, 'receiver' => $receiver_id1[$i]['userId']);
                                $this->notificationsetupmodel->saveReceiverNotification($datar);
                            }
                        }
                    }
                    $message['message'] = "Payment  Saved......!";
                    $this->session->set_userdata($message);
                    redirect(admin_url() . '/extraincomesetup/index', 'refresh');
                } else {
                    $errormessage['errormessage'] = "Payment Not Saved......!";
                    $this->session->set_userdata($errormessage);
                    redirect(admin_url() . '/extraincomesetup/index', 'refresh');
                }
            } else {
                $errormessage['errormessage'] = "Payment Information Missing......!";
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/extraincomesetup/index', 'refresh');
            }
        }
    }

    public function viewincome() {
        $data['allextraincome']       = $this->extraincomesetupmodel->getAllExtraincome();
        $data['total_extra_income']   = $this->extraincomesetupmodel->getTotalExtraIncomeByYear();
        //print_r($data);die();
        if (!empty($data['allextraincome'])) {
            $current_year = date('Y');
            $data['total_income']   = $this->extraincomesetupmodel->getExtraIncomeByYear($current_year);

//            print_r($data['total_income']);
//            exit();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/extraincome/viewextraincome', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = "No Income Avaiable......!";
            $this->session->set_userdata($errormessage);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('dashbord');
            $this->load->view('common/footer');
        }
    }

    public function incomeSearch(){

        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
            $data['expense']        =$this->balancesetupmodel->getTotalExpense();
            $data['salaryExpense']  =$this->balancesetupmodel->getTotalSalaryExpense();
            $data['landIncome']     =$this->balancesetupmodel->getTotalLandIncome();
            $data['extraIncome']    =$this->balancesetupmodel->getTotalExtraIncome();
            //echo '<pre>';print_r($data);die();
            if(empty($data['expense']['expenseAmount']) && empty($data['salaryExpense']['salaryAmount']) && empty($data['landIncome']['amount']) && empty($data['extraIncome']['amount']))
            {
                $errormessage['errormessage']='No Income And Expense Found ';
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');

            }else{
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/balance/income_search',$data);
                $this->load->view('common/footer');
            }
        }else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function searchIncome($flag=''){
       if(!empty($flag) && $flag=='year'){
           $admin_id = $this->session->userdata('admin_id');
           if ((!empty($admin_id)) || ($admin_id != "")) {
               $year = $this->input->post('select_year');
               if(is_numeric($year) > 0){
                   $data['allincome'] = $this->extraincomesetupmodel->getIncomeByYear($year);
                   $data['year']      = $year;
                   $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
                   $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
                   // $data['alladminId']  =$this->employeemodel->getAlladminsId();
                   // $data['adminbyId']=$this->employeemodel->getAdminById();
                   $this->load->view('common/header');
                   $this->load->view('common/menu');
                   $this->load->view('admin/income/search_result_table',$data);
                   $this->load->view('common/footer');
               }else{
                   redirect(base_url('admin/home', 'refresh'));
               }
           } else {
               redirect(base_url('admin/home', 'refresh'));
           }
       }else if(!empty($flag) && $flag=='month'){
            $date=$this->input->post('data',true);

            if(!empty($date)){
                $data['allincome']      = $this->extraincomesetupmodel->getIncomeByDateRange($date);
                $data['total_income']   = $this->extraincomesetupmodel->getTotalAmountInDateRange($date);
                $data['allCustomerId']  = $this->CREGModel->getallcustomerId();
                $data['allEmployeeId']  =$this->employeemodel->getAllEmployeeId();
                // $data['alladminId']  =$this->employeemodel->getAlladminsId();
                // $data['adminbyId']      =$this->employeemodel->getAdminById($data['receiver_id']);
                $data['from_date']      = $date['fromdate'];
                $data['to_date']        = $date['todate'];
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/income/search_result_table',$data);
                $this->load->view('common/footer');
            }
       }else{
            redirect(base_url('admin/home', 'refresh'));
       }
    }

    public function alladmins() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['receiver_id']='1234';
            $data['adminbyId']      =$this->employeemodel->getAdminById($data['receiver_id']);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            echo "<pre>";
            echo print_r($data);
            echo "</pre>";
            // $this->load->view('admin/income/extraincome/incomeEntryExtra', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


    public function searchExtraIncome($flag=''){
        if(!empty($flag) && $flag=='month'){
            $date=$this->input->post('data',true);

            if(!empty($date)){
                $data['allincome']      = $this->extraincomesetupmodel->getIncomeByDateRange($date);
                $data['total_income']   = $this->extraincomesetupmodel->getTotalAmountInDateRange($date);
                $data['from_date']      = $date['fromdate'];
                $data['to_date']        = $date['todate'];
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/income/extraincome/viewextraincome',$data);
                $this->load->view('common/footer');
            }else{
                redirect(base_url('admin/home', 'refresh'));
            }
        }
    }

    public function delete_income($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $execution = $this->extraincomesetupmodel->deleteIncomeByid($id);
            if ($execution) {
                $message['message'] = 'income Information Deleted';
                $this->session->set_userdata($message);
                redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
            } else {
                $errormessage['errormessage'] = 'Income Information Not Deleted';
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function edit_income($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['incomeCategory'] = getCategoryInfo();
            $data['incomeInfoByid'] = $this->extraincomesetupmodel->getincomeInfoByid($id);
            if (!empty($data['incomeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/income/extraincome/editextraincome', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function update_income() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnUpdate'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[incomeCategory]',
                        'label' => 'incomeCategory',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[amount]',
                        'label' => 'Amount',
                        'rules' => 'required|xss_clean|trim|numeric'
                    ),
                    array(
                        'field' => 'data[paymentProcess]',
                        'label' => 'Payment Process',
                        'rules' => 'required|xss_clean|trim|numeric'
                    ),
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_description]',
                        'label' => 'Payment Description',
                        'rules' => 'xss_clean|trim'
                    )
                );

                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $errormessage['errormessage'] = "Information Missing......!";
                    $this->session->set_userdata($errormessage);
                    // $data['incomeCategory']=getCategoryInfo();
                    // $data['allCustomerId']=$this->CREGModel->getallcustomerId();
                    //print_r($data['allCustomerId']);die();
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/income/extraincome/viewincome');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true);
                    $data['id'] = $this->input->post('id', true);
                    if (!empty($data) && $data['incomeCategory'] != null) {
                        $execution = $this->extraincomesetupmodel->updateIncomeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "Income Info  Updated......!";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Income Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/extraincomesetup/viewincome', 'refresh');
                    }
                }
            } else {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function searchBydate() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            //$data['incomeCategory']=getCategoryInfo();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/viewIncomebydate');
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    

    /*public function searchSingleBydate() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnSdate'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[incomeCategory]',
                        'label' => 'incomeCategory',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_type]',
                        'label' => 'Payment Type',
                        'rules' => 'xss_clean|trim'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    //$data['incomeCategory']=getCategoryInfo();
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/income/viewIncomebydate');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true); 
                    //if (!empty($data['date']) || $data['incomeCategory'] != null || !empty($data['payment_type'])) {
                        //print_r($data);die();
                    if (!empty($data)){
                        if (empty($data['payment_type']) || $data['payment_type']==null) {
                            $datax['extraincome'] = $this->extraincomesetupmodel->getExtraincomeBydate($data);
                         }
                        if ($data['incomeCategory'] == null || empty($data['incomeCategory'])) {// scince incomeCategory is not available in land account
                            $datax['income'] = $this->extraincomesetupmodel->getincomeBydate($data);
                        }
                        if (empty($datax['extraincome']) && empty($datax['income'])) {
                            $errormessage['errormessage'] = 'No Income Found ';
                            $this->session->set_userdata($errormessage);
                            redirect('admin/extraincomesetup/searchBydate', 'refresh');
                        } else {
                            //$datax['incomeCategory']=getCategoryInfo();
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $this->load->view('admin/income/viewIncomebydate', $datax);
                            $this->load->view('common/footer');
                        }
                    } else {
                        $errormessage['errormessage'] = 'Please Select Date or category';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/extraincomesetup/searchBydate', 'refresh');
                    }
                }
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }*/

    public function searchBydaterange() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnSdateRange'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[fromdate]',
                        'label' => 'Date From',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[todate]',
                        'label' => 'Date To',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[incomeCategory]',
                        'label' => 'incomeCategory',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_type]',
                        'label' => 'Payment Type',
                        'rules' => 'xss_clean|trim'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/income/viewIncomebydate');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true); 
                    if (!empty($data)) {
                       //if(empty($data['payment_type']) && $data['incomeCategory']=""){
                        if (empty($data['payment_type']) || $data['payment_type']==null) {
                            $datax['extraincome'] = $this->extraincomesetupmodel->getExtraincomeBydaterange($data);
                            $datax['allCustomerId'] = $this->CREGModel->getallcustomerId();
                            $datax['allEmployeeId']=$this->employeemodel->getAllEmployeeId();

                            //if($data['incomeCategory'] == null || empty($data['incomeCategory'])){$data['incomeCategory']="";}
                        }
                        if ($data['incomeCategory'] == null || empty($data['incomeCategory'])) {// scince incomeCategory is not available in land account
                            $datax['income'] = $this->extraincomesetupmodel->getincomeBydaterange($data);
                            $datax['allCustomerId'] = $this->CREGModel->getallcustomerId();
                            $datax['allEmployeeId']=$this->employeemodel->getAllEmployeeId();

                        }
                        //echo '<pre>';print_r($datax);die();
                        if (empty($datax['extraincome']) && empty($datax['income'])) {
                            if (!empty($data['date']) && !empty($data['incomeCategory'])) {
                                $errormessage['errormessage'] = 'No Income Found In this Category Of The Date';
                            }
                            $errormessage['errormessage'] = 'No Income Found In this Date Range';
                            $this->session->set_userdata($errormessage);
                            redirect('admin/extraincomesetup/searchBydate', 'refresh');
                        } else {
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $this->load->view('admin/income/viewIncomebydate', $datax);
                            $this->load->view('common/footer');
                        }
                        // }else {
                        // $errormessage['errormessage'] = 'You Should Search By (PaymentType Or IncomeCategory) Anyone Option';
                        // $this->session->set_userdata($errormessage);
                        // redirect('admin/extraincomesetup/searchBydate', 'refresh');}
                    } else {
                        $errormessage['errormessage'] = 'Please Select Date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/extraincomesetup/searchBydate', 'refresh');
                    }
                }
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function searchBydaterangeofCust() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnSdateRangeCust'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[fromdate]',
                        'label' => 'Date From',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[todate]',
                        'label' => 'Date To',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[customerId]',
                        'label' => 'customerId',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[total_cost]',
                        'label' => 'total_cost',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_type]',
                        'label' => 'Payment Type',
                        'rules' => 'xss_clean|trim'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/customer/searchbydate');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true); 
                    if (!empty($data)) {
                       //if(empty($data['payment_type']) && $data['incomeCategory']=""){
                        if (empty($data['payment_type']) || $data['payment_type']==null) {
                            $datax['extraincomecustomer'] = $this->extraincomesetupmodel->getExtraincomeBydaterangeCustomer($data);
                            $datax['customerinbyid'] = $this->extraincomesetupmodel->getcustomerinfobyid($data);

                            //if($data['incomeCategory'] == null || empty($data['incomeCategory'])){$data['incomeCategory']="";}
                        }
                        if ($data['customerId'] == null || empty($data['customerId'])) {// scince incomeCategory is not available in land account
                            $datax['incomecust'] = $this->extraincomesetupmodel->getincomeBydaterangeCust($data);
                        }
                        //echo '<pre>';print_r($datax);die();
                        if (empty($datax['extraincomecustomer']) && empty($datax['incomecust'])) {
                            if (!empty($data['date']) && !empty($data['customerId'])) {
                                $errormessage['errormessage'] = 'No Income Found In this Category Of The Date';
                            }
                            $errormessage['errormessage'] = 'No Income Found In this Date Range';
                            $this->session->set_userdata($errormessage);
                            redirect('admin/landcustomerRegistration/ViewLandCustomer  ', 'refresh');
                        } else {
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $datax['allCustomerId'] = $this->CREGModel->getallcustomerId();
                            $this->load->view('admin/customer/searchbydate', $datax);
                            $this->load->view('common/footer');
                        }
                        // }else {
                        // $errormessage['errormessage'] = 'You Should Search By (PaymentType Or IncomeCategory) Anyone Option';
                        // $this->session->set_userdata($errormessage);
                        // redirect('admin/extraincomesetup/searchBydate', 'refresh');}
                    } else {
                        $errormessage['errormessage'] = 'Please Select Date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/landcustomerRegistration/ViewLandCustomer  ', 'refresh');
                    }
                }
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }



    public function searchBycustomer() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            //$data['incomeCategory']=getCategoryInfo();
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/viewIncomebycustomer', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


    public function searchBydaterangeandCustomer() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnSdateRangeCust'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[fromdate]',
                        'label' => 'Date From',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[todate]',
                        'label' => 'Date To',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[customerId]',
                        'label' => 'customerId',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_type]',
                        'label' => 'Payment Type',
                        'rules' => 'xss_clean|trim'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/income/viewIncomebycustomer');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true); 
                    if (!empty($data)) {
                       //if(empty($data['payment_type']) && $data['incomeCategory']=""){
                        if (empty($data['payment_type']) || $data['payment_type']==null) {
                            $datax['extraincomecustomer'] = $this->extraincomesetupmodel->getExtraincomeBydaterangeandCust($data);
                            //if($data['incomeCategory'] == null || empty($data['incomeCategory'])){$data['incomeCategory']="";}
                        }
                        if ($data['customerId'] == null || empty($data['customerId'])) {// scince incomeCategory is not available in land account
                            $datax['incomecust'] = $this->extraincomesetupmodel->getincomeBydaterangeCust($data);
                        }
                        //echo '<pre>';print_r($datax);die();
                        if (empty($datax['extraincomecustomer']) && empty($datax['incomecust'])) {
                            if (!empty($data['date']) && !empty($data['customerId'])) {
                                $errormessage['errormessage'] = 'No Income Found In this Category Of The Date';
                            }
                            $errormessage['errormessage'] = 'No Income Found In this Date Range';
                            $this->session->set_userdata($errormessage);
                            redirect('admin/extraincomesetup/searchBycustomer  ', 'refresh');
                        } else {
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            $datax['allCustomerId'] = $this->CREGModel->getallcustomerId();
                            $this->load->view('admin/income/viewIncomebycustomer', $datax);
                            $this->load->view('common/footer');
                        }
                        // }else {
                        // $errormessage['errormessage'] = 'You Should Search By (PaymentType Or IncomeCategory) Anyone Option';
                        // $this->session->set_userdata($errormessage);
                        // redirect('admin/extraincomesetup/searchBydate', 'refresh');}
                    } else {
                        $errormessage['errormessage'] = 'Please Select Date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/extraincomesetup/searchBycustomer  ', 'refresh');
                    }
                }
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }



    public function searchByemployee() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            //$data['incomeCategory']=getCategoryInfo();
            $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/viewIncomebyemployee', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }
    public function allcustomersumall() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['customertotal'] =$this->extraincomesetupmodel->incomebycusttotalall();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            echo "<pre>";
            echo print_r($data);
            echo "</pre>";
            // foreach ($data['customertotal'] as $key => $value) {
            //     echo "customerid: ".$value['customerId']." Total: ".$value['amount']."<br>";
            // }
            // $this->load->view('admin/income/extraincome/incomeEntryExtra', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }
    public function allcustomersum() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['customerId']='2019-01-342';
            $data['customertotal'] =$this->extraincomesetupmodel->incomebycusttotal($data['customerId']);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            echo "<pre>";
            echo print_r($data['customertotal']);
            echo "</pre>";
            // $this->load->view('admin/income/extraincome/incomeEntryExtra', $data);
            $this->load->view('common/footer');
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function searchBydaterangeandemp() {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            if (isset($_POST['btnSdateRangeEmp'])) {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $config = array(
                    array(
                        'field' => 'data[employee_id]',
                        'label' => 'employee_id',
                        'rules' => 'xss_clean|trim'
                    )
                );
                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $data['total_income']   = $this->extraincomesetupmodel->getTotalAmountInDateRange($date);
                    $this->load->view('admin/income/viewIncomebyemployee');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true); 
                    if (!empty($data)) {
                        
                        if (!empty($data['employee_id'])) {
                            $datax['custbyemployee'] = $this->extraincomesetupmodel->getincomeBydaterangeEmp($data);
                            // if (!empty($datax['custbyemployee'])) {
                            //     $datay['customertotal']    =$this->extraincomesetupmodel->incomebycusttotalall($data['customerId']);                                
                            // }                            
                        }
                        // echo '<pre>';print_r($datay);die();
                        // foreach ($datay['customertotal'] as $key => $value) {
                        //     echo "".$value['customerId']." ".$value['amount']."<br>";
                        // }
                        if (empty($datax['custbyemployee'])) {                            
                            $errormessage['errormessage'] = 'No Customer Found with this employee';
                            $this->session->set_userdata($errormessage);
                            redirect('admin/extraincomesetup/searchByemployee  ', 'refresh');
                        } else {
                            $this->load->view('common/header');
                            $this->load->view('common/menu');
                            // $datax['customertotal']    =$this->extraincomesetupmodel->incomebycusttotalall($data['customerId']); 
                            $datax['customertotal']    =$this->extraincomesetupmodel->incomebycusttotalall(); 
                            $datax['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
                            $this->load->view('admin/income/viewIncomebyemployee', $datax);
                            $this->load->view('common/footer');
                        }
                        // }else {
                        // $errormessage['errormessage'] = 'You Should Search By (PaymentType Or IncomeCategory) Anyone Option';
                        // $this->session->set_userdata($errormessage);
                        // redirect('admin/extraincomesetup/searchBydate', 'refresh');}
                    } else {
                        $errormessage['errormessage'] = 'Please Select Date';
                        $this->session->set_userdata($errormessage);
                        redirect('admin/extraincomesetup/searchByemployee  ', 'refresh');
                    }
                }
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    /* public function invoice($id)
      {
      $admin_id=$this->session->userdata('admin_id');
      if ((!empty($admin_id)) || ($admin_id !=""))
      {
      $data['extraInvoiceInfo']=$this->extraincomesetupmodel->getinvoiceInfoByid($id);
      //echo '<pre>';print_r($data);die();
      if($data['extraInvoiceInfo'])
      {
      $this->load->view('common/header');
      $this->load->view('common/menu');
      $this->load->view('admin/income/invoice',$data);
      $this->load->view('common/footer');
      }
      else
      {
      redirect(admin_url().'/extraincomesetup/viewincome','refresh');
      }
      }
      else
      {
      redirect(base_url('admin','refresh'));
      }
      } */

      public function invoice($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['extraInvoiceInfo'] = $this->extraincomesetupmodel->getinvoiceInfoByid($id);
            //echo '<pre>';print_r($data);die();
            if ($data['extraInvoiceInfo']) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/income/invoice', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/extraincomesetup/viewextraincome', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function dueInstallment(){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !=""))
        {
            $data['expense']        =$this->balancesetupmodel->getTotalExpense();
            $data['salaryExpense']  =$this->balancesetupmodel->getTotalSalaryExpense();
            $data['landIncome']     =$this->balancesetupmodel->getTotalLandIncome();
            $data['extraIncome']    =$this->balancesetupmodel->getTotalExtraIncome();
            //echo '<pre>';print_r($data);die();
            if(empty($data['expense']['expenseAmount']) && empty($data['salaryExpense']['salaryAmount']) && empty($data['landIncome']['amount']) && empty($data['extraIncome']['amount']))
            {
                $errormessage['errormessage']='No Income And Expense Found ';
                $this->session->set_userdata($errormessage);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('dashbord');
                $this->load->view('common/footer');

            }else{
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/balance/due_installment_view',$data);
                $this->load->view('common/footer');
            }
        }else{
            redirect(base_url('admin','refresh'));
        }
    }

    public function searchDueInstallment($flag =''){
        $admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) {
            if(!empty($flag) && $flag =='search') {
                $year  = $this->input->post('select_year');
                $month = $this->input->post('select_month');
                    if(is_numeric($year) && is_numeric($month)){
                        $make_date = $year.'-'.$month.'-31';
                        $data['dues_info']   = $this->balancesetupmodel->getDueInstallmentByDate($make_date);
                        $data['given_date']  = $make_date;
                        $this->load->view('common/header');
                        $this->load->view('common/menu');
                        $this->load->view('admin/balance/due_installer_result',$data);
                        $this->load->view('common/footer');
//                        print_r($data['dues_info']);
                    }else{
                        redirect(base_url('admin/extraincomesetup/dueInstallment','refresh'));
                    }
            }
        }else{
            redirect(base_url('admin','refresh'));
        }


    }
}
