From cc3ff65fa7c4e49342607e85adc897d6ad8b7477 Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Wed, 18 Mar 2009 09:52:13 +0000 Subject: [PATCH] Added POD, reordered subs to be wg coding guidelines compliant. --- lib/WebGUI/Shop/Vendor.pm | 379 +++++++++++++++++++++----------------- 1 file changed, 205 insertions(+), 174 deletions(-) diff --git a/lib/WebGUI/Shop/Vendor.pm b/lib/WebGUI/Shop/Vendor.pm index 04cafe40f..9df6c2412 100644 --- a/lib/WebGUI/Shop/Vendor.pm +++ b/lib/WebGUI/Shop/Vendor.pm @@ -119,6 +119,48 @@ sub getId { #------------------------------------------------------------------- +=head2 getPayoutTotals ( ) + +Returns a hash ref, containing the payout details for this vendor. The keys in the hash are: + +=head3 paid + +The amount of money already transfered to the vendor. + +=head3 scheduled + +The amount of money scheduled to be transfered to the vendor. + +=head3 notPaid + +The amount of money that is yet to be scheduled for payment to the vendor. + +=head3 total + +The sum of these three values. + +=cut + +sub getPayoutTotals { + my $self = shift; + + my %totals = $self->session->db->buildHash( + 'select vendorPayoutStatus, sum(vendorPayoutAmount) as amount from transactionItem ' + .'where vendorId=? group by vendorPayoutStatus ', + [ $self->getId ] + ); + + # Format the payout categories and calc the total those. + %totals = + map { lcfirst $_ => sprintf '%.2f', $totals{ $_ } } + qw( Paid Scheduled NotPaid ); + $totals{ total } = sprintf '%.2f', sum values %totals; + + return \%totals; +} + +#------------------------------------------------------------------- + =head2 getVendors ( session, options ) Class method. Returns an array reference of WebGUI::Shop::Vendor objects. @@ -407,186 +449,14 @@ sub www_manage { return $console->render($output, $i18n->get("vendors")); } - #------------------------------------------------------------------- -=head2 getPayoutTotals ( ) +=head2 www_managePayouts ( ) -Returns a hash ref, containing the payout details for this vendor. The keys in the hash are: - -=head3 paid - -The amount of money already transfered to the vendor. - -=head3 scheduled - -The amount of money scheduled to be transfered to the vendor. - -=head3 notPaid - -The amount of money that is yet to be scheduled for payment to the vendor. - -=head3 total - -The sum of these three values. +Displays the payout manager. =cut -sub getPayoutTotals { - my $self = shift; - - my %totals = $self->session->db->buildHash( - 'select vendorPayoutStatus, sum(vendorPayoutAmount) as amount from transactionItem ' - .'where vendorId=? group by vendorPayoutStatus ', - [ $self->getId ] - ); - - # Format the payout categories and calc the total those. - %totals = - map { lcfirst $_ => sprintf '%.2f', $totals{ $_ } } - qw( Paid Scheduled NotPaid ); - $totals{ total } = sprintf '%.2f', sum values %totals; - - return \%totals; -} - -#------------------------------------------------------------------- - -=head2 www_submitScheduledPayouts () - -Sets the vendorPayoutStatus flag of scheduled payments to 'Paid'. - -NOTE: This method does no payments at all. In the future this method should trigger some automated payout -mechanism. - -=cut - -sub www_submitScheduledPayouts { - my $class = shift; - my $session = shift; - - my $admin = WebGUI::Shop::Admin->new($session); - return $session->privilege->adminOnly() unless ($admin->canManage); - - $session->db->write( - q{ update transactionItem set vendorPayoutStatus = 'Paid' where vendorPayoutStatus = 'Scheduled' } - ); - - return $class->www_managePayouts( $session ); -} - -#------------------------------------------------------------------- - -=head2 www_setPayoutStatus ( ) - -Sets the vendorPayoutStatus flag for each transaction passed by the form param 'itemId'. The new status is passed -by the form param 'status'. Status can either be 'NotPaid' or 'Scheduled' and may only be applied on items that do -not have their vendorPayoutStatus set to 'Paid'. - -Returns the status to which the item(s) are set. -=cut - -sub www_setPayoutStatus { - my $class = shift; - my $session = shift; - - my $admin = WebGUI::Shop::Admin->new($session); - return $session->privilege->adminOnly() unless ($admin->canManage); - - my @itemIds = $session->form->process('itemId'); - my $status = $session->form->process('status'); - return "error: wrong status [$status]" unless isIn( $status, qw{ NotPaid Scheduled } ); - - foreach my $itemId (@itemIds) { - my $item = WebGUI::Shop::TransactionItem->newByDynamicTransaction( $session, $itemId ); - return "error: invalid transactionItemId [$itemId]" unless $item; - return "error: cannot change status of a Paid item" if $item->get('vendorPayoutStatus') eq 'Paid'; - - $item->update({ vendorPayoutStatus => $status }); - } - - return $status; -} - -#------------------------------------------------------------------- - -=head2 www_vendorTotalsAsJSON ( ) - -Returns a JSON string containing all vendors and their payout details. If a vendor id is passed through form param -'vendorId' only results for that vendor will be returned. - -=cut - -sub www_vendorTotalsAsJSON { - my $class = shift; - my $session = shift; - - my $admin = WebGUI::Shop::Admin->new($session); - return $session->privilege->adminOnly() unless ($admin->canManage); - - my $vendorId = $session->form->process('vendorId'); - my ($vendorPayoutData, @placeholders); - - my @sql; - push @sql, - 'select vendorId, vendorPayoutStatus, sum(vendorPayoutAmount) as total from transactionItem'; - push @sql, ' where vendorId=? ' if $vendorId; - push @sql, ' group by vendorId, vendorPayoutStatus '; - - push @placeholders, $vendorId if $vendorId; - - my $sth = $session->db->read( join( ' ', @sql) , \@placeholders ); - while (my $row = $sth->hashRef) { - $vendorPayoutData->{ $row->{vendorId} }->{ $row->{vendorPayoutStatus} } = $row->{total}; - } - $sth->finish; - - my @dataset; - foreach my $vendorId (keys %{ $vendorPayoutData }) { - my $vendor = WebGUI::Shop::Vendor->new( $session, $vendorId ); - - push @dataset, { - %{ $vendor->get }, - %{ $vendorPayoutData->{ $vendorId } }, - } - } - - $session->http->setMimeType( 'application/json' ); - return JSON::to_json( { vendors => \@dataset } ); -} - -#------------------------------------------------------------------- -sub www_payoutDataAsJSON { - my $class = shift; - my $session = shift; - - my $admin = WebGUI::Shop::Admin->new($session); - return $session->privilege->adminOnly() unless ($admin->canManage); - - my $vendorId = $session->form->process('vendorId'); - my $startIndex = $session->form->process('startIndex'); - my $rowsPerPage = $session->form->process('results') || 100; - my $pageNumber = int( $startIndex / $rowsPerPage ) + 1; - - my $sql = - "select t1.* from transactionItem as t1 join transaction as t2 on t1.transactionId=t2.transactionId " - ." where vendorId=? and vendorPayoutAmount > 0 and vendorPayoutStatus <> 'Paid' order by t2.orderNumber"; - my $placeholders = [ $vendorId ]; - - my $paginator = WebGUI::Paginator->new( $session, '', $rowsPerPage, '', $pageNumber ); - $paginator->setDataByQuery( $sql, undef, 0, $placeholders ); - - my $data = { - totalRecords => $paginator->getRowCount, - results => $paginator->getPageData, - }; - - $session->http->setMimeType( 'application/json' ); - - return JSON::to_json( $data ); -} - -#------------------------------------------------------------------- sub www_managePayouts { my $class = shift; my $session = shift; @@ -629,4 +499,165 @@ CSS return $console->render($output, 'Vendor payout'); #$i18n->get("vendors")); } +#------------------------------------------------------------------- + +=head2 www_payoutDataAsJSON ( ) + +Returns a JSON string containing paginated payout data for a specific vendor. +The following form params should be passed: + +=head3 vendorId + +The vendorId of the vendor you want the payout data for. + +=head3 results + +The number of results to be returned. Defaults to 100. + +=head3 startIndex + +The index of the record at which the payout data should start. + +=cut + +sub www_payoutDataAsJSON { + my $class = shift; + my $session = shift; + + my $admin = WebGUI::Shop::Admin->new($session); + return $session->privilege->adminOnly() unless ($admin->canManage); + + my $vendorId = $session->form->process('vendorId'); + my $startIndex = $session->form->process('startIndex'); + my $rowsPerPage = $session->form->process('results') || 100; + my $pageNumber = int( $startIndex / $rowsPerPage ) + 1; + + my $sql = + "select t1.* from transactionItem as t1 join transaction as t2 on t1.transactionId=t2.transactionId " + ." where vendorId=? and vendorPayoutAmount > 0 and vendorPayoutStatus <> 'Paid' order by t2.orderNumber"; + my $placeholders = [ $vendorId ]; + + my $paginator = WebGUI::Paginator->new( $session, '', $rowsPerPage, '', $pageNumber ); + $paginator->setDataByQuery( $sql, undef, 0, $placeholders ); + + my $data = { + totalRecords => $paginator->getRowCount, + results => $paginator->getPageData, + }; + + $session->http->setMimeType( 'application/json' ); + + return JSON::to_json( $data ); +} + +#------------------------------------------------------------------- + +=head2 www_setPayoutStatus ( ) + +Sets the vendorPayoutStatus flag for each transaction passed by the form param 'itemId'. The new status is passed +by the form param 'status'. Status can either be 'NotPaid' or 'Scheduled' and may only be applied on items that do +not have their vendorPayoutStatus set to 'Paid'. + +Returns the status to which the item(s) are set. +=cut + +sub www_setPayoutStatus { + my $class = shift; + my $session = shift; + + my $admin = WebGUI::Shop::Admin->new($session); + return $session->privilege->adminOnly() unless ($admin->canManage); + + my @itemIds = $session->form->process('itemId'); + my $status = $session->form->process('status'); + return "error: wrong status [$status]" unless isIn( $status, qw{ NotPaid Scheduled } ); + + foreach my $itemId (@itemIds) { + my $item = WebGUI::Shop::TransactionItem->newByDynamicTransaction( $session, $itemId ); + return "error: invalid transactionItemId [$itemId]" unless $item; + return "error: cannot change status of a Paid item" if $item->get('vendorPayoutStatus') eq 'Paid'; + + $item->update({ vendorPayoutStatus => $status }); + } + + return $status; +} + +#------------------------------------------------------------------- + +=head2 www_submitScheduledPayouts () + +Sets the vendorPayoutStatus flag of scheduled payments to 'Paid'. + +NOTE: This method does no payments at all. In the future this method should trigger some automated payout +mechanism. + +=cut + +sub www_submitScheduledPayouts { + my $class = shift; + my $session = shift; + + my $admin = WebGUI::Shop::Admin->new($session); + return $session->privilege->adminOnly() unless ($admin->canManage); + + $session->db->write( + q{ update transactionItem set vendorPayoutStatus = 'Paid' where vendorPayoutStatus = 'Scheduled' } + ); + + return $class->www_managePayouts( $session ); +} + +#------------------------------------------------------------------- + +=head2 www_vendorTotalsAsJSON ( ) + +Returns a JSON string containing all vendors and their payout details. The following +form parameters can be passed: + +=head3 vendorId + +If passed, the results will include only the totals of this vendor. + +=cut + +sub www_vendorTotalsAsJSON { + my $class = shift; + my $session = shift; + + my $admin = WebGUI::Shop::Admin->new($session); + return $session->privilege->adminOnly() unless ($admin->canManage); + + my $vendorId = $session->form->process('vendorId'); + my ($vendorPayoutData, @placeholders); + + my @sql; + push @sql, + 'select vendorId, vendorPayoutStatus, sum(vendorPayoutAmount) as total from transactionItem'; + push @sql, ' where vendorId=? ' if $vendorId; + push @sql, ' group by vendorId, vendorPayoutStatus '; + + push @placeholders, $vendorId if $vendorId; + + my $sth = $session->db->read( join( ' ', @sql) , \@placeholders ); + while (my $row = $sth->hashRef) { + $vendorPayoutData->{ $row->{vendorId} }->{ $row->{vendorPayoutStatus} } = $row->{total}; + } + $sth->finish; + + my @dataset; + foreach my $vendorId (keys %{ $vendorPayoutData }) { + my $vendor = WebGUI::Shop::Vendor->new( $session, $vendorId ); + + push @dataset, { + %{ $vendor->get }, + %{ $vendorPayoutData->{ $vendorId } }, + } + } + + $session->http->setMimeType( 'application/json' ); + return JSON::to_json( { vendors => \@dataset } ); +} + 1; +