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'),
39 'include_opt_callback' => sub {
40 'prospect_main' => shift
44 'new_callback' => $new_callback,
45 'edit_callback' => $edit_callback,
46 'error_callback' => $error_callback,
48 'html_bottom' => $javascript,
49 'body_etc' => 'onLoad="rescom_changed()"',
54 my $curuser = $FS::CurrentUser::CurrentUser;
55 my $conf = new FS::Conf;
58 if ( $cgi->param('error') ) {
59 $prospectnum = scalar($cgi->param('prospectnum'));
62 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
64 } elsif ( $cgi->keywords ) { #editing
67 unless $curuser->access_right('Edit prospect');
69 } else { #new prospect
72 unless $curuser->access_right('New prospect');
76 my $new_callback = sub {
77 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
79 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
82 #add a link to the image.cgi for this card
83 $opt_hashref->{'html_bottom'} .=
84 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
85 ' WIDTH=604 HEIGHT=328><BR>';
87 #fill in the incoming params: name, address1/address2, city_state_zip
88 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
89 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
91 my $field = $cgi->param($param);
92 my $value = $cgi->param("val$num");
93 $cgi->param($field => $value);
96 if ( $cgi->param('company') ) {
97 $prospect_main->company( $cgi->param('company') );
100 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
101 $cgi->param('contactnum0_first' => $1);
102 $cgi->param('contactnum0_last' => $2);
105 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
106 $cgi->param('locationnum', -1);
107 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
108 $cgi->param('city' => $2);
109 $cgi->param('state' => $3);
110 $cgi->param('zip' => $4);
116 #config to default to commercial and/or disable residential when someone needs
117 $prospect_main->set('residential_commercial', 'Residential');
121 my $edit_callback = sub {
122 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
123 my( $cgi, $prospect_main ) = @_;
125 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
126 die 'multiple locations for prospect '. $prospect_main->prospectnum
127 if scalar(@cust_location) > 1;
128 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
129 if scalar(@cust_location);
130 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
132 $prospect_main->set('residential_commercial',
133 length($prospect_main->company)
139 my $error_callback = sub {
140 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
141 my( $cgi, $prospect_main ) = @_;
142 $cgi->param('locationnum') =~ /^(\-?\d*)$/
143 or die 'illegal locationnum '. $cgi->param('locationnum');
144 my $locationnum = $1;
145 $prospect_main->set('locationnum', $locationnum);
147 $prospect_main->set('residential_commercial',
148 ($cgi->param('residential_commercial') eq 'Commercial')
155 my $m2_error_callback = sub {
156 my($cgi, $object) = @_;
158 #process_o2m fields in process/prospect_main.html
159 my @fields = qw( first last title comment );
160 my @gfields = ( '', map "_$_", @fields );
163 if ( /^contactnum(\d+)$/ ) {
165 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
166 my $x = new FS::contact {
167 'contactnum' => scalar($cgi->param("contactnum$num")),
168 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
181 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
183 my $javascript = <<END;
184 <SCRIPT TYPE="text/javascript">
185 function rescom_changed() {
186 var f = document.edit_topform;
188 if ( f.residential_commercial_Residential.checked ) {
190 c.style.backgroundColor = '#dddddd';
191 } else if ( f.residential_commercial_Commercial.checked ) {
193 c.style.backgroundColor = '#ffffff';