-tr-select-cust_location.html and elements/location.html: optionally support alternat...
[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('Order customer package'); # XXX: fix me
20
21 $cgi->param('custnum') =~ /^(\d+)$/
22   or die 'illegal custnum '. $cgi->param('custnum');
23 my $custnum = $1;
24 my $cust_main = qsearchs({
25   'table'     => 'cust_main',
26   'hashref'   => { 'custnum' => $custnum },
27   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
28 });
29 die 'unknown custnum' unless $cust_main;
30
31 $cgi->param('exportnum') =~ /^(\d+)$/ or die 'illegal exportnum';
32 my $exportnum = $1;
33
34 $cgi->param('phonenum') =~ /^(\d*)$/ or die 'illegal phonenum';
35 my $phonenum = $1;
36
37 $cgi->param('locationnum') =~ /^(\-?\d*)$/
38   or die 'illegal locationnum '. $cgi->param('locationnum');
39 my $locationnum = $1;
40
41 my $oldAutoCommit = $FS::UID::AutoCommit;
42 local $FS::UID::AutoCommit = 0;
43 my $dbh = dbh;
44 my $error = '';
45 my $cust_location;
46 if ( $locationnum == -1 ) { # adding a new one
47   my %location_hash = map { $_ => scalar($cgi->param($_)) }
48         qw( custnum address1 address2 city county state zip country geocode );
49   $location_hash{location_type} = $cgi->param('location_type') 
50     if $cgi->param('location_type');
51   $location_hash{location_number} = $cgi->param('location_number') 
52     if $cgi->param('location_number');
53   $location_hash{location_kind} = $cgi->param('location_kind') 
54     if $cgi->param('location_kind');
55   $cust_location = new FS::cust_location ( { %location_hash } );
56   $error = $cust_location->insert;
57   die "Unable to insert cust_location: $error" if $error;
58 }
59 elsif ( $locationnum eq '' ) { # default service location
60   $cust_location = new FS::cust_location ( {
61         $cust_main->location_hash,
62         custnum => $custnum,
63   } );
64 }
65 elsif ( $locationnum != -2 ) { # -2 = address not required for qual
66   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum })
67     or die 'Invalid locationnum'; 
68 }
69
70 my $export;
71 if ( $exportnum > 0 ) {
72  $export = qsearchs( 'part_export', { 'exportnum' => $exportnum } )
73     or die 'Invalid exportnum';
74 }
75
76 die "Nothing to qualify - neither TN nor address specified" 
77     unless ( defined $cust_location || $phonenum ne '' );
78
79 my $qual;
80 if ( $locationnum != -2 && $cust_location->locationnum > 0 ) {
81     $qual = new FS::qual( { locationnum => $cust_location->locationnum } );
82 }
83 else { # a cust_main default service address *OR* address not required
84     $qual = new FS::qual( { custnum => $custnum } );
85 }
86 $qual->phonenum($phonenum) if $phonenum ne '';
87 $qual->status('N');
88
89 if ( $export ) {
90     $qual->exportnum($export->exportnum);
91     my $qres = $export->qual($qual);
92     $error = "Qualification error: $qres" unless ref($qres);
93     unless ( $error ) {
94         $qual->status($qres->{'status'}) if $qres->{'status'};
95         $qual->vendor_qual_id($qres->{'vendor_qual_id'}) 
96             if $qres->{'vendor_qual_id'};
97         $error = $qual->insert($qres->{'options'}) if ref($qres->{'options'});
98     }
99 }
100
101 unless ( $error || $qual->qualnum ) {
102     $error = $qual->insert;
103 }
104
105 my $qualnum;
106 unless ( $error ) {
107     if($qual->qualnum) {
108         $qualnum = $qual->qualnum;
109     }
110     else {
111         $error = "Unable to save qualification";
112     }
113 }
114
115 </%init>