add some debugging control
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 % if ( $error ) {
2 %   $cgi->param('error', $error);
3 %
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
5 %
6 % } else { 
7 %
8 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
9 %
10 % }
11 <%once>
12
13 my $me = '[edit/process/cust_main.cgi]';
14 my $DEBUG = 0;
15
16 </%once>
17 <%init>
18
19 my $error = '';
20
21 #unmunge stuff
22
23 $cgi->param('tax','') unless defined $cgi->param('tax');
24
25 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
26
27 #my $payby = $cgi->param('payby');
28 my $payby = $cgi->param('select'); # XXX key
29
30 my %noauto = (
31   'CARD' => 'DCRD',
32   'CHEK' => 'DCHK',
33 );
34 $payby = $noauto{$payby}
35   if ! $cgi->param('payauto') && exists $noauto{$payby};
36
37 $cgi->param('payby', $payby);
38
39 if ( $payby ) {
40   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
41     $cgi->param('payinfo',
42       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
43   }
44   $cgi->param('paydate',
45     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
46 }
47
48 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
49 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
50 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
51 $cgi->param('invoicing_list', join(',', @invoicing_list) );
52
53
54 #create new record object
55
56 my $new = new FS::cust_main ( {
57   map {
58     $_, scalar($cgi->param($_))
59 #  } qw(custnum agentnum last first ss company address1 address2 city county
60 #       state zip daytime night fax payby payinfo paydate payname tax
61 #       otaker refnum)
62   } fields('cust_main')
63 } );
64
65  delete( $new->hashref->{'agent_custid'} )
66    unless $new->hashref->{'agent_custid'};
67
68 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
69   $new->setfield("ship_$_", '') foreach qw(
70     last first company address1 address2 city county state zip
71     country daytime night fax
72   );
73 }
74
75 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
76   my $conf = new FS::Conf;
77   my $format = $conf->config('date_format') || "%m/%d/%Y";
78   my $parser = DateTime::Format::Strptime->new(pattern => $format,
79                                                time_zone => 'floating',
80                                               );
81   my $dt =  $parser->parse_datetime($1);
82   if ($dt) {
83     $new->setfield('birthdate', $dt->epoch);
84     $cgi->param('birthdate', $dt->epoch);
85   } else {
86 #    $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
87     $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
88     $cgi->param('birthdate', '');
89   }
90 }
91
92 $new->setfield('paid', $cgi->param('paid') )
93   if $cgi->param('paid');
94
95 #perhaps this stuff should go to cust_main.pm
96 my $cust_pkg = '';
97 my $svc_acct = '';
98 if ( $new->custnum eq '' ) {
99
100   if ( $cgi->param('pkgpart_svcpart') ) {
101     my $x = $cgi->param('pkgpart_svcpart');
102     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
103     my($pkgpart, $svcpart) = ($1, $2);
104     #false laziness: copied from FS::cust_pkg::order (which should become a
105     #FS::cust_main method)
106     my(%part_pkg);
107     # generate %part_pkg
108     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
109     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
110         #my($type_pkgs);
111         #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
112         #  my($pkgpart)=$type_pkgs->pkgpart;
113         #  $part_pkg{$pkgpart}++;
114         #}
115     # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
116     my $pkgpart_href = $agent->pkgpart_hashref;
117     #eslaf
118
119     # this should wind up in FS::cust_pkg!
120     $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
121                "purchase pkgpart ". $pkgpart
122       #unless $part_pkg{ $pkgpart };
123       unless $pkgpart_href->{ $pkgpart };
124
125     $cust_pkg = new FS::cust_pkg ( {
126       #later         'custnum' => $custnum,
127       'pkgpart' => $pkgpart,
128     } );
129     #$error ||= $cust_pkg->check;
130
131     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
132
133     #$error ||= $cust_svc->check;
134
135     my %svc_acct = (
136                      'svcpart'   => $svcpart,
137                      'username'  => $cgi->param('username'),
138                      '_password' => $cgi->param('_password'),
139                      'popnum'    => $cgi->param('popnum'),
140                    );
141     $svc_acct{'domsvc'} = $cgi->param('domsvc')
142       if $cgi->param('domsvc');
143
144     $svc_acct = new FS::svc_acct \%svc_acct;
145
146     #and just in case you were silly
147     $svc_acct->svcpart($svcpart);
148     $svc_acct->username($cgi->param('username'));
149     $svc_acct->_password($cgi->param('_password'));
150     $svc_acct->popnum($cgi->param('popnum'));
151
152     #$error ||= $svc_acct->check;
153
154   } elsif ( $cgi->param('username') ) { #good thing to catch
155     $error = "Can't assign username without a package!";
156   }
157
158   use Tie::RefHash;
159   tie my %hash, 'Tie::RefHash';
160   %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
161   $error ||= $new->insert( \%hash, \@invoicing_list );
162
163   my $conf = new FS::Conf;
164   if ( $conf->exists('backend-realtime') && ! $error ) {
165
166     my $berror =    $new->bill
167                  || $new->apply_payments_and_credits
168                  || $new->collect( 'realtime' => 1 );
169     warn "Warning, error billing during backend-realtime: $berror" if $berror;
170
171   }
172   
173 } else { #create old record object
174
175   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
176   $error ||= "Old record not found!" unless $old;
177   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
178     $new->paycvv($old->paycvv);
179   }
180   if ($new->ss =~ /xx/) {
181     $new->ss($old->ss);
182   }
183   if ($new->stateid =~ /^xxx/) {
184     $new->stateid($old->stateid);
185   }
186   if ($new->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
187     $new->payinfo($old->payinfo);
188   }
189
190   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
191   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
192   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
193
194   $error ||= $new->replace($old, \@invoicing_list);
195
196   warn "$me returned from replace" if $DEBUG;
197   
198 }
199
200 </%init>