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