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