1 package FS::Commission_Mixin;
4 use FS::Record 'qsearch';
8 FS::Commission_Mixin - Common interface for entities that can receive
15 =item commission_where
17 Returns an SQL WHERE fragment to search for commission credits belonging
22 Returns an SQL WHERE fragment to search for sales records
23 (L<FS::cust_bill_pkg>) that would be assigned to this entity for commission.
27 sub commission_where { ... }
33 =item cust_credit_search START, END, OPTIONS
35 Returns a qsearch hashref for the commission credits given to this entity.
36 START and END are a date range.
38 OPTIONS may optionally contain "commission_classnum", a package classnum to
39 limit the commission packages.
43 sub cust_credit_search {
44 my( $self, $sdate, $edate, %search ) = @_;
46 my @where = ( $self->commission_where );
47 push @where, "cust_credit._date >= $sdate" if $sdate;
48 push @where, "cust_credit._date < $edate" if $edate;
50 my $classnum_sql = '';
52 if ( exists($search{'commission_classnum'}) ) {
53 my $classnum = delete($search{'commission_classnum'});
54 push @where, 'part_pkg.classnum '. ( $classnum ? " = $classnum"
58 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
59 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
62 my $extra_sql = 'WHERE ' . join(' AND ', map {"( $_ )"} @where);
64 { 'table' => 'cust_credit',
65 'addl_from' => $addl_from,
66 'extra_sql' => $extra_sql,
70 =item cust_credit START, END, OPTIONS
72 Takes the same options as cust_credit_search, and performs the search.
78 qsearch( $self->cust_credit_search(@_) );
81 =item cust_bill_pkg_search START, END, OPTIONS
83 Returns a qsearch hashref for the sales for which this entity could receive
84 commission. START and END are a date range; OPTIONS may contain:
85 - I<classnum>: limit to this package class (or null, if it's empty)
86 - I<paid>: limit to sales that have no unpaid balance (as of now)
90 sub cust_bill_pkg_search {
91 my( $self, $sdate, $edate, %search ) = @_;
93 my @where = $self->sales_where(%search);
94 push @where, "cust_bill._date >= $sdate" if $sdate;
95 push @where, "cust_bill._date < $edate" if $edate;
97 my $classnum_sql = '';
98 if ( exists( $search{'classnum'} ) ) {
99 my $classnum = $search{'classnum'} || '';
100 die "bad classnum" unless $classnum =~ /^(\d*)$/;
103 "part_pkg.classnum ". ( $classnum ? " = $classnum " : ' IS NULL ' );
106 if ( $search{'paid'} ) {
107 push @where, FS::cust_bill_pkg->owed_sql . ' <= 0.005';
110 my $extra_sql = "WHERE ".join(' AND ', map {"( $_ )"} @where);
112 { 'table' => 'cust_bill_pkg',
113 'select' => 'cust_bill_pkg.*',
114 'addl_from' => ' LEFT JOIN cust_bill USING ( invnum ) '.
115 ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
116 ' LEFT JOIN part_pkg USING ( pkgpart ) '.
117 ' LEFT JOIN cust_main ON ( cust_pkg.custnum = cust_main.custnum )',
118 'extra_sql' => $extra_sql,
122 =item cust_bill_pkg START, END, OPTIONS
124 Same as L</cust_bill_pkg_search> but then performs the search.