credit line items interface: set credit amounts, RT#18676
[freeside.git] / httemplate / edit / credit-cust_bill_pkg.html
1 <& /elements/header-popup.html, 'Credit line items' &>
2
3 <FORM ACTION="process/credit-cust_bill_pkg.html" METHOD="POST">
4 <INPUT TYPE="hidden" NAME="crednum" VALUE="">
5 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum |h %>">
6 <INPUT TYPE="hidden" NAME="paybatch" VALUE="">
7 <INPUT TYPE="hidden" NAME="_date" VALUE="<% time %>">
8 <table>
9
10 % my $old_invnum = 0; 
11 %# foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
12 % foreach my $item ( @items ) {
13 %   my( $setuprecur, $cust_bill_pkg ) = @$item;
14
15 %   my $method = $setuprecur eq 'setup' ? 'setup' : 'recur';
16 %   my $amount = $cust_bill_pkg->$method();
17 %   my $credited = $cust_bill_pkg->credited('', '', 'setuprecur'=>$method);
18 %   $amount -= $credited;
19 %   $amount = sprintf('%.2f', $amount);
20 %   next unless $amount > 0;
21
22 %   if ( $cust_bill_pkg->invnum ne $old_invnum ) {
23       <TR><TD COLSPAN=4 BGCOLOR="#f8f8f8">&nbsp;</TD></TR>
24       <TR><TH COLSPAN=4 BGCOLOR="#f8f8f8" ALIGN="left">Invoice #<% $cust_bill_pkg->invnum %> - <% time2str($date_format, $cust_bill_pkg->cust_bill->_date) %></TD></TR>
25 %     $old_invnum = $cust_bill_pkg->invnum;
26 %   }
27
28 %   my $el_name = 'billpkgnum'. $cust_bill_pkg->billpkgnum. '-'. $setuprecur;
29     <TR>
30       <TD>
31         <INPUT TYPE            = "checkbox"
32                NAME            = "<% $el_name %>"
33                ID              = "<% $el_name %>"
34                onClick         = "calc_total(this)"
35                data-billpkgnum = "<% $cust_bill_pkg->billpkgnum %>"
36                data-setuprecur = "<% $setuprecur %>"
37         >
38       </TD>
39       <TD BGCOLOR="#ffffff"><% $cust_bill_pkg->desc |h %></TD>
40 %#    show one-time/setup vs recur vs usage?
41       <TD BGCOLOR="#ffffff" ALIGN="right"><% $money_char. $amount %></TD>
42       <TD ALIGN="right">
43         <% $money_char %><INPUT TYPE     = "text"
44                                 NAME     = "<% $el_name %>-amount"
45                                 ID       = "<% $el_name %>-amount"
46                                 VALUE    = "<% $amount %>"
47                                 SIZE     = 6
48                                 onChange = "calc_total(this)"
49                                 STYLE    = "text-align:right;"
50                                 DISABLED
51                          >
52       </TD>
53     </TR>
54
55 % }
56
57 <TR><TD COLSPAN=4 BGCOLOR="#f8f8f8">&nbsp;</TD></TR>
58 <TR>
59   <TD></TD>
60   <TD ALIGN="right" COLSPAN=2>Subtotal: </TD>
61   <TD ALIGN="right" ID="subtotal_td"><% $money_char %><% sprintf('%.2f', 0) %></TD>
62 </TR>
63 <TR>
64   <TD></TD>
65   <TD ALIGN="right" COLSPAN=2>Taxes: </TD>
66   <TD ALIGN="right" ID="taxtotal_td"><% $money_char %><% sprintf('%.2f', 0) %></TD>
67 </TR>
68 <TR>
69   <TD></TD>
70   <TH ALIGN="right" COLSPAN=2>Total credit amount: </TD>
71   <TH ALIGN="right" ID="total_td"><% $money_char %><% sprintf('%.2f', 0) %></TD>
72 </TR>
73 <INPUT TYPE="hidden" NAME="amount" ID="total_el" VALUE="0.00">
74
75 </table>
76
77 <table>
78
79 <& /elements/tr-select-reason.html,
80               'field'          => 'reasonnum',
81               'reason_class'   => 'R',
82               #XXX reconcile both this and show_taxes wanteding to enable this
83               'control_button' => "document.getElementById('credit_button')",
84               'cgi'            => $cgi,
85 &>
86
87 <TR>
88   <TD ALIGN="right"><% mt('Additional info') |h %></TD>
89   <TD>
90     <INPUT TYPE="text" NAME="addlinfo" VALUE="<% $cgi->param('addlinfo') |h %>">
91   </TD>
92 </TR>
93
94 </table>
95
96 <BR>
97 <INPUT TYPE="submit" ID="credit_button" VALUE="Credit" DISABLED>
98
99 </FORM>
100
101 <% include( '/elements/xmlhttp.html',
102             'url' =>  $p.'misc/xmlhttp-cust_bill_pkg-calculate_taxes.html',
103             'subs' => [ 'calculate_taxes' ],
104           )
105 %>
106 <SCRIPT TYPE="text/javascript">
107
108 function show_taxes(arg) {
109   var argsHash = eval('(' + arg + ')');
110
111   //XXX add an 'ErrorMessage' section to the HTML and re-enable
112   //var error = argsHash['error'];
113
114   //var paragraph = document.getElementById('ErrorMessage');
115   //if (error) {
116   //  paragraph.innerHTML = 'Error: ' + error;
117   //  paragraph.style.color = '#ff0000';
118   //} else {
119   //  paragraph.innerHTML = '';
120   //}
121
122   var taxlines = argsHash['taxlines'];
123
124 //XXX display the tax lines? just a total will do for now
125 //
126 //  var table = document.getElementById('ApplicationTable');
127 //
128 //  var aFoundRow = 0;
129 //  for (i = 0; taxlines[i]; i++) {
130 //    var itemdesc = taxlines[i][0];
131 //    var locnum   = taxlines[i][2];
132 //    if (taxlines[i][3]) {
133 //      locnum  = taxlines[i][3];
134 //    }
135 //
136 //    var found = 0;
137 //    for (var row = 2; table.rows[row]; row++) {
138 //      var inputs = table.rows[row].getElementsByTagName('input');
139 //      if (! inputs.length) {
140 //        while ( table.rows[row] ) {
141 //           table.deleteRow(row);
142 //        }
143 //        break;
144 //      }
145 //      if ( inputs.item(4).value == itemdesc && inputs.item(2).value == locnum )
146 //      {
147 //        inputs.item(0).value = taxlines[i][1];
148 //        aFoundRow = found = row;
149 //        break;
150 //      }
151 //    }
152 //    if (! found) {
153 //      var row = table.insertRow(table.rows.length);
154 //      var warning_cell = document.createElement('TD');
155 //      warning_cell.style.color = '#ff0000';
156 //      warning_cell.colSpan = 2;
157 //      warning_cell.innerHTML = 'Calculated Tax - ' + itemdesc + ' - ' +
158 //                               taxlines[i][1] + ' will not be applied';
159 //      row.appendChild(warning_cell);
160 //    }
161 //  }
162 //
163 //  if (aFoundRow) {
164 //    sub_changed(table.rows[aFoundRow].getElementsByTagName('input').item(0));
165 //  }
166
167   var subtotal = parseFloat( argsHash['subtotal'] );
168
169   var taxtotal = parseFloat( argsHash['taxtotal'] );
170   document.getElementById('taxtotal_td').innerHTML =
171     '<% $money_char %>' + taxtotal.toFixed(2);
172
173   var total = subtotal + taxtotal;
174   document.getElementById('total_td').innerHTML =
175     '<% $money_char %>' + total.toFixed(2);
176   document.getElementById('total_el').value = total.toFixed(2);
177
178   //XXX reconcile both this and the reason selector wanteding to enable this
179   if ( total > 0 ) {
180     document.getElementById('credit_button').disabled = false;
181   }
182     
183 }
184
185 function calc_total(what) {
186
187   document.getElementById('credit_button').disabled = true;
188
189   var subtotal = 0;
190   // bah, a pain, just using an attribute var re = /^billpkgnum(\d+)$/;
191
192   var el = what.form.elements;
193   var billpkgnums = [];
194   var setuprecurs = [];
195   var amounts = [];
196   for (var i=0; i<el.length; i++) {
197
198     if ( el[i].type == 'checkbox' ) {
199       var amount_el = document.getElementById( el[i].id + '-amount' );
200       if ( el[i].checked ) {
201         amount_el.disabled = false;
202         var amount = amount_el.value;
203         subtotal += parseFloat( amount );
204         amounts.push( amount );
205         billpkgnums.push( el[i].getAttribute('data-billpkgnum') );
206         setuprecurs.push( el[i].getAttribute('data-setuprecur') );
207       } else {
208         amount_el.disabled = true;
209       }
210     }
211
212   }
213
214   document.getElementById('subtotal_td').innerHTML =
215     '<% $money_char %>' + subtotal.toFixed(2);
216
217   var args = new Array(
218     'custnum',     '<% $custnum %>',
219     'subtotal',    subtotal,
220     'billpkgnums', billpkgnums.join(),
221     'setuprecurs', setuprecurs.join(),
222     'amounts',     amounts.join()
223   );
224
225   calculate_taxes( args, show_taxes );
226
227 }
228 </SCRIPT>
229
230 <%init>
231
232 my $curuser = $FS::CurrentUser::CurrentUser;
233 die "access denied" unless $curuser->access_right('Post credit');
234
235 #a tiny bit of false laziness w/search/cust_bill_pkg.cgi, but we're pretty
236 # specialized and a piece of UI, not a report
237 #slightly more false laziness w/httemplate/edit/elements/ApplicationCommon.html
238 # show_taxes & calc_total here/do_calculate_tax there
239
240 my $conf = new FS::Conf;
241 my $money_char = $conf->config('money_char') || '$';
242 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
243
244 $cgi->param('custnum') =~ /^(\d+)$/ or die 'illegal custnum';
245 my $custnum = $1;
246
247 my $cust_main = qsearchs({
248   'table'     => 'cust_main',
249   'hashref'   => { 'custnum' => $custnum },
250   'extra_sql' => ' AND '. $curuser->agentnums_sql,
251 }) or die 'unknown customer';
252
253 my @cust_bill_pkg = qsearch({
254   'select'    => 'cust_bill_pkg.*',
255   'table'     => 'cust_bill_pkg',
256   'addl_from' => 'LEFT JOIN cust_bill USING (invnum)',
257   'extra_sql' => "WHERE custnum = $custnum AND pkgnum != 0",
258   'order_by'  => 'ORDER BY invnum ASC, billpkgnum ASC',
259 });
260
261 my @items = map { my %hash = $_->disintegrate;
262                   map [ $_, $hash{$_} ],
263                     keys(%hash);
264                 }
265               @cust_bill_pkg;
266
267 #omit line items which have been previously credited?  would be nice
268
269 </%init>