mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 17:14:31 -04:00
Create credit if overpaying with API
This commit is contained in:
parent
dad057d99d
commit
26f4cf16c9
@ -9,20 +9,23 @@ use App\Models\Invoice;
|
|||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Ninja\Mailers\ContactMailer;
|
use App\Ninja\Mailers\ContactMailer;
|
||||||
use App\Ninja\Repositories\PaymentRepository;
|
use App\Ninja\Repositories\PaymentRepository;
|
||||||
|
use App\Services\PaymentService;
|
||||||
use Input;
|
use Input;
|
||||||
use Response;
|
use Response;
|
||||||
|
|
||||||
class PaymentApiController extends BaseAPIController
|
class PaymentApiController extends BaseAPIController
|
||||||
{
|
{
|
||||||
protected $paymentRepo;
|
protected $paymentRepo;
|
||||||
|
protected $paymentService;
|
||||||
|
|
||||||
protected $entityType = ENTITY_PAYMENT;
|
protected $entityType = ENTITY_PAYMENT;
|
||||||
|
|
||||||
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer)
|
public function __construct(PaymentRepository $paymentRepo, PaymentService $paymentService, ContactMailer $contactMailer)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->paymentRepo = $paymentRepo;
|
$this->paymentRepo = $paymentRepo;
|
||||||
|
$this->paymentService = $paymentService;
|
||||||
$this->contactMailer = $contactMailer;
|
$this->contactMailer = $contactMailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +111,7 @@ class PaymentApiController extends BaseAPIController
|
|||||||
// check payment has been marked sent
|
// check payment has been marked sent
|
||||||
$request->invoice->markSentIfUnsent();
|
$request->invoice->markSentIfUnsent();
|
||||||
|
|
||||||
$payment = $this->paymentRepo->save($request->input());
|
$payment = $this->paymentService->save($request->input(), null, $request->invoice);
|
||||||
|
|
||||||
if (Input::get('email_receipt')) {
|
if (Input::get('email_receipt')) {
|
||||||
$this->contactMailer->sendPaymentConfirmation($payment);
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
||||||
|
@ -191,16 +191,10 @@ class PaymentController extends BaseController
|
|||||||
|
|
||||||
// if the payment amount is more than the balance create a credit
|
// if the payment amount is more than the balance create a credit
|
||||||
if ($amount > $request->invoice->balance) {
|
if ($amount > $request->invoice->balance) {
|
||||||
$credit = Credit::createNew();
|
$credit = true;
|
||||||
$credit->client_id = $request->invoice->client_id;
|
|
||||||
$credit->credit_date = date_create()->format('Y-m-d');
|
|
||||||
$credit->amount = $credit->balance = $amount - $request->invoice->balance;
|
|
||||||
$credit->private_notes = trans('texts.credit_created_by', ['transaction_reference' => $input['transaction_reference']]);
|
|
||||||
$credit->save();
|
|
||||||
$input['amount'] = $request->invoice->balance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$payment = $this->paymentService->save($input);
|
$payment = $this->paymentService->save($input, null, $request->invoice);
|
||||||
|
|
||||||
if (Input::get('email_receipt')) {
|
if (Input::get('email_receipt')) {
|
||||||
$this->contactMailer->sendPaymentConfirmation($payment);
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
||||||
|
@ -5,6 +5,7 @@ namespace App\Services;
|
|||||||
use App\Models\Account;
|
use App\Models\Account;
|
||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Models\Credit;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Ninja\Datatables\PaymentDatatable;
|
use App\Ninja\Datatables\PaymentDatatable;
|
||||||
use App\Ninja\Repositories\AccountRepository;
|
use App\Ninja\Repositories\AccountRepository;
|
||||||
@ -149,8 +150,19 @@ class PaymentService extends BaseService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save($input, $payment = null)
|
public function save($input, $payment = null, $invoice = null)
|
||||||
{
|
{
|
||||||
|
// if the payment amount is more than the balance create a credit
|
||||||
|
if ($invoice && $input['amount'] > $invoice->balance) {
|
||||||
|
$credit = Credit::createNew();
|
||||||
|
$credit->client_id = $invoice->client_id;
|
||||||
|
$credit->credit_date = date_create()->format('Y-m-d');
|
||||||
|
$credit->amount = $credit->balance = $input['amount'] - $invoice->balance;
|
||||||
|
$credit->private_notes = trans('texts.credit_created_by', ['transaction_reference' => isset($input['transaction_reference']) ? $input['transaction_reference'] : '']);
|
||||||
|
$credit->save();
|
||||||
|
$input['amount'] = $invoice->balance;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->paymentRepo->save($input, $payment);
|
return $this->paymentRepo->save($input, $payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user