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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
|
|
|
use App\DataMapper\InvoiceItem;
|
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\CompanyGateway;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Services\Client\ClientService;
|
|
|
|
use App\Services\Payment\PaymentService;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
|
|
|
|
class AddGatewayFee extends AbstractService
|
|
|
|
{
|
|
|
|
private $company_gateway;
|
|
|
|
|
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
private $amount;
|
|
|
|
|
2020-10-12 11:38:55 +02:00
|
|
|
private $gateway_type_id;
|
|
|
|
|
|
|
|
public function __construct(CompanyGateway $company_gateway, int $gateway_type_id, Invoice $invoice, float $amount)
|
2020-08-25 15:06:38 +02:00
|
|
|
{
|
|
|
|
$this->company_gateway = $company_gateway;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-25 15:06:38 +02:00
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
|
|
|
$this->amount = $amount;
|
2020-10-12 11:38:55 +02:00
|
|
|
|
|
|
|
$this->gateway_type_id = $gateway_type_id;
|
2020-08-25 15:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2020-10-12 11:38:55 +02:00
|
|
|
$gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount, $this->gateway_type_id), $this->invoice->client->currency()->precision);
|
2020-08-25 15:06:38 +02:00
|
|
|
|
2020-10-10 12:57:28 +02:00
|
|
|
if((int)$gateway_fee == 0)
|
2020-10-12 01:27:38 +02:00
|
|
|
return $this->invoice;
|
2020-10-10 12:57:28 +02:00
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
$this->cleanPendingGatewayFees();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($gateway_fee > 0) {
|
2020-08-25 15:06:38 +02:00
|
|
|
return $this->processGatewayFee($gateway_fee);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-25 15:06:38 +02:00
|
|
|
|
|
|
|
return $this->processGatewayDiscount($gateway_fee);
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
private function cleanPendingGatewayFees()
|
|
|
|
{
|
|
|
|
$invoice_items = $this->invoice->line_items;
|
|
|
|
|
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';
|
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)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
$invoice_item->tax_rate2 = $fees_and_limits->fee_tax_rate2;
|
|
|
|
$invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice_items = $this->invoice->line_items;
|
|
|
|
$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();
|
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
/*Update client balance*/ // don't increment until we have process the payment!
|
|
|
|
//$this->invoice->client->service()->updateBalance($gateway_fee)->save();
|
|
|
|
//$this->invoice->ledger()->updateInvoiceBalance($gateway_fee, $notes = 'Gateway fee adjustment');
|
2020-08-30 12:47:32 +02: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)
|
|
|
|
{
|
2020-08-26 00:10:49 +02: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;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($fees_and_limits = $this->company_gateway->getFeesAndLimits()) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice_items = $this->invoice->line_items;
|
|
|
|
$invoice_items[] = $invoice_item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $invoice_items;
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->calc()->getInvoice();
|
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
// $this->invoice->client->service()->updateBalance($gateway_fee)->save();
|
2020-08-30 12:47:32 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
// $this->invoice->ledger()->updateInvoiceBalance($gateway_fee, $notes = 'Discount fee adjustment');
|
2020-08-30 12:47:32 +02:00
|
|
|
|
2020-08-26 00:10:49 +02:00
|
|
|
return $this->invoice;
|
2020-08-25 15:06:38 +02:00
|
|
|
}
|
|
|
|
}
|