/**
 * Function to refund the amount paid by Giftcard back to the Card when the entire order is refunded
 *
 */
function rpgc_refund_order($order_id)
{
    $giftCard_id = get_post_meta($order_id, 'rpgc_id', true);
    $giftCard_refunded = get_post_meta($order_id, 'rpgc_refunded', true);
    if ($giftCard_id && !($giftCard_refunded == 'yes')) {
        $oldBalance = wpr_get_giftcard_balance($giftCard_id);
        $refundAmount = get_post_meta($order_id, 'rpgc_payment', true);
        $giftcard_balance = (double) $oldBalance + (double) $refundAmount;
        update_post_meta($giftCard_id, 'rpgc_balance', $giftcard_balance);
        // Update balance of Giftcard
        update_post_meta($order_id, 'rpgc_refunded', 'yes');
        // prevents multiple refunds of Giftcard
    }
}
    public function sendGiftcardEmail($giftCard)
    {
        $expiry_date = wpr_get_giftcard_expiration($giftCard);
        $date_format = get_option('date_format');
        ob_start();
        ?>

		<div class="message">


			<?php 
        _e('Dear', 'rpgiftcards');
        ?>
 <?php 
        echo wpr_get_giftcard_to($giftCard);
        ?>
,<br /><br />
				
			<?php 
        echo wpr_get_giftcard_from($giftCard);
        ?>
 <?php 
        _e('has selected a', 'rpgiftcards');
        ?>
 <strong><a href="<?php 
        bloginfo('url');
        ?>
"><?php 
        bloginfo('name');
        ?>
</a></strong> <?php 
        _e('Gift Card for you! This card can be used for online purchases at', 'rpgiftcards');
        ?>
 <?php 
        bloginfo('name');
        ?>
. <br />

			<h4><?php 
        _e('Gift Card Amount', 'rpgiftcards');
        ?>
: <?php 
        echo woocommerce_price(wpr_get_giftcard_balance($giftCard));
        ?>
</h4>
			<h4><?php 
        _e('Gift Card Number', 'rpgiftcards');
        ?>
: <?php 
        echo get_the_title($giftCard);
        ?>
</h4>

			<?php 
        if ($expiry_date != "") {
            echo __('Expiration Date', 'rpgiftcards') . ': ' . date_i18n(get_option('date_format'), strtotime($expiry_date));
        }
        ?>
		</div>

		<div style="padding-top: 10px; padding-bottom: 10px; border-top: 1px solid #ccc;">
			<?php 
        echo wpr_get_giftcard_note($giftCard);
        ?>
		</div>

		<div style="padding-top: 10px; border-top: 1px solid #ccc;">
			<?php 
        _e('Using your Gift Card is easy', 'rpgiftcards');
        ?>
:

			<ol>
				<li><?php 
        _e('Shop at', 'rpgiftcards');
        ?>
 <?php 
        bloginfo('name');
        ?>
</li>
				<li><?php 
        _e('Select "Pay with a Gift Card" during checkout.', 'rpgiftcards');
        ?>
</li>
				<li><?php 
        _e('Enter your card number.', 'rpgiftcards');
        ?>
</li>
			</ol>
		</div>

		<?php 
        $return = ob_get_clean();
        return apply_filters('rpgc_email_content_return', $return, $giftCard);
    }
/**
 * Updates the Gift Card and the order information when the order is processed
 *
 */
function rpgc_update_card($order_id)
{
    global $woocommerce;
    $giftCard_id = WC()->session->giftcard_post;
    if ($giftCard_id != '') {
        //Decrease Ballance of card
        $giftcard = new WPR_Giftcard();
        $payment = $giftcard->wpr_get_payment_amount();
        $giftcard->wpr_decrease_balance($giftCard_id);
        $giftCard_IDs = get_post_meta($giftCard_id, 'wpr_existingOrders_id', true);
        $giftCard_IDs[] = $order_id;
        $newBalance = wpr_get_giftcard_balance($giftCard_id);
        update_post_meta($giftCard_id, 'rpgc_balance', $newBalance);
        // Update balance of Giftcard
        update_post_meta($giftCard_id, 'wpr_existingOrders_id', $giftCard_IDs);
        // Saves order id to gifctard post
        update_post_meta($order_id, 'rpgc_id', $giftCard_id);
        update_post_meta($order_id, 'rpgc_payment', $payment);
        update_post_meta($order_id, 'rpgc_balance', $newBalance);
        WC()->session->idForEmail = $order_id;
        unset(WC()->session->giftcard_payment, WC()->session->giftcard_post);
    }
    if (isset(WC()->session->giftcard_data)) {
        update_post_meta($order_id, 'rpgc_data', WC()->session->giftcard_data);
        unset(WC()->session->giftcard_data);
    }
}
 public function wpr_decrease_balance($giftCard_id)
 {
     $newBalance = wpr_get_giftcard_balance($giftCard_id) - $this->wpr_get_payment_amount();
     wpr_set_giftcard_balance($giftCard_id, $newBalance);
     // Check if the gift card ballance is 0 and if it is change the post status to zerobalance
     if (wpr_get_giftcard_balance($giftCard_id) == 0) {
         wpr_update_giftcard_status($giftCard_id, 'zerobalance');
     }
 }
/**
 * Updates the Gift Card and the order information when the order is processed
 *
 */
function rpgc_update_card($order_id)
{
    global $woocommerce;
    $giftCard_id = WC()->session->giftcard_post;
    if ($giftCard_id != '') {
        $newBalance = wpr_get_giftcard_balance($giftCard_id) - WC()->session->giftcard_payment;
        // Check if the gift card ballance is 0 and if it is change the post status to zerobalance
        if (wpr_get_giftcard_balance($giftCard_id) == 0) {
            wpr_update_giftcard_status($giftCard_id, 'zerobalance');
        }
        $giftCard_IDs = get_post_meta($giftCard_id, 'wpr_existingOrders_id', true);
        $giftCard_IDs[] = $order_id;
        update_post_meta($giftCard_id, 'rpgc_balance', $newBalance);
        // Update balance of Giftcard
        update_post_meta($giftCard_id, 'wpr_existingOrders_id', $giftCard_IDs);
        // Saves order id to gifctard post
        update_post_meta($order_id, 'rpgc_id', $giftCard_id);
        update_post_meta($order_id, 'rpgc_payment', WC()->session->giftcard_payment);
        update_post_meta($order_id, 'rpgc_balance', $newBalance);
        WC()->session->idForEmail = $order_id;
        unset(WC()->session->giftcard_payment, WC()->session->giftcard_post);
    }
    if (isset(WC()->session->giftcard_data)) {
        update_post_meta($order_id, 'rpgc_data', WC()->session->giftcard_data);
        unset(WC()->session->giftcard_data);
    }
}