<?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 ChequeInOutEntryView
 *
 * @author BusyMan
 */
class AdvanceMoneyReceipt 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('incomesetupmodel/incomesetupmodel', 'incomesetupmodel');
        $this->load->model('notificationsetupmodel/notificationsetupmodel', 'notificationsetupmodel');
        $this->load->model('chequemodel/chequemodel', 'chequemodel');
        $this->load->model('employeemodel/employeemodel','employeemodel');
        $this->load->model('moneyreceiptmodel/moneyreceiptmodel','moneyreceiptmodel');
    }

    public function index() {
        $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/moneyreceipt/moneyreceiptentry', $data);
        $this->load->view('common/footer');
    }

    public function insert_mr() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            
            array(
                'field' => 'data[mr_no]',
                'label' => 'mr_no',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[description]',
                'label' => 'description',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[payment_date]',
                'label' => 'Date',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[ref_id]',
                'label' => 'ref_id',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[customerId]',
                'label' => 'customerId',
                'rules' => 'required|xss_clean|trim'
            )
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == false) {
            $errormessage['errormessage'] = "Money Receipt information missing......!";
            $this->session->set_userdata($errormessage);
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/moneyreceipt/moneyreceiptentry', $data);
            $this->load->view('common/footer');
        } else {

            $data = $this->input->post('data', true);
            if (!empty($data) && $data['customerId'] != null) {
                $execution = $this->moneyreceiptmodel->insert_moneyreceipt($data);
                if (!empty($execution)) {                   
                    
                    $message['message'] = "New Money Receipt saved! <h3><b>Money Receipt No Was : ".$data['mr_no']."</b></h3>";
                    $this->session->set_userdata($message);

                    redirect(admin_url() . '/advancemoneyreceipt/index', 'refresh');


                } else {
                    $errormessage['errormessage'] = "cheque Not Saved......!";
                    $this->session->set_userdata($errormessage);
                    redirect(admin_url() . '/advancemoneyreceipt/index', 'refresh');
                }
            } else {
                $errormessage['errormessage'] = "Payment Information Missing......!";
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/advancemoneyreceipt/index', 'refresh');
            }
        }
    }



    public function edit_mr($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
            $data['inchequeInfoByid'] = $this->moneyreceiptmodel->getmrInfoByid($id);
            if (!empty($data['inchequeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/moneyreceipt/editmr', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function done_mr($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['inchequeInfoByid'] = $this->moneyreceiptmodel->getmrInfoByid($id);
            if (!empty($data['inchequeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/moneyreceipt/donemr', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

            public function markdone_mr() {
        $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[comment]',
                        'label' => 'comment',
                        'rules' => 'xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[active]',
                        'label' => 'active',
                        'rules' => 'required|xss_clean|trim'
                    )
                );

                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $errormessage['errormessage'] = "Information Missing......!";
                    $this->session->set_userdata($errormessage);
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/moneyreceipt/donemr');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true);
                    $data['id'] = $this->input->post('id', true);
                    if (!empty($data)) {
                        $execution = $this->moneyreceiptmodel->markdonemrById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> Money Receipt Info  Updated</center>";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Money Receipt Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/advancemoneyreceipt/allmrlist', '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 update_mr() {
        $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[mr_no]',
                        'label' => 'mr_no',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[description]',
                        'label' => 'description',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[payment_date]',
                        'label' => 'payment_date',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[ref_id]',
                        'label' => 'ref_id',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[customerId]',
                        'label' => 'customerId',
                        'rules' => 'required|xss_clean|trim'
                    ),
                );

                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $errormessage['errormessage'] = "Information Missing......!";
                    $this->session->set_userdata($errormessage);
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/moneyreceipt/viewmrlist');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true);
                    $data['id'] = $this->input->post('id', true);
                    if (!empty($data) && $data['customerId'] != null) {
                        $execution = $this->moneyreceiptmodel->updatemrById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> Money Receipt Info  Updated </center>";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Money Receipt Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/advancemoneyreceipt/allmrlist', '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 delete_mr($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $execution = $this->moneyreceiptmodel->deletemrByid($id);
            if ($execution) {
                $message['message'] = 'Money Receipt Information Deleted';
                $this->session->set_userdata($message);
                redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
            } else {
                $errormessage['errormessage'] = 'Cheque Information Not Deleted';
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/advancemoneyreceipt/allmrlist', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


    public function donemrlist() {
        $data['allchequein']      = $this->moneyreceiptmodel->getalldonemr();
        $data['total_chequein']   = $this->chequemodel->totalincheques();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
        if (!empty($data['allchequein'])) {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/moneyreceipt/donelistmr', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Cheque Available......!";
            $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 allmrlist() {
        $data['allchequein']      = $this->moneyreceiptmodel->getallmrlist();
        $data['total_chequein']   = $this->chequemodel->totalincheques();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();

        if (!empty($data['allchequein'])) {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/moneyreceipt/viewmrlist', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Cheque Available......!";
            $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 searchincheques($flag=''){
       if(!empty($flag) && $flag=='month'){
            $date=$this->input->post('data',true);

            if(!empty($date)){
                $data['allchequein']      = $this->chequemodel->getallinchequesbydaterange();
                $data['total_chequein']   = $this->chequemodel->totalinchequesamountindaterange();

                $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 donechequein(){
        $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/cheque/incheques/inchequeentry', $data);
        $this->load->view('common/footer');
    }


    /* start checkout */
    //===============================

    public function newchequeout() {
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/cheque/outcheques/outchequeentry');
        $this->load->view('common/footer');
    }

    public function insert_chequeout() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            
            array(
                'field' => 'data[receiver]',
                'label' => 'receiver',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[purpose]',
                'label' => 'purpose',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[bank_name]',
                'label' => 'bank_name',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[amount]',
                'label' => 'amount',
                'rules' => 'required|xss_clean|trim|numeric'
            ),
            array(
                'field' => 'data[date]',
                'label' => 'Date',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[cheque_no]',
                'label' => 'cheque_no',
                'rules' => 'required|xss_clean|trim'
            ),
        );

        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == false) {
            $errormessage['errormessage'] = "cheque information missing......!";
            $this->session->set_userdata($errormessage);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/cheque/outcheques/outchequeentry');
            $this->load->view('common/footer');
        } else {

            $data = $this->input->post('data', true);
            if (!empty($data)) {
                $execution = $this->chequemodel->insert_chequeout($data);
                if (!empty($execution)) {                   
                    
                    $message['message'] = "New Out Cheque entry saved! <h3><b> Token No: ".$this->db->insert_id()."</b></h3>";
                    $this->session->set_userdata($message);

                    redirect(admin_url() . '/chequeinoutentryview/newchequeout', 'refresh');


                } else {
                    $errormessage['errormessage'] = "cheque Not Saved......!";
                    $this->session->set_userdata($errormessage);
                    redirect(admin_url() . '/chequeinoutentryview/newchequeout', 'refresh');
                }
            } else {
                $errormessage['errormessage'] = "Payment Information Missing......!";
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/chequeinoutentryview/newchequeout', 'refresh');
            }
        }
    }

    public function allchequesout() {
        $data['allchequein']      = $this->chequemodel->getalloutcheques();
        $data['total_chequein']   = $this->chequemodel->totaloutcheques();

        if (!empty($data['allchequein'])) {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/cheque/outcheques/viewoutcheques', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Cheque Available......!";
            $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 edit_outcheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['inchequeInfoByid'] = $this->chequemodel->getoutchequeInfoByid($id);
            if (!empty($data['inchequeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/cheque/outcheques/editoutcheques', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


        public function update_outcheque() {
        $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[receiver]',
                        'label' => 'receiver',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[cheque_no]',
                        'label' => 'cheque_no',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[bank_name]',
                        'label' => 'bank_name',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[amount]',
                        'label' => 'amount',
                        'rules' => 'required|xss_clean|trim|numeric'
                    ),
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[purpose]',
                        'label' => 'purpose',
                        'rules' => 'required|xss_clean|trim'
                    ),
                );

                $this->form_validation->set_rules($config);
                if ($this->form_validation->run() == false) {
                    $errormessage['errormessage'] = "Information Missing......!";
                    $this->session->set_userdata($errormessage);
                    $this->load->view('common/header');
                    $this->load->view('common/menu');
                    $this->load->view('admin/cheque/incheques/editincheques');
                    $this->load->view('common/footer');
                } else {
                    $data = $this->input->post('data', true);
                    $data['id'] = $this->input->post('id', true);
                    if (!empty($data)) {
                        $execution = $this->chequemodel->updateOutChequeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> Out Cheque Info  Updated of <h3>Token No: ".$data['id']."</h3></center>";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Outcheque Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/chequeinoutentryview/allchequesout', '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 donechequeoutlist() {
        $data['allchequein']      = $this->chequemodel->getalldoneoutcheques();
        $data['total_chequein']   = $this->chequemodel->totalincheques();

        if (!empty($data['allchequein'])) {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/cheque/outcheques/donelistoutcheques', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Cheque Available......!";
            $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 delete_outcheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $execution = $this->chequemodel->deleteoutchequesByid($id);
            if ($execution) {
                $message['message'] = 'Cheque Information Deleted';
                $this->session->set_userdata($message);
                redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
            } else {
                $errormessage['errormessage'] = 'cheque Information Not Deleted';
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

}