Refactor for how we deal with delete payments and client paid to dates

This commit is contained in:
David Bomba 2022-12-14 16:17:43 +11:00
parent a25711b5eb
commit c131cfcd1e
3 changed files with 38 additions and 51 deletions

View File

@ -351,7 +351,7 @@ class MolliePaymentDriver extends BaseDriver
if ($record) { if ($record) {
if (in_array($payment->status, ['canceled', 'expired', 'failed'])) { if (in_array($payment->status, ['canceled', 'expired', 'failed'])) {
$record->service()->deletePayment(); $record->service()->deletePayment(false); //sometimes mollie does not return but we still decrement the paid to date, this is incorrect.
} }
$record->status_id = $codes[$payment->status]; $record->status_id = $codes[$payment->status];

View File

@ -17,24 +17,26 @@ use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\TransactionEvent; use App\Models\TransactionEvent;
use App\Repositories\ActivityRepository; use App\Repositories\ActivityRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
class DeletePayment class DeletePayment
{ {
public $payment; private float $_paid_to_date_deleted = 0;
private $activity_repository; /**
* @param mixed $payment
public function __construct($payment) * @return void
{ */
$this->payment = $payment; public function __construct(public Payment $payment, private bool $update_client_paid_to_date) {}
$this->activity_repository = new ActivityRepository();
}
/**
* @return mixed
* @throws BindingResolutionException
*/
public function run() public function run()
{ {
\DB::connection(config('database.default'))->transaction(function () { \DB::connection(config('database.default'))->transaction(function () {
if ($this->payment->is_deleted) { if ($this->payment->is_deleted) {
@ -46,7 +48,6 @@ class DeletePayment
$this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment
->updateCreditables() //return the credits first ->updateCreditables() //return the credits first
->adjustInvoices() ->adjustInvoices()
->updateClient()
->deletePaymentables() ->deletePaymentables()
->cleanupPayment() ->cleanupPayment()
->save(); ->save();
@ -58,6 +59,7 @@ class DeletePayment
} }
/** @return $this */
private function cleanupPayment() private function cleanupPayment()
{ {
$this->payment->is_deleted = true; $this->payment->is_deleted = true;
@ -66,6 +68,7 @@ class DeletePayment
return $this; return $this;
} }
/** @return $this */
private function deletePaymentables() private function deletePaymentables()
{ {
$this->payment->paymentables()->update(['deleted_at' => now()]); $this->payment->paymentables()->update(['deleted_at' => now()]);
@ -73,20 +76,16 @@ class DeletePayment
return $this; return $this;
} }
private function updateClient() /** @return $this */
{
//$this->payment->client->service()->updatePaidToDate(-1 * $this->payment->amount)->save();
return $this;
}
private function adjustInvoices() private function adjustInvoices()
{ {
$this->_paid_to_date_deleted = 0;
if ($this->payment->invoices()->exists()) { if ($this->payment->invoices()->exists()) {
$this->payment->invoices()->each(function ($paymentable_invoice) { $this->payment->invoices()->each(function ($paymentable_invoice) {
$net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded; $net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded;
$client = $this->payment->client->fresh(); $this->_paid_to_date_deleted += $net_deletable;
nlog("net deletable amount - refunded = {$net_deletable}"); nlog("net deletable amount - refunded = {$net_deletable}");
@ -105,7 +104,7 @@ class DeletePayment
$client = $this->payment $client = $this->payment
->client ->client
->service() ->service()
->updateBalance($net_deletable) ->updateBalanceAndPaidToDate($net_deletable, $net_deletable*-1)
->save(); ->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) { if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
@ -114,46 +113,30 @@ class DeletePayment
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save(); $paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
} }
} else { } else {
$paymentable_invoice->restore();
//If the invoice is deleted we only update the meta data on the invoice $paymentable_invoice->restore();
//and reduce the clients paid to date
$paymentable_invoice->service() $paymentable_invoice->service()
->updatePaidToDate($net_deletable * -1) ->updatePaidToDate($net_deletable * -1)
->save(); ->save();
} }
$transaction = [
'invoice' => $paymentable_invoice->transaction_event(),
'payment' => $this->payment->transaction_event(),
'client' => $client->transaction_event(),
'credit' => [],
'metadata' => [],
];
// TransactionLog::dispatch(TransactionEvent::PAYMENT_DELETED, $transaction, $paymentable_invoice->company->db);
}); });
} }
$this->payment //sometimes the payment is NOT created properly, this catches the payment and prevents the paid to date reducing inappropriately.
->client if($this->update_client_paid_to_date)
->service() {
->updatePaidToDate(($this->payment->amount - $this->payment->refunded) * -1) $this->payment
->save(); ->client
->service()
$transaction = [ ->updatePaidToDate(min(0, ($this->payment->amount - $this->payment->refunded - $this->_paid_to_date_deleted) * -1))
'invoice' => [], ->save();
'payment' => [], }
'client' => $this->payment->client->transaction_event(),
'credit' => [],
'metadata' => [],
];
// TransactionLog::dispatch(TransactionEvent::CLIENT_STATUS, $transaction, $this->payment->company->db);
return $this; return $this;
} }
/** @return $this */
private function updateCreditables() private function updateCreditables()
{ {
if ($this->payment->credits()->exists()) { if ($this->payment->credits()->exists()) {
@ -183,6 +166,10 @@ class DeletePayment
return $this; return $this;
} }
/**
* @param mixed $status
* @return $this
*/
private function setStatus($status) private function setStatus($status)
{ {
$this->payment->status_id = Payment::STATUS_CANCELLED; $this->payment->status_id = Payment::STATUS_CANCELLED;

View File

@ -87,9 +87,9 @@ class PaymentService
return ((new RefundPayment($this->payment, $data)))->run(); return ((new RefundPayment($this->payment, $data)))->run();
} }
public function deletePayment() :?Payment public function deletePayment($update_client_paid_to_date = true) :?Payment
{ {
return (new DeletePayment($this->payment))->run(); return (new DeletePayment($this->payment, $update_client_paid_to_date))->run();
} }
public function updateInvoicePayment(PaymentHash $payment_hash) :?Payment public function updateInvoicePayment(PaymentHash $payment_hash) :?Payment