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