RT# 82949 - changes section name from fees to pricing, better opiton
[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_contact
51                          JOIN contact USING (contactnum)
52                          JOIN contact_email USING (contactnum)
53                          WHERE cust_main.custnum = cust_contact.custnum
54                            AND cust_contact.invoice_dest = 'Y'
55                            AND contact_email.emailaddress = $dest
56                      )
57       ";
58     } elsif ( $cgi->param('cust_main_invoice_dest_substring') ) {
59       my $dest = dbh->quote('%'. scalar($cgi->param('cust_main_invoice_dest_substring')). '%');
60       $extra_sql = "
61         WHERE EXISTS ( SELECT 1 FROM cust_contact
62                          JOIN contact USING (contactnum)
63                          JOIN contact_email USING (contactnum)
64                          WHERE cust_main.custnum = cust_contact.custnum
65                            AND cust_contact.invoice_dest = 'Y'
66                            AND contact_email.emailaddress ILIKE $dest
67                      )
68       ";
69     }
70
71     my @cust_main = qsearch({
72       'table'     => 'cust_main',
73       'hashref'   =>  \%hash,
74       'extra_sql' => $extra_sql;
75     });
76
77     $return = [ map $_->API_getinfo, @cust_main ];
78
79   }
80
81 } elsif ( $r->method eq 'POST' ) { #create new
82
83   if ( !$custnum && $command eq '' ) {
84
85     my @param = grep { $_ ne 'secret' } $cgi->param;
86
87     $return = FS::cust_main->API_insert(
88       map { $_ => scalar($cgi->param($_)) } @param
89         #qw(
90         #  agentnum refnum agent_custid referral_custnum
91         #  last first company daytime night fax mobile
92         #  invoicing_list postal_invoicing
93         #  payby payinfo paydate paycvv payname
94
95         #  address1 address2 city county state zip country
96         #  ship_company ship_address1 ship_address2 ship_city ship_county
97         #    ship_state ship_zip ship_country
98         #)
99     );
100
101     if ( $return->{error} ) {
102       #XXX RESTful error handline
103       die $return->{error};
104     } elsif ( $return->{custnum} ) {
105       # Return a 201 Status code and the newly created id
106       my $custnum = $return->{custnum};
107       #$r->headers_out('Location' => );
108       #$m->abort(201);
109       $m->redirect( $r->uri."/$custnum", 201);
110     } else {
111       #XXX RESTful exception handling (cust_main->API_insert didn't even behave like we expect!?)
112       die 'guru meditation #159';
113     }
114
115   } elsif ( $custnum && $command eq 'cust_pkg' ) {
116
117     #XXX this needs to order a package, not just insert a record :/
118
119   # #XXX does this need to do anything special?  what's a "wallet payment"?
120   # } elsif ( $custnum && $command eq 'cust_pay' ) {
121
122   } elsif ( $custnum && $command =~ /^(cust_(attachment|pay))$/ ) {
123
124     my $table = $1;
125     my $class = 'FS::'.$table;
126
127     my @param = grep { $_ ne 'secret' } $cgi->param;
128
129     my $return =
130       $class->API_insert( 'custnum' => $custnum, 
131                           map { $_ => scalar($cgi->param($_)) } @param
132                         );
133
134     my $pkey = FS::Record->dbdef_table->$table->primary_key;
135
136     if ( $return->{error} ) {
137       #XXX RESTful error handline
138       die $return->{error};
139     } elsif ( $return->{$pkey} ) {
140       # Return a 201 Status code and the newly created id
141       my $pkey_value = $return->{$pkey};
142       #$r->headers_out('Location' => );
143       #$m->abort(201);
144       $m->redirect( $r->uri."/$pkey_value", 201);
145     } else {
146       #XXX RESTful exception handling (cust_main->API_insert didn't even behave like we expect!?)
147       die 'guru meditation #160';
148     }
149
150   }
151
152 } elsif ( $r->method eq 'PUT' ) { #modify
153
154 }
155
156 </%init>