dd8d00b11dce7b337ba0e302bf83adc7c0d29dae
[freeside.git] / httemplate / edit / process / qual.cgi
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(3). 'misc/qual.html?'. $cgi->query_string ) %>
4 %} else {
5 <% header('Qualification entered') %>
6   <SCRIPT TYPE="text/javascript">
7     window.top.location = '<% popurl(3).'view/qual.cgi?qualnum='. $qual->qualnum %>';
8   </SCRIPT>
9   </BODY></HTML>
10 %}
11 <%init>
12
13 my $curuser = $FS::CurrentUser::CurrentUser;
14
15 die "access denied"
16   unless $curuser->access_right('Qualify service');
17
18 # copied from misc/qual.html :(
19 $cgi->param('custnum') =~ /^(\d+)$/;
20 my $custnum = $1;
21 $cgi->param('prospectnum') =~ /^(\d+)$/;
22 my $prospectnum = $1;
23 my $cust_or_prospect = $custnum ? "cust" : "prospect";
24 my $table = $cust_or_prospect . "_main";
25 my $custnum_or_prospectnum = $custnum ? $custnum : $prospectnum;
26 my $cust_main_or_prospect_main = qsearchs({
27   'table'     => $table,
28   'hashref'   => { $cust_or_prospect."num" => $custnum_or_prospectnum },
29   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
30 });
31 die "neither prospect nor customer specified or found" 
32     unless $cust_main_or_prospect_main;
33
34 $cgi->param('exportnum') =~ /^(\d+)$/ or die 'illegal exportnum';
35 my $exportnum = $1;
36
37 my $phonenum = $cgi->param('phonenum');
38 $phonenum =~ s/\D//g;
39 $phonenum =~ /^(\d*)$/ or die 'illegal phonenum';
40 my $phonenum = $1;
41
42 $cgi->param('locationnum') =~ /^(\-?\d*)$/
43   or die 'illegal locationnum '. $cgi->param('locationnum');
44 my $locationnum = $1;
45
46 my $error = '';
47 my $cust_location = '';
48 my %location_hash = (
49   map { $_ => scalar($cgi->param($_)) }
50     qw( address1 address2 city county state zip country geocode ),
51     grep scalar($cgi->param($_)),
52       qw( location_type location_number location_kind )
53 );
54
55 if ( $locationnum == -1 || $locationnum == -3 ) { # adding a new one
56
57   $cust_location = new FS::cust_location {
58     $cust_or_prospect."num" => $custnum_or_prospectnum,
59     %location_hash,
60   };
61
62           #locationnum '': default service location
63 } elsif ( $locationnum eq '' && $cust_or_prospect eq 'prospect' ) {
64     die "a location must be specified explicitly for prospects";
65
66           #locationnum -2: address not required for qual
67 } elsif ( $locationnum == -2 && $phonenum eq '' ) {
68   $error = "Nothing to qualify - neither phone number nor address specified";
69
70 } else { #existing location, possibly with an edit
71   $cust_location = qsearchs('cust_location', { 'locationnum'=>$locationnum })
72     or die "Unknown locationnum $locationnum";
73   $cust_location->$_($location_hash{$_}) foreach keys %location_hash;
74 }
75
76 my $qual = new FS::qual {
77   'status' => 'N',
78 };
79 $qual->phonenum($phonenum)       if $phonenum ne '';
80 #$qual->locationnum($locationnum) if $locationnum > 0;
81 $qual->exportnum($exportnum)     if $exportnum > 0;
82 $qual->set( $cust_or_prospect."num" => $custnum_or_prospectnum )
83   unless $locationnum == -1 || $locationnum == -3 || $locationnum > 0;
84
85 $error ||= $qual->insert( 'cust_location' => $cust_location );
86
87 </%init>