StoreOrder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\store\order;
  12. use app\common\model\BaseModel;
  13. use app\common\model\community\Community;
  14. use app\common\model\store\product\ProductGroupUser;
  15. use app\common\model\store\service\StoreService;
  16. use app\common\model\store\shipping\Express;
  17. use app\common\model\system\merchant\Merchant;
  18. use app\common\model\user\User;
  19. use app\common\model\store\staff\Staffs;
  20. use app\common\model\user\UserBill;
  21. use app\common\repositories\store\MerchantTakeRepository;
  22. class StoreOrder extends BaseModel
  23. {
  24. public static function tablePk(): ?string
  25. {
  26. return 'order_id';
  27. }
  28. public static function tableName(): string
  29. {
  30. return 'store_order';
  31. }
  32. public function orderProduct()
  33. {
  34. return $this->hasMany(StoreOrderProduct::class, 'order_id', 'order_id');
  35. }
  36. public function refundProduct()
  37. {
  38. return $this->orderProduct()->where('refund_num', '>', 0);
  39. }
  40. public function refundOrder()
  41. {
  42. return $this->hasMany(StoreRefundOrder::class,'order_id','order_id');
  43. }
  44. public function orderStatus()
  45. {
  46. return $this->hasMany(StoreOrderStatus::class,'order_id','order_id')->order('change_time DESC');
  47. }
  48. public function merchant()
  49. {
  50. return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
  51. }
  52. public function user()
  53. {
  54. return $this->hasOne(User::class, 'uid', 'uid');
  55. }
  56. public function staffs()
  57. {
  58. return $this->hasOne(Staffs::class, 'staffs_id', 'staffs_id');
  59. }
  60. public function receipt()
  61. {
  62. return $this->hasOne(StoreOrderReceipt::class, 'order_id', 'order_id');
  63. }
  64. public function spread()
  65. {
  66. return $this->hasOne(User::class, 'uid', 'spread_uid');
  67. }
  68. public function billOne()
  69. {
  70. return $this->hasMany(UserBill::class, 'link_id', 'order_id')->where('type','order_one');
  71. }
  72. public function billTwo()
  73. {
  74. return $this->hasMany(UserBill::class, 'link_id', 'order_id')->where('type','order_two');
  75. }
  76. public function TopSpread()
  77. {
  78. return $this->hasOne(User::class, 'uid', 'top_uid');
  79. }
  80. public function getUserPhoneAttr($value)
  81. {
  82. if (env('SHOW_PHONE',false) && app('request')->hasMacro('adminInfo') && $value && is_numeric($value)){
  83. if (app('request')->userType() !== 2 || app('request')->adminInfo()['level'] != 0) {
  84. return substr_replace($value, '****', 3, 4);
  85. }
  86. }
  87. return $value;
  88. }
  89. public function groupOrder()
  90. {
  91. return $this->hasOne(StoreGroupOrder::class, 'group_order_id', 'group_order_id');
  92. }
  93. public function verifyService()
  94. {
  95. return $this->hasOne(StoreService::class, 'service_id', 'verify_service_id');
  96. }
  97. public function getTakeAttr()
  98. {
  99. return app()->make(MerchantTakeRepository::class)->get($this->mer_id);
  100. }
  101. public function searchDataAttr($query, $value)
  102. {
  103. return getModelTime($query, $value);
  104. }
  105. public function presellOrder()
  106. {
  107. return $this->hasOne(PresellOrder::class, 'order_id', 'order_id');
  108. }
  109. public function finalOrder()
  110. {
  111. return $this->hasOne(PresellOrder::class,'order_id','order_id');
  112. }
  113. public function groupUser()
  114. {
  115. return $this->hasOne(ProductGroupUser::class,'order_id','order_id');
  116. }
  117. public function profitsharing()
  118. {
  119. return $this->hasMany(StoreOrderProfitsharing::class, 'order_id', 'order_id');
  120. }
  121. public function firstProfitsharing()
  122. {
  123. return $this->hasOne(StoreOrderProfitsharing::class, 'order_id', 'order_id')->where('type', 'order');
  124. }
  125. public function presellProfitsharing()
  126. {
  127. return $this->hasOne(StoreOrderProfitsharing::class, 'order_id', 'order_id')->where('type', 'presell');
  128. }
  129. // 核销订单的自订单列表
  130. public function takeOrderList()
  131. {
  132. return $this->hasMany(self::class,'main_id','order_id')->order('verify_time DESC');
  133. }
  134. public function searchMerIdAttr($query, $value)
  135. {
  136. return $query->where('mer_id', $value);
  137. }
  138. public function getRefundStatusAttr()
  139. {
  140. $day = (int)systemConfig('sys_refund_timer') ?: 15;
  141. return ($this->verify_time ? strtotime($this->verify_time) > strtotime('-' . $day . ' day') : true);
  142. }
  143. public function getCancelTimeAttr()
  144. {
  145. if (!$this->apid) {
  146. $timer = ((int)systemConfig('auto_close_order_timer')) ?: 15;
  147. return date('m-d H:i', strtotime("+ $timer minutes", strtotime($this->groupOrder->create_time)));
  148. }
  149. return null;
  150. }
  151. public function getOpenReceiptAttr()
  152. {
  153. return merchantConfig($this->mer_id,'mer_open_receipt') ?: 0;
  154. }
  155. public function getOrderExtendAttr($val)
  156. {
  157. return $val ? json_decode($val, true) : null;
  158. }
  159. public function getRefundExtensionOneAttr()
  160. {
  161. if ( $this->refundOrder ){
  162. return $this->refundOrder()->where('status',3)->sum('extension_one');
  163. }
  164. return 0;
  165. }
  166. public function getRefundExtensionTwoAttr()
  167. {
  168. if ( $this->refundOrder ){
  169. return $this->refundOrder()->where('status',3)->sum('extension_two');
  170. }
  171. return 0;
  172. }
  173. public function community()
  174. {
  175. return $this->hasOne(Community::class, 'order_id', 'order_id')->bind(['community_id']);
  176. }
  177. public function getRefundPriceAttr()
  178. {
  179. return StoreRefundOrder::where('order_id',$this->order_id)->where('status',3)->sum('refund_price');
  180. }
  181. public function getReservationServiceVoucherAttr($value)
  182. {
  183. return $value? json_decode($value, true) : [];
  184. }
  185. public function getClockInInfoAttr($value)
  186. {
  187. return $value? json_decode($value, true) : [];
  188. }
  189. }