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
24 /misc/svc_acct-domains.cgi
25 /misc/part_svc-columns.cgi
26 /edit/elements/svc_forward.html
29 my %session_comps = map { $_=>1 } qw(
30 /elements/location.html
31 /elements/tr-amount_fee.html
32 /elements/select-part_pkg.html
33 /edit/cust_main/first_pkg/select-part_pkg.html
36 my %session_callbacks = (
38 '/elements/location.html' => sub {
39 my( $custnum, $argsref ) = @_;
40 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
41 or return "unknown custnum $custnum";
43 $args{object} = $cust_main->bill_location;
44 @$argsref = ( %args );
48 '/elements/tr-amount_fee.html' => sub {
49 my( $custnum, $argsref ) = @_;
51 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
52 or return "unknown custnum $custnum";
54 my $conf = new FS::Conf;
60 scalar($conf->config('selfservice_process-pkgpart', $cust_main->agentnum)),
61 'process-display' => scalar($conf->config('selfservice_process-display')),
62 'process-skip_first' => $conf->exists('selfservice_process-skip_first'),
63 'num_payments' => scalar($cust_main->cust_pay),
64 'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
66 @$argsref = ( %args );
71 '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
72 my( $custnum, $argsref ) = @_;
73 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
74 or return "unknown custnum $custnum";
76 my $pkgpart = $cust_main->agent->pkgpart_hashref;
78 #false laziness w/ edit/cust_main/first_pkg.html
79 my @first_svc = ( 'svc_acct', 'svc_phone' );
82 grep { $_->svcpart(\@first_svc)
83 && ( $pkgpart->{ $_->pkgpart }
84 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
87 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
89 my $conf = new FS::Conf;
90 if ( $conf->exists('pkg-addon_classnum') ) {
92 my %classnum = map { ( $_->addon_classnum => 1 ) }
93 grep { $_->freq !~ /^0/ }
95 $cust_main->ncancelled_pkgs;
97 unless ( $classnum{''} || ! keys %classnum ) {
98 @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
102 my %args = @$argsref;
103 $args{part_pkg} = \@part_pkg;
104 $args{first_svc} = \@first_svc;
105 $args{no_comment} = 1;
106 $args{label_callback} = sub { shift->pkg_comment };
107 @$argsref = ( %args );
112 '/elements/select-part_pkg.html' => sub {
113 my( $custnum, $argsref ) = @_;
114 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
115 or return "unknown custnum $custnum";
117 my $pkgpart = $cust_main->agent->pkgpart_hashref;
119 #false laziness w/ edit/cust_main/first_pkg.html
120 my @first_svc = ( 'svc_acct', 'svc_phone' );
123 grep { $pkgpart->{ $_->pkgpart }
124 || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
126 qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
128 push @$argsref, 'part_pkg' => \@part_pkg;
135 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
140 warn "$me mason_comp called on $packet\n" if $DEBUG;
142 my $comp = $packet->{'comp'};
143 unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
144 return { 'error' => 'Illegal component' };
147 my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
149 if ( $session_comps{$comp} ) {
151 my $session = _cache->get($packet->{'session_id'})
152 or return ( 'error' => "Can't resume session" ); #better error message
153 my $custnum = $session->{'custnum'};
155 my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
156 return { 'error' => $error } if $error;
160 my $conf = new FS::Conf;
161 $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
162 $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
163 $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
166 $fs_interp->exec($comp, @args); #only FS for now alas...
168 #errors? (turn off in-line error reporting?)
170 return { 'output' => $outbuf };
176 $cache ||= new FS::ClientAPI_SessionCache( {
177 'namespace' => 'FS::ClientAPI::MyAccount',