diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 4e7144bb1332..d32847a91e1b 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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; + } } /** diff --git a/app/Models/Traits/ChargesFees.php b/app/Models/Traits/ChargesFees.php index c55a8932de18..d3085bab95a4 100644 --- a/app/Models/Traits/ChargesFees.php +++ b/app/Models/Traits/ChargesFees.php @@ -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')) { diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 9c4e7c0f6228..cb5ca20d3dd2 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -1087,5 +1087,6 @@ class InvoiceRepository extends BaseRepository } $this->save($data, $invoice); + $invoice->load('invoice_items'); } }