89c558cc21bcda170d4e98a710fa6157b7b8a137
[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 } elsif ( $r->method eq 'PUT' ) { #modify
78
79 }
80
81 </%init>