From 23eee0183ec90c67b367704014095d60675f2524 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 17 Mar 2017 12:47:17 +0200 Subject: [PATCH] Working on gateway fees --- app/Constants.php | 3 +- app/Models/Traits/ChargesFees.php | 17 ++++++++-- app/Ninja/Repositories/InvoiceRepository.php | 35 +++++++++++++++++--- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/Constants.php b/app/Constants.php index bd08211e3429..a4def8e7925f 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -43,7 +43,8 @@ if (! defined('APP_NAME')) { define('INVOICE_ITEM_TYPE_STANDARD', 1); define('INVOICE_ITEM_TYPE_TASK', 2); - define('INVOICE_ITEM_TYPE_GATEWAY_FEE', 3); + define('INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE', 3); + define('INVOICE_ITEM_TYPE_PAID_GATEWAY_FEE', 4); define('PERSON_CONTACT', 'contact'); define('PERSON_USER', 'user'); diff --git a/app/Models/Traits/ChargesFees.php b/app/Models/Traits/ChargesFees.php index 83f6cac4671a..c55a8932de18 100644 --- a/app/Models/Traits/ChargesFees.php +++ b/app/Models/Traits/ChargesFees.php @@ -51,7 +51,6 @@ trait ChargesFees } } - /* if ($account->gateway_fee_location == FEE_LOCATION_ITEM && $includeTax) { $preTaxFee = $fee; @@ -63,8 +62,22 @@ trait ChargesFees $fee += $preTaxFee * $settings->fee_tax_rate2 / 100; } } - */ return round($fee, 2); } + + 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; + } } diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 8d77b8b83e12..f32944e04055 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -12,6 +12,7 @@ use App\Models\Invoice; use App\Models\InvoiceItem; use App\Models\Product; use App\Models\Task; +use App\Models\GatewayType; use App\Services\PaymentService; use Auth; use DB; @@ -1017,9 +1018,14 @@ class InvoiceRepository extends BaseRepository $invoice->load('invoice_items'); } - // first remove fee if already set if ($location == FEE_LOCATION_ITEM) { - // todo + $data = $invoice->toArray(); + foreach ($data['invoice_items'] as $key => $item) { + if ($item['invoice_item_type_id'] == INVOICE_ITEM_TYPE_PENDING_GATEWAY_FEE) { + unset($data[$key]); + } + } + //dd($data['invoice_items']); } else { if ($invoice->$location != 0) { $data = $invoice->toArray(); @@ -1033,16 +1039,35 @@ class InvoiceRepository extends BaseRepository { $account = $invoice->account; $location = $account->gateway_fee_location; + $settings = $account->getGatewaySettings($gatewayTypeId); if (! $location) { return; } $this->clearGatewayFee($invoice); - - $fee = $invoice->calcGatewayFee($gatewayTypeId); $data = $invoice->toArray(); - $data[$location] = $fee; + + if ($location == FEE_LOCATION_ITEM) { + $fee = $invoice->calcGatewayFee($gatewayTypeId, false); + + $item = []; + $item['product_key'] = trans('texts.surcharge'); + $item['notes'] = trans('texts.online_payment_surcharge'); + $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; + } $this->save($data, $invoice); }