Fix contract and peering creation.

agranig/1_0_subfix
Andreas Granig 13 years ago
parent 04296de4c4
commit 9350d4258e

@ -27,9 +27,6 @@ sub contract_list :Chained('/') :PathPart('contract') :CaptureArgs(0) {
]);
my $mapping_rs = $c->model('DB')->resultset('billing_mappings');
# TODO: also order by end_date desc (also in rate-o-mat etc?)
# leave it as is currently to preserve backwards-compatibility
my $rs = $c->model('DB')->resultset('contracts')
->search({
'billing_mappings.id' => {
@ -44,19 +41,22 @@ sub contract_list :Chained('/') :PathPart('contract') :CaptureArgs(0) {
{ -is => undef },
],
},{
alias => 'sub_query',
alias => 'bilmap',
rows => 1,
order_by => {-desc => ['start_date', 'id']},
order_by => {-desc => ['bilmap.start_date', 'bilmap.id']},
})->get_column('id')->as_query,
},
},{
'join' => 'billing_mappings',
'+select' => [
'billing_mappings.id',
'billing_mappings.start_date',
],
'+as' => [
'billing_mapping_id',
'billing_mapping_start_date',
],
alias => 'me',
});
unless($c->user->is_superuser) {
@ -81,7 +81,7 @@ sub create :Chained('contract_list') :PathPart('create') :Args(0) {
my $posted = ($c->request->method eq 'POST');
my $params = {};
Hash::Merge->new('RIGHT_PRECEDENT')->merge($params, delete $c->session->{created_object});
$params = Hash::Merge->new('RIGHT_PRECEDENT')->merge($params, delete $c->session->{created_object});
# TODO: where to store created contact and billing profile?
my $form;
$form = NGCP::Panel::Form::Contract->new;
@ -271,53 +271,66 @@ sub peering_ajax :Chained('peering_list') :PathPart('ajax') :Args(0) {
}, {
'join' => {'billing_mappings' => 'product'},
});
$c->forward( "/ajax_process_resultset", [$rs,
["id","contact_id","billing_profile_id", "billing_profile_name","status"],
["billing_profile.name", "status"]]);
NGCP::Panel::Utils::Datatables::process($c, $rs, $c->stash->{contract_dt_columns});
$c->detach( $c->view("JSON") );
}
sub peering_create :Chained('peering_list') :PathPart('create') :Args(0) {
my ($self, $c) = @_;
my $item = $c->model('DB')->resultset('billing_mappings')->new_result({});
$item->product(
$c->model('DB')->resultset('products')->find({class => 'sippeering'})
);
my $posted = ($c->request->method eq 'POST');
my $params = {};
$params = Hash::Merge->new('RIGHT_PRECEDENT')->merge($params, delete $c->session->{created_object});
# TODO: where to store created contact and billing profile?
say ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ";
use Data::Printer; p $params;
my $form = NGCP::Panel::Form::Contract->new;
if($form->process(
posted => ($c->request->method eq 'POST'),
$form->process(
posted => $posted,
params => $c->request->params,
item => $item,
)) {
# insert ok, populate contract_balance table
try {
NGCP::Panel::Utils::Contract::create_contract_balance(
c => $c,
profile => $item->billing_profile,
contract => $item->contract,
);
} catch($e) {
# TODO: roll back contract and billing_mappings creation and
# redirect to correct entry point
$c->log->error($e);
$c->flash(messages => [{type => 'error', text => 'Failed to create contract balance!'}]);
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/contract/root'));
}
}
item => $params
);
NGCP::Panel::Utils::Navigation::check_form_buttons(
c => $c, form => $form,
fields => {'contract.contact.create' => $c->uri_for('/contact/create'),
c => $c,
form => $form,
fields => {'contact.create' => $c->uri_for('/contact/create'),
'billing_profile.create' => $c->uri_for('/billing/create')},
back_uri => $c->req->uri,
);
if($form->validated) {
$c->flash(messages => [{type => 'success', text => 'Contract successfully created!'}]);
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/contract/root'));
}
if($posted && $form->validated) {
try {
my $schema = $c->model('DB');
$schema->txn_do(sub {
$form->params->{contact_id} = $form->params->{contact}{id};
delete $form->params->{contract};
my $bprof_id = $form->params->{billing_profile}{id};
delete $form->params->{billing_profile};
my $contract = $schema->resultset('contracts')->create($form->params);
my $billing_profile = $schema->resultset('billing_profiles')->find($bprof_id);
my $product = $schema->resultset('products')->find({ class => 'sippeering' });
$contract->billing_mappings->create({
billing_profile_id => $bprof_id,
product_id => $product->id,
});
NGCP::Panel::Utils::Contract::create_contract_balance(
c => $c,
profile => $billing_profile,
contract => $contract,
);
$c->session->{created_object} = { contract => { id => $contract->id }};
say ">>>>>>>>>>>>>>>>>> setting created contract object:";
use Data::Printer; p $c->session->{created_object};
$c->flash(messages => [{type => 'success', text => 'Contract successfully created'}]);
});
} catch($e) {
$c->log->error("Failed to create contract: $e");
$c->flash(messages => [{type => 'error', text => 'Failed to create contract'}]);
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/contract/root'));
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for_action('/contract/root'));
}
$c->stash(create_flag => 1);
$c->stash(form => $form);

@ -20,6 +20,14 @@ sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) {
sub group_list :Chained('/') :PathPart('peering') :CaptureArgs(0) {
my ( $self, $c ) = @_;
$c->stash->{peering_group_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [
{ name => 'id', search => 1, title => '#' },
{ name => 'contract.contact.email', search => 1, title => 'Contact Email' },
{ name => 'name', search => 1, title => 'Name' },
{ name => 'priority', search => 1, title => 'Priority' },
{ name => 'description', search => 1, title => 'Description' },
]);
$c->stash(template => 'peering/list.tt');
}
@ -32,10 +40,7 @@ sub ajax :Chained('group_list') :PathPart('ajax') :Args(0) {
my ($self, $c) = @_;
my $resultset = $c->model('DB')->resultset('voip_peer_groups');
$c->forward( "/ajax_process_resultset", [$resultset,
["id", "name", "priority", "description", "peering_contract_id"],
["name", "priority", "description", "peering_contract_id"]]);
NGCP::Panel::Utils::Datatables::process($c, $resultset, $c->stash->{peering_group_dt_columns});
$c->detach( $c->view("JSON") );
}
@ -44,7 +49,7 @@ sub base :Chained('group_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $group_id) = @_;
unless($group_id && $group_id->is_integer) {
$c->flash(messages => [{type => 'error', text => 'Invalid group id detected!'}]);
$c->flash(messages => [{type => 'error', text => 'Invalid group id detected'}]);
$c->response->redirect($c->uri_for());
$c->detach;
return;
@ -52,7 +57,7 @@ sub base :Chained('group_list') :PathPart('') :CaptureArgs(1) {
my $res = $c->model('DB')->resultset('voip_peer_groups')->find($group_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => 'Peering Group does not exist!'}]);
$c->flash(messages => [{type => 'error', text => 'Peering group does not exist'}]);
$c->response->redirect($c->uri_for());
$c->detach;
return;
@ -111,18 +116,25 @@ sub delete :Chained('base') :PathPart('delete') {
sub create :Chained('group_list') :PathPart('create') :Args(0) {
my ($self, $c) = @_;
my $posted = ($c->request->method eq 'POST');
my $form = NGCP::Panel::Form::PeeringGroup->new;
my $params = {};
say ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> created objects in peering/create is:";
use Data::Printer; p $c->session->{created_object};
$params = Hash::Merge->new('RIGHT_PRECEDENT')->merge($params, delete $c->session->{created_object});
say ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> got new contract:";
use Data::Printer; p $params;
$form->process(
posted => ($c->request->method eq 'POST'),
posted => $posted,
params => $c->request->params,
action => $c->uri_for('create'),
item => $params,
);
NGCP::Panel::Utils::Navigation::check_form_buttons(
c => $c, form => $form,
fields => {'contract.create' => $c->uri_for('/contract/peering/create')},
back_uri => $c->req->uri,
);
if($form->validated) {
if($posted && $form->validated) {
my $formdata = $form->custom_get_values;
try {
$c->model('DB')->resultset('voip_peer_groups')->create(

@ -10,8 +10,8 @@ has_field 'id' => (
required => 1,
template => 'share/templates/helpers/datatables_field.tt',
ajax_src => '/contract/peering/ajax',
table_titles => ['#', 'Contact #', 'Billing Profile #', 'Status'],
table_fields => ['id', 'contact_id', 'billing_profile_id', 'status'],
table_titles => ['#', 'Status', 'Billing Profile'],
table_fields => ['id', 'status', 'billing_mappings_billing_profile_name'],
);
has_field 'create' => (

@ -16,6 +16,13 @@ has_field 'id' => (
type => 'Hidden'
);
has_field 'contract' => (
type => '+NGCP::Panel::Field::Contract',
label => 'Contract',
not_nullable => 1,
);
has_field 'name' => (
type => 'Text',
required => 1,
@ -31,12 +38,6 @@ has_field 'description' => (
type => 'Text',
);
has_field 'contract' => (
type => '+NGCP::Panel::Field::Contract',
label => 'Contract',
not_nullable => 1,
);
has_field 'save' => (
type => 'Submit',
value => 'Save',

@ -2,8 +2,7 @@
[%
helper.name = 'SIP Peering Groups';
helper.messages = messages;
helper.column_titles = [ '#', 'Name', 'Priority', 'Description', 'Contract #' ];
helper.column_fields = [ 'id', 'name', 'priority', 'description', 'peering_contract_id' ];
helper.dt_columns = peering_group_dt_columns;
helper.close_target = close_target;
helper.create_flag = create_flag;
@ -11,14 +10,20 @@
helper.form_object = form;
helper.ajax_uri = c.uri_for_action( "/peering/ajax" );
helper.dt_buttons = [
{ name = 'Edit', uri = "/peering/'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' },
{ name = 'Delete', uri = "/peering/'+full[\"id\"]+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' },
{ name = 'Details', uri = "/peering/'+full[\"id\"]+'/servers", class = 'btn-small btn-tertiary', icon = 'icon-list' },
];
helper.top_buttons = [
{ name = 'Create Peering Group', uri = c.uri_for('/peering/create'), icon = 'icon-star' },
];
UNLESS c.user.read_only;
helper.dt_buttons = [
{ name = 'Edit', uri = "/peering/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' },
{ name = 'Delete', uri = "/peering/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' },
{ name = 'Details', uri = "/peering/'+full.id+'/servers", class = 'btn-small btn-tertiary', icon = 'icon-list' },
];
helper.top_buttons = [
{ name = 'Create Peering Group', uri = c.uri_for('/peering/create'), icon = 'icon-star' },
];
ELSE;
helper.dt_buttons = [
{ name = 'Details', uri = "/peering/'+full.id+'/servers", class = 'btn-small btn-tertiary', icon = 'icon-list' },
];
END;
PROCESS 'helpers/datatables.tt';
-%]

Loading…
Cancel
Save