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 ##actually o2m, but this seems to be working for edit so far
23 #'m2name_table' => 'contact',
24 #'m2name_namecol' => 'contactnum',
25 #'m2_label' => 'Contact',
26 #'m2_error_callback' => $m2_error_callback,
28 'o2m_table' => 'contact',
29 'm2_label' => 'Contact',
30 'm2_error_callback' => $m2_error_callback,
33 { 'field' => 'locationnum',
34 'type' => 'select-cust_location',
35 'empty_label' => 'No address',
38 'new_callback' => $new_callback,
39 'edit_callback' => $edit_callback,
40 'error_callbacck' => $error_callback,
46 my $curuser = $FS::CurrentUser::CurrentUser;
49 if ( $cgi->param('error') ) {
50 $prospectnum = scalar($cgi->param('prospectnum'));
53 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
55 } elsif ( $cgi->keywords ) { #editing
58 unless $curuser->access_right('Edit prospect');
60 } else { #new prospect
63 unless $curuser->access_right('New prospect');
67 my $new_callback = sub {
68 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
70 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
73 #add a link to the image.cgi for this card
74 $opt_hashref->{'html_bottom'} .=
75 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
76 ' WIDTH=604 HEIGHT=328><BR>';
78 #fill in the incoming params: name, address1/address2, city_state_zip
79 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
80 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
82 my $field = $cgi->param($param);
83 my $value = $cgi->param("val$num");
84 $cgi->param($field => $value);
87 if ( $cgi->param('company') ) {
88 $prospect_main->company( $cgi->param('company') );
91 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
92 $cgi->param('contactnum0_first' => $1);
93 $cgi->param('contactnum0_last' => $2);
96 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
97 $cgi->param('locationnum', -1);
98 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
99 $cgi->param('city' => $2);
100 $cgi->param('state' => $3);
101 $cgi->param('zip' => $4);
109 my $edit_callback = sub {
110 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
111 my( $cgi, $prospect_main ) = @_;
113 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
114 die 'multiple locations for prospect '. $prospect_main->prospectnum
115 if scalar(@cust_location) > 1;
116 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
117 if scalar(@cust_location);
118 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
121 my $error_callback = sub {
122 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
123 my( $cgi, $prospect_main ) = @_;
124 $cgi->param('locationnum') =~ /^(\-?\d*)$/
125 or die 'illegal locationnum '. $cgi->param('locationnum');
126 my $locationnum = $1;
127 $prospect_main->set('locationnum', $locationnum);
130 my $m2_error_callback = sub {
131 my($cgi, $object) = @_;
133 #process_o2m fields in process/prospect_main.html
134 my @fields = qw( first last title comment );
135 my @gfields = ( '', map "_$_", @fields );
138 if ( /^contactnum(\d+)$/ ) {
140 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
141 my $x = new FS::contact {
142 'contactnum' => $cgi->param("contactnum$num"),
143 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
156 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;