From 95e5eaf2b0896821b8d85344f66d62cfecc021fe Mon Sep 17 00:00:00 2001 From: Irka Date: Mon, 10 Feb 2014 15:15:21 +0100 Subject: [PATCH] Invoices - a little bit more clean. But still DB is fake, and columns titles are interesting to think about more. --- lib/NGCP/Panel/Controller/Invoice.pm | 153 +++++---------------- share/templates/helpers/datatables_vars.tt | 30 +++- share/templates/invoice/list.tt | 31 +---- 3 files changed, 70 insertions(+), 144 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Invoice.pm b/lib/NGCP/Panel/Controller/Invoice.pm index 09de9e2a3d..e69546da4c 100644 --- a/lib/NGCP/Panel/Controller/Invoice.pm +++ b/lib/NGCP/Panel/Controller/Invoice.pm @@ -1,12 +1,13 @@ -package NGCP::Panel::Controller::Billing; +package NGCP::Panel::Controller::Invoice; use Sipwise::Base; +use Data::Dumper; #use Text::CSV_XS; #use DateTime::Format::ISO8601; BEGIN { extends 'Catalyst::Controller'; } #use NGCP::Panel::Utils::Contract; -#use NGCP::Panel::Utils::Message; +use NGCP::Panel::Utils::Message; #use NGCP::Panel::Utils::Navigation; #use NGCP::Panel::Utils::Datatables; #use NGCP::Panel::Utils::Preferences; @@ -21,14 +22,33 @@ sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRol sub invoice_list :Chained('/') :PathPart('invoice') :CaptureArgs(0) { my ( $self, $c ) = @_; + $c->log->debug("Irkas debug"); my $dispatch_to = '_invoice_resultset_' . $c->user->roles; - my $profiles_rs = $self->$dispatch_to($c); + my $invoice_rs = $self->$dispatch_to($c); + $c->log->debug($dispatch_to); + my $dumpedstr = Data::Dumper::Dumper($invoice_rs); + $c->log->debug("debug string 1"); + print STDERR "STDERR"; + print "STDOUT"; + $c->log->debug( sub { Data::Dumper::Dumper($invoice_rs) } ); + $c->log->debug( { + filter => \&Data::Dumper::Dumper, + value => $invoice_rs, + } ); + $c->log->debug("debug string 2"); + $c->log->debug(Data::Dumper::Dumper($invoice_rs)); + $c->log->debug("debug string 3"); + $c->log->debug($dumpedstr); + $c->log->debug("debug string 4"); $c->stash(invoice_rs => $invoice_rs); + $c->log->debug(Dumper($invoice_rs)); + #To don't keep names, which are view thing, we can put all names in some separated storage, and make it hierarchically, e.g.: name => "Name", name.reseller => "Reseller name", name.reseller.billingprofile => "Name of this billing reseller". + #in some free and good day ) + #as the most often name is just Name - it may save 1-2 letters, but of course we will loose on searching $c->stash->{invoice_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [ - { name => "id", "search" => 1, "title" => $c->loc("#") }, - { name => "name", "search" => 1, "title" => $c->loc("Name") }, - { name => "reseller.name", "search" => 1, "title" => $c->loc("Reseller") }, + { name => "id", "search" => 1, "title" => $c->loc("Internal Id") }, + { name => "serial", "search" => 1, "title" => $c->loc("Peer Id") }, ]); $c->stash(template => 'invoice/list.tt'); @@ -36,14 +56,15 @@ sub invoice_list :Chained('/') :PathPart('invoice') :CaptureArgs(0) { sub _invoice_resultset_admin { my ($self, $c) = @_; - my $rs = $c->model('DB')->resultset('invoice_profiles'); + my $rs = $c->model('DB')->resultset('invoices'); + $c->log->debug(Dumper($rs)); return $rs; } sub _invoice_resultset_reseller { my ($self, $c) = @_; - my $rs = $c->model('DB')->resultset('admins') - ->find($c->user->id)->reseller->invoice_profiles; + my $rs = $c->model('DB')->resultset('invoices'); + $c->log->debug(Dumper($rs)); return $rs; } @@ -54,7 +75,7 @@ sub root :Chained('invoice_list') :PathPart('') :Args(0) { sub ajax :Chained('invoice_list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - my $resultset = $c->stash->{profiles_rs}; + my $resultset = $c->stash->{invoice_rs}; NGCP::Panel::Utils::Datatables::process($c, $resultset, $c->stash->{invoice_dt_columns}); $c->detach( $c->view("JSON") ); @@ -71,119 +92,15 @@ sub base :Chained('invoice_list') :PathPart('') :CaptureArgs(1) { $c->stash->{zone_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [ { name => 'id', search => 1, title => $c->loc('#') }, - { name => 'zone', search => 1, title => $c->loc('Zone') }, - { name => 'detail', search => 1, title => $c->loc('Zone Details') }, - ]); + ]); - my $res = $c->stash->{profiles_rs}->find($invoice_id); + my $res = $c->stash->{invoice_rs}->find($invoice_id); unless(defined($res)) { - $c->flash(messages => [{type => 'error', text => $c->loc('Billing Profile does not exist!')}]); + $c->flash(messages => [{type => 'error', text => $c->loc('Invoice does not exist!')}]); $c->response->redirect($c->uri_for()); return; } - $c->stash(profile => {$res->get_inflated_columns}); - $c->stash(invoice_result => $res); -} - -sub edit :Chained('base') :PathPart('edit') { - my ($self, $c) = @_; - - my $posted = ($c->request->method eq 'POST'); - my $form; - my $params = $c->stash->{profile}; - $params->{reseller}{id} = delete $params->{reseller_id}; - $params = $params->merge($c->session->{created_objects}); - if($c->user->is_superuser) { - $form = NGCP::Panel::Form::BillingProfile::Admin->new; - } else { - $form = NGCP::Panel::Form::BillingProfile::Reseller->new; - } - $form->process( - posted => $posted, - params => $c->request->params, - item => $params, - ); - NGCP::Panel::Utils::Navigation::check_form_buttons( - c => $c, - form => $form, - fields => { - 'reseller.create' => $c->uri_for('/reseller/create'), - }, - back_uri => $c->req->uri, - ); - if($posted && $form->validated) { - try { - if($c->user->is_superuser) { - $form->values->{reseller_id} = $form->values->{reseller}{id}; - } else { - $form->values->{reseller_id} = $c->user->reseller_id; - } - delete $form->values->{reseller}; - my $old_prepaid = $c->stash->{invoice_result}->prepaid; - - my $schema = $c->model('DB'); - $schema->txn_do(sub { - $c->stash->{invoice_result}->update($form->values); - - # if prepaid flag changed, update all subscribers for customers - # who currently have the invoice profile active - my $rs = $schema->resultset('invoice_mappings')->search({ - invoice_invoice_id => $c->stash->{invoice_result}->id, - }); - my $contract_rs = NGCP::Panel::Utils::Contract::get_contract_rs( - schema => $c->model('DB')); - if($old_prepaid && !$c->stash->{invoice_result}->prepaid) { - foreach my $map($rs->all) { - my $contract = $map->contract; - next unless($contract->contact->reseller_id); # skip non-customers - my $chosen_contract = $contract_rs->find({id => $contract->id}); - next unless( defined $chosen_contract && $chosen_contract->get_column('invoice_mapping_id') == $map->id ); # is not current mapping - foreach my $sub($contract->voip_subscribers->all) { - my $prov_sub = $sub->provisioning_voip_subscriber; - next unless($sub->provisioning_voip_subscriber); - my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( - c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); - if($pref->first) { - $pref->first->delete; - } - } - } - } elsif(!$old_prepaid && $c->stash->{invoice_result}->prepaid) { - foreach my $map($rs->all) { - my $contract = $map->contract; - next unless($contract->contact->reseller_id); # skip non-customers - my $chosen_contract = $contract_rs->find({id => $contract->id}); - next unless( defined $chosen_contract && $chosen_contract->get_column('invoice_mapping_id') == $map->id ); # is not current mapping - foreach my $sub($contract->voip_subscribers->all) { - my $prov_sub = $sub->provisioning_voip_subscriber; - next unless($prov_sub); - my $pref = NGCP::Panel::Utils::Preferences::get_usr_preference_rs( - c => $c, attribute => 'prepaid', prov_subscriber => $prov_sub); - if($pref->first) { - $pref->first->update({ value => 1 }); - } else { - $pref->create({ value => 1 }); - } - } - } - } - - }); - - delete $c->session->{created_objects}->{reseller}; - $c->flash(messages => [{type => 'success', text => $c->loc('Billing profile successfully updated')}]); - } catch($e) { - NGCP::Panel::Utils::Message->error( - c => $c, - error => $e, - desc => $c->loc("Failed to update invoice profile."), - ); - } - NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/invoice')); - } - - $c->stash(edit_flag => 1); - $c->stash(form => $form); + $c->stash(invoice_result => $res); } sub create :Chained('invoice_list') :PathPart('create') :Args(0) { diff --git a/share/templates/helpers/datatables_vars.tt b/share/templates/helpers/datatables_vars.tt index 3481ec890d..c09388138e 100644 --- a/share/templates/helpers/datatables_vars.tt +++ b/share/templates/helpers/datatables_vars.tt @@ -13,8 +13,6 @@ initHelper('close_target'); initHelper('create_flag'); initHelper('form_object',form); initHelper('ajax_uri', c.uri_for( c.controller.action_for('ajax') )); - -#here we can generate couple of variants, selection of proper variant will be exact view responsibility UNLESS c.user.read_only; initHelper('dt_buttons',[ { name = c.loc('Edit'), uri = "/${helper.identifier}'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, @@ -23,5 +21,33 @@ UNLESS c.user.read_only; { name = c.loc("Create ${helper.name_single}"), uri = c.uri_for("/${helper.identifier}/create"), icon = 'icon-star' }, ] ); END; +#someIndex='blahblahblah'; +#initHelper('someIndex'); +#Dumper.dump_html(helper); +#UNLESS helper.messages.defined; helper.messages = messages; END; +#UNLESS helper.length_change.defined; helper.length_change = 1; END; +#UNLESS helper.dt_columns.defined; helper.dt_columns = ${"${helper.identifier}_dt_columns"} || dt_columns; END; +#UNLESS helper.close_target.defined; helper.close_target = close_target; END; +#UNLESS helper.create_flag.defined; helper.create_flag = create_flag; END; + +# DEFAULT +# helper.messages = messages +# helper.length_change = 1 +# helper.dt_columns = ${"${helper.identifier}_dt_columns"} || dt_columns +# helper.close_target = close_target +# helper.create_flag = create_flag +# helper.edit_flag = edit_flag +# helper.form_object = form +# helper.ajax_uri = c.uri_for( c.controller.action_for('ajax') ); +# UNLESS c.user.read_only; + # DEFAULT + # helper.dt_buttons = [ + # { name = c.loc('Edit'), uri = "/${helper.identifier}'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, + # ] + # helper.top_buttons = [ + # { name = c.loc("Create ${helper.name_single}"), uri = c.uri_for("/${helper.identifier}/create"), icon = 'icon-star' }, + # ] + # ; +# END; -%] \ No newline at end of file diff --git a/share/templates/invoice/list.tt b/share/templates/invoice/list.tt index 59b49f7dbe..a7910d8a1b 100644 --- a/share/templates/invoice/list.tt +++ b/share/templates/invoice/list.tt @@ -1,28 +1,11 @@ -[% site_config.title = c.loc('Billing Profiles') -%] +[% site_config.title = c.loc('Invoices') -%] [% - helper.name = c.loc('Billing Profile'); - helper.identifier = 'billing_profile'; - helper.messages = messages; - helper.dt_columns = profile_dt_columns; - helper.length_change = 1; - - helper.close_target = close_target; - helper.create_flag = create_flag; - helper.edit_flag = edit_flag; - helper.form_object = form; - helper.ajax_uri = c.uri_for( c.controller.action_for('ajax') ); - - UNLESS c.user.read_only; - helper.dt_buttons = [ - { name = c.loc('Edit'), uri = "/billing/'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, - { name = c.loc('Fees'), uri = "/billing/'+full[\"id\"]+'/fees", class = 'btn-small btn-tertiary', icon = 'icon-shopping-cart' }, - { name = c.loc('Peaktimes'), uri = "/billing/'+full[\"id\"]+'/peaktimes", class = 'btn-small btn-tertiary', icon = 'icon-time' }, - ]; - helper.top_buttons = [ - { name = c.loc('Create Billing Profile'), uri = c.uri_for('/billing/create'), icon = 'icon-star' }, - ]; - END; - + helper.name = c.loc('Invoices'); + helper.name_single = c.loc('Invoice'); + helper.identifier = 'invoice'; + helper.top_buttons = ""; + + PROCESS 'helpers/datatables_vars.tt'; PROCESS 'helpers/datatables.tt'; -%] [% # vim: set tabstop=4 syntax=html expandtab: -%]