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