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