1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Models/Traits/ChargesFees.php

106 lines
2.9 KiB
PHP
Raw Normal View History

2017-03-15 16:24:01 +01:00
<?php
namespace App\Models\Traits;
2017-03-15 19:10:40 +01:00
use App\Models\GatewayType;
2017-03-15 16:24:01 +01:00
use App\Models\InvoiceItem;
use App\Models\AccountGatewaySettings;
/**
* Class ChargesFees
*/
trait ChargesFees
{
2017-03-16 22:12:56 +01:00
public function calcGatewayFee($gatewayTypeId = false, $includeTax = false)
2017-03-15 16:24:01 +01:00
{
2017-03-16 15:03:17 +01:00
$account = $this->account;
$settings = $account->getGatewaySettings($gatewayTypeId);
2017-03-17 13:55:15 +01:00
$location = $account->gateway_fee_location;
$taxField = $location == FEE_LOCATION_CHARGE1 ? 'custom_taxes1' : 'custom_taxes1';
2017-03-15 16:24:01 +01:00
$fee = 0;
if (! $settings) {
return false;
}
if ($settings->fee_amount) {
$fee += $settings->fee_amount;
}
if ($settings->fee_percent) {
2017-03-16 18:18:14 +01:00
// prevent charging taxes twice on the surcharge
2017-03-17 13:55:15 +01:00
if ($location == FEE_LOCATION_ITEM) {
$amount = $this->partial > 0 ? $this->partial : $this->balance;
} else {
$amount = $this->amount;
if ($this->$taxField) {
$taxAmount = 0;
foreach ($this->getTaxes() as $key => $tax) {
$taxAmount += $tax['amount'];
}
$amount -= $taxAmount;
2017-03-16 18:18:14 +01:00
}
}
$fee += $amount * $settings->fee_percent / 100;
2017-03-15 16:24:01 +01:00
}
2017-03-16 22:12:56 +01:00
// calculate final amount with tax
2017-03-17 13:55:15 +01:00
if ($includeTax) {
if ($location == FEE_LOCATION_ITEM) {
$preTaxFee = $fee;
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);
}
2017-03-16 22:12:56 +01:00
}
}
2017-03-17 13:55:15 +01:00
return round($fee, 2);
}
2017-03-15 16:24:01 +01:00
2017-03-17 13:55:15 +01:00
public function getGatewayFee()
{
$account = $this->account;
$location = $account->gateway_fee_location;
2017-03-15 16:24:01 +01:00
2017-03-17 13:55:15 +01:00
if (! $location) {
return 0;
2017-03-15 16:24:01 +01:00
}
2017-03-17 13:55:15 +01:00
if ($location == FEE_LOCATION_ITEM) {
$item = $this->getGatewayFeeItem();
return $item ? $item->amount() : 0;
} else {
return $this->$location;
}
2017-03-15 16:24:01 +01:00
}
2017-03-17 11:47:17 +01:00
public function getGatewayFeeItem()
{
if (! $this->relationLoaded('invoice_items')) {
$this->load('invoice_items');
}
foreach ($this->invoice_items as $item) {
if ($item->invoice_item_type_id == INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE) {
return $item;
}
}
return false;
}
2017-03-15 16:24:01 +01:00
}