1 <% include('elements/edit.html',
2 'name_singular' => 'prospect',
3 'table' => 'prospect_main',
4 'html_table_class'=> 'fsinnerbox',
5 'labels' => { 'prospectnum' => 'Prospect',
6 'disabled' => 'Disabled',
8 'refnum' => 'Advertising source',
9 'company' => 'Company',
10 'contactnum' => 'Contact',
11 'locationnum' => ' ',
12 'taxstatusnum'=> 'Tax status',
15 { 'field' => 'residential_commercial',
17 'options' => [ 'Residential', 'Commercial', ],
18 'onchange' => 'rescom_changed',
20 { 'field' => 'disabled',
24 { 'field' => 'agentnum',
25 'type' => 'select-agent',
26 'empty_label' => 'Select agent',
30 { 'field' => 'refnum',
31 'type' => 'select-part_referral',
32 'empty_label' => 'Select advertising source',
35 { 'field' => 'contactnum',
38 'prospectnum' => $prospectnum,
39 'm2m_method' => 'prospect_contact',
40 'm2m_dstcol' => 'contactnum',
41 'm2_label' => 'Contact',
42 'm2_error_callback' => $m2_error_callback,
43 'include_opt_callback' => sub { 'for_prospect' => 1 },
44 'js_spawn_test' => 'document.edit_topform.residential_commercial_Commercial.checked',
46 { 'field' => 'company',
51 { 'field' => 'locationnum',
52 'type' => 'select-cust_location',
53 'empty_label' => 'No address',
54 'disable_empty' => $conf->exists('prospect_main-location_required'),
55 'alt_format' => $conf->exists('prospect_main-alt_address_format'),
56 'include_opt_callback' => sub {
57 'prospect_main' => shift
60 { 'field' => 'taxstatusnum',
61 'type' => 'select-tax_status',
66 'new_callback' => $new_callback,
67 'edit_callback' => $edit_callback,
68 'error_callback' => $error_callback,
70 'html_bottom' => $javascript,
71 'body_etc' => 'onLoad="rescom_changed()"',
76 my $curuser = $FS::CurrentUser::CurrentUser;
77 my $conf = new FS::Conf;
78 my @agentnums = $curuser->agentnums;
81 if ( $cgi->param('error') ) {
82 $cgi->param('prospectnum') =~ /^(\d*)$/ or die 'illegal prospectnum';
86 unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
88 } elsif ( $cgi->keywords ) { #editing
90 my($query) = $cgi->keywords;
91 $query =~ /^(\d+)$/ or die 'no prospectnum';
95 unless $curuser->access_right('Edit prospect');
97 } else { #new prospect
102 unless $curuser->access_right('New prospect');
106 my $new_callback = sub {
107 my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
109 if ( $cgi->param('session') =~ /^(\w+)$/ ) {
112 #add a link to the image.cgi for this card
113 $opt_hashref->{'html_bottom'} .=
114 qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
115 ' WIDTH=604 HEIGHT=328><BR>';
117 #fill in the incoming params: name, address1/address2, city_state_zip
118 foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
119 $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
121 my $field = $cgi->param($param);
122 my $value = $cgi->param("val$num");
123 $cgi->param($field => $value);
126 if ( $cgi->param('company') ) {
127 $prospect_main->company( $cgi->param('company') );
130 if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
131 $cgi->param('contactnum0_first' => $1);
132 $cgi->param('contactnum0_last' => $2);
135 if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
136 $cgi->param('locationnum', -1);
137 if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
138 $cgi->param('city' => $2);
139 $cgi->param('state' => $3);
140 $cgi->param('zip' => $4);
146 #config to default to commercial and/or disable residential when someone needs
147 $prospect_main->set('residential_commercial', 'Residential');
149 # config to set default agent
150 $prospect_main->agentnum( $agentnums[0] )
151 if scalar(@agentnums) == 1;
152 $prospect_main->agentnum( $conf->config('default_agentnum') )
153 if $conf->exists('default_agentnum');
157 my $edit_callback = sub {
158 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
159 my( $cgi, $prospect_main ) = @_;
161 qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
162 die 'multiple locations for prospect '. $prospect_main->prospectnum
163 if scalar(@cust_location) > 1;
164 $prospect_main->set('locationnum', $cust_location[0]->locationnum)
165 if scalar(@cust_location);
166 #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
168 $prospect_main->set('residential_commercial',
169 length($prospect_main->company)
175 my $error_callback = sub {
176 #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
177 my( $cgi, $prospect_main ) = @_;
178 $cgi->param('locationnum') =~ /^(\-?\d*)$/
179 or die 'illegal locationnum '. $cgi->param('locationnum');
180 my $locationnum = $1;
181 $prospect_main->set('locationnum', $locationnum);
183 $prospect_main->set('residential_commercial',
184 ($cgi->param('residential_commercial') eq 'Commercial')
191 my $m2_error_callback = sub {
192 my($cgi, $object) = @_;
194 #process_o2m fields in process/prospect_main.html
195 my @fields = qw( first last title comment );
196 my @gfields = ( '', map "_$_", @fields );
199 if ( /^contactnum(\d+)$/ ) {
201 if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
202 my $x = new FS::contact {
203 'contactnum' => scalar($cgi->param("contactnum$num")),
204 map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
218 <SCRIPT TYPE="text/javascript">
219 function rescom_changed() {
221 var f = document.edit_topform;
222 if ( f.residential_commercial_Residential.checked ) {
223 $('#company_label').slideUp();
224 $('#company_input0').slideUp();
225 } else if ( f.residential_commercial_Commercial.checked ) {
226 $('#company_label').slideDown();
227 $('#company_input0').slideDown();