qualification address handling changes, RT#7111
[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                             'company'     => 'Company',
7                             'contactnum'  => 'Contact',
8                             'locationnum' => '&nbsp;',
9                           },
10      'fields'          => [
11        { 'field'       => 'agentnum',
12          'type'        => 'select-agent',
13          'empty_label' => 'Select agent',
14        },
15        { 'field'   => 'company',
16          'type'   => 'text',
17          'size'    => 50,
18        },
19        { 'field'             => 'contactnum',
20          'type'              => 'contact',
21          'colspan'           => 6,
22          'o2m_table'      => 'contact',
23          'm2_label'       => 'Contact',
24          'm2_error_callback' => $m2_error_callback,
25
26        },
27        { 'field'       => 'locationnum',
28          'type'        => 'select-cust_location',
29          'empty_label' => 'No address',
30          'alt_format'  => $conf->exists('prospect_main-alt_address_format'),
31        },
32      ],
33      'new_callback'    => $new_callback,
34      'edit_callback'   => $edit_callback,
35      'error_callbacck' => $error_callback,
36      'agent_virt'      => 1,
37    )
38 %>
39 <%init>
40
41 my $curuser = $FS::CurrentUser::CurrentUser;
42 my $conf = new FS::Conf;
43
44 my $prospectnum;
45 if ( $cgi->param('error') ) {
46   $prospectnum = scalar($cgi->param('prospectnum'));
47
48   die "access denied"
49     unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
50
51 } elsif ( $cgi->keywords ) { #editing
52
53   die "access denied"
54     unless $curuser->access_right('Edit prospect');
55
56 } else { #new prospect 
57
58   die "access denied"
59     unless $curuser->access_right('New prospect');
60
61 }
62
63 my $new_callback = sub {
64   my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
65
66   if ( $cgi->param('session') =~ /^(\w+)$/ ) {
67     my $session = $1;
68
69     #add a link to the image.cgi for this card
70     $opt_hashref->{'html_bottom'} .=
71       qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
72       ' WIDTH=604 HEIGHT=328><BR>';
73
74     #fill in the incoming params: name, address1/address2, city_state_zip
75     foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
76       $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
77       my $num = $1;
78       my $field = $cgi->param($param);
79       my $value = $cgi->param("val$num");
80       $cgi->param($field => $value);
81     }
82
83     if ( $cgi->param('company') ) {
84       $prospect_main->company( $cgi->param('company') );
85     }
86
87     if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
88       $cgi->param('contactnum0_first' => $1);
89       $cgi->param('contactnum0_last'  => $2);
90     }
91
92     if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
93       $cgi->param('locationnum', -1);
94       if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
95          $cgi->param('city'  => $2);
96          $cgi->param('state' => $3);
97          $cgi->param('zip'   => $4);
98       }
99     }
100
101   }
102
103 };
104
105 my $edit_callback = sub {
106   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
107   my( $cgi, $prospect_main ) = @_;
108   my @cust_location =
109     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
110   die 'multiple locations for prospect '. $prospect_main->prospectnum
111     if scalar(@cust_location) > 1;
112   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
113     if scalar(@cust_location);
114   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
115 };
116
117 my $error_callback = sub {
118   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
119   my( $cgi, $prospect_main ) = @_;
120   $cgi->param('locationnum') =~ /^(\-?\d*)$/
121     or die 'illegal locationnum '. $cgi->param('locationnum');
122   my $locationnum = $1;
123   $prospect_main->set('locationnum', $locationnum);
124 };
125
126 my $m2_error_callback = sub {
127   my($cgi, $object) = @_;
128
129   #process_o2m fields in process/prospect_main.html
130   my @fields = qw( first last title comment );
131   my @gfields = ( '', map "_$_", @fields );
132
133   map {
134         if ( /^contactnum(\d+)$/ ) {
135           my $num = $1;
136           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
137             my $x = new FS::contact {
138               'contactnum' => scalar($cgi->param("contactnum$num")),
139               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
140             };
141             $x;
142           } else {
143             ();
144           }
145         } else {
146           ();
147         }
148       }
149       $cgi->param;
150 };
151
152 #my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
153
154 </%init>