summaryrefslogtreecommitdiff
path: root/httemplate/edit/prospect_main.html
blob: c260eb8e2c6d06098a2ec171fbf109150006d4a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<% include('elements/edit.html',
     'name_singular'   => 'prospect',
     'table'           => 'prospect_main',
     'labels'          => { 'prospectnum' => 'Prospect',
                            'agentnum'    => 'Agent',
                            'company'     => 'Company',
                            'contactnum'  => 'Contact',
                            'locationnum' => '&nbsp;',
                          },
     'fields'          => [
       { 'field'       => 'agentnum',
         'type'        => 'select-agent',
         'empty_label' => 'Select agent',
       },
       { 'field'   => 'company',
         'type'   => 'text',
         'size'    => 50,
       },
       { 'field'             => 'contactnum',
         'type'              => 'contact',
         'colspan'           => 6,
         ##actually o2m, but this seems to be working for edit so far
         #'m2name_table'      => 'contact',
         #'m2name_namecol'    => 'contactnum',
         #'m2_label'          => 'Contact',
         #'m2_error_callback' => $m2_error_callback,

         'o2m_table'      => 'contact',
         'm2_label'       => 'Contact',
         'm2_error_callback' => $m2_error_callback,

       },
       { 'field'       => 'locationnum',
         'type'        => 'select-cust_location',
         'empty_label' => 'No address',
       },
     ],
     'new_callback'    => $new_callback,
     'edit_callback'   => $edit_callback,
     'error_callbacck' => $error_callback,
     'agent_virt'      => 1,
   )
%>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

my $prospectnum;
if ( $cgi->param('error') ) {
  $prospectnum = scalar($cgi->param('prospectnum'));

  die "access denied"
    unless $curuser->access_right(($prospectnum ? 'Edit' : 'New'). ' prospect');

} elsif ( $cgi->keywords ) { #editing

  die "access denied"
    unless $curuser->access_right('Edit prospect');

} else { #new prospect 

  die "access denied"
    unless $curuser->access_right('New prospect');

}

my $new_callback = sub {
  my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;

  if ( $cgi->param('session') =~ /^(\w+)$/ ) {
    my $session = $1;

    #add a link to the image.cgi for this card
    $opt_hashref->{'html_bottom'} .=
      qq(<BR><IMG SRC="${p}view/image.cgi?type=png;prefname=bizcard$session" ).
      ' WIDTH=604 HEIGHT=328><BR>';

    #fill in the incoming params: name, address1/address2, city_state_zip
    foreach my $param ( grep /^sel\d+$/, $cgi->param ) {
      $param =~ /^sel(\d+)$/ or die 'again, wtf (daily)';
      my $num = $1;
      my $field = $cgi->param($param);
      my $value = $cgi->param("val$num");
      $cgi->param($field => $value);
    }

    if ( $cgi->param('company') ) {
      $prospect_main->company( $cgi->param('company') );
    }

    if ( $cgi->param('name') =~ /^(.*\S+)\s+(\w+)\s*$/ ) {
      $cgi->param('contactnum0_first' => $1);
      $cgi->param('contactnum0_last'  => $2);
    }

    if ( grep $cgi->param($_), qw( address1 address2 city_state_zip ) ) {
      $cgi->param('locationnum', -1);
      if ( $cgi->param('city_state_zip') =~ /^(\s*)([\w\s]+)[\., ]+(\w{2})[, ]+(\d{5}(-\d{4})?)/ ) {
         $cgi->param('city'  => $2);
         $cgi->param('state' => $3);
         $cgi->param('zip'   => $4);
      }
    }

  }

};

my $edit_callback = sub {
  #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
  my( $cgi, $prospect_main ) = @_;
  my @cust_location =
    qsearch('cust_location', { 'prospectnum' => $prospect_main->prospectnum } );
  die 'multiple locations for prospect '. $prospect_main->prospectnum
    if scalar(@cust_location) > 1;
  $prospect_main->set('locationnum', $cust_location[0]->locationnum)
    if scalar(@cust_location);
  #warn 'prospect_main.locationnum '.$prospect_main->get('locationnum');
};

my $error_callback = sub {
  #my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
  my( $cgi, $prospect_main ) = @_;
  $cgi->param('locationnum') =~ /^(\-?\d*)$/
    or die 'illegal locationnum '. $cgi->param('locationnum');
  my $locationnum = $1;
  $prospect_main->set('locationnum', $locationnum);
};

my $m2_error_callback = sub {
  my($cgi, $object) = @_;

  #process_o2m fields in process/prospect_main.html
  my @fields = qw( first last title comment );
  my @gfields = ( '', map "_$_", @fields );

  map {
        if ( /^contactnum(\d+)$/ ) {
          my $num = $1;
          if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
            my $x = new FS::contact {
              'contactnum' => $cgi->param("contactnum$num"),
              map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
            };
            $x;
          } else {
            ();
          }
        } else {
          ();
        }
      }
      $cgi->param;
};

#my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;

</%init>