<?php

class Giftcard_model extends CI_Model {

    /******************************** Funciones de Consulta **************************************/

    public function listado() {
        $dbps = (ENVIRONMENT == 'production') ? 'dementeg_presta' : 'dementelocal';

        $this->db->from('giftcards g');

		$this->db->join($dbps.'.ps_cart_rule cr','ON g.id_cupon = cr.id_cart_rule', 'left');
		$this->db->join($dbps.'.ps_order_cart_rule ocr','ON cr.id_cart_rule = ocr.id_cart_rule', 'left');
        $this->db->join($dbps.'.ps_orders o','ON ocr.id_order = o.id_order', 'left');

        $this->db->where('deleted', 0);
        $this->db->order_by('id', 'DESC');

        $giftcards = $this->db->get()->result_array();


        //prx($giftcards);


        return $giftcards;
    }

    public function info($id) {
        $this->db->from('giftcards');
        $this->db->where('id', $id);
        $this->db->where('deleted', 0);
        $traspaso = $this->db->get()->row_array();

        return $traspaso;
    }


       public function guardar($data) {
        //revisar si viene id y si viene se hace update
        if (empty($data['id'])) {
            $this->db->insert('giftcards', $data);
            $id = $this->db->insert_id();
        }
        else {
            $this->db->where('id', $data['id']);
            $this->db->update('giftcards', $data);
            $id = $data['id'];
        }

        $return['status'] = 'success';
        $return['id_giftcard'] = $id;

        return $return;
    }

	public function get_giftcards_pendientes_navidad() {
		$this->db->where('estado', 1);
		$this->db->where('id_tipo', 2);
		return $this->db->get('giftcards')->result_array();
	}
}