Working on gateway fees

This commit is contained in:
Hillel Coren 2017-03-17 14:55:15 +02:00
parent 5f23c89121
commit fa2e50f15c
3 changed files with 57 additions and 25 deletions

View File

@ -768,7 +768,16 @@ class Invoice extends EntityModel implements BalanceAffecting
*/
public function getRequestedAmount()
{
return $this->partial > 0 ? $this->partial : $this->balance;
$fee = 0;
if ($this->account->gateway_fee_location) {
$fee = $this->getGatewayFee();
}
if ($this->partial > 0) {
return $this->partial + $fee;
} else {
return $this->balance;
}
}
/**

View File

@ -15,7 +15,8 @@ trait ChargesFees
{
$account = $this->account;
$settings = $account->getGatewaySettings($gatewayTypeId);
$taxField = $account->gateway_fee_location == FEE_LOCATION_CHARGE1 ? 'custom_taxes1' : 'custom_taxes1';
$location = $account->gateway_fee_location;
$taxField = $location == FEE_LOCATION_CHARGE1 ? 'custom_taxes1' : 'custom_taxes1';
$fee = 0;
if (! $settings) {
@ -28,44 +29,65 @@ trait ChargesFees
if ($settings->fee_percent) {
// prevent charging taxes twice on the surcharge
$amount = $this->amount;
if ($this->$taxField) {
$taxAmount = 0;
foreach ($this->getTaxes() as $key => $tax) {
$taxAmount += $tax['amount'];
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;
}
$amount -= $taxAmount;
}
$fee += $amount * $settings->fee_percent / 100;
}
// calculate final amount with tax
if ($includeTax && $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 ($includeTax) {
if ($location == FEE_LOCATION_ITEM) {
$preTaxFee = $fee;
if ($account->gateway_fee_location == FEE_LOCATION_ITEM && $includeTax) {
$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;
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);
}
}
}
return round($fee, 2);
}
public function getGatewayFee()
{
$account = $this->account;
$location = $account->gateway_fee_location;
if (! $location) {
return 0;
}
if ($location == FEE_LOCATION_ITEM) {
$item = $this->getGatewayFeeItem();
return $item ? $item->amount() : 0;
} else {
return $this->$location;
}
}
public function getGatewayFeeItem()
{
if (! $this->relationLoaded('invoice_items')) {

View File

@ -1087,5 +1087,6 @@ class InvoiceRepository extends BaseRepository
}
$this->save($data, $invoice);
$invoice->load('invoice_items');
}
}