From: jeff Date: Mon, 24 Aug 2009 05:09:27 +0000 (+0000) Subject: add rated call sales report and option to sales report to count usage separately... X-Git-Tag: root_of_svc_elec_features~892 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=b53214c810513eda313fb7aea5405fb02a3b94c5 add rated call sales report and option to sales report to count usage separately from recurring #5588 --- diff --git a/FS/FS/Report/Table/Monthly.pm b/FS/FS/Report/Table/Monthly.pm index d015f9116..845ab150b 100644 --- a/FS/FS/Report/Table/Monthly.pm +++ b/FS/FS/Report/Table/Monthly.pm @@ -331,7 +331,9 @@ sub cust_bill_pkg { $agentnum ||= $opt{'agentnum'}; - $self->scalar_sql(" + my $usage = cust_bill_pkg_detail(@_); + + my $total = $self->scalar_sql(" SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur) FROM cust_bill_pkg LEFT JOIN cust_bill USING ( invnum ) @@ -344,6 +346,64 @@ sub cust_bill_pkg { AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum) ); + if ($opt{use_usage} && $opt{use_usage} eq 'recurring') { + return $total-$usage; + } elsif ($opt{use_usage} && $opt{use_usage} eq 'usage') { + return $usage; + } else { + return $total; + } +} + +sub cust_bill_pkg_detail { + my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_; + + my @where = ( "cust_bill_pkg.pkgnum != 0" ); + my $comparison = ''; + if ( $opt{'classnum'} =~ /^(\d+)$/ ) { + if ( $1 == 0 ) { + $comparison = "IS NULL"; + } else { + $comparison = "= $1"; + } + + if ( $opt{'use_override'} ) { + push @where, "( + part_pkg.classnum $comparison AND pkgpart_override IS NULL OR + override.classnum $comparison AND pkgpart_override IS NOT NULL + )"; + } else { + push @where, "part_pkg.classnum $comparison"; + } + } + + if ( $opt{'usageclass'} =~ /^(\d+)$/ ) { + if ( $1 == 0 ) { + $comparison = "IS NULL"; + } else { + $comparison = "= $1"; + } + + push @where, "cust_bill_pkg_detail.classnum $comparison"; + } + + $agentnum ||= $opt{'agentnum'}; + + my $where = join( ' AND ', @where ); + + $self->scalar_sql(" + SELECT SUM(amount) + FROM cust_bill_pkg_detail + LEFT JOIN cust_bill_pkg USING ( billpkgnum ) + LEFT JOIN cust_bill ON cust_bill_pkg.invnum = cust_bill.invnum + LEFT JOIN cust_main USING ( custnum ) + LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum + LEFT JOIN part_pkg USING ( pkgpart ) + LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart + WHERE $where + AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum) + ); + } sub setup_pkg { shift->pkg_field( @_, 'setup' ); } diff --git a/httemplate/elements/menu.html b/httemplate/elements/menu.html index 6b256c8d8..464b3b354 100644 --- a/httemplate/elements/menu.html +++ b/httemplate/elements/menu.html @@ -234,6 +234,7 @@ if($curuser->access_right('Financial reports')) { %report_financial = ( 'Sales, Credits and Receipts' => [ $fsurl.'graph/report_money_time.html', 'Sales, credits and receipts summary graph' ], 'Sales Report' => [ $fsurl.'graph/report_cust_bill_pkg.html', 'Sales report and graph (by agent, package class and/or date range)' ], + 'Rated Call Sales Report' => [ $fsurl.'graph/report_cust_bill_pkg_detail.html', 'Sales report and graph (by agent, package class, usage class and/or date range)' ], 'Credit Report' => [ $fsurl.'search/report_cust_credit.html', 'Credit report (by employee and/or date range)' ], ); $report_financial{'A/R Aging'} = [ $fsurl.'search/report_receivables.html', 'Accounts Receivable Aging report' ]; diff --git a/httemplate/graph/cust_bill_pkg.cgi b/httemplate/graph/cust_bill_pkg.cgi index 021189abb..832660ffd 100644 --- a/httemplate/graph/cust_bill_pkg.cgi +++ b/httemplate/graph/cust_bill_pkg.cgi @@ -71,6 +71,9 @@ if ( $cgi->param('classnum') =~ /^(\d*)$/ ) { my $use_override = 0; $use_override = 1 if ( $cgi->param('use_override') ); +my $use_usage = 0; +$use_usage = 1 if ( $cgi->param('use_usage') ); + my $hue = 0; #my $hue_increment = 170; #my $hue_increment = 145; @@ -92,35 +95,40 @@ foreach my $agent ( $sel_agent || qsearch('agent', { 'disabled' => '' } ) ) { my @onetime_colors = (); ### fixup the color handling for package classes... + ### and usage my $n = 0; foreach my $pkg_class ( @pkg_class ) { - - push @items, 'cust_bill_pkg'; - - push @labels, - ( $sel_agent ? '' : $agent->agent.' ' ). - ( $classnum eq '0' - ? ( ref($pkg_class) ? $pkg_class->classname : $pkg_class ) - : '' - ); - - my $row_classnum = ref($pkg_class) ? $pkg_class->classnum : 0; - my $row_agentnum = $agent->agentnum; - push @params, [ 'classnum' => $row_classnum, - 'agentnum' => $row_agentnum, - 'use_override' => $use_override, - ]; - - push @links, "$link;agentnum=$row_agentnum;classnum=$row_classnum;". - "use_override=$use_override;"; - - @recur_colors = ($col_scheme->colors)[0,4,8,1,5,9] - unless @recur_colors; - @onetime_colors = ($col_scheme->colors)[2,6,10,3,7,11] - unless @onetime_colors; - push @colors, shift @recur_colors; - + foreach my $component ( $use_usage ? ('recurring', 'usage') : ('') ) { + + push @items, 'cust_bill_pkg'; + + push @labels, + ( $sel_agent ? '' : $agent->agent.' ' ). + ( $classnum eq '0' + ? ( ref($pkg_class) ? $pkg_class->classname : $pkg_class ) + : '' + ). + " $component"; + + my $row_classnum = ref($pkg_class) ? $pkg_class->classnum : 0; + my $row_agentnum = $agent->agentnum; + push @params, [ 'classnum' => $row_classnum, + 'agentnum' => $row_agentnum, + 'use_override' => $use_override, + 'use_usage' => $component, + ]; + + push @links, "$link;agentnum=$row_agentnum;classnum=$row_classnum;". + "use_override=$use_override;use_usage=$component;"; + + @recur_colors = ($col_scheme->colors)[0,4,8,1,5,9] + unless @recur_colors; + @onetime_colors = ($col_scheme->colors)[2,6,10,3,7,11] + unless @onetime_colors; + push @colors, shift @recur_colors; + + } } $hue += $hue_increment; diff --git a/httemplate/graph/cust_bill_pkg_detail.cgi b/httemplate/graph/cust_bill_pkg_detail.cgi new file mode 100644 index 000000000..642a9ecf3 --- /dev/null +++ b/httemplate/graph/cust_bill_pkg_detail.cgi @@ -0,0 +1,137 @@ +<% include('elements/monthly.html', + 'title' => $title. 'Rated Call Sales Report (Gross)', + 'graph_type' => 'Mountain', + 'items' => \@items, + 'params' => \@params, + 'labels' => \@labels, + 'graph_labels' => \@labels, + 'colors' => \@colors, + 'remove_empty' => 1, + 'bottom_total' => 1, + 'agentnum' => $agentnum, + ) +%> +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); + +#XXX or virtual +my( $agentnum, $sel_agent ) = ('', ''); +if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) { + $agentnum = $1; + $sel_agent = qsearchs('agent', { 'agentnum' => $agentnum } ); + die "agentnum $agentnum not found!" unless $sel_agent; +} +my $title = $sel_agent ? $sel_agent->agent.' ' : ''; + +#false lazinessish w/FS::cust_pkg::search_sql (previously search/cust_pkg.cgi) +my $classnum = ''; +if ( $cgi->param('classnum') =~ /^(\d*)$/ ) { + $classnum = $1; + + if ( $classnum ) { #a specific class + + my $pkg_class = ( qsearchs('pkg_class', { 'classnum' => $classnum } ) ); + die "classnum $classnum not found!" unless $pkg_class; + $title .= $pkg_class->classname.' '; + + } elsif ( $classnum eq '' ) { #the empty class + + $title .= 'Empty class '; + # FS::Report::Table::Monthly.pm has the converse view + $classnum = 0; + + } elsif ( $classnum eq '0' ) { #all classes + + # FS::Report::Table::Monthly.pm has the converse view + $classnum = ''; + } +} +#eslaf + +my $use_override = 0; +$use_override = 1 if ( $cgi->param('use_override') ); + +my $usageclass = 0; +my @usage_class = (); +if ( $cgi->param('usageclass') =~ /^(\d*)$/ ) { + $usageclass = $1; + + if ( $usageclass ) { #a specific class + + @usage_class = ( qsearchs('usage_class', { 'classnum' => $usageclass } ) ); + die "usage class $usageclass not found!" unless $usage_class[0]; + $title .= $usage_class[0]->classname.' '; + + } elsif ( $usageclass eq '' ) { #the empty class -- legacy + + $title .= 'Empty usage class '; + @usage_class = ( '(empty usage class)' ); + + } elsif ( $usageclass eq '0' ) { #all classes + + @usage_class = qsearch('usage_class', {} ); # { 'disabled' => '' } ); + push @usage_class, '(empty usage class)'; + + } +} +#eslaf + +my $hue = 0; +#my $hue_increment = 170; +#my $hue_increment = 145; +my $hue_increment = 125; + +my @items = (); +my @params = (); +my @labels = (); +my @colors = (); + +foreach my $agent ( $sel_agent || qsearch('agent', { 'disabled' => '' } ) ) { + + my $col_scheme = Color::Scheme->new + ->from_hue($hue) #->from_hex($agent->color) + ->scheme('analogic') + ; + my @recur_colors = (); + my @onetime_colors = (); + + ### fixup the color handling for usage classes... + my $n = 0; + + foreach my $usage_class ( @usage_class ) { + + push @items, 'cust_bill_pkg_detail'; + + push @labels, + ( $sel_agent ? '' : $agent->agent.' ' ). + ( $usageclass eq '0' + ? ( ref($usage_class) ? $usage_class->classname : $usage_class ) + : '' + ); + + my $row_classnum = ref($usage_class) ? $usage_class->classnum : 0; + my $row_agentnum = $agent->agentnum; + push @params, [ 'usageclass' => $row_classnum, + 'agentnum' => $row_agentnum, + 'use_override' => $use_override, + 'classnum' => $classnum, + ]; + + @recur_colors = ($col_scheme->colors)[0,4,8,1,5,9] + unless @recur_colors; + @onetime_colors = ($col_scheme->colors)[2,6,10,3,7,11] + unless @onetime_colors; + push @colors, shift @recur_colors; + + } + + $hue += $hue_increment; + +} + +#use Data::Dumper; +#warn Dumper(\@items); + + diff --git a/httemplate/graph/report_cust_bill_pkg.html b/httemplate/graph/report_cust_bill_pkg.html index 073f86773..51655a925 100644 --- a/httemplate/graph/report_cust_bill_pkg.html +++ b/httemplate/graph/report_cust_bill_pkg.html @@ -30,6 +30,12 @@ Separate sub-packages from parents + + + Separate rated usage from recurring fees + + +
diff --git a/httemplate/graph/report_cust_bill_pkg_detail.html b/httemplate/graph/report_cust_bill_pkg_detail.html new file mode 100644 index 000000000..3b85d521c --- /dev/null +++ b/httemplate/graph/report_cust_bill_pkg_detail.html @@ -0,0 +1,48 @@ +<% include('/elements/header.html', 'Usage Sales Report' ) %> + +
+ + + +<% include('/elements/tr-select-from_to.html' ) %> + +<% include('/elements/tr-select-agent.html', + 'label' => 'For agent: ', + 'disable_empty' => 0, + ) +%> + +<% include('/elements/tr-select-pkg_class.html', + 'pre_options' => [ '0' => 'all' ], + 'empty_label' => '(empty class)', + ) +%> + + + + + + +<% include('/elements/tr-select-table.html', + 'label' => 'Usage class: ', + 'element_name' => 'usageclass', + 'table' => 'usage_class', + 'name_col' => 'classname', + 'hashref' => { 'disabled' => '' }, + 'pre_options' => [ '0' => 'all' ], + 'empty_label' => '(empty class)', + ) +%> + +
Separate sub-packages from parents
+ +
+
+ +<% include('/elements/footer.html') %> +<%init> + +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); + + diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi index 8654585b4..62954ea18 100644 --- a/httemplate/search/cust_bill_pkg.cgi +++ b/httemplate/search/cust_bill_pkg.cgi @@ -8,7 +8,10 @@ '#', 'Description', 'Setup charge', - 'Recurring charge', + ( $use_usage eq 'usage' + ? 'Usage charge' + : 'Recurring charge' + ), 'Invoice', 'Date', FS::UI::Web::cust_header(), @@ -22,7 +25,17 @@ #strikethrough or "N/A ($amount)" or something these when # they're not applicable to pkg_tax search sub { sprintf($money_char.'%.2f', shift->setup ) }, - sub { sprintf($money_char.'%.2f', shift->recur ) }, + sub { my $row = shift; + my $value = 0; + if ( $use_usage eq 'recurring' ) { + $value = $row->recur - $row->usage; + } elsif ( $use_usage eq 'usage' ) { + $value = $row->usage; + } else { + $value = $row->recur; + } + sprintf($money_char.'%.2f', $value ); + }, 'invnum', sub { time2str('%b %d %Y', shift->_date ) }, \&FS::UI::Web::cust_fields, @@ -106,6 +119,8 @@ if ( $cgi->param('classnum') =~ /^(\d+)$/ ) { } } +my $use_usage = $cgi->param('use_usage'); + push @where, ' ( '. join(' OR ', map ' taxclass = '.dbh->quote($_), $cgi->param('taxclass') ). @@ -368,8 +383,15 @@ if ( $cgi->param('pkg_tax') ) { } else { - $count_query = - "SELECT COUNT(*), SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)"; + $count_query = "SELECT COUNT(*), "; + + if ( $use_usage eq 'recurring' ) { + $count_query .= "SUM(setup + recur - usage)"; + } elsif ( $use_usage eq 'usage' ) { + $count_query .= "SUM(usage)"; + } else { + $count_query .= "SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)"; + } } @@ -414,7 +436,17 @@ if ( $cgi->param('nottax') ) { } -$count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where"; +if ($use_usage) { + $count_query .= + " FROM (SELECT cust_bill_pkg.setup, cust_bill_pkg.recur, + ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_bill_pkg_detail + WHERE cust_bill_pkg.billpkgnum = cust_bill_pkg_detail.billpkgnum + ) AS usage FROM cust_bill_pkg $join_cust $join_pkg $where + ) AS countquery"; +} else { + $count_query .= " FROM cust_bill_pkg $join_cust $join_pkg $where"; +} +warn "count_query is $count_query\n"; my @select = ( 'cust_bill_pkg.*',