0e6c49975158541faa52b5d097f9b32f6ecde5bb
[freeside.git] / httemplate / edit / elements / ApplicationCommon.html
1 <%doc>
2
3 Examples:
4
5   #cust_bill_pay
6   include('elements/ApplicationCommon.html',
7     'form_action' => 'process/cust_bill_pay.cgi', 
8     'src_table'   => 'cust_pay',
9     'src_thing'   => 'payment',
10     'dst_table'   => 'cust_bill',
11     'dst_thing'   => 'invoice',
12   )
13
14   #cust_credit_bill
15   include('elements/ApplicationCommon.html',
16     'form_action' => 'process/cust_credit_bill.cgi',
17     'src_table'   => 'cust_credit',
18     'src_thing'   => 'credit',
19     'dst_table'   => 'cust_bill',
20     'dst_thing'   => 'invoice',
21   )
22
23   #cust_pay_refund
24   include('elements/ApplicationCommon.html',
25     'form_action' => 'process/cust_pay_refund.cgi',
26     'src_table'   => 'cust_pay',
27     'src_thing'   => 'payment',
28     'dst_table'   => 'cust_refund',
29     'dst_thing'   => 'refund',
30   )
31
32   #cust_credit_refund
33   include('elements/ApplicationCommon.html',
34     'form_action' => 'process/cust_credit_refund.cgi',
35     'src_table'   => 'cust_credit',
36     'src_thing'   => 'credit',
37     'dst_table'   => 'cust_refund',
38     'dst_thing'   => 'refund',
39   )
40
41 </%doc>
42 <% include('/elements/header-popup.html', "Apply $src_thing$to" ) %>
43
44 <% include('/elements/error.html') %>
45
46 <FORM ACTION="<% $p1. $opt{'form_action'} %>" METHOD=POST>
47
48 <% $src_thing %> #<B><% $src_pkeyvalue %></B><BR>
49 <INPUT TYPE="hidden" NAME="<% $src_pkey %>" VALUE="<% $src_pkeyvalue %>">
50
51 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
52
53 <TR>
54   <TD ALIGN="right">Date: </TD>
55   <TD><B><% time2str("%D", $src->_date) %></B></TD>
56 </TR>
57
58 <TR>
59   <TD ALIGN="right">Amount: </TD>
60   <TD><B><% $money_char %><% $src->amount %></B></TD>
61 </TR>
62
63 <TR>
64   <TD ALIGN="right">Unapplied amount: </TD>
65   <TD><B><% $money_char %><% $unapplied %></B></TD>
66 </TR>
67
68 % if ( $src_table eq 'cust_credit' ) {
69     <TR>
70       <TD ALIGN="right">Reason: </TD>
71       <TD><B><% $src->reason %></B></TD>
72     </TR>
73 % }
74
75 </TABLE>
76 <BR>
77
78 <SCRIPT TYPE="text/javascript">
79 function changed(what) {
80   dst = what.options[what.selectedIndex].value;
81
82 % foreach my $dst ( @dst ) {
83
84     if ( dst == <% $dst->$dst_pkey %> ) {
85       what.form.amount.value = "<% min($dst->$dst_unapplied, $unapplied) %>";
86     }
87
88 % } 
89
90 }
91 </SCRIPT>
92
93 Apply to:
94
95 <TABLE BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
96
97 <TR>
98   <TD ALIGN="right"><% $dst_thing %>: </TD>
99   <TD><SELECT NAME="<% $dst_pkey %>" SIZE=1 onChange="changed(this)">
100 <OPTION VALUE="">
101
102 % foreach my $dst ( @dst ) { 
103   <OPTION<% $dst->$dst_pkey eq $dst_pkeyvalue ? ' SELECTED' : '' %> VALUE="<% $dst->$dst_pkey %>">#<% $dst->$dst_pkey %> - <% time2str("%D", $dst->_date) %> - $<% $dst->$dst_unapplied %>
104 % } 
105
106 </SELECT>
107   </TD>
108 </TR>
109
110 <TR>
111   <TD ALIGN="right">Amount: </TD>
112   <TD><% $money_char %><INPUT TYPE="text" NAME="amount" VALUE="<% $amount %>" SIZE=8 MAXLENGTH=8></TD>
113 </TR>
114
115 </TABLE>
116
117 <BR>
118 <CENTER><INPUT TYPE="submit" VALUE="Apply"></CENTER>
119
120 </FORM>
121
122 <% include('/elements/footer.html') %>
123
124 <%init>
125
126 my %opt = @_;
127
128 my $conf = new FS::Conf;
129 my $money_char = $conf->config('money_char') || '$';
130
131 my $src_thing = ucfirst($opt{'src_thing'});
132 my $src_table = $opt{'src_table'};
133 my $src_pkey = dbdef->table($src_table)->primary_key;
134
135 my $dst_thing = ucfirst($opt{'dst_thing'});
136 my $dst_table = $opt{'dst_table'};
137 my $dst_pkey = dbdef->table($dst_table)->primary_key;
138 my $dst_unapplied = $dst_table eq 'cust_bill' ? 'owed' : 'unapplied';
139
140 my $to = $dst_table eq 'cust_refund' ? ' to Refund' : '';
141
142 my($src_pkeyvalue, $amount, $dst_pkeyvalue);
143 if ( $cgi->param('error') ) {
144   $src_pkeyvalue = $cgi->param($src_pkey);
145   $amount    = $cgi->param('amount');
146   $dst_pkeyvalue    = $cgi->param($dst_pkey);
147 } else {
148   my($query) = $cgi->keywords;
149   $query =~ /^(\d+)$/;
150   $src_pkeyvalue = $1;
151   $amount = '';
152   $dst_pkeyvalue = '';
153 }
154
155 my $otaker = getotaker;
156
157 my $p1 = popurl(1);
158
159 my $src = qsearchs($src_table, { $src_pkey => $src_pkeyvalue } );
160 die "$src_thing $src_pkeyvalue not found!" unless $src;
161
162 my $unapplied = $src->unapplied;
163
164 my @dst = sort {    $a->_date     <=> $b->_date
165                  or $a->$dst_pkey <=> $b->$dst_pkey
166                }
167           grep { $_->$dst_unapplied != 0 }
168           qsearch($dst_table, { 'custnum' => $src->custnum } );
169
170 </%init>