1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Delete payment workflow

This commit is contained in:
David Bomba 2020-06-28 13:05:58 +10:00
parent 8eed07b8f5
commit f36bbf75b8
5 changed files with 81 additions and 10 deletions

View File

@ -261,6 +261,12 @@ class Payment extends BaseModel
return $this->status_id == self::STATUS_REFUNDED;
}
public function setStatus($status)
{
$this->status_id = $status;
$this->save();
}
public function markVoided()
{
if ($this->isVoided() || $this->isPartiallyRefunded() || $this->isRefunded()) {

View File

@ -196,11 +196,22 @@ class PaymentRepository extends BaseRepository
public function delete($payment)
{
//cannot double delete a payment
if($payment->is_deleted)
return;
$payment->service()->deletePayment();
return parent::delete($payment);
}
public function restore($payment)
{
//we cannot restore a deleted payment.
if($payment->is_deleted)
return;
return parent::restore($payment);
}
}

View File

@ -28,14 +28,14 @@ class DeletePayment
public function run()
{
return $this->setStatus() //sets status of payment
return $this->setStatus(Payment::STATUS_VOIDED) //sets status of payment
->updateCreditables() //return the credits first
->updatePaymentables() //update the paymentable items
->adjustInvoices()
->updateClient()
->save();
}
//
//reverse paymentables->invoices
@ -45,7 +45,63 @@ class DeletePayment
//set applied amount to 0
private function updateClient()
{
$this->payment->client->service()->updatePaidToDate(-1*$this->payment->amount)->save();
return $this;
}
private function adjustInvoices()
{
if ($this->payment->invoices()->exists())
{
$this->payment->invoices()->each(function ($paymentable_invoice){
$paymentable_invoice->service()->updateBalance($paymentable_invoice->pivot->amount)->save();
$paymentable_invoice->ledger()->updateInvoiceBalance($paymentable_invoice->pivot->amount)->save();
if(floatval($paymentable_invoice->balance) == 0)
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
else
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
//fire event for this credit
//
});
}
return $this;
}
private function updateCreditables()
{
if ($this->payment->credits()->exists())
{
$this->payment->credits()->each(function ($paymentable_credit){
$paymentable_credit->balance += $paymentable_credit->pivot->amount;
$paymentable_credit->setStatus(Credit::STATUS_SENT);
//fire event for this credit
//
});
}
return $this;
}
private function setStatus($status)
{
$this->payment->status_id = Payment::STATUS_VOIDED;
return $this;
}
/**
* Saves the payment
*

View File

@ -80,7 +80,7 @@ class PaymentService
public function deletePayment() :?Payment
{
return (new DeletePayment())->run();
return (new DeletePayment($this->payment))->run();
}
public function updateInvoicePayment() :?Payment

View File

@ -249,11 +249,9 @@ class RefundPayment
// $this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string);
$client = $this->payment->client->fresh();
$client->paid_to_date -= $this->total_refund;
$client->save();
$client->service()->updatePaidToDate(-1*$this->total_refund)->save();
}
return $this;