optimize customer view (esp. with lots of commission credits), RT#26416
[freeside.git] / httemplate / view / cust_main / payment_history / payment.html
1 <% $payment %>
2 <% "$info$desc$view$apply$refund$void$delete$unapply" %>
3 <%init>
4
5 my( $cust_pay, %opt ) = @_;
6
7 my $date_format = $opt{'date_format'} || '%m/%d/%Y';
8
9 my @cust_bill_pay = $cust_pay->cust_bill_pay;
10 my @cust_pay_refund = $cust_pay->cust_pay_refund;
11
12 my ($payby,$payinfo) = translate_payinfo($cust_pay);
13 my $target = "$payby$payinfo";
14 $payby = translate_payby($payby,$payinfo);
15 my $info = $payby ? "($payby$payinfo)" : '';
16
17 my $desc = '';
18 if ( $opt{'pkg-balances'} && $cust_pay->pkgnum ) {
19   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cust_pay->pkgnum } );
20   $desc .= ' for '. $cust_pkg->pkg_label_long;
21 }
22
23 my %cust_bill_pay_width = ('width' => 392);
24 my %cust_bill_pay_height = ();
25 if ( $opt{'cust_bill_pay_pkg-manual'} ) {
26   %cust_bill_pay_width = ('width' => 592);
27   %cust_bill_pay_height = ('height' => 436);
28 }
29
30 my $apply = '';
31
32 my $italicize_otaker = 0;
33 my $otaker = $cust_pay->otaker;
34 if ( $otaker eq 'fs_daily' ) {
35     $otaker = 'auto billing';
36     $italicize_otaker = 1;
37 }
38 if ( $otaker eq 'fs_selfservice' ) {
39     $otaker = 'customer self-service';
40     $italicize_otaker = 1;
41 }
42
43 my $payment = emt("Payment by [_1]",$otaker);
44 $payment =~ s/$otaker/<i>$otaker<\/i>/ if $italicize_otaker;
45
46 if (    scalar(@cust_bill_pay)   == 0
47      && scalar(@cust_pay_refund) == 0 ) {
48   #completely unapplied
49   $payment = emt("Unapplied Payment by [_1]",$otaker);
50   $payment =~ s/$otaker/<i>$otaker<\/i>/ if $italicize_otaker;
51   $payment = '<B><FONT COLOR="#FF0000">'.$payment.'</FONT></B>';
52   if ( $opt{'Apply payment'} ) {
53     if ( $opt{total_owed} > 0 ) {
54       $apply = ' ('.
55                include( '/elements/popup_link.html',
56                           'label'       => emt('apply'),
57                           'action'      => "${p}edit/cust_bill_pay.cgi?".
58                                            $cust_pay->paynum,
59                           'actionlabel' => emt('Apply payment'),
60                           %cust_bill_pay_width,
61                           %cust_bill_pay_height,
62                       ).
63                 ')';
64     }
65     if ( $opt{total_unapplied_refunds} > 0 ) {
66       $apply.= ' ('.
67                include( '/elements/popup_link.html',
68                           'label'       => emt('apply to refund'),
69                           'action'      => "${p}edit/cust_pay_refund.cgi?".
70                                            $cust_pay->paynum,
71                           'actionlabel' => emt('Apply payment to refund'),
72                           'width'       => 392,
73                       ).
74                ')';
75     }
76   }
77 } elsif (    scalar(@cust_bill_pay)   == 1
78           && scalar(@cust_pay_refund) == 0
79           && $cust_pay->unapplied == 0     ) {
80   #applied to one invoice, the usual situation
81   $desc .= ' '. $cust_bill_pay[0]->applied_to_invoice;
82 } elsif (    scalar(@cust_bill_pay)   == 0
83           && scalar(@cust_pay_refund) == 1
84           && $cust_pay->unapplied == 0     ) {
85   #applied to one refund
86   $desc .= emt(" refunded on [_1]", time2str($date_format, $cust_pay_refund[0]->_date) );
87 } else {
88   #complicated
89   $desc .= '<BR>';
90   foreach my $app ( sort { $a->_date <=> $b->_date }
91                          ( @cust_bill_pay, @cust_pay_refund ) ) {
92     if ( $app->isa('FS::cust_bill_pay') ) {
93       $desc .= '&nbsp;&nbsp;' . $opt{money_char} . $app->amount . ' ' .
94                 $app->applied_to_invoice . '<BR>';
95     } elsif ( $app->isa('FS::cust_pay_refund') ) {
96       $desc .= '&nbsp;&nbsp;' .
97                emt("[_1][_2] refunded on [_3]", $opt{money_char}, $app->amount,
98                         time2str($date_format, $app->_date) ) . '<BR>';
99     } else {
100       die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
101     }
102   }
103   if ( $cust_pay->unapplied > 0 ) {
104     $desc .= '&nbsp;&nbsp;'.
105              '<B><FONT COLOR="#FF0000">'.
106              emt("[_1][_2] unapplied", $opt{money_char}, $cust_pay->unapplied).
107              '</FONT></B>';
108     if ( $opt{'Apply payment'} ) {
109       if ( $opt{total_owed} > 0 ) {
110         $apply = ' ('.
111                  include( '/elements/popup_link.html',
112                             'label'      => emt('apply'),
113                             'action'     => "${p}edit/cust_bill_pay.cgi?".
114                                             $cust_pay->paynum,
115                             'actionlabel' => emt('Apply payment'),
116                             %cust_bill_pay_width,
117                             %cust_bill_pay_height,
118                         ).
119                  ')';
120       }
121       if ( $opt{total_unapplied_refunds} > 0 ) {
122         $apply.= ' ('.
123                  include( '/elements/popup_link.html',
124                             'label'      => emt('apply to refund'),
125                             'action'     => "${p}edit/cust_pay_refund.cgi?".
126                                             $cust_pay->paynum,
127                             'actionlabel' => emt('Apply payment to refund'),
128                             'width'      => 392,
129                         ).
130                  ')';
131       }
132     }
133     $desc .= '<BR>';
134   }
135 }
136
137 my $view =
138   ' ('. include('/elements/popup_link.html',
139                   'label'     => emt('view receipt'),
140                   'action'    => "${p}view/cust_pay.html?link=popup;paynum=".
141                                   $cust_pay->paynum,
142                   'actionlabel' => emt('Payment Receipt'),
143                ).
144    ')';
145
146 my $refund = '';
147 my $refund_days = $opt{'card_refund-days'} || 120;
148 my $refund_right = '';
149 $refund_right = 'Refund credit card payment' if $payby eq 'CARD';
150 $refund_right = 'Refund Echeck payment'      if $payby eq 'CHEK';
151 if (    $cust_pay->closed !~ /^Y/i
152      && $cust_pay->payby =~ /^(CARD|CHEK)$/
153      && time-$cust_pay->_date < $refund_days*86400
154      && $cust_pay->unrefunded > 0
155      && $opt{$refund_right}
156 ) {
157   $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
158             qq!paynum=!. $cust_pay->paynum. '"'.
159             qq! TITLE="! .emt('Send a refund for this payment to the payment gateway') 
160             . '">' . emt('refund') . '</A>)';
161 }
162
163 my $void = '';
164 my $voidmsg = $cust_pay->payby =~ /^(CARD|CHEK)$/
165               ? ' (' . emt('do not send anything to the payment gateway').')'
166               : '';
167 $void = areyousure_link("${p}misc/void-cust_pay.cgi?".$cust_pay->paynum,
168                         emt('Are you sure you want to void this payment?'),
169                         emt('Void this payment from the database') . $voidmsg,
170                         emt('void')
171                        )
172   if $cust_pay->closed !~ /^Y/i
173   && (    ( $cust_pay->payby eq 'CARD'          && $opt{'Credit card void'} )
174        || ( $cust_pay->payby eq 'CHEK'          && $opt{'Echeck void'}      )
175        || ( $cust_pay->payby !~ /^(CARD|CHEK)$/ && $opt{'Void payments'}    )
176      );
177
178 my $delete = '';
179 $delete = areyousure_link("${p}misc/delete-cust_pay.cgi?".$cust_pay->paynum,
180                             emt('Are you sure you want to delete this payment?'),
181                             emt('Delete this payment from the database completely - not recommended'),
182                             emt('delete')
183                          )
184   if $cust_pay->closed !~ /^Y/i
185   && $opt{'deletepayments'}
186   && $opt{'Delete payment'};
187
188 my $unapply = '';
189 $unapply = areyousure_link("${p}misc/unapply-cust_pay.cgi?".$cust_pay->paynum,
190                             emt('Are you sure you want to unapply this payment?'),
191                             emt('Keep this payment, but dissociate it from the invoices it is currently applied against'),
192                             emt('unapply')
193                           )
194   if $cust_pay->closed !~ /^Y/i
195   && scalar(@cust_bill_pay)           
196   && $opt{'Unapply payment'};
197
198 </%init>