silvercommerce /
tax-admin
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace SilverCommerce\TaxAdmin\Traits; |
||||||
| 4 | |||||||
| 5 | use LogicException; |
||||||
| 6 | use NumberFormatter; |
||||||
| 7 | |||||||
| 8 | trait Taxable |
||||||
| 9 | { |
||||||
| 10 | /** |
||||||
| 11 | * Filter the results returned by an extension |
||||||
| 12 | * |
||||||
| 13 | * @param mixed $results Possible results |
||||||
| 14 | * |
||||||
| 15 | * @return mixed |
||||||
| 16 | */ |
||||||
| 17 | public function filterTaxableExtensionResults($results) |
||||||
| 18 | { |
||||||
| 19 | if (!empty($results) && is_array($results)) { |
||||||
| 20 | $results = array_filter( |
||||||
| 21 | $results, |
||||||
| 22 | function ($v) { |
||||||
| 23 | return !is_null($v); |
||||||
| 24 | } |
||||||
| 25 | ); |
||||||
| 26 | if (is_array($results) && count($results) > 0) { |
||||||
| 27 | return $results[0]; |
||||||
| 28 | } |
||||||
| 29 | } |
||||||
| 30 | |||||||
| 31 | return; |
||||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | /** |
||||||
| 35 | * Get currency formatter |
||||||
| 36 | * |
||||||
| 37 | * @return NumberFormatter |
||||||
| 38 | */ |
||||||
| 39 | public function getFormatter() |
||||||
| 40 | { |
||||||
| 41 | if (!$this->hasMethod('getLocale')) { |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 42 | throw new LogicException('Object must implement \SilverCommerce\TaxAdmin\Interfaces\TaxableProvider'); |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | return NumberFormatter::create( |
||||||
| 46 | $this->getLocale(), |
||||||
|
0 ignored issues
–
show
It seems like
getLocale() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 47 | NumberFormatter::CURRENCY |
||||||
| 48 | ); |
||||||
| 49 | } |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * Get ISO 4217 currency code from curent locale |
||||||
| 53 | * |
||||||
| 54 | * @return string |
||||||
| 55 | */ |
||||||
| 56 | public function getCurrency() |
||||||
| 57 | { |
||||||
| 58 | return $this |
||||||
| 59 | ->getFormatter() |
||||||
| 60 | ->getTextAttribute(NumberFormatter::CURRENCY_CODE); |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * Get a currency symbol from the current site local |
||||||
| 65 | * |
||||||
| 66 | * @return string |
||||||
| 67 | */ |
||||||
| 68 | public function getCurrencySymbol() |
||||||
| 69 | { |
||||||
| 70 | return $this |
||||||
| 71 | ->getFormatter() |
||||||
| 72 | ->getSymbol(NumberFormatter::CURRENCY_SYMBOL); |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Shortcut to get the price of this product without tax |
||||||
| 77 | * |
||||||
| 78 | * @return float |
||||||
| 79 | */ |
||||||
| 80 | public function getNoTaxPrice() |
||||||
| 81 | { |
||||||
| 82 | if (!$this->hasMethod('getBasePrice')) { |
||||||
| 83 | throw new LogicException('Object must implement \SilverCommerce\TaxAdmin\Interfaces\TaxableProvider'); |
||||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | $price = $this->getBasePrice(); |
||||||
|
0 ignored issues
–
show
It seems like
getBasePrice() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 87 | |||||||
| 88 | if ($this->hasMethod('extend')) { |
||||||
| 89 | $result = $this->filterTaxableExtensionResults( |
||||||
| 90 | $this->extend("updateNoTaxPrice", $price) |
||||||
|
0 ignored issues
–
show
It seems like
extend() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 91 | ); |
||||||
| 92 | |||||||
| 93 | if (!empty($result)) { |
||||||
| 94 | return $result; |
||||||
| 95 | } |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | return $price; |
||||||
| 99 | } |
||||||
| 100 | |||||||
| 101 | /** |
||||||
| 102 | * Get the percentage tax rate assotiated with this field |
||||||
| 103 | * |
||||||
| 104 | * @return float |
||||||
| 105 | */ |
||||||
| 106 | public function getTaxPercentage() |
||||||
| 107 | { |
||||||
| 108 | $percent = $this->getTaxRate()->Rate; |
||||||
|
0 ignored issues
–
show
It seems like
getTaxRate() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 109 | |||||||
| 110 | if ($this->hasMethod('extend')) { |
||||||
| 111 | $result = $this->filterTaxableExtensionResults( |
||||||
| 112 | $this->extend("updateTaxPercentage", $percent) |
||||||
| 113 | ); |
||||||
| 114 | |||||||
| 115 | if (!empty($result)) { |
||||||
| 116 | return $result; |
||||||
| 117 | } |
||||||
| 118 | } |
||||||
| 119 | |||||||
| 120 | return $percent; |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | /** |
||||||
| 124 | * Get a final tax amount for this object. You can extend this |
||||||
| 125 | * method using "UpdateTax" allowing third party modules to alter |
||||||
| 126 | * tax amounts dynamically. |
||||||
| 127 | * |
||||||
| 128 | * @return float |
||||||
| 129 | */ |
||||||
| 130 | public function getTaxAmount() |
||||||
| 131 | { |
||||||
| 132 | if (!$this->exists()) { |
||||||
|
0 ignored issues
–
show
It seems like
exists() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 133 | return 0; |
||||||
| 134 | } |
||||||
| 135 | |||||||
| 136 | $price = $this->getBasePrice(); |
||||||
| 137 | $percent = $this->getTaxPercentage(); |
||||||
| 138 | |||||||
| 139 | // Round using default rounding defined on MathsHelper |
||||||
| 140 | $tax = ($price / 100) * $percent; |
||||||
| 141 | |||||||
| 142 | if ($this->hasMethod('extend')) { |
||||||
| 143 | $result = $this->filterTaxableExtensionResults( |
||||||
| 144 | $this->extend("updateTaxAmount", $tax) |
||||||
| 145 | ); |
||||||
| 146 | |||||||
| 147 | if (!empty($result)) { |
||||||
| 148 | return $result; |
||||||
| 149 | } |
||||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | return $tax; |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | /** |
||||||
| 156 | * Get the Total price and tax |
||||||
| 157 | * |
||||||
| 158 | * @return float |
||||||
| 159 | */ |
||||||
| 160 | public function getPriceAndTax() |
||||||
| 161 | { |
||||||
| 162 | $notax = $this->getNoTaxPrice(); |
||||||
| 163 | $tax = $this->getTaxAmount(); |
||||||
| 164 | $price = $notax + $tax; |
||||||
| 165 | |||||||
| 166 | if ($this->hasMethod('extend')) { |
||||||
| 167 | $result = $this->filterTaxableExtensionResults( |
||||||
| 168 | $this->extend("updatePriceAndTax", $price) |
||||||
| 169 | ); |
||||||
| 170 | |||||||
| 171 | if (!empty($result)) { |
||||||
| 172 | return $result; |
||||||
| 173 | } |
||||||
| 174 | } |
||||||
| 175 | |||||||
| 176 | return $price; |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | /** |
||||||
| 180 | * Generate a string to go with the the product price. We can |
||||||
| 181 | * overwrite the wording of this by using Silverstripes language |
||||||
| 182 | * files |
||||||
| 183 | * |
||||||
| 184 | * @param bool|null $include_tax Should this include tax or not? |
||||||
| 185 | * |
||||||
| 186 | * @return string |
||||||
| 187 | */ |
||||||
| 188 | public function getTaxString($include_tax = null) |
||||||
| 189 | { |
||||||
| 190 | if (!$this->hasMethod('getTaxRate') |
||||||
| 191 | || !$this->hasMethod('getShowPriceWithTax') |
||||||
| 192 | ) { |
||||||
| 193 | throw new LogicException('Object must implement \SilverCommerce\TaxAdmin\Interfaces\TaxableProvider'); |
||||||
| 194 | } |
||||||
| 195 | |||||||
| 196 | $string = ""; |
||||||
| 197 | $rate = $this->getTaxRate(); |
||||||
| 198 | |||||||
| 199 | if (empty($include_tax)) { |
||||||
| 200 | $include_tax = $this->getShowPriceWithTax(); |
||||||
|
0 ignored issues
–
show
It seems like
getShowPriceWithTax() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 201 | } |
||||||
| 202 | |||||||
| 203 | if ($rate->exists() && $include_tax) { |
||||||
| 204 | $string = _t( |
||||||
| 205 | self::class . ".TaxIncludes", |
||||||
| 206 | "inc. {title}", |
||||||
| 207 | ["title" => $rate->Title] |
||||||
| 208 | ); |
||||||
| 209 | } elseif ($rate->exists() && !$include_tax) { |
||||||
| 210 | $string = _t( |
||||||
| 211 | self::class . ".TaxExcludes", |
||||||
| 212 | "ex. {title}", |
||||||
| 213 | ["title" => $rate->Title] |
||||||
| 214 | ); |
||||||
| 215 | } |
||||||
| 216 | |||||||
| 217 | if ($this->hasMethod('extend')) { |
||||||
| 218 | $result = $this->filterTaxableExtensionResults( |
||||||
| 219 | $this->extend("updateTaxString", $string) |
||||||
| 220 | ); |
||||||
| 221 | |||||||
| 222 | if (!empty($result)) { |
||||||
| 223 | return $result; |
||||||
| 224 | } |
||||||
| 225 | } |
||||||
| 226 | |||||||
| 227 | return $string; |
||||||
| 228 | } |
||||||
| 229 | |||||||
| 230 | /** |
||||||
| 231 | * Return a formatted price (based on locale) |
||||||
| 232 | * |
||||||
| 233 | * @param bool $include_tax Should the formatted price include tax? |
||||||
| 234 | * |
||||||
| 235 | * @return string |
||||||
| 236 | */ |
||||||
| 237 | public function getFormattedPrice($include_tax = false) |
||||||
| 238 | { |
||||||
| 239 | $currency = $this->getCurrency(); |
||||||
| 240 | $formatter = $this->getFormatter(); |
||||||
| 241 | |||||||
| 242 | if ($include_tax) { |
||||||
| 243 | $amount = $this->getPriceAndTax(); |
||||||
| 244 | } else { |
||||||
| 245 | $amount = $this->getNoTaxPrice(); |
||||||
| 246 | } |
||||||
| 247 | |||||||
| 248 | // Without currency, format as basic localised number |
||||||
| 249 | if (!$currency) { |
||||||
| 250 | return $formatter->format($amount); |
||||||
| 251 | } |
||||||
| 252 | |||||||
| 253 | return $formatter->formatCurrency($amount, $currency); |
||||||
| 254 | } |
||||||
| 255 | |||||||
| 256 | /** |
||||||
| 257 | * Get nicely formatted currency (based on current locale) |
||||||
| 258 | * |
||||||
| 259 | * @return string |
||||||
| 260 | */ |
||||||
| 261 | public function getNicePrice() |
||||||
| 262 | { |
||||||
| 263 | if ($this->hasMethod('renderWith')) { |
||||||
| 264 | return $this->renderWith(__CLASS__ . "_NicePrice"); |
||||||
|
0 ignored issues
–
show
It seems like
renderWith() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 265 | } |
||||||
| 266 | |||||||
| 267 | return ""; |
||||||
| 268 | } |
||||||
| 269 | } |
||||||
| 270 |