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