[ic] Gift Certificates

Russ Mann interchange-users@interchange.redhat.com
Mon Apr 8 15:19:01 2002


> I don't, really. Total cost is the sum of subtotal + shipping +
> handling +
> salestax. I apply the credit to each of these in turn via using the
> [assign] tag, until the cert value has been consumed or they have
> all been
> reduced to zero. Total cost just becomes a 'report' of the net effect at
> that point.
>
> - Ed L.

Dear Ed,

Sorry for previous bottom quoting, I just read the thread on that and will
change my ways.

Can you send some code on how you do this?  The code I'm using is below, and
it requires multiple checks.  I was afraid to try doing that with four
numbers, instead of just the subtotal.  FYI, I'm currently using a serial
gift certificate number, with a second code for a password.  Could probably
reduce it to just the password as the G.C. code.

Thanks,

Russ

[if !scratch order_discounted]
[seti pre_gc_subtotal_cart][subtotal noformat=1][/seti]
[seti subtotal_cart][subtotal noformat=1][/seti]
[flag type=write tables="gift_certs"]
[perl tables="gift_certs"]
$redeemed_by = $Session->{username};
$subtotal = $Scratch->{subtotal_cart};
$gift_cert_number = $Values->{gift_cert_number};
$gift_cert_passcode = $Values->{gift_cert_passcode};
$gift_certs = $Db{gift_certs};

if ($gift_certs->record_exists($gift_cert_number)) {
    $passcode_getter = $gift_certs->field_accessor(passcode);
    $db_passcode = $passcode_getter->($gift_cert_number);
	if ($db_passcode eq $gift_cert_passcode) {
    $gift_value_getter = $gift_certs->field_accessor(available_amount);
    $db_gift_value = $gift_value_getter->($gift_cert_number);
	if ($db_gift_value <= 0) {
return <<EOP;
Certificate has already been fully redeemed.
EOP
	}
    $redeemed_amount_getter = $gift_certs->field_accessor(redeemed_amount);
    $db_redeemed_amount = $redeemed_amount_getter->($gift_cert_number);
	    if ($db_gift_value <= $subtotal) {
		$new_subtotal = $subtotal - $db_gift_value;
		$Scratch->{coupon_amount} = $db_gift_value;
		$Scratch->{order_discounted} = 1;
		$redeemed_amount = $db_redeemed_amount + $db_gift_value;
		$available_amount = 0;
		$redeemed = 1;
		$active = 0;
	$sql_req = "UPDATE gift_certs SET redeemed_amount = '$redeemed_amount',
available_amount = '$available_amount', redeemed = '$redeemed', active =
'$active', redeemed_by = '$redeemed_by', update_date = now() WHERE code =
'$gift_cert_number'";
	$Db{gift_certs}->query("$sql_req");
$print_db_gift_value = $Tag->currency(0, 0,  $db_gift_value);
return <<EOP;
This gift cerfificate was successfully redeemed.  The discounted amount is
$print_db_gift_value.  Certificate has been fully redeemed.
EOP
		} else {
		$new_subtotal = 0;
		$Scratch->{coupon_amount} = $subtotal;
		$Scratch->{order_discounted} = 1;
		$redeemed_amount = $db_redeemed_amount + $subtotal;
		$available_amount = $db_gift_value - $subtotal;
		$redeemed = 0;
		$active = 1;
	$sql_req = "UPDATE gift_certs SET redeemed_amount = '$redeemed_amount',
available_amount = '$available_amount', redeemed = '$redeemed', active =
'$active', redeemed_by = '$redeemed_by', update_date = now() WHERE code =
'$gift_cert_number'";
	$Db{gift_certs}->query("$sql_req");
$print_subtotal = $Tag->currency(0, 0,  $subtotal);
$print_available_amount = $Tag->currency(0, 0,  $available_amount);
return <<EOP;
This gift cerfificate was successfully redeemed.  The discounted amount is
$print_subtotal.  Certificate has $print_available_amount remaining.
EOP
		}
	} else {
return <<EOP;
The information you entered was incorrect.  Please check your Gift
Certificate email and try again.
EOP
	}
} else {
return <<EOP;
The information you entered was incorrect.  Please check your Gift
Certificate email and try again.
EOP
}

[/perl]
[discount ENTIRE_ORDER]$s - $Scratch->{coupon_amount};[/discount]
[/if]