send commission reports by email, #33101
[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                                      sub { my $m = shift->months_used;
22                                            $m =~ /\./ ? sprintf('%.2f',$m) : $m;
23                                          },
24                                      'otaker',
25                                      'pkg',
26                                      \&FS::UI::Web::cust_fields,
27                                    ],
28                   'links'       => [
29                                      '',
30                                      '',
31                                      '',
32                                      '',
33                                      '',
34                                      '',
35                                      ( map { $_ ne 'Cust. Status' ? $clink : ''}
36                                            FS::UI::Web::cust_header()
37                                      ),
38                                    ],
39                   'align'       => 'clcrll'. FS::UI::Web::cust_aligns(),
40                   'color'       => [ 
41                                      '',
42                                      '',
43                                      '',
44                                      '',
45                                      '',
46                                      '',
47                                      FS::UI::Web::cust_colors(),
48                                    ],
49                  'style'        => [ 
50                                      '',
51                                      '',
52                                      '',
53                                      '',
54                                      '',
55                                      '',
56                                      FS::UI::Web::cust_styles(),
57                                    ],
58            
59 &>
60 <%init>
61
62 die "access denied"
63   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
64
65 #my $conf = new FS::Conf;
66
67 #here is the agent virtualization
68 my $agentnums_sql =
69   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
70
71 my @where = ( $agentnums_sql );
72
73 #status
74 if ( $cgi->param('status') eq 'active' ) {
75   push @where, "     (    cust_pkg_discount.disabled IS NULL
76                        OR cust_pkg_discount.disabled != 'Y'  )
77                  AND ( months IS NULL OR months_used < months ) ";
78                        #XXX also end date
79 } elsif ( $cgi->param('status') eq 'expired' ) {
80   push @where, "    (      cust_pkg_discount.disabled IS NOT NULL
81                       AND  cust_pkg_discount.disabled = 'Y'       )
82                  OR ( months IS NOT NULL AND months_used >= months )
83                ";     #XXX also end date
84 }
85
86 #classnum
87 #false laziness w/cust_pkg.pm::search
88 if ( grep { $_ eq 'classnum' } $cgi->param ) {
89
90 #  my @classnum = ();
91 #  if ( ref($params->{'classnum'}) ) {
92 #
93 #    if ( ref($params->{'classnum'}) eq 'HASH' ) {
94 #      @classnum = grep $params->{'classnum'}{$_}, keys %{ $params->{'classnum'} };
95 #    } elsif ( ref($params->{'classnum'}) eq 'ARRAY' ) {
96 #      @classnum = @{ $params->{'classnum'} };
97 #    } else {
98 #      die 'unhandled classnum ref '. $params->{'classnum'};
99 #    }
100 #
101 #
102 #  } elsif ( $params->{'classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
103 #    @classnum = ( $1 );
104 #  }
105 #
106 #  if ( @classnum ) {
107
108    if ( $cgi->param('classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
109     my @classnum = ( $1 );
110
111     my @c_where = ();
112     my @nums = grep $_, @classnum;
113     push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
114     my $null = scalar( grep { $_ eq '' } @classnum );
115     push @c_where, 'discount.classnum IS NULL' if $null;
116
117     if ( scalar(@c_where) == 1 ) {
118       push @where, @c_where;
119     } elsif ( @c_where ) {
120       push @where, ' ( '. join(' OR ', @c_where). ' ) ';
121     }
122
123   }
124
125 }
126
127 #usernum
128 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
129   push @where, "cust_pkg_discount.usernum = $1";
130 }
131
132 #agent
133 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
134   push @where, "cust_main.agentnum = $1";
135 }
136
137 my $count_query = "SELECT COUNT(*), SUM(amount)";
138
139 my $join = ' LEFT JOIN discount  USING ( discountnum )
140              LEFT JOIN cust_pkg  USING ( pkgnum )
141              LEFT JOIN part_pkg  USING ( pkgpart ) '.
142              FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
143
144 my $where = ' WHERE '. join(' AND ', @where);
145
146 $count_query .= " FROM cust_pkg_discount $join $where";
147
148 my @select = (
149                'cust_pkg_discount.*',
150                'part_pkg.pkg',
151              );
152 push @select, 'cust_main.custnum',
153               FS::UI::Web::cust_sql_fields();
154
155 my $query = {
156   'table'     => 'cust_pkg_discount',
157   'addl_from' => $join,
158   'hashref'   => {},
159   'select'    => join(', ', @select ),
160   'extra_sql' => $where,
161   'order_by'  => 'ORDER BY pkgdiscountnum',
162 };
163
164 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
165
166 my $conf = new FS::Conf;
167
168 </%init>