From fd80da85c87268e59900de206cf10f58967dfe07 Mon Sep 17 00:00:00 2001 From: Chris Mytton Date: Tue, 10 Oct 2023 13:06:40 +0100 Subject: [PATCH] [Waste] Add option to force a DD renewal. Sometimes we get new subscriptions through that should be renewals. If that's the case then we need to treat the payment as a renewal, regardless of what the payment is listed as. --- bin/fixmystreet.com/waste-reconcile-dd | 6 ++++- perllib/FixMyStreet/Roles/DDProcessor.pm | 11 ++++++++ t/cobrand/bromley_waste.t | 32 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/bin/fixmystreet.com/waste-reconcile-dd b/bin/fixmystreet.com/waste-reconcile-dd index 79407bd97b9..3454594b5eb 100755 --- a/bin/fixmystreet.com/waste-reconcile-dd +++ b/bin/fixmystreet.com/waste-reconcile-dd @@ -18,11 +18,15 @@ my ($opts, $usage) = describe_options( ['verbose|v', 'more verbose output'], ['cobrand=s', 'which cobrand to fetch events for'], ['help|h', "print usage message and exit" ], + ['reference=s', 'only look at direct debits with this reference'], + ['force-renewal', 'when run with --reference, force the direct debit to be a renewal'], ); $usage->die if $opts->help; $usage->die unless $opts->cobrand; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($opts->cobrand)->new; $cobrand->waste_reconcile_direct_debits({ - verbose => $opts->verbose + verbose => $opts->verbose, + reference => $opts->reference, + force_renewal => $opts->force_renewal, }); diff --git a/perllib/FixMyStreet/Roles/DDProcessor.pm b/perllib/FixMyStreet/Roles/DDProcessor.pm index bd0e7470cde..eab743c30ac 100644 --- a/perllib/FixMyStreet/Roles/DDProcessor.pm +++ b/perllib/FixMyStreet/Roles/DDProcessor.pm @@ -82,6 +82,10 @@ sub waste_reconcile_direct_debits { $payment = DDPayment->new({ data => $payment, cobrand => $self }); next unless $payment->date; + # If there's a `reference` key in $params then we only want to look at + # payments that match that reference. + next if $params->{reference} && $payment->payer ne $params->{reference}; + $self->log( "looking at payment " . $payment->payer ); $self->log( "payment date: " . $payment->date ); @@ -93,6 +97,11 @@ sub waste_reconcile_direct_debits { $self->log( "category: $category ($type)" ); + if ( $params->{reference} && $params->{force_renewal} ) { + $self->log( "Overriding type $type to renew" ); + $type = $self->waste_subscription_types->{Renew}; + } + my ($uprn, $rs) = $self->_process_reference($payment->payer); next unless $rs; $rs = $rs->search({ category => 'Garden Subscription' }); @@ -206,6 +215,8 @@ sub waste_reconcile_direct_debits { $payment = DDCancelPayment->new({ data => $payment, cobrand => $self }); next unless $payment->date; + next if $params->{reference} && $payment->payer ne $params->{reference}; + $self->log("looking at payment " . $payment->payer); my ($uprn, $rs) = $self->_process_reference($payment->payer); diff --git a/t/cobrand/bromley_waste.t b/t/cobrand/bromley_waste.t index e0617372940..239a515bda7 100644 --- a/t/cobrand/bromley_waste.t +++ b/t/cobrand/bromley_waste.t @@ -1475,6 +1475,38 @@ subtest 'check direct debit reconcilliation' => sub { $ad_hoc_skipped->discard_changes; is $ad_hoc_skipped->state, 'unconfirmed', "ad hoc report not confirmed on second run"; + warnings_are { + $c->waste_reconcile_direct_debits({ reference => $hidden_ref }); + } [ + "\n", + "looking at payment $hidden_ref\n", + "payment date: 16/03/2021\n", + "category: Garden Subscription (1)\n", + "extra query is {payerReference: $hidden_ref\n", + "is a new/ad hoc\n", + "looking at potential match " . $hidden->id . "\n", + "potential match is a dd payment\n", + "potential match type is 1\n", + "found matching report " . $hidden->id . " with state hidden\n", + "no matching record found for Garden Subscription payment with id $hidden_ref\n", + "done looking at payment $hidden_ref\n", + ], "warns if given reference"; + + $hidden->update({ state => 'fixed - council' }); + warnings_are { + $c->waste_reconcile_direct_debits({ reference => $hidden_ref, force_renewal => 1 }); + } [ + "\n", + "looking at payment $hidden_ref\n", + "payment date: 16/03/2021\n", + "category: Garden Subscription (1)\n", + "Overriding type 1 to renew\n", + "extra query is {payerReference: $hidden_ref\n", + "is a renewal\n", + "looking at potential match " . $hidden->id . " with state fixed - council\n", + "is a matching new report\n", + "no matching service to renew for $hidden_ref\n", + ], "gets past the first stage if forced renewal"; }; };