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