1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

fixes for payment pivots

This commit is contained in:
David Bomba 2023-06-25 00:40:46 +10:00
parent e6b620bc56
commit 89f22dc034
2 changed files with 28 additions and 13 deletions

View File

@ -11,19 +11,20 @@
namespace App\Repositories;
use App\Events\Payment\PaymentWasCreated;
use App\Events\Payment\PaymentWasDeleted;
use App\Jobs\Credit\ApplyCreditPayment;
use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Utils\Ninja;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use App\Models\Paymentable;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use App\Jobs\Credit\ApplyCreditPayment;
use App\Events\Payment\PaymentWasCreated;
use App\Events\Payment\PaymentWasDeleted;
use App\Libraries\Currency\Conversion\CurrencyApi;
/**
* PaymentRepository.
@ -138,7 +139,7 @@ class PaymentRepository extends BaseRepository
$invoices = Invoice::withTrashed()->whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
$payment->invoices()->saveMany($invoices);
// $payment->invoices()->saveMany($invoices);
//todo optimize this into a single query
foreach ($data['invoices'] as $paid_invoice) {
@ -146,6 +147,14 @@ class PaymentRepository extends BaseRepository
$invoice = $invoices->firstWhere('id', $paid_invoice['invoice_id']);
if ($invoice) {
$paymentable = new Paymentable();
$paymentable->payment_id = $payment->id;
$paymentable->paymentable_id = $invoice->id;
$paymentable->paymentable_type = 'invoices';
$paymentable->amount = $paid_invoice['amount'];
$paymentable->save();
$invoice = $invoice->service()
->markSent()
->applyPayment($payment, $paid_invoice['amount'])
@ -161,11 +170,9 @@ class PaymentRepository extends BaseRepository
if (array_key_exists('credits', $data) && is_array($data['credits'])) {
$credit_totals = array_sum(array_column($data['credits'], 'amount'));
// $credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
$credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->get();
$payment->credits()->saveMany($credits);
// $payment->credits()->saveMany($credits);
//todo optimize into a single query
foreach ($data['credits'] as $paid_credit) {
@ -173,6 +180,14 @@ class PaymentRepository extends BaseRepository
$credit = $credits->firstWhere('id', $paid_credit['credit_id']);
if ($credit) {
$paymentable = new Paymentable();
$paymentable->payment_id = $payment->id;
$paymentable->paymentable_id = $credit->id;
$paymentable->paymentable_type = Credit::class;
$paymentable->amount = $paid_invoice['amount'];
$paymentable->save();
$credit = $credit->service()->markSent()->save();
(new ApplyCreditPayment($credit, $payment, $paid_credit['amount'], $credit->company))->handle();
}

View File

@ -84,8 +84,8 @@ class ApplyPayment extends AbstractService
/* Update Pivot Record amount */
$this->payment->invoices->each(function ($inv) use ($amount_paid) {
if ($inv->id == $this->invoice->id) {
$inv->pivot->amount = ($amount_paid * -1);
$inv->pivot->save();
// $inv->pivot->amount = ($amount_paid * -1);
// $inv->pivot->save();
$inv->paid_to_date += floatval($amount_paid * -1);
$inv->save();