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