2020-08-25 15:06:38 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-25 15:06:38 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-08-25 15:06:38 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-08-25 15:06:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\DataMapper\InvoiceItem;
|
|
|
|
use App\Models\CompanyGateway;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
2021-12-10 11:50:46 +01:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2024-08-13 06:30:44 +02:00
|
|
|
use App\Models\Product;
|
2020-08-25 15:06:38 +02:00
|
|
|
|
|
|
|
class AddGatewayFee extends AbstractService
|
|
|
|
{
|
2023-04-26 14:31:53 +02:00
|
|
|
public function __construct(private CompanyGateway $company_gateway, private int $gateway_type_id, public Invoice $invoice, private float $amount)
|
2020-08-25 15:06:38 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2021-01-04 14:07:50 +01:00
|
|
|
$gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount, $this->gateway_type_id, $this->invoice->uses_inclusive_taxes), $this->invoice->client->currency()->precision);
|
2020-08-25 15:06:38 +02:00
|
|
|
|
2023-10-11 05:18:56 +02:00
|
|
|
if (! $gateway_fee || $gateway_fee == 0) {
|
2020-10-12 01:27:38 +02:00
|
|
|
return $this->invoice;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-10-10 12:57:28 +02:00
|
|
|
|
2024-08-13 06:30:44 +02:00
|
|
|
// Removes existing stale gateway fees
|
|
|
|
$this->cleanPendingGatewayFees();
|
|
|
|
|
2021-01-17 10:35:01 +01:00
|
|
|
// If a gateway fee is > 0 insert the line item
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($gateway_fee > 0) {
|
2020-08-25 15:06:38 +02:00
|
|
|
return $this->processGatewayFee($gateway_fee);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have reached this far, then we are apply a gateway discount
|
2020-08-25 15:06:38 +02:00
|
|
|
return $this->processGatewayDiscount($gateway_fee);
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
private function cleanPendingGatewayFees()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice_items = (array) $this->invoice->line_items;
|
2020-08-26 00:10:49 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$invoice_items = collect($invoice_items)->filter(function ($item) {
|
2020-08-31 04:00:43 +02:00
|
|
|
return $item->type_id != '3';
|
2024-09-29 08:52:34 +02:00
|
|
|
})->toArray();
|
2020-08-26 00:10:49 +02:00
|
|
|
|
|
|
|
$this->invoice->line_items = $invoice_items;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:06:38 +02:00
|
|
|
private function processGatewayFee($gateway_fee)
|
|
|
|
{
|
2022-03-24 03:11:09 +01:00
|
|
|
$balance = $this->invoice->balance;
|
|
|
|
|
2021-12-10 11:50:46 +01:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->invoice->company->settings));
|
2021-12-14 12:02:53 +01:00
|
|
|
App::setLocale($this->invoice->client->locale());
|
2021-12-10 11:50:46 +01:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$invoice_item = new InvoiceItem();
|
2020-08-31 04:00:43 +02:00
|
|
|
$invoice_item->type_id = '3';
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->product_key = ctrans('texts.surcharge');
|
|
|
|
$invoice_item->notes = ctrans('texts.online_payment_surcharge');
|
|
|
|
$invoice_item->quantity = 1;
|
|
|
|
$invoice_item->cost = $gateway_fee;
|
|
|
|
|
2020-10-12 11:38:55 +02:00
|
|
|
if ($fees_and_limits = $this->company_gateway->getFeesAndLimits($this->gateway_type_id)) {
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->tax_rate1 = $fees_and_limits->fee_tax_rate1;
|
2023-05-10 04:21:10 +02:00
|
|
|
$invoice_item->tax_name1 = $fees_and_limits->fee_tax_name1;
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->tax_rate2 = $fees_and_limits->fee_tax_rate2;
|
2023-05-10 04:21:10 +02:00
|
|
|
$invoice_item->tax_name2 = $fees_and_limits->fee_tax_name2;
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3;
|
2023-05-10 04:21:10 +02:00
|
|
|
$invoice_item->tax_name3 = $fees_and_limits->fee_tax_name3;
|
2024-08-14 00:34:57 +02:00
|
|
|
$invoice_item->tax_id = (string)Product::PRODUCT_TYPE_OVERRIDE_TAX;
|
2020-08-26 00:10:49 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice_items = (array) $this->invoice->line_items;
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_items[] = $invoice_item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $invoice_items;
|
|
|
|
|
2020-08-30 12:47:32 +02:00
|
|
|
/**Refresh Invoice values*/
|
2020-08-26 00:10:49 +02:00
|
|
|
$this->invoice = $this->invoice->calc()->getInvoice();
|
|
|
|
|
2022-03-24 03:11:09 +01:00
|
|
|
$new_balance = $this->invoice->balance;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (floatval($new_balance) - floatval($balance) != 0) {
|
2022-03-24 03:11:09 +01:00
|
|
|
$adjustment = $new_balance - $balance;
|
|
|
|
|
2023-11-07 11:58:52 +01:00
|
|
|
$this->invoice
|
|
|
|
->ledger()
|
|
|
|
->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee');
|
2023-11-07 04:54:44 +01:00
|
|
|
|
2023-11-07 11:58:52 +01:00
|
|
|
$this->invoice->client->service()->calculateBalance();
|
2023-11-07 04:54:44 +01:00
|
|
|
|
2022-03-24 03:11:09 +01:00
|
|
|
}
|
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
return $this->invoice;
|
2020-08-25 15:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function processGatewayDiscount($gateway_fee)
|
|
|
|
{
|
2022-03-24 03:11:09 +01:00
|
|
|
$balance = $this->invoice->balance;
|
|
|
|
|
2021-12-10 11:50:46 +01:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->invoice->company->settings));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$invoice_item = new InvoiceItem();
|
2020-08-31 04:00:43 +02:00
|
|
|
$invoice_item->type_id = '3';
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->product_key = ctrans('texts.discount');
|
|
|
|
$invoice_item->notes = ctrans('texts.online_payment_discount');
|
|
|
|
$invoice_item->quantity = 1;
|
|
|
|
$invoice_item->cost = $gateway_fee;
|
|
|
|
|
2021-12-02 21:01:13 +01:00
|
|
|
if ($fees_and_limits = $this->company_gateway->getFeesAndLimits($this->gateway_type_id)) {
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_item->tax_rate1 = $fees_and_limits->fee_tax_rate1;
|
|
|
|
$invoice_item->tax_rate2 = $fees_and_limits->fee_tax_rate2;
|
|
|
|
$invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoice_items = (array) $this->invoice->line_items;
|
2020-08-26 00:10:49 +02:00
|
|
|
$invoice_items[] = $invoice_item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $invoice_items;
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->calc()->getInvoice();
|
|
|
|
|
2022-03-24 03:11:09 +01:00
|
|
|
$new_balance = $this->invoice->balance;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (floatval($new_balance) - floatval($balance) != 0) {
|
2022-03-24 03:11:09 +01:00
|
|
|
$adjustment = $new_balance - $balance;
|
|
|
|
|
2023-11-07 11:58:52 +01:00
|
|
|
$this->invoice
|
|
|
|
->ledger()
|
|
|
|
->updateInvoiceBalance($adjustment * -1, 'Adjustment for adding gateway DISCOUNT');
|
2023-11-07 04:54:44 +01:00
|
|
|
|
2023-11-07 11:58:52 +01:00
|
|
|
$this->invoice->client->service()->calculateBalance();
|
2022-03-24 03:11:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
return $this->invoice;
|
2020-08-25 15:06:38 +02:00
|
|
|
}
|
|
|
|
}
|