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 'o2m_table' => 'contact',
38 'm2_label' => 'Contact',
39 'm2_error_callback' => $m2_error_callback,
42 { 'field' => 'locationnum',
43 'type' => 'select-cust_location',
44 'empty_label' => 'No address',
45 'disable_empty' => $conf->exists('prospect_main-location_required'),
46 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
47 'include_opt_callback' => sub {
48 'prospect_main' => shift
51 { 'field' => 'taxstatusnum',
52 'type' => 'select-tax_status',
56 'new_callback' => $new_callback,
57 'edit_callback' => $edit_callback,
58 'error_callback' => $error_callback,
60 'html_bottom' => $javascript,
61 'body_etc' => 'onLoad="rescom_changed()"',
66 my $curuser = $FS::CurrentUser::CurrentUser;
67 my $conf = new FS::Conf;
70 if ( $cgi->param('error') ) {
71 $prospectnum = scalar($cgi->param('prospectnum'));
74 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
76 } elsif ( $cgi->keywords ) { #editing
79 unless $curuser->access_right('Edit prospect');
81 } else { #new prospect
84 unless $curuser->access_right('New prospect');
88 my $new_callback = sub {
89 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
91 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
94 #add a link to the image.cgi for this card
95 $opt_hashref->{'html_bottom'} .=
96 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
97 ' WIDTH=604 HEIGHT=328><BR>';
99 #fill in the incoming params: name, address1/address2, city_state_zip
100 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
101 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
103 my $field = $cgi->param($param);
104 my $value = $cgi->param("val$num");
105 $cgi->param($field => $value);
108 if ( $cgi->param('company') ) {
109 $prospect_main->company( $cgi->param('company') );
112 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
113 $cgi->param('contactnum0_first' => $1);
114 $cgi->param('contactnum0_last' => $2);
117 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
118 $cgi->param('locationnum', -1);
119 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
120 $cgi->param('city' => $2);
121 $cgi->param('state' => $3);
122 $cgi->param('zip' => $4);
128 #config to default to commercial and/or disable residential when someone needs
129 $prospect_main->set('residential_commercial', 'Residential');
133 my $edit_callback = sub {
134 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
135 my( $cgi, $prospect_main ) = @_;
137 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
138 die 'multiple locations for prospect '. $prospect_main->prospectnum
139 if scalar(@cust_location) > 1;
140 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
141 if scalar(@cust_location);
142 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
144 $prospect_main->set('residential_commercial',
145 length($prospect_main->company)
151 my $error_callback = sub {
152 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
153 my( $cgi, $prospect_main ) = @_;
154 $cgi->param('locationnum') =~ /^(\-?\d*)$/
155 or die 'illegal locationnum '. $cgi->param('locationnum');
156 my $locationnum = $1;
157 $prospect_main->set('locationnum', $locationnum);
159 $prospect_main->set('residential_commercial',
160 ($cgi->param('residential_commercial') eq 'Commercial')
167 my $m2_error_callback = sub {
168 my($cgi, $object) = @_;
170 #process_o2m fields in process/prospect_main.html
171 my @fields = qw( first last title comment );
172 my @gfields = ( '', map "_$_", @fields );
175 if ( /^contactnum(\d+)$/ ) {
177 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
178 my $x = new FS::contact {
179 'contactnum' => scalar($cgi->param("contactnum$num")),
180 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
193 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
195 my $javascript = <<END;
196 <SCRIPT TYPE="text/javascript">
197 function rescom_changed() {
198 var f = document.edit_topform;
200 if ( f.residential_commercial_Residential.checked ) {
202 c.style.backgroundColor = '#dddddd';
203 } else if ( f.residential_commercial_Commercial.checked ) {
205 c.style.backgroundColor = '#ffffff';