REST API, RT#28181
[freeside.git] / httemplate / REST / 1.0 / cust_main
1 <% encode_rest($return) %>\
2 <%init>
3
4 rest_auth($cgi);
5
6 my( $custnum, $command ) = split('/', rest_uri_remain($r, $m), 2 );
7
8 if ( $r->method eq 'GET' ) {
9
10   my $return = [];
11
12   if ( $custnum ) {
13
14     my $cust_main = qsearchs('cust_main', { 'custnum'=>$custnum } )
15       or die "unknown custnum $custnum";
16
17     if ( $command eq '' ) {
18
19       $return = $cust_main->API_getinfo;
20
21     } elsif ( $command =~ /^(cust_(pkg|attachment|bill|pay))$/ ) {
22
23       my $method = $1;
24
25       $return = [ map $_->API_getinfo, $cust_main->$method ];
26
27     } elsif ( $command eq 'part_pkg' ) {
28
29       my %pkgpart = map { $_->pkgpart => 1 } $cust_main->cust_pkg;
30
31       $return = [ map $_->API_getinfo,
32                     map qsearchs('part_pkg', { 'pkgpart'=>$_ }),
33                       keys %pkgpart;
34                 ];
35
36     }
37
38   } else { #list
39
40     my %hash = ( map { $_ => scalar($cgi->param($_)) }
41                    qw( agentnum salesnum refnum classnum usernum
42                        referral_custnum
43                      )
44                );
45  
46     my $extra_sql = '';
47     if ( $cgi->param('cust_main_invoice_dest') ) {
48       my $dest = dbh->quote(scalar($cgi->param('cust_main_invoice_dest')));
49       $extra_sql = "
50         WHERE EXISTS ( SELECT 1 FROM cust_main_invoice
51                          WHERE cust_main.custnum = cust_main_invoice.custnum
52                            AND dest = $dest
53                      )
54       ";
55     } elsif ( $cgi->param('cust_main_invoice_dest_substring') ) {
56       my $dest = dbh->quote('%'. scalar($cgi->param('cust_main_invoice_dest_substring')). '%');
57       $extra_sql = "
58         WHERE EXISTS ( SELECT 1 FROM cust_main_invoice
59                          WHERE cust_main.custnum = cust_main_invoice.custnum
60                            AND dest ILIKE $dest
61                      )
62       ";
63     }
64
65     my @cust_main = qsearch({
66       'table'     => 'cust_main',
67       'hashref'   =>  \%hash,
68       'extra_sql' => $extra_sql;
69     });
70
71     $return = [ map $_->API_getinfo, @cust_main ];
72
73   }
74
75 } elsif ( $r->method eq 'POST' ) { #create new
76
77   if ( !$custnum && $command eq '' ) {
78
79     my @param = grep { $_ ne 'secret' } $cgi->param;
80
81     $return = FS::cust_main->API_insert(
82       map { $_ => scalar($cgi->param($_)) } @param
83         #qw(
84         #  agentnum refnum agent_custid referral_custnum
85         #  last first company daytime night fax mobile
86         #  invoicing_list postal_invoicing
87         #  payby payinfo paydate paycvv payname
88
89         #  address1 address2 city county state zip country
90         #  ship_company ship_address1 ship_address2 ship_city ship_county
91         #    ship_state ship_zip ship_country
92         #)
93     );
94
95     if ( $return->{error} ) {
96       #XXX RESTful error handline
97       die $return->{error};
98     } elsif ( $return->{custnum} ) {
99       # Return a 201 Status code and the newly created id
100       my $custnum = $return->{custnum};
101       #$r->headers_out('Location' => );
102       #$m->abort(201);
103       $m->redirect( $r->uri."/$custnum", 201);
104     } else {
105       #XXX RESTful exception handling (cust_main->API_insert didn't even behave like we expect!?)
106       die 'guru meditation #159';
107     }
108
109   } elsif ( $custnum && $command eq 'cust_pkg' ) {
110
111     #XXX this needs to order a package, not just insert a record :/
112
113   # #XXX does this need to do anything special?  what's a "wallet payment"?
114   # } elsif ( $custnum && $command eq 'cust_pay' ) {
115
116   } elsif ( $custnum && $command =~ /^(cust_(attachment|pay))$/ ) {
117
118     my $table = $1;
119     my $class = 'FS::'.$table;
120
121     my @param = grep { $_ ne 'secret' } $cgi->param;
122
123     my $return =
124       $class->API_insert( 'custnum' => $custnum, 
125                           map { $_ => scalar($cgi->param($_)) } @param
126                         );
127
128     my $pkey = FS::Record->dbdef_table->$table->primary_key;
129
130     if ( $return->{error} ) {
131       #XXX RESTful error handline
132       die $return->{error};
133     } elsif ( $return->{$pkey} ) {
134       # Return a 201 Status code and the newly created id
135       my $pkey_value = $return->{$pkey};
136       #$r->headers_out('Location' => );
137       #$m->abort(201);
138       $m->redirect( $r->uri."/$pkey_value", 201);
139     } else {
140       #XXX RESTful exception handling (cust_main->API_insert didn't even behave like we expect!?)
141       die 'guru meditation #160';
142     }
143
144   }
145
146 } elsif ( $r->method eq 'PUT' ) { #modify
147
148 }
149
150 </%init>