| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tax Validation Service |
| 4 |
* |
| 5 |
* Validates tax configuration for single and multiple taxes |
| 6 |
* |
| 7 |
* @package Yatra\Services |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace Yatra\Services; |
| 14 |
|
| 15 |
class TaxValidationService |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Validate single tax configuration |
| 19 |
* |
| 20 |
* @param array $taxData Tax data |
| 21 |
* @return array Validation result |
| 22 |
*/ |
| 23 |
public static function validateSingleTax(array $taxData): array |
| 24 |
{ |
| 25 |
$errors = []; |
| 26 |
|
| 27 |
// Validate tax name |
| 28 |
if (empty($taxData['tax_name'])) { |
| 29 |
$errors[] = __('Tax name is required', 'yatra'); |
| 30 |
} elseif (strlen($taxData['tax_name']) > 50) { |
| 31 |
$errors[] = __('Tax name cannot exceed 50 characters', 'yatra'); |
| 32 |
} |
| 33 |
|
| 34 |
// Validate tax rate |
| 35 |
if (!isset($taxData['tax_rate'])) { |
| 36 |
$errors[] = __('Tax rate is required', 'yatra'); |
| 37 |
} elseif (!is_numeric($taxData['tax_rate'])) { |
| 38 |
$errors[] = __('Tax rate must be a number', 'yatra'); |
| 39 |
} elseif ($taxData['tax_rate'] < 0 || $taxData['tax_rate'] > 100) { |
| 40 |
$errors[] = __('Tax rate must be between 0 and 100', 'yatra'); |
| 41 |
} |
| 42 |
|
| 43 |
return [ |
| 44 |
'valid' => empty($errors), |
| 45 |
'errors' => $errors, |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Validate multiple taxes configuration |
| 51 |
* |
| 52 |
* @param array $taxes Array of tax configurations |
| 53 |
* @return array Validation result |
| 54 |
*/ |
| 55 |
public static function validateMultipleTaxes(array $taxes): array |
| 56 |
{ |
| 57 |
$errors = []; |
| 58 |
|
| 59 |
if (empty($taxes)) { |
| 60 |
$errors[] = __('At least one tax must be configured', 'yatra'); |
| 61 |
return [ |
| 62 |
'valid' => false, |
| 63 |
'errors' => $errors, |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
$total_rate = 0; |
| 68 |
|
| 69 |
foreach ($taxes as $index => $tax) { |
| 70 |
$tax_errors = []; |
| 71 |
|
| 72 |
// Validate tax name |
| 73 |
if (empty($tax['name'])) { |
| 74 |
$tax_errors[] = __('Tax name is required', 'yatra'); |
| 75 |
} elseif (strlen($tax['name']) > 50) { |
| 76 |
$tax_errors[] = __('Tax name cannot exceed 50 characters', 'yatra'); |
| 77 |
} |
| 78 |
|
| 79 |
// Validate tax rate |
| 80 |
if (!isset($tax['rate'])) { |
| 81 |
$tax_errors[] = __('Tax rate is required', 'yatra'); |
| 82 |
} elseif (!is_numeric($tax['rate'])) { |
| 83 |
$tax_errors[] = __('Tax rate must be a number', 'yatra'); |
| 84 |
} elseif ($tax['rate'] < 0 || $tax['rate'] > 100) { |
| 85 |
$tax_errors[] = __('Tax rate must be between 0 and 100', 'yatra'); |
| 86 |
} else { |
| 87 |
$total_rate += (float) $tax['rate']; |
| 88 |
} |
| 89 |
|
| 90 |
if (!empty($tax_errors)) { |
| 91 |
$errors[] = sprintf( |
| 92 |
/* translators: 1: tax index number, 2: comma-separated list of error messages. */ |
| 93 |
__('Tax %1$d: %2$s', 'yatra'), |
| 94 |
$index + 1, |
| 95 |
implode(', ', $tax_errors) |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Check total tax rate |
| 101 |
if ($total_rate > 100) { |
| 102 |
$errors[] = __('Total tax rate cannot exceed 100%', 'yatra'); |
| 103 |
} |
| 104 |
|
| 105 |
return [ |
| 106 |
'valid' => empty($errors), |
| 107 |
'errors' => $errors, |
| 108 |
'total_rate' => $total_rate, |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get default tax configuration examples |
| 114 |
* |
| 115 |
* @return array Example tax configurations |
| 116 |
*/ |
| 117 |
public static function getDefaultTaxExamples(): array |
| 118 |
{ |
| 119 |
return [ |
| 120 |
'single_tax' => [ |
| 121 |
'tax_name' => 'VAT', |
| 122 |
'tax_rate' => 20, |
| 123 |
], |
| 124 |
'multiple_taxes' => [ |
| 125 |
[ |
| 126 |
'name' => 'VAT', |
| 127 |
'rate' => 15, |
| 128 |
], |
| 129 |
[ |
| 130 |
'name' => 'Service Fee', |
| 131 |
'rate' => 5, |
| 132 |
], |
| 133 |
[ |
| 134 |
'name' => 'Tourism Tax', |
| 135 |
'rate' => 2, |
| 136 |
], |
| 137 |
], |
| 138 |
'country_specific' => [ |
| 139 |
'US' => [ |
| 140 |
'name' => 'Sales Tax', |
| 141 |
'rate' => 8.25, |
| 142 |
], |
| 143 |
'UK' => [ |
| 144 |
'name' => 'VAT', |
| 145 |
'rate' => 20, |
| 146 |
], |
| 147 |
'EU' => [ |
| 148 |
'name' => 'VAT', |
| 149 |
'rate' => 21, |
| 150 |
], |
| 151 |
], |
| 152 |
]; |
| 153 |
} |
| 154 |
} |
| 155 |
|