<?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 ChequeInOutEntryView 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');
    }

    public function index() {
        // $data['incomeCategory']=$this->incomecategorymodel->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/cheque/incheques/inchequeentry', $data);
        $this->load->view('common/footer');
    }

    public function insert_chequein() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            
            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[customerId]',
                'label' => 'customerId',
                '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);
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            //print_r($data['allCustomerId']);die();
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/cheque/incheques/inchequeentry', $data);
            $this->load->view('common/footer');
        } else {

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

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


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

    public function done_incheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['inchequeInfoByid'] = $this->chequemodel->getinchequeInfoByid($id);
            if (!empty($data['inchequeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/cheque/incheques/doneincheque', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

    public function edit_incheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['inchequeInfoByid'] = $this->chequemodel->getinchequeInfoByid($id);
            if (!empty($data['inchequeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/cheque/incheques/editincheques', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

        public function markdone_incheque() {
        $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/cheque/incheques/doneincheque');
                    $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->markdoneInChequeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> Cheque Info  Updated of <h3>Token No: ".$data['id']."</h3></center>";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Incheque Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/chequeinoutentryview/allchequesin', '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_incheque() {
        $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[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[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/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) && $data['customerId'] != null) {
                        $execution = $this->chequemodel->updateInChequeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> Cheque Info  Updated of <h3>Token No: ".$data['id']."</h3></center>";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Incheque Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/chequeinoutentryview/allchequesin', '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_incheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $execution = $this->chequemodel->deleteinchequesByid($id);
            if ($execution) {
                $message['message'] = 'Cheque Information Deleted';
                $this->session->set_userdata($message);
                redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
            } else {
                $errormessage['errormessage'] = 'cheque Information Not Deleted';
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/chequeinoutentryview/allchequesin', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


    public function donechequeinlist() {
        $data['allchequein']      = $this->chequemodel->getalldoneincheques();
        $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/incheques/donelistincheques', $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 allchequesin() {
        $data['allchequein']      = $this->chequemodel->getallincheques();
        $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/incheques/viewincheques', $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 done_outcheque($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $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/doneoutcheque', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/chequeinoutentryview/allchequesout', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

            public function markdone_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[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/cheque/outcheques/doneoutcheque');
                    $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->markdoneoutChequeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "<center> 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'] = "Incheque 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 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'));
        }
    }

}