From 9157588f9789190386e914598e8e23b362c70139 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Wed, 2 Jul 2014 11:44:20 +0200 Subject: [PATCH] MT#7803 API: fetch invoice details via API --- lib/NGCP/Panel/Controller/API/Invoices.pm | 190 ++++++++++++++++++ lib/NGCP/Panel/Controller/API/InvoicesItem.pm | 91 +++++++++ lib/NGCP/Panel/Form/Invoice/InvoiceAPI.pm | 89 ++++++++ lib/NGCP/Panel/Role/API/Invoices.pm | 79 ++++++++ 4 files changed, 449 insertions(+) create mode 100644 lib/NGCP/Panel/Controller/API/Invoices.pm create mode 100644 lib/NGCP/Panel/Controller/API/InvoicesItem.pm create mode 100644 lib/NGCP/Panel/Form/Invoice/InvoiceAPI.pm create mode 100644 lib/NGCP/Panel/Role/API/Invoices.pm diff --git a/lib/NGCP/Panel/Controller/API/Invoices.pm b/lib/NGCP/Panel/Controller/API/Invoices.pm new file mode 100644 index 0000000000..9a8f38c011 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/Invoices.pm @@ -0,0 +1,190 @@ +package NGCP::Panel::Controller::API::Invoices; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::CheckTrailingSlash; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +class_has 'api_description' => ( + is => 'ro', + isa => 'Str', + default => + 'Defines invoices generated by the system.', +); + +class_has 'query_params' => ( + is => 'ro', + isa => 'ArrayRef', + default => sub {[ + { + param => 'period_start_ge', + description => 'Filter for invoices newer or equal to the given date (YYYY-MM-DDThh:mm:ss)', + query => { + first => sub { + my $q = shift; + { 'period_start' => { '>=' => $q }}; + }, + second => sub { }, + }, + }, + { + param => 'period_end_le', + description => 'Filter for invoices older or equal to the given date (YYYY-MM-DDThh:mm:ss)', + query => { + first => sub { + my $q = shift; + { 'period_end' => { '<=' => $q }}; + }, + second => sub { }, + }, + }, + { + param => 'reseller_id', + description => 'Filter for invoices of customers belonging to a certain reseller', + query => { + first => sub { + my $q = shift; + { 'contact.reseller_id' => $q }; + }, + second => sub { + return { join => { contract => 'contact' } }; + }, + }, + }, + { + param => 'customer_id', + description => 'Filter for invoices belonging to a specific customer', + query => { + first => sub { + my $q = shift; + { contract_id => $q }; + }, + second => sub {}, + }, + }, + { + param => 'serial', + description => 'Filter for invoices matching a serial (patterns allowed)', + query => { + first => sub { + my $q = shift; + { serial => { like => $q }}; + }, + second => sub {}, + }, + }, + ]}, +); + +with 'NGCP::Panel::Role::API::Invoices'; + +class_has('resource_name', is => 'ro', default => 'invoices'); +class_has('dispatch_path', is => 'ro', default => '/api/invoices/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-invoices'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => [qw/admin reseller/], + Args => 0, + Does => [qw(ACL CheckTrailingSlash RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods } + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); +} + +sub GET :Allow { + my ($self, $c) = @_; + my $page = $c->request->params->{page} // 1; + my $rows = $c->request->params->{rows} // 10; + { + my $items = $self->item_rs($c); + (my $total_count, $items) = $self->paginate_order_collection($c, $items); + my (@embedded, @links); + my $form = $self->get_form($c); + for my $item ($items->all) { + push @embedded, $self->hal_from_item($c, $item, $form); + push @links, Data::HAL::Link->new( + relation => 'ngcp:'.$self->resource_name, + href => sprintf('/%s%d', $c->request->path, $item->id), + ); + } + push @links, + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s', $c->request->path, $page, $rows)); + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows)); + } + + my $hal = Data::HAL->new( + embedded => [@embedded], + links => [@links], + ); + $hal->resource({ + total_count => $total_count, + }); + my $response = HTTP::Response->new(HTTP_OK, undef, + HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name, + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/InvoicesItem.pm b/lib/NGCP/Panel/Controller/API/InvoicesItem.pm new file mode 100644 index 0000000000..f97448afcf --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/InvoicesItem.pm @@ -0,0 +1,91 @@ +package NGCP::Panel::Controller::API::InvoicesItem; +use Sipwise::Base; +use namespace::sweep; +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::DateTime; +use NGCP::Panel::Utils::ValidateJSON qw(); +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +with 'NGCP::Panel::Role::API::Invoices'; + +class_has('resource_name', is => 'ro', default => 'invoices'); +class_has('dispatch_path', is => 'ro', default => '/api/invoices/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-invoices'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => [qw/admin reseller/], + Args => 1, + Does => [qw(ACL RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods } + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); +} + +sub GET :Allow { + my ($self, $c, $id) = @_; + { + last unless $self->valid_id($c, $id); + my $item = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, invoice => $item); + + my $hal = $self->hal_from_item($c, $item); + + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + (map { # XXX Data::HAL must be able to generate links with multiple relations + s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|; + s/rel=self/rel="item self"/; + $_ + } $hal->http_headers), + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c, $id) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c, $id) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Patch => 'application/json-patch+json', + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/Invoice/InvoiceAPI.pm b/lib/NGCP/Panel/Form/Invoice/InvoiceAPI.pm new file mode 100644 index 0000000000..576972de76 --- /dev/null +++ b/lib/NGCP/Panel/Form/Invoice/InvoiceAPI.pm @@ -0,0 +1,89 @@ +package NGCP::Panel::Form::Invoice::InvoiceAPI; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler'; +use Moose::Util::TypeConstraints; + +has_field 'customer_id' => ( + type => 'PosInteger', + label => 'Customer', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The customer this invoice belongs to.'] + }, +); + +has_field 'serial' => ( + type => 'Text', + label => 'Serial', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The invoice serial number.'] + }, +); + +has_field 'period_start' => ( + type => '+NGCP::Panel::Field::DateTime', + label => 'Invoice Period Start', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The start of the invoice period.'] + }, +); + +has_field 'period_end' => ( + type => '+NGCP::Panel::Field::DateTime', + label => 'Invoice Period End', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The end of the invoice period.'] + }, +); + +has_field 'amount_net' => ( + type => 'Money', + label => 'Net Amount', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The net amount of the invoice in USD, EUR etc.'] + }, +); + +has_field 'amount_vat' => ( + type => 'Money', + label => 'VAT Amount', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The vat amount of the invoice in USD, EUR etc.'] + }, +); + +has_field 'amount_total' => ( + type => 'Money', + label => 'Total Amount', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The vat amount of the invoice in USD, EUR etc.'] + }, +); + +has_field 'sent_date' => ( + type => '+NGCP::Panel::Field::DateTime', + label => 'Invoice Period Start', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The date the invoice has been sent by email or null if not sent.'] + }, +); + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/Invoices.pm b/lib/NGCP/Panel/Role/API/Invoices.pm new file mode 100644 index 0000000000..6c7983281e --- /dev/null +++ b/lib/NGCP/Panel/Role/API/Invoices.pm @@ -0,0 +1,79 @@ +package NGCP::Panel::Role::API::Invoices; +use Moose::Role; +use Sipwise::Base; +with 'NGCP::Panel::Role::API' => { + -alias =>{ item_rs => '_item_rs', }, + -excludes => [ 'item_rs' ], +}; + +use boolean qw(true); +use TryCatch; +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Status qw(:constants); +use NGCP::Panel::Form::Invoice::InvoiceAPI; + +sub item_rs { + my ($self, $c) = @_; + + my $item_rs = $c->model('DB')->resultset('invoices'); + if($c->user->roles eq "admin") { + } elsif($c->user->roles eq "reseller") { + $item_rs = $item_rs->search({ + 'contact.reseller_id' => $c->user->reseller_id + },{ + join => { contract => 'contact' }, + }); + } + return $item_rs; +} + +sub get_form { + my ($self, $c) = @_; + return NGCP::Panel::Form::Invoice::InvoiceAPI->new(ctx => $c); +} + +sub hal_from_item { + my ($self, $c, $item, $form) = @_; + my %resource = $item->get_inflated_columns; + $resource{customer_id} = delete $resource{contract_id}; + + my $hal = Data::HAL->new( + links => [ + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), + Data::HAL::Link->new(relation => 'ngcp:customers', href => sprintf("/api/customers/%d", $item->contract_id)), + ], + relation => 'ngcp:'.$self->resource_name, + ); + + $form //= $self->get_form($c); + + $self->validate_form( + c => $c, + resource => \%resource, + form => $form, + run => 0, + exceptions => [qw/customer_id/], + ); + + $resource{id} = int($item->id); + $hal->resource({%resource}); + return $hal; +} + +sub item_by_id { + my ($self, $c, $id) = @_; + my $item_rs = $self->item_rs($c); + return $item_rs->find($id); +} + +1; +# vim: set tabstop=4 expandtab: