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