Refactor payments for credits

This commit is contained in:
David Bomba 2020-10-15 11:37:16 +11:00
parent 4ca112e61e
commit 0d17b299a0
8 changed files with 30 additions and 25 deletions

View File

@ -115,8 +115,6 @@ class InvoiceController extends Controller
'total' => $total, 'total' => $total,
]; ];
//REFACTOR entry point for online payments starts here
return $this->render('invoices.payment', $data); return $this->render('invoices.payment', $data);
} }

View File

@ -68,10 +68,15 @@ class PaymentController extends Controller
* *
* @return \Illuminate\Http\RedirectResponse|mixed * @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')); $gateway = CompanyGateway::findOrFail(request()->input('company_gateway_id'));
//refactor from here!
/** /**
* find invoices * find invoices
* *
@ -171,17 +176,16 @@ class PaymentController extends Controller
$first_invoice = $invoices->first(); $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; $starting_invoice_amount = $first_invoice->amount;
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
/** /**
* * Gateway fee is calculated
* The best way to determine the exact gateway fee is to not calculate it in isolation (due to rounding) * by adding it as a line item, and then subtract
* but to simply add it as a line item, and then subtract the starting and finishing amounts of * the starting and finishing amounts of the invoice.
* the invoice.
*/ */
$fee_totals = $first_invoice->amount - $starting_invoice_amount; $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)]); return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
} }
public function processCreditPayment(Request $request)
{
}
} }

View File

@ -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; return $payment_urls;
} }

View File

@ -66,22 +66,6 @@ class Company extends BaseModel
const ENTITY_RECURRING_TASK = 'task'; const ENTITY_RECURRING_TASK = 'task';
const ENTITY_RECURRING_QUOTE = 'recurring_quote'; 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 $presenter = \App\Models\Presenters\CompanyPresenter::class;
protected $fillable = [ protected $fillable = [

View File

@ -23,6 +23,8 @@ class CompanyGateway extends BaseModel
{ {
use SoftDeletes; use SoftDeletes;
public const GATEWAY_CREDIT = 10000000;
protected $casts = [ protected $casts = [
'fees_and_limits' => 'object', 'fees_and_limits' => 'object',
'updated_at' => 'timestamp', 'updated_at' => 'timestamp',

View File

@ -27,6 +27,7 @@ class GatewayType extends StaticModel
const SOFORT = 7; const SOFORT = 7;
const APPLE_PAY = 8; const APPLE_PAY = 8;
const SEPA = 9; const SEPA = 9;
const CREDIT = 10;
public function gateway() public function gateway()
{ {

View File

@ -47,6 +47,7 @@ class ClientService
public function getCreditBalance() :float public function getCreditBalance() :float
{ {
$credits = $this->client->credits $credits = $this->client->credits
->where('is_deleted', false) ->where('is_deleted', false)
->where('balance', '>', 0) ->where('balance', '>', 0)

View File

@ -55,6 +55,8 @@ class AutoBillInvoice extends AbstractService
return $this->invoice->service()->markPaid()->save(); return $this->invoice->service()->markPaid()->save();
//if the credits cover the payments, we stop here, build the payment with credits and exit early //if the credits cover the payments, we stop here, build the payment with credits and exit early
if($this->invoice->company->use_credits_payment == 'always' || $this->invoice->company->use_credits_payment == 'optin')
$this->applyCreditPayment(); $this->applyCreditPayment();
info("partial = {$this->invoice->partial}"); info("partial = {$this->invoice->partial}");