<?php

class Kcardtransaction extends CI_Model
{
    public $transid;
    public $kcardno;
    public $customerid;
    public $invoiceno;
    public $purchasedate;
    public $expirydate;
    public $brcode;
    public $isactive;
    public $editdate;
    public $creationdate;

    public function __construct()
	{
		parent::__construct();

        $this->load->database();
    }


    public function getCardTransactionWithMinCustInfo($brcode)
    {
        $this->db->select("kcardtransaction.transid, kcardtransaction.customerid, customer.lastname, customer.firstname, kcardtransaction.kcardno, kcardtransaction.invoiceno, kcardtransaction.purchasedate, kcardtransaction.expirydate, kcardtransaction.brcode, kcardtransaction.isactive, kcardtransaction.editdate");
        $this->db->from("kcardtransaction");
        $this->db->join("customer", "kcardtransaction.customerid=customer.customerid");
        $this->db->where(array("kcardtransaction.brcode"=>$brcode, "kcardtransaction.isactive"=>1));

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

    public function getCardTransactionWithMinCustInfoOrderBy($col, $order, $brcode)
    {
        $this->db->select("kcardtransaction.transid, kcardtransaction.customerid, customer.lastname, customer.firstname, kcardtransaction.kcardno, kcardtransaction.invoiceno, kcardtransaction.purchasedate, kcardtransaction.expirydate, kcardtransaction.brcode, kcardtransaction.isactive, kcardtransaction.editdate");
        $this->db->from("kcardtransaction");
        $this->db->join("customer", "kcardtransaction.customerid=customer.customerid");
        $this->db->where(array("kcardtransaction.brcode"=>$brcode, "kcardtransaction.isactive"=>1));

        return $this->db->order_by($col, $order)->get();
    }

    public function getCardTransactionWithMinCustInfoByKCardNo($kcardno)
    {
        $this->db->select("kcardtransaction.transid, kcardtransaction.customerid, customer.lastname, customer.firstname, kcardtransaction.kcardno, kcardtransaction.invoiceno, kcardtransaction.purchasedate, kcardtransaction.expirydate, kcardtransaction.brcode, kcardtransaction.isactive, kcardtransaction.editdate");
        $this->db->from("kcardtransaction");
        $this->db->join("customer", "kcardtransaction.customerid=customer.customerid");
        $this->db->where(array("kcardtransaction.kcardno"=>$kcardno, "kcardtransaction.isactive"=>1));

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


    public function addNewCardTransaction()
    {
        $data = array(
            "kcardno" => $this->kcardno,
            "customerid" => $this->customerid,
            "invoiceno" => $this->invoiceno,
            "purchasedate" => $this->purchasedate,
            "expirydate" => $this->expirydate,
            "brcode" => $this->brcode,
            "isactive" => $this->isactive
        );

        $this->db->set($data);
        $this->db->set("editdate", "NOW()", FALSE);
        $success = $this->db->set("creationdate", "NOW()", FALSE)->insert("kcardtransaction");

        if($success)
            return array("success"=>$success, "data" => $this->db->insert_id());
        else
            return array("success"=>$success, "data" => $this->db->error());
    }


    public function deactivateCard($transid)
    {
        $this->db->where("transid", $transid);

        $success = $this->db->update("kcardtransaction", array("isactive"=>0));

        if($success)
            return array("success"=>$success, "data" => $transid." deactivated");
        else
            return array("success"=>$success, "data" => $this->db->error());
    }
}