RT# 75357 - v3 fix for creating contact rows in prospects
[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          'include_opt_callback' => sub { 'for_prospect' => '1' },
58
59        },
60        { 'field'         => 'locationnum',
61          'type'          => 'select-cust_location',
62          'empty_label'   => 'No address',
63          'disable_empty' => $conf->exists('prospect_main-location_required'),
64          'alt_format'    => $conf->exists('prospect_main-alt_address_format'),
65          'include_opt_callback' => sub { 
66             'prospect_main' => shift
67           },
68        },
69      ],
70      'new_callback'    => $new_callback,
71      'edit_callback'   => $edit_callback,
72      'error_callback'  => $error_callback,
73      'agent_virt'      => 1,
74      'html_bottom'     => $javascript,
75      'body_etc'        => 'onLoad="rescom_changed()"',
76    )
77 %>
78 <%init>
79
80 my $curuser = $FS::CurrentUser::CurrentUser;
81 my $conf = new FS::Conf;
82
83 my $prospectnum;
84 if ( $cgi->param('error') ) {
85   $prospectnum = scalar($cgi->param('prospectnum'));
86
87   die "access denied"
88     unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
89
90 } elsif ( $cgi->keywords ) { #editing
91
92   die "access denied"
93     unless $curuser->access_right('Edit prospect');
94
95 } else { #new prospect 
96
97   die "access denied"
98     unless $curuser->access_right('New prospect');
99
100 }
101
102 my $new_callback = sub {
103   my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
104
105   if ( $cgi->param('session') =~ /^(\w+)$/ ) {
106     my $session = $1;
107
108     #add a link to the image.cgi for this card
109     $opt_hashref->{'html_bottom'} .=
110       qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
111       ' WIDTH=604 HEIGHT=328><BR>';
112
113     #fill in the incoming params: name, address1/address2, city_state_zip
114     foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
115       $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
116       my $num = $1;
117       my $field = $cgi->param($param);
118       my $value = $cgi->param("val$num");
119       $cgi->param($field => $value);
120     }
121
122     if ( $cgi->param('company') ) {
123       $prospect_main->company( $cgi->param('company') );
124     }
125
126     if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
127       $cgi->param('contactnum0_first' => $1);
128       $cgi->param('contactnum0_last'  => $2);
129     }
130
131     if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
132       $cgi->param('locationnum', -1);
133       if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
134          $cgi->param('city'  => $2);
135          $cgi->param('state' => $3);
136          $cgi->param('zip'   => $4);
137       }
138     }
139
140   }
141
142   #config to default to commercial and/or disable residential when someone needs
143   $prospect_main->set('residential_commercial', 'Residential');
144
145 };
146
147 my $edit_callback = sub {
148   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
149   my( $cgi, $prospect_main ) = @_;
150   my @cust_location =
151     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
152   die 'multiple locations for prospect '. $prospect_main->prospectnum
153     if scalar(@cust_location) > 1;
154   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
155     if scalar(@cust_location);
156   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
157
158   $prospect_main->set('residential_commercial',
159     length($prospect_main->company)
160       ? 'Commercial'
161       : 'Residential'
162   );
163 };
164
165 my $error_callback = sub {
166   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
167   my( $cgi, $prospect_main ) = @_;
168   $cgi->param('locationnum') =~ /^(\-?\d*)$/
169     or die 'illegal locationnum '. $cgi->param('locationnum');
170   my $locationnum = $1;
171   $prospect_main->set('locationnum', $locationnum);
172
173   $prospect_main->set('residential_commercial',
174     ($cgi->param('residential_commercial') eq 'Commercial')
175       ? 'Commercial'
176       : 'Residential'
177   );
178
179 };
180
181 my $m2_error_callback = sub {
182   my($cgi, $object) = @_;
183
184   #process_o2m fields in process/prospect_main.html
185   my @fields = qw( first last title comment );
186   my @gfields = ( '', map "_$_", @fields );
187
188   map {
189         if ( /^contactnum(\d+)$/ ) {
190           my $num = $1;
191           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
192             my $x = new FS::contact {
193               'contactnum' => scalar($cgi->param("contactnum$num")),
194               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
195             };
196             $x;
197           } else {
198             ();
199           }
200         } else {
201           ();
202         }
203       }
204       $cgi->param;
205 };
206
207 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
208
209 my $javascript = <<END;
210   <SCRIPT TYPE="text/javascript">
211     function rescom_changed() {
212       var f = document.edit_topform;
213       var c = f.company;
214       if        ( f.residential_commercial_Residential.checked ) {
215         c.disabled = true;
216         c.style.backgroundColor = '#dddddd';
217       } else if ( f.residential_commercial_Commercial.checked ) {
218         c.disabled = false;
219         c.style.backgroundColor = '#ffffff';
220       }
221     }
222   </SCRIPT>
223 END
224
225 </%init>