sub check {
my $self = shift;
- $self->locationnum('')
- if defined($self->locationnum) && length($self->locationnum)
- && ( $self->locationnum == 0 || $self->locationnum == -1 );
+ $self->locationnum('') if !$self->locationnum || $self->locationnum == -1;
my $error =
$self->ut_numbern('pkgnum')
return "Unable to transfer all services from package ". $self->pkgnum;
}
+ #reset usage if changing pkgpart
+ if ($self->pkgpart != $cust_pkg->pkgpart) {
+ my $part_pkg = $cust_pkg->part_pkg;
+ $error = $part_pkg->reset_usage($cust_pkg, $part_pkg->is_prepaid
+ ? ()
+ : ( 'null' => 1 )
+ )
+ if $part_pkg->can('reset_usage');
+
+ if ($error) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Error setting usage values: $error";
+ }
+ }
+
#Good to go, cancel old package.
$error = $self->cancel( quiet=>1 );
if ($error) {
qsearch( {
'table' => 'part_svc',
'hashref' => {},
- 'extra_sql' => "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
- WHERE pkg_svc.svcpart = part_svc.svcpart
- AND pkg_svc.pkgpart = $pkgpart
- AND quantity > 0
- )
- AND 0 < ( SELECT count(*)
- FROM cust_svc
- LEFT JOIN cust_pkg using ( pkgnum )
- WHERE cust_svc.svcpart = part_svc.svcpart
- AND pkgnum = $pkgnum
- )",
+ 'extra_sql' =>
+ "WHERE 0 = ( SELECT COUNT(*) FROM pkg_svc
+ WHERE pkg_svc.svcpart = part_svc.svcpart
+ AND pkg_svc.pkgpart = ?
+ AND quantity > 0
+ )
+ AND 0 < ( SELECT COUNT(*) FROM cust_svc
+ LEFT JOIN cust_pkg using ( pkgnum )
+ WHERE cust_svc.svcpart = part_svc.svcpart
+ AND pkgnum = ?
+ )",
+ 'extra_param' => [ [$self->pkgpart=>'int'], [$self->pkgnum=>'int'] ],
} );
}
sub location_sql {
my($class, %opt) = @_;
my $ornull = $opt{'ornull'};
- my $nec = $opt{'noempty_county'};
my $conf = new FS::Conf;
} else {
@bill_param = qw( county state state country );
}
- unshift @bill_param, 'county' unless $nec;
+ unshift @bill_param, 'county'; # unless $nec;
my $main_where;
my @main_param;
$main_where = "(
( ( ship_last IS NULL OR ship_last = '' )
- AND ". _location_sql_where('cust_main', '', $ornull, $nec ). "
+ AND ". _location_sql_where('cust_main', '', $ornull ). "
)
OR ( ship_last IS NOT NULL AND ship_last != ''
- AND ". _location_sql_where('cust_main', 'ship_', $ornull, $nec ). "
+ AND ". _location_sql_where('cust_main', 'ship_', $ornull ). "
)
)";
# AND payby != 'COMP'
my @param;
if ( $conf->exists('tax-pkg_address') ) {
- my $loc_where = _location_sql_where( 'cust_location',
- '', #prefix
- $ornull,
- $nec,
- );
+ my $loc_where = _location_sql_where( 'cust_location', '', $ornull );
$where = " (
( cust_pkg.locationnum IS NULL AND $main_where )
#subroutine, helper for location_sql
sub _location_sql_where {
- my $table = shift;
- my $prefix = @_ ? shift : '';
- my $ornull = @_ ? shift : '';
- my $no_empty_county = @_ ? shift : '';
+ my $table = shift;
+ my $prefix = @_ ? shift : '';
+ my $ornull = @_ ? shift : '';
+
+# $ornull = $ornull ? " OR ( ? IS NULL AND $table.${prefix}county IS NULL ) " : '';
- $ornull = $ornull ? ' OR ? IS NULL ' : '';
- my $or_empty_county = $no_empty_county ? '' : " OR ? = '' ";
+ $ornull = $ornull ? ' OR ? IS NULL ' : '';
+
+ my $or_empty_county = " OR ( ? = '' AND $table.${prefix}county IS NULL ) ";
+ my $or_empty_state = " OR ( ? = '' AND $table.${prefix}state IS NULL ) ";
"
( $table.${prefix}county = ? $or_empty_county $ornull )
- AND ( $table.${prefix}state = ? OR ? = '' $ornull )
+ AND ( $table.${prefix}state = ? $or_empty_state $ornull )
AND $table.${prefix}country = ?
";
}
=cut
sub set_usage {
- my ($self, $valueref) = @_;
+ my ($self, $valueref, %opt) = @_;
foreach my $cust_svc ($self->cust_svc){
my $svc_x = $cust_svc->svc_x;
- $svc_x->set_usage($valueref)
+ $svc_x->set_usage($valueref, %opt)
if $svc_x->can("set_usage");
}
}