<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/* 	
 * 	Author 	: Wahyu
 * 	Tanggal : 20-11-2014	
 * 	Revisi 	: 
 */

class Payment_listing extends CI_Controller {

    // inisialisasi manual
    public $menu_id = 264;
    // inisialisasi otomatis
    public $add = "add";
    public $edit = "edit";
    public $delete = "delete";
    public $to_preview = "to_preview";
    public $to_pdf = "to_pdf";
    public $to_excel = "to_excel";

    public function __construct() {
        parent::__construct();
        $this->load->model('m_menu');
        $this->load->model('m_payment_listing');
        $this->load->model('setup/m_branchinfo');
        $this->load->helper('message');
        $this->load->helper('status');
        $this->load->helper('convert_date');
        $this->load->helper('custom_string_helper');
    }

    // Function Index	
    public function index() {
        //untuk cek login dan menu apakah bisa diakses oleh user yang login
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            $arr_group_id = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);

            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    // set data
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('expense_cashbon/v_payment_listing', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    public function ajax_payment_listing() {
        $BranchCode = $this->session->userdata('branch');
        $listing_payment = $this->m_payment_listing->get_all($BranchCode);

//        foreach ($listing_payment as $row) {
//            $EBonDate = convert_to_dmy($row->EBonDate);
//            $EPayDate = convert_to_dmy($row->EPayDate);
//            $type = $row->EPRealTransType;
//
//            if ($type == 1) {
//                //CRV
//                $amount_crv = $row->EPRealAmount;
//                $amount_cpv = 0;
//            }
//
//            if ($type == 2) {
//                //CPV
//                $amount_crv = 0;
//                $amount_cpv = $row->EPRealAmount;
//            }
//
//            $hasil[] = array(
//                'BranchName' => $row->BranchName,
//                'DepartmentName' => $row->DepartmentName,
//                'EmployeeName' => $row->EmployeeName,
//                'EBonDate' => $EBonDate,
//                'EBonAmount' => $row->EBonAmount,
//                'EPayDate' => $EPayDate,
//                'ExpenseAmount' => $row->ExpenseAmount,
//                'amount_crv' => $amount_crv,
//                'amount_cpv' => $amount_cpv,
//                'EPayNumber' => $row->EPayNumber
//            );
//        }
        $hasil = $this->loop_data_in_ajax($listing_payment);

        if (!empty($hasil)) {
            echo json_encode($hasil);
        } else {
            $notFound = array();
            echo json_encode($notFound);
        }
    }

    public function to_preview($obj_string = null) {
//        $listing_payment = $this->m_payment_listing->get_all($BranchCode);
        $data['menu_id']        = $this->menu_id;
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id);
//        echo "obj_string = $obj_string <hr>";
        $data_filter            = arrayParamInUrlToStr($obj_string);
//        echo "data_filter = ".  json_encode($data_filter)."<hr>";
        $data['company']        = $this->m_branchinfo->view_detail($this->session->userdata('branch'));
        $listing_payment        = $this->m_payment_listing->get_all_to_print($data_filter);
        $data['list']           = $this->loop_data_in_ajax($listing_payment);
//        echo "list = ".  json_encode($data['list'] )."<hr>";
        
        $this->load->view('v_header');
        $this->load->view('expense_cashbon/v_print_payment_listing', $data);
        $this->load->view('v_footer');
    }
    public function to_preview_LAMA() {
        // set data
        $BranchCode = $this->session->userdata('branch');
        $listing_payment = $this->m_payment_listing->get_all($BranchCode);

        foreach ($listing_payment as $row) {
            $EBonDate = convert_to_dmy($row->EBonDate);
            $EPayDate = convert_to_dmy($row->EPayDate);
            $type = $row->EPRealTransType;

            if ($type == 1) {
                //CRV
                $amount_crv = $row->EPRealAmount;
                $amount_cpv = 0;
            }

            if ($type == 2) {
                //CPV
                $amount_crv = 0;
                $amount_cpv = $row->EPRealAmount;
            }

            $hasil[] = array(
                'BranchName' => $row->BranchName,
                'DepartmentName' => $row->DepartmentName,
                'EmployeeName' => $row->EmployeeName,
                'EBonDate' => $EBonDate,
                'EBonAmount' => $row->EBonAmount,
                'EPayDate' => $EPayDate,
                'ExpenseAmount' => $row->ExpenseAmount,
                'amount_crv' => $amount_crv,
                'amount_cpv' => $amount_cpv
            );
        }

        $data['list'] = json_encode($hasil);
        $this->load->view('v_header');
        $this->load->view('expense_cashbon/v_print_payment_listing', $data);
        $this->load->view('v_footer');
    }

    function loop_data_in_ajax($listing_payment){   
        $hasil = array();  
        foreach ($listing_payment as $row) {
            $EBonDate = convert_to_dmy($row->EBonDate);
            $EPayDate = convert_to_dmy($row->EPayDate);
            $type = $row->EPRealTransType;

            if ($type == 1) {
                //CRV
                $amount_crv = $row->EPRealAmount;
                $amount_cpv = 0;
            }

            if ($type == 2) {
                //CPV
                $amount_crv = 0;
                $amount_cpv = $row->EPRealAmount;
            }

            $hasil[] = array(
                'BranchName' => $row->BranchName,
                'DepartmentName' => $row->DepartmentName,
                'EmployeeName' => $row->EmployeeName,
                'EBonDate' => $EBonDate,
                'EBonAmount' => $row->EBonAmount,
                'EPayDate' => $EPayDate,
                'ExpenseAmount' => $row->ExpenseAmount,
                'amount_crv' => $amount_crv,
                'amount_cpv' => $amount_cpv,
                'EPayNumber' => $row->EPayNumber
            );
        }
        return $hasil;
    }
}
