<?php

class M_journalvoucher extends CI_Model {

    public $table = 'at_journalvoucher';
    public $detail = 'at_jvchdetail';
    public $journal = 'at_jvchjournal';
    public $account = "at_compaccountgl";

    function get_all() {
        /* =========================lemparan parah dari custom.js untuk searching_data_transaksi===================== */
        $param = $this->input->post('param');
        /* ====================================end=================================================================== */

        /* =================================post dari find di kiri==================================================== */
        $Number = isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
        $Desc = isset($param['txt_Description']) ? $param['txt_Description'] : '';

        $DateFrom = isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
        $DateTo = isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
        $TransStatus = isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
        $BranchCode = isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
        /* ============================================end============================================================ */

        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        /* ==========================================by session======================================================= */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select('at_journalvoucher.*, at_journalvoucher.JVchTotalDebet as JournalVoucherAmout');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_journalvoucher.BranchCode');
        $this->db->join('am_employee', 'am_employee.EmployeeCode = at_journalvoucher.CreatedBy', 'INNER');

        /* ===================================find by number dan desc========================================================= */
        if ($Number != "") {
            $this->db->like($this->table . '.JVchNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like($this->table . '.JVchDesc', $Desc);
        }
        /* =============================================end=================================================================== */

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where($this->table . '.BranchCode', $BranchCode);
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi by company-------------------------------------//
            /*
              branch status 0 = HO
              branch status 1 = depo/branch
              jangan lupa sesion data di atas untuk setiap kondisi
              jangan lupa tambahin case when trans status
             */
            $this->db->where($this->table . '.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }
        /* =======================================================end======================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where($this->table . '.JVchDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where($this->table . '.JVchDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }

        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if ($TransStatus != '') {
            $this->db->where_in($this->table . '.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in($this->table . '.TransStatus', array(0, 1, 4, 5, 6, 7));
        }
        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.JVchNumber', 'ASC');
        $this->db->order_by($this->table . '.TransStatus', 'DESC');
        /* ===============================end================================================================================= */
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        return $query->result();
    }

    function get_all_to_print($data_filter) {
        /* =================================post dari find di kiri==================================================== */
        $CompanyCode    = $data_filter['cmb_company'];
        $BranchCode     = $data_filter['cmb_branch'];
        $Number         = $data_filter['txt_IDNumber'];
        $Desc           = $data_filter['txt_Description'];
        $DateFrom       = $data_filter['txt_DateFrom'];
        $DateTo         = $data_filter['txt_DateTo'];
        $TransStatus    = $data_filter['chk_TransStatus'];
        /* ============================================end============================================================ */

        /* ==========================================by session======================================================= */
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');
        $sess_branch_status = $this->session->userdata('branch_status');
        /* =========================================================================================================== */

        $this->db->select('at_journalvoucher.*, at_journalvoucher.JVchTotalDebet as JournalVoucherAmout');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_journalvoucher.BranchCode');
        $this->db->join('am_employee', 'am_employee.EmployeeCode = at_journalvoucher.CreatedBy', 'INNER');

        /* ===================================find by number dan desc========================================================= */
        if ($Number != "") {
            $this->db->like($this->table . '.JVchNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like($this->table . '.JVchDesc', $Desc);
        }
        /* =============================================end=================================================================== */

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where($this->table . '.BranchCode', $BranchCode);
        } else {
            $this->db->where($this->table . '.BranchCode', $sess_branch);
        }
        /* =======================================================end======================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where($this->table . '.JVchDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where($this->table . '.JVchDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }

        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if (!empty($TransStatus)) {
            $this->db->where_in($this->table . '.TransStatus', $TransStatus);
        } else {
            $this->db->where_in($this->table . '.TransStatus', array(0, 1, 4, 5, 6, 7));
        }
        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.JVchNumber', 'ASC');
        $this->db->order_by($this->table . '.TransStatus', 'DESC');
        /* ===============================end================================================================================= */
        $query = $this->db->get();
        return $query->result();
    }
    function get_jvch_type($id) {
        $this->db->select_min('JVchType');
        $this->db->from('at_jvchdetail');
        $this->db->where('JVchNumber', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $JVchType = $row->JVchType;
            }

            return $JVchType;
        } else {
            return FALSE;
        }
    }

    function get_jvch_amount($id) {
        $this->db->select('SUM(JVchDebetSaldo) as amount');
        $this->db->from('at_jvchdetail');
        $this->db->where('JVchNumber', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $amount = $row->amount;
            }

            return $amount;
        } else {
            return FALSE;
        }
    }

    function get_akun() {
        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        /* =================================post dari find di atas pop up==================================================== */
        $param = $this->input->post('param');
        $AccountNumber = isset($param['txt_AccountNumber']) ? $param['txt_AccountNumber'] : '';
        $AccountName = isset($param['txt_AccountName']) ? $param['txt_AccountName'] : '';
        /* ============================================end============================================================ */

        /* =========================by session==================================================================== */
        $sess_branch = $this->session->userdata('branch');
        $sess_company = $this->session->userdata('company');
        /* =============================end====================================================================== */

        $this->db->select('at_compaccountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.AccountSubTypeID, am_accountgl.CurrencyCode, am_currency.CurrencyRate, am_accountsubtype.AccountTypeName, am_accounttype.AccountTypeName as TypeName');
        $this->db->from($this->account);
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $this->db->join('am_accountsubtype', 'am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');
        $this->db->join('am_currency', 'am_currency.CurrencyCode = am_accountgl.CurrencyCode');
        $this->db->join('am_accounttype', 'am_accounttype.AccountTypeID = am_accountsubtype.AccountTypeID');

        $this->db->where('at_compaccountgl.BranchCode', $sess_branch);
        $this->db->where('am_currency.CompanyCode', $sess_company);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('at_compaccountgl.TransStatus', 2);
        $this->db->where('am_accountsubtype.StatusSelected', 1);

        //$this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        /* ===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ================== */
        if ($AccountNumber != '') {
            $this->db->like('am_accountgl.AccountNumber', $AccountNumber);
        }
        if ($AccountName != '') {
            $this->db->like('am_accountgl.AccountName', $AccountName);
        }
        /* ====================================end====================================================================================== */

        $this->db->order_by('at_compaccountgl.AccountNumber', 'asc');
        $this->db->limit($end, $beginning);

        return $this->db->get()->result();
    }

    function get_vendor($currency) {
        $branch = $this->session->userdata('branch');
        $company = $this->session->userdata('company');
        /* =================================post dari find di atas pop up==================================================== */
        $param = $this->input->post('param');
        $VendorCode = isset($param['txt_VendorCode']) ? $param['txt_VendorCode'] : '';
        $VendorName = isset($param['txt_VendorName']) ? $param['txt_VendorName'] : '';
        $CurrencyCode = isset($param['txt_CurrencyCode']) ? $param['txt_CurrencyCode'] : '';
        /* ============================================end============================================================ */

        $this->db->select('am_vendor.*,am_currency.CurrencyRate');
        $this->db->from('am_vendor');
        $this->db->join('am_currency', 'am_vendor.CurrencyCode=am_currency.CurrencyCode', 'LEFT');


        $this->db->where('am_vendor.TransStatus !=', 3);
        $this->db->where('am_vendor.BranchCode =', $branch);
        if (($VendorCode == '') && ($VendorName == '')) {
            $this->db->where('am_vendor.CurrencyCode =', $currency);
        } else {
            $this->db->where('am_vendor.CurrencyCode =', $CurrencyCode);
        }

        $this->db->where('am_currency.CompanyCode =', $company);
        if ($VendorCode != '') {
            $this->db->like('am_vendor.VendorCode ', $VendorCode);
        }

        if ($VendorName != '') {
            $this->db->like('am_vendor.VendorName ', $VendorName);
        }

        $this->db->order_by('am_vendor.VendorCode', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }

    function check_code_exist($new_jv_number) {
        $query = $this->db->get_where($this->table, array('JVchNumber' => $new_jv_number), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function create_jv_number_for_add_balance($date = '', $branch) {
        if ($date != '') {
            $month = date('m', strtotime($date));
            $year = date('y', strtotime($date));
        } else {
            $month = date('m');
            $year = date('y');
        }

        $jv_number = $branch . "/" . $month . $year . "JV/";

        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(JVchNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(JVchNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('JVchNumber', $jv_number);
        $query = $this->db->get($this->table);
        $this->db->_protect_identifiers = true;

        $num = $query->row()->New_Number;
        $long = strlen($num);

        if ($long == 1) {
            $number = '00' . $num;
        } elseif ($long == 2) {
            $number = '0' . $num;
        } else {
            $number = $num;
        }

        $new_jv_number = $jv_number . $number;

        while ($this->check_code_exist($new_jv_number)) {
            $num++;
            $long = strlen($num);

            if ($long == 1) {
                $number = '00' . $num;
            } elseif ($long == 2) {
                $number = '0' . $num;
            } else {
                $number = $num;
            }

            $new_jv_number = $jv_number . $number;
        }

        return $new_jv_number;
    }

    function create_jv_number($date = '') {
        $branch = $this->session->userdata('branch');

        if ($date != '') {
            $month = date('m', strtotime($date));
            $year = date('y', strtotime($date));
        } else {
            $month = date('m');
            $year = date('y');
        }

        $jv_number = $branch . "/" . $month . $year . "JV/";

        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(JVchNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(JVchNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('JVchNumber', $jv_number);
        $query = $this->db->get($this->table);
        $this->db->_protect_identifiers = true;

        $num = $query->row()->New_Number;
        $long = strlen($num);

        if ($long == 1) {
            $number = '00' . $num;
        } elseif ($long == 2) {
            $number = '0' . $num;
        } else {
            $number = $num;
        }

        $new_jv_number = $jv_number . $number;

        while ($this->check_code_exist($new_jv_number)) {
            $num++;
            $long = strlen($num);

            if ($long == 1) {
                $number = '00' . $num;
            } elseif ($long == 2) {
                $number = '0' . $num;
            } else {
                $number = $num;
            }

            $new_jv_number = $jv_number . $number;
        }

        return $new_jv_number;
    }

    /* ==============================================================end================================= */

    function get_customer($currency) {
        $branch = $this->session->userdata('branch');
        $company = $this->session->userdata('company');

        /* =================================post dari find di atas pop up==================================================== */
        $param = $this->input->post('param');
        $CustomerCode = isset($param['txt_CustomerCode']) ? $param['txt_CustomerCode'] : '';
        $CustomerName = isset($param['txt_CustomerName']) ? $param['txt_CustomerName'] : '';
        $CurrencyCode = isset($param['txt_CurrencyCode']) ? $param['txt_CurrencyCode'] : '';
        /* ============================================end============================================================ */

        $this->db->select('am_customer.*,am_currency.CurrencyRate');
        $this->db->from('am_customer');
        $this->db->join('am_currency', 'am_customer.CurrencyCode=am_currency.CurrencyCode', 'LEFT');


        $this->db->where('am_customer.TransStatus !=', 3);
        $this->db->where('am_customer.BranchCode =', $branch);

        if (($CustomerCode == '') && ($CustomerName == '')) {
            $this->db->where('am_customer.CurrencyCode =', $currency);
        } else {
            $this->db->where('am_customer.CurrencyCode =', $CurrencyCode);
        }

        $this->db->where('am_currency.CompanyCode =', $company);

        if ($CustomerCode != '') {
            $this->db->like('am_customer.CustomerCode ', $CustomerCode);
        }

        if ($CustomerName != '') {
            $this->db->like('am_customer.CustomerName ', $CustomerName);
        }

        $this->db->order_by('am_customer.CustomerCode', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }
	
	function reverse_transsaction($trans_data){
		$this->db->trans_start();
		
		if(!empty($trans_data['delete_dp_sales']['TransNumber'])){
			for($x=0;$x<count($trans_data['delete_dp_sales']['TransNumber']);$x++){
				$this->delete_dp($trans_data['delete_dp_sales']['TransNumber'][$x],'at_sorddetaildp','SOrdNumber');
			}
		}
		
		if(!empty($trans_data['delete_dp_purchase']['TransNumber'])){
			for($x=0;$x<count($trans_data['delete_dp_purchase']['TransNumber']);$x++){
				$this->delete_dp($trans_data['delete_dp_purchase']['TransNumber'][$x],'at_pordetaildp','POrdNumber');
			}
		}	
		
		$this->update($trans_data['update_group']['data'],$trans_data['update_group']['TransNumber']);
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function approved_transaction($trans_data){
		$this->db->trans_start();
		
		if(!empty($trans_data['insert_sord_detail_dp']['data'])){
			for($x=0;$x<count($trans_data['insert_sord_detail_dp']['data']);$x++){
				$this->insert_detail_dp($trans_data['insert_sord_detail_dp']['data'][$x], 'at_sorddetaildp');
			}
		}
		
		if(!empty($trans_data['insert_pord_detail_dp']['data'])){
			for($x=0;$x<count($trans_data['insert_pord_detail_dp']['data']);$x++){
				$this->insert_detail_dp($trans_data['insert_pord_detail_dp']['data'][$x], 'at_pordetaildp');
			}
		}
		
		$this->update($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
    function insert($data) {
        $this->db->insert($this->table, $data);
    }

    function insert_detail($data_detail) {
        $this->db->_protect_identifiers = FALSE;
        $this->db->insert($this->detail, $data_detail);
        $this->db->_protect_identifiers = TRUE;
    }

    function insert_journal($data_journal) {
        $this->db->insert($this->journal, $data_journal);
    }

    function update($data, $txt_no) {
        $this->db->update($this->table, $data, array('JVchNumber' => $txt_no));
    }

    function update_detail($detail_data, $JVchDetailID) {
        $this->db->update($this->detail, $detail_data, array('JVchDetailID' => $JVchDetailID));
    }

    function delete_jurnal($id) {
        $this->db->delete($this->journal, array('JVchNumber' => $id));
    }
	
	function delete_dp($id,$table_dp,$field){
        $this->db->delete($table_dp, array($field => $id));
    }
	
	function insert_detail_dp($data_pord_detail_dp, $table_dp) {
        $this->db->insert($table_dp, $data_pord_detail_dp);
    }
	
    // Function Suspend	
    function suspend($data, $JVchNumber) {
        $this->db->update($this->table, $data, array('JVchNumber' => $JVchNumber));
    }

    function delete_data_grid($id) {
        $this->db->delete($this->detail, array('JVchDetailID' => $id));
    }

    function get_detail_print($id) {
        $this->db->select('at_journalvoucher.TransStatus, at_journalvoucher.JVchDate, am_company.CompanyName, am_branch.BranchName,
							am_branch.BranchAddress,
							at_journalvoucher.JVchNumber, am_branch.BranchName, at_journalvoucher.JVchDesc, 
							prepared.EmployeeName as nm_createby, mng.EmployeeName as nm_approve');
        $this->db->from($this->table);

        $this->db->join('am_branch', 'am_branch.BranchCode = at_journalvoucher.BranchCode');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode');
        $this->db->join('am_employee as prepared', 'prepared.EmployeeCode = at_journalvoucher.CreatedBy', 'LEFT');
        $this->db->join('am_employee as mng', 'mng.EmployeeCode = at_journalvoucher.AppLevel1Code', 'LEFT');

        $this->db->where('at_journalvoucher.JVchNumber', $id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function get_detail_item_print($id) {
        $this->db->_protect_identifiers = FALSE;
        $this->db->select(" 
							at_jvchdetail.JVchAccount, at_jvchdetail.JVchDebetSaldo, at_jvchdetail.JVchCreditSaldo, at_jvchdetail.JVchRate, at_jvchdetail.JVchDetailMemo,
							am_customer.CustomerName, am_vendor.VendorName, am_accountgl.AccountName
						");
        $this->db->join('am_customer', 'am_customer.CustomerCode = at_jvchdetail.CustomerCode', 'LEFT');
        $this->db->join('am_vendor', 'am_vendor.VendorCode = at_jvchdetail.VendorCode', 'LEFT');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_jvchdetail.JVchAccount');

        $this->db->where('at_jvchdetail.JVchNumber', $id);
        $this->db->distinct();
        $query = $this->db->get('at_jvchdetail');
        $this->db->_protect_identifiers = TRUE;
        return $query->result_array();
    }

    function get_detail($JVNumber) {
        $this->db->select('jvt.*, br.BranchName, cp.CompanyName');
        $this->db->from('at_journalvoucher as jvt');
        $this->db->join('am_branch as br', 'br.BranchCode = jvt.BranchCode', 'INNER');
        $this->db->join('am_company as cp', 'cp.CompanyCode = br.CompanyCode', 'INNER');
        $this->db->where('JVchNumber', $JVNumber);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function get_journal_by_number($JVchNumber) {
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('jvj.*, agl.AccountNumber, agl.AccountName');
        $this->db->from('at_jvchjournal as jvj');
        $this->db->join('am_accountgl as agl', 'agl.AccountNumber = jvj.JVchAccountNumber', 'INNER');
        $this->db->where('JVchNumber', $JVchNumber);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;

        return $query->result();
    }

    function get_detail_edit($id) {
        $this->db->select('BranchCode, JVchDate, JVchNumber, JVchDesc, JVchTotalDebet, JVchTotalCredit, 
		 					JVchType, AppLevel1Status, AppLevel1Code, AppLevel1Date, TransStatus, AppLevel2Status,CreatedBy');
        $this->db->from('at_journalvoucher');
        $this->db->where('JVchNumber', $id);

        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

    function get_journal_detail($jv_number) {
        $this->db->select('at_jvchdetail.JVchDebetSaldo as Debit, at_jvchdetail.JVchCreditSaldo as Credit, at_jvchdetail.JVchRate as Rate,
							at_jvchdetail.CurrencyCode, (at_jvchdetail.JVchDebetSaldo * at_jvchdetail.JVchRate) as Debit_Currency,
							(at_jvchdetail.JVchCreditSaldo * at_jvchdetail.JVchRate) as Credit_Currency,
							at_jvchdetail.JVchDetailMemo as Memo, am_customer.CustomerName as Customer, at_jvchdetail.CustomerCode, at_jvchdetail.JVchDetailID, 
							am_vendor.VendorName as Vendor, at_jvchdetail.VendorCode, 
							am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountsubtype.AccountTypeName as SubTypeName, 
							am_accounttype.AccountTypeName as AccountType');
        $this->db->from('at_jvchdetail');
        $this->db->join('am_customer', 'am_customer.CustomerCode=at_jvchdetail.CustomerCode', 'LEFT');
        $this->db->join('am_vendor', 'am_vendor.VendorCode=at_jvchdetail.VendorCode', 'LEFT');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber=at_jvchdetail.JVchAccount', 'INNER');

        $this->db->join('am_accountsubtype', 'am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');
        $this->db->join('am_accounttype', 'am_accounttype.AccountTypeID = am_accountsubtype.AccountTypeID');

        $this->db->where('JVchNumber', $jv_number);
        $query = $this->db->get();

        return $query->result();
    }
	
	function check_dp_for_reverse_jv($id,$table_group, $field_number, $field_id){
        $this->db->select($table_group.'.'.$field_number);
        $this->db->from($table_group);
	    
	    $this->db->where($table_group.'.'.$field_id,$id);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
    }
}
