RefundApply.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/12
  6. * Time: 下午1:38
  7. */
  8. namespace app\common\models\refund;
  9. use app\common\models\BaseModel;
  10. use app\common\models\Member;
  11. use app\common\models\Order;
  12. use app\frontend\modules\refund\services\RefundService;
  13. use Illuminate\Database\Eloquent\Collection;
  14. use Illuminate\Database\Eloquent\Builder;
  15. /**
  16. * Class RefundApply
  17. * @package app\common\models\refund
  18. * @property float price 退款金额
  19. * @property float freight_price 退款运费
  20. * @property float other_price 退款其他金额
  21. * @property float apply_price 退款商品申请金额和
  22. * @property int status 售后状态
  23. * @property int part_refund 0、换货售后无状态;1、部分退款;2、最后一次退款;3、一次性全部申请退款;4、订单关闭并退款
  24. * @property integer refund_type 售后类型
  25. * @property Order order
  26. * @property ReturnExpress returnExpress
  27. * @property Collection refundOrderGoods
  28. * @property Collection processLog
  29. * @property Collection hasManyResendExpress
  30. *
  31. */
  32. class RefundApply extends BaseModel
  33. {
  34. protected $table = 'yz_order_refund';
  35. protected $hidden = ['updated_at', 'created_at','uniacid', 'uid', 'order_id'];
  36. protected $fillable = [];
  37. protected $guarded = ['id'];
  38. static protected $needLog = true;
  39. protected $appends = [
  40. 'refund_type_name', 'status_name', 'is_refunded', 'is_refunding', 'is_refund_fail', 'plugin_id',
  41. 'receive_status_name', 'refund_way_type_name',
  42. ];
  43. protected $attributes = [
  44. 'images' => '[]',
  45. 'refund_proof_imgs' => '[]',
  46. 'content' => '',
  47. 'reply' => '',
  48. 'remark' => '',
  49. 'refund_address' => '',
  50. 'reject_reason' => '',
  51. 'refund_way_type' => 0,
  52. 'part_refund' => 0,
  53. 'receive_status' => 0,
  54. 'apply_price' => 0,
  55. 'freight_price' => 0,
  56. 'other_price' => 0,
  57. ];
  58. protected $casts = [
  59. 'images' => 'json',
  60. 'refund_proof_imgs' => 'json'
  61. ];
  62. //类型
  63. const REFUND_TYPE_REFUND_MONEY = 0;
  64. const REFUND_TYPE_RETURN_GOODS = 1;
  65. const REFUND_TYPE_EXCHANGE_GOODS = 2;
  66. //状态
  67. const CLOSE = '-3';//关闭 废弃换货也是售后完成 状态应该是 6
  68. const CANCEL = '-2';//用户取消
  69. const REJECT = '-1';//驳回
  70. const WAIT_CHECK = '0';//待审核
  71. const WAIT_RETURN_GOODS = '1';//待退货
  72. const WAIT_RECEIVE_RETURN_GOODS = '2';//待收货\用户发货
  73. const WAIT_RESEND_GOODS = '3';//重新发货
  74. const WAIT_RECEIVE_RESEND_GOODS = '4';//重新收货\商家发货
  75. const WAIT_REFUND = '5';//待打款
  76. const COMPLETE = '6';//已完成
  77. const CONSENSUS = '7';//手动退款
  78. //区别
  79. const PART_REFUND = 1;
  80. const ORDER_CLOSE = 4;
  81. public function getDates()
  82. {
  83. return ['create_time', 'refund_time', 'operate_time', 'send_time', 'return_time', 'end_time', 'cancel_pay_time', 'cancel_send_time'] + parent::getDates();
  84. }
  85. public function __construct(array $attributes = [])
  86. {
  87. parent::__construct($attributes);
  88. if (!isset($this->uniacid)) {
  89. $this->uniacid = \YunShop::app()->uniacid;
  90. }
  91. // todo 转移到前端
  92. // if (!isset($this->uid)) {
  93. // $this->uid = \YunShop::app()->getMemberId();
  94. // }
  95. }
  96. //禁止使用该方法,使用 RefundOperationService::orderCloseAndRefund($order) 创建售后申请
  97. public static function createByOrder(Order $order)
  98. {
  99. $refundApply = new \app\backend\modules\refund\services\operation\OrderCloseAndRefund();
  100. $refundApply->setRelation('order',$order);
  101. $result = \Illuminate\Support\Facades\DB::transaction(function () use ($refundApply) {
  102. return $refundApply->execute();
  103. });
  104. return $refundApply;
  105. $refundApply = new static();
  106. $refundApply->images = [];
  107. $refundApply->content = '订单状态改变失败退款';
  108. $refundApply->reason = '订单状态改变失败退款';
  109. $refundApply->order_id = $order->id;
  110. $refundApply->refund_type = 0;
  111. $refundApply->refund_sn = RefundService::createOrderRN();
  112. $refundApply->create_time = time();
  113. $refundApply->price = $order->price;
  114. return $refundApply;
  115. }
  116. /**
  117. * 获取订单已售后完成记录
  118. * @param $order_id
  119. * @param array $typeArray 售后类型 默认只查询: 退款、退货退款的
  120. * @return self
  121. */
  122. public static function getAfterSales($order_id, $typeArray = [0,1])
  123. {
  124. $model = self::select([
  125. 'order_id', 'uid','refund_type', 'status', 'refund_sn', 'part_refund',
  126. 'price', 'apply_price', 'freight_price', 'other_price', 'price',
  127. ])->where('status', '>=', RefundApply::COMPLETE)
  128. ->where('order_id', $order_id);
  129. if ($typeArray) {
  130. $model->whereIn('refund_type', $typeArray);
  131. }
  132. return $model;
  133. }
  134. /**
  135. * 商城会员信息
  136. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  137. */
  138. public function hasOneMember()
  139. {
  140. return $this->hasOne(Member::class,'uid', 'uid');
  141. }
  142. public function returnExpress()
  143. {
  144. return $this->hasOne(ReturnExpress::class, 'refund_id', 'id');
  145. }
  146. public function resendExpress()
  147. {
  148. return $this->hasOne(ResendExpress::class, 'refund_id', 'id')->orderBy('id','desc');
  149. }
  150. public function changeLog()
  151. {
  152. return $this->hasOne(RefundChangeLog::class, 'refund_id', 'id');
  153. }
  154. public function processLog()
  155. {
  156. return $this->hasMany(RefundProcessLog::class, 'refund_id', 'id')->orderBy('id','desc');
  157. }
  158. public function hasManyResendExpress()
  159. {
  160. return $this->hasMany(ResendExpress::class, 'refund_id', 'id');
  161. }
  162. /**
  163. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  164. */
  165. public function refundOrderGoods()
  166. {
  167. //这里需要查询软删除的,显示需要
  168. return $this->hasMany(RefundGoodsLog::class, 'refund_id', 'id')->withTrashed();
  169. }
  170. public function getAllTypeAttribute()
  171. {
  172. return collect([
  173. [
  174. 'id' => self::REFUND_TYPE_REFUND_MONEY,
  175. 'name' => '仅退款',
  176. ], [
  177. 'id' => self::REFUND_TYPE_RETURN_GOODS,
  178. 'name' => '退款退货',
  179. ], [
  180. 'id' => self::REFUND_TYPE_EXCHANGE_GOODS,
  181. 'name' => '换货',
  182. ],
  183. ]);
  184. }
  185. public function getAllStatusAttribute()
  186. {
  187. return collect([
  188. [
  189. 'id' => self::CLOSE,
  190. 'name' => '已关闭',
  191. ], [
  192. 'id' => self::CANCEL,
  193. 'name' => '用户取消',
  194. ], [
  195. 'id' => self::REJECT,
  196. 'name' => '驳回',
  197. ], [
  198. 'id' => self::WAIT_CHECK,
  199. 'name' => '待审核',
  200. ], [
  201. 'id' => self::WAIT_RETURN_GOODS,
  202. 'name' => '待退货',
  203. ], [
  204. 'id' => self::WAIT_RECEIVE_RETURN_GOODS,
  205. 'name' => '待收货',
  206. ], [
  207. 'id' => self::WAIT_RESEND_GOODS,
  208. 'name' => '重新发货',
  209. ], [
  210. 'id' => self::WAIT_RECEIVE_RESEND_GOODS,
  211. 'name' => '重新收货',
  212. ], [
  213. 'id' => self::COMPLETE,
  214. 'name' => '已完成',
  215. ], [
  216. 'id' => self::CONSENSUS,
  217. 'name' => '手动退款',
  218. ]
  219. ]);
  220. }
  221. public function getRefundTypeNameAttribute()
  222. {
  223. return $this->getRefundTypeName()[$this->refund_type];
  224. }
  225. protected function getRefundTypeName()
  226. {
  227. return [
  228. self::REFUND_TYPE_REFUND_MONEY => '退款',
  229. self::REFUND_TYPE_RETURN_GOODS => '退款退货',
  230. self::REFUND_TYPE_EXCHANGE_GOODS => '换货',
  231. ];
  232. }
  233. protected function getStatusNameMapping()
  234. {
  235. return [
  236. self::CLOSE => '已关闭',
  237. self::CANCEL => '用户取消',
  238. self::REJECT => '已驳回',
  239. self::WAIT_CHECK => '待审核',
  240. self::WAIT_RETURN_GOODS => '待退货',
  241. self::WAIT_RECEIVE_RETURN_GOODS => '商家待收货',
  242. self::WAIT_RESEND_GOODS => '商家分批发货',
  243. self::WAIT_RECEIVE_RESEND_GOODS => '待买家收货',
  244. self::WAIT_REFUND => '待退款',
  245. self::COMPLETE => '已' . $this->getRefundTypeName()[$this->refund_type],
  246. self::CONSENSUS => '已手动退款',
  247. ];
  248. }
  249. public function scopeRefunding($query)
  250. {
  251. return $query->where('status', '>=', self::WAIT_CHECK)->where('status', '<', self::COMPLETE);
  252. }
  253. public function scopeRefunded($query)
  254. {
  255. return $query->where('status', '>=', self::COMPLETE);
  256. }
  257. public function scopeRefundMoney($query)
  258. {
  259. return $query->where('refund_type', self::REFUND_TYPE_REFUND_MONEY);
  260. }
  261. public function scopeReturnGoods($query)
  262. {
  263. return $query->where('refund_type', self::REFUND_TYPE_RETURN_GOODS);
  264. }
  265. public function scopeExchangeGoods($query)
  266. {
  267. return $query->where('refund_type', self::REFUND_TYPE_EXCHANGE_GOODS);
  268. }
  269. public function getIsPlugin($order_id)
  270. {
  271. return \app\common\models\Order::where('id', $order_id)->select('is_plugin', 'plugin_id')->first();
  272. }
  273. public function getSupplierId($order_id)
  274. {
  275. return \Yunshop\Supplier\common\models\SupplierOrder::where('order_id', $order_id)->value('supplier_id');
  276. }
  277. public function getStoreId($order_id)
  278. {
  279. return \Yunshop\StoreCashier\common\models\StoreOrder::where('order_id', $order_id)->value('store_id');
  280. }
  281. public function getStatusNameAttribute()
  282. {
  283. return $this->getStatusNameMapping()[$this->status];
  284. }
  285. public function getReceiveStatusNameAttribute()
  286. {
  287. switch ($this->receive_status) {
  288. case 0:
  289. return '未收到货';
  290. case 1:
  291. return '已收到货';
  292. default:
  293. return '不填写';
  294. }
  295. }
  296. public function getRefundWayTypeNameAttribute()
  297. {
  298. switch ($this->refund_way_type) {
  299. case 0:
  300. return '自行寄回';
  301. case 1:
  302. return '上门取货';
  303. default:
  304. return '其他方式';
  305. }
  306. }
  307. public function getIsRefundedAttribute()
  308. {
  309. return $this->isRefunded();
  310. }
  311. public function getIsRefundingAttribute()
  312. {
  313. return $this->isRefunding();
  314. }
  315. public function getIsRefundFailAttribute()
  316. {
  317. return $this->isRefundFail();
  318. }
  319. public function getPartRefundNameAttribute()
  320. {
  321. switch ($this->part_refund) {
  322. case 0:
  323. return '售后';
  324. case 1:
  325. return '部分退款';
  326. case 2:
  327. return '最后退款';
  328. case 3:
  329. return '全部退款';
  330. case 4:
  331. return '订单关闭并退款';
  332. default:
  333. return $this->part_refund;
  334. }
  335. }
  336. /**
  337. * 是否部分退款
  338. * @return bool
  339. */
  340. final public function isPartRefund()
  341. {
  342. return $this->part_refund === self::PART_REFUND;
  343. }
  344. /**
  345. * 退款失败
  346. * @return bool
  347. */
  348. public function isRefundFail()
  349. {
  350. if ($this->status < self::WAIT_CHECK) {
  351. return true;
  352. }
  353. return false;
  354. }
  355. /**
  356. * 已退款
  357. * @return bool
  358. */
  359. public function isRefunded()
  360. {
  361. if ($this->status >= self::COMPLETE) {
  362. return true;
  363. }
  364. return false;
  365. }
  366. /**
  367. * 退款中
  368. * @return bool
  369. */
  370. public function isRefunding()
  371. {
  372. if ($this->status < self::WAIT_CHECK) {
  373. return false;
  374. }
  375. if ($this->status >= self::COMPLETE) {
  376. return false;
  377. }
  378. return true;
  379. }
  380. //用于区分插件与商城订单
  381. public function getPluginIdAttribute()
  382. {
  383. if ($this->order) {
  384. return $this->order->plugin_id;
  385. }
  386. return 0;
  387. }
  388. /**
  389. * todo 为了配合供应商做出的修改,需要重新考虑区分插件与商城订单的机制
  390. * {@inheritdoc}
  391. */
  392. public function order()
  393. {
  394. return $this->belongsTo(Order::class, 'order_id', 'id');
  395. }
  396. protected static function boot()
  397. {
  398. parent::boot();
  399. static::addGlobalScope(function (Builder $builder) {
  400. $builder->uniacid();
  401. });
  402. }
  403. public function save(array $options = [])
  404. {
  405. // if(!in_array($this->getOriginal('status'),[6,7]) && $this->isRefunded()){
  406. // $this->order->hasOneOrderPay->refund();
  407. // }
  408. return parent::save($options);
  409. }
  410. }