strip non-digits from phone numbers instead of throwing a nasty error, on dsl qualifi...
[freeside.git] / httemplate / edit / process / qual.cgi
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 %  $dbh->rollback if $oldAutoCommit;
4 <% $cgi->redirect(popurl(3). 'misc/qual.html?'. $cgi->query_string ) %>
5 %} else {
6 %  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
7 <% header('Qualification entered') %>
8   <SCRIPT TYPE="text/javascript">
9     window.top.location = '<% popurl(3). "view/qual.cgi?qualnum=$qualnum" %>';
10   </SCRIPT>
11
12   </BODY></HTML>
13 %}
14 <%init>
15
16 my $curuser = $FS::CurrentUser::CurrentUser;
17
18 die "access denied"
19   unless $curuser->access_right('Qualify service');
20
21 # copied from misc/qual.html :(
22 $cgi->param('custnum') =~ /^(\d+)$/;
23 my $custnum = $1;
24 $cgi->param('prospectnum') =~ /^(\d+)$/;
25 my $prospectnum = $1;
26 my $cust_or_prospect = $custnum ? "cust" : "prospect";
27 my $table = $cust_or_prospect . "_main";
28 my $custnum_or_prospectnum = $custnum ? $custnum : $prospectnum;
29 my $cust_main_or_prospect_main = qsearchs({
30   'table'     => $table,
31   'hashref'   => { $cust_or_prospect."num" => $custnum_or_prospectnum },
32   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
33 });
34 die "neither prospect nor customer specified or found" 
35     unless $cust_main_or_prospect_main;
36
37 $cgi->param('exportnum') =~ /^(\d+)$/ or die 'illegal exportnum';
38 my $exportnum = $1;
39
40 my $phonenum = $cgi->param('phonenum');
41 $phonenum =~ s/\D//g;
42 $phonenum =~ /^(\d*)$/ or die 'illegal phonenum';
43 my $phonenum = $1;
44
45 $cgi->param('locationnum') =~ /^(\-?\d*)$/
46   or die 'illegal locationnum '. $cgi->param('locationnum');
47 my $locationnum = $1;
48
49 my $oldAutoCommit = $FS::UID::AutoCommit;
50 local $FS::UID::AutoCommit = 0;
51 my $dbh = dbh;
52 my $error = '';
53 my $cust_location;
54 if ( $locationnum == -1 ) { # adding a new one
55   my %location_hash = map { $_ => scalar($cgi->param($_)) }
56         qw( address1 address2 city county state zip country geocode );
57   $location_hash{$cust_or_prospect."num"} = $custnum_or_prospectnum;
58   $location_hash{location_type} = $cgi->param('location_type') 
59     if $cgi->param('location_type');
60   $location_hash{location_number} = $cgi->param('location_number') 
61     if $cgi->param('location_number');
62   $location_hash{location_kind} = $cgi->param('location_kind') 
63     if $cgi->param('location_kind');
64   $cust_location = new FS::cust_location ( { %location_hash } );
65   $error = $cust_location->insert;
66   die "Unable to insert cust_location: $error" if $error;
67 }
68 elsif ( $locationnum eq '' ) { # default service location
69     if ( $custnum ) { 
70           $cust_location = new FS::cust_location ( {
71                 $cust_main_or_prospect_main->location_hash,
72                 custnum => $custnum,
73           } );
74     } elsif ( $prospectnum ) {
75         die "a location must be specified explicitly for prospects";
76     }
77 }
78 elsif ( $locationnum != -2 ) { # -2 = address not required for qual
79   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum })
80     or die 'Invalid locationnum'; 
81 }
82
83 my $export;
84 if ( $exportnum > 0 ) {
85  $export = qsearchs( 'part_export', { 'exportnum' => $exportnum } )
86     or die 'Invalid exportnum';
87 }
88
89 die "Nothing to qualify - neither TN nor address specified" 
90     unless ( defined $cust_location || $phonenum ne '' );
91
92 my $qual;
93 if ( $locationnum != -2 && $cust_location->locationnum > 0 ) {
94     $qual = new FS::qual( { locationnum => $cust_location->locationnum } );
95 }
96 else { # a cust_main default service address *OR* address not required
97     $qual = new FS::qual( { $cust_or_prospect."num" => $custnum_or_prospectnum } );
98 }
99 $qual->phonenum($phonenum) if $phonenum ne '';
100 $qual->status('N');
101
102 if ( $export ) {
103     $qual->exportnum($export->exportnum);
104     my $qres = $export->qual($qual);
105     $error = "Qualification error: $qres" unless ref($qres);
106     unless ( $error ) {
107         $qual->status($qres->{'status'}) if $qres->{'status'};
108         $qual->vendor_qual_id($qres->{'vendor_qual_id'}) 
109             if $qres->{'vendor_qual_id'};
110         $error = $qual->insert($qres->{'options'}) if ref($qres->{'options'});
111     }
112 }
113
114 unless ( $error || $qual->qualnum ) {
115     $error = $qual->insert;
116 }
117
118 my $qualnum;
119 unless ( $error ) {
120     if($qual->qualnum) {
121         $qualnum = $qual->qualnum;
122     }
123     else {
124         $error = "Unable to save qualification";
125     }
126 }
127
128 </%init>