PluginProbe
Moloni / 3.0.51
Moloni v3.0.51
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 3.0.51, at src/Tools.php

218 lines 5.9 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 /**
6 * Multiple tools for handling recurring tasks
7 * Class Tools
8 * @package Moloni
9 */
10 class Tools
11 {
12
13 /**
14 * @param string $string
15 * @param int $productId
16 * @param int $variationId
17 * @return string
18 */
19 public static function createReferenceFromString($string, $productId = 0, $variationId = 0)
20 {
21 $reference = '';
22
23 $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string);
24 $name = explode(' ', $string);
25
26 if (!defined('USE_NAME_FOR_MOLONI_REFERENCE') || USE_NAME_FOR_MOLONI_REFERENCE == 1) {
27 if ((int)$productId > 0) {
28 $reference .= '_' . $productId;
29 }
30
31 if ((int)$variationId > 0) {
32 $reference .= '_' . $variationId;
33 }
34
35 foreach ($name as $word) {
36 $reference .= '_' . mb_substr($word, 0, 3);
37 }
38 } else {
39 if ((int)$productId > 0) {
40 $reference .= $productId;
41 }
42
43 if ((int)$variationId > 0) {
44 $reference .= '_' . $variationId;
45 }
46 }
47
48 return mb_substr($reference, 0, 30);
49 }
50
51 /**
52 * Get a tax id given a tax rate
53 * As a fallback if we don't find a tax with the same rate we return the company default
54 * @param $taxRate
55 * @return mixed
56 * @throws Error
57 */
58 public static function getTaxIdFromRate($taxRate)
59 {
60 $defaultTax = 0;
61 $taxesList = Curl::simple('taxes/getAll', []);
62 if (!empty($taxesList) && is_array($taxesList)) {
63 foreach ($taxesList as $tax) {
64 if ((int)$tax['active_by_default'] === 1) {
65 $defaultTax = $tax['tax_id'];
66 }
67
68 if ((float)$tax['value'] === (float)$taxRate) {
69 return $tax['tax_id'];
70 }
71 }
72 }
73
74 return $defaultTax;
75 }
76
77 /**
78 * Get full tax Object given a tax rate
79 * As a fallback if we don't find a tax with the same rate we return the company default
80 * @param $taxRate
81 * @return mixed
82 * @throws Error
83 */
84 public static function getTaxFromRate($taxRate)
85 {
86 $defaultTax = 0;
87 $taxesList = Curl::simple('taxes/getAll', []);
88 if (!empty($taxesList) && is_array($taxesList)) {
89 foreach ($taxesList as $tax) {
90 if ((int)$tax['active_by_default'] === 1) {
91 $defaultTax = $tax;
92 }
93
94 if ((float)$tax['value'] === (float)$taxRate) {
95 return $tax;
96 }
97 }
98 }
99
100 return $defaultTax;
101 }
102
103 /**
104 * @param $countryCode
105 * @return string
106 * @throws Error
107 */
108 public static function getCountryIdFromCode($countryCode)
109 {
110 $countriesList = Curl::simple('countries/getAll', []);
111 if (!empty($countriesList) && is_array($countriesList)) {
112 foreach ($countriesList as $country) {
113 if (strtoupper($country['iso_3166_1']) === strtoupper($countryCode)) {
114 return $country['country_id'];
115 break;
116 }
117 }
118 }
119
120 return '1';
121 }
122
123 /**
124 * @param int $from
125 * @param int $to
126 * @return float
127 * @throws Error
128 */
129 public static function getCurrencyExchangeRate($from, $to)
130 {
131 $currenciesList = Curl::simple('currencyExchange/getAll', []);
132 if (!empty($currenciesList) && is_array($currenciesList)) {
133 foreach ($currenciesList as $currency) {
134 if ((int)$currency['from'] === $from && (int)$currency['to'] === $to) {
135 return (float)$currency['value'];
136 }
137 }
138 }
139
140 return 1;
141 }
142
143 /**
144 * @param string $currencyCode
145 * @return int
146 * @throws Error
147 */
148 public static function getCurrencyIdFromCode($currencyCode)
149 {
150 $currenciesList = Curl::simple('currencies/getAll', []);
151 if (!empty($currenciesList) && is_array($currenciesList)) {
152 foreach ($currenciesList as $currency) {
153 if ($currency['iso4217'] === mb_strtoupper($currencyCode)) {
154 return $currency['currency_id'];
155 }
156 }
157 }
158
159 return 1;
160 }
161
162 /**
163 * @param $input
164 * @return string
165 */
166 public static function zipCheck($input)
167 {
168 $zipCode = trim(str_replace(' ', '', $input));
169 $zipCode = preg_replace('/[^0-9]/', '', $zipCode);
170 if (strlen($zipCode) == 7) {
171 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . $zipCode[5] . $zipCode[6];
172 }
173 if (strlen($zipCode) == 6) {
174 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . $zipCode[5] . '0';
175 }
176 if (strlen($zipCode) == 5) {
177 $zipCode = $zipCode[0] . $zipCode[1] . $zipCode[2] . $zipCode[3] . '-' . $zipCode[4] . '00';
178 }
179 if (strlen($zipCode) == 4) {
180 $zipCode = $zipCode . '-' . '000';
181 }
182 if (strlen($zipCode) == 3) {
183 $zipCode = $zipCode . '0-' . '000';
184 }
185 if (strlen($zipCode) == 2) {
186 $zipCode = $zipCode . '00-' . '000';
187 }
188 if (strlen($zipCode) == 1) {
189 $zipCode = $zipCode . '000-' . '000';
190 }
191 if (strlen($zipCode) == 0) {
192 $zipCode = '1000-100';
193 }
194 if (self::finalCheck($zipCode)) {
195 return $zipCode;
196 }
197
198 return '1000-100';
199 }
200
201 /**
202 * Validate a Zip Code format
203 * @param string $zipCode
204 * @return bool
205 */
206 private static function finalCheck($zipCode)
207 {
208 $regexp = "/[0-9]{4}\-[0-9]{3}/";
209 if (preg_match($regexp, $zipCode)) {
210 return (true);
211 }
212
213 return (false);
214 }
215
216
217 }
218