From 2e761a72c380dc9d7320ccd822cfe8476534b369 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Sun, 14 Apr 2013 17:08:36 -0700 Subject: [PATCH] package contacts / "name per packages", RT#22185 --- FS/FS/Schema.pm | 1 + FS/FS/contact_Mixin.pm | 19 +++ FS/FS/cust_main/Packages.pm | 22 +++ FS/FS/cust_pkg.pm | 5 +- FS/MANIFEST | 2 + FS/t/contact_Mixin.t | 5 + httemplate/elements/contact.html | 6 +- httemplate/elements/tr-select-contact.html | 204 +++++++++++++++++++++++ httemplate/misc/order_pkg.html | 6 + httemplate/view/cust_main/packages.html | 7 +- httemplate/view/cust_main/packages/contact.html | 48 ++++++ httemplate/view/cust_main/packages/location.html | 4 - httemplate/view/cust_main/packages/package.html | 5 - httemplate/view/cust_main/packages/section.html | 17 +- httemplate/view/cust_main/packages/status.html | 2 +- 15 files changed, 323 insertions(+), 30 deletions(-) create mode 100644 FS/FS/contact_Mixin.pm create mode 100644 FS/t/contact_Mixin.t create mode 100644 httemplate/elements/tr-select-contact.html create mode 100644 httemplate/view/cust_main/packages/contact.html diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 924d0d999..bbf3b42fe 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -1721,6 +1721,7 @@ sub tables_hashref { 'custnum', 'int', '', '', '', '', 'pkgpart', 'int', '', '', '', '', 'pkgbatch', 'varchar', 'NULL', $char_d, '', '', + 'contactnum', 'int', 'NULL', '', '', '', 'locationnum', 'int', 'NULL', '', '', '', 'otaker', 'varchar', 'NULL', 32, '', '', 'usernum', 'int', 'NULL', '', '', '', diff --git a/FS/FS/contact_Mixin.pm b/FS/FS/contact_Mixin.pm new file mode 100644 index 000000000..33cd3509a --- /dev/null +++ b/FS/FS/contact_Mixin.pm @@ -0,0 +1,19 @@ +package FS::contact_Mixin; + +use strict; +use FS::Record qw( qsearchs ); +use FS::contact; + +=item contact + +Returns the contact object, if any (see L). + +=cut + +sub contact { + my $self = shift; + return '' unless $self->contactnum; + qsearchs( 'contact', { 'contactnum' => $self->contactnum } ); +} + +1; diff --git a/FS/FS/cust_main/Packages.pm b/FS/FS/cust_main/Packages.pm index 29e3bec13..f83bce915 100644 --- a/FS/FS/cust_main/Packages.pm +++ b/FS/FS/cust_main/Packages.pm @@ -100,6 +100,28 @@ sub order_pkg { local $FS::UID::AutoCommit = 0; my $dbh = dbh; + if ( $opt->{'contactnum'} and $opt->{'contactnum'} != -1 ) { + + $cust_pkg->contactnum($opt->{'contactnum'}); + + } elsif ( $opt->{'contact'} ) { + + if ( ! $opt->{'contact'}->contactnum ) { + # not inserted yet + my $error = $opt->{'contact'}->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting contact (transaction rolled back): $error"; + } + } + $cust_pkg->contactnum($opt->{'contact'}->contactnum); + + #} else { + # + # $cust_pkg->contactnum(); + + } + if ( $opt->{'locationnum'} and $opt->{'locationnum'} != -1 ) { $cust_pkg->locationnum($opt->{'locationnum'}); diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index b2cb4139d..bb1b4e37a 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -1,7 +1,8 @@ package FS::cust_pkg; use strict; -use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::location_Mixin +use base qw( FS::otaker_Mixin FS::cust_main_Mixin + FS::contact_Mixin FS::location_Mixin FS::m2m_Common FS::option_Common ); use vars qw($disable_agentcheck $DEBUG $me); use Carp qw(cluck); @@ -17,6 +18,7 @@ use FS::CurrentUser; use FS::cust_svc; use FS::part_pkg; use FS::cust_main; +use FS::contact; use FS::cust_location; use FS::pkg_svc; use FS::cust_bill_pkg; @@ -620,6 +622,7 @@ sub check { $self->ut_numbern('pkgnum') || $self->ut_foreign_key('custnum', 'cust_main', 'custnum') || $self->ut_numbern('pkgpart') + || $self->ut_foreign_keyn('contactnum', 'contact', 'contactnum' ) || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum') || $self->ut_numbern('start_date') || $self->ut_numbern('setup') diff --git a/FS/MANIFEST b/FS/MANIFEST index da4832d41..94232905e 100644 --- a/FS/MANIFEST +++ b/FS/MANIFEST @@ -490,6 +490,8 @@ FS/phone_type.pm t/phone_type.t FS/contact_email.pm t/contact_email.t +FS/contact_Mixin.pm +t/contact_Mixin.t FS/prospect_main.pm t/prospect_main.t FS/o2m_Common.pm diff --git a/FS/t/contact_Mixin.t b/FS/t/contact_Mixin.t new file mode 100644 index 000000000..89dcc37c5 --- /dev/null +++ b/FS/t/contact_Mixin.t @@ -0,0 +1,5 @@ +BEGIN { $| = 1; print "1..1\n" } +END {print "not ok 1\n" unless $loaded;} +use FS::contact_Mixin; +$loaded=1; +print "ok 1\n"; diff --git a/httemplate/elements/contact.html b/httemplate/elements/contact.html index 490ba2303..3d5177612 100644 --- a/httemplate/elements/contact.html +++ b/httemplate/elements/contact.html @@ -2,9 +2,9 @@ - +
-% if ( @contact_class ) { +% if ( @contact_class && ! $opt{name_only} ) { + <<%$th%> ALIGN="right" VALIGN="top"><% $opt{'label'} || emt('Service contact') %>> + + + + +<%init> + +#based on / kinda false laziness w/tr-select-cust_contact.html + +my $conf = new FS::Conf; + +my %opt = @_; +my $cgi = $opt{'cgi'}; +my $cust_pkg = $opt{'cust_pkg'}; +my $cust_main = $opt{'cust_main'}; +my $prospect_main = $opt{'prospect_main'}; +die "cust_main or prospect_main required" unless $cust_main or $prospect_main; + +my $contactnum = ''; +if ( $cgi->param('error') ) { + $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum"; + $contactnum = $1; +} else { + if ( length($opt{'curr_value'}) ) { + $contactnum = $opt{'curr_value'}; + } elsif ($prospect_main) { + my @cust_contact = $prospect_main->cust_contact; + $contactnum = $cust_contact[0]->contactnum if scalar(@cust_contact)==1; + } else { #$cust_main + $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum"; + $contactnum = $1; + } +} + +##probably could use explicit controls +#my $editable = $cust_main ? 0 : 1; #could use explicit control +my $editable = 0; +my $addnew = $cust_main ? 1 : ( $contactnum>0 ? 0 : 1 ); + +my @contact_fields = map "contactnum_$_", qw( first last ); + +my $contact; #the one that shows by default in the contact edit space +if ( $contactnum && $contactnum > 0 ) { + $contact = qsearchs('contact', { 'contactnum' => $contactnum } ) + or die "unknown contactnum"; +} else { + $contact = new FS::contact; + if ( $contactnum == -1 ) { + $contact->$_( $cgi->param($_) ) foreach @contact_fields; #XXX + } elsif ( $cust_pkg && $cust_pkg->contactnum ) { + my $pkg_contact = $cust_pkg->contact; + $contact->$_( $pkg_contact->$_ ) foreach @contact_fields; #XXX why are we making a new one gagain?? + $opt{'empty_label'} ||= 'package contact: '.$pkg_contact->line; + } elsif ( $cust_main ) { + $contact = new FS::contact; #I think + } +} + +my $contact_sort = sub { + lc($a->last) cmp lc($b->last) + or lc($a->first) cmp lc($b->first) +}; + +my @contact; +push @contact, $cust_main->cust_contact if $cust_main; +push @contact, $prospect_main->contact if $prospect_main; +push @contact, $contact + if !$cust_main && $contact && $contact->contactnum > 0 + && ! grep { $_->contactnum == $contact->contactnum } @contact; + +@contact = sort $contact_sort grep !$_->disabled, @contact; + +$contact = $contact[0] + if ( $prospect_main ) + && !$opt{'is_optional'} + && @contact; + +my $disabled = + ( $contactnum < 0 + || ( $editable && $contactnum ) + || ( $prospect_main + && !$opt{'is_optional'} && !@contact && $addnew + ) + ) + ? '' + : 'DISABLED'; + +my $th = $opt{'no_bold'} ? 'TD' : 'TH'; + + diff --git a/httemplate/misc/order_pkg.html b/httemplate/misc/order_pkg.html index 993ea366c..e09ba986d 100644 --- a/httemplate/misc/order_pkg.html +++ b/httemplate/misc/order_pkg.html @@ -93,6 +93,12 @@ &> % } +<& /elements/tr-select-contact.html, + 'cgi' => $cgi, + 'cust_main' => $cust_main, + 'prospect_main' => $prospect_main, +&> + % if ( $cgi->param('lock_locationnum') ) {
+ + +<% include('/elements/contact.html', + 'object' => $contact, + #'onchange' ? probably not + 'disabled' => $disabled, + 'name_only' => 1, + ) +%> + +
-% if ( $conf->exists('cust_pkg-group_by_location') and $show_location ) { +% if ( $conf->exists('cust_pkg-group_by_location') ) { <& locations.html, 'cust_main' => $cust_main, 'packages' => $packages, @@ -113,7 +113,6 @@ table.usage { <& packages/section.html, 'cust_main' => $cust_main, 'packages' => $packages, - 'show_location' => $show_location, &>
% } @@ -140,10 +139,6 @@ my $curuser = $FS::CurrentUser::CurrentUser; my( $packages, $num_old_packages ) = get_packages($cust_main, $conf); - -my $show_location = $conf->exists('cust_pkg-always_show_location') - || (grep $_->locationnum ne $cust_main->ship_locationnum, @$packages); - my $countrydefault = scalar($conf->config('countrydefault')) || 'US'; #subroutines diff --git a/httemplate/view/cust_main/packages/contact.html b/httemplate/view/cust_main/packages/contact.html new file mode 100644 index 000000000..a6f8a4273 --- /dev/null +++ b/httemplate/view/cust_main/packages/contact.html @@ -0,0 +1,48 @@ +% if ( $contact ) { + <% $contact->line |h %> +% } + +% if ( ! $cust_pkg->get('cancel') +% && $FS::CurrentUser::CurrentUser->access_right('Change customer package') +% ) +% { + +%#XXX ( <%pkg_change_contact_link($cust_pkg)%> ) +%# % if ( $cust_pkg->contactnum ) { +%# ( <%edit_contact_link($cust_pkg->contactnum)%> ) +%# % } + +% } + + +<%init> + +my $conf = new FS::Conf; +my %opt = @_; + +my $cust_pkg = $opt{'cust_pkg'}; + +my $contact = $cust_pkg->contact; + +sub pkg_change_contact_link { + my $cust_pkg = shift; + my $pkgpart = $cust_pkg->pkgpart; + include( '/elements/popup_link-cust_pkg.html', +#XXX + 'action' => $p. "misc/change_pkg.cgi?contactnum=-1", + 'label' => emt('Change contact'), + 'actionlabel' => emt('Change'), + 'cust_pkg' => $cust_pkg, + ); +} + +sub edit_contact_link { + my $contactnum = shift; + include( '/elements/popup_link.html', + 'action' => $p. "edit/cust_contact.cgi?contactnum=$contactnum", + 'label' => emt('Edit contact'), + 'actionlabel' => emt('Edit'), + ); +} + + diff --git a/httemplate/view/cust_main/packages/location.html b/httemplate/view/cust_main/packages/location.html index 95694850e..f2d379841 100644 --- a/httemplate/view/cust_main/packages/location.html +++ b/httemplate/view/cust_main/packages/location.html @@ -1,5 +1,3 @@ - - % if ( $default ) {
% } @@ -41,13 +39,11 @@ % } - <%init> my $conf = new FS::Conf; my %opt = @_; -my $bgcolor = $opt{'bgcolor'}; my $cust_pkg = $opt{'cust_pkg'}; my $countrydefault = $opt{'countrydefault'} || 'US'; my $statedefault = $opt{'statedefault'} diff --git a/httemplate/view/cust_main/packages/package.html b/httemplate/view/cust_main/packages/package.html index 0b72d195e..520305a9a 100644 --- a/httemplate/view/cust_main/packages/package.html +++ b/httemplate/view/cust_main/packages/package.html @@ -186,11 +186,6 @@ % !$supplemental and % $part_pkg->freq ne '0' ) { -% if ( !$opt{'show_location'} ) { - - ( <% pkg_change_location_link($cust_pkg) %> ) - -% } % if ( FS::Conf->new->exists('invoice-unitprice') ) { ( <% pkg_change_quantity_link($cust_pkg) %> ) diff --git a/httemplate/view/cust_main/packages/section.html b/httemplate/view/cust_main/packages/section.html index 8ea7a7d5f..5f54c0a36 100755 --- a/httemplate/view/cust_main/packages/section.html +++ b/httemplate/view/cust_main/packages/section.html @@ -3,9 +3,7 @@ % #my $width = $show_location ? 'WIDTH="25%"' : 'WIDTH="33%"'; <% mt('Package') |h %> <% mt('Status') |h %> -% if ( $show_location ) { - <% mt('Location') |h %> -% } + <% mt('Contact/Location') |h %> <% mt('Services') |h %> @@ -13,6 +11,7 @@ % foreach my $cust_pkg (@$packages) { <& .packagerow, $cust_pkg, 'cust_main' => $opt{'cust_main'}, + 'bgcolor' => $opt{'bgcolor'}, %conf_opt &> % } @@ -27,10 +26,11 @@ <& package.html, %iopt &> - <& status.html, %iopt &> -% if ( $iopt{'show_location'} ) { - <& location.html, %iopt &> -% } + <& status.html, %iopt &> + + <& contact.html, %iopt &> + <& location.html, %iopt &> + <& services.html, %iopt &> % $row++; @@ -51,7 +51,6 @@ my $conf = new FS::Conf; my $curuser = $FS::CurrentUser::CurrentUser; my $packages = $opt{'packages'}; -my $show_location = $opt{'show_location'}; # Sort order is hardcoded for now, can change this if needed. @$packages = sort { @@ -86,8 +85,6 @@ my %conf_opt = ( 'maestro-status_test' => $conf->exists('maestro-status_test'), 'cust_pkg-large_pkg_size' => scalar($conf->config('cust_pkg-large_pkg_size')), - # for packages.html Change location link - 'show_location' => $show_location, ); diff --git a/httemplate/view/cust_main/packages/status.html b/httemplate/view/cust_main/packages/status.html index 6be0296a3..9d5a88e0f 100644 --- a/httemplate/view/cust_main/packages/status.html +++ b/httemplate/view/cust_main/packages/status.html @@ -1,4 +1,4 @@ - + %#this should use cust_pkg->status and cust_pkg->statuscolor eventually -- 2.11.0