+ # sales_pkg_class number-of-months limit, grr
+ # (we should be able to just check for the cust_event record from the
+ # commission credit, but the report is supposed to act as a check on that)
+ #
+ # Pg-specific, of course
+ my $setup_date = 'TO_TIMESTAMP( cust_pkg.setup )';
+ my $interval = "(sales_pkg_class.commission_duration || ' months')::interval";
+ my $charge_date = 'TO_TIMESTAMP( cust_bill._date )';
+ push @where, "CASE WHEN sales_pkg_class.commission_duration IS NOT NULL ".
+ "THEN $charge_date < $setup_date + $interval ".
+ "ELSE TRUE END";
+
+ if ( $search{'paid'} ) {
+ push @where, FS::cust_bill_pkg->owed_sql . ' <= 0.005';
+ }
+
+ my $extra_sql = "WHERE ".join(' AND ', map {"( $_ )"} @where);
+
+ { 'table' => 'cust_bill_pkg',
+ 'select' => 'cust_bill_pkg.*',
+ 'addl_from' => ' LEFT JOIN cust_bill USING ( invnum ) '.
+ ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
+ ' LEFT JOIN part_pkg USING ( pkgpart ) '.
+ ' LEFT JOIN cust_main ON ( cust_pkg.custnum = cust_main.custnum )'.
+ ' JOIN sales_pkg_class ON ( '.
+ ' COALESCE( sales_pkg_class.classnum, 0) = COALESCE( part_pkg.classnum, 0) )',
+ 'extra_sql' => $extra_sql,
+ };
+}
+
+sub cust_bill_pkg {
+ my $self = shift;
+ qsearch( $self->cust_bill_pkg_search(@_) )
+}
+
+sub cust_credit_search {
+ my( $self, $sdate, $edate, %search ) = @_;
+
+ $search{'hashref'}->{'commission_salesnum'} = $self->salesnum;
+
+ my @where = ();
+ push @where, "cust_credit._date >= $sdate" if $sdate;
+ push @where, "cust_credit._date < $edate" if $edate;
+
+ my $classnum_sql = '';
+ if ( exists($search{'commission_classnum'}) ) {
+ my $classnum = delete($search{'commission_classnum'});
+ push @where, 'part_pkg.classnum '. ( $classnum ? " = $classnum"
+ : " IS NULL " );
+
+ $search{'addl_from'} .=
+ ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
+ ' LEFT JOIN part_pkg USING ( pkgpart ) ';
+ }
+
+ my $extra_sql = "AND ".join(' AND ', map {"( $_ )"} @where);
+
+ { 'table' => 'cust_credit',
+ 'extra_sql' => $extra_sql,
+ %search,
+ };
+}
+
+sub cust_credit {
+ my $self = shift;
+ qsearch( $self->cust_credit_search(@_) )