Working on gateway fees

This commit is contained in:
Hillel Coren 2017-03-14 23:27:09 +02:00
parent f1c464c4cf
commit 5fcf0c57a4
4 changed files with 79 additions and 19 deletions

View File

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use Utils;
/** /**
* Class AccountGatewaySettings. * Class AccountGatewaySettings.
*/ */
@ -41,4 +43,28 @@ class AccountGatewaySettings extends EntityModel
{ {
// to Disable created_at // to Disable created_at
} }
public function areFeesEnabled()
{
return floatval($this->fee_amount) || floatval($this->fee_percent);
}
public function feesToString()
{
$parts = [];
if (floatval($this->fee_amount)) {
$parts[] = Utils::formatMoney($this->fee_amount);
}
if (floatval($this->fee_percent)) {
$parts[] = $this->fee_percent . '%';
}
if (floatval($this->fee_tax_rate1) || floatval($this->fee_tax_rate1)) {
$parts[] = trans('texts.tax');
}
return join(' + ', $parts);
}
} }

View File

@ -12,6 +12,7 @@ use Utils;
class AccountGatewayDatatable extends EntityDatatable class AccountGatewayDatatable extends EntityDatatable
{ {
private static $accountGateways; private static $accountGateways;
private static $accountGatewaySettings;
public $entityType = ENTITY_ACCOUNT_GATEWAY; public $entityType = ENTITY_ACCOUNT_GATEWAY;
@ -62,26 +63,16 @@ class AccountGatewayDatatable extends EntityDatatable
[ [
'limit', 'limit',
function ($model) { function ($model) {
if ($model->gateway_id == GATEWAY_CUSTOM) { $gatewayTypes = $this->getGatewayTypes($model->id, $model->gateway_id);
$gatewayTypes = [GATEWAY_TYPE_CUSTOM];
} else {
$accountGateway = $this->getAccountGateway($model->id);
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = $paymentDriver->gatewayTypes();
$gatewayTypes = array_diff($gatewayTypes, [GATEWAY_TYPE_TOKEN]);
}
$html = ''; $html = '';
foreach ($gatewayTypes as $gatewayTypeId) { foreach ($gatewayTypes as $gatewayTypeId) {
$accountGatewaySettings = AccountGatewaySettings::scope()->where('account_gateway_settings.gateway_type_id', $accountGatewaySettings = $this->getAccountGatewaySetting($gatewayTypeId);
'=', $gatewayTypeId)->first(); $gatewayType = Utils::getFromCache($gatewayTypeId, 'gatewayTypes');
$gatewayType = GatewayType::find($gatewayTypeId);
if (count($gatewayTypes) > 1) { if (count($gatewayTypes) > 1) {
if ($html) { if ($html) {
$html .= '<br>'; $html .= '<br>';
} }
$html .= $gatewayType->name . ' &mdash; '; $html .= $gatewayType->name . ' &mdash; ';
} }
@ -106,7 +97,25 @@ class AccountGatewayDatatable extends EntityDatatable
[ [
'fees', 'fees',
function ($model) { function ($model) {
return 'Fees description...'; $gatewayTypes = $this->getGatewayTypes($model->id, $model->gateway_id);
$html = '';
foreach ($gatewayTypes as $gatewayTypeId) {
$accountGatewaySettings = $this->getAccountGatewaySetting($gatewayTypeId);
if (! $accountGatewaySettings || ! $accountGatewaySettings->areFeesEnabled()) {
continue;
}
$gatewayType = Utils::getFromCache($gatewayTypeId, 'gatewayTypes');
if (count($gatewayTypes) > 1) {
if ($html) {
$html .= '<br>';
}
$html .= $gatewayType->name . ' &mdash; ';
}
$html .= $accountGatewaySettings->feesToString();
};
return $html ?: trans('texts.no_fees');
}, },
], ],
]; ];
@ -168,9 +177,7 @@ class AccountGatewayDatatable extends EntityDatatable
$actions[] = [ $actions[] = [
trans('texts.set_limits_fees', ['gateway_type' => $gatewayType->name]), trans('texts.set_limits_fees', ['gateway_type' => $gatewayType->name]),
function () use ($gatewayType) { function () use ($gatewayType) {
$accountGatewaySettings = AccountGatewaySettings::scope() //$accountGatewaySettings = $this->getAccountGatewaySetting($gatewayType->id);
->where('account_gateway_settings.gateway_type_id', '=', $gatewayType->id)
->first();
//$min = $accountGatewaySettings && $accountGatewaySettings->min_limit !== null ? $accountGatewaySettings->min_limit : 'null'; //$min = $accountGatewaySettings && $accountGatewaySettings->min_limit !== null ? $accountGatewaySettings->min_limit : 'null';
//$max = $accountGatewaySettings && $accountGatewaySettings->max_limit !== null ? $accountGatewaySettings->max_limit : 'null'; //$max = $accountGatewaySettings && $accountGatewaySettings->max_limit !== null ? $accountGatewaySettings->max_limit : 'null';
return "javascript:showLimitsModal('{$gatewayType->name}', {$gatewayType->id})"; return "javascript:showLimitsModal('{$gatewayType->name}', {$gatewayType->id})";
@ -200,4 +207,30 @@ class AccountGatewayDatatable extends EntityDatatable
return static::$accountGateways[$id]; return static::$accountGateways[$id];
} }
private function getAccountGatewaySetting($gatewayTypeId)
{
if (isset(static::$accountGatewaySettings[$gatewayTypeId])) {
return static::$accountGatewaySettings[$gatewayTypeId];
}
static::$accountGatewaySettings[$gatewayTypeId] = AccountGatewaySettings::scope()
->where('account_gateway_settings.gateway_type_id', '=', $gatewayTypeId)->first();
return static::$accountGatewaySettings[$gatewayTypeId];
}
private function getGatewayTypes($id, $gatewayId)
{
if ($gatewayId == GATEWAY_CUSTOM) {
$gatewayTypes = [GATEWAY_TYPE_CUSTOM];
} else {
$accountGateway = $this->getAccountGateway($id);
$paymentDriver = $accountGateway->paymentDriver();
$gatewayTypes = $paymentDriver->gatewayTypes();
$gatewayTypes = array_diff($gatewayTypes, [GATEWAY_TYPE_TOKEN]);
}
return $gatewayTypes;
}
} }

View File

@ -2405,6 +2405,7 @@ $LANG = array(
'fee_percent' => 'Percent', 'fee_percent' => 'Percent',
'fees_tax_help' => 'Enable line item taxes to set fee tax rates.', 'fees_tax_help' => 'Enable line item taxes to set fee tax rates.',
'fees_sample' => 'The fee for a :amount invoice would be :total.', 'fees_sample' => 'The fee for a :amount invoice would be :total.',
'no_fees' => 'No Fees',
); );

View File

@ -51,7 +51,7 @@
->setOptions('sPaginationType', 'bootstrap') ->setOptions('sPaginationType', 'bootstrap')
->setOptions('bFilter', false) ->setOptions('bFilter', false)
->setOptions('bAutoWidth', false) ->setOptions('bAutoWidth', false)
->setOptions('aoColumns', [[ "sWidth"=> "30%" ], ["sWidth"=> "30%"], ["sWidth"=> "20%"], ["sWidth"=> "20%"]]) ->setOptions('aoColumns', [[ "sWidth"=> "24%" ], ["sWidth"=> "27%"], ["sWidth"=> "27%"], ["sWidth"=> "20%"]])
->setOptions('aoColumnDefs', [['bSortable'=>false, 'aTargets'=>[1, 2, 3]]]) ->setOptions('aoColumnDefs', [['bSortable'=>false, 'aTargets'=>[1, 2, 3]]])
->render('datatable') !!} ->render('datatable') !!}
@ -234,7 +234,7 @@
} }
$('#paymentLimitsModal').modal('show'); $('#paymentLimitsModal').modal('show');
updateFeeSample(); updateFeeSample();
} }