mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 23:57:33 -05:00 
			
		
		
		
	Delete payment workflow
This commit is contained in:
		
							parent
							
								
									8eed07b8f5
								
							
						
					
					
						commit
						f36bbf75b8
					
				@ -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()) {
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
     * 
 | 
			
		||||
 | 
			
		||||
@ -80,7 +80,7 @@ class PaymentService
 | 
			
		||||
 | 
			
		||||
    public function deletePayment() :?Payment
 | 
			
		||||
    {
 | 
			
		||||
        return (new DeletePayment())->run();
 | 
			
		||||
        return (new DeletePayment($this->payment))->run();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function updateInvoicePayment() :?Payment
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user