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 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
35 'new_callback' => $new_callback,
36 'edit_callback' => $edit_callback,
37 'error_callbacck' => $error_callback,
43 my $curuser = $FS::CurrentUser::CurrentUser;
44 my $conf = new FS::Conf;
47 if ( $cgi->param('error') ) {
48 $prospectnum = scalar($cgi->param('prospectnum'));
51 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
53 } elsif ( $cgi->keywords ) { #editing
56 unless $curuser->access_right('Edit prospect');
58 } else { #new prospect
61 unless $curuser->access_right('New prospect');
65 my $new_callback = sub {
66 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
68 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
71 #add a link to the image.cgi for this card
72 $opt_hashref->{'html_bottom'} .=
73 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
74 ' WIDTH=604 HEIGHT=328><BR>';
76 #fill in the incoming params: name, address1/address2, city_state_zip
77 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
78 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
80 my $field = $cgi->param($param);
81 my $value = $cgi->param("val$num");
82 $cgi->param($field => $value);
85 if ( $cgi->param('company') ) {
86 $prospect_main->company( $cgi->param('company') );
89 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
90 $cgi->param('contactnum0_first' => $1);
91 $cgi->param('contactnum0_last' => $2);
94 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
95 $cgi->param('locationnum', -1);
96 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
97 $cgi->param('city' => $2);
98 $cgi->param('state' => $3);
99 $cgi->param('zip' => $4);
107 my $edit_callback = sub {
108 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
109 my( $cgi, $prospect_main ) = @_;
111 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
112 die 'multiple locations for prospect '. $prospect_main->prospectnum
113 if scalar(@cust_location) > 1;
114 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
115 if scalar(@cust_location);
116 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
119 my $error_callback = sub {
120 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
121 my( $cgi, $prospect_main ) = @_;
122 $cgi->param('locationnum') =~ /^(\-?\d*)$/
123 or die 'illegal locationnum '. $cgi->param('locationnum');
124 my $locationnum = $1;
125 $prospect_main->set('locationnum', $locationnum);
128 my $m2_error_callback = sub {
129 my($cgi, $object) = @_;
131 #process_o2m fields in process/prospect_main.html
132 my @fields = qw( first last title comment );
133 my @gfields = ( '', map "_$_", @fields );
136 if ( /^contactnum(\d+)$/ ) {
138 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
139 my $x = new FS::contact {
140 'contactnum' => scalar($cgi->param("contactnum$num")),
141 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
154 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;