import torrus 1.0.9
[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 $cgi->param('phonenum') =~ /^(\d*)$/ or die 'illegal phonenum';
41 my $phonenum = $1;
42
43 $cgi->param('locationnum') =~ /^(\-?\d*)$/
44   or die 'illegal locationnum '. $cgi->param('locationnum');
45 my $locationnum = $1;
46
47 my $oldAutoCommit = $FS::UID::AutoCommit;
48 local $FS::UID::AutoCommit = 0;
49 my $dbh = dbh;
50 my $error = '';
51 my $cust_location;
52 if ( $locationnum == -1 ) { # adding a new one
53   my %location_hash = map { $_ => scalar($cgi->param($_)) }
54         qw( address1 address2 city county state zip country geocode );
55   $location_hash{$cust_or_prospect."num"} = $custnum_or_prospectnum;
56   $location_hash{location_type} = $cgi->param('location_type') 
57     if $cgi->param('location_type');
58   $location_hash{location_number} = $cgi->param('location_number') 
59     if $cgi->param('location_number');
60   $location_hash{location_kind} = $cgi->param('location_kind') 
61     if $cgi->param('location_kind');
62   $cust_location = new FS::cust_location ( { %location_hash } );
63   $error = $cust_location->insert;
64   die "Unable to insert cust_location: $error" if $error;
65 }
66 elsif ( $locationnum eq '' ) { # default service location
67     if ( $custnum ) { 
68           $cust_location = new FS::cust_location ( {
69                 $cust_main_or_prospect_main->location_hash,
70                 custnum => $custnum,
71           } );
72     } elsif ( $prospectnum ) {
73         die "a location must be specified explicitly for prospects";
74     }
75 }
76 elsif ( $locationnum != -2 ) { # -2 = address not required for qual
77   $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum })
78     or die 'Invalid locationnum'; 
79 }
80
81 my $export;
82 if ( $exportnum > 0 ) {
83  $export = qsearchs( 'part_export', { 'exportnum' => $exportnum } )
84     or die 'Invalid exportnum';
85 }
86
87 die "Nothing to qualify - neither TN nor address specified" 
88     unless ( defined $cust_location || $phonenum ne '' );
89
90 my $qual;
91 if ( $locationnum != -2 && $cust_location->locationnum > 0 ) {
92     $qual = new FS::qual( { locationnum => $cust_location->locationnum } );
93 }
94 else { # a cust_main default service address *OR* address not required
95     $qual = new FS::qual( { $cust_or_prospect."num" => $custnum_or_prospectnum } );
96 }
97 $qual->phonenum($phonenum) if $phonenum ne '';
98 $qual->status('N');
99
100 if ( $export ) {
101     $qual->exportnum($export->exportnum);
102     my $qres = $export->qual($qual);
103     $error = "Qualification error: $qres" unless ref($qres);
104     unless ( $error ) {
105         $qual->status($qres->{'status'}) if $qres->{'status'};
106         $qual->vendor_qual_id($qres->{'vendor_qual_id'}) 
107             if $qres->{'vendor_qual_id'};
108         $error = $qual->insert($qres->{'options'}) if ref($qres->{'options'});
109     }
110 }
111
112 unless ( $error || $qual->qualnum ) {
113     $error = $qual->insert;
114 }
115
116 my $qualnum;
117 unless ( $error ) {
118     if($qual->qualnum) {
119         $qualnum = $qual->qualnum;
120     }
121     else {
122         $error = "Unable to save qualification";
123     }
124 }
125
126 </%init>