beginning of prospect/CRM/contact work
[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                           },
9      'fields'          => [
10        { 'field'       => 'agentnum',
11          'type'        => 'select-agent',
12          'empty_label' => 'Select agent',
13        },
14        { 'field'   => 'company',
15          'type'   => 'text',
16          'size'    => 50,
17        },
18        { 'field'             => 'contactnum',
19          'type'              => 'contact',
20          'colspan'           => 6,
21          #actually o2m, but this seems to be working for edit so far
22          'm2name_table'      => 'contact',
23          'm2name_namecol'    => 'contactnum',
24          'm2_label'          => 'Contact',
25          'm2_error_callback' => sub { my($cgi, $object) = @_; (); }, #XXX
26        },
27        { 'field'       => 'locationnum',
28          'type'        => 'select-cust_location',
29          'empty_label' => 'No address',
30        },
31      ],
32      'edit_callback'   => $edit_callback,
33      'error_callbacck' => $error_callback,
34      'agent_virt'      => 1,
35    )
36 %>
37 <%init>
38
39 my $curuser = $FS::CurrentUser::CurrentUser;
40
41 my $prospectnum;
42 if ( $cgi->param('error') ) {
43   $prospectnum = scalar($cgi->param('prospectnum'));
44
45   die "access denied"
46     unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');
47
48 } elsif ( $cgi->keywords ) { #editing
49
50   die "access denied"
51     unless $curuser->access_right('Edit prospect');
52
53 } else { #new prospect 
54
55   die "access denied"
56     unless $curuser->access_right('New prospect');
57
58 }
59
60 my $edit_callback = sub {
61   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
62   my( $cgi, $prospect_main ) = @_;
63   my @cust_location =
64     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
65   die 'multiple locations for prospect '. $prospect_main->prospectnum
66     if scalar(@cust_location) > 1;
67   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
68     if scalar(@cust_location);
69   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
70 };
71
72 my $error_callback = sub {
73   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
74   my( $cgi, $prospect_main ) = @_;
75   $cgi->param('locationnum') =~ /^(\-?\d*)$/
76     or die 'illegal locationnum '. $cgi->param('locationnum');
77   my $locationnum = $1;
78   $prospect_main->set('locationnum', $locationnum);
79 };
80
81 my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
82
83 </%init>