mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Payment amount greater than invoice amount #1317
This commit is contained in:
parent
dd15832469
commit
5fb0ee1393
@ -6,6 +6,7 @@ use App\Http\Requests\CreatePaymentRequest;
|
|||||||
use App\Http\Requests\PaymentRequest;
|
use App\Http\Requests\PaymentRequest;
|
||||||
use App\Http\Requests\UpdatePaymentRequest;
|
use App\Http\Requests\UpdatePaymentRequest;
|
||||||
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\Mailers\ContactMailer;
|
use App\Ninja\Mailers\ContactMailer;
|
||||||
@ -180,17 +181,28 @@ class PaymentController extends BaseController
|
|||||||
{
|
{
|
||||||
// check payment has been marked sent
|
// check payment has been marked sent
|
||||||
$request->invoice->markSentIfUnsent();
|
$request->invoice->markSentIfUnsent();
|
||||||
|
|
||||||
$input = $request->input();
|
$input = $request->input();
|
||||||
$input['invoice_id'] = Invoice::getPrivateId($input['invoice']);
|
$amount = Utils::parseFloat($input['amount']);
|
||||||
$input['client_id'] = Client::getPrivateId($input['client']);
|
$credit = false;
|
||||||
$payment = $this->paymentRepo->save($input);
|
|
||||||
|
// if the payment amount is more than the balance create a credit
|
||||||
|
if ($amount > $request->invoice->balance) {
|
||||||
|
$credit = Credit::createNew();
|
||||||
|
$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);
|
||||||
|
|
||||||
if (Input::get('email_receipt')) {
|
if (Input::get('email_receipt')) {
|
||||||
$this->contactMailer->sendPaymentConfirmation($payment);
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
||||||
Session::flash('message', trans('texts.created_payment_emailed_client'));
|
Session::flash('message', trans($credit ? 'texts.created_payment_and_credit_emailed_client' : 'texts.created_payment_emailed_client'));
|
||||||
} else {
|
} else {
|
||||||
Session::flash('message', trans('texts.created_payment'));
|
Session::flash('message', trans($credit ? 'texts.created_payment_and_credit' : 'texts.created_payment'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->to($payment->client->getRoute() . '#payments');
|
return redirect()->to($payment->client->getRoute() . '#payments');
|
||||||
|
@ -28,10 +28,15 @@ class CreatePaymentRequest extends PaymentRequest
|
|||||||
->invoices()
|
->invoices()
|
||||||
->firstOrFail();
|
->firstOrFail();
|
||||||
|
|
||||||
|
$this->merge([
|
||||||
|
'invoice_id' => $invoice->id,
|
||||||
|
'client_id' => $invoice->client->id,
|
||||||
|
]);
|
||||||
|
|
||||||
$rules = [
|
$rules = [
|
||||||
'client' => 'required', // TODO: change to client_id once views are updated
|
'client' => 'required', // TODO: change to client_id once views are updated
|
||||||
'invoice' => 'required', // TODO: change to invoice_id once views are updated
|
'invoice' => 'required', // TODO: change to invoice_id once views are updated
|
||||||
'amount' => "required|numeric|between:0.01,{$invoice->balance}",
|
'amount' => "required|numeric|min:0.01",
|
||||||
'payment_date' => 'required',
|
'payment_date' => 'required',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -140,6 +140,12 @@ class PaymentService extends BaseService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function save($input, $payment = null)
|
||||||
|
{
|
||||||
|
return $this->paymentRepo->save($input, $payment);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getDatatable($clientPublicId, $search)
|
public function getDatatable($clientPublicId, $search)
|
||||||
{
|
{
|
||||||
$datatable = new PaymentDatatable(true, $clientPublicId);
|
$datatable = new PaymentDatatable(true, $clientPublicId);
|
||||||
|
@ -2389,6 +2389,9 @@ $LANG = array(
|
|||||||
'updated_payment_term' => 'Successfully updated payment term',
|
'updated_payment_term' => 'Successfully updated payment term',
|
||||||
'archived_payment_term' => 'Successfully archived payment term',
|
'archived_payment_term' => 'Successfully archived payment term',
|
||||||
'resend_invite' => 'Resend Invitation',
|
'resend_invite' => 'Resend Invitation',
|
||||||
|
'credit_created_by' => 'Credit created by payment :transaction_reference',
|
||||||
|
'created_payment_and_credit' => 'Successfully created payment and credit',
|
||||||
|
'created_payment_and_credit_emailed_client' => 'Successfully created payment and credit, and emailed client',
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user