Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / httemplate / search / sales_commission.html
index 19e1188..57b6cdc 100644 (file)
+% if ( $salesnum ) {
+<% $cgi->redirect($sales_link->[0] . $salesnum) %>
+% } else {
 <& elements/search.html,
-     'title'         => 'Sales person commission report',
+     'title'         => $title,
      'name_singular' => 'sales person',
-#     'redirect'      => sub { #my( $sales, $cgi ) = @);
-#                              $saleslink;
-#                            },
-     'header'        => [ 'Sales person', 'Sales', 'Commission', ],
-     'fields'        => [ 'salesperson', sub{'XXX';}, sub{'XXX';}, ],
-     'links'         => [ '', $saleslink, $commissionlink ],
-     'query'         => { 'table' => 'sales', },
-     'count_query'   => 'SELECT COUNT(*) FROM sales',
+     '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 $saleslink = [ 'sales_pkg_class.html?salesnum=', 'salesnum' ];
+my $conf = new FS::Conf;
 
-my $commissionlink = ''; #XXX
+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;
+
+    #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>