RT# 79737 - Added ability to us a cc surcharge of a flat fee.
[freeside.git] / FS / FS / ClientAPI / MasonComponent.pm
1 package FS::ClientAPI::MasonComponent;
2
3 use strict;
4 use vars qw( $cache $DEBUG $me );
5 use subs qw( _cache );
6 use FS::Mason qw( mason_interps );
7 use FS::Conf;
8 use FS::ClientAPI_SessionCache;
9 use FS::Record qw( qsearch qsearchs );
10 use FS::cust_main;
11 use FS::part_pkg;
12
13 $DEBUG = 0;
14 $me = '[FS::ClientAPI::MasonComponent]';
15
16 my %allowed_comps = map { $_=>1 } qw(
17   /elements/customer-statement.html
18   /elements/select-did.html
19   /misc/areacodes.cgi
20   /misc/exchanges.cgi
21   /misc/phonenums.cgi
22   /misc/states.cgi
23   /misc/counties.cgi
24   /misc/cities.cgi
25   /misc/svc_acct-domains.cgi
26   /misc/part_svc-columns.cgi
27   /edit/elements/svc_forward.html
28 );
29
30 my %session_comps = map { $_=>1 } qw(
31   /elements/location.html
32   /elements/tr-amount_fee.html
33   /elements/select-part_pkg.html
34   /edit/cust_main/first_pkg/select-part_pkg.html
35 );
36
37 my %session_callbacks = (
38
39   '/elements/location.html' => sub {
40     my( $custnum, $argsref ) = @_;
41     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
42       or return "unknown custnum $custnum";
43     my %args = @$argsref;
44     $args{object} = $cust_main->bill_location;
45     @$argsref = ( %args );
46     return ''; #no error
47   },
48
49   '/elements/tr-amount_fee.html' => sub {
50     my( $custnum, $argsref ) = @_;
51
52     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
53       or return "unknown custnum $custnum";
54
55     my $conf = new FS::Conf;
56
57     my %args = @$argsref;
58     %args = (
59       %args,
60       'process-pkgpart'    =>
61         scalar($conf->config('selfservice_process-pkgpart', $cust_main->agentnum)),
62       'process-display'    => scalar($conf->config('selfservice_process-display')),
63       'process-skip_first' => $conf->exists('selfservice_process-skip_first'),
64       'num_payments'       => scalar($cust_main->cust_pay), 
65       'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage', $cust_main->agentnum)),
66       'surcharge_flatfee'  => scalar($conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum)),
67     );
68     @$argsref = ( %args );
69
70     return ''; #no error
71   },
72
73   '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
74     my( $custnum, $argsref ) = @_;
75     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
76       or return "unknown custnum $custnum";
77
78     my $pkgpart = $cust_main->agent->pkgpart_hashref;
79
80     #false laziness w/ edit/cust_main/first_pkg.html
81     my @first_svc = ( 'svc_acct', 'svc_phone' );
82
83     my @part_pkg =
84       grep { $_->svcpart(\@first_svc)
85              && ( $pkgpart->{ $_->pkgpart } 
86                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
87                 )
88            }
89       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
90
91     my $conf = new FS::Conf;
92     if ( $conf->exists('pkg-addon_classnum') ) {
93
94       my %classnum = map  { ( $_->addon_classnum => 1 ) }
95                      grep { $_->freq !~ /^0/ }
96                      map  { $_->part_pkg }
97                           $cust_main->ncancelled_pkgs;
98
99       unless ( $classnum{''} || ! keys %classnum ) {
100         @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
101       }
102     }
103
104     my %args = @$argsref;
105     $args{part_pkg} = \@part_pkg;
106     $args{first_svc} = \@first_svc;
107     $args{no_comment} = 1;
108     $args{label_callback} = sub { shift->pkg_comment };
109     @$argsref = ( %args );
110     return ''; #no error
111
112   },
113
114   '/elements/select-part_pkg.html' => sub {
115     my( $custnum, $argsref ) = @_;
116     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
117       or return "unknown custnum $custnum";
118
119     my $pkgpart = $cust_main->agent->pkgpart_hashref;
120
121     #false laziness w/ edit/cust_main/first_pkg.html
122     my @first_svc = ( 'svc_acct', 'svc_phone' );
123
124     my @part_pkg =
125       grep { $pkgpart->{ $_->pkgpart } 
126                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
127            }
128       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
129
130     push @$argsref, 'part_pkg' =>  \@part_pkg;
131     '';
132   },
133
134 );
135
136 my $outbuf;
137 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
138
139 sub mason_comp {
140   my $packet = shift;
141
142   warn "$me mason_comp called on $packet\n" if $DEBUG;
143
144   my $comp = $packet->{'comp'};
145   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
146     return { 'error' => 'Illegal component' };
147   }
148
149   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
150
151   if ( $session_comps{$comp} ) {
152
153     my $session = _cache->get($packet->{'session_id'})
154       or return ( 'error' => "Can't resume session" ); #better error message
155     my $custnum = $session->{'custnum'};
156
157     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
158     return { 'error' => $error } if $error;
159
160   }
161
162   my $conf = new FS::Conf;
163   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
164   $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
165   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
166
167   $outbuf = '';
168   $fs_interp->exec($comp, @args); #only FS for now alas...
169
170   #errors? (turn off in-line error reporting?)
171
172   return { 'output' => $outbuf };
173
174 }
175
176 #hmm
177 sub _cache {
178   $cache ||= new FS::ClientAPI_SessionCache( {
179                'namespace' => 'FS::ClientAPI::MyAccount',
180              } );
181 }
182
183 1;