deposit slips
[freeside.git] / httemplate / search / elements / commission.html
1 <& search.html, %opt &>
2 <%doc>
3 <& elements/commission.html,
4   name_singular => 'sales person', # or 'agent', 'employee', etc.
5   header        => [ 'Sales person' ], # 'One-Time Sales', 'Recurring Sales',
6                                        # 'Commission' will be appended
7   fields        => [ 'salesperson' ], # ditto
8   links         => [ [ '/view/sales.html?', 'salesnum' ] ], # usual conventions
9   sales_detail_link   => [ 'sales_commission_pkg.html?', 'salesnum' ],
10   credit_detail_link  => [ 'cust_credit.html?commission_salesnum=', 'salesnum' ],
11   align         => 'l',
12   query         => {  table   => 'sales', # must be a Commission_Mixin
13                       #other params as appropriate
14                    },
15   count_query   => 'SELECT COUNT(*) FROM sales ...',
16
17   # all other elements/search.html stuff will be passed through
18 &>
19
20 The hash passed as 'query' will be passed through to the cust_bill_pkg_search
21 and cust_credit_search methods, and so can contain type-specific options.
22 </%doc>
23 <%init>
24 die "access denied"
25   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
26
27 my %opt = @_;
28 my $conf = new FS::Conf;
29
30 my $money_char = $conf->config('money_char') || '$';
31
32 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
33
34 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
35
36 my $query = $opt{'query'};
37
38 my $paid = $cgi->param('paid') ? 1 : 0;
39 if ($beginning) {
40   $opt{'title'} .= ': ' . time2str($date_format, $beginning) . ' to ' .
41                           time2str($date_format, $ending);
42 }
43 if ($paid) {
44   $opt{'title'} .= ' - paid sales only';
45 }
46
47 my $sales_sub_maker = sub {
48   my $field = shift;
49   sub {
50     my $object = shift;
51     my $search = $object->cust_bill_pkg_search(
52       $beginning,
53       $ending,
54       'paid' => $paid,
55       %$query,
56     );
57     $search->{select} = "SUM(cust_bill_pkg.$field) AS total_amount";
58     my $result = qsearchs($search);
59     my $total = $result->get('total_amount') || 0;
60
61     return $money_char. sprintf('%.2f', $total);
62   };
63 };
64
65 my $commission_sub = sub {
66   my $object = shift;
67
68   my $search = $object->cust_credit_search(
69     $beginning,
70     $ending,
71     %$query
72   );
73   $search->{select} = 'SUM(cust_credit.amount) AS total_amount';
74   my $result = qsearchs($search);
75   my $total = $result->get('total_amount') || 0;
76
77   return $money_char. sprintf('%.2f', $total);
78 };
79
80 my $sales_link = $opt{'sales_detail_link'};
81 if ($sales_link) {
82   my ($pre, $post) = split('\?', $sales_link->[0], 2);
83   $sales_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post;
84 }
85
86 my $commission_link = $opt{'credit_detail_link'};
87 if ($commission_link) {
88   my ($pre, $post) = split('\?', $commission_link->[0], 2);
89   $commission_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post;
90 }
91
92 # merge our new stuff into %opt
93 my $header = $opt{'header'};
94 push @$header,
95   'One-time sales',
96   'Recurring sales',
97   'Commission'
98 ;
99
100 my $fields = $opt{'fields'};
101 push @$fields, 
102   $sales_sub_maker->('setup'),
103   $sales_sub_maker->('recur'),
104   $commission_sub
105 ;
106
107 push @{$opt{'links'}}, $sales_link, $sales_link, $commission_link;
108 $opt{'align'} .= 'rrr';
109
110 </%init>