PluginProbe
Moloni / trunk
Moloni vtrunk
5.0.10 5.0.09 5.0.08 5.0.07 5.0.06 trunk 0.4.0.00 0.4.8.1 3.0.10 3.0.11 3.0.35 3.0.36 3.0.37 3.0.38 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.0.46 3.0.47 3.0.48 3.0.49 3.0.50 All 98 releases
moloni / src / Tools.php

Tools.php in Moloni trunk, at src/Tools.php

295 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Moloni;
4
5 use Moloni\Enums\SaftType;
6 use Moloni\Enums\TaxType;
7 use Moloni\Exceptions\APIException;
8
9 /**
10 * Multiple tools for handling recurring tasks
11 * Class Tools
12 * @package Moloni
13 */
14 class Tools
15 {
16 /**
17 * Array with all european country codes
18 *
19 * @var string[]
20 */
21 public static $europeanCountryCodes = [
22 'AT' => 'Austria',
23 'BE' => 'Belgium',
24 'BG' => 'Bulgaria',
25 'HR' => 'Croatia',
26 'CY' => 'Cyprus',
27 'CZ' => 'Czech Republic',
28 'DK' => 'Denmark',
29 'EE' => 'Estonia',
30 'FI' => 'Finland',
31 'FR' => 'France',
32 'DE' => 'Germany',
33 'GR' => 'Greece',
34 'HU' => 'Hungary',
35 'IE' => 'Ireland',
36 'IT' => 'Italy',
37 'LV' => 'Latvia',
38 'LT' => 'Lithuania',
39 'LU' => 'Luxembourg',
40 'MT' => 'Malta',
41 'NL' => 'Netherlands',
42 'PL' => 'Poland',
43 'PT' => 'Portugal',
44 'RO' => 'Romania',
45 'SK' => 'Slovakia',
46 'SI' => 'Slovenia',
47 'ES' => 'Spain',
48 'SE' => 'Sweden',
49 ];
50
51 /**
52 * @param string $string
53 * @param int $productId
54 * @param int $variationId
55 * @return string
56 */
57 public static function createReferenceFromString($string, $productId = 0, $variationId = 0)
58 {
59 $reference = '';
60
61 $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);
62 $name = explode(' ', $string);
63
64 if (!defined('USE_NAME_FOR_MOLONI_REFERENCE') || USE_NAME_FOR_MOLONI_REFERENCE == 1) {
65 if ((int)$productId > 0) {
66 $reference .= '_' . $productId;
67 }
68
69 if ((int)$variationId > 0) {
70 $reference .= '_' . $variationId;
71 }
72
73 foreach ($name as $word) {
74 $reference .= '_' . mb_substr($word, 0, 3);
75 }
76 } else {
77 if ((int)$productId > 0) {
78 $reference .= $productId;
79 }
80
81 if ((int)$variationId > 0) {
82 $reference .= '_' . $variationId;
83 }
84 }
85
86 return mb_substr($reference, 0, 30);
87 }
88
89 /**
90 * Get full tax Object given a tax rate
91 *
92 * @param float $taxRate Tax rate value
93 * @param string $countryCode Country code string
94 *
95 * @return array
96 *
97 * @throws APIException
98 */
99 public static function getTaxFromRate($taxRate, $countryCode = 'PT')
100 {
101 $countryCode = strtoupper($countryCode);
102 $taxesList = Curl::simple('taxes/getAll', []);
103 $moloniTax = false;
104 $defaultTax = 0;
105
106 if (!empty($taxesList) && is_array($taxesList)) {
107 foreach ($taxesList as $tax) {
108 if ($tax['fiscal_zone'] !== $countryCode) {
109 continue;
110 }
111
112 if ((int)$tax['active_by_default'] === 1) {
113 $defaultTax = $tax;
114 }
115
116 if ((int)$tax['saft_type'] !== SaftType::IVA) {
117 continue;
118 }
119
120 if ((int)$tax['type'] !== TaxType::PERCENTAGE) {
121 continue;
122 }
123
124 if ((float)$tax['value'] === (float)$taxRate) {
125 $moloniTax = $tax;
126 break;
127 }
128 }
129 }
130
131 if (!$moloniTax && (float)$taxRate > 0) {
132 $newTax = self::createTax($taxRate, $countryCode);
133
134 if (isset($newTax['tax_id'])) {
135 $moloniTax = $newTax;
136
137 //The value here will always be this, we save a request to get the inserted tax
138 $moloniTax['saft_type'] = SaftType::IVA;
139 }
140 }
141
142 if (!$moloniTax) {
143 $moloniTax = $defaultTax;
144 }
145
146 return $moloniTax;
147 }
148
149 /**
150 * Creates a tax in Moloni
151 *
152 * @param float $taxRate Tax rate value
153 * @param string $countryCode Country code string
154 *
155 * @return array|bool
156 *
157 * @throws APIException
158 */
159 public static function createTax($taxRate, $countryCode = 'PT')
160 {
161 $values = [];
162
163 $values['name'] = 'VAT ' . $countryCode;
164 $values['value'] = $taxRate;
165 $values['type'] = TaxType::PERCENTAGE;
166 $values['saft_type'] = SaftType::IVA;
167 $values['vat_type'] = "OUT";
168 $values['stamp_tax'] = "0";
169 $values['exemption_reason'] = EXEMPTION_REASON;
170 $values['fiscal_zone'] = $countryCode;
171 $values['active_by_default'] = "0";
172
173 return Curl::simple('taxes/insert', $values);
174 }
175
176 /**
177 * @param $countryCode
178 * @return string
179 *
180 * @throws APIException
181 */
182 public static function getCountryIdFromCode($countryCode)
183 {
184 $countriesList = Curl::simple('countries/getAll', []);
185
186 if (!empty($countriesList) && is_array($countriesList)) {
187 foreach ($countriesList as $country) {
188 if (strtoupper($country['iso_3166_1']) === strtoupper($countryCode)) {
189 return $country['country_id'];
190 }
191 }
192 }
193
194 return '1';
195 }
196
197 /**
198 * @param int $from
199 * @param int $to
200 * @return float
201 *
202 * @throws APIException
203 */
204 public static function getCurrencyExchangeRate($from, $to)
205 {
206 $currenciesList = Curl::simple('currencyExchange/getAll', []);
207
208 if (!empty($currenciesList) && is_array($currenciesList)) {
209 foreach ($currenciesList as $currency) {
210 if ((int)$currency['from'] === $from && (int)$currency['to'] === $to) {
211 return (float)$currency['value'];
212 }
213 }
214 }
215
216 return 1;
217 }
218
219 /**
220 * @param string $currencyCode
221 * @return int
222 *
223 * @throws APIException
224 */
225 public static function getCurrencyIdFromCode(string $currencyCode): int
226 {
227 $currenciesList = Curl::simple('currencies/getAll', []);
228
229 if (!empty($currenciesList) && is_array($currenciesList)) {
230 foreach ($currenciesList as $currency) {
231 if ($currency['iso4217'] === mb_strtoupper($currencyCode)) {
232 return $currency['currency_id'];
233 }
234 }
235 }
236
237 return 1;
238 }
239
240 /**
241 * @param $input
242 * @return string
243 */
244 public static function zipCheck($input)
245 {
246 $zipCode = trim(str_replace(' ', '', $input));
247 $zipCode = preg_replace('/[^0-9]/', '', $zipCode);
248 if (strlen($zipCode) == 7) {
249 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . $zipCode[5] . $zipCode[6];
250 }
251 if (strlen($zipCode) == 6) {
252 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . $zipCode[5] . '0';
253 }
254 if (strlen($zipCode) == 5) {
255 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . '00';
256 }
257 if (strlen($zipCode) == 4) {
258 $zipCode = $zipCode . '-' . '000';
259 }
260 if (strlen($zipCode) == 3) {
261 $zipCode = $zipCode . '0-' . '000';
262 }
263 if (strlen($zipCode) == 2) {
264 $zipCode = $zipCode . '00-' . '000';
265 }
266 if (strlen($zipCode) == 1) {
267 $zipCode = $zipCode . '000-' . '000';
268 }
269 if (strlen($zipCode) == 0) {
270 $zipCode = '1000-100';
271 }
272 if (self::finalCheck($zipCode)) {
273 return $zipCode;
274 }
275
276 return '1000-100';
277 }
278
279 /**
280 * Validate a Zip Code format
281 * @param string $zipCode
282 * @return bool
283 */
284 private static function finalCheck($zipCode)
285 {
286 $regexp = "/[0-9]{4}\-[0-9]{3}/";
287 if (preg_match($regexp, $zipCode)) {
288 return (true);
289 }
290
291 return (false);
292 }
293
294 }
295