1 <& elements/search.html,
2 'title' => 'Discounts',
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total', ],
16 FS::UI::Web::cust_header(),
19 #'billpkgdiscountnum',
20 sub { $_[0]->cust_pkg_discount->discount->description },
21 sub { $_[0]->cust_pkg_discount->discount->classname },
22 sub { sprintf($money_char.'%.2f', shift->amount ) },
23 sub { my $m = shift->months;
24 $m =~ /\./ ? sprintf('%.2f', $m) : $m;
26 'pkg',#sub { $_[0]->cust_bill_pkg->cust_pkg->part_pkg->pkg },
28 sub { time2str('%b %d %Y', shift->_date ) },
29 \&FS::UI::Web::cust_fields,
42 '', #link to customer discount???
49 ( map { $_ ne 'Cust. Status' ? $clink : '' }
50 FS::UI::Web::cust_header()
53 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
54 'align' => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
64 FS::UI::Web::cust_colors(),
75 FS::UI::Web::cust_styles(),
81 #a little false laziness below w/cust_bill_pkg.cgi
84 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
86 my $conf = new FS::Conf;
88 #here is the agent virtualization
90 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
92 my @where = ( $agentnums_sql );
94 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
95 push @where, "_date >= $beginning",
98 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
99 push @where, "cust_main.agentnum = $1";
103 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
104 push @where, "cust_pkg_discount.usernum = $1";
107 # (discount) classnum
108 my $join_discount = '';
109 #false laziness w/cust_pkg_discount.html and cust_pkg.pm::search
110 if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
113 # if ( ref($params->{'discount_classnum'}) ) {
115 # if ( ref($params->{'discount_classnum'}) eq 'HASH' ) {
116 # @classnum = grep $params->{'discount_classnum'}{$_}, keys %{ $params->{'discount_classnum'} };
117 # } elsif ( ref($params->{'discount_classnum'}) eq 'ARRAY' ) {
118 # @classnum = @{ $params->{'discount_classnum'} };
120 # die 'unhandled discount_classnum ref '. $params->{'discount_classnum'};
124 # } elsif ( $params->{'discount_classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
125 # @classnum = ( $1 );
130 if ( $cgi->param('discount_classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
131 my @classnum = ( $1 );
133 $join_discount = 'LEFT JOIN discount USING (discountnum)';
136 my @nums = grep $_, @classnum;
137 push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
138 my $null = scalar( grep { $_ eq '' } @classnum );
139 push @c_where, 'discount.classnum IS NULL' if $null;
141 if ( scalar(@c_where) == 1 ) {
142 push @where, @c_where;
143 } elsif ( @c_where ) {
144 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
151 # #(package) classnum
152 # # not specified: all classes
155 # my $use_override = $cgi->param('use_override');
156 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
157 # my $comparison = '';
159 # $comparison = "IS NULL";
161 # $comparison = "= $1";
164 # if ( $use_override ) {
166 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
167 # override.classnum $comparison AND pkgpart_override IS NOT NULL
170 # push @where, "part_pkg.classnum $comparison";
174 my $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
176 my $join_cust_pkg_discount =
177 'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
180 ' JOIN cust_bill USING ( invnum ) '.
181 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
184 ' JOIN cust_bill_pkg USING ( billpkgnum )
185 LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
186 LEFT JOIN part_pkg USING ( pkgpart ) ';
187 #LEFT JOIN part_pkg AS override
188 # ON pkgpart_override = override.pkgpart ';
190 my $join = "$join_cust_pkg_discount $join_discount $join_pkg $join_cust";
192 my $where = ' WHERE '. join(' AND ', @where);
194 $count_query .= " FROM cust_bill_pkg_discount $join $where";
197 'cust_bill_pkg_discount.*',
202 push @select, 'part_pkg.pkg';
203 push @select, 'cust_main.custnum',
204 FS::UI::Web::cust_sql_fields();
207 'table' => 'cust_bill_pkg_discount',
208 'addl_from' => $join,
210 'select' => join(', ', @select ),
211 'extra_sql' => $where,
212 'order_by' => 'ORDER BY _date, billpkgdiscountnum',
215 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
216 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
218 my $conf = new FS::Conf;
219 my $money_char = $conf->config('money_char') || '$';