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