remove debugging
[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' => $m2_error_callback,
26
27          'o2m_table'      => 'contact',
28          'm2_label'       => 'Contact',
29          'm2_error_callback' => $m2_error_callback,
30
31        },
32        { 'field'       => 'locationnum',
33          'type'        => 'select-cust_location',
34          'empty_label' => 'No address',
35        },
36      ],
37      'edit_callback'   => $edit_callback,
38      'error_callbacck' => $error_callback,
39      'agent_virt'      => 1,
40    )
41 %>
42 <%init>
43
44 my $curuser = $FS::CurrentUser::CurrentUser;
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 $edit_callback = sub {
66   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
67   my( $cgi, $prospect_main ) = @_;
68   my @cust_location =
69     qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
70   die 'multiple locations for prospect '. $prospect_main->prospectnum
71     if scalar(@cust_location) > 1;
72   $prospect_main->set('locationnum', $cust_location[0]->locationnum)
73     if scalar(@cust_location);
74   #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
75 };
76
77 my $error_callback = sub {
78   #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
79   my( $cgi, $prospect_main ) = @_;
80   $cgi->param('locationnum') =~ /^(\-?\d*)$/
81     or die 'illegal locationnum '. $cgi->param('locationnum');
82   my $locationnum = $1;
83   $prospect_main->set('locationnum', $locationnum);
84 };
85
86 my $m2_error_callback = sub {
87   my($cgi, $object) = @_;
88
89   #process_o2m fields in process/prospect_main.html
90   my @fields = qw( first last title comment );
91   my @gfields = ( '', map "_$_", @fields );
92
93   map {
94         if ( /^contactnum(\d+)$/ ) {
95           my $num = $1;
96           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
97             my $x = new FS::contact {
98               'contactnum' => $cgi->param("contactnum$num"),
99               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
100             };
101             $x;
102           } else {
103             ();
104           }
105         } else {
106           ();
107         }
108       }
109       $cgi->param;
110 };
111
112 my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
113
114 </%init>