1 package FS::ClientAPI::MasonComponent;
4 use vars qw( $cache $DEBUG $me );
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)),
67 @$argsref = ( %args );
72 '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
73 my( $custnum, $argsref ) = @_;
74 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
75 or return "unknown custnum $custnum";
77 my $pkgpart = $cust_main->agent->pkgpart_hashref;
79 #false laziness w/ edit/cust_main/first_pkg.html
80 my @first_svc = ( 'svc_acct', 'svc_phone' );
83 grep { $_->svcpart(\@first_svc)
84 && ( $pkgpart->{ $_->pkgpart }
85 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
88 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
90 my $conf = new FS::Conf;
91 if ( $conf->exists('pkg-addon_classnum') ) {
93 my %classnum = map { ( $_->addon_classnum => 1 ) }
94 grep { $_->freq !~ /^0/ }
96 $cust_main->ncancelled_pkgs;
98 unless ( $classnum{''} || ! keys %classnum ) {
99 @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
103 my %args = @$argsref;
104 $args{part_pkg} = \@part_pkg;
105 $args{first_svc} = \@first_svc;
106 $args{no_comment} = 1;
107 $args{label_callback} = sub { shift->pkg_comment };
108 @$argsref = ( %args );
113 '/elements/select-part_pkg.html' => sub {
114 my( $custnum, $argsref ) = @_;
115 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
116 or return "unknown custnum $custnum";
118 my $pkgpart = $cust_main->agent->pkgpart_hashref;
120 #false laziness w/ edit/cust_main/first_pkg.html
121 my @first_svc = ( 'svc_acct', 'svc_phone' );
124 grep { $pkgpart->{ $_->pkgpart }
125 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
127 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
129 push @$argsref, 'part_pkg' => \@part_pkg;
136 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
141 warn "$me mason_comp called on $packet\n" if $DEBUG;
143 my $comp = $packet->{'comp'};
144 unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
145 return { 'error' => 'Illegal component' };
148 my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
150 if ( $session_comps{$comp} ) {
152 my $session = _cache->get($packet->{'session_id'})
153 or return ( 'error' => "Can't resume session" ); #better error message
154 my $custnum = $session->{'custnum'};
156 my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
157 return { 'error' => $error } if $error;
161 my $conf = new FS::Conf;
162 $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
163 $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
164 $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
167 $fs_interp->exec($comp, @args); #only FS for now alas...
169 #errors? (turn off in-line error reporting?)
171 return { 'output' => $outbuf };
177 $cache ||= new FS::ClientAPI_SessionCache( {
178 'namespace' => 'FS::ClientAPI::MyAccount',