1 package FS::ClientAPI::MasonComponent;
4 use vars qw( $cache $DEBUG $me );
5 use subs qw( _cache _mason_comp );
6 use FS::Mason qw( mason_interps );
8 use FS::ClientAPI_SessionCache;
9 use FS::Record qw( qsearch qsearchs );
14 $me = '[FS::ClientAPI::MasonComponent]';
16 my %allowed_comps = map { $_=>1 } qw(
17 /elements/customer-statement.html
18 /elements/select-did.html
25 /misc/svc_acct-domains.cgi
26 /misc/part_svc-columns.cgi
27 /edit/elements/svc_forward.html
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
37 my %session_callbacks = (
39 '/elements/location.html' => sub {
40 my( $custnum, $argsref ) = @_;
41 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
42 or return "unknown custnum $custnum";
44 $args{object} = $cust_main->bill_location;
45 @$argsref = ( %args );
49 '/elements/tr-amount_fee.html' => sub {
50 my( $custnum, $argsref ) = @_;
52 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
53 or return "unknown custnum $custnum";
55 my $conf = new FS::Conf;
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)),
68 @$argsref = ( %args );
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";
78 my $pkgpart = $cust_main->agent->pkgpart_hashref;
80 #false laziness w/ edit/cust_main/first_pkg.html
81 my @first_svc = ( 'svc_acct', 'svc_phone' );
84 grep { $_->svcpart(\@first_svc)
85 && ( $pkgpart->{ $_->pkgpart }
86 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
89 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
91 my $conf = new FS::Conf;
92 if ( $conf->exists('pkg-addon_classnum') ) {
94 my %classnum = map { ( $_->addon_classnum => 1 ) }
95 grep { $_->freq !~ /^0/ }
97 $cust_main->ncancelled_pkgs;
99 unless ( $classnum{''} || ! keys %classnum ) {
100 @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
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 );
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";
119 my $pkgpart = $cust_main->agent->pkgpart_hashref;
121 #false laziness w/ edit/cust_main/first_pkg.html
122 my @first_svc = ( 'svc_acct', 'svc_phone' );
125 grep { $pkgpart->{ $_->pkgpart }
126 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
128 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
130 push @$argsref, 'part_pkg' => \@part_pkg;
137 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
141 my $me = 'mason_comp';
142 my $namespace = 'FS::ClientAPI::MyAccount';
144 _mason_comp($packet, $me, $namespace);
148 sub payment_only_mason_comp {
150 my $me = 'payment_only_mason_comp';
151 my $namespace = 'FS::ClientAPI::PaymentOnly';
153 _mason_comp($packet, $me, $namespace);
159 my $namespace = shift;
161 warn "$me called on $packet\n" if $DEBUG;
163 my $comp = $packet->{'comp'};
164 unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
165 return { 'error' => 'Illegal component' };
168 my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
170 if ( $session_comps{$comp} ) {
172 my $session = _cache($namespace)->get($packet->{'session_id'})
173 or return ( 'error' => "Can't resume session" ); #better error message
174 my $custnum = $session->{'custnum'};
176 my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
177 return { 'error' => $error } if $error;
181 my $conf = new FS::Conf;
182 $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
183 $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
184 $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
187 $fs_interp->exec($comp, @args); #only FS for now alas...
189 #errors? (turn off in-line error reporting?)
191 return { 'output' => $outbuf };
197 my $namespace = shift || 'FS::ClientAPI::MyAccount';
199 $cache ||= new FS::ClientAPI_SessionCache( {
200 'namespace' => $namespace,