diff --git a/lib/admin/Controller/peering.pm b/lib/admin/Controller/peering.pm index be424db..dd5c286 100644 --- a/lib/admin/Controller/peering.pm +++ b/lib/admin/Controller/peering.pm @@ -4,6 +4,7 @@ use strict; use warnings; use base 'Catalyst::Controller'; use Data::Dumper; +use admin::Utils; =head1 NAME @@ -30,8 +31,40 @@ sub index : Private { undef, \$peer_groups ); - $c->stash->{peer_groups} = $$peer_groups{result}; - $c->stash->{editid} = $c->request->params->{editid}; + if (ref $$peer_groups{result} eq 'ARRAY' and @{$$peer_groups{result}}) { + $c->stash->{peer_groups} = $$peer_groups{result}; + } + $c->stash->{editid} = $c->request->params->{editid}; + + my $peering_contracts; + my $contracts; + return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'get_sip_peering_contracts', + 1, + \$peering_contracts + ); + if (ref $$peering_contracts{result} eq 'ARRAY' and @{$$peering_contracts{result}}) { + $contracts = []; + #foreach my $sdentry (sort {$a->{id} <=> $b->{id}} @{$$peering_contracts{result}}) { + foreach my $peering_contract (@{$$peering_contracts{result}}) { + my %contract; # = {}; + $contract{id} = $peering_contract->{id}; + if(defined $peering_contract->{billing_profile} and length $peering_contract->{billing_profile}) { + my $profile; + return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'get_billing_profile', + { handle => $peering_contract->{billing_profile} }, + \$profile + ); + $contract{billing_profile_name} = $$profile{data}{name}; + } else { + $contract{billing_profile_name} = ''; + } + $contract{short_contact} = admin::Utils::short_contact($c,$peering_contract->{contact}); + #$contract{status} = $peering_contract->{status}; + $contract{create_timestamp} = $peering_contract->{create_timestamp}; + push @$contracts,\%contract; + } + $c->stash->{contracts} = $contracts; + } return 1; } @@ -745,6 +778,179 @@ sub copy_rewrite : Local { } +=head2 detail + +Show SIP peering contract details. + +=cut + +sub contract_detail : Local { + my ( $self, $c ) = @_; + $c->stash->{template} = 'tt/contract_detail.tt'; + + my $contract; + my $contract_id = $c->request->params->{contract_id} || undef; + if(defined $contract_id) { + return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'get_sip_peering_contract_by_id', + { id => $contract_id }, + \$contract + ); + } + if(ref $c->session->{restore_contract_input} eq 'HASH') { + if($c->config->{billing_features}) { + $contract->{billing_profile} = $c->session->{restore_contract_input}{billing_profile}; + } + $contract->{contact} = $c->session->{restore_contract_input}{contact}; + delete $c->session->{restore_contract_input}; + } + + # we only use this to fill the drop-down lists + if($c->request->params->{edit_contract}) { + my $billing_profiles; + return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'get_billing_profiles', + undef, + \$billing_profiles + ); + + $c->stash->{billing_profiles} = [ sort { $$a{data}{name} cmp $$b{data}{name} } + @{$$billing_profiles{result}} ]; + } else { + if(defined $contract->{billing_profile}) { + my $profile; + return unless $c->model('Provisioning')->call_prov( $c, 'billing', 'get_billing_profile', + { handle => $contract->{billing_profile} }, + \$profile + ); + $contract->{billing_profile_name} = $$profile{data}{name}; + } + } + + $c->stash->{contract} = $contract; + $c->stash->{contact_form_fields} = admin::Utils::get_contract_contact_form_fields($c,$contract->{contact}); + if($c->config->{billing_features}) { + $c->stash->{billing_features} = 1; + } + $c->stash->{edit_contract} = $c->request->params->{edit_contract}; + + return 1; +} + +=head2 save_account + +Create or update details of a SIP peering contract. + +=cut + +sub save_contract : Local { + my ( $self, $c ) = @_; + + my %messages; + my %settings; + + my $contract_id = $c->request->params->{contract_id} || undef; + + my $billing_profile = $c->request->params->{billing_profile}; + $settings{billing_profile} = $billing_profile if defined $billing_profile; + + my %contact; + my $contract_contact_form_fields = admin::Utils::get_contract_contact_form_fields($c,undef); + if (ref $contract_contact_form_fields eq 'ARRAY') { + foreach my $form_field (@$contract_contact_form_fields) { + if (defined $c->request->params->{$form_field->{field}} and length($c->request->params->{$form_field->{field}})) { + $contact{$form_field->{field}} = $c->request->params->{$form_field->{field}}; + } + } + } + $settings{contact} = \%contact; + + if(keys %settings or (!$c->config->{billing_features} and !defined $contract_id)) { + if(defined $contract_id) { + if($c->model('Provisioning')->call_prov( $c, 'billing', 'update_sip_peering_contract', + { id => $contract_id, + data => \%settings, + }, + undef)) + { + $messages{conmsg} = 'Server.Voip.SavedSettings'; + $c->session->{messages} = \%messages; + $c->response->redirect("/peering/contract_detail?edit_contract=1&amb;contract_id=$contract_id"); + return; + } + } else { + if($c->model('Provisioning')->call_prov( $c, 'billing', 'create_sip_peering_contract', + { data => \%settings }, + \$contract_id)) + { + $messages{conmsg} = 'Web.Account.Created'; + $c->session->{messages} = \%messages; + $c->response->redirect("/peering/contract_detail?edit_contract=1&amb;contract_id=$contract_id"); + return; + } + } + } + + $c->session->{messages} = \%messages; + $c->session->{restore_contract_input} = \%settings; + $c->response->redirect("/peering/contract_detail?edit_contract=1&amb;contract_id=$contract_id"); + return; +} + +=head2 terminate + +Terminates a SIP peering contract. + +=cut + +sub terminate_contract : Local { + my ( $self, $c ) = @_; + + my %messages; + + my $contract_id = $c->request->params->{contract_id}; + + if($c->model('Provisioning')->call_prov( $c, 'billing', 'terminate_sip_peering_contract', + { id => $contract_id }, + undef)) + { + $messages{topmsg} = 'Server.Voip.SubscriberDeleted'; + $c->session->{messages} = \%messages; + $c->response->redirect("/peering"); + return; + } + + $c->session->{messages} = \%messages; + $c->response->redirect("/peering/contract_detail?edit_contract=1&amb;contract_id=$contract_id"); + return; +} + +=head2 delete + +Deletes a SIP peering contract. + +=cut + +sub delete_contract : Local { + my ( $self, $c ) = @_; + + my %messages; + + my $contract_id = $c->request->params->{contract_id}; + + if($c->model('Provisioning')->call_prov( $c, 'billing', 'delete_sip_peering_contract', + { id => $contract_id }, + undef)) + { + $messages{topmsg} = 'Server.Voip.SubscriberDeleted'; + $c->session->{messages} = \%messages; + $c->response->redirect("/peering"); + return; + } + + $c->session->{messages} = \%messages; + $c->response->redirect("/peering/contract_detail?edit_contract=1&amb;contract_id=$contract_id"); + return; +} + =head1 BUGS AND LIMITATIONS =over diff --git a/lib/admin/Utils.pm b/lib/admin/Utils.pm index 08e86ae..e6164d2 100644 --- a/lib/admin/Utils.pm +++ b/lib/admin/Utils.pm @@ -62,4 +62,47 @@ sub get_default_slot_list { } +#-# sub short_contact +#-# parameter $c, $contact +#-# return $short_contact +#-# description gets a short representation of a (contract) contact +sub short_contact { + my ($c,$contact) = @_; + + if (defined $contact->{company} and length($contact->{company})) { + return $contact->{company}; + } elsif (defined $contact->{lastname} and length($contact->{lastname})) { + if (defined $contact->{firstname} and length($contact->{firstname})) { + return $contact->{lastname} . ', ' . $contact->{firstname}; + } else { + return $contact->{lastname}; + } + } elsif (defined $contact->{firstname} and length($contact->{firstname})) { + return $contact->{firstname}; + } else { + #die? + return ''; + } + +} + +#-# sub get_contract_contact_form_fields +#-# parameter $c +#-# return \%contract_contact_form_fields +#-# description defines contract contact form fields +sub get_contract_contact_form_fields { + my ($c,$contact) = @_; + + return [ { field => 'firstname', + label => 'Firtst Name', + value => $contact->{firstname} }, + { field => 'lastname', + label => 'Last Name', + value => $contact->{lastname} }, + { field => 'company', + label => 'Company', + value => $contact->{company} }]; + +} + 1; diff --git a/root/tt/contract_detail.tt b/root/tt/contract_detail.tt new file mode 100755 index 0000000..28f6ba3 --- /dev/null +++ b/root/tt/contract_detail.tt @@ -0,0 +1,74 @@ + [% IF contract.id %] +

SIP Peering Contract [% contract.id %]

+ [% ELSE %] +

New SIP Peering Contract

+ [% END %] + + [% UNLESS contract.terminate_timestamp || Catalyst.session.admin.read_only %] +
+ back + [% IF edit_contract %] + [% IF contract.id %] + terminate + delete + [% END %] + [% END %] +
+ [% END %] +
+ + [% IF messages.conmsg %]
[% messages.conmsg %]
[% END %] + [% IF messages.conerr %]
[% messages.conerr %]
[% END %] +
+ + + [% IF contract.id %] + + [% END %] + + [% IF billing_features %] + + + + + [% END %] + + [% FOREACH contact_form_field = contact_form_fields %] + + + + + [% END %] + + [% IF contract.id %] + + + [% IF contract.terminate_timestamp %] + + [% END %] + [% END %] +
ContractID:[% contract.id %]
billing profile: + [% IF edit_contract %] + + [% ELSE %] + [% contract.billing_profile_name %] + [% END %] +
[% contact_form_field.label %]: + [% IF edit_contract %] + + [% ELSE %] + [% contact_form_field.value %] + [% END %] +
created:[% contract.create_timestamp %]
modified:[% contract.modify_timestamp %]
terminated:
[% contract.terminate_timestamp %]
+ [% IF edit_contract %] + + [% END %] +
+
diff --git a/root/tt/peering.tt b/root/tt/peering.tt index b1c4cff..9669dfa 100644 --- a/root/tt/peering.tt +++ b/root/tt/peering.tt @@ -1,9 +1,51 @@ +

SIP Peering Contracts

+ + [% UNLESS Catalyst.session.admin.read_only %] + [% IF Catalyst.config.billing_features %] +
+ create new +
+ [% END %] + [% END %] + +
+ [% IF contracts %] + + + + + + + + [% FOREACH contract = contracts %] + + + + + + [% UNLESS Catalyst.session.admin.read_only %] + [% IF Catalyst.config.billing_features %] + + [% END %] + [% END %] + + [% END %] +
ContractIDBilling ProfileContactcreated +
[% contract.id %][% contract.billing_profile_name%][% contract.short_contact %][% contract.create_timestamp %]edit
+ [% ELSE %] +
+ No sip peering contracts created yet. +
+ [% END %] +
+

SIP Peering Groups

[% IF messages.epeermsg %]
[% messages.epeermsg %]
[% END %] [% IF messages.epeererr %]
[% messages.epeererr %]
[% END %] - + + [% IF peer_groups %] @@ -65,6 +107,11 @@ [% id = id + 1 %] [% END %]
Name
+ [% ELSE %] +
+ No sip peering groups defined yet. +
+ [% END %]
[% UNLESS Catalyst.session.admin.read_only %] @@ -122,3 +169,4 @@ [% END %] +