diff --git a/app/Http/Controllers/PaymentApiController.php b/app/Http/Controllers/PaymentApiController.php index 9710fdc4fdc8..164f34f3416e 100644 --- a/app/Http/Controllers/PaymentApiController.php +++ b/app/Http/Controllers/PaymentApiController.php @@ -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); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 2501c4d4ea79..7b8ad7081755 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -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); diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 7e12baaeec59..00eec4ddfb99 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -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); }