qualification address handling changes, RT#7111
[freeside.git] / httemplate / edit / prospect_main.html
1 <% include('elements/edit.html',
2      'name_singular'   => 'prospect',
3      'table'           => 'prospect_main',
4      'labels'          => { 'prospectnum' => 'Prospect',
5                             'agentnum'    => 'Agent',
6                             'company'     => 'Company',
7                             'contactnum'  => 'Contact',
8                             'locationnum' => '&nbsp;',
9                           },
10      'fields'          => [
11        { 'field'       => 'agentnum',
12          'type'        => 'select-agent',
13          'empty_label' => 'Select agent',
14          'colspan'     => 6,
15        },
16        { 'field'   => 'company',
17          'type'    => 'text',
18          'size'    => 50,
19          'colspan' => 6,
20        },
21        { 'field'             => 'contactnum',
22          'type'              => 'contact',
23          'colspan'           => 6,
24          'o2m_table'      => 'contact',
25          'm2_label'       => 'Contact',
26          'm2_error_callback' => $m2_error_callback,
27
28        },
29        { 'field'         => 'locationnum',
30          'type'          => 'select-cust_location',
31          'empty_label'   => 'No address',
32          'disable_empty' => $conf->exists('prospect_main-location_required'),
33          'alt_format'    => $conf->exists('prospect_main-alt_address_format'),
34        },
35      ],
36      'new_callback'    => $new_callback,
37      'edit_callback'   => $edit_callback,
38      'error_callbacck' => $error_callback,
39      'agent_virt'      => 1,
40    )
41 %>
42 <%init>
43
44 my $curuser = $FS::CurrentUser::CurrentUser;
45 my $conf = new FS::Conf;
46
47 my $prospectnum;
48 if ( $cgi->param('error') ) {
49   $prospectnum = scalar($cgi->param('prospectnum'));
50
51   die "access denied"
52     unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
53
54 } elsif ( $cgi->keywords ) { #editing
55
56   die "access denied"
57     unless $curuser->access_right('Edit prospect');
58
59 } else { #new prospect 
60
61   die "access denied"
62     unless $curuser->access_right('New prospect');
63
64 }
65
66 my $new_callback = sub {
67   my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
68
69   if ( $cgi->param('session') =~ /^(\w+)$/ ) {
70     my $session = $1;
71
72     #add a link to the image.cgi for this card
73     $opt_hashref->{'html_bottom'} .=
74       qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
75       ' WIDTH=604 HEIGHT=328><BR>';
76
77     #fill in the incoming params: name, address1/address2, city_state_zip
78     foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
79       $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
80       my $num = $1;
81       my $field = $cgi->param($param);
82       my $value = $cgi->param("val$num");
83       $cgi->param($field => $value);
84     }
85
86     if ( $cgi->param('company') ) {
87       $prospect_main->company( $cgi->param('company') );
88     }
89
90     if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
91       $cgi->param('contactnum0_first' => $1);
92       $cgi->param('contactnum0_last'  => $2);
93     }
94
95     if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
96       $cgi->param('locationnum', -1);
97       if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
98          $cgi->param('city'  => $2);
99          $cgi->param('state' => $3);
100          $cgi->param('zip'   => $4);
101       }
102     }
103
104   }
105
106 };
107
108 my $edit_callback = sub {
109   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
110   my( $cgi, $prospect_main ) = @_;
111   my @cust_location =
112     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
113   die 'multiple locations for prospect '. $prospect_main->prospectnum
114     if scalar(@cust_location) > 1;
115   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
116     if scalar(@cust_location);
117   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
118 };
119
120 my $error_callback = sub {
121   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
122   my( $cgi, $prospect_main ) = @_;
123   $cgi->param('locationnum') =~ /^(\-?\d*)$/
124     or die 'illegal locationnum '. $cgi->param('locationnum');
125   my $locationnum = $1;
126   $prospect_main->set('locationnum', $locationnum);
127 };
128
129 my $m2_error_callback = sub {
130   my($cgi, $object) = @_;
131
132   #process_o2m fields in process/prospect_main.html
133   my @fields = qw( first last title comment );
134   my @gfields = ( '', map "_$_", @fields );
135
136   map {
137         if ( /^contactnum(\d+)$/ ) {
138           my $num = $1;
139           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
140             my $x = new FS::contact {
141               'contactnum' => scalar($cgi->param("contactnum$num")),
142               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
143             };
144             $x;
145           } else {
146             ();
147           }
148         } else {
149           ();
150         }
151       }
152       $cgi->param;
153 };
154
155 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
156
157 </%init>