mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Fixes for balances
This commit is contained in:
parent
b7dced76cd
commit
6351cc8c60
@ -500,9 +500,11 @@ class CheckData extends Command
|
||||
{
|
||||
$client = Client::withTrashed()->find($_client->client_id);
|
||||
|
||||
$credits_from_reversal = Credit::withTrashed()->where('client_id', $client->id)->where('is_deleted', 0)->whereNotNull('invoice_id')->sum('amount');
|
||||
|
||||
$credits_used_for_payments = $this->clientCreditPaymentables($client);
|
||||
|
||||
$total_paid_to_date = $_client->payments_applied + $credits_used_for_payments[0]->credit_payment;
|
||||
$total_paid_to_date = $_client->payments_applied + $credits_used_for_payments[0]->credit_payment - $credits_from_reversal;
|
||||
|
||||
if(round($total_paid_to_date,2) != round($_client->client_paid_to_date,2)){
|
||||
|
||||
|
@ -17,6 +17,7 @@ use App\DataMapper\Transactions\InvoiceCancelledTransaction;
|
||||
use App\DataMapper\Transactions\InvoiceDeletedTransaction;
|
||||
use App\DataMapper\Transactions\InvoiceFeeAppliedTransaction;
|
||||
use App\DataMapper\Transactions\InvoicePaymentTransaction;
|
||||
use App\DataMapper\Transactions\InvoiceReversalTransaction;
|
||||
use App\DataMapper\Transactions\InvoiceUpdatedTransaction;
|
||||
use App\DataMapper\Transactions\MarkPaidTransaction;
|
||||
use App\DataMapper\Transactions\PaymentAppliedTransaction;
|
||||
@ -54,6 +55,7 @@ class TransactionLog implements ShouldQueue
|
||||
TransactionEvent::INVOICE_DELETED => InvoiceDeletedTransaction::class,
|
||||
TransactionEvent::INVOICE_PAYMENT_APPLIED => InvoicePaymentTransaction::class,
|
||||
TransactionEvent::INVOICE_CANCELLED => InvoiceCancelledTransaction::class,
|
||||
TransactionEvent::INVOICE_REVERSED => InvoiceReversalTransaction::class,
|
||||
TransactionEvent::INVOICE_FEE_APPLIED => InvoiceFeeAppliedTransaction::class,
|
||||
TransactionEvent::PAYMENT_MADE => PaymentMadeTransaction::class,
|
||||
TransactionEvent::GATEWAY_PAYMENT_MADE => GatewayPaymentMadeTransaction::class,
|
||||
|
@ -843,13 +843,13 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
|
||||
public function transaction_event()
|
||||
{
|
||||
$this->fresh();
|
||||
$client = $this->fresh();
|
||||
|
||||
return [
|
||||
'client_id' => $this->id,
|
||||
'client_balance' => $this->balance ?: 0,
|
||||
'client_paid_to_date' => $this->paid_to_date ?: 0,
|
||||
'client_credit_balance' => $this->credit_balance ?: 0
|
||||
'client_id' => $client->id,
|
||||
'client_balance' => $client->balance ?: 0,
|
||||
'client_paid_to_date' => $client->paid_to_date ?: 0,
|
||||
'client_credit_balance' => $client->credit_balance ?: 0
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -305,13 +305,13 @@ class Credit extends BaseModel
|
||||
public function transaction_event()
|
||||
{
|
||||
|
||||
$this->fresh();
|
||||
$credit = $this->fresh();
|
||||
|
||||
return [
|
||||
'credit_id' => $this->id,
|
||||
'credit_amount' => $this->amount ?: 0,
|
||||
'credit_balance' => $this->balance ?: 0,
|
||||
'credit_status' => $this->status_id ?: 1,
|
||||
'credit_id' => $credit->id,
|
||||
'credit_amount' => $credit->amount ?: 0,
|
||||
'credit_balance' => $credit->balance ?: 0,
|
||||
'credit_status' => $credit->status_id ?: 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -539,15 +539,15 @@ class Invoice extends BaseModel
|
||||
public function transaction_event()
|
||||
{
|
||||
|
||||
$this->fresh();
|
||||
$invoice = $this->fresh();
|
||||
|
||||
return [
|
||||
'invoice_id' => $this->id,
|
||||
'invoice_amount' => $this->amount ?: 0,
|
||||
'invoice_partial' => $this->partial ?: 0,
|
||||
'invoice_balance' => $this->balance ?: 0,
|
||||
'invoice_paid_to_date' => $this->paid_to_date ?: 0,
|
||||
'invoice_status' => $this->status_id ?: 1,
|
||||
'invoice_id' => $invoice->id,
|
||||
'invoice_amount' => $invoice->amount ?: 0,
|
||||
'invoice_partial' => $invoice->partial ?: 0,
|
||||
'invoice_balance' => $invoice->balance ?: 0,
|
||||
'invoice_paid_to_date' => $invoice->paid_to_date ?: 0,
|
||||
'invoice_status' => $invoice->status_id ?: 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -326,15 +326,15 @@ class Payment extends BaseModel
|
||||
|
||||
public function transaction_event()
|
||||
{
|
||||
$this->fresh();
|
||||
$payment = $this->fresh();
|
||||
|
||||
return [
|
||||
'payment_id' => $this->id,
|
||||
'payment_amount' => $this->amount ?: 0,
|
||||
'payment_applied' => $this->applied ?: 0,
|
||||
'payment_refunded' => $this->refunded ?: 0,
|
||||
'payment_status' => $this->status_id ?: 1,
|
||||
'paymentables' => $this->paymentables->toArray(),
|
||||
'payment_id' => $payment->id,
|
||||
'payment_amount' => $payment->amount ?: 0,
|
||||
'payment_applied' => $payment->applied ?: 0,
|
||||
'payment_refunded' => $payment->refunded ?: 0,
|
||||
'payment_status' => $payment->status_id ?: 1,
|
||||
'paymentables' => $payment->paymentables->toArray(),
|
||||
'payment_request' => request() ? request()->all() : [],
|
||||
];
|
||||
}
|
||||
|
@ -33,7 +33,8 @@ class TransactionEvent extends StaticModel
|
||||
public const INVOICE_PAYMENT_APPLIED = 4;
|
||||
public const INVOICE_CANCELLED = 5;
|
||||
public const INVOICE_FEE_APPLIED = 6;
|
||||
|
||||
public const INVOICE_REVERSED = 7;
|
||||
|
||||
public const PAYMENT_MADE = 100;
|
||||
public const PAYMENT_APPLIED = 101;
|
||||
public const PAYMENT_REFUND = 102;
|
||||
|
@ -15,11 +15,13 @@ use App\Events\Invoice\InvoiceWasReversed;
|
||||
use App\Factory\CreditFactory;
|
||||
use App\Factory\InvoiceItemFactory;
|
||||
use App\Helpers\Invoice\InvoiceSum;
|
||||
use App\Jobs\Ninja\TransactionLog;
|
||||
use App\Models\Client;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Models\Paymentable;
|
||||
use App\Models\TransactionEvent;
|
||||
use App\Services\AbstractService;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\GeneratesCounter;
|
||||
@ -136,6 +138,16 @@ class HandleReversal extends AbstractService
|
||||
|
||||
event(new InvoiceWasReversed($this->invoice, $this->invoice->company, Ninja::eventVars()));
|
||||
|
||||
$transaction = [
|
||||
'invoice' => $this->invoice->transaction_event(),
|
||||
'payment' => $this->payment->transaction_event(),
|
||||
'client' => $this->invoice->client->transaction_event(),
|
||||
'credit' => [],
|
||||
'metadata' => [],
|
||||
];
|
||||
|
||||
TransactionLog::dispatch(TransactionEvent::PAYMENT_REVERSED, $transaction, $this->invoice->company->db);
|
||||
|
||||
return $this->invoice;
|
||||
//create a ledger row for this with the resulting Credit ( also include an explanation in the notes section )
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user