Working on credit payments"

This commit is contained in:
David Bomba 2020-10-14 10:53:20 +11:00
parent 8087cd7c88
commit a5555cc434
5 changed files with 83 additions and 19 deletions

View File

@ -19,13 +19,13 @@ use Illuminate\Support\Facades\Log;
class PaymentFactory class PaymentFactory
{ {
public static function create(int $company_id, int $user_id) :Payment public static function create(int $company_id, int $user_id, int $client_id = 0) :Payment
{ {
$payment = new Payment; $payment = new Payment;
$payment->company_id = $company_id; $payment->company_id = $company_id;
$payment->user_id = $user_id; $payment->user_id = $user_id;
$payment->client_id = 0; $payment->client_id = $client_id;
$payment->client_contact_id = null; $payment->client_contact_id = null;
$payment->invitation_id = null; $payment->invitation_id = null;
$payment->company_gateway_id = null; $payment->company_gateway_id = null;

View File

@ -225,22 +225,33 @@ class PaymentController extends Controller
->processPaymentResponse($request); ->processPaymentResponse($request);
} }
/**
* Pay for invoice/s using credits only.
*
* @param Request $request The request object
* @return Response The response view
*/
public function credit_response(Request $request) public function credit_response(Request $request)
{ {
$payment_hash = PaymentHash::find($request->input('payment_hash')); $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->input('payment_hash')])->first();
if($payment_hash->payment->exists()) /* Hydrate the $payment */
if($payment_hash->payment()->exists())
$payment = $payment_hash->payment; $payment = $payment_hash->payment;
else { else {
$payment = PaymentFactory::create($payment_hash->fee_invoice->company_id, $payment_hash->fee_invoice->user_id)->save(); $payment = PaymentFactory::create($payment_hash->fee_invoice->company_id, $payment_hash->fee_invoice->user_id);
$payment->client_id = $payment_hash->fee_invoice->client_id;
$payment->save();
$payment_hash->payment_id = $payment->id; $payment_hash->payment_id = $payment->id;
$payment_hash->save(); $payment_hash->save();
} }
/* Iterate through the invoices and apply credits to them */
collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment, $payment_hash){ collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment, $payment_hash){
$invoice = Invoice::find($this->decodePrimaryKey($payable_invoice['invoice_id'])); $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id));
$amount = $payable_invoice['amount']; $amount = $payable_invoice->amount;
$credits = $payment_hash->fee_invoice $credits = $payment_hash->fee_invoice
->client ->client
@ -253,10 +264,10 @@ class PaymentController extends Controller
$invoice_balance = $invoice->balance; $invoice_balance = $invoice->balance;
//credit payment applied //credit payment applied
$invoice = $credit->service()->applyPayment($invoice, $amount, $payment); $credit->service()->applyPayment($invoice, $amount, $payment);
//amount paid from invoice calculated //amount paid from invoice calculated
$remaining_balance = ($invoice_balance - $invoice->balance); $remaining_balance = ($invoice_balance - $invoice->fresh()->balance);
//reduce the amount to be paid on the invoice from the NEXT credit //reduce the amount to be paid on the invoice from the NEXT credit
$amount -= $remaining_balance; $amount -= $remaining_balance;
@ -268,6 +279,7 @@ class PaymentController extends Controller
}); });
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
} }
} }

View File

@ -13,6 +13,7 @@ namespace App\Services\Client;
use App\Models\Client; use App\Models\Client;
use App\Utils\Number; use App\Utils\Number;
use Illuminate\Database\Eloquent\Collection;
class ClientService class ClientService
{ {
@ -54,7 +55,7 @@ class ClientService
return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision);
} }
public function getCredits() :float public function getCredits() :Collection
{ {
return $this->client->credits return $this->client->credits

View File

@ -12,9 +12,12 @@
namespace App\Services\Credit; namespace App\Services\Credit;
use App\DataMapper\InvoiceItem; use App\DataMapper\InvoiceItem;
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Models\Credit; use App\Models\Credit;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Utils\Ninja;
class ApplyPayment class ApplyPayment
{ {
@ -38,19 +41,20 @@ class ApplyPayment
$this->payment = $payment->fresh(); $this->payment = $payment->fresh();
} }
public function run() :Invoice public function run() :Credit
{ {
//$available_credit_balance = $this->credit->balance; //$available_credit_balance = $this->credit->balance;
$applicable_amount = min($this->amount, $this->credit->balance); $applicable_amount = min($this->amount, $this->credit->balance);
//check invoice partial for amount to be cleared first $invoice_balance = $this->invoice->balance;
/* Check invoice partial for amount to be cleared first */
if($this->invoice->partial > 0){ if($this->invoice->partial > 0){
$partial_payment = min($this->invoice->partial, $applicable_amount); $partial_payment = min($this->invoice->partial, $applicable_amount);
$this->invoice->partial -= $partial_payment; $this->invoice->partial -= $partial_payment;
$this->invoice->balance -= $partial_payment; $invoice_balance -= $partial_payment;
$this->amount -= $partial_payment; $this->amount -= $partial_payment;
// $this->credit->balance -= $partial_payment; // $this->credit->balance -= $partial_payment;
$applicable_amount -= $partial_payment; $applicable_amount -= $partial_payment;
@ -58,18 +62,21 @@ class ApplyPayment
} }
if($this->amount > 0 && $applicable_amount > 0 && $this->invoice->balance > 0){ /* If there is remaining amount use it on the balance */
if($this->amount > 0 && $applicable_amount > 0 && $invoice_balance > 0){
$balance_payment = min($this->invoice->balance, $this->amount); $balance_payment = min($invoice_balance, $this->amount);
$this->invoice->balance -= $balance_payment; $invoice_balance -= $balance_payment;
$this->amount -= $balance_payment; $this->amount -= $balance_payment;
// $this->credit->balance -= $balance_payment; // $this->credit->balance -= $balance_payment;
$this->amount_applied += $balance_payment; $this->amount_applied += $balance_payment;
} }
return $this->invoice; $this->addPaymentToLedger();
return $this->credit;
} }
@ -93,5 +100,49 @@ class ApplyPayment
} }
private function addPaymentToLedger()
{
$this->payment->amount += $this->amount_applied;
$this->payment->applied += $this->amount_applied;
$this->payment->status_id = Payment::STATUS_COMPLETED;
$this->payment->currency_id = $this->credit->client->getSetting('currency_id');
$this->payment->save();
$this->payment
->invoices()
->attach($this->invoice->id, ['amount' => $this->amount_applied]);
$this->payment
->credits()
->attach($this->credit->id, ['amount' => $this->amount_applied]);
$this->payment
->ledger()
->updatePaymentBalance($this->amount_applied * -1);
$this->payment
->client
->service()
->updateBalance($this->amount_applied * -1)
->adjustCreditBalance($this->amount_applied * -1)
->updatePaidToDate($this->amount_applied)
->save();
$this->invoice
->service()
->updateBalance($this->amount_applied * -1)
->updateStatus()
->save();
$this->credit
->ledger()
->updateCreditBalance(($this->amount_applied * -1), "Credit payment applied to Invoice {$this->invoice->number}");
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars()));
if((int)$this->invoice->balance == 0)
event(new InvoiceWasPaid($this->invoice, $this->payment->company, Ninja::eventVars()));
}
} }

View File

@ -64,7 +64,7 @@ class CreditService
public function applyPayment($invoice, $amount, $payment) public function applyPayment($invoice, $amount, $payment)
{ {
$this->credit = (new ApplyPayment($this->client, $invoice, $amount, $payment))->run(); $this->credit = (new ApplyPayment($this->credit, $invoice, $amount, $payment))->run();
return $this; return $this;
} }