diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index ee489b53d7..fba9fcb378 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -236,7 +236,7 @@ class CreateSingleAccount extends Command $client->id_number = $this->getNextClientNumber($client); $settings = $client->settings; - $settings->currency_id = (string) rand(1, 79); + $settings->currency_id = "1"; $client->settings = $settings; $country = Country::all()->random(); diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index cde944a988..561f9480a1 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -287,4 +287,6 @@ class InvoiceSum { return $this->getTotalTaxes(); } + + } diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 924fb7098e..b84d409093 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -301,4 +301,5 @@ class InvoiceSumInclusive { return $this->getTotalTaxes(); } + } diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 8d909ef93b..82ff104c4e 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -34,18 +34,7 @@ use Turbo124\Beacon\Facades\LightLogs; class LoginController extends BaseController { - /* - |-------------------------------------------------------------------------- - | Login Controller - |-------------------------------------------------------------------------- - | - | This controller handles authenticating users for the application and - | redirecting them to your home screen. The controller uses a trait - | to conveniently provide its functionality to your applications. - | - */ - - /* + /** * @OA\Tag( * name="login", * description="Authentication", diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 72d1c1680d..85289b87c5 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -95,6 +95,7 @@ class InvoiceController extends Controller } $invoices->map(function ($invoice) { + $invoice->service()->removeUnpaidGatewayFees()->save(); $invoice->balance = Number::formatValue($invoice->balance, $invoice->client->currency()); $invoice->partial = Number::formatValue($invoice->partial, $invoice->client->currency()); diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index bdbb69a6f8..c74d6721e7 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -163,19 +163,29 @@ class PaymentController extends Controller $first_invoice = $invoices->first(); - $fee_totals = round($gateway->calcGatewayFee($invoice_totals, true), $first_invoice->client->currency()->precision); + $starting_invoice_amount = $first_invoice->amount; - if (!$first_invoice->uses_inclusive_taxes) { - $fee_tax = 0; - $fee_tax += round(($first_invoice->tax_rate1 / 100) * $fee_totals, $first_invoice->client->currency()->precision); - $fee_tax += round(($first_invoice->tax_rate2 / 100) * $fee_totals, $first_invoice->client->currency()->precision); - $fee_tax += round(($first_invoice->tax_rate3 / 100) * $fee_totals, $first_invoice->client->currency()->precision); + // $fee_totals = round($gateway->calcGatewayFee($invoice_totals, true), $first_invoice->client->currency()->precision); - $fee_totals += $fee_tax; - } + // if (!$first_invoice->uses_inclusive_taxes) { + // $fee_tax = 0; + // $fee_tax += round(($first_invoice->tax_rate1 / 100) * $fee_totals, $first_invoice->client->currency()->precision); + // $fee_tax += round(($first_invoice->tax_rate2 / 100) * $fee_totals, $first_invoice->client->currency()->precision); + // $fee_tax += round(($first_invoice->tax_rate3 / 100) * $fee_totals, $first_invoice->client->currency()->precision); + + // $fee_totals += $fee_tax; + // } $first_invoice->service()->addGatewayFee($gateway, $invoice_totals)->save(); + /** + * + * The best way to determine the exact gateway fee is to not calculate it in isolation (due to rounding) + * but to simply add it as a line item, and then subtract the starting and finishing amounts of + * the invoice. + */ + $fee_totals = $first_invoice->amount - $starting_invoice_amount; + $payment_hash = new PaymentHash; $payment_hash->hash = Str::random(128); $payment_hash->data = $payable_invoices; diff --git a/app/Http/Controllers/OpenAPI/AccountSchema.php b/app/Http/Controllers/OpenAPI/AccountSchema.php index 79400ecb6e..b78df617bf 100644 --- a/app/Http/Controllers/OpenAPI/AccountSchema.php +++ b/app/Http/Controllers/OpenAPI/AccountSchema.php @@ -1,5 +1,5 @@ fee_percent) { - $fee += $amount * $fees_and_limits->fee_percent / 100; + $fee += round(($amount * $fees_and_limits->fee_percent / 100),2); info("fee after adding fee percent = {$fee}"); } @@ -300,17 +300,17 @@ class CompanyGateway extends BaseModel /**/ if ($include_taxes) { if ($fees_and_limits->fee_tax_rate1) { - $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100; + $fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100),2); info("fee after adding fee tax 1 = {$fee}"); } if ($fees_and_limits->fee_tax_rate2) { - $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100; + $fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100),2); info("fee after adding fee tax 2 = {$fee}"); } if ($fees_and_limits->fee_tax_rate3) { - $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100; + $fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100),2); info("fee after adding fee tax 3 = {$fee}"); } } diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 595078dbbc..978838a981 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -104,19 +104,22 @@ class Gateway extends StaticModel { switch ($this->id) { case 1: - return ['methods' => [GatewayType::CREDIT_CARD], 'refund' => true, 'token_billing' => true ]; //Authorize.net + return [GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true]];//Authorize.net break; case 15: - return ['methods' => [GatewayType::PAYPAL], 'refund' => true, 'token_billing' => false ]; //Paypal + return [GatewayType::PAYPAL => ['refund' => true, 'token_billing' => false]]; //Paypal break; case 20: - return ['methods' => [GatewayType::CREDIT_CARD, GatewayType::BANK_TRANSFER, GatewayType::ALIPAY, GatewayType::APPLE_PAY], 'refund' => true, 'token_billing' => true ]; //Stripe + return [GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true], + GatewayType::BANK_TRANSFER => ['refund' => true, 'token_billing' => true], + GatewayType::ALIPAY => ['refund' => false, 'token_billing' => false], + GatewayType::APPLE_PAY => ['refund' => false, 'token_billing' => false]]; //Stripe break; case 39: - return ['methods' => [GatewayType::CREDIT_CARD], 'refund' => true, 'token_billing' => true ]; //Checkout + return [GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true]]; //Checkout break; default: - return ['methods' => [], 'refund' => false, 'token_billing' => false]; + return []; break; } } diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 18e3937aac..bc88092d30 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -44,6 +44,9 @@ class AddGatewayFee extends AbstractService { $gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount), $this->invoice->client->currency()->precision); + if((int)$gateway_fee == 0) + return; + $this->cleanPendingGatewayFees(); if ($gateway_fee > 0) { diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 51a98d1616..d814e382f7 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -212,6 +212,8 @@ class InvoiceService public function updateStatus() { + info("invoice balance = {$this->invoice->balance}"); + if((int)$this->invoice->balance == 0) $this->setStatus(Invoice::STATUS_PAID); @@ -232,7 +234,7 @@ class InvoiceService return $item; })->toArray(); - $this->invoice = $this->invoice->calc()->getInvoice(); + //$this->invoice = $this->invoice->calc()->getInvoice(); $this->deletePdf(); diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 5f253b09ff..764b465eea 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -67,12 +67,16 @@ class PaymentService } }); - $this->payment->ledger()->updatePaymentBalance($this->payment->amount); + $this->payment + ->ledger() + ->updatePaymentBalance($this->payment->amount); $client->service() ->updateBalance($this->payment->amount) ->updatePaidToDate($this->payment->amount * -1) ->save(); + + return $this; } public function refundPayment(array $data) :?Payment diff --git a/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php b/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php index c870ead515..c5b029f921 100644 --- a/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php +++ b/database/migrations/2020_09_27_215800_update_gateway_table_visible_column.php @@ -31,7 +31,6 @@ class UpdateGatewayTableVisibleColumn extends Migration }); - Schema::table('expenses', function ($t){ $t->renameColumn('invoice_category_id', 'category_id'); });