<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: RefundModel.php, v 1.0 2013/03/16 11:18:11
//All functions 
//
class RefundModel extends Model {
    public function saveCronExecution() {
        $sql = "	INSERT INTO
					" . DB_PREFIX . "refund_executions
					(
						refundExecutionDate
					)
				VALUES
					(
						NOW()
					)";
        $this->ObjDatabase->setQuery($sql);
        return $this->ObjDatabase->getInsertId();
    }
    public function createRefundTransaction($orderID = '', $productID, $userID, $arrTransaction, $refundExecutionID, $masterOrderID = '') {
        $sql = "	INSERT INTO
								" . DB_PREFIX . "refund_transactions
								(
									masterOrderID,
									orderID,
									productID,
									userID,
									completedDate,
									refundType,
									refundGateway,
									refundAmount,
									refundExecutionID
								)
							VALUES
								(
									'" . $masterOrderID . "',
									'" . $orderID . "',
									'" . $productID . "',
									'" . $userID . "',
									NOW(),
									'" . $arrTransaction['refundType'] . "',
									'" . $this->escape($arrTransaction['refundGateway']) . "',
									'" . $arrTransaction['refundAmount'] . "',
									'" . $refundExecutionID . "'
								)";
        $this->ObjDatabase->setQuery($sql);
        return $this->ObjDatabase->getInsertId();
    }
    public function updateRefundTransaction($orderID = '', $refundTransactionID = '', $arrTransaction, $masterOrderID = '') {
        $sql = "	UPDATE
								" . DB_PREFIX . "refund_transactions
							SET
								transactionID='" . $arrTransaction['transactionID'] . "',
								completedDate=NOW(),
								refundResponse='" . $this->escape($arrTransaction['refundResponse']) . "',
								refundAmount='" . $arrTransaction['refundAmount'] . "'
							WHERE
								1";
        if ($masterOrderID) {
            $sql .= " 		AND	masterOrderID='" . $masterOrderID . "'";
        }
        if ($orderID) {
            $sql .= "		AND	orderID='" . $orderID . "'";
        }
        if ($refundTransactionID) {
            $sql .= "		AND	refundTransactionID='" . $refundTransactionID . "'";
        }
        return $this->ObjDatabase->setQuery($sql);
    }
    public function getRefundDetails($orderID = '', $productID = '', $userID = '', $masterOrderID = '') {
        $sql = "	SELECT
								transactionID,
								completedDate,
								refundType,
								refundGateway,
								refundResponse,
								refundAmount 
							FROM
								" . DB_PREFIX . "refund_transactions
							WHERE
								1";
        if ($masterOrderID) {
            $sql .= "	AND masterOrderID='" . $masterOrderID . "'";
        }
        if ($orderID) {
            $sql .= "	AND orderID='" . $orderID . "'";
        }
        if ($productID) {
            $sql .= "	AND productID='" . $productID . "'";
        }
        if ($userID) {
            $sql .= "	AND userID='" . $userID . "'";
        }
        $sql .= "	ORDER BY
								refundTransactionID DESC";
        return $this->ObjDatabase->readValue($sql);
    }
}