summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/qual.cgi
blob: 21516542cbaf54510c38824908b688d57304f11a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%if ($error) {
%  $cgi->param('error', $error);
<% $cgi->redirect(popurl(3). 'misc/qual.html?'. $cgi->query_string ) %>
%} else {
<& /elements/header-popup.html, 'Qualification entered' &>
  <SCRIPT TYPE="text/javascript">
    window.top.location = '<% popurl(3).'view/qual.cgi?qualnum='. $qual->qualnum %>';
  </SCRIPT>
  </BODY></HTML>
%}
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('Qualify service');

# copied from misc/qual.html :(
$cgi->param('custnum') =~ /^(\d+)$/;
my $custnum = $1;
$cgi->param('prospectnum') =~ /^(\d+)$/;
my $prospectnum = $1;
my $cust_or_prospect = $custnum ? "cust" : "prospect";
my $table = $cust_or_prospect . "_main";
my $custnum_or_prospectnum = $custnum ? $custnum : $prospectnum;
my $cust_main_or_prospect_main = qsearchs({
  'table'     => $table,
  'hashref'   => { $cust_or_prospect."num" => $custnum_or_prospectnum },
  'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
});
die "neither prospect nor customer specified or found" 
    unless $cust_main_or_prospect_main;

$cgi->param('exportnum') =~ /^(\d+)$/ or die 'illegal exportnum';
my $exportnum = $1;

my $phonenum = $cgi->param('phonenum');
$phonenum =~ s/\D//g;
$phonenum =~ /^(\d*)$/ or die 'illegal phonenum';
my $phonenum = $1;

$cgi->param('locationnum') =~ /^(\-?\d*)$/
  or die 'illegal locationnum '. $cgi->param('locationnum');
my $locationnum = $1;

my $error = '';
my $cust_location = '';
my %location_hash = (
  map { $_ => scalar($cgi->param($_)) }
    qw( address1 address2 city county state zip country geocode ),
    grep scalar($cgi->param($_)),
      qw( location_type location_number location_kind )
);

if ( $locationnum == -1 || $locationnum == -3 ) { # adding a new one

  $cust_location = new FS::cust_location {
    $cust_or_prospect."num" => $custnum_or_prospectnum,
    %location_hash,
  };

          #locationnum '': default service location
} elsif ( $locationnum eq '' && $cust_or_prospect eq 'prospect' ) {
    die "a location must be specified explicitly for prospects";

          #locationnum -2: address not required for qual
} elsif ( $locationnum == -2 && $phonenum eq '' ) {
  $error = "Nothing to qualify - neither phone number nor address specified";

} else { #existing location, possibly with an edit
  $cust_location = qsearchs('cust_location', { 'locationnum'=>$locationnum })
    or die "Unknown locationnum $locationnum";
  $cust_location->$_($location_hash{$_}) foreach keys %location_hash;
}

my $qual = new FS::qual {
  'status' => 'N',
};
$qual->phonenum($phonenum)       if $phonenum ne '';
#$qual->locationnum($locationnum) if $locationnum > 0;
$qual->exportnum($exportnum)     if $exportnum > 0;
$qual->set( $cust_or_prospect."num" => $custnum_or_prospectnum )
  unless $locationnum == -1 || $locationnum == -3 || $locationnum > 0;

$error ||= $qual->insert( 'cust_location' => $cust_location );

</%init>