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