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/select-did.html
23 /misc/svc_acct-domains.cgi
24 /misc/part_svc-columns.cgi
27 my %session_comps = map { $_=>1 } qw(
28 /elements/location.html
29 /elements/tr-amount_fee.html
30 /elements/select-part_pkg.html
31 /edit/cust_main/first_pkg/select-part_pkg.html
34 my %session_callbacks = (
36 '/elements/location.html' => sub {
37 my( $custnum, $argsref ) = @_;
38 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
39 or return "unknown custnum $custnum";
41 $args{object} = $cust_main->bill_location;
42 @$argsref = ( %args );
46 '/elements/tr-amount_fee.html' => sub {
47 my( $custnum, $argsref ) = @_;
49 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
50 or return "unknown custnum $custnum";
52 my $conf = new FS::Conf;
58 scalar($conf->config('selfservice_process-pkgpart', $cust_main->agentnum)),
59 'process-display' => scalar($conf->config('selfservice_process-display')),
60 'process-skip_first' => $conf->exists('selfservice_process-skip_first'),
61 'num_payments' => scalar($cust_main->cust_pay),
62 'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
64 @$argsref = ( %args );
69 '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
70 my( $custnum, $argsref ) = @_;
71 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
72 or return "unknown custnum $custnum";
74 my $pkgpart = $cust_main->agent->pkgpart_hashref;
76 #false laziness w/ edit/cust_main/first_pkg.html
77 my @first_svc = ( 'svc_acct', 'svc_phone' );
80 grep { $_->svcpart(\@first_svc)
81 && ( $pkgpart->{ $_->pkgpart }
82 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
85 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
87 my $conf = new FS::Conf;
88 if ( $conf->exists('pkg-addon_classnum') ) {
90 my %classnum = map { ( $_->addon_classnum => 1 ) }
91 grep { $_->freq !~ /^0/ }
93 $cust_main->ncancelled_pkgs;
95 unless ( $classnum{''} || ! keys %classnum ) {
96 @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
100 my %args = @$argsref;
101 $args{part_pkg} = \@part_pkg;
102 $args{first_svc} = \@first_svc;
103 $args{no_comment} = 1;
104 $args{label_callback} = sub { shift->pkg_comment };
105 @$argsref = ( %args );
110 '/elements/select-part_pkg.html' => sub {
111 my( $custnum, $argsref ) = @_;
112 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
113 or return "unknown custnum $custnum";
115 my $pkgpart = $cust_main->agent->pkgpart_hashref;
117 #false laziness w/ edit/cust_main/first_pkg.html
118 my @first_svc = ( 'svc_acct', 'svc_phone' );
121 grep { $pkgpart->{ $_->pkgpart }
122 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
124 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
126 push @$argsref, 'part_pkg' => \@part_pkg;
133 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
138 warn "$me mason_comp called on $packet\n" if $DEBUG;
140 my $comp = $packet->{'comp'};
141 unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
142 return { 'error' => 'Illegal component' };
145 my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
147 if ( $session_comps{$comp} ) {
149 my $session = _cache->get($packet->{'session_id'})
150 or return ( 'error' => "Can't resume session" ); #better error message
151 my $custnum = $session->{'custnum'};
153 my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
154 return { 'error' => $error } if $error;
158 my $conf = new FS::Conf;
159 $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
160 $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
161 $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
164 $fs_interp->exec($comp, @args); #only FS for now alas...
166 #errors? (turn off in-line error reporting?)
168 return { 'output' => $outbuf };
174 $cache ||= new FS::ClientAPI_SessionCache( {
175 'namespace' => 'FS::ClientAPI::MyAccount',