PreOrderServiceFee.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/8/21
  6. * Time: 9:50
  7. */
  8. namespace app\frontend\models\order;
  9. use app\common\models\order\OrderServiceFee;
  10. use app\frontend\modules\order\models\PreOrder;
  11. use app\frontend\modules\order\serviceFee\BaseOrderServiceFee;
  12. class PreOrderServiceFee extends OrderServiceFee
  13. {
  14. protected $appends = ['checked', 'show'];
  15. /**
  16. * @var PreOrder
  17. */
  18. public $order;
  19. /**
  20. * @var BaseOrderServiceFee
  21. */
  22. protected $serviceFee;
  23. public function init(BaseOrderServiceFee $serviceFee, PreOrder $order)
  24. {
  25. $this->serviceFee = $serviceFee;
  26. $this->setOrder($order);
  27. }
  28. public function setOrder(PreOrder $order)
  29. {
  30. $this->order = $order;
  31. $this->order->orderServiceFees->push($this);
  32. }
  33. public function getServiceFee()
  34. {
  35. return $this->serviceFee;
  36. }
  37. public function getUidAttribute()
  38. {
  39. return $this->order->uid;
  40. }
  41. public function getCodeAttribute()
  42. {
  43. return $this->getServiceFee()->getCode();
  44. }
  45. public function getNameAttribute()
  46. {
  47. return $this->getServiceFee()->getName();
  48. }
  49. /**
  50. * @return mixed
  51. * @throws \app\common\exceptions\AppException
  52. */
  53. public function getAmountAttribute()
  54. {
  55. return $this->getServiceFee()->getAmount();
  56. }
  57. /**
  58. * 调用当前类不存在方法时 查询 BaseOrderServiceFee 类存在代替处理
  59. * @param string $method
  60. * @param array $parameters
  61. * @return mixed
  62. */
  63. public function __call($method, $parameters)
  64. {
  65. if (method_exists($this->getServiceFee(), $method)) {
  66. return $this->getServiceFee()->$method($parameters);
  67. }
  68. return parent::__call($method, $parameters); // TODO: Change the autogenerated stub
  69. }
  70. /**
  71. * @return mixed
  72. * @throws \app\common\exceptions\AppException
  73. */
  74. // public function getAmount()
  75. // {
  76. //
  77. // return $this->getServiceFee()->getAmount();
  78. // }
  79. /**
  80. * @return bool
  81. */
  82. public function getCheckedAttribute()
  83. {
  84. return $this->getServiceFee()->isChecked();
  85. }
  86. /**
  87. * @return bool
  88. */
  89. public function getShowAttribute()
  90. {
  91. return $this->getServiceFee()->isShow();
  92. }
  93. /**
  94. * @return array
  95. */
  96. public function toArray()
  97. {
  98. $this->code = (string)$this->code;
  99. $this->name = (string)$this->name;
  100. $this->amount = sprintf('%.2f', $this->amount);
  101. return parent::toArray();
  102. }
  103. /**
  104. * @return bool
  105. */
  106. public function beforeSaving()
  107. {
  108. if (!$this->getServiceFee()->isChecked()) {
  109. return false;
  110. }
  111. $this->uid = (string)$this->uid;
  112. $this->code = (string)$this->code;
  113. $this->name = (string)$this->name;
  114. $this->amount = sprintf('%.2f', $this->amount);
  115. return parent::beforeSaving();
  116. }
  117. }