mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 10:14:36 -04:00
Fixes for payment side of ledger
This commit is contained in:
parent
8149251180
commit
4dca7da070
@ -291,7 +291,7 @@ class BaseController extends Controller
|
|||||||
* Thresholds for displaying large account on first load
|
* Thresholds for displaying large account on first load
|
||||||
*/
|
*/
|
||||||
if (request()->has('first_load') && request()->input('first_load') == 'true') {
|
if (request()->has('first_load') && request()->input('first_load') == 'true') {
|
||||||
if (auth()->user()->getCompany()->invoices->count() > 1000) {
|
if (auth()->user()->getCompany()->invoices->count() > 1000 || auth()->user()->getCompany()->products->count() > 1000) {
|
||||||
$data = $mini_load;
|
$data = $mini_load;
|
||||||
} else {
|
} else {
|
||||||
$data = $first_load;
|
$data = $first_load;
|
||||||
|
@ -56,7 +56,7 @@ class PaymentRepository extends BaseRepository
|
|||||||
return $this->applyPayment($data, $payment);
|
return $this->applyPayment($data, $payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->refundPayment($data, $payment);
|
return $payment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,9 +67,17 @@ class PaymentRepository extends BaseRepository
|
|||||||
*/
|
*/
|
||||||
private function applyPayment(array $data, Payment $payment): ?Payment
|
private function applyPayment(array $data, Payment $payment): ?Payment
|
||||||
{
|
{
|
||||||
|
|
||||||
//check currencies here and fill the exchange rate data if necessary
|
//check currencies here and fill the exchange rate data if necessary
|
||||||
if (!$payment->id) {
|
if (!$payment->id) {
|
||||||
$this->processExchangeRates($data, $payment);
|
$this->processExchangeRates($data, $payment);
|
||||||
|
|
||||||
|
/*We only update the paid to date ONCE per payment*/
|
||||||
|
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
|
||||||
|
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
||||||
|
$client = Client::find($data['client_id']);
|
||||||
|
$client->service()->updatePaidToDate($invoice_totals)->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Fill the payment*/
|
/*Fill the payment*/
|
||||||
@ -77,6 +85,7 @@ class PaymentRepository extends BaseRepository
|
|||||||
$payment->status_id = Payment::STATUS_COMPLETED;
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
$payment->save();
|
$payment->save();
|
||||||
|
|
||||||
|
|
||||||
/*Ensure payment number generated*/
|
/*Ensure payment number generated*/
|
||||||
if (!$payment->number || strlen($payment->number) == 0) {
|
if (!$payment->number || strlen($payment->number) == 0) {
|
||||||
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
$payment->number = $payment->client->getNextPaymentNumber($payment->client);
|
||||||
@ -86,19 +95,33 @@ class PaymentRepository extends BaseRepository
|
|||||||
$credit_totals = 0;
|
$credit_totals = 0;
|
||||||
|
|
||||||
/*Iterate through invoices and apply payments*/
|
/*Iterate through invoices and apply payments*/
|
||||||
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
|
||||||
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
||||||
|
|
||||||
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
|
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
|
||||||
|
|
||||||
|
|
||||||
$payment->invoices()->saveMany($invoices);
|
$payment->invoices()->saveMany($invoices);
|
||||||
|
|
||||||
|
info("iterating through payment invoices");
|
||||||
|
|
||||||
foreach ($data['invoices'] as $paid_invoice) {
|
foreach ($data['invoices'] as $paid_invoice) {
|
||||||
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->first();
|
|
||||||
|
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->with('client')->first();
|
||||||
|
|
||||||
|
info("current client balance = {$invoice->client->balance}");
|
||||||
|
|
||||||
if ($invoice) {
|
if ($invoice) {
|
||||||
$invoice->service()->applyPayment($payment, $paid_invoice['amount'])->save();
|
|
||||||
|
info("apply payment amount {$paid_invoice['amount']}");
|
||||||
|
|
||||||
|
$invoice = $invoice->service()->markSent()->applyPayment($payment, $paid_invoice['amount'])->save();
|
||||||
|
|
||||||
|
info("after processing invoice the client balance is now {$invoice->client->balance}");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//payment is made, but not to any invoice, therefore we are applying the payment to the clients paid_to_date only
|
//payment is made, but not to any invoice, therefore we are applying the payment to the clients paid_to_date only
|
||||||
@ -136,53 +159,6 @@ class PaymentRepository extends BaseRepository
|
|||||||
return $payment->fresh();
|
return $payment->fresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated Refundable trait replaces this.
|
|
||||||
*/
|
|
||||||
private function refundPayment(array $data, Payment $payment): string
|
|
||||||
{
|
|
||||||
// //temp variable to sum the total refund/credit amount
|
|
||||||
// $invoice_total_adjustment = 0;
|
|
||||||
|
|
||||||
// if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
|
|
||||||
|
|
||||||
// foreach ($data['invoices'] as $adjusted_invoice) {
|
|
||||||
|
|
||||||
// $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->first();
|
|
||||||
|
|
||||||
// $invoice_total_adjustment += $adjusted_invoice['amount'];
|
|
||||||
|
|
||||||
// if (array_key_exists('credits', $adjusted_invoice)) {
|
|
||||||
|
|
||||||
// //process and insert credit notes
|
|
||||||
// foreach ($adjusted_invoice['credits'] as $credit) {
|
|
||||||
|
|
||||||
// $credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->id, auth()->user()->id), $invoice);
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// } else {
|
|
||||||
// //todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
|
|
||||||
// return 'Amount must equal the sum of invoice adjustments';
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// //adjust applied amount
|
|
||||||
// $payment->applied += $invoice_total_adjustment;
|
|
||||||
|
|
||||||
// //adjust clients paid to date
|
|
||||||
// $client = $payment->client;
|
|
||||||
// $client->paid_to_date += $invoice_total_adjustment;
|
|
||||||
|
|
||||||
// $payment->save();
|
|
||||||
// $client->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the client is paying in a currency other than
|
* If the client is paying in a currency other than
|
||||||
|
@ -37,7 +37,14 @@ class ApplyPayment extends AbstractService
|
|||||||
->ledger()
|
->ledger()
|
||||||
->updatePaymentBalance($this->payment_amount*-1);
|
->updatePaymentBalance($this->payment_amount*-1);
|
||||||
|
|
||||||
$this->payment->client->service()->updateBalance($this->payment_amount*-1)->save();
|
info("apply paymenet method - current client balance = {$this->payment->client->balance}");
|
||||||
|
|
||||||
|
info("reducing client balance by payment amount {$this->payment_amount}");
|
||||||
|
|
||||||
|
$this->invoice->client->service()->updateBalance($this->payment_amount*-1)->save();
|
||||||
|
// $this->invoice->client->service()->updateBalance($this->payment_amount*-1)->updatePaidToDate($this->payment_amount)->save();
|
||||||
|
|
||||||
|
info("post client balance = {$this->invoice->client->balance}");
|
||||||
|
|
||||||
/* Update Pivot Record amount */
|
/* Update Pivot Record amount */
|
||||||
$this->payment->invoices->each(function ($inv) {
|
$this->payment->invoices->each(function ($inv) {
|
||||||
@ -47,6 +54,10 @@ class ApplyPayment extends AbstractService
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->invoice->fresh('client');
|
||||||
|
|
||||||
|
info("1 end of apply payment method the client balnace = {$this->invoice->client->balance}");
|
||||||
|
|
||||||
if ($this->invoice->hasPartial()) {
|
if ($this->invoice->hasPartial()) {
|
||||||
//is partial and amount is exactly the partial amount
|
//is partial and amount is exactly the partial amount
|
||||||
if ($this->invoice->partial == $this->payment_amount) {
|
if ($this->invoice->partial == $this->payment_amount) {
|
||||||
@ -61,9 +72,11 @@ class ApplyPayment extends AbstractService
|
|||||||
} elseif ($this->payment_amount < $this->invoice->balance) { //partial invoice payment made
|
} elseif ($this->payment_amount < $this->invoice->balance) { //partial invoice payment made
|
||||||
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount*-1);
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount*-1);
|
||||||
}
|
}
|
||||||
|
info("2 end of apply payment method the client balnace = {$this->invoice->client->balance}");
|
||||||
|
|
||||||
$this->invoice->service()->applyNumber()->save();
|
$this->invoice->service()->applyNumber()->save();
|
||||||
|
|
||||||
|
info("3 end of apply payment method the client balnace = {$this->invoice->client->balance}");
|
||||||
return $this->invoice;
|
return $this->invoice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,10 +48,14 @@ class MarkSent extends AbstractService
|
|||||||
->setDueDate()
|
->setDueDate()
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
|
info("marking invoice sent currently client balance = {$this->client->balance}");
|
||||||
|
|
||||||
$this->client->service()->updateBalance($this->invoice->balance)->save();
|
$this->client->service()->updateBalance($this->invoice->balance)->save();
|
||||||
|
|
||||||
|
info("after marking invoice sent currently client balance = {$this->client->balance}");
|
||||||
|
|
||||||
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance);
|
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance);
|
||||||
|
|
||||||
return $this->invoice;
|
return $this->invoice->fresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,7 @@ class CompanyLedgerTest extends TestCase
|
|||||||
|
|
||||||
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
||||||
|
|
||||||
|
//client->balance should = 10
|
||||||
$invoice->service()->markSent()->save();
|
$invoice->service()->markSent()->save();
|
||||||
|
|
||||||
$this->assertEquals($invoice->client->balance, 10);
|
$this->assertEquals($invoice->client->balance, 10);
|
||||||
@ -193,6 +194,7 @@ class CompanyLedgerTest extends TestCase
|
|||||||
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
$invoice = Invoice::find($this->decodePrimaryKey($acc['data']['id']));
|
||||||
$invoice->service()->markSent()->save();
|
$invoice->service()->markSent()->save();
|
||||||
|
|
||||||
|
//client balance should = 20
|
||||||
$this->assertEquals($invoice->client->balance, 20);
|
$this->assertEquals($invoice->client->balance, 20);
|
||||||
$invoice_ledger = $invoice->company_ledger->sortByDesc('id')->first();
|
$invoice_ledger = $invoice->company_ledger->sortByDesc('id')->first();
|
||||||
|
|
||||||
@ -211,7 +213,6 @@ class CompanyLedgerTest extends TestCase
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
'date' => '2020/12/11',
|
'date' => '2020/12/11',
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$response = $this->withHeaders([
|
$response = $this->withHeaders([
|
||||||
@ -224,7 +225,8 @@ class CompanyLedgerTest extends TestCase
|
|||||||
$payment = Payment::find($this->decodePrimaryKey($acc['data']['id']));
|
$payment = Payment::find($this->decodePrimaryKey($acc['data']['id']));
|
||||||
|
|
||||||
$payment_ledger = $payment->company_ledger->sortByDesc('id')->first();
|
$payment_ledger = $payment->company_ledger->sortByDesc('id')->first();
|
||||||
$invoice->fresh();
|
|
||||||
|
info($payment->client->balance);
|
||||||
|
|
||||||
$this->assertEquals($payment->client->balance, $payment_ledger->balance);
|
$this->assertEquals($payment->client->balance, $payment_ledger->balance);
|
||||||
$this->assertEquals($payment->client->paid_to_date, 10);
|
$this->assertEquals($payment->client->paid_to_date, 10);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user