1 <% include('elements/edit.html',
2 'name_singular' => 'prospect',
3 'table' => 'prospect_main',
4 'html_table_class'=> 'fsinnerbox',
5 'labels' => { 'prospectnum' => 'Prospect',
7 'refnum' => 'Advertising source',
8 'company' => 'Company',
9 'contactnum' => 'Contact',
10 'locationnum' => ' ',
11 'taxstatusnum'=> 'Tax status',
14 { 'field' => 'agentnum',
15 'type' => 'select-agent',
16 'empty_label' => 'Select agent',
19 { 'field' => 'refnum',
20 'type' => 'select-part_referral',
21 'empty_label' => 'Select advertising source',
24 { 'field' => 'residential_commercial',
26 'options' => [ 'Residential', 'Commercial', ],
27 'onchange' => 'rescom_changed',
29 { 'field' => 'company',
34 { 'field' => 'contactnum',
37 'prospectnum' => $prospectnum,
38 'm2m_method' => 'prospect_contact',
39 'm2m_dstcol' => 'contactnum',
40 'm2_label' => 'Contact',
41 'm2_error_callback' => $m2_error_callback,
44 { 'field' => 'locationnum',
45 'type' => 'select-cust_location',
46 'empty_label' => 'No address',
47 'disable_empty' => $conf->exists('prospect_main-location_required'),
48 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
49 'include_opt_callback' => sub {
50 'prospect_main' => shift
53 { 'field' => 'taxstatusnum',
54 'type' => 'select-tax_status',
59 'new_callback' => $new_callback,
60 'edit_callback' => $edit_callback,
61 'error_callback' => $error_callback,
63 'html_bottom' => $javascript,
64 'body_etc' => 'onLoad="rescom_changed()"',
69 my $curuser = $FS::CurrentUser::CurrentUser;
70 my $conf = new FS::Conf;
73 if ( $cgi->param('error') ) {
74 $cgi->param('prospectnum') =~ /^(\d*)$/ or die 'illegal prospectnum';
78 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
80 } elsif ( $cgi->keywords ) { #editing
82 my($query) = $cgi->keywords;
83 $query =~ /^(\d+)$/ or die 'no prospectnum';
87 unless $curuser->access_right('Edit prospect');
89 } else { #new prospect
94 unless $curuser->access_right('New prospect');
98 my $new_callback = sub {
99 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
101 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
104 #add a link to the image.cgi for this card
105 $opt_hashref->{'html_bottom'} .=
106 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
107 ' WIDTH=604 HEIGHT=328><BR>';
109 #fill in the incoming params: name, address1/address2, city_state_zip
110 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
111 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
113 my $field = $cgi->param($param);
114 my $value = $cgi->param("val$num");
115 $cgi->param($field => $value);
118 if ( $cgi->param('company') ) {
119 $prospect_main->company( $cgi->param('company') );
122 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
123 $cgi->param('contactnum0_first' => $1);
124 $cgi->param('contactnum0_last' => $2);
127 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
128 $cgi->param('locationnum', -1);
129 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
130 $cgi->param('city' => $2);
131 $cgi->param('state' => $3);
132 $cgi->param('zip' => $4);
138 #config to default to commercial and/or disable residential when someone needs
139 $prospect_main->set('residential_commercial', 'Residential');
143 my $edit_callback = sub {
144 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
145 my( $cgi, $prospect_main ) = @_;
147 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
148 die 'multiple locations for prospect '. $prospect_main->prospectnum
149 if scalar(@cust_location) > 1;
150 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
151 if scalar(@cust_location);
152 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
154 $prospect_main->set('residential_commercial',
155 length($prospect_main->company)
161 my $error_callback = sub {
162 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
163 my( $cgi, $prospect_main ) = @_;
164 $cgi->param('locationnum') =~ /^(\-?\d*)$/
165 or die 'illegal locationnum '. $cgi->param('locationnum');
166 my $locationnum = $1;
167 $prospect_main->set('locationnum', $locationnum);
169 $prospect_main->set('residential_commercial',
170 ($cgi->param('residential_commercial') eq 'Commercial')
177 my $m2_error_callback = sub {
178 my($cgi, $object) = @_;
180 #process_o2m fields in process/prospect_main.html
181 my @fields = qw( first last title comment );
182 my @gfields = ( '', map "_$_", @fields );
185 if ( /^contactnum(\d+)$/ ) {
187 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
188 my $x = new FS::contact {
189 'contactnum' => scalar($cgi->param("contactnum$num")),
190 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
203 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
205 my $javascript = <<END;
206 <SCRIPT TYPE="text/javascript">
207 function rescom_changed() {
208 var f = document.edit_topform;
210 if ( f.residential_commercial_Residential.checked ) {
212 c.style.backgroundColor = '#dddddd';
213 } else if ( f.residential_commercial_Commercial.checked ) {
215 c.style.backgroundColor = '#ffffff';