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