Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / elements / tr-select-contact.html
1 <%doc>
2
3 Example:
4
5   include('/elements/tr-select-contact.html',
6             'cgi'       => $cgi,
7
8             'cust_main'     => $cust_main,
9             #or
10             'prospect_main' => $prospect_main,
11
12             #optional
13             'empty_label'   => '(default contact)',
14          )
15
16 </%doc>
17
18 <SCRIPT TYPE="text/javascript">
19
20   function contact_disable(what) {
21 %   for (@contact_fields) { 
22       what.form.<%$_%>.disabled = true;
23       var ftype = what.form.<%$_%>.tagName;
24       if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
25       else what.form.<%$_%>.value = '';
26       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
27 %   } 
28   }
29
30   function contact_clear(what) {
31 %   for (@contact_fields) { 
32       var ftype = what.form.<%$_%>.tagName;
33       if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
34 %   }
35   }
36
37   function contact_enable(what) {
38 %   for (@contact_fields) { 
39       what.form.<%$_%>.disabled = false;
40       var ftype = what.form.<%$_%>.tagName;
41       if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
42 %   } 
43   }
44
45   function contactnum_changed(what) {
46     var contactnum = what.options[what.selectedIndex].value;
47     if ( contactnum == -1 ) { //Add new contact
48       contact_clear(what);
49
50       contact_enable(what);
51       return;
52     }
53
54 %   if ( $editable ) {
55       if ( contactnum == 0 ) {
56 %   }
57
58 %       #sleep/wait until dropdowns are updated?
59         contact_disable(what);
60
61 %   if ( $editable ) {
62       } else {
63
64 %       #sleep/wait until dropdowns are updated?
65         contact_enable(what);
66
67       }
68 %   }
69
70   }
71
72   function changeSelect(what, value) {
73     for ( var i=0; i<what.length; i++) {
74       if ( what.options[i].value == value ) {
75         what.selectedIndex = i;
76       }
77     }
78   }
79
80 </SCRIPT>
81
82 <TR>
83   <<%$th%> ALIGN="right" VALIGN="top"><% $opt{'label'} || emt('Service contact') %></<%$th%>>
84   <TD VALIGN="top" COLSPAN=7>
85     <SELECT NAME     = "contactnum"
86             ID       = "contactnum"
87             STYLE    = "vertical-align:top;margin:3px"
88             onchange = "contactnum_changed(this);"
89     >
90 % if ( $cust_main ) {
91       <OPTION VALUE=""><% $opt{'empty_label'} || '(customer default)' |h %>
92 % }
93 %
94 %     foreach my $contact ( @contact ) {
95         <OPTION VALUE="<% $contact->contactnum %>"
96                 <% $contactnum == $contact->contactnum ? 'SELECTED' : '' %>
97         ><% $contact->line |h %>
98 %     }
99 %     if ( $addnew ) {
100         <OPTION VALUE="-1"
101                 <% $contactnum == -1 ? 'SELECTED' : '' %>
102         >New contact
103 %     }
104     </SELECT>
105
106 <% include('/elements/contact.html',
107              'object'       => $contact,
108              #'onchange' ?  probably not
109              'disabled'     => $disabled,
110              'name_only'    => 1,
111           )
112 %>
113
114   </TD>
115 </TR>
116
117 <SCRIPT TYPE="text/javascript">
118   contactnum_changed(document.getElementById('contactnum'));
119 </SCRIPT>
120 <%init>
121
122 #based on / kinda false laziness w/tr-select-cust_contact.html
123
124 my $conf = new FS::Conf;
125
126 my %opt = @_;
127 my $cgi           = $opt{'cgi'};
128 my $cust_pkg      = $opt{'cust_pkg'};
129 my $cust_main     = $opt{'cust_main'};
130 my $prospect_main = $opt{'prospect_main'};
131 die "cust_main or prospect_main required" unless $cust_main or $prospect_main;
132
133 my $contactnum = '';
134 if ( $cgi->param('error') ) {
135   $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
136   $contactnum = $1;
137 } else {
138   if ( length($opt{'curr_value'}) ) {
139     $contactnum = $opt{'curr_value'};
140   } elsif ($prospect_main) {
141     my @prospect_contact = $prospect_main->prospect_contact;
142     $contactnum = $prospect_contact[0]->contactnum if scalar(@prospect_contact)==1;
143   } else { #$cust_main
144     $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
145     $contactnum = $1;
146   }
147 }
148
149 ##probably could use explicit controls
150 #my $editable = $cust_main ? 0 : 1; #could use explicit control
151 my $editable = 0;
152 my $addnew = $cust_main ? 1 : ( $contactnum>0 ? 0 : 1 );
153
154 my @contact_fields = map "contactnum_$_", qw( first last );
155
156 my $contact; #the one that shows by default in the contact edit space
157 if ( $contactnum && $contactnum > 0 ) {
158   $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
159     or die "unknown contactnum";
160 } else {
161   $contact = new FS::contact;
162   if ( $contactnum == -1 ) {
163     $contact->$_( $cgi->param($_) ) foreach @contact_fields; #XXX
164   } elsif ( $cust_pkg && $cust_pkg->contactnum ) {
165     my $pkg_contact = $cust_pkg->contact_obj;
166     $contact->$_( $pkg_contact->$_ ) foreach @contact_fields; #XXX why are we making a new one gagain??
167     $opt{'empty_label'} ||= 'package contact: '.$pkg_contact->line;
168   } elsif ( $cust_main ) {
169     $contact = new FS::contact; #I think
170   }
171 }
172
173 my $contact_sort = sub {
174      lc($a->last)  cmp lc($b->last)
175   or lc($a->first) cmp lc($b->first)
176 };
177
178 my @contact;
179 push @contact, map $_->contact, $cust_main->cust_contact
180   if $cust_main;
181 push @contact, map $_->contact, $prospect_main->prospect_contact
182   if $prospect_main;
183 push @contact, $contact
184   if !$cust_main && $contact && $contact->contactnum > 0
185   && ! grep { $_->contactnum == $contact->contactnum } @contact;
186
187 @contact = sort $contact_sort grep !$_->disabled, @contact;
188
189 $contact = $contact[0]
190   if ( $prospect_main )
191   && !$opt{'is_optional'}
192   && @contact;
193
194 my $disabled =
195   ( $contactnum < 0
196     || ( $editable && $contactnum )
197     || ( $prospect_main
198          && !$opt{'is_optional'} && !@contact && $addnew
199        )
200   )
201     ? ''
202     : 'DISABLED';
203
204 my $th = $opt{'no_bold'} ? 'TD' : 'TH';
205
206 </%init>