1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00

Working on negative payments for credits

This commit is contained in:
David Bomba 2021-11-22 15:25:43 +11:00
parent 46c0ba1c7d
commit 60d9b4f649
3 changed files with 64 additions and 1 deletions

View File

@ -524,6 +524,11 @@ class CreditController extends BaseController
{
/*If we are using bulk actions, we don't want to return anything */
switch ($action) {
case 'mark_paid':
$credit->service()->markPaid()->save();
return $this->itemResponse($credit);
break;
case 'clone_to_credit':
$credit = CloneCreditFactory::create($credit, auth()->user()->id);

View File

@ -191,7 +191,7 @@ class PaymentRepository extends BaseRepository {
if(array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']))
return $payment;
$client = Client::find($data['client_id']);
$client = Client::withTrashed()->find($data['client_id']);
$client_currency = $client->getSetting('currency_id');
$company_currency = $client->company->settings->currency_id;

View File

@ -11,8 +11,12 @@
namespace App\Services\Credit;
use App\Factory\PaymentFactory;
use App\Jobs\Util\UnlinkFile;
use App\Models\Credit;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Repositories\PaymentRepository;
use App\Services\Credit\CreateInvitations;
use App\Services\Credit\TriggeredActions;
use App\Utils\Traits\MakesHash;
@ -79,6 +83,60 @@ class CreditService
return $this;
}
/*
For euro users - we mark a credit as paid when
we need to document a refund of sorts.
Criteria: Credit must be a negative value
A negative payment for the balance will be generated
This amount will be reduced from the clients paid to date.
*/
public function markPaid()
{
if($this->credit->balance > 0)
return $this;
$payment_repo = new PaymentRepository();
//set credit balance to zero
$adjustment = $this->credit->balance;
$this->updateBalance($adjustment)
->updatePaidToDate($adjustment)
->save();
//create a negative payment of total $this->credit->balance
$payment = PaymentFactory::create($this->credit->company_id, $this->credit->user_id);
$payment->client_id = $this->credit->client_id;
$payment->amount = $adjustment;
$payment->applied = $adjustment;
$payment->refunded = 0;
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->type_id = PaymentType::CREDIT;
$payment->is_manual = true;
$payment->date = now();
$payment->saveQuietly();
$payment->number = $payment->client->getNextPaymentNumber($payment->client, $payment);
$payment = $payment_repo->processExchangeRates(['client_id' => $this->credit->client_id], $payment);
$payment->saveQuietly();
$payment
->credits()
->attach($this->credit->id, ['amount' => $adjustment]);
//reduce client paid_to_date by $this->credit->balance amount
$this->credit
->client
->updatePaidToDate($adjustment)
->save();
event('eloquent.created: App\Models\Payment', $payment);
return $this;
}
public function markSent()
{
$this->credit = (new MarkSent($this->credit->client, $this->credit))->run();