summaryrefslogtreecommitdiff
path: root/httemplate/search/sales_commission.html
blob: 710461c409decb9a4f7b00fbb3f5bd29f0818bd0 (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
111
112
113
114
115
% 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";

if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
  $query{hashref}->{agentnum} = $1;
  $count_query .= " WHERE agentnum = $1";
}

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;

    my $search = $sales->cust_bill_pkg_search(
      $beginning,
      $ending,
      'cust_main_sales' => $cust_main_sales,
      'paid' => $paid,
    );
    die 'cust_bill_pkg_search hashref not yet handled' if $search->{hashref};

    my $total = FS::Record->scalar_sql(
      "SELECT SUM(cust_bill_pkg.$field) FROM cust_bill_pkg ". #$search->{table}
      $search->{addl_from}. ' '. $search->{extra_sql}
    );

    return $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;

  my $search = $sales->cust_credit_search( $beginning, $ending );

  my $sql =
    "SELECT SUM(cust_credit.amount) FROM cust_credit ". #$search->{table}
    $search->{addl_from}. ' '.
    ' WHERE commission_salesnum = ? '. #$search->{hashref}
    $search->{extra_sql};

  my $total = FS::Record->scalar_sql($sql, $sales->salesnum);

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

my $commission_link = [ 'cust_credit.html?'.
                          "begin=$beginning;".
                          "end=$ending;".
                          "cust_main_sales=$cust_main_sales;".
                          'commission_salesnum=',
                        'salesnum'
                      ];

</%init>