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