+sub with_report_option {
+ my ($self, $num, $use_override) = @_;
+ # $num can be a single number, or a comma-delimited list of numbers,
+ # or an arrayref. 0 matches the empty set
+ # or the word 'multiple' for all packages with more than one report class
+ return '' if !defined($num);
+
+ $num = join(',', @$num) if ref($num);
+
+ # stringify the set of report options for each pkgpart
+ my $table = $use_override ? 'override' : 'part_pkg';
+ my $subselect = "
+ SELECT replace(optionname, 'report_option_', '') AS num
+ FROM part_pkg_option
+ WHERE optionname like 'report_option_%'
+ AND part_pkg_option.pkgpart = $table.pkgpart
+ ORDER BY num";
+
+ my $comparison;
+ if ( $num eq 'multiple' ) {
+ $comparison = "(SELECT COUNT(*) FROM ($subselect) AS x) > 1";
+ } else {
+
+ my @num = split(/\s*,\s*/, $num);
+
+ #$comparison = "(SELECT COALESCE(string_agg(num, ','), '') FROM ( #Pg 9-ism
+ $comparison = "(SELECT COALESCE(array_to_string(array_agg(num), ','), '')
+ FROM ($subselect) AS x
+ ) = '". join(',', grep $_, @num). "'";
+
+ $comparison = "( $comparison OR NOT EXISTS ($subselect) )"
+ if grep !$_, @num;
+
+ }
+ if ( $use_override ) {
+ # then also allow the non-override package to match
+ $comparison = "( $comparison OR " . $self->with_report_option($num) . ")";
+ }
+ $comparison;
+}
+
+sub with_cust_classnum {
+ my ($self, %opt) = @_;
+ if ( $opt{'cust_classnum'} ) {
+ my $classnums = $opt{'cust_classnum'};
+ $classnums = [ $classnums ] if !ref($classnums);
+ @$classnums = grep /^\d+$/, @$classnums;
+ return 'cust_main.classnum in('. join(',',@$classnums) .')'
+ if @$classnums;
+ }
+ ();
+}
+
+