Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model;
  13. use app\common\model\order\RefundHis;
  14. use app\common\model\store\shop\Order as ShopOrder;
  15. use app\common\service\Order as OrderService;
  16. use app\common\service\order\Complete as OrderCompleteService;
  17. use app\common\enum\OrderType as OrderTypeEnum;
  18. use app\common\enum\order\PayStatus as PayStatusEnum;
  19. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  20. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  21. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  22. use app\common\library\helper;
  23. use app\console\model\Setting as SettingModel;
  24. /**
  25. * 订单模型
  26. * Class Order
  27. * @package app\common\model
  28. */
  29. class Order extends BaseModel
  30. {
  31. // 定义表名
  32. protected $name = 'order';
  33. // 定义表名(外部引用)
  34. public static $tableName = 'order';
  35. // 定义主键
  36. protected $pk = 'order_id';
  37. // 定义别名
  38. protected $alias = 'order';
  39. /**
  40. * 追加字段
  41. * @var array
  42. */
  43. protected $append = [
  44. 'state_text', // 售后单状态文字描述
  45. 'state_value', // 售后状态码 20已取消 30已完成 11待支付 12待收货未发货 13待收货已发货
  46. ];
  47. /**
  48. * 订单商品列表
  49. * @return \think\model\relation\HasMany
  50. */
  51. public function goods()
  52. {
  53. $module = self::getCalledModule();
  54. return $this->hasMany("app\\{$module}\\model\\OrderGoods")->withoutField('content');
  55. }
  56. /**
  57. * 关联订单收货地址表
  58. * @return \think\model\relation\HasOne
  59. */
  60. public function address()
  61. {
  62. $module = self::getCalledModule();
  63. return $this->hasOne("app\\{$module}\\model\\OrderAddress");
  64. }
  65. /**
  66. * 关联用户表
  67. * @return \think\model\relation\BelongsTo
  68. */
  69. public function user()
  70. {
  71. $module = self::getCalledModule();
  72. return $this->belongsTo("app\\{$module}\\model\\User");
  73. }
  74. /**
  75. * 关联核销人用户
  76. * @return \think\model\relation\BelongsTo
  77. */
  78. public function hxUser()
  79. {
  80. $module = self::getCalledModule();
  81. return $this->belongsTo("app\\{$module}\\model\\User", 'hx_user_id')->field('user_id,mobile,nick_name');
  82. }
  83. /**
  84. * 关联门店表
  85. * @return \think\model\relation\BelongsTo
  86. */
  87. public function shops()
  88. {
  89. $module = self::getCalledModule();
  90. return $this->belongsTo("app\\{$module}\\model\\shop\\Shops", 'staff_shop_id');
  91. }
  92. /**
  93. * 关联推广人
  94. * @return \think\model\relation\BelongsTo
  95. */
  96. public function staffUser()
  97. {
  98. return $this->belongsTo('User','staff_user_id','user_id')->field('user_id,mobile,nick_name');
  99. }
  100. /**
  101. * 关联物流公司表
  102. * @return \think\model\relation\BelongsTo
  103. */
  104. public function express()
  105. {
  106. $module = self::getCalledModule();
  107. return $this->belongsTo("app\\{$module}\\model\\Express");
  108. }
  109. /**
  110. * 关联物流公司表
  111. * @return \think\model\relation\BelongsTo
  112. */
  113. public function userRiceCard()
  114. {
  115. $module = self::getCalledModule();
  116. return $this->belongsTo("app\\{$module}\\model\\card\\userRiceCard",'rice_card_id','id');
  117. }
  118. /**
  119. * 关联优惠券信息
  120. * @return \think\model\relation\belongsTo
  121. */
  122. public function coupon()
  123. {
  124. $module = self::getCalledModule();
  125. return $this->belongsTo("app\\{$module}\\model\\UserCoupon",'coupon_id','user_coupon_id');
  126. }
  127. /**
  128. * 订单状态文字描述
  129. * @param $value
  130. * @param $data
  131. * @return string
  132. */
  133. public function getStateTextAttr($value, $data)
  134. {
  135. // 订单状态
  136. // if (in_array($data['order_status'], [OrderStatusEnum::CANCELLED, OrderStatusEnum::COMPLETED])) {
  137. // $orderStatus = [OrderStatusEnum::CANCELLED => '已取消', OrderStatusEnum::COMPLETED => '已完成'];
  138. // return $orderStatus[$data['order_status']];
  139. // }
  140. // 订单状态
  141. if (isset($data['order_status']) && $data['order_status'] != OrderStatusEnum::NORMAL) {
  142. return OrderStatusEnum::data()[$data['order_status']]['name'];
  143. }
  144. // 付款状态
  145. if (isset($data['pay_status']) && $data['pay_status'] == PayStatusEnum::PENDING) {
  146. return '待支付';
  147. }
  148. // 订单类型:单独购买
  149. if (isset($data['delivery_status']) && $data['delivery_status'] == 10) {
  150. return '待收货'; //待收货未发货
  151. // return '已付款待发货'; //待收货未发货
  152. }
  153. if (isset($data['receipt_status']) && $data['receipt_status'] == 10) {
  154. return '待收货'; //待收货已发货
  155. // return '已付款待收货'; //待收货未发货
  156. }
  157. return $value;
  158. }
  159. /**
  160. * 订单状态码
  161. * @param $value
  162. * @param $data
  163. * @return string
  164. */
  165. public function getStateValueAttr($value, $data)
  166. {
  167. // 订单状态
  168. // if (in_array($data['order_status'], [OrderStatusEnum::CANCELLED, OrderStatusEnum::COMPLETED])) {
  169. // $orderStatus = [OrderStatusEnum::CANCELLED => '已取消', OrderStatusEnum::COMPLETED => '已完成'];
  170. // return $orderStatus[$data['order_status']];
  171. // }
  172. // 订单状态
  173. if (isset($data['order_status']) && $data['order_status'] != OrderStatusEnum::NORMAL) {
  174. return OrderStatusEnum::data()[$data['order_status']]['value'];
  175. }
  176. // 付款状态
  177. if (isset($data['pay_status']) && $data['pay_status'] == PayStatusEnum::PENDING) {
  178. return 11;//'待支付';
  179. }
  180. // 订单类型:单独购买
  181. if (isset($data['delivery_status']) && $data['delivery_status'] == 10) {
  182. return 12;//'待发货';
  183. }
  184. if (isset($data['receipt_status']) && $data['receipt_status'] == 10) {
  185. return 13;//'待收货';
  186. }
  187. return $value;
  188. }
  189. /**
  190. * 获取器:订单金额(含优惠折扣)
  191. * @param $value
  192. * @param $data
  193. * @return string
  194. */
  195. public function getOrderPriceAttr($value, $data)
  196. {
  197. // 兼容旧数据:订单金额
  198. if ($value == 0) {
  199. return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']);
  200. }
  201. return $value;
  202. }
  203. /**
  204. * 改价金额(差价)
  205. * @param $value
  206. * @return array
  207. */
  208. public function getUpdatePriceAttr($value)
  209. {
  210. return [
  211. 'symbol' => $value < 0 ? '-' : '+',
  212. 'value' => sprintf('%.2f', abs($value))
  213. ];
  214. }
  215. /**
  216. * 付款时间
  217. * @param $value
  218. * @return false|string
  219. */
  220. public function getPayTimeAttr($value)
  221. {
  222. return format_time($value);
  223. }
  224. /**
  225. * 发货时间
  226. * @param $value
  227. * @return false|string
  228. */
  229. public function getDeliveryTimeAttr($value)
  230. {
  231. return format_time($value);
  232. }
  233. /**
  234. * 收货时间
  235. * @param $value
  236. * @return false|string
  237. */
  238. public function getReceiptTimeAttr($value)
  239. {
  240. return format_time($value);
  241. }
  242. /**
  243. * 提货时间
  244. */
  245. public function getPickupTimeAttr($value)
  246. {
  247. return format_time($value);
  248. }
  249. /**
  250. * 提货截止时间
  251. */
  252. public function getPickupDeadlineAttr($value)
  253. {
  254. return format_time($value);
  255. }
  256. /**
  257. * 生成订单号
  258. * @return string
  259. */
  260. public function orderNo(string $prefix='rw')
  261. {
  262. return OrderService::createOrderNo($prefix);
  263. }
  264. /**
  265. * 生成核销码
  266. * @return string
  267. */
  268. public function hxCode()
  269. {
  270. return OrderService::createHxCode("H");
  271. }
  272. /**
  273. * 订单详情
  274. * @param $where
  275. * @param array $with
  276. * @return array|null|static
  277. */
  278. public static function detail($where, $with = [])
  279. {
  280. is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where;
  281. return self::get($filter, $with);
  282. }
  283. /**
  284. * 批量获取订单列表
  285. * @param $orderIds
  286. * @param array $with
  287. * @return array
  288. * @throws \think\db\exception\DataNotFoundException
  289. * @throws \think\db\exception\DbException
  290. * @throws \think\db\exception\ModelNotFoundException
  291. */
  292. public function getListByIds(array $orderIds, array $with = [])
  293. {
  294. $data = $this->getListByInArray('order_id', $orderIds, $with);
  295. return helper::arrayColumn2Key($data, 'order_id');
  296. }
  297. /**
  298. * 批量获取订单列表
  299. * @param $field
  300. * @param $data
  301. * @param array $with
  302. * @return \think\Collection
  303. * @throws \think\db\exception\DataNotFoundException
  304. * @throws \think\db\exception\DbException
  305. * @throws \think\db\exception\ModelNotFoundException
  306. */
  307. private function getListByInArray($field, $data, $with = [])
  308. {
  309. return $this->with($with)
  310. ->where($field, 'in', $data)
  311. ->where('is_delete', '=', 0)
  312. ->select();
  313. }
  314. /**
  315. * 根据订单号批量查询
  316. * @param $orderNos
  317. * @param array $with
  318. * @return \think\Collection
  319. * @throws \think\db\exception\DataNotFoundException
  320. * @throws \think\db\exception\DbException
  321. * @throws \think\db\exception\ModelNotFoundException
  322. */
  323. public function getListByOrderNos($orderNos, $with = [])
  324. {
  325. return $this->getListByInArray('order_no', $orderNos, $with);
  326. }
  327. /**
  328. * 批量更新订单
  329. * @param $orderIds
  330. * @param $data
  331. * @return false|int
  332. */
  333. public function onBatchUpdate($orderIds, $data)
  334. {
  335. return static::updateBase($data, [['order_id', 'in', $orderIds]]);
  336. }
  337. /**
  338. * 提货截止时间:提交订单2天内 单位:分钟
  339. */
  340. public static function getPickupDeadline()
  341. {
  342. if (env('SERVE_ENV') == 'test') {
  343. return 5;
  344. }
  345. // return 2*24*60; v2.1.39改版 用户下单(门店自提)后超时未核销将自动退款。自动退款时间从48小时延长到7天(168小时)
  346. return 7*24*60;
  347. }
  348. /**
  349. * 申请开票截止时间:订单付款后的40天内可以申请开票 单位:分钟
  350. */
  351. public static function getInvoiceDeadline()
  352. {
  353. if (env('SERVE_ENV') == 'test') {
  354. return 5;
  355. }
  356. return 40*24*60;
  357. }
  358. /**
  359. * 自动评价n天以前的的已确认收货的订单商品 单位:天
  360. */
  361. public static function getCommentDeadline()
  362. {
  363. if (env('SERVE_ENV') == 'test') {
  364. return 1; // 1天
  365. }
  366. return 15;
  367. }
  368. public static function sumSalesVolume($orderIds){
  369. if(!count($orderIds))return 0;
  370. $res = self::whereIn('order_id',$orderIds)->field('sum(pay_price + rice_card_money - express_price) as volume')->find();
  371. return $res->volume?:0;
  372. }
  373. /**
  374. * 关联分销数据
  375. * @return \think\model\relation\hasMany
  376. */
  377. public function commissionsDetail()
  378. {
  379. $model = "app\\common\\model\\user\\CommissionsDetail";
  380. return $this->hasMany($model,'order_id','order_id')->order('role','asc');
  381. }
  382. /**
  383. * 关联分销上级
  384. * @return \think\model\relation\BelongsTo
  385. */
  386. public function commissionsUser()
  387. {
  388. return $this->belongsTo('User','staff_user_id','user_id')->bind(['staff_user_name'=>'nick_name']);
  389. }
  390. /**
  391. * 自然渠道
  392. * @param $where
  393. * @return int|mixed
  394. * @throws \think\db\exception\DataNotFoundException
  395. * @throws \think\db\exception\DbException
  396. * @throws \think\db\exception\ModelNotFoundException
  397. */
  398. public function sumNatureSaleVolume($where){
  399. $res = self::where($where)->field('sum(pay_price + rice_card_money - express_price) as volume')->find();
  400. $orderMoney = $res->volume?:0;
  401. //退款金额
  402. $refMoney = OrderRefund::alias('od')
  403. ->where('od.finance_refund',10)
  404. ->whereExists('select 1 from yoshop_order where order_id=od.order_id and staff_user_id=0 and staff_shop_id=1 and pay_status=20 and is_delete=0')
  405. ->sum('refund_money');
  406. return helper::bcsub($orderMoney,$refMoney,2);
  407. }
  408. }