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' => ' ',
12 { 'field' => 'agentnum',
13 'type' => 'select-agent',
14 'empty_label' => 'Select agent',
17 { 'field' => 'refnum',
18 'type' => 'select-part_referral',
19 'empty_label' => 'Select advertising source',
22 { 'field' => 'residential_commercial',
24 'options' => [ 'Residential', 'Commercial', ],
25 'onchange' => 'rescom_changed',
27 { 'field' => 'company',
32 { 'field' => 'contactnum',
35 'o2m_table' => 'contact',
36 'm2_label' => 'Contact',
37 'm2_error_callback' => $m2_error_callback,
40 { 'field' => 'locationnum',
41 'type' => 'select-cust_location',
42 'empty_label' => 'No address',
43 'disable_empty' => $conf->exists('prospect_main-location_required'),
44 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
45 'include_opt_callback' => sub {
46 'prospect_main' => shift
50 'new_callback' => $new_callback,
51 'edit_callback' => $edit_callback,
52 'error_callback' => $error_callback,
54 'html_bottom' => $javascript,
55 'body_etc' => 'onLoad="rescom_changed()"',
60 my $curuser = $FS::CurrentUser::CurrentUser;
61 my $conf = new FS::Conf;
64 if ( $cgi->param('error') ) {
65 $prospectnum = scalar($cgi->param('prospectnum'));
68 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
70 } elsif ( $cgi->keywords ) { #editing
73 unless $curuser->access_right('Edit prospect');
75 } else { #new prospect
78 unless $curuser->access_right('New prospect');
82 my $new_callback = sub {
83 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
85 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
88 #add a link to the image.cgi for this card
89 $opt_hashref->{'html_bottom'} .=
90 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
91 ' WIDTH=604 HEIGHT=328><BR>';
93 #fill in the incoming params: name, address1/address2, city_state_zip
94 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
95 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
97 my $field = $cgi->param($param);
98 my $value = $cgi->param("val$num");
99 $cgi->param($field => $value);
102 if ( $cgi->param('company') ) {
103 $prospect_main->company( $cgi->param('company') );
106 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
107 $cgi->param('contactnum0_first' => $1);
108 $cgi->param('contactnum0_last' => $2);
111 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
112 $cgi->param('locationnum', -1);
113 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
114 $cgi->param('city' => $2);
115 $cgi->param('state' => $3);
116 $cgi->param('zip' => $4);
122 #config to default to commercial and/or disable residential when someone needs
123 $prospect_main->set('residential_commercial', 'Residential');
127 my $edit_callback = sub {
128 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
129 my( $cgi, $prospect_main ) = @_;
131 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
132 die 'multiple locations for prospect '. $prospect_main->prospectnum
133 if scalar(@cust_location) > 1;
134 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
135 if scalar(@cust_location);
136 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
138 $prospect_main->set('residential_commercial',
139 length($prospect_main->company)
145 my $error_callback = sub {
146 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
147 my( $cgi, $prospect_main ) = @_;
148 $cgi->param('locationnum') =~ /^(\-?\d*)$/
149 or die 'illegal locationnum '. $cgi->param('locationnum');
150 my $locationnum = $1;
151 $prospect_main->set('locationnum', $locationnum);
153 $prospect_main->set('residential_commercial',
154 ($cgi->param('residential_commercial') eq 'Commercial')
161 my $m2_error_callback = sub {
162 my($cgi, $object) = @_;
164 #process_o2m fields in process/prospect_main.html
165 my @fields = qw( first last title comment );
166 my @gfields = ( '', map "_$_", @fields );
169 if ( /^contactnum(\d+)$/ ) {
171 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
172 my $x = new FS::contact {
173 'contactnum' => scalar($cgi->param("contactnum$num")),
174 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
187 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
189 my $javascript = <<END;
190 <SCRIPT TYPE="text/javascript">
191 function rescom_changed() {
192 var f = document.edit_topform;
194 if ( f.residential_commercial_Residential.checked ) {
196 c.style.backgroundColor = '#dddddd';
197 } else if ( f.residential_commercial_Commercial.checked ) {
199 c.style.backgroundColor = '#ffffff';