diff options
Diffstat (limited to 'FS')
-rw-r--r-- | FS/FS/ClientAPI/MyAccount.pm | 23 | ||||
-rw-r--r-- | FS/FS/ClientAPI_XMLRPC.pm | 1 | ||||
-rw-r--r-- | FS/FS/Schema.pm | 2 | ||||
-rw-r--r-- | FS/FS/Upgrade.pm | 6 | ||||
-rw-r--r-- | FS/FS/cust_bill_pkg_tax_location.pm | 2 | ||||
-rw-r--r-- | FS/FS/cust_location.pm | 19 | ||||
-rw-r--r-- | FS/FS/cust_main/Billing_Realtime.pm | 444 | ||||
-rw-r--r-- | FS/FS/cust_main/Packages.pm | 98 | ||||
-rw-r--r-- | FS/FS/cust_pay_pending.pm | 26 | ||||
-rw-r--r-- | FS/FS/cust_pkg.pm | 6 | ||||
-rw-r--r-- | FS/FS/part_event/Action/rt_ticket.pm | 100 | ||||
-rw-r--r-- | FS/FS/part_event/Condition.pm | 2 | ||||
-rw-r--r-- | FS/FS/part_event/Condition/hasnt_pkg_class_cancelled.pm | 22 | ||||
-rw-r--r-- | FS/FS/part_event/Condition/hasnt_pkgpart_cancelled.pm | 15 | ||||
-rw-r--r-- | FS/FS/password_history.pm | 7 |
15 files changed, 537 insertions, 236 deletions
diff --git a/FS/FS/ClientAPI/MyAccount.pm b/FS/FS/ClientAPI/MyAccount.pm index 630346a81..23fbf6cf6 100644 --- a/FS/FS/ClientAPI/MyAccount.pm +++ b/FS/FS/ClientAPI/MyAccount.pm @@ -645,6 +645,29 @@ sub customer_info_short { }; } +sub customer_recurring { + my $p = shift; + + my($context, $session, $custnum) = _custoragent_session_custnum($p); + return { 'error' => $session } if $context eq 'error'; + + my %return; + + my $conf = new FS::Conf; + + my $search = { 'custnum' => $custnum }; + $search->{'agentnum'} = $session->{'agentnum'} if $context eq 'agent'; + my $cust_main = qsearchs('cust_main', $search ) + or return { 'error' => "customer_info_short: unknown custnum $custnum" }; + + $return{'display_recurring'} = [ $cust_main->display_recurring ]; + + return { 'error' => '', + 'custnum' => $custnum, + %return, + }; +} + sub billing_history { my $p = shift; diff --git a/FS/FS/ClientAPI_XMLRPC.pm b/FS/FS/ClientAPI_XMLRPC.pm index 97019d194..7a1fc3e0f 100644 --- a/FS/FS/ClientAPI_XMLRPC.pm +++ b/FS/FS/ClientAPI_XMLRPC.pm @@ -111,6 +111,7 @@ sub ss2clientapi { 'switch_acct' => 'MyAccount/switch_acct', 'customer_info' => 'MyAccount/customer_info', 'customer_info_short' => 'MyAccount/customer_info_short', + 'customer_recurring' => 'MyAccount/customer_recurring', 'contact_passwd' => 'MyAccount/contact/contact_passwd', 'list_contacts' => 'MyAccount/contact/list_contacts', diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 980cd2116..b7ec7df19 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -1713,7 +1713,7 @@ sub tables_hashref { 'cust_pay_pending' => { 'columns' => [ 'paypendingnum','serial', '', '', '', '', - 'custnum', 'int', '', '', '', '', + 'custnum', 'int', 'NULL', '', '', '', 'paid', @money_type, '', '', '_date', @date_type, '', '', 'payby', 'char', '', 4, '', '', #CARD/BILL/COMP, should diff --git a/FS/FS/Upgrade.pm b/FS/FS/Upgrade.pm index 5b2750577..8b7d7337f 100644 --- a/FS/FS/Upgrade.pm +++ b/FS/FS/Upgrade.pm @@ -329,6 +329,9 @@ sub upgrade_data { tie my %hash, 'Tie::IxHash', + #fix whitespace - before cust_main + 'cust_location' => [], + #cust_main (remove paycvv from history) 'cust_main' => [], @@ -444,9 +447,6 @@ sub upgrade_data { #mark certain taxes as system-maintained, # and fix whitespace 'cust_main_county' => [], - - #fix whitespace - 'cust_location' => [], ; \%hash; diff --git a/FS/FS/cust_bill_pkg_tax_location.pm b/FS/FS/cust_bill_pkg_tax_location.pm index f16e930b9..0e510005f 100644 --- a/FS/FS/cust_bill_pkg_tax_location.pm +++ b/FS/FS/cust_bill_pkg_tax_location.pm @@ -341,7 +341,7 @@ sub upgrade_taxable_billpkgnum { } #for $i } else { # the more complicated case - $log->warn("mismatched charges and tax links in pkg#$pkgnum", + $log->warning("mismatched charges and tax links in pkg#$pkgnum", object => $cust_bill); my $tax_amount = sum(map {$_->amount} @tax_links); # remove all tax link records and recreate them to be 1:1 with diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm index 481ebb11a..67a5e3eb1 100644 --- a/FS/FS/cust_location.pm +++ b/FS/FS/cust_location.pm @@ -14,6 +14,12 @@ use FS::cust_main_county; use FS::part_export; use FS::GeocodeCache; +# Essential fields. Can't be modified in place, will be considered in +# deciding if a location is "new", and (because of that) can't have +# leading/trailing whitespace. +my @essential = (qw(custnum address1 address2 city county state zip country + location_number location_type location_kind disabled)); + $import = 0; $DEBUG = 0; @@ -143,9 +149,6 @@ sub find_or_insert { warn "find_or_insert:\n".Dumper($self) if $DEBUG; - my @essential = (qw(custnum address1 address2 city county state zip country - location_number location_type location_kind disabled)); - if ($conf->exists('cust_main-no_city_in_address')) { warn "Warning: passed city to find_or_insert when cust_main-no_city_in_address is configured, ignoring it" if $self->get('city'); @@ -346,9 +349,9 @@ sub check { return '' if $self->disabled; # so that disabling locations never fails - # maybe should just do all fields in the table? - # or in every table? - $self->trim_whitespace(qw(district city county state country)); + # whitespace in essential fields leads to problems figuring out if a + # record is "new"; get rid of it. + $self->trim_whitespace(@essential); my $error = $self->ut_numbern('locationnum') @@ -907,7 +910,9 @@ sub _upgrade_data { # trim whitespace on records that need it local $allow_location_edit = 1; - foreach my $field (qw(city county state country district)) { + foreach my $field (@essential) { + next if $field eq 'custnum'; + next if $field eq 'disabled'; foreach my $location (qsearch({ table => 'cust_location', extra_sql => " WHERE $field LIKE ' %' OR $field LIKE '% '" diff --git a/FS/FS/cust_main/Billing_Realtime.pm b/FS/FS/cust_main/Billing_Realtime.pm index 8ebad53cb..5d35fc2c4 100644 --- a/FS/FS/cust_main/Billing_Realtime.pm +++ b/FS/FS/cust_main/Billing_Realtime.pm @@ -5,7 +5,7 @@ use vars qw( $conf $DEBUG $me ); use vars qw( $realtime_bop_decline_quiet ); #ugh use Data::Dumper; use Business::CreditCard 0.35; -use FS::UID qw( dbh ); +use FS::UID qw( dbh myconnect ); use FS::Record qw( qsearch qsearchs ); use FS::Misc qw( send_email ); use FS::payby; @@ -1731,6 +1731,7 @@ sub realtime_verify_bop { my $self = shift; local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG; + my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop'); my %options = (); if (ref($_[0]) eq 'HASH') { @@ -1836,29 +1837,14 @@ sub realtime_verify_bop { # run transaction(s) ### - warn "claiming mutex on customer ". $self->custnum. "\n" if $DEBUG > 1; - $self->select_for_update; #mutex ... just until we get our pending record in - warn "obtained mutex on customer ". $self->custnum. "\n" if $DEBUG > 1; - - #the checks here are intended to catch concurrent payments - #double-form-submission prevention is taken care of in cust_pay_pending::check - - #also check and make sure there aren't *other* pending payments for this cust - - my @pending = qsearch('cust_pay_pending', { - 'custnum' => $self->custnum, - 'status' => { op=>'!=', value=>'done' } - }); - - return "A payment is already being processed for this customer (". - join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ). - "); verification transaction aborted." - if scalar(@pending); - - #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out + my $error; + my $transaction; #need this back so we can do _tokenize_card + # don't mutex the customer here, because they might be uncommitted. and + # this is only verification. it doesn't matter if they have other + # unfinished verifications. my $cust_pay_pending = new FS::cust_pay_pending { - 'custnum' => $self->custnum, + 'custnum_pending' => 1, 'paid' => '1.00', '_date' => '', 'payby' => $bop_method2payby{'CC'}, @@ -1875,220 +1861,254 @@ sub realtime_verify_bop { $cust_pay_pending->payunique( $options{payunique} ) if defined($options{payunique}) && length($options{payunique}); - warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n" - if $DEBUG > 1; - my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted - return $cpp_new_err if $cpp_new_err; + IMMEDIATE: { + # open a separate handle for creating/updating the cust_pay_pending + # record + local $FS::UID::dbh = myconnect(); + local $FS::UID::AutoCommit = 1; + + # if this is an existing customer (and we can tell now because + # this is a fresh transaction), it's safe to assign their custnum + # to the cust_pay_pending record, and then the verification attempt + # will remain linked to them even if it fails. + if ( FS::cust_main->by_key($self->custnum) ) { + $cust_pay_pending->set('custnum', $self->custnum); + } - warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n" - if $DEBUG > 1; - warn Dumper($cust_pay_pending) if $DEBUG > 2; + warn "inserting cust_pay_pending record for customer ". $self->custnum. "\n" + if $DEBUG > 1; - my $transaction = new $namespace( $payment_gateway->gateway_module, - $self->_bop_options(\%options), - ); + # if this fails, just return; everything else will still allow the + # cust_pay_pending to have its custnum set later + my $cpp_new_err = $cust_pay_pending->insert; + return $cpp_new_err if $cpp_new_err; - $transaction->content( - 'type' => 'CC', - $self->_bop_auth(\%options), - 'action' => 'Authorization Only', - 'description' => $options{'description'}, - 'amount' => '1.00', - #'invoice_number' => $options{'invnum'}, - 'customer_id' => $self->custnum, - %$bop_content, - 'reference' => $cust_pay_pending->paypendingnum, #for now - 'callback_url' => $payment_gateway->gateway_callback_url, - 'cancel_url' => $payment_gateway->gateway_cancel_url, - 'email' => $email, - %content, #after - ); + warn "inserted cust_pay_pending record for customer ". $self->custnum. "\n" + if $DEBUG > 1; + warn Dumper($cust_pay_pending) if $DEBUG > 2; - $cust_pay_pending->status('pending'); - my $cpp_pending_err = $cust_pay_pending->replace; - return $cpp_pending_err if $cpp_pending_err; + $transaction = new $namespace( $payment_gateway->gateway_module, + $self->_bop_options(\%options), + ); - warn Dumper($transaction) if $DEBUG > 2; + $transaction->content( + 'type' => 'CC', + $self->_bop_auth(\%options), + 'action' => 'Authorization Only', + 'description' => $options{'description'}, + 'amount' => '1.00', + #'invoice_number' => $options{'invnum'}, + 'customer_id' => $self->custnum, + %$bop_content, + 'reference' => $cust_pay_pending->paypendingnum, #for now + 'callback_url' => $payment_gateway->gateway_callback_url, + 'cancel_url' => $payment_gateway->gateway_cancel_url, + 'email' => $email, + %content, #after + ); - unless ( $BOP_TESTING ) { - $transaction->test_transaction(1) - if $conf->exists('business-onlinepayment-test_transaction'); - $transaction->submit(); - } else { - if ( $BOP_TESTING_SUCCESS ) { - $transaction->is_success(1); - $transaction->authorization('fake auth'); + $cust_pay_pending->status('pending'); + my $cpp_pending_err = $cust_pay_pending->replace; + return $cpp_pending_err if $cpp_pending_err; + + warn Dumper($transaction) if $DEBUG > 2; + + unless ( $BOP_TESTING ) { + $transaction->test_transaction(1) + if $conf->exists('business-onlinepayment-test_transaction'); + $transaction->submit(); } else { - $transaction->is_success(0); - $transaction->error_message('fake failure'); + if ( $BOP_TESTING_SUCCESS ) { + $transaction->is_success(1); + $transaction->authorization('fake auth'); + } else { + $transaction->is_success(0); + $transaction->error_message('fake failure'); + } } - } - my $log = FS::Log->new('FS::cust_main::Billing_Realtime::realtime_verify_bop'); + if ( $transaction->is_success() ) { - if ( $transaction->is_success() ) { + $cust_pay_pending->status('authorized'); + my $cpp_authorized_err = $cust_pay_pending->replace; + return $cpp_authorized_err if $cpp_authorized_err; - $cust_pay_pending->status('authorized'); - my $cpp_authorized_err = $cust_pay_pending->replace; - return $cpp_authorized_err if $cpp_authorized_err; + my $auth = $transaction->authorization; + my $ordernum = $transaction->can('order_number') + ? $transaction->order_number + : ''; - my $auth = $transaction->authorization; - my $ordernum = $transaction->can('order_number') - ? $transaction->order_number - : ''; + my $reverse = new $namespace( $payment_gateway->gateway_module, + $self->_bop_options(\%options), + ); - my $reverse = new $namespace( $payment_gateway->gateway_module, - $self->_bop_options(\%options), - ); + $reverse->content( 'action' => 'Reverse Authorization', + $self->_bop_auth(\%options), - $reverse->content( 'action' => 'Reverse Authorization', - $self->_bop_auth(\%options), + # B:OP + 'amount' => '1.00', + 'authorization' => $transaction->authorization, + 'order_number' => $ordernum, - # B:OP - 'amount' => '1.00', - 'authorization' => $transaction->authorization, - 'order_number' => $ordernum, + # vsecure + 'result_code' => $transaction->result_code, + 'txn_date' => $transaction->txn_date, - # vsecure - 'result_code' => $transaction->result_code, - 'txn_date' => $transaction->txn_date, + %content, + ); + $reverse->test_transaction(1) + if $conf->exists('business-onlinepayment-test_transaction'); + $reverse->submit(); - %content, - ); - $reverse->test_transaction(1) - if $conf->exists('business-onlinepayment-test_transaction'); - $reverse->submit(); + if ( $reverse->is_success ) { - if ( $reverse->is_success ) { + $cust_pay_pending->status('done'); + $cust_pay_pending->statustext('reversed'); + my $cpp_reversed_err = $cust_pay_pending->replace; + return $cpp_reversed_err if $cpp_reversed_err; - $cust_pay_pending->status('done'); - my $cpp_authorized_err = $cust_pay_pending->replace; - return $cpp_authorized_err if $cpp_authorized_err; + } else { - } else { + my $e = "Authorization successful but reversal failed, custnum #". + $self->custnum. ': '. $reverse->result_code. + ": ". $reverse->error_message; + $log->warning($e); + warn $e; + return $e; - my $e = "Authorization successful but reversal failed, custnum #". - $self->custnum. ': '. $reverse->result_code. - ": ". $reverse->error_message; - $log->warning($e); - warn $e; - return $e; + } - } + ### Address Verification ### + # + # Single-letter codes vary by cardtype. + # + # Erring on the side of accepting cards if avs is not available, + # only rejecting if avs occurred and there's been an explicit mismatch + # + # Charts below taken from vSecure documentation, + # shows codes for Amex/Dscv/MC/Visa + # + # ACCEPTABLE AVS RESPONSES: + # Both Address and 5-digit postal code match Y A Y Y + # Both address and 9-digit postal code match Y A X Y + # United Kingdom – Address and postal code match _ _ _ F + # International transaction – Address and postal code match _ _ _ D/M + # + # ACCEPTABLE, BUT ISSUE A WARNING: + # Ineligible transaction; or message contains a content error _ _ _ E + # System unavailable; retry R U R R + # Information unavailable U W U U + # Issuer does not support AVS S U S S + # AVS is not applicable _ _ _ S + # Incompatible formats – Not verified _ _ _ C + # Incompatible formats – Address not verified; postal code matches _ _ _ P + # International transaction – address not verified _ G _ G/I + # + # UNACCEPTABLE AVS RESPONSES: + # Only Address matches A Y A A + # Only 5-digit postal code matches Z Z Z Z + # Only 9-digit postal code matches Z Z W W + # Neither address nor postal code matches N N N N + + if (my $avscode = uc($transaction->avs_code)) { + + # map codes to accept/warn/reject + my $avs = { + 'American Express card' => { + 'A' => 'r', + 'N' => 'r', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'Y' => 'a', + 'Z' => 'r', + }, + 'Discover card' => { + 'A' => 'a', + 'G' => 'w', + 'N' => 'r', + 'U' => 'w', + 'W' => 'w', + 'Y' => 'r', + 'Z' => 'r', + }, + 'MasterCard' => { + 'A' => 'r', + 'N' => 'r', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'W' => 'r', + 'X' => 'a', + 'Y' => 'a', + 'Z' => 'r', + }, + 'VISA card' => { + 'A' => 'r', + 'C' => 'w', + 'D' => 'a', + 'E' => 'w', + 'F' => 'a', + 'G' => 'w', + 'I' => 'w', + 'M' => 'a', + 'N' => 'r', + 'P' => 'w', + 'R' => 'w', + 'S' => 'w', + 'U' => 'w', + 'W' => 'r', + 'Y' => 'a', + 'Z' => 'r', + }, + }; + my $cardtype = cardtype($content{card_number}); + if ($avs->{$cardtype}) { + my $avsact = $avs->{$cardtype}->{$avscode}; + my $warning = ''; + if ($avsact eq 'r') { + return "AVS code verification failed, cardtype $cardtype, code $avscode"; + } elsif ($avsact eq 'w') { + $warning = "AVS did not occur, cardtype $cardtype, code $avscode"; + } elsif (!$avsact) { + $warning = "AVS code unknown, cardtype $cardtype, code $avscode"; + } # else $avsact eq 'a' + if ($warning) { + $log->warning($warning); + warn $warning; + } + } # else $cardtype avs handling not implemented + } # else !$transaction->avs_code + + } else { # is not success + + # status is 'done' not 'declined', as in _realtime_bop_result + $cust_pay_pending->status('done'); + $error = $transaction->error_message || 'Unknown error'; + $cust_pay_pending->statustext($error); + # could also record failure_status here, + # but it's not supported by B::OP::vSecureProcessing... + # need a B::OP module with (reverse) auth only to test it with + my $cpp_declined_err = $cust_pay_pending->replace; + return $cpp_declined_err if $cpp_declined_err; - ### Address Verification ### - # - # Single-letter codes vary by cardtype. - # - # Erring on the side of accepting cards if avs is not available, - # only rejecting if avs occurred and there's been an explicit mismatch - # - # Charts below taken from vSecure documentation, - # shows codes for Amex/Dscv/MC/Visa - # - # ACCEPTABLE AVS RESPONSES: - # Both Address and 5-digit postal code match Y A Y Y - # Both address and 9-digit postal code match Y A X Y - # United Kingdom – Address and postal code match _ _ _ F - # International transaction – Address and postal code match _ _ _ D/M - # - # ACCEPTABLE, BUT ISSUE A WARNING: - # Ineligible transaction; or message contains a content error _ _ _ E - # System unavailable; retry R U R R - # Information unavailable U W U U - # Issuer does not support AVS S U S S - # AVS is not applicable _ _ _ S - # Incompatible formats – Not verified _ _ _ C - # Incompatible formats – Address not verified; postal code matches _ _ _ P - # International transaction – address not verified _ G _ G/I - # - # UNACCEPTABLE AVS RESPONSES: - # Only Address matches A Y A A - # Only 5-digit postal code matches Z Z Z Z - # Only 9-digit postal code matches Z Z W W - # Neither address nor postal code matches N N N N - - if (my $avscode = uc($transaction->avs_code)) { - - # map codes to accept/warn/reject - my $avs = { - 'American Express card' => { - 'A' => 'r', - 'N' => 'r', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'Y' => 'a', - 'Z' => 'r', - }, - 'Discover card' => { - 'A' => 'a', - 'G' => 'w', - 'N' => 'r', - 'U' => 'w', - 'W' => 'w', - 'Y' => 'r', - 'Z' => 'r', - }, - 'MasterCard' => { - 'A' => 'r', - 'N' => 'r', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'W' => 'r', - 'X' => 'a', - 'Y' => 'a', - 'Z' => 'r', - }, - 'VISA card' => { - 'A' => 'r', - 'C' => 'w', - 'D' => 'a', - 'E' => 'w', - 'F' => 'a', - 'G' => 'w', - 'I' => 'w', - 'M' => 'a', - 'N' => 'r', - 'P' => 'w', - 'R' => 'w', - 'S' => 'w', - 'U' => 'w', - 'W' => 'r', - 'Y' => 'a', - 'Z' => 'r', - }, - }; - my $cardtype = cardtype($content{card_number}); - if ($avs->{$cardtype}) { - my $avsact = $avs->{$cardtype}->{$avscode}; - my $warning = ''; - if ($avsact eq 'r') { - return "AVS code verification failed, cardtype $cardtype, code $avscode"; - } elsif ($avsact eq 'w') { - $warning = "AVS did not occur, cardtype $cardtype, code $avscode"; - } elsif (!$avsact) { - $warning = "AVS code unknown, cardtype $cardtype, code $avscode"; - } # else $avsact eq 'a' - if ($warning) { - $log->warning($warning); - warn $warning; - } - } # else $cardtype avs handling not implemented - } # else !$transaction->avs_code + } - } else { # is not success + } # end of IMMEDIATE; we now have our $error and $transaction - # status is 'done' not 'declined', as in _realtime_bop_result - $cust_pay_pending->status('done'); - $cust_pay_pending->statustext( $transaction->error_message || 'Unknown error' ); - # could also record failure_status here, - # but it's not supported by B::OP::vSecureProcessing... - # need a B::OP module with (reverse) auth only to test it with - my $cpp_declined_err = $cust_pay_pending->replace; - return $cpp_declined_err if $cpp_declined_err; + ### + # Save the custnum (as part of the main transaction, so it can reference + # the cust_main) + ### + if (!$cust_pay_pending->custnum) { + $cust_pay_pending->set('custnum', $self->custnum); + my $set_custnum_err = $cust_pay_pending->replace; + if ($set_custnum_err) { + $log->error($set_custnum_err); + $error ||= $set_custnum_err; + # but if there was a real verification error also, return that one + } } ### @@ -2113,7 +2133,9 @@ sub realtime_verify_bop { # result handling ### - $transaction->is_success() ? '' : $transaction->error_message(); + # $error contains the transaction error_message, if is_success was false. + + return $error; } diff --git a/FS/FS/cust_main/Packages.pm b/FS/FS/cust_main/Packages.pm index 8f96f81aa..4d0eee7ed 100644 --- a/FS/FS/cust_main/Packages.pm +++ b/FS/FS/cust_main/Packages.pm @@ -706,6 +706,104 @@ sub num_usage_pkgs { FS::Record->scalar_sql($sql, $self->custnum); } +=item display_recurring + +Returns an array of hash references, one for each recurring freq +on billable customer packages, with keys of freq, freq_pretty and amount +(the amount that this customer will next be charged at the given frequency.) + +Results will be numerically sorted by freq. + +Only intended for display purposes, not used for actual billing. + +=cut + +sub display_recurring { + my $cust_main = shift; + + my $sth = dbh->prepare(" + SELECT DISTINCT freq FROM cust_pkg LEFT JOIN part_pkg USING (pkgpart) + WHERE freq IS NOT NULL AND freq != '0' + AND ( cancel IS NULL OR cancel = 0 ) + AND custnum = ? + ") or die $DBI::errstr; + + $sth->execute($cust_main->custnum) or die $sth->errstr; + + #not really a numeric sort because freqs can actually be all sorts of things + # but good enough for the 99% cases of ordering monthly quarterly annually + my @freqs = sort { $a <=> $b } map { $_->[0] } @{ $sth->fetchall_arrayref }; + + $sth->finish; + + my @out; + + foreach my $freq (@freqs) { + + my @cust_pkg = qsearch({ + 'table' => 'cust_pkg', + 'addl_from' => 'LEFT JOIN part_pkg USING (pkgpart)', + 'hashref' => { 'custnum' => $cust_main->custnum, }, + 'extra_sql' => 'AND ( cancel IS NULL OR cancel = 0 ) + AND freq = '. dbh->quote($freq), + 'order_by' => 'ORDER BY COALESCE(start_date,0), pkgnum', # to ensure old pkgs come before change_to_pkg + }) or next; + + my $freq_pretty = $cust_pkg[0]->part_pkg->freq_pretty; + + my $amount = 0; + my $skip_pkg = {}; + foreach my $cust_pkg (@cust_pkg) { + my $part_pkg = $cust_pkg->part_pkg; + next if $cust_pkg->susp + && ! $cust_pkg->option('suspend_bill') + && ( ! $part_pkg->option('suspend_bill') + || $cust_pkg->option('no_suspend_bill') + ); + + #pkg change handling + next if $skip_pkg->{$cust_pkg->pkgnum}; + if ($cust_pkg->change_to_pkgnum) { + #if change is on or before next bill date, use new pkg + next if $cust_pkg->expire <= $cust_pkg->bill; + #if change is after next bill date, use old (this) pkg + $skip_pkg->{$cust_pkg->change_to_pkgnum} = 1; + } + + my $pkg_amount = 0; + + #add recurring amounts for this package and its billing add-ons + foreach my $l_part_pkg ( $part_pkg->self_and_bill_linked ) { + $pkg_amount += $l_part_pkg->base_recur($cust_pkg); + } + + #subtract amounts for any active discounts + #(there should only be one at the moment, otherwise this makes no sense) + foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) { + my $discount = $cust_pkg_discount->discount; + #and only one of these for each + $pkg_amount -= $discount->amount; + $pkg_amount -= $amount * $discount->percent/100; + } + + $pkg_amount *= ( $cust_pkg->quantity || 1 ); + + $amount += $pkg_amount; + + } #foreach $cust_pkg + + next unless $amount; + push @out, { + 'freq' => $freq, + 'freq_pretty' => $freq_pretty, + 'amount' => $amount, + }; + + } #foreach $freq + + return @out; +} + =back =head1 BUGS diff --git a/FS/FS/cust_pay_pending.pm b/FS/FS/cust_pay_pending.pm index 3a54e2d26..48748025e 100644 --- a/FS/FS/cust_pay_pending.pm +++ b/FS/FS/cust_pay_pending.pm @@ -213,7 +213,7 @@ sub check { my $error = $self->ut_numbern('paypendingnum') - || $self->ut_foreign_key('custnum', 'cust_main', 'custnum') + || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum') || $self->ut_money('paid') || $self->ut_numbern('_date') || $self->ut_textn('payunique') @@ -232,6 +232,10 @@ sub check { ; return $error if $error; + if (!$self->custnum and !$self->get('custnum_pending')) { + return 'custnum required'; + } + $self->_date(time) unless $self->_date; # UNIQUE index should catch this too, without race conditions, but this @@ -451,6 +455,26 @@ sub decline { $self->replace; } +=item reverse [ STATUSTEXT ] + +Sets the status of this pending payment to "done" (with statustext +"reversed (manual)" unless otherwise specified). + +Currently only used when resolving pending payments manually. + +=cut + +# almost complete false laziness with decline, +# but want to avoid confusion, in case any additional steps/defaults are ever added to either +sub reverse { + my $self = shift; + my $statustext = shift || "reversed (manual)"; + + $self->status('done'); + $self->statustext($statustext); + $self->replace; +} + # _upgrade_data # # Used by FS::Upgrade to migrate to a new database. diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 101dd81d0..548d00051 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -6108,6 +6108,12 @@ sub _upgrade_data { # class method } } +# will autoload in v4+ +sub rt_field_charge { + my $self = shift; + qsearch('rt_field_charge',{ 'pkgnum' => $self->pkgnum }); +} + =back =head1 BUGS diff --git a/FS/FS/part_event/Action/rt_ticket.pm b/FS/FS/part_event/Action/rt_ticket.pm new file mode 100644 index 000000000..4a2b993a1 --- /dev/null +++ b/FS/FS/part_event/Action/rt_ticket.pm @@ -0,0 +1,100 @@ +package FS::part_event::Action::rt_ticket; + +use strict; +use base qw( FS::part_event::Action ); +use FS::Record qw( qsearchs ); +use FS::msg_template; + +sub description { 'Open an RT ticket for the customer' } + +#need to be valid for msg_template substitution +sub eventtable_hashref { + { 'cust_main' => 1, + 'cust_bill' => 1, + 'cust_pkg' => 1, + 'cust_pay' => 1, + 'svc_acct' => 1, + }; +} + +sub option_fields { + ( + 'msgnum' => { 'label' => 'Template', + 'type' => 'select-table', + 'table' => 'msg_template', + 'name_col' => 'msgname', + 'hashref' => { disabled => '' }, + 'disable_empty' => 1, + }, + 'queueid' => { 'label' => 'Queue', + 'type' => 'select-rt-queue', + }, + 'requestor' => { 'label' => 'Requestor', + 'type' => 'select', + 'options' => [ 0, 1 ], + 'labels' => { + 0 => 'Customer\'s invoice address', + 1 => 'Template From: address', + }, + }, + + ); +} + +sub default_weight { 59; } + +sub do_action { + + my( $self, $object ) = @_; + + my $cust_main = $self->cust_main($object) + or die "Could not load cust_main"; + + my $msgnum = $self->option('msgnum'); + my $msg_template = qsearchs('msg_template', { 'msgnum' => $msgnum } ) + or die "Template $msgnum not found"; + + my $queueid = $self->option('queueid') + or die "No queue specified"; + + # technically this only works if create_ticket is implemented, + # and it is only implemented in RT_Internal, + # but we can let create_ticket throw that error + my $conf = new FS::Conf; + die "rt_ticket event - no ticket system configured" + unless $conf->config('ticket_system'); + + FS::TicketSystem->init(); + + my %msg = $msg_template->prepare( + 'cust_main' => $cust_main, + 'object' => $object, + ); + + my $subject = $msg{'subject'}; + chomp($subject); + + my $requestor = $self->option('requestor') + ? $msg_template->from_addr + : [ $cust_main->invoicing_list_emailonly ]; + + my $svcnum = ref($object) eq 'FS::svc_acct' + ? $object->svcnum + : undef; + + my $err_or_ticket = FS::TicketSystem->create_ticket( + '', #session should already exist + 'queue' => $queueid, + 'subject' => $subject, + 'requestor' => $requestor, + 'message' => $msg{'html_body'}, + 'mime_type' => 'text/html', + 'custnum' => $cust_main->custnum, + 'svcnum' => $svcnum, + ); + die $err_or_ticket unless ref($err_or_ticket); + return ''; + +} + +1; diff --git a/FS/FS/part_event/Condition.pm b/FS/FS/part_event/Condition.pm index 36fbe9a0d..d1d519683 100644 --- a/FS/FS/part_event/Condition.pm +++ b/FS/FS/part_event/Condition.pm @@ -312,7 +312,7 @@ sub option_age_from { } elsif ( $age =~ /^(\d+)d$/i ) { $mday -= $1; } elsif ( $age =~ /^(\d+)h$/i ) { - $hour -= $hour; + $hour -= $1; } else { die "unparsable age: $age"; } diff --git a/FS/FS/part_event/Condition/hasnt_pkg_class_cancelled.pm b/FS/FS/part_event/Condition/hasnt_pkg_class_cancelled.pm index 353e6469f..d54fb88fa 100644 --- a/FS/FS/part_event/Condition/hasnt_pkg_class_cancelled.pm +++ b/FS/FS/part_event/Condition/hasnt_pkg_class_cancelled.pm @@ -21,9 +21,16 @@ sub option_fields { 'type' => 'select-pkg_class', 'multiple' => 1, }, - 'age' => { 'label' => 'Cancellation in last', - 'type' => 'freq', - }, + 'age_newest' => { 'label' => 'Cancelled more than', + 'type' => 'freq', + 'post_text' => ' ago (blank for no limit)', + 'allow_blank' => 1, + }, + 'age' => { 'label' => 'Cancelled less than', + 'type' => 'freq', + 'post_text' => ' ago (blank for no limit)', + 'allow_blank' => 1, + }, ); } @@ -32,11 +39,12 @@ sub condition { my $cust_main = $self->cust_main($object); - my $age = $self->option_age_from('age', $opt{'time'} ); + my $oldest = length($self->option('age')) ? $self->option_age_from('age', $opt{'time'} ) : 0; + my $newest = $self->option_age_from('age_newest', $opt{'time'} ); + + my $pkgclass = $self->option('pkgclass') || {}; - #XXX test - my $hashref = $self->option('pkgclass') || {}; - ! grep { $hashref->{ $_->part_pkg->classnum } && $_->get('cancel') > $age } + ! grep { $pkgclass->{ $_->part_pkg->classnum } && ($_->get('cancel') > $oldest) && ($_->get('cancel') <= $newest) } $cust_main->cancelled_pkgs; } diff --git a/FS/FS/part_event/Condition/hasnt_pkgpart_cancelled.pm b/FS/FS/part_event/Condition/hasnt_pkgpart_cancelled.pm index b4ff6c32b..42845cb8a 100644 --- a/FS/FS/part_event/Condition/hasnt_pkgpart_cancelled.pm +++ b/FS/FS/part_event/Condition/hasnt_pkgpart_cancelled.pm @@ -18,8 +18,15 @@ sub option_fields { 'type' => 'select-part_pkg', 'multiple' => 1, }, - 'age' => { 'label' => 'Cancellation in last', + 'age_newest' => { 'label' => 'Cancelled more than', 'type' => 'freq', + 'post_text' => ' ago (blank for no limit)', + 'allow_blank' => 1, + }, + 'age' => { 'label' => 'Cancelled less than', + 'type' => 'freq', + 'post_text' => ' ago (blank for no limit)', + 'allow_blank' => 1, }, ); } @@ -29,10 +36,12 @@ sub condition { my $cust_main = $self->cust_main($object); - my $age = $self->option_age_from('age', $opt{'time'} ); + my $oldest = length($self->option('age')) ? $self->option_age_from('age', $opt{'time'} ) : 0; + my $newest = $self->option_age_from('age_newest', $opt{'time'} ); my $if_pkgpart = $self->option('if_pkgpart') || {}; - ! grep { $if_pkgpart->{ $_->pkgpart } && $_->get('cancel') > $age } + + ! grep { $if_pkgpart->{ $_->pkgpart } && ($_->get('cancel') > $oldest) && ($_->get('cancel') <= $newest) } $cust_main->cancelled_pkgs; } diff --git a/FS/FS/password_history.pm b/FS/FS/password_history.pm index a34f6169b..13d16010d 100644 --- a/FS/FS/password_history.pm +++ b/FS/FS/password_history.pm @@ -161,9 +161,14 @@ sub password_equals { } sub _upgrade_schema { + my $class = shift; + # if the table doesn't exist yet then nothing needs to happen here + my $dbdef_table = $class->dbdef_table + or return; + # clean up history records where linked_acct has gone away my @where; - for my $fk ( grep /__/, __PACKAGE__->dbdef_table->columns ) { + for my $fk ( grep /__/, $dbdef_table->columns ) { my ($table, $key) = split(/__/, $fk); push @where, " ( $fk IS NOT NULL AND NOT EXISTS(SELECT 1 FROM $table WHERE $table.$key = $fk) )"; |