c634828272f53387a627289d547c02e6396935c7
[freeside.git] / httemplate / search / cust_bill_pkg_discount.html
1 <& 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                    'Class',
11                    'Amount',
12                    'Months',
13                    'Package',
14                    'Invoice',
15                    'Date',
16                    FS::UI::Web::cust_header(),
17                  ],
18                  'fields'      => [
19                    #'billpkgdiscountnum',
20                    sub { $_[0]->cust_pkg_discount->discount->description },
21                    sub { $_[0]->cust_pkg_discount->discount->classname },
22                    sub { sprintf($money_char.'%.2f', shift->amount ) },
23                    $months_sub,
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                    '',
32                    'amount',
33                    'months',
34                    'pkg',
35                    'invnum',
36                    '_date',
37                  ],
38                  'links'       => [
39                    #'',
40                    '', #link to customer discount???
41                    '',
42                    '',
43                    '',
44                    '',
45                    $ilink,
46                    $ilink,
47                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
48                          FS::UI::Web::cust_header()
49                    ),
50                  ],
51                  #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
52                  'align' => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
53                  'color' => [ 
54                               #'',
55                               '',
56                               '',
57                               '',
58                               '',
59                               '',
60                               '',
61                               '',
62                               FS::UI::Web::cust_colors(),
63                             ],
64                  'style' => [ 
65                               #'',
66                               '',
67                               '',
68                               '',
69                               '',
70                               '',
71                               '',
72                               '',
73                               FS::UI::Web::cust_styles(),
74                             ],
75            
76 &>
77 <%init>
78
79 #a little false laziness below w/cust_bill_pkg.cgi
80
81 die "access denied"
82   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
83
84 my $conf = new FS::Conf;
85
86 #here is the agent virtualization
87 my $agentnums_sql =
88   $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
89
90 my @where = ( $agentnums_sql );
91
92 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
93 push @where, "_date >= $beginning",
94              "_date <= $ending";
95
96 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
97   push @where, "cust_main.agentnum = $1";
98 }
99
100 #usernum
101 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
102   push @where, "cust_pkg_discount.usernum = $1";
103 }
104
105 # (discount) classnum
106 my $join_discount = '';
107 #false laziness w/cust_pkg_discount.html and cust_pkg.pm::search
108 if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
109
110 #  my @classnum = ();
111 #  if ( ref($params->{'discount_classnum'}) ) {
112 #
113 #    if ( ref($params->{'discount_classnum'}) eq 'HASH' ) {
114 #      @classnum = grep $params->{'discount_classnum'}{$_}, keys %{ $params->{'discount_classnum'} };
115 #    } elsif ( ref($params->{'discount_classnum'}) eq 'ARRAY' ) {
116 #      @classnum = @{ $params->{'discount_classnum'} };
117 #    } else {
118 #      die 'unhandled discount_classnum ref '. $params->{'discount_classnum'};
119 #    }
120 #
121 #
122 #  } elsif ( $params->{'discount_classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
123 #    @classnum = ( $1 );
124 #  }
125 #
126 #  if ( @classnum ) {
127
128    if ( $cgi->param('discount_classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
129     my @classnum = ( $1 );
130
131     $join_discount = 'LEFT JOIN discount USING (discountnum)';
132
133     my @c_where = ();
134     my @nums = grep $_, @classnum;
135     push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
136     my $null = scalar( grep { $_ eq '' } @classnum );
137     push @c_where, 'discount.classnum IS NULL' if $null;
138
139     if ( scalar(@c_where) == 1 ) {
140       push @where, @c_where;
141     } elsif ( @c_where ) {
142       push @where, ' ( '. join(' OR ', @c_where). ' ) ';
143     }
144
145   }
146
147 }
148
149 # #(package) classnum
150 # # not specified: all classes
151 # # 0: empty class
152 # # N: classnum
153 # my $use_override = $cgi->param('use_override');
154 # if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
155 #   my $comparison = '';
156 #   if ( $1 == 0 ) {
157 #     $comparison = "IS NULL";
158 #   } else {
159 #     $comparison = "= $1";
160 #   }
161
162 #   if ( $use_override ) {
163 #     push @where, "(
164 #       part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
165 #       override.classnum $comparison AND pkgpart_override IS NOT NULL
166 #     )";
167 #   } else {
168 #     push @where, "part_pkg.classnum $comparison";
169 #   }
170 # }
171
172 my $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
173
174 my $join_cust_pkg_discount =
175   'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
176
177 my $join_cust =
178   '         JOIN cust_bill USING ( invnum ) '.
179   FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
180
181 my $join_pkg =
182   '    JOIN cust_bill_pkg USING ( billpkgnum )
183   LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
184   LEFT JOIN part_pkg USING ( pkgpart ) ';
185   #LEFT JOIN part_pkg AS override
186   #  ON pkgpart_override = override.pkgpart ';
187
188 my $join = "$join_cust_pkg_discount $join_discount $join_pkg $join_cust";
189
190 my $where = ' WHERE '. join(' AND ', @where);
191
192 $count_query .= " FROM cust_bill_pkg_discount $join $where";
193
194 my @select = (
195                'cust_bill_pkg_discount.*',
196                #'cust_bill_pkg.*',
197                'cust_bill.invnum',
198                'cust_bill._date',
199              );
200 push @select, 'part_pkg.pkg';
201 push @select, 'cust_main.custnum',
202               FS::UI::Web::cust_sql_fields();
203
204 my $query = {
205   'table'     => 'cust_bill_pkg_discount',
206   'addl_from' => $join,
207   'hashref'   => {},
208   'select'    => join(', ', @select ),
209   'extra_sql' => $where,
210   'order_by'  => 'ORDER BY _date, billpkgdiscountnum',
211 };
212
213 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
214 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
215
216 my $conf = new FS::Conf;
217 my $money_char = $conf->config('money_char') || '$';
218
219 my $months_sub = sub {
220   my $cust_bill_pkg_discount = shift;
221   return 'Setup'
222     if $cust_bill_pkg_discount->cust_pkg_discount->setuprecur eq 'setup';
223   sprintf('%.2f', $cust_bill_pkg_discount->months);
224 };
225
226 </%init>