diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 36df31e76e..ef94c316da 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -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()) { diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 90b6170408..dd4f48b68c 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -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); } } diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index fe52c98c85..67a982ac95 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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 * diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index a25f845bcf..bd7337c803 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -80,7 +80,7 @@ class PaymentService public function deletePayment() :?Payment { - return (new DeletePayment())->run(); + return (new DeletePayment($this->payment))->run(); } public function updateInvoicePayment() :?Payment diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 9f3b7644a3..f683580624 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -248,12 +248,10 @@ class RefundPayment // $ledger_string = "Refund for Invoice {$invoice->number} for amount " . $refunded_invoice['amount']; //todo // $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;