PaymentTradeItemRefund.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace common\models\common;
  3. use common\models\order\OrderRefund;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%payment_trade_item_refund}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $trade_id 支付流水ID
  11. * @property int $item_id 组合支付ID
  12. * @property int $refund_id 售后订单ID
  13. * @property float $refund_fee 售后金额
  14. * @property int $refund_status 是否售后
  15. * @property int $pay_type 支付类型
  16. * @property int|null $refund_at 售后时间
  17. * @property int|null $updated_at
  18. * @property int|null $created_at
  19. * @property string $refund_source 退款数据来源(Mall是主商城 否则是插件)
  20. */
  21. class PaymentTradeItemRefund extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * 已退款
  25. */
  26. const REFUND = 1;
  27. /**
  28. * 已拒绝
  29. */
  30. const REFUSE = 2;
  31. /**
  32. * 等待
  33. */
  34. const WAIT = 0;
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%payment_trade_item_refund}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'trade_id', 'item_id', 'refund_id', 'pay_type'], 'required'],
  49. [['mall_id', 'trade_id', 'item_id', 'refund_id', 'refund_status', 'pay_type', 'refund_at', 'updated_at', 'created_at'], 'integer'],
  50. [['refund_source'], 'string'],
  51. [['refund_fee'], 'number'],
  52. ];
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'mall_id' => 'Mall ID',
  62. 'trade_id' => 'Trade ID',
  63. 'item_id' => 'Item ID',
  64. 'refund_id' => 'Refund ID',
  65. 'refund_fee' => 'Refund Fee',
  66. 'refund_status' => 'Is Refund',
  67. 'pay_type' => 'Pay Type',
  68. 'refund_at' => 'Refund At',
  69. 'updated_at' => 'Updated At',
  70. 'created_at' => 'Created At',
  71. 'refund_source' => 'Refund Source',
  72. ];
  73. }
  74. /**
  75. * @param integer $refund_id
  76. * @return array
  77. */
  78. public static function getMixedPayRefundList(int $trade_id, int $refund_id, string $refund_source = 'Mall')
  79. {
  80. return static::find()
  81. ->where(['trade_id' => $trade_id, 'refund_id' => $refund_id, 'refund_status' => 0, 'refund_source' => $refund_source])
  82. ->all();
  83. }
  84. /**
  85. * @param array $params
  86. * @param OrderRefund|null $refund
  87. * @return float
  88. */
  89. public function getRefundFee(array $params = [], $refund = null)
  90. {
  91. // 不使用$params传入金额
  92. return $this->refund_fee;
  93. }
  94. /**
  95. * @param integer $trade_id
  96. * @param integer $refund_id
  97. * @param string $refund_source
  98. * @return int
  99. */
  100. public static function setRefund(int $trade_id, int $refund_id, string $refund_source = 'Mall')
  101. {
  102. return static::updateAll(
  103. ['refund_status' => static::REFUND, 'refund_at' => time()],
  104. ['trade_id' => $trade_id, 'refund_id' => $refund_id, 'refund_status' => static::WAIT, 'refund_source' => $refund_source]
  105. );
  106. }
  107. /**
  108. * @param integer $trade_id
  109. * @param integer $refund_id
  110. * @param string $refund_source
  111. * @return int
  112. */
  113. public static function setRefuse(int $trade_id, int $refund_id, string $refund_source = 'Mall')
  114. {
  115. return static::updateAll(
  116. ['refund_status' => static::REFUSE, 'refund_at' => time()],
  117. ['trade_id' => $trade_id, 'refund_id' => $refund_id, 'refund_status' => static::WAIT, 'refund_source' => $refund_source]
  118. );
  119. }
  120. /**
  121. * @return int
  122. */
  123. public function getTradeId()
  124. {
  125. return $this->trade_id;
  126. }
  127. /**
  128. * @param OrderRefund $refund
  129. * @param PaymentTrade $trade
  130. * @param PaymentTradeItem $item
  131. * @param float $fee
  132. * @param string $refund_source
  133. * @return boolean
  134. */
  135. public static function addRefundItem(
  136. OrderRefund $refund, PaymentTrade $trade, PaymentTradeItem $item, float $fee, string $refund_source = 'Mall'
  137. )
  138. {
  139. $model = new static();
  140. $model->mall_id = $refund->mall_id;
  141. $model->trade_id = $trade->id;
  142. $model->item_id = $item->id;
  143. $model->pay_mode = $item->pay_mode;
  144. $model->refund_id = $refund->id;
  145. $model->pay_type = $item->pay_type;
  146. $model->refund_fee = $fee;
  147. $model->refund_source = $refund_source;
  148. return $model->save();
  149. }
  150. }