From 31c4bf83d759729caaa67a667ba6b9374b176a5a Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Wed, 13 May 2009 12:52:19 +0000 Subject: [PATCH] Refactored genereation of tranasaction related tmpl_vars into WG::Shop::Transaction->getTransactionVars. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Account/Shop.pm | 92 +++---------------------------- lib/WebGUI/Shop/PayDriver.pm | 73 ++++--------------------- lib/WebGUI/Shop/Transaction.pm | 98 +++++++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 150 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index ef29a5a85..760bf1d67 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -22,6 +22,7 @@ - Added better Survey Expression Engine validation warnings - added #9203: Survey Visualization - Added: United States Postal Service Shipping Driver. + - Refactored transaction tmpl_var generation into WebGUI::Shop::Transaction. ( Martin Kamerbeek / Oqapi ) 7.7.5 - Adding StoryManager. diff --git a/lib/WebGUI/Account/Shop.pm b/lib/WebGUI/Account/Shop.pm index e4155ae8b..edce68d91 100644 --- a/lib/WebGUI/Account/Shop.pm +++ b/lib/WebGUI/Account/Shop.pm @@ -283,95 +283,17 @@ sub www_viewTransaction { my $session = $self->session; my $transactionId = $session->form->get('transactionId'); - my $transaction = shift || WebGUI::Shop::Transaction->new($session,$transactionId); + my $transaction = shift || WebGUI::Shop::Transaction->new( $session,$transactionId ); my $notice = shift; - return $session->insufficient unless ($transaction->get('userId') eq $session->user->userId); - - my $i18n = WebGUI::International->new($session, 'Shop'); - my ($style, $url) = $session->quick(qw(style url)); - - my %var = ( - %{$transaction->get}, - notice => $notice, - cancelRecurringUrl => $url->page('shop=transaction;method=cancelRecurring;transactionId='.$transaction->getId), - amount => sprintf("%.2f", $transaction->get('amount')), - inShopCreditDeduction => sprintf("%.2f", $transaction->get('inShopCreditDeduction')), - taxes => sprintf("%.2f", $transaction->get('taxes')), - shippingPrice => sprintf("%.2f", $transaction->get('shippingPrice')), - shippingAddress => $transaction->formatAddress({ - name => $transaction->get('shippingAddressName'), - address1 => $transaction->get('shippingAddress1'), - address2 => $transaction->get('shippingAddress2'), - address3 => $transaction->get('shippingAddress3'), - city => $transaction->get('shippingCity'), - state => $transaction->get('shippingState'), - code => $transaction->get('shippingCode'), - country => $transaction->get('shippingCountry'), - phoneNumber => $transaction->get('shippingPhoneNumber'), - }), - paymentAddress => $transaction->formatAddress({ - name => $transaction->get('paymentAddressName'), - address1 => $transaction->get('paymentAddress1'), - address2 => $transaction->get('paymentAddress2'), - address3 => $transaction->get('paymentAddress3'), - city => $transaction->get('paymentCity'), - state => $transaction->get('paymentState'), - code => $transaction->get('paymentCode'), - country => $transaction->get('paymentCountry'), - phoneNumber => $transaction->get('paymentPhoneNumber'), - }), - ); - - # items - my @items = (); - foreach my $item (@{$transaction->getItems}) { - my $address = ''; - if ($transaction->get('shippingAddressId') ne $item->get('shippingAddressId')) { - $address = $transaction->formatAddress({ - name => $item->get('shippingAddressName'), - address1 => $item->get('shippingAddress1'), - address2 => $item->get('shippingAddress2'), - address3 => $item->get('shippingAddress3'), - city => $item->get('shippingCity'), - state => $item->get('shippingState'), - code => $item->get('shippingCode'), - country => $item->get('shippingCountry'), - phoneNumber => $item->get('shippingPhoneNumber'), - }); - } - - # Post purchase actions - my $actionsLoop = []; - my $actions = $item->getSku->getPostPurchaseActions( $item ); - for my $label ( keys %{$actions} ) { - push @{$actionsLoop}, { - label => $label, - url => $actions->{$label}, - } - } - - my %taxConfiguration = %{ from_json( $item->get( 'taxConfiguration' ) || '{}' ) }; - my %taxVars = - map { ( "tax_$_" => $taxConfiguration{ $_ } ) } - keys %taxConfiguration; - - push @items, { - %{ $item->get }, - %taxVars, - viewItemUrl => $url->page('shop=transaction;method=viewItem;transactionId='.$transaction->getId.';itemId='.$item->getId), - price => sprintf("%.2f", $item->get('price')), - itemShippingAddress => $address, - orderStatus => $i18n->get($item->get('orderStatus')), - actionsLoop => $actionsLoop, - }; - } - $var{items} = \@items; - - $self->appendCommonVars(\%var); + return $session->insufficient unless $transaction->get('userId') eq $session->user->userId; + + my $var = $transaction->getTransactionVars; + $var->{ notice } = $notice; + $self->appendCommonVars( $var ); # render - return $self->processTemplate(\%var,$session->setting->get("shopMyPurchasesDetailTemplateId")); + return $self->processTemplate( $var, $session->setting->get('shopMyPurchasesDetailTemplateId') ); } diff --git a/lib/WebGUI/Shop/PayDriver.pm b/lib/WebGUI/Shop/PayDriver.pm index aaf1bd6e6..a0b4ca2ab 100644 --- a/lib/WebGUI/Shop/PayDriver.pm +++ b/lib/WebGUI/Shop/PayDriver.pm @@ -673,87 +673,32 @@ Sends out a receipt and a sale notification to the buyer and the store owner res sub sendNotifications { my ($self, $transaction) = @_; my $session = $self->session; - my $i18n = WebGUI::International->new($session, 'PayDriver'); - my ($style, $url) = $session->quick(qw(style url)); - my %var = ( - %{$transaction->get}, - viewDetailUrl => $url->page('shop=transaction;method=viewMy;transactionId='.$transaction->getId,1), - amount => sprintf("%.2f", $transaction->get('amount')), - inShopCreditDeduction => sprintf("%.2f", $transaction->get('inShopCreditDeduction')), - taxes => sprintf("%.2f", $transaction->get('taxes')), - shippingPrice => sprintf("%.2f", $transaction->get('shippingPrice')), - shippingAddress => $transaction->formatAddress({ - name => $transaction->get('shippingAddressName'), - address1 => $transaction->get('shippingAddress1'), - address2 => $transaction->get('shippingAddress2'), - address3 => $transaction->get('shippingAddress3'), - city => $transaction->get('shippingCity'), - state => $transaction->get('shippingState'), - code => $transaction->get('shippingCode'), - country => $transaction->get('shippingCountry'), - phoneNumber => $transaction->get('shippingPhoneNumber'), - }), - paymentAddress => $transaction->formatAddress({ - name => $transaction->get('paymentAddressName'), - address1 => $transaction->get('paymentAddress1'), - address2 => $transaction->get('paymentAddress2'), - address3 => $transaction->get('paymentAddress3'), - city => $transaction->get('paymentCity'), - state => $transaction->get('paymentState'), - code => $transaction->get('paymentCode'), - country => $transaction->get('paymentCountry'), - phoneNumber => $transaction->get('paymentPhoneNumber'), - }), - ); - - # items - my @items = (); - foreach my $item (@{$transaction->getItems}) { - my $address = ''; - if ($transaction->get('shippingAddressId') ne $item->get('shippingAddressId')) { - $address = $transaction->formatAddress({ - name => $item->get('shippingAddressName'), - address1 => $item->get('shippingAddress1'), - address2 => $item->get('shippingAddress2'), - address3 => $item->get('shippingAddress3'), - city => $item->get('shippingCity'), - state => $item->get('shippingState'), - code => $item->get('shippingCode'), - country => $item->get('shippingCountry'), - phoneNumber => $item->get('shippingPhoneNumber'), - }); - } - push @items, { - %{$item->get}, - viewItemUrl => $url->page('shop=transaction;method=viewItem;transactionId='.$transaction->getId.';itemId='.$item->getId, 1), - price => sprintf("%.2f", $item->get('price')), - itemShippingAddress => $address, - orderStatus => $i18n->get($item->get('orderStatus'),'Shop'), - }; - } - $var{items} = \@items; + my $i18n = WebGUI::International->new($session, 'PayDriver'); + my $url = $session->url; + my $var = $transaction->getTransactionVars; # render my $template = WebGUI::Asset::Template->new( $session, $self->get("receiptEmailTemplateId") ); my $inbox = WebGUI::Inbox->new($session); - my $receipt = $template->process(\%var); + my $receipt = $template->process( $var ); WebGUI::Macro::process($session, \$receipt); # purchase receipt $inbox->addMessage( { message => $receipt, - subject => $i18n->get('receipt subject').' '.$transaction->get('orderNumber'), + subject => $i18n->get('receipt subject') . ' ' . $transaction->get('orderNumber'), userId => $transaction->get('userId'), status => 'completed', } ); # shop owner notification - $var{viewDetailUrl} = $url->page('shop=transaction;method=view;transactionId='.$transaction->getId,1); - my $notification = $template->process(\%var); + # Shop owner uses method=view rather than method=viewMy + $var->{viewDetailUrl} = $url->page( 'shop=transaction;method=view;transactionId='.$transaction->getId, 1 ); + my $notification = $template->process( $var ); WebGUI::Macro::process($session, \$notification); $inbox->addMessage( { message => $notification, - subject => $i18n->get('a sale has been made').' '.$transaction->get('orderNumber'), + subject => $i18n->get('a sale has been made') . ' ' . $transaction->get('orderNumber'), groupId => $self->get('saleNotificationGroupId'), status => 'unread', } ); diff --git a/lib/WebGUI/Shop/Transaction.pm b/lib/WebGUI/Shop/Transaction.pm index 8fb618a6a..7720b7a62 100644 --- a/lib/WebGUI/Shop/Transaction.pm +++ b/lib/WebGUI/Shop/Transaction.pm @@ -3,7 +3,7 @@ package WebGUI::Shop::Transaction; use strict; use Class::InsideOut qw{ :std }; -use JSON; +use JSON qw{ from_json }; use WebGUI::Asset::Template; use WebGUI::Exception::Shop; use WebGUI::Form; @@ -395,6 +395,102 @@ sub getTransactionIdsForUser { #------------------------------------------------------------------- +sub getTransactionVars { + my $self = shift; + my $url = $self->session->url; + my $i18n = WebGUI::International->new( $self->session, 'Shop' ); + + my $var = { + %{ $self->get }, + viewDetailUrl => $url->page( 'shop=transaction;method=viewMy;transactionId='.$self->getId, 1 ), + cancelRecurringUrl => $url->page('shop=transaction;method=cancelRecurring;transactionId='.$self->getId), + amount => sprintf( "%.2f", $self->get('amount') ), + inShopCreditDeduction => sprintf( "%.2f", $self->get('inShopCreditDeduction') ), + taxes => sprintf( "%.2f", $self->get('taxes') ), + shippingPrice => sprintf( "%.2f", $self->get('shippingPrice') ), + shippingAddress => $self->formatAddress( { + name => $self->get('shippingAddressName'), + address1 => $self->get('shippingAddress1'), + address2 => $self->get('shippingAddress2'), + address3 => $self->get('shippingAddress3'), + city => $self->get('shippingCity'), + state => $self->get('shippingState'), + code => $self->get('shippingCode'), + country => $self->get('shippingCountry'), + phoneNumber => $self->get('shippingPhoneNumber'), + } ), + paymentAddress => $self->formatAddress({ + name => $self->get('paymentAddressName'), + address1 => $self->get('paymentAddress1'), + address2 => $self->get('paymentAddress2'), + address3 => $self->get('paymentAddress3'), + city => $self->get('paymentCity'), + state => $self->get('paymentState'), + code => $self->get('paymentCode'), + country => $self->get('paymentCountry'), + phoneNumber => $self->get('paymentPhoneNumber'), + } ), + }; + + # items + my @items = (); + foreach my $item (@{$self->getItems}) { + my $address = ''; + if ($self->get('shippingAddressId') ne $item->get('shippingAddressId')) { + $address = $self->formatAddress({ + name => $item->get('shippingAddressName'), + address1 => $item->get('shippingAddress1'), + address2 => $item->get('shippingAddress2'), + address3 => $item->get('shippingAddress3'), + city => $item->get('shippingCity'), + state => $item->get('shippingState'), + code => $item->get('shippingCode'), + country => $item->get('shippingCountry'), + phoneNumber => $item->get('shippingPhoneNumber'), + }); + } + + # Post purchase actions + my $actionsLoop = []; + my $actions = $item->getSku->getPostPurchaseActions( $item ); + for my $label ( keys %{$actions} ) { + push @{$actionsLoop}, { + label => $label, + url => $actions->{$label}, + } + } + + my %taxConfiguration = %{ from_json( $item->get( 'taxConfiguration' ) || '{}' ) }; + my %taxVars = + map { ( "tax_$_" => $taxConfiguration{ $_ } ) } + keys %taxConfiguration; + + my $price = $item->get('price'); + my $quantity = $item->get('quantity'); + my $taxRate = $item->get('taxRate'); + my $taxAmount = $price * $taxRate / 100; + + push @items, { + %{$item->get}, + %taxVars, + viewItemUrl => $url->page('shop=transaction;method=viewItem;transactionId='.$self->getId.';itemId='.$item->getId, 1), + price => sprintf("%.2f", $item->get('price')), + pricePlusTax => sprintf( "%.2f", $price + $taxAmount ), + extendedPrice => sprintf( "%.2f", $quantity * $price ), + extendedPricePlusTax => sprintf( "%.2f", $quantity * ( $price + $taxAmount ) ), + itemShippingAddress => $address, + orderStatus => $i18n->get( $item->get('orderStatus'), 'Shop' ), + actionsLoop => $actionsLoop, + }; + } + $var->{items} = \@items; + + return $var; + +} + +#------------------------------------------------------------------- + =head2 isFirst ( ) Returns 1 if this is the first of a set of recurring transactions.