additional by-otaker searches fixed for the brave new world of usernum, RT#9555
[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                  'links'       => [
30                    #'',
31                    '', #link to customer discount???
32                    '',
33                    '',
34                    '',
35                    $ilink,
36                    $ilink,
37                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
38                          FS::UI::Web::cust_header()
39                    ),
40                  ],
41                  #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
42                  'align' => 'lrrlrr'.FS::UI::Web::cust_aligns(),
43                  'color' => [ 
44                               #'',
45                               '',
46                               '',
47                               '',
48                               '',
49                               '',
50                               '',
51                               FS::UI::Web::cust_colors(),
52                             ],
53                  'style' => [ 
54                               #'',
55                               '',
56                               '',
57                               '',
58                               '',
59                               '',
60                               '',
61                               FS::UI::Web::cust_styles(),
62                             ],
63            )
64 %>
65 <%init>
66
67 #a little false laziness below w/cust_bill_pkg.cgi
68
69 die "access denied"
70   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
71
72 my $conf = new FS::Conf;
73
74 #here is the agent virtualization
75 my $agentnums_sql =
76   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
77
78 my @where = ( $agentnums_sql );
79
80 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
81 push @where, "_date >= $beginning",
82              "_date <= $ending";
83
84 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
85   push @where, "cust_main.agentnum = $1";
86 }
87
88 #usernum
89 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
90   push @where, "cust_pkg_discount.usernum = $1";
91 }
92
93 # #classnum
94 # # not specified: all classes
95 # # 0: empty class
96 # # N: classnum
97 # my $use_override = $cgi->param('use_override');
98 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
99 #   my $comparison = '';
100 #   if ( $1 == 0 ) {
101 #     $comparison = "IS NULL";
102 #   } else {
103 #     $comparison = "= $1";
104 #   }
105
106 #   if ( $use_override ) {
107 #     push @where, "(
108 #       part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
109 #       override.classnum $comparison AND pkgpart_override IS NOT NULL
110 #     )";
111 #   } else {
112 #     push @where, "part_pkg.classnum $comparison";
113 #   }
114 # }
115
116 my $count_query = "SELECT COUNT(*), SUM(amount)";
117
118 my $join_cust_pkg_discount =
119   'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
120
121 my $join_cust =
122   '      JOIN cust_bill_pkg USING ( billpkgnum )
123          JOIN cust_bill USING ( invnum ) 
124     LEFT JOIN cust_main USING ( custnum ) ';
125
126 my $join_pkg =
127   ' LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
128   LEFT JOIN part_pkg USING ( pkgpart ) ';
129   #LEFT JOIN part_pkg AS override
130   #  ON pkgpart_override = override.pkgpart ';
131
132 my $where = ' WHERE '. join(' AND ', @where);
133
134 $count_query .=
135   " FROM cust_bill_pkg_discount $join_cust_pkg_discount $join_cust $join_pkg ".
136   $where;
137
138 my @select = (
139                'cust_bill_pkg_discount.*',
140                #'cust_bill_pkg.*',
141                'cust_bill.invnum',
142                'cust_bill._date',
143              );
144 push @select, 'part_pkg.pkg';
145 push @select, 'cust_main.custnum',
146               FS::UI::Web::cust_sql_fields();
147
148 my $query = {
149   'table'     => 'cust_bill_pkg_discount',
150   'addl_from' => "$join_cust_pkg_discount $join_cust $join_pkg",
151   'hashref'   => {},
152   'select'    => join(', ', @select ),
153   'extra_sql' => $where,
154   'order_by'  => 'ORDER BY _date, billpkgdiscountnum',
155 };
156
157 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
158 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
159
160 my $conf = new FS::Conf;
161 my $money_char = $conf->config('money_char') || '$';
162
163 </%init>