<?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 customerCalls 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('extraincomesetupmodel/extraincomesetupmodel', 'extraincomesetupmodel');
        $this->load->model('notificationsetupmodel/notificationsetupmodel', 'notificationsetupmodel');
        $this->load->model('chequemodel/chequemodel', 'chequemodel');
        $this->load->model('employeemodel/employeemodel','employeemodel');
        $this->load->model('callmodel/callmodel','callmodel');
        $this->load->model('ref_customer_dep_model');
    }

    public function index() {
        $data['allemployee'] = $this->ref_customer_dep_model->fetch_employee();
        $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
        $data['allCustomers'] = $this->CREGModel->getallcustomers();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/calls/callentry', $data);
        $this->load->view('common/footer');
    }
    
    
    public function entrybyid() {
        $data['allemployee'] = $this->ref_customer_dep_model->fetch_employee();
        $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
        $data['allCustomers'] = $this->CREGModel->getallcustomers();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/calls/entrybyid', $data);
        $this->load->view('common/footer');
    }    
    


     function fetch_customer()
     {
      if($this->input->post('userId'))
      {
       echo $this->ref_customer_dep_model->findcustomer($this->input->post('userId'));
      }
     }

    public function insert_call() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            array(
                'field' => 'data[description]',
                'label' => 'description',
                'rules' => 'required|xss_clean|trim'
            ),
            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'] = "Call Info 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/calls/callentry', $data);
            $this->load->view('common/footer');
        } else {
            $data = $this->input->post('data', true);
            $data['receiver_id'] = $this->session->userdata('employeeId');
            // echo "<pre>";
            // echo print_r($data);
            // echo "</pre>";
            if (!empty($data) && $data['customerId'] != null) {
                $execution = $this->callmodel->insert_calldetails($data);
                if (!empty($execution)) {                   
                    
                    $message['message'] = "New Call Record Saved.";
                    $this->session->set_userdata($message);

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


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



    public function edit_call($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['callInfoById'] = $this->callmodel->callInfoById($id);
            if (!empty($data['callInfoById'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/calls/editcall', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/call/allcalls', 'refresh');
            }
        } 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_call($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $execution = $this->callmodel->deletecallByid($id);
            if ($execution) {
                $message['message'] = 'Call Information Deleted';
                $this->session->set_userdata($message);
                redirect(admin_url() . '/customerCalls/allcalls', 'refresh');
            } else {
                $errormessage['errormessage'] = 'Call Information Not Deleted';
                $this->session->set_userdata($errormessage);
                redirect(admin_url() . '/customerCalls/allcalls', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }


    public function allcalls() {
        $data['allcalls']= $this->callmodel->getallcalls();
        $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
        $data['allCustomerName'] = $this->CREGModel->getallcustomerNames();
        $data['allEmployeeId']=$this->employeemodel->getAllEmployeeId();
        if (!empty($data['allcalls'])) {
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/calls/allcalls', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Call 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 searchcall(){
        // if(!empty($flag) && $flag=='month'){
            $date=$this->input->post('data',true);
            if(!empty($date)){
                $data['from_date']      = $date['fromdate'];
                $data['to_date']        = $date['todate'];
                $data['allcalls']      = $this->callmodel->getCallByDate($date);
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/calls/search_call_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');
    }
}