restructure agent commission reporting, #23348
[freeside.git] / httemplate / search / elements / commission.html
diff --git a/httemplate/search/elements/commission.html b/httemplate/search/elements/commission.html
new file mode 100644 (file)
index 0000000..6f61063
--- /dev/null
@@ -0,0 +1,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>