1 <% include('elements/edit.html',
2 'name_singular' => 'prospect',
3 'table' => 'prospect_main',
4 'labels' => { 'prospectnum' => 'Prospect',
6 'company' => 'Company',
7 'contactnum' => 'Contact',
8 'locationnum' => ' ',
11 { 'field' => 'agentnum',
12 'type' => 'select-agent',
13 'empty_label' => 'Select agent',
16 { 'field' => 'company',
21 { 'field' => 'contactnum',
24 'o2m_table' => 'contact',
25 'm2_label' => 'Contact',
26 'm2_error_callback' => $m2_error_callback,
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'),
36 'new_callback' => $new_callback,
37 'edit_callback' => $edit_callback,
38 'error_callbacck' => $error_callback,
44 my $curuser = $FS::CurrentUser::CurrentUser;
45 my $conf = new FS::Conf;
48 if ( $cgi->param('error') ) {
49 $prospectnum = scalar($cgi->param('prospectnum'));
52 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
54 } elsif ( $cgi->keywords ) { #editing
57 unless $curuser->access_right('Edit prospect');
59 } else { #new prospect
62 unless $curuser->access_right('New prospect');
66 my $new_callback = sub {
67 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
69 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
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>';
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)';
81 my $field = $cgi->param($param);
82 my $value = $cgi->param("val$num");
83 $cgi->param($field => $value);
86 if ( $cgi->param('company') ) {
87 $prospect_main->company( $cgi->param('company') );
90 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
91 $cgi->param('contactnum0_first' => $1);
92 $cgi->param('contactnum0_last' => $2);
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);
108 my $edit_callback = sub {
109 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
110 my( $cgi, $prospect_main ) = @_;
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');
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);
129 my $m2_error_callback = sub {
130 my($cgi, $object) = @_;
132 #process_o2m fields in process/prospect_main.html
133 my @fields = qw( first last title comment );
134 my @gfields = ( '', map "_$_", @fields );
137 if ( /^contactnum(\d+)$/ ) {
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,
155 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;