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