This commit was generated by cvs2svn to compensate for changes in r9232,
[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 # #classnum
89 # # not specified: all classes
90 # # 0: empty class
91 # # N: classnum
92 # my $use_override = $cgi->param('use_override');
93 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
94 #   my $comparison = '';
95 #   if ( $1 == 0 ) {
96 #     $comparison = "IS NULL";
97 #   } else {
98 #     $comparison = "= $1";
99 #   }
100
101 #   if ( $use_override ) {
102 #     push @where, "(
103 #       part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
104 #       override.classnum $comparison AND pkgpart_override IS NOT NULL
105 #     )";
106 #   } else {
107 #     push @where, "part_pkg.classnum $comparison";
108 #   }
109 # }
110
111 my $count_query = "SELECT COUNT(*), SUM(amount)";
112
113 my $join_cust =  '      JOIN cust_bill_pkg USING ( billpkgnum )
114                         JOIN cust_bill USING ( invnum ) 
115                    LEFT JOIN cust_main USING ( custnum ) ';
116
117 my $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
118                  LEFT JOIN part_pkg USING ( pkgpart ) ';
119                  #LEFT JOIN part_pkg AS override
120                  #  ON pkgpart_override = override.pkgpart ';
121
122 my $where = ' WHERE '. join(' AND ', @where);
123
124 $count_query .= " FROM cust_bill_pkg_discount $join_cust $join_pkg $where";
125
126 my @select = (
127                'cust_bill_pkg_discount.*',
128                #'cust_bill_pkg.*',
129                'cust_bill.invnum',
130                'cust_bill._date',
131              );
132 push @select, 'part_pkg.pkg';
133 push @select, 'cust_main.custnum',
134               FS::UI::Web::cust_sql_fields();
135
136 my $query = {
137   'table'     => 'cust_bill_pkg_discount',
138   'addl_from' => "$join_cust $join_pkg",
139   'hashref'   => {},
140   'select'    => join(', ', @select ),
141   'extra_sql' => $where,
142   'order_by'  => 'ORDER BY _date, billpkgdiscountnum',
143 };
144
145 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
146 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
147
148 my $conf = new FS::Conf;
149 my $money_char = $conf->config('money_char') || '$';
150
151 </%init>