5 include('/elements/tr-select-contact.html',
8 'cust_main' => $cust_main,
10 'prospect_main' => $prospect_main,
13 'empty_label' => '(default contact)',
18 <SCRIPT TYPE="text/javascript">
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';
30 function contact_clear(what) {
31 % for (@contact_fields) {
32 var ftype = what.form.<%$_%>.tagName;
33 if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
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';
45 function contactnum_changed(what) {
46 var contactnum = what.options[what.selectedIndex].value;
47 if ( contactnum == -1 ) { //Add new contact
55 if ( contactnum == 0 ) {
58 % #sleep/wait until dropdowns are updated?
59 contact_disable(what);
64 % #sleep/wait until dropdowns are updated?
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;
83 <<%$th%> ALIGN="right" VALIGN="top"><% $opt{'label'} || emt('Service contact') %></<%$th%>>
84 <TD VALIGN="top" COLSPAN=7>
85 <SELECT NAME = "contactnum"
87 STYLE = "vertical-align:top;margin:3px"
88 onchange = "contactnum_changed(this);"
91 <OPTION VALUE=""><% $opt{'empty_label'} || '(customer default)' |h %>
94 % foreach my $contact ( @contact ) {
95 <OPTION VALUE="<% $contact->contactnum %>"
96 <% $contactnum == $contact->contactnum ? 'SELECTED' : '' %>
97 ><% $contact->line |h %>
101 <% $contactnum == -1 ? 'SELECTED' : '' %>
106 <% include('/elements/contact.html',
107 'object' => $contact,
108 #'onchange' ? probably not
109 'disabled' => $disabled,
117 <SCRIPT TYPE="text/javascript">
118 contactnum_changed(document.getElementById('contactnum'));
122 #based on / kinda false laziness w/tr-select-cust_contact.html
124 my $conf = new FS::Conf;
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;
134 if ( $cgi->param('error') ) {
135 $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
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;
144 $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
149 ##probably could use explicit controls
150 #my $editable = $cust_main ? 0 : 1; #could use explicit control
152 my $addnew = $cust_main ? 1 : ( $contactnum>0 ? 0 : 1 );
154 my @contact_fields = map "contactnum_$_", qw( first last );
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";
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
173 my $contact_sort = sub {
174 lc($a->last) cmp lc($b->last)
175 or lc($a->first) cmp lc($b->first)
179 push @contact, map $_->contact, $cust_main->cust_contact
181 push @contact, map $_->contact, $prospect_main->prospect_contact
183 push @contact, $contact
184 if !$cust_main && $contact && $contact->contactnum > 0
185 && ! grep { $_->contactnum == $contact->contactnum } @contact;
187 @contact = sort $contact_sort grep !$_->disabled, @contact;
189 $contact = $contact[0]
190 if ( $prospect_main )
191 && !$opt{'is_optional'}
196 || ( $editable && $contactnum )
198 && !$opt{'is_optional'} && !@contact && $addnew
204 my $th = $opt{'no_bold'} ? 'TD' : 'TH';