From 0d17b299a0126140f74e3f15e34fc0099f9cf5ec Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Oct 2020 11:37:16 +1100 Subject: [PATCH] Refactor payments for credits --- .../ClientPortal/InvoiceController.php | 2 -- .../ClientPortal/PaymentController.php | 21 +++++++++++++------ app/Models/Client.php | 8 +++++++ app/Models/Company.php | 16 -------------- app/Models/CompanyGateway.php | 2 ++ app/Models/GatewayType.php | 1 + app/Services/Client/ClientService.php | 1 + app/Services/Invoice/AutoBillInvoice.php | 4 +++- 8 files changed, 30 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 85289b87c533..1f39b528f3eb 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -115,8 +115,6 @@ class InvoiceController extends Controller 'total' => $total, ]; - //REFACTOR entry point for online payments starts here - return $this->render('invoices.payment', $data); } diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 85220ddc2aff..cfc602556b67 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -68,10 +68,15 @@ class PaymentController extends Controller * * @return \Illuminate\Http\RedirectResponse|mixed */ - public function process() + public function process(Request $request) { + if($request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT) + return $this->processCreditPayment($request); + $gateway = CompanyGateway::findOrFail(request()->input('company_gateway_id')); + //refactor from here! + /** * find invoices * @@ -171,17 +176,16 @@ class PaymentController extends Controller $first_invoice = $invoices->first(); - $credit_totals = $first_invoice->client->service()->getCreditBalance(); + $credit_totals = $first_invoice->company->use_credits_payment == 'off' ? 0 : $first_invoice->client->service()->getCreditBalance(); $starting_invoice_amount = $first_invoice->amount; $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); /** - * - * The best way to determine the exact gateway fee is to not calculate it in isolation (due to rounding) - * but to simply add it as a line item, and then subtract the starting and finishing amounts of - * the invoice. + * Gateway fee is calculated + * by adding it as a line item, and then subtract + * the starting and finishing amounts of the invoice. */ $fee_totals = $first_invoice->amount - $starting_invoice_amount; @@ -282,4 +286,9 @@ class PaymentController extends Controller return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } + + public function processCreditPayment(Request $request) + { + + } } diff --git a/app/Models/Client.php b/app/Models/Client.php index e02fd1b5c898..b2a0d8dfd093 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -524,6 +524,14 @@ class Client extends BaseModel implements HasLocalePreference } } + if($this->company->use_credit_payment = 'optin' && $this->service()->getCreditBalance() > 0) { + $payment_urls[] = [ + 'label' => ctrans('texts.apply_credit'), + 'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT, + 'gateway_type_id' => GatewayType::CREDIT, + ]; + } + return $payment_urls; } diff --git a/app/Models/Company.php b/app/Models/Company.php index 9f149f806a01..4690d8c2fc6e 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -66,22 +66,6 @@ class Company extends BaseModel const ENTITY_RECURRING_TASK = 'task'; const ENTITY_RECURRING_QUOTE = 'recurring_quote'; - // const int kModuleRecurringInvoices = 1; - // const int kModuleCredits = 2; - // const int kModuleQuotes = 4; - // const int kModuleTasks = 8; - // const int kModuleExpenses = 16; - // const int kModuleProjects = 32; - // const int kModuleVendors = 64; - // const int kModuleTickets = 128; - // const int kModuleProposals = 256; - // const int kModuleRecurringExpenses = 512; - // const int kModuleRecurringTasks = 1024; - // const int kModuleRecurringQuotes = 2048; - // kModuleInvoices = 4096; - // kModulePayments = 8192; - // 16383 - protected $presenter = \App\Models\Presenters\CompanyPresenter::class; protected $fillable = [ diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index fbf065fd19d8..f37f594112af 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -23,6 +23,8 @@ class CompanyGateway extends BaseModel { use SoftDeletes; + public const GATEWAY_CREDIT = 10000000; + protected $casts = [ 'fees_and_limits' => 'object', 'updated_at' => 'timestamp', diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 5ed2e80c2fc5..04b72832a2a6 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -27,6 +27,7 @@ class GatewayType extends StaticModel const SOFORT = 7; const APPLE_PAY = 8; const SEPA = 9; + const CREDIT = 10; public function gateway() { diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 593091d3c915..9632db976cf1 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -47,6 +47,7 @@ class ClientService public function getCreditBalance() :float { + $credits = $this->client->credits ->where('is_deleted', false) ->where('balance', '>', 0) diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index d3afe99dd3d1..9c53612e7f7c 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -55,7 +55,9 @@ class AutoBillInvoice extends AbstractService return $this->invoice->service()->markPaid()->save(); //if the credits cover the payments, we stop here, build the payment with credits and exit early - $this->applyCreditPayment(); + + if($this->invoice->company->use_credits_payment == 'always' || $this->invoice->company->use_credits_payment == 'optin') + $this->applyCreditPayment(); info("partial = {$this->invoice->partial}"); info("balance = {$this->invoice->balance}");