您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RateCast.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Casts;
  3. use App\Enums\Accounting\AdjustmentComputation;
  4. use App\Utilities\Currency\CurrencyAccessor;
  5. use App\Utilities\RateCalculator;
  6. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  7. use Illuminate\Database\Eloquent\Model;
  8. class RateCast implements CastsAttributes
  9. {
  10. public function get($model, string $key, $value, array $attributes): string
  11. {
  12. if (! $value) {
  13. return '0';
  14. }
  15. $currency_code = $this->getDefaultCurrencyCode();
  16. $computation = AdjustmentComputation::parse($attributes['computation'] ?? $attributes['discount_computation'] ?? null);
  17. if ($computation?->isFixed()) {
  18. return money($value, $currency_code)->formatSimple();
  19. }
  20. return RateCalculator::formatScaledRate($value);
  21. }
  22. public function set(Model $model, string $key, mixed $value, array $attributes): int
  23. {
  24. if (! $value) {
  25. return 0;
  26. }
  27. if (is_int($value)) {
  28. return $value;
  29. }
  30. $computation = AdjustmentComputation::parse($attributes['computation'] ?? $attributes['discount_computation'] ?? null);
  31. $currency_code = $this->getDefaultCurrencyCode();
  32. if ($computation?->isFixed()) {
  33. return money($value, $currency_code, true)->getAmount();
  34. }
  35. return RateCalculator::parseLocalizedRate($value);
  36. }
  37. private function getDefaultCurrencyCode(): string
  38. {
  39. return CurrencyAccessor::getDefaultCurrency();
  40. }
  41. }