order_pkg doc
[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   /edit/cust_main/first_pkg/select-part_pkg.html
31 );
32
33 my %session_callbacks = (
34
35   '/elements/location.html' => sub {
36     my( $custnum, $argsref ) = @_;
37     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
38       or return "unknown custnum $custnum";
39     my %args = @$argsref;
40     $args{object} = $cust_main->bill_location;
41     @$argsref = ( %args );
42     return ''; #no error
43   },
44
45   '/elements/tr-amount_fee.html' => sub {
46     my( $custnum, $argsref ) = @_;
47
48     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
49       or return "unknown custnum $custnum";
50
51     my $conf = new FS::Conf;
52
53     my %args = @$argsref;
54     %args = (
55       %args,
56       'process-pkgpart'    =>
57         scalar($conf->config('selfservice_process-pkgpart', $cust_main->agentnum)),
58       'process-display'    => scalar($conf->config('selfservice_process-display')),
59       'process-skip_first' => $conf->exists('selfservice_process-skip_first'),
60       'num_payments'       => scalar($cust_main->cust_pay), 
61       'surcharge_percentage' => scalar($conf->config('credit-card-surcharge-percentage')),
62     );
63     @$argsref = ( %args );
64
65     return ''; #no error
66   },
67
68   '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
69     my( $custnum, $argsref ) = @_;
70     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
71       or return "unknown custnum $custnum";
72
73     my $pkgpart = $cust_main->agent->pkgpart_hashref;
74
75     #false laziness w/ edit/cust_main/first_pkg.html
76     my @first_svc = ( 'svc_acct', 'svc_phone' );
77
78     my @part_pkg =
79       grep { $_->svcpart(\@first_svc)
80              && ( $pkgpart->{ $_->pkgpart } 
81                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
82                 )
83            }
84       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
85
86     my $conf = new FS::Conf;
87     if ( $conf->exists('pkg-addon_classnum') ) {
88
89       my %classnum = map  { ( $_->addon_classnum => 1 ) }
90                      grep { $_->freq !~ /^0/ }
91                      map  { $_->part_pkg }
92                           $cust_main->ncancelled_pkgs;
93
94       unless ( $classnum{''} || ! keys %classnum ) {
95         @part_pkg = grep $classnum{ $_->classnum }, @part_pkg;
96       }
97     }
98
99     my %args = @$argsref;
100     $args{part_pkg} = \@part_pkg;
101     $args{first_svc} = \@first_svc;
102     $args{no_comment} = 1;
103     $args{label_callback} = sub { shift->pkg_comment };
104     @$argsref = ( %args );
105     return ''; #no error
106
107   },
108
109 );
110
111 my $outbuf;
112 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
113
114 sub mason_comp {
115   my $packet = shift;
116
117   warn "$me mason_comp called on $packet\n" if $DEBUG;
118
119   my $comp = $packet->{'comp'};
120   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
121     return { 'error' => 'Illegal component' };
122   }
123
124   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
125
126   if ( $session_comps{$comp} ) {
127
128     my $session = _cache->get($packet->{'session_id'})
129       or return ( 'error' => "Can't resume session" ); #better error message
130     my $custnum = $session->{'custnum'};
131
132     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
133     return { 'error' => $error } if $error;
134
135   }
136
137   my $conf = new FS::Conf;
138   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
139   $FS::Mason::Request::FSURL .= '/' unless $FS::Mason::Request::FSURL =~ /\/$/;
140   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
141
142   $outbuf = '';
143   $fs_interp->exec($comp, @args); #only FS for now alas...
144
145   #errors? (turn off in-line error reporting?)
146
147   return { 'output' => $outbuf };
148
149 }
150
151 #hmm
152 sub _cache {
153   $cache ||= new FS::ClientAPI_SessionCache( {
154                'namespace' => 'FS::ClientAPI::MyAccount',
155              } );
156 }
157
158 1;