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