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

class M_import_recur extends CI_Model {
	public $table = 'ah_importrecur';
	
	function get_all() {
        /* =========================lemparan parah dari custom.js untuk searching_data_transaksi===================== */
        $param = $this->input->post('param');
        /* ====================================end=================================================================== */

        /* =================================post dari find di kiri==================================================== */
        $Number				= $this->input->post('NoTransaction');
		$Desc				= $this->input->post('ItemName');

       /* $DateFrom = isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
        $DateTo = isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';*/
        
        /* ============================================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("NoTransaction,OldNoTransaction, DateTransaction,ItemName,TotalInvoice,CurrencyRate,PeriodUsed, AdvNetDueIn");
        $this->db->from($this->table);
		$this->db->join('sys_advancedetail sysd', 'sysd.AdvDetailName = ah_importrecur.ItemName', 'INNER');
        

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

        

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

       

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.NoTransaction', 'ASC');
        /* ===============================end================================================================================= */
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        return $query->result();
    }
	 
	function get_invoice_recur($PInvNumber){
		$this->db->select('PInvDetailID');
		$this->db->from('at_pinvdetail as pinvd');
		$this->db->where('pinvd.PInvNumber', $PInvNumber);
		
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
	}
	
	function get_data_adv(){
		$this->db->select('sysd.AdvDetailName, sysd.AdvDetailDesc, sysd.AdvNetDueIn, sysd.AdvDetailID, sysd.AdvCategoryID');
		$this->db->from('sys_advancedetail as sysd');
		$this->db->join('sys_advancecategory sysc', 'sysc.AdvCategoryID = sysd.AdvCategoryID', 'INNER');
		$this->db->where('sysc.CompanyCode', $this->session->userdata('company'));
		
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
	}
	
	function get_data_currency(){
		$this->db->select('CurrencyCode,CurrencyName,CurrencyRate');
		$this->db->from('am_currency');
		
		$this->db->where('CompanyCode', $this->session->userdata('company'));
		$this->db->where('TransStatus', 1);
		
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return false;
        }
	}
	
	function insert_transaction($transaction_data){
		$this->db->trans_start();
		
		$this->insert($transaction_data['insert_group']['data']);
		$this->insert_pi_group($transaction_data['insert_group_pi']['data']);
		$this->insert_pi_detail($transaction_data['insert_detail_pi']['data'], $transaction_data['insert_detail_pi']['PInvNumber']);
		
		$this->insert_pi_journal($transaction_data['insert_journal_pi_debet_two']['data']);
		$this->insert_pi_journal($transaction_data['insert_journal_pi_kredit_two']['data']);
		
		$this->insert_jv_group($transaction_data['insert_group_jv']['data']);
		
		$this->insert_jv_detail($transaction_data['jv_detail_debet']['data']);
		$this->insert_jv_detail($transaction_data['jv_detail_credit']['data']);
		
		$this->insert_jv_journal($transaction_data['jv_journal_debet']['data']);
		$this->insert_jv_journal($transaction_data['jv_journal_credit']['data']);
		
		$this->update_pi_group($transaction_data['update_group_pi']['data'],$transaction_data['update_group_pi']['PInvNumber']);
		$this->update_jv_group($transaction_data['update_group_jv']['data'],$transaction_data['update_group_jv']['JVNumber']);
		
		//$this->insert_detail_recur($transaction_data['insert_recur']['data']);
		
		$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_pi_group($data) {
        $this->db->insert('at_purchaseinvoice', $data);
    }
	
	function insert_jv_group($data) {
        $this->db->insert('at_journalvoucher', $data);
    }
	
	function insert_pi_detail($data) {
        $this->db->insert('at_pinvdetail', $data);
    }
	
	function insert_pi_journal($data) {
        $this->db->insert('at_pinvjournal', $data);
    }
	
	function insert_jv_detail($data) {
        $this->db->insert('at_jvchdetail', $data);
    }
	
	function insert_jv_journal($data) {
        $this->db->insert('at_jvchjournal', $data);
    }
	
	function update_pi_group($data, $txt_no) {
        $this->db->update('at_purchaseinvoice', $data, array('PInvNumber' => $txt_no));
    }
	
	function update_jv_group($data, $txt_no) {
        $this->db->update('at_journalvoucher', $data, array('JVchNumber' => $txt_no));
    }
	
	function insert_detail_recur($data) {
        $this->db->insert('at_pinvdetailrecur', $data);
    }
}

/* End of file m_itemprice.php */
/* Location: ./application/modules_core/file/models/m_itemprice.php */