#!/usr/bin/perl
use strict;

package API::Order::GiftCard;

our $Config;
BEGIN {
	use API::Config;
	$Config = $API::Config::Config;
}
use lib ($Config->{'Website_libs'});

use ECS::SQL;
use ECS::Error;
use ECS::Config;

use ECS::Payment::GiftCard;

my $website_config = \%ECS::Config::c;

sub checkGiftCardExists {
	my $self = shift;
	my $input = shift;

	my $result = {
		'success' => 1,
		'error' => undef,
		'update_DB'	=> $Config->{'update_DB'}
	};
	
	return $result if (!$Config->{'update_DB'});
	
	unless ($input->{'giftCardNumber'}) {
		$result->{'success'} = 0;
		$result->{'error'} = 'No Gift Card Number specified to verify gift card.';
		return $result;
	}
	
	no warnings 'uninitialized';
	my $gift_card = ECS::Payment::GiftCard::load({
		'DBH'           => ECS::SQL::connect('SLAVE'),
		'cardNumber'    => $input->{'giftCardNumber'},
		'allowInactive' => 1,
	});
	$gift_card ||= { productID => undef };
	# check in OTHERSITE
	undef $gift_card unless $gift_card->{productID};
	if (!$gift_card) {
		 $gift_card = ECS::Payment::GiftCard::load({
			'DBH'			=> ECS::SQL::connect_tms('SLAVE'),
			'cardNumber'	=> $input->{'giftCardNumber'},
			'allowInactive' => 1,
		})
	}
	
	unless ($gift_card && $gift_card->{productID}) {
		$result->{'success'} = 0;
		$result->{'error'} = 'Failed to fetch gift card details or gift card does not exist.';
		return $result;
	}
	
	$result->{'data'} = $gift_card;
	return $result;
}

1;
