1 <& elements/search.html,
2 'title' => 'Package discounts',
5 'count_query' => $count_query,
7 'header' => [ 'Status',
13 FS::UI::Web::cust_header(
14 # $cgi->param('cust_fields')
18 sub { ucfirst( shift->status ) },
19 sub { shift->discount->description },
20 sub { shift->discount->classname },
24 \&FS::UI::Web::cust_fields,
33 ( map { $_ ne 'Cust. Status' ? $clink : ''}
34 FS::UI::Web::cust_header()
37 'align' => 'clcrll'. FS::UI::Web::cust_aligns(),
45 FS::UI::Web::cust_colors(),
54 FS::UI::Web::cust_styles(),
61 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
63 #my $conf = new FS::Conf;
65 #here is the agent virtualization
67 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
69 my @where = ( $agentnums_sql );
72 if ( $cgi->param('status') eq 'active' ) {
73 push @where, " ( cust_pkg_discount.disabled IS NULL
74 OR cust_pkg_discount.disabled != 'Y' )
75 AND ( months IS NULL OR months_used < months ) ";
77 } elsif ( $cgi->param('status') eq 'expired' ) {
78 push @where, " ( cust_pkg_discount.disabled IS NOT NULL
79 AND cust_pkg_discount.disabled = 'Y' )
80 OR ( months IS NOT NULL AND months_used >= months )
85 #false laziness w/cust_pkg.pm::search
86 if ( grep { $_ eq 'classnum' } $cgi->param ) {
89 # if ( ref($params->{'classnum'}) ) {
91 # if ( ref($params->{'classnum'}) eq 'HASH' ) {
92 # @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
93 # } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
94 # @classnum = @{ $params->{'classnum'} };
96 # die 'unhandled classnum ref '. $params->{'classnum'};
100 # } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
101 # @classnum = ( $1 );
106 if ( $cgi->param('classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
107 my @classnum = ( $1 );
110 my @nums = grep $_, @classnum;
111 push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
112 my $null = scalar( grep { $_ eq '' } @classnum );
113 push @c_where, 'discount.classnum IS NULL' if $null;
115 if ( scalar(@c_where) == 1 ) {
116 push @where, @c_where;
117 } elsif ( @c_where ) {
118 push @where, ' ( '. join(' OR ', @c_where). ' ) ';
126 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
127 push @where, "cust_pkg_discount.usernum = $1";
131 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
132 push @where, "cust_main.agentnum = $1";
135 my $count_query = "SELECT COUNT(*), SUM(amount)";
137 my $join = ' LEFT JOIN discount USING ( discountnum )
138 LEFT JOIN cust_pkg USING ( pkgnum )
139 LEFT JOIN part_pkg USING ( pkgpart ) '.
140 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
142 my $where = ' WHERE '. join(' AND ', @where);
144 $count_query .= " FROM cust_pkg_discount $join $where";
147 'cust_pkg_discount.*',
150 push @select, 'cust_main.custnum',
151 FS::UI::Web::cust_sql_fields();
154 'table' => 'cust_pkg_discount',
155 'addl_from' => $join,
157 'select' => join(', ', @select ),
158 'extra_sql' => $where,
159 'order_by' => 'ORDER BY pkgdiscountnum',
162 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
164 my $conf = new FS::Conf;
166 my $months_used_sub = sub {
167 my $cust_pkg_discount = shift;
168 return 'Setup only' if $cust_pkg_discount->setuprecur eq 'setup';
169 return sprintf('%.2f', $cust_pkg_discount->months_used);