RT# 75357 - Prospects can now be disabled
[freeside.git] / httemplate / edit / prospect_main.html
1 <SCRIPT>
2   function checkPasswordValidation(fieldid)  {
3     var validationResult = document.getElementById(fieldid+'_result').innerHTML;
4     if (validationResult.match(/Password valid!/)) {
5       return true;
6     }
7     else {
8       return false;
9     }
10   }
11 </SCRIPT>
12
13 <& '/elements/validate_password_js.html', &>
14
15 <% include('elements/edit.html',
16      'name_singular'   => 'prospect',
17      'table'           => 'prospect_main',
18      'labels'          => { 'prospectnum' => 'Prospect',
19                             'disabled'    => 'Disabled',
20                             'agentnum'    => 'Agent',
21                             'refnum'      => 'Advertising source',
22                             'company'     => 'Company',
23                             'contactnum'  => 'Contact',
24                             'locationnum' => '&nbsp;',
25                           },
26      'fields'          => [
27        { 'field'       => 'agentnum',
28          'type'        => 'select-agent',
29          'empty_label' => 'Select agent',
30          'colspan'     => 6,
31        },
32        { 'field'       => 'refnum',
33          'type'        => 'select-part_referral',
34          'empty_label' => 'Select advertising source',
35          'colspan'     => 6,
36        },
37        { 'field'    => 'residential_commercial',
38          'type'     => 'radio',
39          'options'  => [ 'Residential', 'Commercial', ],
40          'onchange' => 'rescom_changed',
41        },
42        { 'field'    => 'disabled',
43          'type'     => 'checkbox',
44          'value'    => 'Y',
45        },
46        { 'field'    => 'company',
47          'type'     => 'text',
48          'size'     => 50,
49          'colspan'  => 6,
50        },
51        { 'field'             => 'contactnum',
52          'type'              => 'contact',
53          'colspan'           => 6,
54          'o2m_table'      => 'contact',
55          'm2_label'       => 'Contact',
56          'm2_error_callback' => $m2_error_callback,
57
58        },
59        { 'field'         => 'locationnum',
60          'type'          => 'select-cust_location',
61          'empty_label'   => 'No address',
62          'disable_empty' => $conf->exists('prospect_main-location_required'),
63          'alt_format'    => $conf->exists('prospect_main-alt_address_format'),
64          'include_opt_callback' => sub { 
65             'prospect_main' => shift
66           },
67        },
68      ],
69      'new_callback'    => $new_callback,
70      'edit_callback'   => $edit_callback,
71      'error_callback'  => $error_callback,
72      'agent_virt'      => 1,
73      'html_bottom'     => $javascript,
74      'body_etc'        => 'onLoad="rescom_changed()"',
75    )
76 %>
77 <%init>
78
79 my $curuser = $FS::CurrentUser::CurrentUser;
80 my $conf = new FS::Conf;
81
82 my $prospectnum;
83 if ( $cgi->param('error') ) {
84   $prospectnum = scalar($cgi->param('prospectnum'));
85
86   die "access denied"
87     unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
88
89 } elsif ( $cgi->keywords ) { #editing
90
91   die "access denied"
92     unless $curuser->access_right('Edit prospect');
93
94 } else { #new prospect 
95
96   die "access denied"
97     unless $curuser->access_right('New prospect');
98
99 }
100
101 my $new_callback = sub {
102   my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
103
104   if ( $cgi->param('session') =~ /^(\w+)$/ ) {
105     my $session = $1;
106
107     #add a link to the image.cgi for this card
108     $opt_hashref->{'html_bottom'} .=
109       qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
110       ' WIDTH=604 HEIGHT=328><BR>';
111
112     #fill in the incoming params: name, address1/address2, city_state_zip
113     foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
114       $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
115       my $num = $1;
116       my $field = $cgi->param($param);
117       my $value = $cgi->param("val$num");
118       $cgi->param($field => $value);
119     }
120
121     if ( $cgi->param('company') ) {
122       $prospect_main->company( $cgi->param('company') );
123     }
124
125     if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
126       $cgi->param('contactnum0_first' => $1);
127       $cgi->param('contactnum0_last'  => $2);
128     }
129
130     if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
131       $cgi->param('locationnum', -1);
132       if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
133          $cgi->param('city'  => $2);
134          $cgi->param('state' => $3);
135          $cgi->param('zip'   => $4);
136       }
137     }
138
139   }
140
141   #config to default to commercial and/or disable residential when someone needs
142   $prospect_main->set('residential_commercial', 'Residential');
143
144 };
145
146 my $edit_callback = sub {
147   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
148   my( $cgi, $prospect_main ) = @_;
149   my @cust_location =
150     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
151   die 'multiple locations for prospect '. $prospect_main->prospectnum
152     if scalar(@cust_location) > 1;
153   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
154     if scalar(@cust_location);
155   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
156
157   $prospect_main->set('residential_commercial',
158     length($prospect_main->company)
159       ? 'Commercial'
160       : 'Residential'
161   );
162 };
163
164 my $error_callback = sub {
165   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
166   my( $cgi, $prospect_main ) = @_;
167   $cgi->param('locationnum') =~ /^(\-?\d*)$/
168     or die 'illegal locationnum '. $cgi->param('locationnum');
169   my $locationnum = $1;
170   $prospect_main->set('locationnum', $locationnum);
171
172   $prospect_main->set('residential_commercial',
173     ($cgi->param('residential_commercial') eq 'Commercial')
174       ? 'Commercial'
175       : 'Residential'
176   );
177
178 };
179
180 my $m2_error_callback = sub {
181   my($cgi, $object) = @_;
182
183   #process_o2m fields in process/prospect_main.html
184   my @fields = qw( first last title comment );
185   my @gfields = ( '', map "_$_", @fields );
186
187   map {
188         if ( /^contactnum(\d+)$/ ) {
189           my $num = $1;
190           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
191             my $x = new FS::contact {
192               'contactnum' => scalar($cgi->param("contactnum$num")),
193               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
194             };
195             $x;
196           } else {
197             ();
198           }
199         } else {
200           ();
201         }
202       }
203       $cgi->param;
204 };
205
206 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
207
208 my $javascript = <<END;
209   <SCRIPT TYPE="text/javascript">
210     function rescom_changed() {
211       var f = document.edit_topform;
212       var c = f.company;
213       if        ( f.residential_commercial_Residential.checked ) {
214         c.disabled = true;
215         c.style.backgroundColor = '#dddddd';
216       } else if ( f.residential_commercial_Commercial.checked ) {
217         c.disabled = false;
218         c.style.backgroundColor = '#ffffff';
219       }
220     }
221   </SCRIPT>
222 END
223
224 </%init>