tax exemption by tax name, RT#5127
[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 die "access denied"
20   unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
21
22 my $conf = new FS::Conf;
23
24 my $error = '';
25
26 #unmunge stuff
27
28 $cgi->param('tax','') unless defined $cgi->param('tax');
29
30 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
31
32 my $payby = $cgi->param('payby');
33
34 my %noauto = (
35   'CARD' => 'DCRD',
36   'CHEK' => 'DCHK',
37 );
38 $payby = $noauto{$payby}
39   if ! $cgi->param('payauto') && exists $noauto{$payby};
40
41 $cgi->param('payby', $payby);
42
43 if ( $payby ) {
44   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
45     $cgi->param('payinfo',
46       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
47   }
48   $cgi->param('paydate',
49     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
50 }
51
52 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
53 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
54 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
55 $cgi->param('invoicing_list', join(',', @invoicing_list) );
56
57
58 #create new record object
59
60 my $new = new FS::cust_main ( {
61   map {
62     $_, scalar($cgi->param($_))
63 #  } qw(custnum agentnum last first ss company address1 address2 city county
64 #       state zip daytime night fax payby payinfo paydate payname tax
65 #       otaker refnum)
66   } fields('cust_main')
67 } );
68
69 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
70   $new->setfield("ship_$_", '') foreach qw(
71     last first company address1 address2 city county state zip
72     country daytime night fax
73   );
74 }
75
76 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
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 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
96 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
97
98 #perhaps this stuff should go to cust_main.pm
99 if ( $new->custnum eq '' ) {
100
101   my $cust_pkg = '';
102   my $svc;
103
104   if ( $cgi->param('pkgpart_svcpart') ) {
105
106     my $x = $cgi->param('pkgpart_svcpart');
107     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
108     my($pkgpart, $svcpart) = ($1, $2);
109     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
110     #false laziness: copied from FS::cust_pkg::order (which should become a
111     #FS::cust_main method)
112     my(%part_pkg);
113     # generate %part_pkg
114     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
115     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
116
117     if ( $agent ) {
118       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
119       my $pkgpart_href = $agent->pkgpart_hashref
120         if $agent;
121       #eslaf
122
123       # this should wind up in FS::cust_pkg!
124       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
125                  ") can't purchase pkgpart ". $pkgpart
126         #unless $part_pkg{ $pkgpart };
127         unless $pkgpart_href->{ $pkgpart }
128             || $agent->agentnum == $part_pkg->agentnum;
129     } else {
130       $error = 'Select agent';
131     }
132
133     $cust_pkg = new FS::cust_pkg ( {
134       #later         'custnum' => $custnum,
135       'pkgpart' => $pkgpart,
136     } );
137     #$error ||= $cust_pkg->check;
138
139     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
140
141     #$error ||= $cust_svc->check;
142
143     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
144     my $svcdb = $part_svc->svcdb;
145
146     if ( $svcdb eq 'svc_acct' ) {
147
148       my %svc_acct = (
149                        'svcpart'   => $svcpart,
150                        'username'  => scalar($cgi->param('username')),
151                        '_password' => scalar($cgi->param('_password')),
152                        'popnum'    => scalar($cgi->param('popnum')),
153                      );
154       $svc_acct{'domsvc'} = $cgi->param('domsvc')
155         if $cgi->param('domsvc');
156
157       $svc = new FS::svc_acct \%svc_acct;
158
159       #and just in case you were silly
160       $svc->svcpart($svcpart);
161       $svc->username($cgi->param('username'));
162       $svc->_password($cgi->param('_password'));
163       $svc->popnum($cgi->param('popnum'));
164
165     } elsif ( $svcdb eq 'svc_phone' ) {
166
167       my %svc_phone = (
168                         'svcpart' => $svcpart,
169                         map { $_ => scalar($cgi->param($_)) }
170                           qw( countrycode phonenum sip_password pin phone_name )
171                       );
172
173       $svc = new FS::svc_phone \%svc_phone;
174
175     } else {
176       die "$svcdb not handled on new customer yet";
177     }
178
179     #$error ||= $svc_acct->check;
180
181   }
182
183   use Tie::RefHash;
184   tie my %hash, 'Tie::RefHash';
185   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
186   $error ||= $new->insert( \%hash, \@invoicing_list,
187                            'tax_exemption' => \@tax_exempt,
188                          );
189
190   my $conf = new FS::Conf;
191   if ( $conf->exists('backend-realtime') && ! $error ) {
192
193     my $berror =    $new->bill
194                  || $new->apply_payments_and_credits
195                  || $new->collect( 'realtime' => 1 );
196     warn "Warning, error billing during backend-realtime: $berror" if $berror;
197
198   }
199   
200 } else { #create old record object
201
202   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
203   $error ||= "Old record not found!" unless $old;
204   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
205     $new->paycvv($old->paycvv);
206   }
207   if ($new->ss =~ /xx/) {
208     $new->ss($old->ss);
209   }
210   if ($new->stateid =~ /^xxx/) {
211     $new->stateid($old->stateid);
212   }
213   if ($new->payby =~ /^(CARD|DCRD)$/ && $new->payinfo =~ /xx/) {
214     $new->payinfo($old->payinfo);
215   } elsif ($new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
216     #fix for #3085 "edit of customer's routing code only surprisingly causes
217     #nothing to happen...
218     # this probably won't do the right thing when we don't have the
219     # public key (can't actually get the real $old->payinfo)
220     my($new_account, $new_aba) = split('@', $new->payinfo);
221     my($old_account, $old_aba) = split('@', $old->payinfo);
222     $new_account = $old_account if $new_account =~ /xx/;
223     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
224     $new->payinfo($new_account.'@'.$new_aba);
225   }
226
227   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
228   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
229   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
230
231   $error ||= $new->replace( $old, \@invoicing_list,
232                             'tax_exemption' => \@tax_exempt,
233                           );
234
235   warn "$me returned from replace" if $DEBUG;
236   
237 }
238
239 </%init>