fix unapplied payment report, RT#73048, fallout from #25944
[freeside.git] / httemplate / search / cust_pkg_discount.html
1 <& elements/search.html,
2                   'title'       => 'Package discounts', 
3                   'name'        => 'discounts',
4                   'query'       => $query,
5                   'count_query' => $count_query,
6                   #'redirect'    => $link,
7                   'header'      => [ 'Status',
8                                      'Discount',
9                                      'Class',
10                                      'Months used',
11                                      'Employee',
12                                      'Package',
13                                      FS::UI::Web::cust_header(
14                                      #  $cgi->param('cust_fields')
15                                      ),
16                                    ],
17                   'fields'      => [
18                                      sub { ucfirst( shift->status ) },
19                                      sub { shift->discount->description },
20                                      sub { shift->discount->classname },
21                                      $months_used_sub,
22                                      'otaker',
23                                      'pkg',
24                                      \&FS::UI::Web::cust_fields,
25                                    ],
26                   'links'       => [
27                                      '',
28                                      '',
29                                      '',
30                                      '',
31                                      '',
32                                      '',
33                                      ( map { $_ ne 'Cust. Status' ? $clink : ''}
34                                            FS::UI::Web::cust_header()
35                                      ),
36                                    ],
37                   'align'       => 'clcrll'. FS::UI::Web::cust_aligns(),
38                   'color'       => [ 
39                                      '',
40                                      '',
41                                      '',
42                                      '',
43                                      '',
44                                      '',
45                                      FS::UI::Web::cust_colors(),
46                                    ],
47                  'style'        => [ 
48                                      '',
49                                      '',
50                                      '',
51                                      '',
52                                      '',
53                                      '',
54                                      FS::UI::Web::cust_styles(),
55                                    ],
56            
57 &>
58 <%init>
59
60 die "access denied"
61   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
62
63 #my $conf = new FS::Conf;
64
65 #here is the agent virtualization
66 my $agentnums_sql =
67   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
68
69 my @where = ( $agentnums_sql );
70
71 #status
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 ) ";
76                        #XXX also end date
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 )
81                ";     #XXX also end date
82 }
83
84 #classnum
85 #false laziness w/cust_pkg.pm::search
86 if ( grep { $_ eq 'classnum' } $cgi->param ) {
87
88 #  my @classnum = ();
89 #  if ( ref($params->{'classnum'}) ) {
90 #
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'} };
95 #    } else {
96 #      die 'unhandled classnum ref '. $params->{'classnum'};
97 #    }
98 #
99 #
100 #  } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
101 #    @classnum = ( $1 );
102 #  }
103 #
104 #  if ( @classnum ) {
105
106    if ( $cgi->param('classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
107     my @classnum = ( $1 );
108
109     my @c_where = ();
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;
114
115     if ( scalar(@c_where) == 1 ) {
116       push @where, @c_where;
117     } elsif ( @c_where ) {
118       push @where, ' ( '. join(' OR ', @c_where). ' ) ';
119     }
120
121   }
122
123 }
124
125 #usernum
126 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
127   push @where, "cust_pkg_discount.usernum = $1";
128 }
129
130 #agent
131 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
132   push @where, "cust_main.agentnum = $1";
133 }
134
135 my $count_query = "SELECT COUNT(*), SUM(amount)";
136
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');
141
142 my $where = ' WHERE '. join(' AND ', @where);
143
144 $count_query .= " FROM cust_pkg_discount $join $where";
145
146 my @select = (
147                'cust_pkg_discount.*',
148                'part_pkg.pkg',
149              );
150 push @select, 'cust_main.custnum',
151               FS::UI::Web::cust_sql_fields();
152
153 my $query = {
154   'table'     => 'cust_pkg_discount',
155   'addl_from' => $join,
156   'hashref'   => {},
157   'select'    => join(', ', @select ),
158   'extra_sql' => $where,
159   'order_by'  => 'ORDER BY pkgdiscountnum',
160 };
161
162 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
163
164 my $conf = new FS::Conf;
165
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);
170 };
171 </%init>