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',
15 { 'field' => 'company',
19 { 'field' => 'contactnum',
22 'o2m_table' => 'contact',
23 'm2_label' => 'Contact',
24 'm2_error_callback' => $m2_error_callback,
27 { 'field' => 'locationnum',
28 'type' => 'select-cust_location',
29 'empty_label' => 'No address',
30 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
33 'new_callback' => $new_callback,
34 'edit_callback' => $edit_callback,
35 'error_callbacck' => $error_callback,
41 my $curuser = $FS::CurrentUser::CurrentUser;
42 my $conf = new FS::Conf;
45 if ( $cgi->param('error') ) {
46 $prospectnum = scalar($cgi->param('prospectnum'));
49 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
51 } elsif ( $cgi->keywords ) { #editing
54 unless $curuser->access_right('Edit prospect');
56 } else { #new prospect
59 unless $curuser->access_right('New prospect');
63 my $new_callback = sub {
64 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
66 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
69 #add a link to the image.cgi for this card
70 $opt_hashref->{'html_bottom'} .=
71 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
72 ' WIDTH=604 HEIGHT=328><BR>';
74 #fill in the incoming params: name, address1/address2, city_state_zip
75 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
76 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
78 my $field = $cgi->param($param);
79 my $value = $cgi->param("val$num");
80 $cgi->param($field => $value);
83 if ( $cgi->param('company') ) {
84 $prospect_main->company( $cgi->param('company') );
87 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
88 $cgi->param('contactnum0_first' => $1);
89 $cgi->param('contactnum0_last' => $2);
92 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
93 $cgi->param('locationnum', -1);
94 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
95 $cgi->param('city' => $2);
96 $cgi->param('state' => $3);
97 $cgi->param('zip' => $4);
105 my $edit_callback = sub {
106 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
107 my( $cgi, $prospect_main ) = @_;
109 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
110 die 'multiple locations for prospect '. $prospect_main->prospectnum
111 if scalar(@cust_location) > 1;
112 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
113 if scalar(@cust_location);
114 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
117 my $error_callback = sub {
118 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
119 my( $cgi, $prospect_main ) = @_;
120 $cgi->param('locationnum') =~ /^(\-?\d*)$/
121 or die 'illegal locationnum '. $cgi->param('locationnum');
122 my $locationnum = $1;
123 $prospect_main->set('locationnum', $locationnum);
126 my $m2_error_callback = sub {
127 my($cgi, $object) = @_;
129 #process_o2m fields in process/prospect_main.html
130 my @fields = qw( first last title comment );
131 my @gfields = ( '', map "_$_", @fields );
134 if ( /^contactnum(\d+)$/ ) {
136 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
137 my $x = new FS::contact {
138 'contactnum' => scalar($cgi->param("contactnum$num")),
139 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
152 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;