From 631a069a0201827e0bb9a00d6e495edc7f8d4e88 Mon Sep 17 00:00:00 2001 From: Irka Date: Fri, 21 Feb 2014 15:44:34 +0100 Subject: [PATCH] MT#5879 Very basic logic without storing to db. But seems as will be able to make it generic against template types. --- lib/NGCP/Panel/Controller/Customer.pm | 74 ++++++++++++- lib/NGCP/Panel/Utils/Contract.pm | 6 +- lib/NGCP/Panel/Utils/InvoiceTemplate.pm | 17 +++ lib/NGCP/Panel/View/SVG.pm | 26 ++++- share/static/js/background.js | 57 ++++++++++ share/static/js/libs/svg-edit/svg-editor.js | 2 +- share/templates/customer/calls.tt | 69 ++---------- share/templates/customer/calls_svg.tt | 117 +++++++++++++------- 8 files changed, 258 insertions(+), 110 deletions(-) create mode 100644 lib/NGCP/Panel/Utils/InvoiceTemplate.pm create mode 100644 share/static/js/background.js diff --git a/lib/NGCP/Panel/Controller/Customer.pm b/lib/NGCP/Panel/Controller/Customer.pm index e77bfd888a..a46d58c068 100644 --- a/lib/NGCP/Panel/Controller/Customer.pm +++ b/lib/NGCP/Panel/Controller/Customer.pm @@ -19,6 +19,7 @@ use NGCP::Panel::Utils::Navigation; use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::Subscriber; use NGCP::Panel::Utils::Sounds; +use NGCP::Panel::Utils::InvoiceTemplate; use Template; =head1 NAME @@ -726,14 +727,79 @@ sub calls :Chained('base') :PathPart('calls') :Args(0) { #$c->stash(template => 'customer/calls.tt'); # $c->stash(contract => $contract_rs->first); } -sub calls_svg :Chained('base') :PathPart('calls/svg') :Args(0) { - my ($self, $c) = @_; +sub loglong{ + my ($str) = @_; + use Data::Dumper; + open(my $log,">>/tmp/irka.log"); + print $log Dumper($str); + close $log; +} +sub calls_svg :Chained('base') :PathPart('calls/template') :Args { + my ($self, $c, $tt_type, $tt_viewmode ) = @_; + #die(); #$c->view('SVG'); + #handle request + $tt_viewmode //= ''; + my $invoicetemplate = $c->request->body_parameters->{template} || ''; + $c->log->debug("1.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^/is ).";\n"); + + if(!$invoicetemplate){ + #getCustomerActiveInvoiceTemplateFromDB. + } + if(!$invoicetemplate){ + #getDefault + try{ + NGCP::Panel::Utils::InvoiceTemplate::getDefault( c => $c, invoicetemplate => \$invoicetemplate ); + } catch($e) { + NGCP::Panel::Utils::Message->error( + c => $c, + error => 'default invoice template error', + desc => $c->loc('There is no one invoice template in the system.'), + ); + } + }#else{ + #here we have invoice content - of customer or default, so no checking of invoicetemplate is necessary + #if($invoicetemplate){ + #sub candidate, preSaveCustomTemplate + #if it is presaving, making it for default isn't necessary, default should contain it + #but while let it be here + $c->log->debug("3.invoicetemplate is empty=".($invoicetemplate?0:1).";viewbox=".($invoicetemplate !~/^/is).";\n"); + if( $invoicetemplate !~/^/is ){ + (my ($width)) = ($invoicetemplate =~/^/is ); + $c->log->debug("width=$width;\n"); + (my ($height)) = ($invoicetemplate =~/^/is ); + my($replaced) = $invoicetemplate =~s/^log->debug("replaced=$replaced;\n"); + #storeToDB + } + #} + + #prepare response $c->response->content_type('image/svg+xml'); - $c->stash(template => 'customer/calls_svg.tt'); - $c->detach($c->view('SVG')); + $c->log->debug("tt_viewmode=$tt_viewmode;\n"); + if($tt_viewmode eq 'raw'){ + $c->stash->{VIEW_NO_TT_PROCESS} = 1; + $c->response->body($invoicetemplate); + return; + }else{ + my $contacts = $c->model('DB')->resultset('contacts')->search({ id => $c->stash->{contract}->id }); + #some preprocessing should be done only before showing. So, there will be: + #preSaveCustomTemplate prerpocessing + #preShowCustomTemplate prerpocessing + { + #preShowInvoice + $invoicetemplate =~s/{?}?//g; + } + + $c->stash( provider => $contacts->first ); + use irka; + irka::loglong(\$invoicetemplate); + $c->stash( template => \$invoicetemplate ); + $c->log->debug("before_detach;\n"); + $c->detach($c->view('SVG')); + } } sub pbx_group_ajax :Chained('base') :PathPart('pbx/group/ajax') :Args(0) { diff --git a/lib/NGCP/Panel/Utils/Contract.pm b/lib/NGCP/Panel/Utils/Contract.pm index 818052a0d3..2c8e9e63ee 100644 --- a/lib/NGCP/Panel/Utils/Contract.pm +++ b/lib/NGCP/Panel/Utils/Contract.pm @@ -275,11 +275,11 @@ sub get_contract_calls_rs{ { sum => 'me.source_customer_free_time', -as => 'free_time', } , { sum => 'me.duration', -as => 'duration', } , { count => '*', -as => 'number', } , - 'billing_zones_history.zone', + 'source_customer_billing_zones_history.zone', ], 'as' => [qw/cost free_time duration number zone/], - join => 'billing_zones_history', - group_by => 'billing_zones_history.zone', + join => 'source_customer_billing_zones_history', + group_by => 'source_customer_billing_zones_history.zone', } ); return $zonecalls_rs; diff --git a/lib/NGCP/Panel/Utils/InvoiceTemplate.pm b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm new file mode 100644 index 0000000000..ad10adaf63 --- /dev/null +++ b/lib/NGCP/Panel/Utils/InvoiceTemplate.pm @@ -0,0 +1,17 @@ +package NGCP::Panel::Utils::InvoiceTemplate; +use strict; +use warnings; + +use Sipwise::Base; +use DBIx::Class::Exception; +use NGCP::Panel::Utils::DateTime; + +sub getDefault{ + my %params = @_; + + my $c = $params{c}; + #in future kay be we will store it in Db, but now it is convenient to edit template as file + return ${$params{invoicetemplate}} = $c->view('SVG')->getTemplateContent($c, 'customer/calls_svg.tt'); +} + +1; \ No newline at end of file diff --git a/lib/NGCP/Panel/View/SVG.pm b/lib/NGCP/Panel/View/SVG.pm index da02cc512d..644b5f3c26 100644 --- a/lib/NGCP/Panel/View/SVG.pm +++ b/lib/NGCP/Panel/View/SVG.pm @@ -21,8 +21,32 @@ sub process { my ( $self, $c ) = @_; $c->res->content_type("image/svg+xml"); - $self->SUPER::process($c); + + + if($c->stash->{VIEW_NO_TT_PROCESS}) { + $c->log->debug("VIEW_NO_TT_PROCESS=".$c->stash->{VIEW_NO_TT_PROCESS}.";\n"); + + my $output = $self->getTemplateContent($c); + + $c->log->debug("output is empty=".($output?0:1).";\n"); + + $c->response->body($output); + #$self->{template}->{LOAD_TEMPLATES}->load(); + } else{ + $c->log->debug("VIEW INVOICE TEMPLATE:just send to process;\n"); + $self->SUPER::process($c) ; + } return 1; } +sub getTemplateContent{ + my ( $self, $c, $template ) = @_; + if(defined $template){ + $c->log->debug("getTemplateContent: template=$template;"); + } + $template ||= ( $c->stash->{template} || $c->action . $self->config->{TEMPLATE_EXTENSION} ); + $c->log->debug("getTemplateContent: template=$template;"); + + return $self->{template}->context->insert($template); +} 1; \ No newline at end of file diff --git a/share/static/js/background.js b/share/static/js/background.js new file mode 100644 index 0000000000..fbf15f7a08 --- /dev/null +++ b/share/static/js/background.js @@ -0,0 +1,57 @@ +// Background fetches a page into the specified element +function fetch_into(div, uri, q, callback) { + var xmlHttpReq = false; +// alert(q); +// Mozilla/Safari + if (window.XMLHttpRequest) { + xmlHttpReq = new XMLHttpRequest(); + if (typeof xmlHttpReq.overrideMimeType != 'undefined') { + xmlHttpReq.overrideMimeType('text/xml'); + } + } +// IE + else if (window.ActiveXObject) { + xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); + } + //var fd = new FormData(); + xmlHttpReq.open('POST', uri, true); +// xmlHttpReq.open('GET', uri, true); + xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); + xmlHttpReq.onreadystatechange = function() { + if (xmlHttpReq.readyState == 4) { + document.getElementById(div).innerHTML=xmlHttpReq.responseText; + if(typeof callback == 'function'){ + callback.call(); + }else{ + eval(callback); + } + } + } +// alert(q); + xmlHttpReq.send(q); +} +function background(uri,q,callback) { + var xmlHttpReq = false; + if (window.XMLHttpRequest) { + xmlHttpReq = new XMLHttpRequest(); +// xmlHttpReq.overrideMimeType('text/xml'); + } else if (window.ActiveXObject) { + xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); + } + + xmlHttpReq.open('POST', uri, true); + xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); + if(callback) + xmlHttpReq.onreadystatechange = function(){ + if (xmlHttpReq.readyState == 4) { + if(typeof callback == 'function'){ +// alert(xmlHttpReq.responseText); + callback(xmlHttpReq.responseText); + }else{ + eval(callback); + } + } + } + xmlHttpReq.send(q); +} + diff --git a/share/static/js/libs/svg-edit/svg-editor.js b/share/static/js/libs/svg-edit/svg-editor.js index 91ca89c9c9..c3931aa0af 100644 --- a/share/static/js/libs/svg-edit/svg-editor.js +++ b/share/static/js/libs/svg-edit/svg-editor.js @@ -356,7 +356,7 @@ TO-DOS } }); editor.curConfig = curConfig; // Update exported value - alert('359:unit='+editor.curConfig.baseUnit); + //alert('359:unit='+editor.curConfig.baseUnit); }; /** diff --git a/share/templates/customer/calls.tt b/share/templates/customer/calls.tt index 44ba397399..abe543f2f9 100644 --- a/share/templates/customer/calls.tt +++ b/share/templates/customer/calls.tt @@ -66,6 +66,7 @@ [%END%] + - +
-