Merge commit '4969f31e1f' into WebGUI8

This commit is contained in:
Colin Kuskie 2010-06-26 14:37:31 -07:00
commit e5b82bc861
61 changed files with 2199 additions and 521 deletions

View file

@ -82,32 +82,33 @@ sub appendAddressFormVars {
$properties ||= {};
$prefix ||= '';
$var ||= {};
my $hasAddress = keys %{ $properties };
for ( qw{ address1 address2 address3 label firstName lastName city state organization } ) {
$var->{ $prefix . $_ . 'Field' } = WebGUI::Form::text( $session, {
name => $prefix . $_,
maxlength => 35,
defaultValue => $properties->{ $_ } || $form->get($prefix . $_),
defaultValue => $hasAddress ? $properties->{ $_ } : $form->get($prefix . $_),
} );
}
$var->{ $prefix . 'countryField' } =
WebGUI::Form::country( $session,{
name => $prefix . 'country',
defaultValue => $properties->{ country } || $form->get($prefix . 'country' ),
defaultValue => $hasAddress ? $properties->{ country } : $form->get($prefix . 'country' ),
} );
$var->{ $prefix . 'codeField' } =
WebGUI::Form::zipcode( $session, {
name => $prefix . 'code',
defaultValue => $properties->{ code } || $form->get($prefix . 'code' ),
defaultValue => $hasAddress ? $properties->{ code } : $form->get($prefix . 'code' ),
} );
$var->{ $prefix . 'phoneNumberField' } =
WebGUI::Form::phone( $session, {
name => $prefix . 'phoneNumber',
defaultValue => $properties->{ phoneNumber } || $form->get($prefix . 'phoneNumber' ),
defaultValue => $hasAddress ? $properties->{ phoneNumber } : $form->get($prefix . 'phoneNumber' ),
} );
$var->{ $prefix . 'emailField' } =
WebGUI::Form::email( $session, {
name => $prefix . 'email',
defaultValue => $properties->{ email } || $form->get($prefix . 'email' ),
defaultValue => $hasAddress ? $properties->{ email } : $form->get($prefix . 'email' ),
} );
}
@ -328,7 +329,7 @@ sub missingFields {
$addressData = $address;
}
my @missingFields = ();
FIELD: foreach my $field (qw/label firstName lastName address1 city code country phoneNumber/) {
FIELD: foreach my $field (qw/label firstName lastName address1 city state code country phoneNumber/) {
push @missingFields, $field if $addressData->{$field} eq '';
}
return @missingFields;

View file

@ -287,15 +287,39 @@ sub getBillingAddress {
#-------------------------------------------------------------------
=head2 getPaymentGateway ()
=head2 getI18nError ()
Returns the WebGUI::Shop::PayDriver object that is attached to this cart for payment.
Returns an internationalized version of the current error, if it exists.
=cut
sub getPaymentGateway {
my $self = shift;
return WebGUI::Shop::Pay->new($self->session)->getPaymentGateway($self->get("gatewayId"));
sub getI18nError {
my ($self) = @_;
my $error = $self->error;
my $i18n = WebGUI::International->new($self->session, 'Shop');
return $error eq 'no billing address' ? $i18n->get('no billing address')
: $error eq 'no shipping address' ? $i18n->get('no shipping address')
: $error eq 'billing label' ? $i18n->get('billing label')
: $error eq 'billing firstName' ? $i18n->get('billing firstName')
: $error eq 'billing lastName' ? $i18n->get('billing lastName')
: $error eq 'billing address1' ? $i18n->get('billing address1')
: $error eq 'billing city' ? $i18n->get('billing city')
: $error eq 'billing code' ? $i18n->get('billing code')
: $error eq 'billing state' ? $i18n->get('billing state')
: $error eq 'billing country' ? $i18n->get('billing country')
: $error eq 'billing phoneNumber' ? $i18n->get('billing phoneNumber')
: $error eq 'shipping label' ? $i18n->get('shipping label')
: $error eq 'shipping firstName' ? $i18n->get('shipping firstName')
: $error eq 'shipping lastName' ? $i18n->get('shipping lastName')
: $error eq 'shipping address1' ? $i18n->get('shipping address1')
: $error eq 'shipping city' ? $i18n->get('shipping city')
: $error eq 'shipping code' ? $i18n->get('shipping code')
: $error eq 'shipping state' ? $i18n->get('shipping state')
: $error eq 'shipping country' ? $i18n->get('shipping country')
: $error eq 'shipping phoneNumber' ? $i18n->get('shipping phoneNumber')
: $error eq 'no shipping method set' ? $i18n->get('Choose a shipping method and update the cart to checkout')
: $error eq 'no payment gateway set' ? $i18n->get('Choose a payment gateway and update the cart to checkout')
: $error ;
}
#-------------------------------------------------------------------
@ -377,6 +401,19 @@ sub getItemsByAssetId {
#-------------------------------------------------------------------
=head2 getPaymentGateway ()
Returns the WebGUI::Shop::PayDriver object that is attached to this cart for payment.
=cut
sub getPaymentGateway {
my $self = shift;
return WebGUI::Shop::Pay->new($self->session)->getPaymentGateway($self->get("gatewayId"));
}
#-------------------------------------------------------------------
=head2 getPosUser
Returns the userId of the user making a purchase. If there is a cashier and the cashier has specified a user, then that user will be returned. Otherwise, if it's a direct sale then $session->user will be returned.
@ -542,7 +579,7 @@ sub readyForCheckout {
}
if (my @missingFields = $book->missingFields($address->get)) {
$self->error($missingFields[0]);
$self->error('billing '.$missingFields[0]);
return 0;
}
@ -554,7 +591,7 @@ sub readyForCheckout {
}
if (my @missingFields = $book->missingFields($shipAddress->get)) {
$self->error($missingFields[0]);
$self->error('shipping '.$missingFields[0]);
return 0;
}
@ -702,8 +739,9 @@ Updates the cart totals, the address fields and the shipping and billing options
=cut
sub updateFromForm {
my $self = shift;
my $form = $self->session->form;
my $self = shift;
my $session = $self->session;
my $form = $session->form;
foreach my $item (@{$self->getItems}) {
if ($form->get("quantity-".$item->getId) ne "") {
eval { $item->setQuantity($form->get("quantity-".$item->getId)) };
@ -740,12 +778,12 @@ sub updateFromForm {
my $address = $self->getBillingAddress();
$address->update(\%billingData);
}
elsif ($billingAddressId ne 'new_address' && $billingAddressId) {
elsif ($billingAddressId ne 'new_address' && $billingAddressId ne 'update_address' && $billingAddressId) {
##User changed the address selector to another address field
$cartProperties->{billingAddressId} = $billingAddressId;
}
elsif (@missingBillingFields) {
$self->error('missing billing '.$missingBillingFields[0]);
$self->error('billing '.$missingBillingFields[0]);
}
else {
$self->session->log->warn('billing address: something else: ');
@ -763,7 +801,7 @@ sub updateFromForm {
my $shippingAddressId = $form->process('shippingAddressId');
##No missing shipping fields, if we set to the same as the billing fields
if (@missingShippingFields) {
$self->error('missing shipping '.$missingShippingFields[0]);
$self->error('shipping '.$missingShippingFields[0]);
}
if ($shippingAddressId eq 'new_address' && ! @missingShippingFields) {
##Add a new address
@ -775,7 +813,7 @@ sub updateFromForm {
my $address = $self->getBillingAddress();
$address->update(\%shippingData);
}
elsif ($shippingAddressId ne 'new_address' && $shippingAddressId) {
elsif ($shippingAddressId ne 'new_address' && $shippingAddressId ne 'update_address' && $shippingAddressId) {
$cartProperties->{shippingAddressId} = $shippingAddressId;
}
else {
@ -921,7 +959,7 @@ sub www_update {
##Handle rounding errors, and checkout immediately if the amount is 0 since
##at least the ITransact driver won't accept $0 checkout.
if (sprintf('%.2f', $total + $self->calculateShopCreditDeduction($total)) eq '0.00') {
my $transaction = WebGUI::Shop::Transaction->create($session, {self => $self});
my $transaction = WebGUI::Shop::Transaction->create($session, {cart => $self});
$transaction->completePurchase('zero', 'success', 'success');
$self->onCompletePurchase;
$transaction->sendNotifications();
@ -968,7 +1006,7 @@ sub www_view {
my %var = (
%{$self->get},
formHeader => WebGUI::Form::formHeader($session)
formHeader => WebGUI::Form::formHeader($session, { extras => q|id="wgCartId"|, })
. WebGUI::Form::hidden($session, {name=>"shop", value=>"cart"})
. WebGUI::Form::hidden($session, {name=>"method", value=>"update"})
. WebGUI::Form::hidden($session, {name=>"itemId", value=>""})
@ -1117,7 +1155,7 @@ sub www_view {
my $billingAddressId = $self->get('billingAddressId');
if ($billingAddressId) {
$billingAddressOptions{'update_address'} = $i18n->get('Update this address');
$billingAddressOptions{'update_address'} = sprintf $i18n->get('Update %s'), $self->getBillingAddress->get('label');
}
%billingAddressOptions = (%billingAddressOptions, %addressOptions);
@ -1133,7 +1171,7 @@ sub www_view {
my $shippingAddressId = $self->get('shippingAddressId');
if ($shippingAddressId) {
$shippingAddressOptions{'update_address'} = $i18n->get('Update this address');
$shippingAddressOptions{'update_address'} = sprintf $i18n->get('Update %s'), $self->getShippingAddress->get('label');
}
%shippingAddressOptions = (%shippingAddressOptions, %addressOptions);
@ -1183,12 +1221,12 @@ sub www_view {
$var{totalPrice} = $var{subtotalPrice} + $var{shippingPrice} + $var{tax};
my $credit = WebGUI::Shop::Credit->new($session, $posUser->userId);
$var{ inShopCreditAvailable } = $credit->getSum;
$var{ inShopCreditDeduction } = $credit->calculateDeduction($var{totalPrice});
$var{ inShopCreditDeduction } = $self->calculateShopCreditDeduction($var{totalPrice});
$var{ totalPrice } = $self->formatCurrency($var{totalPrice} + $var{inShopCreditDeduction});
foreach my $field (qw/subtotalPrice inShopCreditAvailable inShopCreditDeduction totalPrice shippingPrice tax/) {
$var{$field} = sprintf q|<span id="%sWrap">%s</span>|, $field, $var{$field};
}
$var{ error } = $self->error;
$var{ error } = $self->getI18nError;
# render the cart
my $template = WebGUI::Asset->newById($session, $session->setting->get("shopCartTemplateId"));

View file

@ -49,8 +49,11 @@ The reason for this adjustment.
sub adjust {
my ($self, $amount, $comment) = @_;
my $user = WebGUI::User->new($self->session, $self->userId);
return 0 if $user->isVisitor;
$self->session->db->write("insert into shopCredit (creditId, userId, amount, comment, dateOfAdjustment) values (?,?,?,?,now())",
[$self->session->id->generate, $self->userId, $amount, $comment]);
return $amount;
}
#-------------------------------------------------------------------
@ -179,11 +182,11 @@ sub www_adjust {
my ($class, $session) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->insufficient() unless $admin->canManage;
my $form = $session->form;
my $credit = $class->new($session, $form->get('userId'));
$credit->adjust($form->get('amount'), $form->get('comment'));
my $i18n = WebGUI::International->new($session, "Shop");
my $message = sprintf $i18n->get('add credit message'), $form->get('amount'), WebGUI::User->new($session, $form->get('userId'))->username, $credit->getSum;
my $form = $session->form;
my $credit = $class->new($session, $form->get('userId'));
my $amount = $credit->adjust($form->get('amount'), $form->get('comment'));
my $i18n = WebGUI::International->new($session, "Shop");
my $message = sprintf $i18n->get('add credit message'), $amount, WebGUI::User->new($session, $form->get('userId'))->username, $credit->getSum;
return $class->www_manage($session, $message);
}
@ -199,14 +202,16 @@ sub www_manage {
my ($class, $session, $message) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->insufficient() unless $admin->canManage;
my $i18n = WebGUI::International->new($session, "Shop");
my $f = WebGUI::HTMLForm->new($session);
$f->hidden(name=>'shop',value=>'credit');
$f->hidden(name=>'method',value=>'adjust');
my $i18n = WebGUI::International->new($session, "Shop");
my $f = WebGUI::HTMLForm->new($session);
my $userId = $session->form->process('userId') || $session->user->userId;
my $user = WebGUI::User->new($session, $userId);
$f->hidden(name => 'shop', value => 'credit');
$f->hidden(name => 'method', value => 'adjust');
$f->user(
name => 'userId',
label => $i18n->get('username'),
value => $session->user->userId,
value => $userId,
);
$f->float(
name => 'amount',
@ -217,6 +222,10 @@ sub www_manage {
label => $i18n->get('notes'),
);
$f->submit;
if (! $message) {
my $credit = $class->new($session, $userId);
$message ||= sprintf $i18n->get('current credit message'), $user->username, $credit->getSum;
}
return $admin->getAdminConsole->render($message.$f->print, $i18n->get('in shop credit'));
}