2020-06-28 00:24:08 +02:00
< ? php
2020-07-01 03:06:40 +02:00
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-07-01 03:06:40 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2021-01-03 22:54:54 +01:00
* @ copyright Copyright ( c ) 2021. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-07-01 03:06:40 +02:00
*
* @ license https :// opensource . org / licenses / AAL
*/
2020-06-28 00:24:08 +02:00
namespace App\Services\Payment ;
use App\Models\Credit ;
use App\Models\Invoice ;
use App\Models\Payment ;
use App\Repositories\ActivityRepository ;
class DeletePayment
{
public $payment ;
private $activity_repository ;
public function __construct ( $payment )
{
$this -> payment = $payment ;
$this -> activity_repository = new ActivityRepository ();
}
public function run ()
{
2020-11-25 15:19:52 +01:00
if ( $this -> payment -> is_deleted ) {
2020-11-23 13:55:04 +01:00
return $this -> payment ;
2020-11-25 15:19:52 +01:00
}
2020-11-23 13:55:04 +01:00
2020-07-06 13:22:36 +02:00
return $this -> setStatus ( Payment :: STATUS_CANCELLED ) //sets status of payment
2020-06-28 00:24:08 +02:00
-> updateCreditables () //return the credits first
-> adjustInvoices ()
2020-06-28 05:05:58 +02:00
-> updateClient ()
2020-11-15 22:23:20 +01:00
-> deletePaymentables ()
2020-11-23 13:55:04 +01:00
-> cleanupPayment ()
2020-06-28 00:24:08 +02:00
-> save ();
}
//reverse paymentables->invoices
2020-09-06 11:38:10 +02:00
2020-06-28 00:24:08 +02:00
//reverse paymentables->credits
2020-09-06 11:38:10 +02:00
//set refunded to amount
2020-06-28 00:24:08 +02:00
//set applied amount to 0
2020-11-23 13:55:04 +01:00
private function cleanupPayment ()
{
$this -> payment -> is_deleted = true ;
$this -> payment -> delete ();
return $this ;
}
2020-11-15 22:23:20 +01:00
private function deletePaymentables ()
{
$this -> payment -> paymentables () -> update ([ 'deleted_at' => now ()]);
return $this ;
}
2020-06-28 05:05:58 +02:00
private function updateClient ()
{
2020-09-06 11:38:10 +02:00
$this -> payment -> client -> service () -> updatePaidToDate ( - 1 * $this -> payment -> amount ) -> save ();
2020-06-28 05:05:58 +02:00
return $this ;
}
private function adjustInvoices ()
{
2020-09-06 11:38:10 +02:00
if ( $this -> payment -> invoices () -> exists ()) {
2021-01-24 07:44:14 +01:00
2020-09-06 11:38:10 +02:00
$this -> payment -> invoices () -> each ( function ( $paymentable_invoice ) {
2021-01-24 07:44:14 +01:00
$paymentable_invoice -> service ()
-> updateBalance ( $paymentable_invoice -> pivot -> amount )
-> updatePaidToDate ( $paymentable_invoice -> pivot -> amount * - 1 )
-> save ();
$paymentable_invoice -> ledger ()
-> updateInvoiceBalance ( $paymentable_invoice -> pivot -> amount , " Adjusting invoice { $paymentable_invoice -> number } due to deletion of Payment { $this -> payment -> number } " )
-> save ();
$paymentable_invoice -> client
-> service ()
-> updateBalance ( $paymentable_invoice -> pivot -> amount )
-> save ();
2020-09-06 11:38:10 +02:00
2020-11-15 22:23:20 +01:00
if ( $paymentable_invoice -> balance == $paymentable_invoice -> amount ) {
2020-06-28 05:05:58 +02:00
$paymentable_invoice -> service () -> setStatus ( Invoice :: STATUS_SENT ) -> save ();
2020-09-06 11:38:10 +02:00
} else {
2020-06-28 05:05:58 +02:00
$paymentable_invoice -> service () -> setStatus ( Invoice :: STATUS_PARTIAL ) -> save ();
2020-09-06 11:38:10 +02:00
}
2020-06-28 05:05:58 +02:00
//fire event for this credit
//
});
}
return $this ;
}
private function updateCreditables ()
{
2020-09-06 11:38:10 +02:00
if ( $this -> payment -> credits () -> exists ()) {
$this -> payment -> credits () -> each ( function ( $paymentable_credit ) {
2021-01-24 07:44:14 +01:00
$paymentable_credit -> service ()
-> updateBalance ( $paymentable_credit -> pivot -> amount )
-> updatePaidToDate ( $paymentable_credit -> pivot -> amount *- 1 )
-> setStatus ( Credit :: STATUS_SENT )
-> save ();
2020-06-28 05:05:58 +02:00
});
}
return $this ;
}
private function setStatus ( $status )
{
2020-07-06 13:22:36 +02:00
$this -> payment -> status_id = Payment :: STATUS_CANCELLED ;
2020-06-28 05:05:58 +02:00
return $this ;
}
2020-09-06 11:38:10 +02:00
2020-06-28 00:24:08 +02:00
/**
2020-09-06 11:38:10 +02:00
* Saves the payment .
*
2020-06-28 00:24:08 +02:00
* @ return Payment $payment
*/
private function save ()
{
$this -> payment -> save ();
return $this -> payment ;
}
}