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' => 'residential_commercial',
18 'options' => [ 'Residential', 'Commercial', ],
19 'onchange' => 'rescom_changed',
21 { 'field' => 'company',
26 { 'field' => 'contactnum',
29 'o2m_table' => 'contact',
30 'm2_label' => 'Contact',
31 'm2_error_callback' => $m2_error_callback,
34 { 'field' => 'locationnum',
35 'type' => 'select-cust_location',
36 'empty_label' => 'No address',
37 'disable_empty' => $conf->exists('prospect_main-location_required'),
38 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
41 'new_callback' => $new_callback,
42 'edit_callback' => $edit_callback,
43 'error_callback' => $error_callback,
45 'html_bottom' => $javascript,
46 'body_etc' => 'onLoad="rescom_changed()"',
51 my $curuser = $FS::CurrentUser::CurrentUser;
52 my $conf = new FS::Conf;
55 if ( $cgi->param('error') ) {
56 $prospectnum = scalar($cgi->param('prospectnum'));
59 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
61 } elsif ( $cgi->keywords ) { #editing
64 unless $curuser->access_right('Edit prospect');
66 } else { #new prospect
69 unless $curuser->access_right('New prospect');
73 my $new_callback = sub {
74 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
76 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
79 #add a link to the image.cgi for this card
80 $opt_hashref->{'html_bottom'} .=
81 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
82 ' WIDTH=604 HEIGHT=328><BR>';
84 #fill in the incoming params: name, address1/address2, city_state_zip
85 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
86 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
88 my $field = $cgi->param($param);
89 my $value = $cgi->param("val$num");
90 $cgi->param($field => $value);
93 if ( $cgi->param('company') ) {
94 $prospect_main->company( $cgi->param('company') );
97 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
98 $cgi->param('contactnum0_first' => $1);
99 $cgi->param('contactnum0_last' => $2);
102 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
103 $cgi->param('locationnum', -1);
104 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
105 $cgi->param('city' => $2);
106 $cgi->param('state' => $3);
107 $cgi->param('zip' => $4);
113 #config to default to commercial and/or disable residential when someone needs
114 $prospect_main->set('residential_commercial', 'Residential');
118 my $edit_callback = sub {
119 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
120 my( $cgi, $prospect_main ) = @_;
122 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
123 die 'multiple locations for prospect '. $prospect_main->prospectnum
124 if scalar(@cust_location) > 1;
125 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
126 if scalar(@cust_location);
127 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
129 $prospect_main->set('residential_commercial',
130 length($prospect_main->company)
136 my $error_callback = sub {
137 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
138 my( $cgi, $prospect_main ) = @_;
139 $cgi->param('locationnum') =~ /^(\-?\d*)$/
140 or die 'illegal locationnum '. $cgi->param('locationnum');
141 my $locationnum = $1;
142 $prospect_main->set('locationnum', $locationnum);
144 $prospect_main->set('residential_commercial',
145 ($cgi->param('residential_commercial') eq 'Commercial')
152 my $m2_error_callback = sub {
153 my($cgi, $object) = @_;
155 #process_o2m fields in process/prospect_main.html
156 my @fields = qw( first last title comment );
157 my @gfields = ( '', map "_$_", @fields );
160 if ( /^contactnum(\d+)$/ ) {
162 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
163 my $x = new FS::contact {
164 'contactnum' => scalar($cgi->param("contactnum$num")),
165 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
178 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
180 my $javascript = <<END;
181 <SCRIPT TYPE="text/javascript">
182 function rescom_changed() {
183 var f = document.edit_topform;
185 if ( f.residential_commercial_Residential.checked ) {
187 } else if ( f.residential_commercial_Commercial.checked ) {