1 <% include('elements/edit.html',
2 'name_singular' => 'prospect',
3 'table' => 'prospect_main',
4 'labels' => { 'prospectnum' => 'Prospect',
6 'refnum' => 'Advertising source',
7 'company' => 'Company',
8 'contactnum' => 'Contact',
9 'locationnum' => ' ',
10 'taxstatusnum'=> 'Tax status',
13 { 'field' => 'agentnum',
14 'type' => 'select-agent',
15 'empty_label' => 'Select agent',
18 { 'field' => 'refnum',
19 'type' => 'select-part_referral',
20 'empty_label' => 'Select advertising source',
23 { 'field' => 'residential_commercial',
25 'options' => [ 'Residential', 'Commercial', ],
26 'onchange' => 'rescom_changed',
28 { 'field' => 'company',
33 { 'field' => 'contactnum',
36 'o2m_table' => 'contact',
37 'm2_label' => 'Contact',
38 'm2_error_callback' => $m2_error_callback,
41 { 'field' => 'locationnum',
42 'type' => 'select-cust_location',
43 'empty_label' => 'No address',
44 'disable_empty' => $conf->exists('prospect_main-location_required'),
45 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
46 'include_opt_callback' => sub {
47 'prospect_main' => shift
50 { 'field' => 'taxstatusnum',
51 'type' => 'select-tax_status',
55 'new_callback' => $new_callback,
56 'edit_callback' => $edit_callback,
57 'error_callback' => $error_callback,
59 'html_bottom' => $javascript,
60 'body_etc' => 'onLoad="rescom_changed()"',
65 my $curuser = $FS::CurrentUser::CurrentUser;
66 my $conf = new FS::Conf;
69 if ( $cgi->param('error') ) {
70 $prospectnum = scalar($cgi->param('prospectnum'));
73 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
75 } elsif ( $cgi->keywords ) { #editing
78 unless $curuser->access_right('Edit prospect');
80 } else { #new prospect
83 unless $curuser->access_right('New prospect');
87 my $new_callback = sub {
88 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
90 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
93 #add a link to the image.cgi for this card
94 $opt_hashref->{'html_bottom'} .=
95 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
96 ' WIDTH=604 HEIGHT=328><BR>';
98 #fill in the incoming params: name, address1/address2, city_state_zip
99 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
100 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
102 my $field = $cgi->param($param);
103 my $value = $cgi->param("val$num");
104 $cgi->param($field => $value);
107 if ( $cgi->param('company') ) {
108 $prospect_main->company( $cgi->param('company') );
111 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
112 $cgi->param('contactnum0_first' => $1);
113 $cgi->param('contactnum0_last' => $2);
116 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
117 $cgi->param('locationnum', -1);
118 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
119 $cgi->param('city' => $2);
120 $cgi->param('state' => $3);
121 $cgi->param('zip' => $4);
127 #config to default to commercial and/or disable residential when someone needs
128 $prospect_main->set('residential_commercial', 'Residential');
132 my $edit_callback = sub {
133 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
134 my( $cgi, $prospect_main ) = @_;
136 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
137 die 'multiple locations for prospect '. $prospect_main->prospectnum
138 if scalar(@cust_location) > 1;
139 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
140 if scalar(@cust_location);
141 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
143 $prospect_main->set('residential_commercial',
144 length($prospect_main->company)
150 my $error_callback = sub {
151 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
152 my( $cgi, $prospect_main ) = @_;
153 $cgi->param('locationnum') =~ /^(\-?\d*)$/
154 or die 'illegal locationnum '. $cgi->param('locationnum');
155 my $locationnum = $1;
156 $prospect_main->set('locationnum', $locationnum);
158 $prospect_main->set('residential_commercial',
159 ($cgi->param('residential_commercial') eq 'Commercial')
166 my $m2_error_callback = sub {
167 my($cgi, $object) = @_;
169 #process_o2m fields in process/prospect_main.html
170 my @fields = qw( first last title comment );
171 my @gfields = ( '', map "_$_", @fields );
174 if ( /^contactnum(\d+)$/ ) {
176 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
177 my $x = new FS::contact {
178 'contactnum' => scalar($cgi->param("contactnum$num")),
179 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
192 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
194 my $javascript = <<END;
195 <SCRIPT TYPE="text/javascript">
196 function rescom_changed() {
197 var f = document.edit_topform;
199 if ( f.residential_commercial_Residential.checked ) {
201 c.style.backgroundColor = '#dddddd';
202 } else if ( f.residential_commercial_Commercial.checked ) {
204 c.style.backgroundColor = '#ffffff';