allow empty custnum in sub check (but call that an error in sub insert),
[freeside.git] / htdocs / edit / process / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.7 1999-02-28 00:03:42 ivan Exp $
4 #
5 # Usage: post form to:
6 #        http://server.name/path/cust_main.cgi
7 #
8 # ivan@voicenet.com 96-dec-04
9 #
10 # added referral check
11 # ivan@voicenet.com 97-jun-4
12 #
13 # rewrote for new API
14 # ivan@voicenet.com 97-jul-28
15 #
16 # same as above (again) and clean up some stuff ivan@sisd.com 98-feb-23
17 #
18 # Changes to allow page to work at a relative position in server
19 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
20 #       bmccane@maxbaud.net     98-apr-3
21 #
22 # $Log: cust_main.cgi,v $
23 # Revision 1.7  1999-02-28 00:03:42  ivan
24 # removed misleading comments
25 #
26 # Revision 1.6  1999/01/25 12:10:00  ivan
27 # yet more mod_perl stuff
28 #
29 # Revision 1.5  1999/01/19 05:13:50  ivan
30 # for mod_perl: no more top-level my() variables; use vars instead
31 # also the last s/create/new/;
32 #
33 # Revision 1.4  1999/01/18 09:22:32  ivan
34 # changes to track email addresses for email invoicing
35 #
36 # Revision 1.3  1998/12/17 08:40:19  ivan
37 # s/CGI::Request/CGI.pm/; etc
38 #
39 # Revision 1.2  1998/11/18 08:57:36  ivan
40 # i18n, s/CGI-modules/CGI.pm/, FS::CGI::idiot instead of inline, FS::CGI::popurl
41 #
42
43 use strict;
44 use vars qw( $cgi $payby @invoicing_list $new $custnum $error );
45 use CGI;
46 use CGI::Carp qw(fatalsToBrowser);
47 use FS::UID qw(cgisuidsetup getotaker);
48 use FS::CGI qw( popurl );
49 use FS::Record qw(qsearchs fields);
50 use FS::cust_main;
51
52 $cgi = new CGI;
53 &cgisuidsetup($cgi);
54
55 #unmunge stuff
56
57 $cgi->param('tax','') unless defined($cgi->param('tax'));
58
59 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
60
61 $cgi->param('state') =~ /^(\w+)( \((\w+)\))? \/ (\w+)$/;
62 $cgi->param('state', $1);
63 $cgi->param('county', $3 || '');
64 $cgi->param('country', $4);
65
66 if ( $payby = $cgi->param('payby') ) {
67   $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
68   $cgi->param('paydate',
69   $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
70   $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
71 }
72
73 $cgi->param('otaker', &getotaker );
74
75 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
76 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
77
78 #create new record object
79
80 $new = new FS::cust_main ( {
81   map {
82     $_, scalar($cgi->param($_))
83 #  } qw(custnum agentnum last first ss company address1 address2 city county
84 #       state zip daytime night fax payby payinfo paydate payname tax
85 #       otaker refnum)
86   } fields('cust_main')
87 } );
88
89 $error = $new->check_invoicing_list( \@invoicing_list );
90
91 #perhaps the invocing_list magic should move to cust_main.pm?
92 if ( $new->custnum eq '' ) {
93   #false laziness: copied from cust_pkg.pm
94   HERE!
95   #
96   $error ||= $new->insert;
97 } else { #create old record object
98   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
99   $error ||= "Old record not found!" unless $old;
100   $error ||= $new->replace($old);
101 }
102
103 if ( $error ) {
104   $cgi->param('error', $error);
105   print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
106 } else { 
107   $new->invoicing_list( \@invoicing_list );
108   $custnum = $new->custnum;
109   print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum#cust_main");
110