From 0b5cc49707a65912a40803124a2bda1df7b8c267 Mon Sep 17 00:00:00 2001 From: Pasqualino Ferrentino Date: Thu, 20 Sep 2007 09:17:33 +0200 Subject: [PATCH] Added the GetInvoicesListByStateCommand, tested --- src/lib/Bcd/Commands/CommandFactory.pm | 2 + .../Bcd/Commands/GetInvoicesListByStateCommand.pm | 124 +++++++++++++++++++++ src/lib/Bcd/Data/Invoices.pm | 27 +++++ src/lib/Bcd/Errors/ErrorCodes.pm | 1 + src/t/TestInvoices.pm | 53 +++++++++ 5 files changed, 207 insertions(+) create mode 100644 src/lib/Bcd/Commands/GetInvoicesListByStateCommand.pm diff --git a/src/lib/Bcd/Commands/CommandFactory.pm b/src/lib/Bcd/Commands/CommandFactory.pm index f529c81..2e96a3e 100644 --- a/src/lib/Bcd/Commands/CommandFactory.pm +++ b/src/lib/Bcd/Commands/CommandFactory.pm @@ -111,6 +111,7 @@ use Bcd::Commands::PayInvoiceCommand; use Bcd::Commands::CollectInvoiceCommand; use Bcd::Commands::BuyObjectCommand; use Bcd::Commands::GetInvoicesSummaryCommand; +use Bcd::Commands::GetInvoicesListByStateCommand; use Data::Dumper; @@ -374,6 +375,7 @@ sub new{ $self->register_command("GetFoundersSummaryCommand"); $self->register_command("GetFounderProfileCommand"); $self->register_command("GetInvoicesSummaryCommand"); + $self->register_command("GetInvoicesListByStateCommand"); $self->register_command("GetPresencesCommand"); $self->register_command("GetPriceForThisAdCommand"); $self->register_command("GetOtherAntProfileCommand"); diff --git a/src/lib/Bcd/Commands/GetInvoicesListByStateCommand.pm b/src/lib/Bcd/Commands/GetInvoicesListByStateCommand.pm new file mode 100644 index 0000000..b904f35 --- /dev/null +++ b/src/lib/Bcd/Commands/GetInvoicesListByStateCommand.pm @@ -0,0 +1,124 @@ +package Bcd::Commands::GetInvoicesListByStateCommand; + +# This file is part of the breadcrumbs daemon (bcd). +# Copyright (C) 2007 Pasqualino Ferrentino + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +# Contact: lino.ferrentino@yahoo.it (in Italian, English or German). + +use strict; +use warnings; + +use threads; +use threads::shared; + +#My base class is the create user command +use Bcd::Commands::InvoiceCommand; +use base(qq/Bcd::Commands::InvoiceCommand/); +use Data::Dumper; +use Bcd::Constants::AdsConstants; + +use constant NAME => "ws_get_invoices_list"; + +sub get_name{ + return NAME; +} + +sub new { + my $class = shift; + my $self = $class->SUPER::new(@_); + + $self->{privilege_required} = Bcd::Data::Users::ANT_ROLE; + + #I have two parameters, one to have the emitted/received flag + push (@{$self->{parameters}}, "invoice_type"); + push (@{$self->{parameters}}, "invoice_state"); + + + bless ($self, $class); + return $self; +} + +sub _exec{ + my ($self, $stash) = @_; + + my $my_id = $stash->get_session_id($self->{session_id}); + + #I should build a dataset + my $emitted; + if ($self->{invoice_type} eq "emitted"){ + $emitted = 1; + } elsif($self->{invoice_type} eq "received"){ + $emitted = 0; + } else { + $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_ENUM_VALUE_UNDEFINED; + return; + } + + #now I should transform the state + my $state_required; + if ($self->{invoice_state} eq "authorized"){ + $state_required = Bcd::Constants::InvoicesConstants::AUTHORIZED; + } elsif ($self->{invoice_state} eq "emitted"){ + $state_required = Bcd::Constants::InvoicesConstants::EMITTED; + } elsif ($self->{invoice_state} eq "paid"){ + $state_required = Bcd::Constants::InvoicesConstants::PAID; + } elsif ($self->{invoice_state} eq "collected"){ + $state_required = Bcd::Constants::InvoicesConstants::COLLECTED; + } else { + $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_ENUM_VALUE_UNDEFINED; + return; + } + + #ok, now I can have the dataset... + my $ds; + if ($emitted == 1){ + $ds = Bcd::Data::Invoices->get_emitted_invoices_list_by_state_ds + ($stash, $my_id, $state_required); + } else { + $ds = Bcd::Data::Invoices->get_received_invoices_list_by_state_ds + ($stash, $my_id, $state_required); + } + + #ok, I now should add the nick in the dataset + push(@{$ds->[0]}, "nick"); + + foreach(@{$ds}[1..$#{$ds}]){ + #ok, I get the id + my $id = $_->[1]; + my $nick = Bcd::Data::Users->get_nick_from_id($stash, $id); + push(@{$_}, $nick); + } + + #ok, I can now freeze the dataset. + $self->{frozen_output} = Storable::freeze $ds; + + + #ok, it should be all ok + $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_OK; +} + +#I have the long token as the output +sub _add_your_specific_output{ + my ($self, $output) = @_; + + my $ds = Storable::thaw(delete $self->{frozen_output}); + $self->_add_record_set($ds, 'invoices_list', $output); + +} + +1; diff --git a/src/lib/Bcd/Data/Invoices.pm b/src/lib/Bcd/Data/Invoices.pm index 6b8b7b2..da3b697 100644 --- a/src/lib/Bcd/Data/Invoices.pm +++ b/src/lib/Bcd/Data/Invoices.pm @@ -55,12 +55,39 @@ use constant { GET_RECEIVED_INVOICES_SUMMARY_BY_STATE => qq{SELECT count(*), sum(quantity * amount) as total FROM invoices WHERE user_to = ? AND id_status = ?}, + GET_RECEIVED_INVOICES_LIST_BY_STATE => + qq{SELECT i.id, a.id_user as seller, i.amount, i.quantity, (i.amount * i.quantity) AS total }. + qq{FROM invoices AS i, ads AS a }. + qq{WHERE i.id_ad = a.id AND i.user_to = ? AND i.id_status = ?}, + GET_EMITTED_INVOICES_SUMMARY_BY_STATE => qq{SELECT count(i.*), sum(i.quantity * i.amount) as total FROM invoices AS i, ads AS a }. qq{WHERE i.id_ad = a.id AND a.id_user = ? AND i.id_status = ?}, + + GET_EMITTED_INVOICES_LIST_BY_STATE => + qq{SELECT i.id, i.user_to as buyer, i.amount, i.quantity, (i.amount * i.quantity) AS total }. + qq{FROM invoices AS i, ads AS a }. + qq{WHERE i.id_ad = a.id AND a.id_user = ? AND id_status = ?}, }; +sub get_received_invoices_list_by_state_ds{ + my ($class, $stash, $buyer, $state) = @_; + + return $stash->select_all_ds + (GET_RECEIVED_INVOICES_LIST_BY_STATE, + $buyer, $state); + +} + +sub get_emitted_invoices_list_by_state_ds{ + my ($class, $stash, $seller, $state) = @_; + + return $stash->select_all_ds + (GET_EMITTED_INVOICES_LIST_BY_STATE, + $seller, $state); +} + sub get_received_invoices_summary_by_state_arr{ my ($class, $stash, $buyer, $state) = @_; diff --git a/src/lib/Bcd/Errors/ErrorCodes.pm b/src/lib/Bcd/Errors/ErrorCodes.pm index ec2773c..11817e9 100644 --- a/src/lib/Bcd/Errors/ErrorCodes.pm +++ b/src/lib/Bcd/Errors/ErrorCodes.pm @@ -129,6 +129,7 @@ use constant { BEC_REQUIRED_PARAMETER_MISSING => 7005, BEC_BOOLEAN_PARAMETER_OUT_OF_RANGE => 7006, BEC_INTEGER_OUT_OF_RANGE => 7007, + BEC_ENUM_VALUE_UNDEFINED => 7008, diff --git a/src/t/TestInvoices.pm b/src/t/TestInvoices.pm index 41c42d8..d69e525 100644 --- a/src/t/TestInvoices.pm +++ b/src/t/TestInvoices.pm @@ -160,6 +160,41 @@ sub test_pay_invoice{ my $digest = $sha->hexdigest; $self->assert_equals($digest, $hash->{secret_token}); + + ########################### + ##Just a test for the list of invoices. + $command = Bcd::Data::Model->instance()->get_command("ws_get_invoices_list"); + $self->assert_not_null($command); + $command->parse_line($session); + $command->parse_line("received"); + $command->parse_line("paid"); + + $command->eot(); + + $command->exec_only_for_test($stash); + $exit_code = $command->get_exit_code(); + + $res = Bcd::Commands::CommandParser->parse_command_output($command->get_output()); + + $self->assert_equals(1, scalar(@{$res->{invoices_list}})); + $self->assert_equals("tina", $res->{invoices_list}->[0]->{nick}); + + $session = $self->_connect_to_bcd("tina", 8012898, "tinap"); + + $command->start_parsing_command(); + $command->parse_line($session); + $command->parse_line("emitted"); + $command->parse_line("paid"); + + $command->eot(); + + $command->exec_only_for_test($stash); + $exit_code = $command->get_exit_code(); + + $res = Bcd::Commands::CommandParser->parse_command_output($command->get_output()); + + $self->assert_equals(1, scalar(@{$res->{invoices_list}})); + $self->assert_equals("barbara", $res->{invoices_list}->[0]->{nick}); } sub test_collect_invoice{ @@ -392,6 +427,24 @@ sub test_invoice_basic{ $self->assert_equals(1, $res->{count_emitted_collected}); $self->assert_equals(2970, $res->{total_emitted_collected}); + + + #now I just test the two functions which return the invoices list + my $ds = Bcd::Data::Invoices->get_emitted_invoices_list_by_state_ds + ($stash, 16, Bcd::Constants::InvoicesConstants::COLLECTED); + + $self->assert_equals(17, $ds->[1]->[1]); + $self->assert_equals(90, $ds->[1]->[2]); + $self->assert_equals(33, $ds->[1]->[3]); + $self->assert_equals(2970, $ds->[1]->[4]); + + $ds = Bcd::Data::Invoices->get_received_invoices_list_by_state_ds + ($stash, 17, Bcd::Constants::InvoicesConstants::COLLECTED); + + $self->assert_equals(16, $ds->[1]->[1]); + $self->assert_equals(90, $ds->[1]->[2]); + $self->assert_equals(33, $ds->[1]->[3]); + $self->assert_equals(2970, $ds->[1]->[4]); } sub test_authorize_invoice_command{ -- 2.11.4.GIT