This commit was generated by cvs2svn to compensate for changes in r4888,
[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 %if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
50 %  $new->setfield("ship_$_", '') foreach qw(
51 %    last first company address1 address2 city county state zip
52 %    country daytime night fax
53 %  );
54 %}
55 %
56 %if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/ ) {
57 %  $new->setfield('birthdate', str2time($1));
58 %}
59 %
60 %$new->setfield('paid', $cgi->param('paid') )
61 %  if $cgi->param('paid');
62 %
63 %#perhaps this stuff should go to cust_main.pm
64 %my $cust_pkg = '';
65 %my $svc_acct = '';
66 %if ( $new->custnum eq '' ) {
67 %
68 %  if ( $cgi->param('pkgpart_svcpart') ) {
69 %    my $x = $cgi->param('pkgpart_svcpart');
70 %    $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
71 %    my($pkgpart, $svcpart) = ($1, $2);
72 %    #false laziness: copied from FS::cust_pkg::order (which should become a
73 %    #FS::cust_main method)
74 %    my(%part_pkg);
75 %    # generate %part_pkg
76 %    # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
77 %    my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
78 %       #my($type_pkgs);
79 %       #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
80 %       #  my($pkgpart)=$type_pkgs->pkgpart;
81 %       #  $part_pkg{$pkgpart}++;
82 %       #}
83 %    # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
84 %    my $pkgpart_href = $agent->pkgpart_hashref;
85 %    #eslaf
86 %
87 %    # this should wind up in FS::cust_pkg!
88 %    $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
89 %               "purchase pkgpart ". $pkgpart
90 %      #unless $part_pkg{ $pkgpart };
91 %      unless $pkgpart_href->{ $pkgpart };
92 %
93 %    $cust_pkg = new FS::cust_pkg ( {
94 %      #later         'custnum' => $custnum,
95 %      'pkgpart' => $pkgpart,
96 %    } );
97 %    #$error ||= $cust_pkg->check;
98 %
99 %    #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
100 %
101 %    #$error ||= $cust_svc->check;
102 %
103 %    $svc_acct = new FS::svc_acct ( {
104 %                                     'svcpart'   => $svcpart,
105 %                                     'username'  => $cgi->param('username'),
106 %                                     '_password' => $cgi->param('_password'),
107 %                                     'popnum'    => $cgi->param('popnum'),
108 %                                   } );
109 %
110 %    #and just in case you were silly
111 %    $svc_acct->svcpart($svcpart);
112 %    $svc_acct->username($cgi->param('username'));
113 %    $svc_acct->_password($cgi->param('_password'));
114 %    $svc_acct->popnum($cgi->param('popnum'));
115 %
116 %    #$error ||= $svc_acct->check;
117 %
118 %  } elsif ( $cgi->param('username') ) { #good thing to catch
119 %    $error = "Can't assign username without a package!";
120 %  }
121 %
122 %  use Tie::RefHash;
123 %  tie my %hash, 'Tie::RefHash';
124 %  %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
125 %  $error ||= $new->insert( \%hash, \@invoicing_list );
126 %
127 %  my $conf = new FS::Conf;
128 %  if ( $conf->exists('backend-realtime') && ! $error ) {
129 %
130 %    my $berror = $new->bill;
131 %    $new->apply_payments;
132 %    $new->apply_credits;
133 %    $berror ||= $new->collect( 'realtime' => 1 );
134 %    warn "Warning, error billing during backend-realtime: $berror" if $berror;
135 %
136 %  }
137 %  
138 %} else { #create old record object
139 %
140 %  my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
141 %  $error ||= "Old record not found!" unless $old;
142 %  if ( defined dbdef->table('cust_main')->column('paycvv')
143 %       && length($old->paycvv)
144 %       && $new->paycvv =~ /^\s*\*+\s*$/ ) {
145 %    $new->paycvv($old->paycvv);
146 %  }
147 %  $error ||= $new->replace($old, \@invoicing_list);
148 %  
149 %}
150 %
151 %if ( $error ) {
152 %  $cgi->param('error', $error);
153 %  print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
154 %} else { 
155 %  print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum);
156 %} 
157 %
158
159