summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/commission.html
blob: 6f610639fa729bed994919071c8f3616a33f8da8 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
<& search.html, %opt &>
<%doc>
<& elements/commission.html,
  name_singular => 'sales person', # or 'agent', 'employee', etc.
  header        => [ 'Sales person' ], # 'One-Time Sales', 'Recurring Sales',
                                       # 'Commission' will be appended
  fields        => [ 'salesperson' ], # ditto
  links         => [ [ '/view/sales.html?', 'salesnum' ] ], # usual conventions
  sales_detail_link   => [ 'sales_commission_pkg.html?', 'salesnum' ],
  credit_detail_link  => [ 'cust_credit.html?commission_salesnum=', 'salesnum' ],
  align         => 'l',
  query         => {  table   => 'sales', # must be a Commission_Mixin
                      #other params as appropriate
                   },
  count_query   => 'SELECT COUNT(*) FROM sales ...',

  # all other elements/search.html stuff will be passed through
&>

The hash passed as 'query' will be passed through to the cust_bill_pkg_search
and cust_credit_search methods, and so can contain type-specific options.
</%doc>
<%init>
die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');

my %opt = @_;
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 = $opt{'query'};

my $paid = $cgi->param('paid') ? 1 : 0;
if ($beginning) {
  $opt{'title'} .= ': ' . time2str($date_format, $beginning) . ' to ' .
                          time2str($date_format, $ending);
}
if ($paid) {
  $opt{'title'} .= ' - paid sales only';
}

my $sales_sub_maker = sub {
  my $field = shift;
  sub {
    my $object = shift;
    my $search = $object->cust_bill_pkg_search(
      $beginning,
      $ending,
      'paid' => $paid,
      %$query,
    );
    $search->{select} = "SUM(cust_bill_pkg.$field) AS total_amount";
    my $result = qsearchs($search);
    my $total = $result->get('total_amount') || 0;

    return $money_char. sprintf('%.2f', $total);
  };
};

my $commission_sub = sub {
  my $object = shift;

  my $search = $object->cust_credit_search(
    $beginning,
    $ending,
    %$query
  );
  $search->{select} = 'SUM(cust_credit.amount) AS total_amount';
  my $result = qsearchs($search);
  my $total = $result->get('total_amount') || 0;

  return $money_char. sprintf('%.2f', $total);
};

my $sales_link = $opt{'sales_detail_link'};
if ($sales_link) {
  my ($pre, $post) = split('\?', $sales_link->[0], 2);
  $sales_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post;
}

my $commission_link = $opt{'credit_detail_link'};
if ($commission_link) {
  my ($pre, $post) = split('\?', $commission_link->[0], 2);
  $commission_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post;
}

# merge our new stuff into %opt
my $header = $opt{'header'};
push @$header,
  'One-time sales',
  'Recurring sales',
  'Commission'
;

my $fields = $opt{'fields'};
push @$fields, 
  $sales_sub_maker->('setup'),
  $sales_sub_maker->('recur'),
  $commission_sub
;

push @{$opt{'links'}}, $sales_link, $sales_link, $commission_link;
$opt{'align'} .= 'rrr';

</%init>