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