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

class OrderModel extends CI_Model { 

    public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function get_rows($where)
    {
		$sql = "select order_tb.*,
		               site_tb.title as site_name,
		               country_tb.symbol as symbol, country_tb.rate as currency_rate,
		               order_payments_tb.txn_id as txn_id, order_payments_tb.payment_gross as payment_amount, order_payments_tb.refund_status as refund_status
		        from order_tb
		        left join site_tb on order_tb.site_id = site_tb.id
		        left join country_tb on site_tb.country_id = country_tb.id
		        left join order_payments_tb on order_tb.id = order_payments_tb.order_id
		       ".$where;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	function get_for_order_rows($id)
    {
        $sql = "select order_history_tb.price as price, order_history_tb.quantity as quantity,
						souvenir_tb.title as merchandise_title, souvenir_tb.description as merchandise_desc,souvenir_tb.discount as discount
				from order_history_tb				
				left join souvenir_tb on order_history_tb.merchandise_id = souvenir_tb.id
				where order_history_tb.order_id = '".$id."'";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

    
	function get_for_order_row($id)
    {
		$sql = "select order_tb.id as row_id, order_tb.site_id as site_id,order_tb.orderID as orderID, order_tb.total_price as total_price, order_tb.created_at as orderDate, order_tb.isDelivery as delivery,
						country_tb.symbol as symbol, country_tb.rate as currency_rate,
						guest_tb.email as email,
						delivery_tb.address as delivery_address
				from order_tb
				left join site_tb on order_tb.site_id = site_tb.id
				left join country_tb on site_tb.country_id = country_tb.id
				left join guest_tb on order_tb.user_id = guest_tb.id
				left join delivery_tb on order_tb.id = delivery_tb.order_id
				where order_tb.orderID = '".$id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	public function insert_row($insert_data)
	{
		$this->db->insert('order_tb', $insert_data);
	}

	function delete_row($edit_id)
    {		
		$this->db->delete('order_tb', array('id' => $edit_id));
	}
	
	function get_row($id)
    {
		$sql = "select * from order_tb where id = '".$id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	function get_max_row()
    {
		$sql = "select max(orderID) as max_orderID from order_tb";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	function get_max_id_row()
    {
		$sql = "select max(id) as cnt from order_tb";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if(isset($row["cnt"]))
			return $row["cnt"];
		else
			return 0;
	}

	public function update_row($update_data, $id)
	{
		$this->db->update('order_tb', $update_data, $id);
	}	

	function get_count($where)
    {
		$sql = "select count(id) as cnt from order_tb".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if(isset($row["cnt"]))
			return $row["cnt"];
		else
			return 0;
	}
	
	function get_detail_rows($id)
    {
		$sql = "select order_history_tb.*, 
		               souvenir_tb.title as title, souvenir_tb.comp_photo_url as comp_photo_url, souvenir_tb.discount as discount, 
		               country_tb.symbol as symbol, country_tb.rate as currency_rate
		        from order_history_tb 
		        left join souvenir_tb on souvenir_tb.id = order_history_tb.merchandise_id
		        left join country_tb on souvenir_tb.country_id = country_tb.id
		        where order_history_tb.order_id = '".$id."'";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	public function insertTransaction($data){	
		$insert = $this->db->insert('order_payments_tb', $data);
		return $insert?true:false;
	}
	
	function get_payment_id($txn_id)
    {
		$sql = "select payment_id from order_payments_tb where txn_id = '".$txn_id."'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row["payment_id"];
    }
    
    
    function get_payment_id_byorderid($order_id)
    {
		$sql = "select payment_id from order_payments_tb where order_id = '".$order_id."'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row["payment_id"];
    }
	
	public function insert_refund_row($refund_data)
	{
		$this->db->insert('refund_order_tb', $refund_data);
	}
	
	public function update_order_row($update_data, $id)
	{
		$this->db->update('order_payments_tb', $update_data, $id);
	}
	
	function get_refund_rows($where)
    {
		$sql = "select refund_order_tb.id, refund_order_tb.created_at as refund_date, 
		                order_payments_tb.*, order_tb.orderID, order_tb.isDelivery, 
		                site_tb.title as site_name,
		                guest_tb.email as guest
		            from refund_order_tb 
		            left join order_payments_tb on refund_order_tb.txn_id = order_payments_tb.txn_id 
		            left join order_tb on order_payments_tb.order_id = order_tb.id 
		            left join site_tb on order_tb.site_id = site_tb.id
		            left join guest_tb on guest_tb.id = order_tb.user_id
		       ".$where;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	function get_guest_id($payment_id)
    {
		$sql = "select guest_id from order_payments_tb where payment_id = '".$payment_id."'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row["guest_id"];
    }
    
    function get_package_row($payment_id){
        $sql = "select txn_id, payment_gross from order_payments_tb where payment_id = '".$payment_id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
    }
}