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

    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/income/landincome/incomeEntry', $data);
        $this->load->view('common/footer');
    }

    public function insert_income() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $config = array(
            
            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[payment_type]',
                'label' => 'Payment Type',
                'rules' => 'required|xss_clean|trim|numeric'
            ),
            array(
                'field' => 'data[date]',
                'label' => 'Date',
                'rules' => 'required|xss_clean|trim'
            ),
            array(
                'field' => 'data[customerId]',
                'label' => 'Customer Id',
                '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['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/income/landincome/incomeEntry', $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['year'] = date('Y');
            $data['receiver_id'] = $this->session->userdata('employeeId');                       
            // print_r($data);die();
            if (!empty($data) && $data['customerId'] != null) {
                $execution = $this->incomesetupmodel->insert_amount($data);
                if (!empty($execution)) {
                    $datan['sender'] = $data['receiver_id'];
                    $datan['date'] = date('Y-m-d', strtotime($data['date']));
                    $datan['notification'] = $data['amount'] . ' taka was deposited by landcustome(' . $data['customerId'] . ').';
                    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);
                    
                    $result = $this->incomesetupmodel->GetCustomerlist($data);
                    
                    // //send email confirmation
                    // $this->load->library('email');
                    // $config = array(
                    //     'protocol'  => 'smtp',
                    //     'smtp_host' => 'ssl://smtp.gmail.com',
                    //     'smtp_port' => 465,
                    //     'smtp_user' => 'mail.hasanhomesltd@gmail.com',
                    //     'smtp_pass' => 'Arif12345',
                    //     'mailtype'  => 'html',
                    //     'charset'   => 'utf-8'
                    // );
                    // $this->email->initialize($config);
                    // $this->email->set_mailtype("html");
                    // $this->email->set_newline("\r\n");

                    // $this->email->from('mail.hasanhomesltd@gmail.com', 'HasanHomes');
                    // $this->email->to("".$result['email']."");
                    // //$this->email->cc('mail@mail.com');
                    // $this->email->subject('Payment Successfull');
                    // $this->email->message("Dear Customer, Your Tk. ".$data['amount']."/- as " . $data['payment_description'] . " Received Successfully. Thank You. " .$result['project_name']." .");
                    // if($this->email->send()){
                    //     $msg="Confirmation Mail Send";
                    // }
                    // else{
                    //     $msg="Mail Not Send";
                    // }
                    //send email confirmation
                    
                    
                    

                    //get the paryment type
                    // if ($data['payment_type']==1) {
                    //     $ptype= "Installment";
                    // }
                    // elseif ($data['payment_type']==2) {
                    //     $ptype= "Downpayment";
                    // }
                    // elseif ($data['payment_type']==3) {
                    //     $ptype= "One Step";
                    // }
                    // elseif ($data['payment_type']==4) {
                    //     $ptype= "Others";
                    // }
                    // elseif ($data['payment_type']==4) {
                    //     $ptype= "Payment";
                    // }
                    // get the customer mobile
                    
                    // echo $result['mobile'];
                    // echo $result['project_name'];
                    //send sms api integration
                    $to = "".$result['mobile']."";
                    $token = "7ef048da3b996ce923978de4e783d10f";
                    $message = "Dear Customer, Your Tk. ".$data['amount']."/- as " . $data['payment_description'] . " Received Successfully. Thank You. " .$result['project_name']." .";

                    $url = "http://api.greenweb.com.bd/api.php";


                    $data= array(
                    'to'=>"$to",
                    'message'=>"$message",
                    'token'=>"$token"
                    ); 
                    $ch = curl_init(); 
                    curl_setopt($ch, CURLOPT_URL,$url);
                    curl_setopt($ch, CURLOPT_ENCODING, '');
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
                    $smsresult = curl_exec($ch);
                    //send sms api integration

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


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

    public function viewincome() {
        $data['allincome']      = $this->incomesetupmodel->getallincome();
        $data['total_income']   = $this->incomesetupmodel->totalAmount();

        if (!empty($data['allincome'])) {
            $current_year           = date('Y');
            $data['income_year']    = $this->incomesetupmodel->totalAmountByYear($current_year);
            $this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/income/landincome/viewincome', $data);
            $this->load->view('common/footer');
        } else {
            $errormessage['errormessage'] = " This Time No Income 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 installmentDueList($month, $year){
        if($month != '' && $year !=''){

        }
        echo ' '.$month.' '.$year;
    }

    public function test(){
        echo 'Success';
    }

    public function installmentDue(){

        $this->load->view('common/header');
        $this->load->view('common/menu');
        $this->load->view('admin/income/installment_due');
        $this->load->view('common/footer');
    }

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

    public function edit_landincome($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['allCustomerId'] = $this->CREGModel->getallcustomerId();
            $data['incomeInfoByid'] = $this->incomesetupmodel->getincomeInfoByid($id);
            if (!empty($data['incomeInfoByid'])) {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/income/landincome/editincome', $data);
                $this->load->view('common/footer');
            } else {
                redirect(admin_url() . '/landincomesetup/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[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[payment_type]',
                        'label' => 'Payment Type',
                        'rules' => 'required|xss_clean|trim|numeric'
                    ),
                    array(
                        'field' => 'data[date]',
                        'label' => 'Date',
                        'rules' => 'required|xss_clean|trim'
                    ),
                    array(
                        'field' => 'data[customerId]',
                        'label' => 'Category Id',
                        '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']=$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/extraincomesetup/incomeSearch');
                    $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->incomesetupmodel->updateIncomeById($data);
                        if (!empty($execution)) {
                            $message['message'] = "Income Info  Updated......!";
                            $this->session->set_userdata($message);
                            redirect(admin_url() . '/extraincomesetup/incomeSearch', 'refresh');
                        } else {
                            $errormessage['errormessage'] = "Income Info Not Updated......!";
                            $this->session->set_userdata($errormessage);
                            redirect(admin_url() . '/extraincomesetup/incomeSearch', 'refresh');
                        }
                    } else {
                        $errormessage['errormessage'] = "Information Missing......!";
                        $this->session->set_userdata($errormessage);
                        redirect(admin_url() . '/extraincomesetup/incomeSearch', '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 invoice($id) {
        $admin_id = $this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id != "")) {
            $data['invoiceInfo'] = $this->incomesetupmodel->getinvoiceInfoByid($id);
            //echo '<pre>';print_r($data);die();
            if ($data['invoiceInfo']) {
                $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() . '/landincomesetup/viewincome', 'refresh');
            }
        } else {
            redirect(base_url('admin', 'refresh'));
        }
    }

}
