Create credit if overpaying with API

This commit is contained in:
Hillel Coren 2017-10-24 13:16:46 +03:00
parent dad057d99d
commit 26f4cf16c9
3 changed files with 20 additions and 11 deletions

View File

@ -9,20 +9,23 @@ use App\Models\Invoice;
use App\Models\Payment;
use App\Ninja\Mailers\ContactMailer;
use App\Ninja\Repositories\PaymentRepository;
use App\Services\PaymentService;
use Input;
use Response;
class PaymentApiController extends BaseAPIController
{
protected $paymentRepo;
protected $paymentService;
protected $entityType = ENTITY_PAYMENT;
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer)
public function __construct(PaymentRepository $paymentRepo, PaymentService $paymentService, ContactMailer $contactMailer)
{
parent::__construct();
$this->paymentRepo = $paymentRepo;
$this->paymentService = $paymentService;
$this->contactMailer = $contactMailer;
}
@ -108,7 +111,7 @@ class PaymentApiController extends BaseAPIController
// check payment has been marked sent
$request->invoice->markSentIfUnsent();
$payment = $this->paymentRepo->save($request->input());
$payment = $this->paymentService->save($request->input(), null, $request->invoice);
if (Input::get('email_receipt')) {
$this->contactMailer->sendPaymentConfirmation($payment);

View File

@ -191,16 +191,10 @@ class PaymentController extends BaseController
// 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;
$credit = true;
}
$payment = $this->paymentService->save($input);
$payment = $this->paymentService->save($input, null, $request->invoice);
if (Input::get('email_receipt')) {
$this->contactMailer->sendPaymentConfirmation($payment);

View File

@ -5,6 +5,7 @@ namespace App\Services;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Invoice;
use App\Ninja\Datatables\PaymentDatatable;
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);
}