HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/hi.naniguide.com/app/Services/Billing/TaxResolver.php
<?php

namespace App\Services\Billing;

use App\Models\Customer;
use App\Models\Setting;

class TaxResolver
{
    public function resolveForCustomer(Customer $customer): ?array
    {
        $address = is_array($customer->billing_address) ? $customer->billing_address : [];

        $country = $this->normalizeCountry((string) ($address['country'] ?? $customer->country ?? ''));
        $state = $this->normalizeState((string) ($address['state'] ?? $customer->state ?? ''));

        if ($country === '') {
            return null;
        }

        if ($country === 'US') {
            $rates = Setting::get('us_sales_tax_rates', []);
            if (!is_array($rates)) {
                $rates = [];
            }

            $rate = null;
            if ($state !== '') {
                $rate = $rates[$state] ?? $rates[strtoupper($state)] ?? null;
            }

            if (is_numeric($rate)) {
                $percentage = (float) $rate;
                if ($percentage > 0) {
                    return [
                        'type' => 'sales_tax',
                        'percentage' => $percentage,
                        'country' => 'US',
                        'state' => $state !== '' ? $state : null,
                        'display_name' => 'Sales Tax',
                        'inclusive' => false,
                    ];
                }
            }

            return null;
        }

        $vatEnabled = (bool) Setting::get('vat_enabled', false);
        $vatRate = Setting::get('vat_rate', null);

        if (!$vatEnabled || !is_numeric($vatRate)) {
            return null;
        }

        $percentage = (float) $vatRate;
        if ($percentage <= 0) {
            return null;
        }

        return [
            'type' => 'vat',
            'percentage' => $percentage,
            'country' => $country,
            'state' => null,
            'display_name' => 'VAT',
            'inclusive' => false,
        ];
    }

    private function normalizeCountry(string $country): string
    {
        $country = strtoupper(trim($country));
        if ($country === '') {
            return '';
        }

        if (in_array($country, ['US', 'USA', 'UNITED STATES', 'UNITED STATES OF AMERICA'], true)) {
            return 'US';
        }

        if (strlen($country) === 2) {
            return $country;
        }

        return '';
    }

    private function normalizeState(string $state): string
    {
        $state = strtoupper(trim($state));
        if ($state === '') {
            return '';
        }

        return $state;
    }
}