Merge branch 'master' of https://github.com/jgoodman/Freeside
[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 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
37   $query{hashref}->{agentnum} = $1;
38   $count_query .= " WHERE agentnum = $1";
39 }
40
41 my $salesnum = '';
42 if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) {
43   $salesnum = $1;
44 } else {
45   $cgi->delete('salesnum');
46 }
47
48 my $title = 'Sales person commission';
49 $title .= ': '. time2str($date_format, $beginning). ' to '.
50                 time2str($date_format, $ending)
51   if $beginning;
52
53 my $paid = $cgi->param('paid') ? 1 : 0;
54 $title .= ' - paid sales only' if $paid;
55
56 my $cust_main_sales = $cgi->param('cust_main_sales') eq 'Y' ? 'Y' : '';
57
58 my $sales_link = [ 'sales_pkg_class.html?'.
59                    # pass all of our parameters along
60                    $cgi->query_string. ';salesnum=',
61                    'salesnum'
62                  ];
63
64 my $sales_sub_maker = sub {
65   my $field = shift;
66   sub {
67     my $sales = shift;
68
69     #efficiency improvement: ask the db for a sum instead of all the records
70     my $total = 0;
71     my @cust_bill_pkg = $sales->cust_bill_pkg(
72       $beginning,
73       $ending,
74       'cust_main_sales' => $cust_main_sales,
75       'paid' => $paid,
76     );
77     $total += $_->get($field) foreach @cust_bill_pkg;
78
79     $money_char. sprintf('%.2f', $total);
80   };
81 };
82
83 my $commission_sub = sub {
84   my $sales = shift;
85
86   #efficiency improvement: ask the db for a sum instead of all the records
87   my $total_credit = 0;
88   my @cust_credit  = $sales->cust_credit( $beginning, $ending );
89   $total_credit += $_->amount foreach @cust_credit;
90
91   $money_char. sprintf('%.2f', $total_credit);
92 };
93
94 my $commission_link = [ 'cust_credit.html?'.
95                           "begin=$beginning;".
96                           "end=$ending;".
97                           "cust_main_sales=$cust_main_sales;".
98                           'commission_salesnum=',
99                         'salesnum'
100                       ];
101
102 </%init>