Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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               'id'             => 'select_reason',
84               'control_button' => "document.getElementById('credit_button')",
85               'cgi'            => $cgi,
86 &>
87
88 <TR>
89   <TD ALIGN="right"><% mt('Additional info') |h %></TD>
90   <TD>
91     <INPUT TYPE="text" NAME="addlinfo" VALUE="<% $cgi->param('addlinfo') |h %>">
92   </TD>
93 </TR>
94
95 % if ( $conf->exists('credits-auto-apply-disable') ) {
96         <INPUT TYPE="HIDDEN" NAME="apply" VALUE="no">
97 % } else {
98   <TR>
99     <TD ALIGN="right"><% mt('Apply to selected line items') |h %></TD>
100     <TD><SELECT NAME="apply"><OPTION VALUE="yes" SELECTED><% mt('yes') |h %><OPTION><% mt('no') |h %></SELECT></TD>
101   </TR>
102 % }
103
104 </table>
105
106 <BR>
107 <INPUT TYPE="submit" ID="credit_button" VALUE="Credit" DISABLED>
108
109 </FORM>
110
111 <% include( '/elements/xmlhttp.html',
112             'url' =>  $p.'misc/xmlhttp-cust_bill_pkg-calculate_taxes.html',
113             'subs' => [ 'calculate_taxes' ],
114           )
115 %>
116 <SCRIPT TYPE="text/javascript">
117
118 document.getElementById('select_reason').disabled = true;
119   // start it disabled because no line items are selected yet
120 function show_taxes(arg) {
121   var argsHash = eval('(' + arg + ')');
122
123   //XXX add an 'ErrorMessage' section to the HTML and re-enable
124   //var error = argsHash['error'];
125
126   //var paragraph = document.getElementById('ErrorMessage');
127   //if (error) {
128   //  paragraph.innerHTML = 'Error: ' + error;
129   //  paragraph.style.color = '#ff0000';
130   //} else {
131   //  paragraph.innerHTML = '';
132   //}
133
134   var taxlines = argsHash['taxlines'];
135
136 //XXX display the tax lines? just a total will do for now
137 //
138 //  var table = document.getElementById('ApplicationTable');
139 //
140 //  var aFoundRow = 0;
141 //  for (i = 0; taxlines[i]; i++) {
142 //    var itemdesc = taxlines[i][0];
143 //    var locnum   = taxlines[i][2];
144 //    if (taxlines[i][3]) {
145 //      locnum  = taxlines[i][3];
146 //    }
147 //
148 //    var found = 0;
149 //    for (var row = 2; table.rows[row]; row++) {
150 //      var inputs = table.rows[row].getElementsByTagName('input');
151 //      if (! inputs.length) {
152 //        while ( table.rows[row] ) {
153 //           table.deleteRow(row);
154 //        }
155 //        break;
156 //      }
157 //      if ( inputs.item(4).value == itemdesc && inputs.item(2).value == locnum )
158 //      {
159 //        inputs.item(0).value = taxlines[i][1];
160 //        aFoundRow = found = row;
161 //        break;
162 //      }
163 //    }
164 //    if (! found) {
165 //      var row = table.insertRow(table.rows.length);
166 //      var warning_cell = document.createElement('TD');
167 //      warning_cell.style.color = '#ff0000';
168 //      warning_cell.colSpan = 2;
169 //      warning_cell.innerHTML = 'Calculated Tax - ' + itemdesc + ' - ' +
170 //                               taxlines[i][1] + ' will not be applied';
171 //      row.appendChild(warning_cell);
172 //    }
173 //  }
174 //
175 //  if (aFoundRow) {
176 //    sub_changed(table.rows[aFoundRow].getElementsByTagName('input').item(0));
177 //  }
178
179   var subtotal = parseFloat( argsHash['subtotal'] );
180
181   var taxtotal = parseFloat( argsHash['taxtotal'] );
182   document.getElementById('taxtotal_td').innerHTML =
183     '<% $money_char %>' + taxtotal.toFixed(2);
184
185   var total = subtotal + taxtotal;
186   document.getElementById('total_td').innerHTML =
187     '<% $money_char %>' + total.toFixed(2);
188   document.getElementById('total_el').value = total.toFixed(2);
189
190   //XXX reconcile both this and the reason selector wanteding to enable this
191   if ( total > 0 ) {
192     //document.getElementById('credit_button').disabled = false;
193     document.getElementById('select_reason').disabled = false;
194   }
195     
196 }
197
198 function calc_total(what) {
199
200   //document.getElementById('credit_button').disabled = true;
201   document.getElementById('select_reason').disabled = true;
202
203   var subtotal = 0;
204   // bah, a pain, just using an attribute var re = /^billpkgnum(\d+)$/;
205
206   var el = what.form.elements;
207   var billpkgnums = [];
208   var setuprecurs = [];
209   var amounts = [];
210   for (var i=0; i<el.length; i++) {
211
212     if ( el[i].type == 'checkbox' ) {
213       var amount_el = document.getElementById( el[i].id + '-amount' );
214       if ( el[i].checked ) {
215         amount_el.disabled = false;
216         var amount = amount_el.value;
217         subtotal += parseFloat( amount );
218         amounts.push( amount );
219         billpkgnums.push( el[i].getAttribute('data-billpkgnum') );
220         setuprecurs.push( el[i].getAttribute('data-setuprecur') );
221       } else {
222         amount_el.disabled = true;
223       }
224     }
225
226   }
227
228   document.getElementById('subtotal_td').innerHTML =
229     '<% $money_char %>' + subtotal.toFixed(2);
230
231   var args = new Array(
232     'custnum',     '<% $custnum %>',
233     'subtotal',    subtotal,
234     'billpkgnums', billpkgnums.join(),
235     'setuprecurs', setuprecurs.join(),
236     'amounts',     amounts.join()
237   );
238
239   calculate_taxes( args, show_taxes );
240
241 }
242 </SCRIPT>
243
244 <%init>
245
246 my $curuser = $FS::CurrentUser::CurrentUser;
247 die "access denied" unless $curuser->access_right('Post credit');
248
249 #a tiny bit of false laziness w/search/cust_bill_pkg.cgi, but we're pretty
250 # specialized and a piece of UI, not a report
251 #slightly more false laziness w/httemplate/edit/elements/ApplicationCommon.html
252 # show_taxes & calc_total here/do_calculate_tax there
253
254 my $conf = new FS::Conf;
255 my $money_char = $conf->config('money_char') || '$';
256 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
257
258 $cgi->param('custnum') =~ /^(\d+)$/ or die 'illegal custnum';
259 my $custnum = $1;
260
261 my $cust_main = qsearchs({
262   'table'     => 'cust_main',
263   'hashref'   => { 'custnum' => $custnum },
264   'extra_sql' => ' AND '. $curuser->agentnums_sql,
265 }) or die 'unknown customer';
266
267 my @cust_bill_pkg = qsearch({
268   'select'    => 'cust_bill_pkg.*',
269   'table'     => 'cust_bill_pkg',
270   'addl_from' => 'LEFT JOIN cust_bill USING (invnum)',
271   'extra_sql' => "WHERE custnum = $custnum AND pkgnum != 0",
272   'order_by'  => 'ORDER BY invnum ASC, billpkgnum ASC',
273 });
274
275 my @items = map { my %hash = $_->disintegrate;
276                   map [ $_, $hash{$_} ],
277                     keys(%hash);
278                 }
279               @cust_bill_pkg;
280
281 #omit line items which have been previously credited?  would be nice
282
283 </%init>