Handle negative payments

This commit is contained in:
David Bomba 2021-11-22 21:09:28 +11:00
parent 60d9b4f649
commit 24c733827b
4 changed files with 11 additions and 4 deletions

View File

@ -185,7 +185,7 @@ class PaymentRepository extends BaseRepository {
* @param $payment
* @return
*/
private function processExchangeRates($data, $payment)
public function processExchangeRates($data, $payment)
{
if(array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']))

View File

@ -16,6 +16,7 @@ use App\Jobs\Util\UnlinkFile;
use App\Models\Credit;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Repositories\CreditRepository;
use App\Repositories\PaymentRepository;
use App\Services\Credit\CreateInvitations;
use App\Services\Credit\TriggeredActions;
@ -97,7 +98,7 @@ class CreditService
if($this->credit->balance > 0)
return $this;
$payment_repo = new PaymentRepository();
$payment_repo = new PaymentRepository(new CreditRepository());
//set credit balance to zero
$adjustment = $this->credit->balance;
@ -129,6 +130,7 @@ class CreditService
//reduce client paid_to_date by $this->credit->balance amount
$this->credit
->client
->service()
->updatePaidToDate($adjustment)
->save();

View File

@ -130,7 +130,7 @@ class AutoBillInvoice extends AbstractService
info("Auto Bill payment captured for ".$this->invoice->number);
}
return $this->invoice;
return $this->invoice->fresh();
}
/**

View File

@ -139,8 +139,13 @@ class DeletePayment
if ($this->payment->credits()->exists()) {
$this->payment->credits()->each(function ($paymentable_credit) {
$multiplier = 1;
if($paymentable_credit->pivot->amount < 0)
$multiplier = -1;
$paymentable_credit->service()
->updateBalance($paymentable_credit->pivot->amount)
->updateBalance($paymentable_credit->pivot->amount*$multiplier)
->updatePaidToDate($paymentable_credit->pivot->amount*-1)
->setStatus(Credit::STATUS_SENT)
->save();