From 2d334de084e3772c87f5d9aec2f667c02476b98c Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Fri, 28 Mar 2014 15:25:47 +0100 Subject: [PATCH] MT#5879 Apply newly created id to worm to continue work with cretated template. --- lib/NGCP/Panel/Controller/Customer.pm | 31 +++++++++++++------ lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm | 30 +++++++++++++----- share/templates/customer/invoice.tt | 15 +++++++-- .../customer/invoice_template_svg.tt | 2 +- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index 6c4ccf2bc5..4c9ed456fe 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -897,7 +897,7 @@ sub invoice_template_list :Chained('base') :PathPart('') :Args(0) { $c->log->debug('invoice_template_list'); $c->stash( template => 'customer/invoice_template_list.tt' ); $c->forward( 'invoice_template_list_data' ); - $c->detach($c->view('SVG'));#just no wrapper - aybe there is some other way? + $c->detach($c->view('SVG'));#just no wrapper - maybe there is some other way? } sub invoice :Chained('invoice_template_list_data') :PathPart('') :Args(0) { @@ -960,6 +960,17 @@ sub invoice_template :Chained('invoice_data') :PathPart('template') :Args { #here we also may be better should contact model, not DB directly. Will return to this separation later #at the end - we can figure out rather basic controller behaviour ($out->{tt_id},undef,$out->{tt_data}) = $backend->getCustomerInvoiceTemplate( %$in, result => \$tt_string_customer ); + + $out->{json} = { + tt_data => { + tt_id => $out->{tt_data}->get_column('id'), + }, + }; + foreach(qw/name is_active/){ + $out->{json}->{tt_data}->{$_} = $out->{tt_data}->get_column($_); + } + + } #we need to get default to 1) sanitize (if in->tt_string) or 2)if not in->tt_string and no customer->tt_string @@ -991,11 +1002,18 @@ sub invoice_template :Chained('invoice_data') :PathPart('template') :Args { } #/sanitize - to sub, later - $backend->storeCustomerInvoiceTemplate( + my($tt_stored) = $backend->storeCustomerInvoiceTemplate( %$in, tt_string_sanitized => \$tt_string_sanitized, ); + $out->{json} = { + tt_data => { + tt_id => $tt_stored->{tt_id}, + }, + }; + + $out->{tt_string} = $tt_string_sanitized; }elsif(!$tt_string_customer || $tt_string_force_default){ $out->{tt_string} = $tt_string_default; @@ -1057,13 +1075,8 @@ sub invoice_template :Chained('invoice_data') :PathPart('template') :Args { }, }; #can be empty if we just load default - if($out->{tt_data}){ - $aaData->{form} = { - tt_id => $out->{tt_data}->get_column('id'), - }; - foreach(qw/name is_active/){ - $aaData->{form}->{$_} = $out->{tt_data}->get_column($_); - } + if($out->{json} && $out->{json}->{tt_data}){ + $aaData->{form} = $out->{json}->{tt_data}; }else{ #if we didn't have tt_data - then we have empty form fields with applied defaults $aaData->{form} = $in; diff --git a/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm index e58cb46008..2823e8a031 100644 --- a/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm +++ b/lib/NGCP/Panel/Model/DB/InvoiceTemplate.pm @@ -7,11 +7,10 @@ use Data::Dumper; sub getDefaultConditions{ my $self = shift; my ($params) = @_; - irka::loglong(Dumper($params)); + #irka::loglong(Dumper($params)); my ($contract_id,$tt_sourcestate,$tt_type,$tt_id) = @$params{qw/contract_id tt_sourcestate tt_type tt_id/}; my $conditions = {}; irka::loglong("getDefaultConditions: tt_id=$tt_id;\n"); - #my $tt_record = $self->resultset('invoice_template')->search({ if($tt_id){ $conditions = { id => $tt_id }; }else{ @@ -29,7 +28,7 @@ sub getCustomerInvoiceTemplate{ my ($contract_id,$tt_sourcestate,$tt_type,$tt_id) = @params{qw/contract_id tt_sourcestate tt_type tt_id/}; irka::loglong("getCustomerInvoiceTemplate: tt_id=$tt_id;\n"); - irka::loglong(Dumper(\%params)); + #irka::loglong(Dumper(\%params)); my $result = ''; my $conditions = $self->getDefaultConditions(\%params); @@ -73,25 +72,40 @@ sub storeCustomerInvoiceTemplate{ # })->update_all({ # is_active => 0, # }); - + #I think that SQl + DBI are much more flexible and compact + my $tt_record_created; + my $tt_record_updated; if( !$tt_id ){ - $self->schema->resultset('invoice_template')->create({ + $tt_record_created = $self->schema->resultset('invoice_template')->create({ reseller_id => $contract_id, type => $tt_type, - is_active => 1, + is_active => $is_active, name => $name, 'base64_'.$tt_sourcestate => $$tt_string, }); + if($tt_record_created){ + $tt_id = $tt_record_created->id(); + } }else{ my $conditions = $self->getDefaultConditions(\%params); - my $tt_record = $self->schema->resultset('invoice_template')->search($conditions); - $tt_record->update({ + $tt_record_updated = $self->schema->resultset('invoice_template')->search($conditions); + $tt_record_updated->update({ is_active => $is_active, name => $name, 'base64_'.$tt_sourcestate => $$tt_string, }); } + if($is_active && $tt_id){ + $self->schema->resultset('invoice_template')->search({ + reseller_id => $contract_id, + type => $tt_type, + id => {'!=' => $tt_id }, + })->update_all({ + is_active => 0, + }); + } }); + return { tt_id => $tt_id }; } sub getCustomerInvoiceTemplateList{ my $self = shift; diff --git a/share/templates/customer/invoice.tt b/share/templates/customer/invoice.tt index 0700862bf5..0208623b00 100644 --- a/share/templates/customer/invoice.tt +++ b/share/templates/customer/invoice.tt @@ -159,10 +159,21 @@ function savePreviewedAndShowParsed( data ){ function saveTemplate( data ) { var svgString = getSvgString(); data.tt_sourcestate='saved'; + data.tt_output_type = 'json'; var q = uriForAction( data, 'invoice_template_saved' ); //alert('saveTemplate: q='+q+';'); - $.post( q, { template: svgString }) - .done( function( httpResponse ) { + url: q, + datatype: "json", + + $.ajax( + type: "POST", + url: q, + dataType: 'json', + data: { template: svgString }, + ).done( function( jsonResponse ) { + if(jsonResponse.aaData && jsonResponse.aaData.form){ + $('form[name=invoice_template]').loadJSON(jsonResponse.aaData.form); + } refreshTemplateList( data.contract_id ); }); } diff --git a/share/templates/customer/invoice_template_svg.tt b/share/templates/customer/invoice_template_svg.tt index 20a729178e..b8d542ac08 100644 --- a/share/templates/customer/invoice_template_svg.tt +++ b/share/templates/customer/invoice_template_svg.tt @@ -24,7 +24,7 @@ -