sales person commission report fixes, #25255
[freeside.git] / httemplate / search / sales_commission.html
1 % if ( $salesnum ) {
2 <% $cgi->redirect($sales_link->[0] . $salesnum) %>
3 % } else {
4 <& elements/search.html,
5      'title'         => $title,
6      'name_singular' => 'sales person',
7      'header'        => [ 'Sales person', 'One-Time Sales', 'Recurring Sales', 'Commission', ],
8      'fields'        => [ 'salesperson',
9                           $sales_sub_maker->('setup'),
10                           $sales_sub_maker->('recur'),
11                           $commission_sub,
12                         ],
13      'links'         => [ '', $sales_link, $sales_link, $commission_link ],
14      'align'         => 'lrrr',
15      'query'         => \%query,
16      'count_query'   => $count_query,
17      'disableable'   => 1,
18 &>
19 % }
20 <%init>
21
22 die "access denied"
23   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
24
25 my $conf = new FS::Conf;
26
27 my $money_char = $conf->config('money_char') || '$';
28
29 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
30
31 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
32
33 my %query = ( 'table' => 'sales' );
34 my $count_query = "SELECT COUNT(*) FROM sales";
35
36 my $salesnum;
37 if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) {
38   $salesnum = $1;
39 }
40
41 my $title = 'Sales person commission';
42 $title .= ': '. time2str($date_format, $beginning). ' to '.
43                 time2str($date_format, $ending)
44   if $beginning;
45
46 my $cust_main_sales = $cgi->param('cust_main_sales') eq 'Y' ? 'Y' : '';
47
48 my $sales_link = [ 'sales_pkg_class.html?'.
49                      "begin=$beginning;".
50                      "end=$ending;".
51                      "cust_main_sales=$cust_main_sales;".
52                      "salesnum=",
53                    'salesnum'
54                  ];
55
56 my $sales_sub_maker = sub {
57   my $field = shift;
58   sub {
59     my $sales = shift;
60
61     #efficiency improvement: ask the db for a sum instead of all the records
62     my $total = 0;
63     my @cust_bill_pkg = $sales->cust_bill_pkg(
64       $beginning,
65       $ending,
66       'cust_main_sales' => $cust_main_sales,
67     );
68     $total += $_->get($field) foreach @cust_bill_pkg;
69
70     $money_char. sprintf('%.2f', $total);
71   };
72 };
73
74 my $commission_sub = sub {
75   my $sales = shift;
76
77   #efficiency improvement: ask the db for a sum instead of all the records
78   my $total_credit = 0;
79   my @cust_credit  = $sales->cust_credit( $beginning, $ending );
80   $total_credit += $_->amount foreach @cust_credit;
81
82   $money_char. sprintf('%.2f', $total_credit);
83 };
84
85 my $commission_link = [ 'cust_credit.html?'.
86                           "begin=$beginning;".
87                           "end=$ending;".
88                           "cust_main_sales=$cust_main_sales;".
89                           'commission_salesnum=',
90                         'salesnum'
91                       ];
92
93 </%init>