diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 4829a9b75e88..6ee7b715d67a 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -64,6 +64,8 @@ class PaymentRepository extends BaseRepository */ private function applyPayment(array $data, Payment $payment): ?Payment { +nlog($data); + $is_existing_payment = true; //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) { $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(); $payment->invoices()->saveMany($invoices); @@ -155,6 +159,11 @@ class PaymentRepository extends BaseRepository 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; //wont work because - check tests diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index c8489ef54bc1..952c5038f31c 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -30,52 +30,97 @@ class ApplyPayment extends AbstractService $this->payment_amount = $payment_amount; } + /* Apply payment to a single invoice */ 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 ->ledger() - ->updatePaymentBalance($this->payment_amount * -1); + ->updatePaymentBalance($amount_paid); - // info("apply payment 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(); - - // info("post client balance = {$this->invoice->client->balance}"); + $this->invoice->client->service()->updateBalance($amount_paid)->save(); /* 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) { - $inv->pivot->amount = $this->payment_amount; + $inv->pivot->amount = ($amount_paid*-1); $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(); - // info("3 end of apply payment method the client balnace = {$this->invoice->client->balance}"); - return $this->invoice; } }