Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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/select-did.html
18   /misc/areacodes.cgi
19   /misc/exchanges.cgi
20   /misc/phonenums.cgi
21   /misc/states.cgi
22   /misc/counties.cgi
23   /misc/svc_acct-domains.cgi
24   /misc/part_svc-columns.cgi
25 );
26
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
32 );
33
34 my %session_callbacks = (
35
36   '/elements/location.html' => sub {
37     my( $custnum, $argsref ) = @_;
38     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
39       or return "unknown custnum $custnum";
40     my %args = @$argsref;
41     $args{object} = $cust_main->bill_location;
42     @$argsref = ( %args );
43     return ''; #no error
44   },
45
46   '/elements/tr-amount_fee.html' => sub {
47     my( $custnum, $argsref ) = @_;
48
49     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
50       or return "unknown custnum $custnum";
51
52     my $conf = new FS::Conf;
53
54     my %args = @$argsref;
55     %args = (
56       %args,
57       'process-pkgpart'    =>
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')),
63     );
64     @$argsref = ( %args );
65
66     return ''; #no error
67   },
68
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";
73
74     my $pkgpart = $cust_main->agent->pkgpart_hashref;
75
76     #false laziness w/ edit/cust_main/first_pkg.html
77     my @first_svc = ( 'svc_acct', 'svc_phone' );
78
79     my @part_pkg =
80       grep { $_->svcpart(\@first_svc)
81              && ( $pkgpart->{ $_->pkgpart } 
82                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
83                 )
84            }
85       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
86
87     my $conf = new FS::Conf;
88     if ( $conf->exists('pkg-addon_classnum') ) {
89
90       my %classnum = map  { ( $_->addon_classnum => 1 ) }
91                      grep { $_->freq !~ /^0/ }
92                      map  { $_->part_pkg }
93                           $cust_main->ncancelled_pkgs;
94
95       unless ( $classnum{''} || ! keys %classnum ) {
96         @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
97       }
98     }
99
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 );
106     return ''; #no error
107
108   },
109
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";
114
115     my $pkgpart = $cust_main->agent->pkgpart_hashref;
116
117     #false laziness w/ edit/cust_main/first_pkg.html
118     my @first_svc = ( 'svc_acct', 'svc_phone' );
119
120     my @part_pkg =
121       grep { $pkgpart->{ $_->pkgpart } 
122                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
123            }
124       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
125
126     push @$argsref, 'part_pkg' =>  \@part_pkg;
127     '';
128   },
129
130 );
131
132 my $outbuf;
133 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
134
135 sub mason_comp {
136   my $packet = shift;
137
138   warn "$me mason_comp called on $packet\n" if $DEBUG;
139
140   my $comp = $packet->{'comp'};
141   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
142     return { 'error' => 'Illegal component' };
143   }
144
145   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
146
147   if ( $session_comps{$comp} ) {
148
149     my $session = _cache->get($packet->{'session_id'})
150       or return ( 'error' => "Can't resume session" ); #better error message
151     my $custnum = $session->{'custnum'};
152
153     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
154     return { 'error' => $error } if $error;
155
156   }
157
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'} || '';
162
163   $outbuf = '';
164   $fs_interp->exec($comp, @args); #only FS for now alas...
165
166   #errors? (turn off in-line error reporting?)
167
168   return { 'output' => $outbuf };
169
170 }
171
172 #hmm
173 sub _cache {
174   $cache ||= new FS::ClientAPI_SessionCache( {
175                'namespace' => 'FS::ClientAPI::MyAccount',
176              } );
177 }
178
179 1;