From a661ced3f9f678a645780eaa0b183d2de5f100fa Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 10 Jan 2009 00:43:06 +0000 Subject: [PATCH] more work on package service addresses: hide locations when they're all the default, config to show them anyway / finish implementing package ordering, fix all the state/county weirdness when changing the location dropdown. RT#4499 --- FS/FS/Conf.pm | 7 + FS/FS/cust_main.pm | 135 +++++++++++++---- FS/FS/cust_pkg.pm | 6 +- httemplate/edit/process/quick-cust_pkg.cgi | 38 ++++- httemplate/elements/location.html | 4 + httemplate/elements/tr-select-cust_location.html | 176 +++++++++++++++++++++++ httemplate/misc/location.cgi | 1 + httemplate/misc/order_pkg.html | 112 +-------------- httemplate/view/cust_main/packages.html | 9 +- 9 files changed, 348 insertions(+), 140 deletions(-) create mode 100644 httemplate/elements/tr-select-cust_location.html diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 9c9c6aaaf..a7a0b45c5 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -1948,6 +1948,13 @@ worry that config_items is freeside-specific and icky. }, { + 'key' => 'cust_pkg-always_show_location', + 'section' => 'UI', + 'description' => "Always display package locations, even when they're all the default service address.", + 'type' => 'checkbox', + }, + + { 'key' => 'svc_acct-edit_uid', 'section' => 'shell', 'description' => 'Allow UID editing.', diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index fb6808107..94280a4db 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -697,9 +697,7 @@ sub order_pkgs { my $cust_pkgs = shift; my $seconds = shift; my %options = @_; - my %svc_options = (); - $svc_options{'depend_jobnum'} = $options{'depend_jobnum'} - if exists $options{'depend_jobnum'}; + warn "$me order_pkgs called with options ". join(', ', map { "$_: $options{$_}" } keys %options ). "\n" if $DEBUG; @@ -718,36 +716,125 @@ sub order_pkgs { local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'}; foreach my $cust_pkg ( keys %$cust_pkgs ) { - $cust_pkg->custnum( $self->custnum ); - my $error = $cust_pkg->insert; + + my $error = $self->order_pkg( 'cust_pkg' => $cust_pkg, + 'svcs' => $cust_pkgs->{$cust_pkg}, + 'seconds' => $seconds, + 'depend_jobnum' => $options{'depend_jobnum'}, + ); if ( $error ) { $dbh->rollback if $oldAutoCommit; - return "inserting cust_pkg (transaction rolled back): $error"; + return $error; } - foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) { - if ( $svc_something->svcnum ) { - my $old_cust_svc = $svc_something->cust_svc; - my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash }; - $new_cust_svc->pkgnum( $cust_pkg->pkgnum); - $error = $new_cust_svc->replace($old_cust_svc); - } else { - $svc_something->pkgnum( $cust_pkg->pkgnum ); - if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) { - $svc_something->seconds( $svc_something->seconds + $$seconds ); - $$seconds = 0; - } - $error = $svc_something->insert(%svc_options); - } - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - #return "inserting svc_ (transaction rolled back): $error"; - return $error; + + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; #no error +} + +=item order_pkg HASHREF | OPTION => VALUE ... + +Orders a single package. This is the preferred and most flexible method for +ordering a single package, including the ability to set a (new or existing) +location as well as insert services. + +Options may be passed as a list of key/value pairs or as a hash reference. +Options are: + +=over 4 + +=item cust_pkg + +FS::cust_pkg object + +=item cust_location + +Optional FS::cust_location object + +=item svcs + +Optional arryaref of FS::svc_* service objects. + +=item depend_jobnum + +If this option is set to a job queue jobnum (see L{$_}" } keys %$opt ). "\n" + if $DEBUG; + + my $cust_pkg = $opt->{'cust_pkg'}; + my $seconds = $opt->{'seconds'}; + my $svcs = $opt->{'svcs'} || []; + + my %svc_options = (); + $svc_options{'depend_jobnum'} = $opt->{'depend_jobnum'} + if exists($opt->{'depend_jobnum'}) && $opt->{'depend_jobnum'}; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + if ( $opt->{'cust_location'} && + ( ! $cust_pkg->locationnum || $cust_pkg->locationnum == -1 ) ) { + my $error = $opt->{'cust_location'}->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting cust_location (transaction rolled back): $error"; + } + $cust_pkg->locationnum($opt->{'cust_location'}->locationnum); + } + + $cust_pkg->custnum( $self->custnum ); + + my $error = $cust_pkg->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting cust_pkg (transaction rolled back): $error"; + } + + foreach my $svc_something ( @{ $opt->{'svcs'} } ) { + if ( $svc_something->svcnum ) { + my $old_cust_svc = $svc_something->cust_svc; + my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash }; + $new_cust_svc->pkgnum( $cust_pkg->pkgnum); + $error = $new_cust_svc->replace($old_cust_svc); + } else { + $svc_something->pkgnum( $cust_pkg->pkgnum ); + if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) { + $svc_something->seconds( $svc_something->seconds + $$seconds ); + $$seconds = 0; } + $error = $svc_something->insert(%svc_options); + } + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting svc_ (transaction rolled back): $error"; } } $dbh->commit or die $dbh->errstr if $oldAutoCommit; ''; #no error + } =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ] diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 03cec75b8..25985ce1f 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -436,11 +436,13 @@ replace methods. sub check { my $self = shift; + $self->locationnum('') if $self->locationnum == 0 || $self->locationnum == -1; + my $error = $self->ut_numbern('pkgnum') || $self->ut_foreign_key('custnum', 'cust_main', 'custnum') || $self->ut_numbern('pkgpart') - || $self->ut_foreign_keyn('locationnum', 'location', 'locationnum') + || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum') || $self->ut_numbern('setup') || $self->ut_numbern('bill') || $self->ut_numbern('susp') @@ -1584,7 +1586,7 @@ Returns the location object, if any (see L). sub cust_location { my $self = shift; return '' unless $self->locationnum; - qsearchs( 'cust_main', { 'locationnum' => $self->locationnum } ); + qsearchs( 'cust_location', { 'locationnum' => $self->locationnum } ); } =item cust_location_or_main diff --git a/httemplate/edit/process/quick-cust_pkg.cgi b/httemplate/edit/process/quick-cust_pkg.cgi index 2aec344c5..9c2474330 100644 --- a/httemplate/edit/process/quick-cust_pkg.cgi +++ b/httemplate/edit/process/quick-cust_pkg.cgi @@ -2,7 +2,7 @@ % $cgi->param('error', $error); <% $cgi->redirect(popurl(3). 'misc/order_pkg.html?'. $cgi->query_string ) %> %} else { -% my $frag = "cust_pkg". $cust_pkg[0]->pkgnum; +% my $frag = "cust_pkg". $cust_pkg->pkgnum; <% header('Package ordered') %> + + + Service location + + + + + +<% include('/elements/location.html', + 'object' => $cust_location, + #'onchange' ? probably not + 'disabled' => ( $locationnum == -1 ? '' : 'DISABLED' ), + 'no_asterisks' => 1, + ) +%> + +<%once> + +my @location_fields = qw( address1 address2 city county state zip country ); + + +<%init> + +my $conf = new FS::Conf; +my $countrydefault = $conf->config('countrydefault') || 'US'; +my $statedefault = $conf->config('statedefault') + || ($countrydefault eq 'US' ? 'CA' : ''); + +my %opt = @_; +my $cgi = $opt{'cgi'}; +my $cust_main = $opt{'cust_main'}; + +$cgi->param('locationnum') =~ /^(\-?\d*)$/ or die "illegal locationnum"; +my $locationnum = $1; +my $cust_location; +if ( $locationnum && $locationnum != -1 ) { + $cust_location = qsearchs('cust_location', { 'locationnum' => $locationnum } ) + or die "unknown locationnum"; +} else { + $cust_location = new FS::cust_location; + if ( $cgi->param('error') && $locationnum == -1 ) { + $cust_location->$_( $cgi->param($_) ) foreach @location_fields; + } else { + $cust_location->$_( $cust_main->$_() ) foreach @location_fields; + } +} + + diff --git a/httemplate/misc/location.cgi b/httemplate/misc/location.cgi index 3c3a85545..419c59f2e 100644 --- a/httemplate/misc/location.cgi +++ b/httemplate/misc/location.cgi @@ -4,6 +4,7 @@ my $locationnum = $cgi->param('arg'); my $cust_location = qsearchs({ + 'select' => 'cust_location.*', 'table' => 'cust_location', 'hashref' => { 'locationnum' => $locationnum }, 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )', diff --git a/httemplate/misc/order_pkg.html b/httemplate/misc/order_pkg.html index f91143154..2c8335154 100644 --- a/httemplate/misc/order_pkg.html +++ b/httemplate/misc/order_pkg.html @@ -1,71 +1,7 @@ <% include('/elements/header-popup.html', 'Order new package' ) %> -<% include('/elements/xmlhttp.html', - 'url' => $p.'misc/location.cgi', - 'subs' => [ 'get_location' ], - ) -%> -