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 ) },
24 'pkg',#sub { $_[0]->cust_bill_pkg->cust_pkg->part_pkg->pkg },
26 sub { time2str('%b %d %Y', shift->_date ) },
27 \&FS::UI::Web::cust_fields,
40 '', #link to customer discount???
47 ( map { $_ ne 'Cust. Status' ? $clink : '' }
48 FS::UI::Web::cust_header()
51 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
52 'align' => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
62 FS::UI::Web::cust_colors(),
73 FS::UI::Web::cust_styles(),
79 #a little false laziness below w/cust_bill_pkg.cgi
82 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
84 my $conf = new FS::Conf;
86 #here is the agent virtualization
88 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
90 my @where = ( $agentnums_sql );
92 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
93 push @where, "_date >= $beginning",
96 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
97 push @where, "cust_main.agentnum = $1";
101 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
102 push @where, "cust_pkg_discount.usernum = $1";
105 # (discount) classnum
106 my $join_discount = '';
107 #false laziness w/cust_pkg_discount.html and cust_pkg.pm::search
108 if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
111 # if ( ref($params->{'discount_classnum'}) ) {
113 # if ( ref($params->{'discount_classnum'}) eq 'HASH' ) {
114 # @classnum = grep $params->{'discount_classnum'}{$_}, keys %{ $params->{'discount_classnum'} };
115 # } elsif ( ref($params->{'discount_classnum'}) eq 'ARRAY' ) {
116 # @classnum = @{ $params->{'discount_classnum'} };
118 # die 'unhandled discount_classnum ref '. $params->{'discount_classnum'};
122 # } elsif ( $params->{'discount_classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
123 # @classnum = ( $1 );
128 if ( $cgi->param('discount_classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
129 my @classnum = ( $1 );
131 $join_discount = 'LEFT JOIN discount USING (discountnum)';
134 my @nums = grep $_, @classnum;
135 push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
136 my $null = scalar( grep { $_ eq '' } @classnum );
137 push @c_where, 'discount.classnum IS NULL' if $null;
139 if ( scalar(@c_where) == 1 ) {
140 push @where, @c_where;
141 } elsif ( @c_where ) {
142 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
149 # #(package) classnum
150 # # not specified: all classes
153 # my $use_override = $cgi->param('use_override');
154 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
155 # my $comparison = '';
157 # $comparison = "IS NULL";
159 # $comparison = "= $1";
162 # if ( $use_override ) {
164 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
165 # override.classnum $comparison AND pkgpart_override IS NOT NULL
168 # push @where, "part_pkg.classnum $comparison";
172 my $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
174 my $join_cust_pkg_discount =
175 'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
178 ' JOIN cust_bill USING ( invnum ) '.
179 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
182 ' JOIN cust_bill_pkg USING ( billpkgnum )
183 LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
184 LEFT JOIN part_pkg USING ( pkgpart ) ';
185 #LEFT JOIN part_pkg AS override
186 # ON pkgpart_override = override.pkgpart ';
188 my $join = "$join_cust_pkg_discount $join_discount $join_pkg $join_cust";
190 my $where = ' WHERE '. join(' AND ', @where);
192 $count_query .= " FROM cust_bill_pkg_discount $join $where";
195 'cust_bill_pkg_discount.*',
200 push @select, 'part_pkg.pkg';
201 push @select, 'cust_main.custnum',
202 FS::UI::Web::cust_sql_fields();
205 'table' => 'cust_bill_pkg_discount',
206 'addl_from' => $join,
208 'select' => join(', ', @select ),
209 'extra_sql' => $where,
210 'order_by' => 'ORDER BY _date, billpkgdiscountnum',
213 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
214 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
216 my $conf = new FS::Conf;
217 my $money_char = $conf->config('money_char') || '$';
219 my $months_sub = sub {
220 my $cust_bill_pkg_discount = shift;
222 if $cust_bill_pkg_discount->cust_pkg_discount->setuprecur eq 'setup';
223 sprintf('%.2f', $cust_bill_pkg_discount->months);