clean up various other RT initialization inconsistancy, RT#18719
[freeside.git] / httemplate / search / cust_bill_pkg_discount.html
1 <% include( 'elements/search.html',
2                  'title'       => 'Discounts',
3                  'name'        => 'discounts',
4                  'query'       => $query,
5                  'count_query' => $count_query,
6                  'count_addl'  => [ $money_char. '%.2f total', ],
7                  'header'      => [
8                    #'#',
9                    'Discount',
10                    'Amount',
11                    'Months',
12                    'Package',
13                    'Invoice',
14                    'Date',
15                    FS::UI::Web::cust_header(),
16                  ],
17                  'fields'      => [
18                    #'billpkgdiscountnum',
19                    sub { $_[0]->cust_pkg_discount->discount->description },
20                    sub { sprintf($money_char.'%.2f', shift->amount ) },
21                    sub { my $m = shift->months;
22                          $m =~ /\./ ? sprintf('%.2f', $m) : $m;
23                        },
24                    'pkg',#sub { $_[0]->cust_bill_pkg->cust_pkg->part_pkg->pkg },
25                    'invnum',
26                    sub { time2str('%b %d %Y', shift->_date ) },
27                    \&FS::UI::Web::cust_fields,
28                  ],
29                  'sort_fields' => [
30                    '',
31                    'amount',
32                    'months',
33                    'pkg',
34                    'invnum',
35                    '_date',
36                  ],
37                  'links'       => [
38                    #'',
39                    '', #link to customer discount???
40                    '',
41                    '',
42                    '',
43                    $ilink,
44                    $ilink,
45                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
46                          FS::UI::Web::cust_header()
47                    ),
48                  ],
49                  #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
50                  'align' => 'lrrlrr'.FS::UI::Web::cust_aligns(),
51                  'color' => [ 
52                               #'',
53                               '',
54                               '',
55                               '',
56                               '',
57                               '',
58                               '',
59                               FS::UI::Web::cust_colors(),
60                             ],
61                  'style' => [ 
62                               #'',
63                               '',
64                               '',
65                               '',
66                               '',
67                               '',
68                               '',
69                               FS::UI::Web::cust_styles(),
70                             ],
71            )
72 %>
73 <%init>
74
75 #a little false laziness below w/cust_bill_pkg.cgi
76
77 die "access denied"
78   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
79
80 my $conf = new FS::Conf;
81
82 #here is the agent virtualization
83 my $agentnums_sql =
84   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
85
86 my @where = ( $agentnums_sql );
87
88 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
89 push @where, "_date >= $beginning",
90              "_date <= $ending";
91
92 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
93   push @where, "cust_main.agentnum = $1";
94 }
95
96 #usernum
97 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
98   push @where, "cust_pkg_discount.usernum = $1";
99 }
100
101 # #classnum
102 # # not specified: all classes
103 # # 0: empty class
104 # # N: classnum
105 # my $use_override = $cgi->param('use_override');
106 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
107 #   my $comparison = '';
108 #   if ( $1 == 0 ) {
109 #     $comparison = "IS NULL";
110 #   } else {
111 #     $comparison = "= $1";
112 #   }
113
114 #   if ( $use_override ) {
115 #     push @where, "(
116 #       part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
117 #       override.classnum $comparison AND pkgpart_override IS NOT NULL
118 #     )";
119 #   } else {
120 #     push @where, "part_pkg.classnum $comparison";
121 #   }
122 # }
123
124 my $count_query = "SELECT COUNT(*), SUM(amount)";
125
126 my $join_cust_pkg_discount =
127   'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
128
129 my $join_cust =
130   '      JOIN cust_bill_pkg USING ( billpkgnum )
131          JOIN cust_bill USING ( invnum ) 
132     LEFT JOIN cust_main USING ( custnum ) ';
133
134 my $join_pkg =
135   ' LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
136   LEFT JOIN part_pkg USING ( pkgpart ) ';
137   #LEFT JOIN part_pkg AS override
138   #  ON pkgpart_override = override.pkgpart ';
139
140 my $where = ' WHERE '. join(' AND ', @where);
141
142 $count_query .=
143   " FROM cust_bill_pkg_discount $join_cust_pkg_discount $join_cust $join_pkg ".
144   $where;
145
146 my @select = (
147                'cust_bill_pkg_discount.*',
148                #'cust_bill_pkg.*',
149                'cust_bill.invnum',
150                'cust_bill._date',
151              );
152 push @select, 'part_pkg.pkg';
153 push @select, 'cust_main.custnum',
154               FS::UI::Web::cust_sql_fields();
155
156 my $query = {
157   'table'     => 'cust_bill_pkg_discount',
158   'addl_from' => "$join_cust_pkg_discount $join_cust $join_pkg",
159   'hashref'   => {},
160   'select'    => join(', ', @select ),
161   'extra_sql' => $where,
162   'order_by'  => 'ORDER BY _date, billpkgdiscountnum',
163 };
164
165 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
166 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
167
168 my $conf = new FS::Conf;
169 my $money_char = $conf->config('money_char') || '$';
170
171 </%init>