1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Changed gateway_fee_location to gateway_fee_enabled

This commit is contained in:
Hillel Coren 2017-03-26 10:37:32 +03:00
parent 7278b34ad3
commit 0d80ebdbbf
16 changed files with 53 additions and 178 deletions

View File

@ -394,10 +394,6 @@ if (! defined('APP_NAME')) {
define('GATEWAY_TYPE_CUSTOM', 6);
define('GATEWAY_TYPE_TOKEN', 'token');
define('FEE_LOCATION_CHARGE1', 'custom_value1');
define('FEE_LOCATION_CHARGE2', 'custom_value2');
define('FEE_LOCATION_ITEM', 'invoice_item');
define('REMINDER1', 'reminder1');
define('REMINDER2', 'reminder2');
define('REMINDER3', 'reminder3');

View File

@ -1189,7 +1189,7 @@ class AccountController extends BaseController
$account = Auth::user()->account;
$account->token_billing_type_id = Input::get('token_billing_type_id');
$account->auto_bill_on_due_date = boolval(Input::get('auto_bill_on_due_date'));
$account->gateway_fee_location = Input::get('gateway_fee_location') ?: null;
$account->gateway_fee_enabled = boolval(Input::get('gateway_fee_enabled'));
$account->save();

View File

@ -171,7 +171,7 @@ class Account extends Eloquent
'payment_terms',
'reset_counter_frequency_id',
'payment_type_id',
'gateway_fee_location',
'gateway_fee_enabled',
'reset_counter_date',
];
@ -475,11 +475,6 @@ class Account extends Eloquent
return $this->invoice_number_prefix != $this->quote_number_prefix;
}
public function hasGatewayFeeSurcharge()
{
return $this->gateway_fee_location == FEE_LOCATION_CHARGE1 || $this->gateway_fee_location == FEE_LOCATION_CHARGE2;
}
/**
* @return mixed
*/

View File

@ -591,7 +591,7 @@ class Invoice extends EntityModel implements BalanceAffecting
$this->save();
// mark fees as paid
if ($balanceAdjustment != 0 && $this->account->gateway_fee_location == FEE_LOCATION_ITEM) {
if ($balanceAdjustment != 0 && $this->account->gateway_fee_enabled) {
if ($invoiceItem = $this->getGatewayFeeItem()) {
$invoiceItem->markFeePaid();
}
@ -769,7 +769,7 @@ class Invoice extends EntityModel implements BalanceAffecting
public function getRequestedAmount()
{
$fee = 0;
if ($this->account->gateway_fee_location) {
if ($this->account->gateway_fee_enabled) {
$fee = $this->getGatewayFee();
}

View File

@ -15,11 +15,9 @@ trait ChargesFees
{
$account = $this->account;
$settings = $account->getGatewaySettings($gatewayTypeId);
$location = $account->gateway_fee_location;
$taxField = $location == FEE_LOCATION_CHARGE1 ? 'custom_taxes1' : 'custom_taxes1';
$fee = 0;
if (! $settings) {
if (! $account->gateway_fee_enabled) {
return false;
}
@ -34,24 +32,14 @@ trait ChargesFees
// calculate final amount with tax
if ($includeTax) {
if ($location == FEE_LOCATION_ITEM) {
$preTaxFee = $fee;
$preTaxFee = $fee;
if ($settings->fee_tax_rate1) {
$fee += $preTaxFee * $settings->fee_tax_rate1 / 100;
}
if ($settings->fee_tax_rate1) {
$fee += $preTaxFee * $settings->fee_tax_rate1 / 100;
}
if ($settings->fee_tax_rate2) {
$fee += $preTaxFee * $settings->fee_tax_rate2 / 100;
}
} elseif ($this->$taxField) {
$preTaxFee = $fee;
if (floatval($this->tax_rate1)) {
$fee += round($preTaxFee * $this->tax_rate1 / 100, 2);
}
if (floatval($this->tax_rate2)) {
$fee += round($preTaxFee * $this->tax_rate2 / 100, 2);
}
if ($settings->fee_tax_rate2) {
$fee += $preTaxFee * $settings->fee_tax_rate2 / 100;
}
}
@ -61,18 +49,13 @@ trait ChargesFees
public function getGatewayFee()
{
$account = $this->account;
$location = $account->gateway_fee_location;
if (! $location) {
if (! $account->gateway_fee_enabled) {
return 0;
}
if ($location == FEE_LOCATION_ITEM) {
$item = $this->getGatewayFeeItem();
return $item ? $item->amount() : 0;
} else {
return $this->$location;
}
$item = $this->getGatewayFeeItem();
return $item ? $item->amount() : 0;
}
public function getGatewayFeeItem()

View File

@ -97,7 +97,7 @@ class AccountGatewayDatatable extends EntityDatatable
[
'fees',
function ($model) {
if (! $feeLocation = $model->gateway_fee_location) {
if (! $model->gateway_fee_enabled) {
return trans('texts.fees_disabled');
}
@ -119,15 +119,8 @@ class AccountGatewayDatatable extends EntityDatatable
}
$html .= $accountGatewaySettings->feesToString();
if ($feeLocation == FEE_LOCATION_ITEM) {
if ($accountGatewaySettings->hasTaxes()) {
$html .= ' + ' . trans('texts.tax');
}
} else {
$field = ($feeLocation == FEE_LOCATION_CHARGE1 ? 'custom_invoice_taxes1' : 'custom_invoice_taxes1');
if (\Auth::user()->account->$field) {
$html .= ' + ' . trans('texts.tax');
}
if ($accountGatewaySettings->hasTaxes()) {
$html .= ' + ' . trans('texts.tax');
}
};
return $html ?: trans('texts.no_fees');

View File

@ -258,7 +258,7 @@ class InvoicePresenter extends EntityPresenter
$invoice = $this->entity;
$account = $invoice->account;
if (! $account->gateway_fee_location) {
if (! $account->gateway_fee_enabled) {
return '';
}

View File

@ -26,6 +26,6 @@ class AccountGatewayRepository extends BaseRepository
'gateways.name as gateway',
'account_gateways.deleted_at',
'account_gateways.gateway_id',
'accounts.gateway_fee_location');
'accounts.gateway_fee_enabled');
}
}

View File

@ -1015,37 +1015,18 @@ class InvoiceRepository extends BaseRepository
public function clearGatewayFee($invoice)
{
$account = $invoice->account;
$location = $account->gateway_fee_location;
if (! $location) {
return false;
}
if (! $invoice->relationLoaded('invoice_items')) {
$invoice->load('invoice_items');
}
// once an invoice with fee surcharge has been paid don't clear it
if ($account->hasGatewayFeeSurcharge() && $invoice->amount != $invoice->balance) {
return false;
}
if ($location == FEE_LOCATION_ITEM) {
$data = $invoice->toArray();
foreach ($data['invoice_items'] as $key => $item) {
if ($item['invoice_item_type_id'] == INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE) {
unset($data['invoice_items'][$key]);
$this->save($data, $invoice);
$invoice->load('invoice_items');
break;
}
}
} else {
if ($invoice->$location != 0) {
$data = $invoice->toArray();
$data[$location] = 0;
$data = $invoice->toArray();
foreach ($data['invoice_items'] as $key => $item) {
if ($item['invoice_item_type_id'] == INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE) {
unset($data['invoice_items'][$key]);
$this->save($data, $invoice);
$invoice->load('invoice_items');
break;
}
}
}
@ -1053,13 +1034,12 @@ class InvoiceRepository extends BaseRepository
public function setGatewayFee($invoice, $gatewayTypeId)
{
$account = $invoice->account;
$location = $account->gateway_fee_location;
$settings = $account->getGatewaySettings($gatewayTypeId);
if (! $location) {
if (! $account->gateway_fee_enabled) {
return;
}
$settings = $account->getGatewaySettings($gatewayTypeId);
$this->clearGatewayFee($invoice);
if (! $settings) {
@ -1067,27 +1047,19 @@ class InvoiceRepository extends BaseRepository
}
$data = $invoice->toArray();
$fee = $invoice->calcGatewayFee($gatewayTypeId);
if ($location == FEE_LOCATION_ITEM) {
$fee = $invoice->calcGatewayFee($gatewayTypeId);
$item = [];
$item['product_key'] = $fee >= 0 ? trans('texts.surcharge') : trans('texts.discount');
$item['notes'] = $fee >= 0 ? trans('texts.online_payment_surcharge') : trans('texts.online_payment_discount');
$item['qty'] = 1;
$item['cost'] = $fee;
$item['tax_rate1'] = $settings->fee_tax_rate1;
$item['tax_name1'] = $settings->fee_tax_name1;
$item['tax_rate2'] = $settings->fee_tax_rate2;
$item['tax_name2'] = $settings->fee_tax_name2;
$item['invoice_item_type_id'] = INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE;
$data['invoice_items'][] = $item;
} else {
$fee = $invoice->calcGatewayFee($gatewayTypeId);
$data[$location] = $fee;
}
$item = [];
$item['product_key'] = $fee >= 0 ? trans('texts.surcharge') : trans('texts.discount');
$item['notes'] = $fee >= 0 ? trans('texts.online_payment_surcharge') : trans('texts.online_payment_discount');
$item['qty'] = 1;
$item['cost'] = $fee;
$item['tax_rate1'] = $settings->fee_tax_rate1;
$item['tax_name1'] = $settings->fee_tax_name1;
$item['tax_rate2'] = $settings->fee_tax_rate2;
$item['tax_name2'] = $settings->fee_tax_name2;
$item['invoice_item_type_id'] = INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE;
$data['invoice_items'][] = $item;
$this->save($data, $invoice);
$invoice->load('invoice_items');

View File

@ -263,7 +263,7 @@ class AccountTransformer extends EntityTransformer
'payment_terms' => (int) $account->payment_terms,
'reset_counter_frequency_id' => (int) $account->reset_counter_frequency_id,
'payment_type_id' => (int) $account->payment_type_id,
'gateway_fee_location' => $account->gateway_fee_location,
'gateway_fee_enabled' => (bool) $account->gateway_fee_enabled,
'reset_counter_date' => $account->reset_counter_date,
];
}

View File

@ -15,7 +15,7 @@ class AddGatewayFeeLocation extends Migration
Schema::table('accounts', function ($table) {
$table->dropColumn('auto_wrap');
$table->dropColumn('utf8_invoices');
$table->enum('gateway_fee_location', [FEE_LOCATION_CHARGE1, FEE_LOCATION_CHARGE2, FEE_LOCATION_ITEM])->nullable();
$table->boolean('gateway_fee_enabled')->default(0);
$table->date('reset_counter_date')->nullable();
});
}
@ -28,7 +28,7 @@ class AddGatewayFeeLocation extends Migration
public function down()
{
Schema::table('accounts', function ($table) {
$table->dropColumn('gateway_fee_location');
$table->dropColumn('gateway_fee_enabled');
$table->dropColumn('reset_counter_date');
});
}

File diff suppressed because one or more lines are too long

View File

@ -256,7 +256,6 @@
{!! Former::text('custom_invoice_label1')
->label(trans('texts.field_label'))
->placeholder($account->gateway_fee_location == FEE_LOCATION_CHARGE1 ? trans('texts.surcharge') : ' ')
->addGroupClass('pad-checkbox')
->append(Former::checkbox('custom_invoice_taxes1')
->value(1)
@ -264,7 +263,6 @@
{!! Former::text('custom_invoice_label2')
->label(trans('texts.field_label'))
->placeholder($account->gateway_fee_location == FEE_LOCATION_CHARGE2 ? trans('texts.surcharge') : ' ')
->addGroupClass('pad-checkbox')
->append(Former::checkbox('custom_invoice_taxes2')
->value(1)

View File

@ -8,7 +8,7 @@
{!! Former::open()->addClass('warn-on-exit') !!}
{!! Former::populateField('token_billing_type_id', $account->token_billing_type_id) !!}
{!! Former::populateField('auto_bill_on_due_date', $account->auto_bill_on_due_date) !!}
{!! Former::populateField('gateway_fee_location', $account->gateway_fee_location) !!}
{!! Former::populateField('gateway_fee_enabled', $account->gateway_fee_enabled) !!}
<div class="panel panel-default">
<div class="panel-heading">
@ -26,21 +26,11 @@
trans('texts.on_due_date') => ['value'=>1, 'name'=>'auto_bill_on_due_date'],
])->help(trans('texts.auto_bill_ach_date_help')) !!}
{!! Former::checkbox('gateway_fee_location')
->onchange('onGatewayFeeChange()')
{!! Former::checkbox('gateway_fee_enabled')
->help('gateway_fees_help')
->label('gateway_fees')
->text('enable')
->value(FEE_LOCATION_ITEM) !!}
{{-- Former::select('gateway_fee_location')
->addOption(trans('texts.disabled'), '')
->addOption(trans('texts.location_first_surcharge') . ($account->custom_invoice_label1 ? ' | ' . $account->custom_invoice_label1 : ''), FEE_LOCATION_CHARGE1)
->addOption(trans('texts.location_second_surcharge') . ($account->custom_invoice_label2 ? ' | ' . $account->custom_invoice_label2 : '' ), FEE_LOCATION_CHARGE2)
->addOption(trans('texts.location_line_item'), FEE_LOCATION_ITEM)
->onchange('onGatewayFeeChange()')
->help('gateway_fees_help')
->label('gateway_fees') --}}
->value(1) !!}
<br/>
{!! Former::actions( Button::success(trans('texts.save'))->withAttributes(['id' => 'formSave'])->submit()->appendIcon(Icon::create('floppy-disk')) ) !!}
</div>
@ -154,7 +144,6 @@
->step('any')
->append('%') !!}
<div id="taxDiv" style="display:none">
@if ($account->invoice_item_taxes)
{!! Former::select('tax_rate1')
->onchange('onTaxRateChange(1)')
@ -171,7 +160,6 @@
@endif
@endif
</div>
<div style="display:none">
{!! Former::text('fee_tax_name1') !!}
@ -182,12 +170,9 @@
<div class="help-block">
<span id="feeSample"></span>
@if ($account->gateway_fee_location == FEE_LOCATION_ITEM && !$account->invoice_item_taxes && $account->invoice_taxes && count($taxRates))
@if ($account->gateway_fee_enabled && !$account->invoice_item_taxes && $account->invoice_taxes && count($taxRates))
<br/>{{ trans('texts.fees_tax_help') }}
@endif
@if ($account->hasGatewayFeeSurcharge())
<br/>{!! trans('texts.fees_surcharge_help', ['link' => link_to('/settings/invoice_settings#invoice_surcharges', trans('texts.label_and_taxes'), ['target' => '_blank'])]) !!}
@endif
</div>
<br/><b>{{ trans('texts.gateway_fees_disclaimer') }}</b>
@ -269,10 +254,6 @@
updateFeeSample();
@if ($account->gateway_fee_location == FEE_LOCATION_ITEM)
$('#taxDiv').show();
@endif
if (gateway_type_id == {{ GATEWAY_TYPE_CUSTOM }}) {
$('#feesEnabled').hide();
$('#feesDisabled').show();
@ -397,20 +378,6 @@
onTaxRateChange(instance);
}
function onGatewayFeeChange()
{
if (window.hasShownGatewayFeeWarning) {
return;
}
window.hasShownGatewayFeeWarning = true;
var accountEnabled = '{{ $account->gateway_fee_location ? 'true' : 'false' }}';
var formEnabled = $('#gateway_fee_location').is(':checked');
if (accountEnabled && ! formEnabled) {
swal("{!! trans('texts.warning') !!}", "{!! trans('texts.gateway_fee_change_warning') !!}");
}
}
</script>
@stop

View File

@ -157,11 +157,8 @@
{!! Former::text('due_date')->data_bind("datePicker: due_date, valueUpdate: 'afterkeydown'")->label(trans("texts.{$entityType}_due_date"))
->placeholder($invoice->exists || $invoice->isQuote() ? ' ' : $account->present()->dueDatePlaceholder())
->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))->appendIcon('calendar')->addGroupClass('due_date') !!}
@if (! $account->hasGatewayFeeSurcharge())
{!! Former::text('partial')->data_bind("value: partial, valueUpdate: 'afterkeydown'")->onkeyup('onPartialChange()')
->addGroupClass('partial')!!}
@endif
{!! Former::text('partial')->data_bind("value: partial, valueUpdate: 'afterkeydown'")->onkeyup('onPartialChange()')
->addGroupClass('partial')!!}
</div>
@if ($entityType == ENTITY_INVOICE)
<div data-bind="visible: is_recurring" style="display: none">

View File

@ -39,22 +39,8 @@ class GatewayFeesCest
$partialFee = $feeAmount + ($total / 2 * $feeAmount / 100);
$partialFeeWithTax = $partialFee + ($partialFee * $taxRate / 100);
// surcharge gateway fee
$this->configureSurchargeTaxRates($I, $taxName, $taxRate);
$this->configureFees($I, $feeAmount, $feePercent);
$I->createProduct($I, $productKey, $cost);
$I->createClient($I, $clientEmail, $clientName);
// without taxing the fee
$I->uncheckSettingOption($I, 'invoice_settings#invoice_surcharges', 'custom_invoice_taxes1');
$this->createInvoice($I, $clientName, $productKey, $total, $fee);
// with taxing the fee
$I->checkSettingOption($I, 'invoice_settings#invoice_surcharges', 'custom_invoice_taxes1');
$this->createInvoice($I, $clientName, $productKey, $total, $feeWithTax);
// line item gateway fee
$this->configureLineItemTaxRates($I, $taxName, $taxRate);
$productKey = $this->faker->word();
@ -68,7 +54,6 @@ class GatewayFeesCest
$this->configureGatewayFeeTax($I, $taxName, $taxRate);
$this->createInvoice($I, $clientName, $productKey, $total, $feeWithTax);
// partial invoice
$invitationKey = $this->createInvoice($I, $clientName, $productKey, $total, $partialFeeWithTax, $total / 2);
$this->createPayment($I, $invitationKey, $total + $partialFeeWithTax, 0, $partialFeeWithTax);
@ -94,24 +79,13 @@ class GatewayFeesCest
$I->click('#modalSave');
}
private function configureSurchargeTaxRates($I, $taxName, $taxRate)
{
$taxOption = $taxName . ': ' . number_format($taxRate, 3) . '%';
$I->createTaxRate($I, $taxName, $taxRate);
$I->amOnPage('/settings/tax_rates');
$I->checkOption('#invoice_item_taxes');
$I->selectOption('default_tax_rate_id', $taxOption);
$I->click('Save');
}
private function configureLineItemTaxRates($I, $taxName, $taxRate)
{
$taxOption = $taxName . ': ' . number_format($taxRate, 3) . '%';
// set the gateway fee to use line items
$I->amOnPage('/settings/online_payments#fees');
$I->selectOption('gateway_fee_location', 'invoice_item');
$I->checkOption('gateway_fee_enabled');
$I->click('#formSave');
// disable the default invoice tax