mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
Working on payments
This commit is contained in:
parent
887b2bb8c8
commit
e054894ffa
@ -64,6 +64,8 @@ class PaymentRepository extends BaseRepository
|
|||||||
*/
|
*/
|
||||||
private function applyPayment(array $data, Payment $payment): ?Payment
|
private function applyPayment(array $data, Payment $payment): ?Payment
|
||||||
{
|
{
|
||||||
|
nlog($data);
|
||||||
|
|
||||||
$is_existing_payment = true;
|
$is_existing_payment = true;
|
||||||
|
|
||||||
//check currencies here and fill the exchange rate data if necessary
|
//check currencies here and fill the exchange rate data if necessary
|
||||||
@ -117,6 +119,8 @@ class PaymentRepository extends BaseRepository
|
|||||||
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
|
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'));
|
||||||
|
|
||||||
|
nlog("invoice totals = {$invoice_totals}");
|
||||||
|
|
||||||
$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);
|
||||||
@ -155,6 +159,11 @@ class PaymentRepository extends BaseRepository
|
|||||||
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nlog("payment amount = {$payment->amount}");
|
||||||
|
nlog("payment applied = {$payment->applied}");
|
||||||
|
nlog("invoice totals = {$invoice_totals}");
|
||||||
|
nlog("credit totals = {$credit_totals}");
|
||||||
|
|
||||||
$payment->applied += ($invoice_totals - $credit_totals); //wont work because - check tests
|
$payment->applied += ($invoice_totals - $credit_totals); //wont work because - check tests
|
||||||
// $payment->applied += $invoice_totals; //wont work because - check tests
|
// $payment->applied += $invoice_totals; //wont work because - check tests
|
||||||
|
|
||||||
|
@ -30,52 +30,97 @@ class ApplyPayment extends AbstractService
|
|||||||
$this->payment_amount = $payment_amount;
|
$this->payment_amount = $payment_amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Apply payment to a single invoice */
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$this->invoice->fresh('client');
|
||||||
|
|
||||||
|
$amount_paid = 0;
|
||||||
|
|
||||||
|
if ($this->invoice->hasPartial()) {
|
||||||
|
|
||||||
|
if ($this->invoice->partial == $this->payment_amount)
|
||||||
|
{
|
||||||
|
|
||||||
|
//is partial and amount is exactly the partial amount
|
||||||
|
|
||||||
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
}
|
||||||
|
elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount)
|
||||||
|
{
|
||||||
|
//partial amount exists, but the amount is less than the partial amount
|
||||||
|
|
||||||
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->updatePartial($amount_paid)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
}
|
||||||
|
elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount)
|
||||||
|
{
|
||||||
|
//partial exists and the amount paid is GREATER than the partial amount
|
||||||
|
|
||||||
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ($this->payment_amount == $this->invoice->balance)
|
||||||
|
{
|
||||||
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
}
|
||||||
|
elseif ($this->payment_amount < $this->invoice->balance)
|
||||||
|
{
|
||||||
|
//partial invoice payment made
|
||||||
|
|
||||||
|
$amount_paid = $this->payment_amount * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
elseif ($this->payment_amount > $this->invoice->balance)
|
||||||
|
{
|
||||||
|
//partial invoice payment made
|
||||||
|
|
||||||
|
$amount_paid = $this->invoice->balance * -1;
|
||||||
|
|
||||||
|
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($amount_paid);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $this->payment
|
||||||
|
// ->ledger()
|
||||||
|
// ->updatePaymentBalance($this->payment_amount * -1);
|
||||||
|
|
||||||
|
|
||||||
$this->payment
|
$this->payment
|
||||||
->ledger()
|
->ledger()
|
||||||
->updatePaymentBalance($this->payment_amount * -1);
|
->updatePaymentBalance($amount_paid);
|
||||||
|
|
||||||
// info("apply payment method - current client balance = {$this->payment->client->balance}");
|
$this->invoice->client->service()->updateBalance($amount_paid)->save();
|
||||||
|
|
||||||
// info("reducing client balance by payment amount {$this->payment_amount}");
|
|
||||||
|
|
||||||
$this->invoice->client->service()->updateBalance($this->payment_amount * -1)->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) use($amount_paid){
|
||||||
if ($inv->id == $this->invoice->id) {
|
if ($inv->id == $this->invoice->id) {
|
||||||
$inv->pivot->amount = $this->payment_amount;
|
$inv->pivot->amount = ($amount_paid*-1);
|
||||||
$inv->pivot->save();
|
$inv->pivot->save();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->invoice->fresh('client');
|
|
||||||
|
|
||||||
// info("1 end of apply payment method the client balance = {$this->invoice->client->balance}");
|
|
||||||
|
|
||||||
if ($this->invoice->hasPartial()) {
|
|
||||||
//is partial and amount is exactly the partial amount
|
|
||||||
if ($this->invoice->partial == $this->payment_amount) {
|
|
||||||
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount * -1);
|
|
||||||
} elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->payment_amount) { //partial amount exists, but the amount is less than the partial amount
|
|
||||||
$this->invoice->service()->updatePartial($this->payment_amount * -1)->updateBalance($this->payment_amount * -1);
|
|
||||||
} elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->payment_amount) { //partial exists and the amount paid is GREATER than the partial amount
|
|
||||||
$this->invoice->service()->clearPartial()->setDueDate()->setStatus(Invoice::STATUS_PARTIAL)->updateBalance($this->payment_amount * -1);
|
|
||||||
}
|
|
||||||
} elseif ($this->payment_amount == $this->invoice->balance) { //total invoice paid.
|
|
||||||
$this->invoice->service()->clearPartial()->setStatus(Invoice::STATUS_PAID)->updateBalance($this->payment_amount * -1);
|
|
||||||
} elseif ($this->payment_amount < $this->invoice->balance) { //partial invoice payment made
|
|
||||||
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user