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