added in-store credit

added refund mechanism
modified ems to use refund mechanism
This commit is contained in:
JT Smith 2008-04-20 01:12:25 +00:00
parent 9d99c7e71d
commit 8d03655bdb
14 changed files with 441 additions and 43 deletions

View file

@ -434,7 +434,7 @@ sub www_view {
my $form = $session->form;
my $callback = $form->get('callback');
$callback =~ s/'/"/g;
$callback = JSON::from_json($callback);
$callback = JSON->new->utf8->decode($callback);
my $callbackForm = '';
foreach my $param (@{$callback->{params}}) {
$callbackForm .= WebGUI::Form::hidden($session, {name=>$param->{name}, value=>$param->{value}});

View file

@ -374,14 +374,13 @@ sub new {
=head2 onCompletePurchase ()
Calls onCompletePurchase() on all the items in the cart. Then deletes all the items in the cart without calling $item->remove() on them which would affect inventory levels. See also delete() and empty().
Deletes all the items in the cart without calling $item->remove() on them which would affect inventory levels. See also delete() and empty().
=cut
sub onCompletePurchase {
my $self = shift;
foreach my $item (@{$self->getItems}) {
$item->getSku->onCompletePurchase($item);
$item->delete;
}
$self->delete;

View file

@ -126,7 +126,7 @@ sub get {
return {};
}
else {
return JSON::from_json($properties{id $self}{$name});
return JSON->new->utf8->decode($properties{id $self}{$name});
}
}
return $properties{id $self}{$name};
@ -313,7 +313,7 @@ sub update {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") {
$properties{$id}{options} = JSON::to_json($newProperties->{options});
$properties{$id}{options} = JSON->new->utf8->encode($newProperties->{options});
}
$self->cart->session->db->setRow("cartItem","itemId",$properties{$id});
}

149
lib/WebGUI/Shop/Credit.pm Normal file
View file

@ -0,0 +1,149 @@
package WebGUI::Shop::Credit;
use strict;
use Class::InsideOut qw{ :std };
use WebGUI::Shop::Admin;
use WebGUI::Exception::Shop;
use WebGUI::International;
=head1 NAME
Package WebGUI::Shop::Credit
=head1 DESCRIPTION
Keeps track of what in-store credit is owed a customer. All refunds are issued as in-store credit.
=head1 SYNOPSIS
use WebGUI::Shop::Credit;
my $credit = WebGUI::Shop::Credit->new($session, $userId);
=head1 METHODS
These subroutines are available from this package:
=cut
readonly session => my %session;
readonly userId => my %userId;
#-------------------------------------------------------------------
=head2 adjust ( amount, [ comment ] )
Adjusts the amount of credit this user has by a specified amount.
=head3 amount
The amount to adjust the credit by. A positive number adds credit, and a negative number removes credit.
=head3 comment
The reason for this adjustment.
=cut
sub adjust {
my ($self, $amount, $comment) = @_;
$self->session->db->write("insert into shopCredit (creditId, userId, amount, comment, dateOfAdjustment) values (?,?,?,?,now())",
[$self->session->id->generate, $self->userId, $amount, $comment]);
}
#-------------------------------------------------------------------
=head2 getGeneralLedger ( session )
A class method. Returns a WebGUI::SQL::ResultSet containing the data from the shopCredit table for all users.
=head3 session
A reference to the current session.
=cut
sub getGeneralLedger {
my ($class, $session) = @_;
return $session->db->read("select * from shopCredit order by dateOfAdjustment");
}
#-------------------------------------------------------------------
=head2 getLedger ()
Returns a WebGUI::SQL::ResultSet containing the data from the shopCredit table for this user.
=cut
sub getLedger {
my $self = shift;
return $self->session->db->read("select * from shopCredit where userId=?",[$self->userId]);
}
#-------------------------------------------------------------------
=head2 getSum ()
Returns the amount of credit that is owed to this user.
=cut
sub getSum {
my $self = shift;
my $credit = $self->session->db->getScalar("select sum(amount) from shopCredit where userId=? order by dateOfAdjustment",[$self->userId]);
return sprintf("%.2f", $credit);
}
#-------------------------------------------------------------------
=head2 new ( session, userId )
Constructor.
=head3 session
A reference to the current session.
=head3 userId
A unique id for a user that you want to adjust the credit of.
=cut
sub new {
my ($class, $session, $userId) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
unless (defined $userId) {
WebGUI::Error::InvalidParam->throw( param=>$userId, error=>"Need a userId.");
}
my $self = register $class;
my $id = id $self;
$session{ $id } = $session;
$userId{ $id } = $userId;
return $self;
}
#-------------------------------------------------------------------
=head2 session ()
Returns a reference to the current session.
=cut
#-------------------------------------------------------------------
=head2 userId ()
Returns a reference to the userId.
=cut
1;

View file

@ -11,6 +11,7 @@ use WebGUI::International;
use WebGUI::Paginator;
use WebGUI::Shop::Admin;
use WebGUI::Shop::AddressBook;
use WebGUI::Shop::Credit;
use WebGUI::Shop::TransactionItem;
=head1 NAME
@ -69,7 +70,7 @@ sub addItem {
=head2 completePurchase ( transactionCode, statusCode, statusMessage )
See also denyPurchase(). Completes a purchase by updating the transaction as a success, and clearing the cart of it's items.
See also denyPurchase(). Completes a purchase by updating the transaction as a success, and calling onCompletePurchase on all the skus in the transaction.
=head3 transactionCode
@ -87,6 +88,9 @@ The extended status message that came back from the payment gateway when trying
sub completePurchase {
my ($self, $transactionCode, $statusCode, $statusMessage) = @_;
foreach my $item (@{$self->getItems}) {
$item->getSku->onCompletePurchase($item);
}
$self->update({
transactionCode => $transactionCode,
isSuccessful => 1,
@ -437,7 +441,7 @@ sub www_getTransactionsAsJson {
$results{'sort'} = undef;
$results{'dir'} = "desc";
$session->http->setMimeType('text/json');
return JSON::to_json(\%results);
return JSON->new->utf8->encode(\%results);
}
#-------------------------------------------------------------------

View file

@ -5,6 +5,7 @@ use Class::InsideOut qw{ :std };
use JSON;
use WebGUI::DateTime;
use WebGUI::Exception::Shop;
use WebGUI::Shop::Transaction;
=head1 NAME
@ -97,7 +98,7 @@ sub get {
return {};
}
else {
return JSON::from_json($properties{id $self}{$name});
return JSON->new->utf8->decode($properties{id $self}{$name});
}
}
return $properties{id $self}{$name};
@ -135,6 +136,21 @@ sub getSku {
return $asset;
}
#-------------------------------------------------------------------
=head2 issueCredit ( )
Returns the money from this item to the user in the form of in-store credit.
=cut
sub issueCredit {
my $self = shift;
my $credit = WebGUI::Shop::Credit->new($self->transaction->session, $self->transaction->get('userId'));
$credit->adjust($self->get('price'), "Issued credit on sku ".$self->get('assetId')." for transaction item ".$self->getId." on transaction ".$self->transaction->getId);
$self->getSku->onRefund($self);
$self->update({shippingStatus=>'Cancelled'});
}
#-------------------------------------------------------------------
@ -174,6 +190,36 @@ sub new {
return $self;
}
#-------------------------------------------------------------------
=head2 newByDynamicTransaction ( session, itemId )
Constructor, but will dynamically find the approriate transaction and attach it to the item object.
=head3 session
A reference to the current session.
=head3 itemId
The unique id for this transaction item.
=cut
sub newByDynamicTransaction {
my ($class, $session, $itemId) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
unless (defined $itemId) {
WebGUI::Error::InvalidParam->throw(error=>"Need an itemId.");
}
my $transactionId = $session->db->quickScalar("select transactionId from transactionItem where itemId=?",[$itemId]);
my $transaction = WebGUI::Shop::Transaction->new($session, $transactionId);
return $class->new($transaction, $itemId);
}
#-------------------------------------------------------------------
@ -243,7 +289,7 @@ sub update {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") {
$properties{$id}{options} = JSON::to_json($newProperties->{options});
$properties{$id}{options} = JSON->new->utf8->encode($newProperties->{options});
}
if (exists $newProperties->{shippingStatus}) {
$properties{$id}{shippingDate} = WebGUI::DateTime->new($self->transaction->session,time())->toDatabase;