Settlement.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use app\model\BaseModel;
  12. use app\model\order\OrderCommon;
  13. /**
  14. * 门店订单与结算
  15. */
  16. class Settlement extends BaseModel
  17. {
  18. /**
  19. * 店铺门店结算
  20. * @param $site_id
  21. * @param $end_time
  22. * @return array
  23. */
  24. public function settlement($site_id, $end_time)
  25. {
  26. //获取最近的店铺结算时间
  27. $last_time = model('shop')->getInfo([ [ 'site_id', '=', $site_id ] ], 'store_settlement_time');
  28. $start_time = $last_time[ 'store_settlement_time' ];
  29. //查询门店结算配置
  30. $config_model = new Config();
  31. $config = $config_model->getStoreWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
  32. if ($config[ 'is_settlement' ] != 1) {
  33. return $this->success();
  34. }
  35. $period_type = $config[ 'period_type' ];
  36. //结算周期为4的话是立即结算
  37. if ($period_type == 4)
  38. return $this->success();
  39. $period_type = array ( 1 => 'day', 2 => 'week', 3 => 'month' )[ $period_type ] ?? 'day';
  40. //只有开启周期性结算开关,才会进行
  41. if (( $end_time - $start_time ) < ( 3600 * 20 )) {
  42. return $this->success();
  43. }
  44. //店铺列表
  45. $store_list = model('store')->getList([ [ 'site_id', '=', $site_id ] ], 'site_name, store_id, store_name, account');
  46. model('store_settlement')->startTrans();
  47. try {
  48. $store_withdraw_model = new StoreWithdraw();
  49. //循环各个店铺数据
  50. foreach ($store_list as $k => $store) {
  51. $store_id = $store[ 'store_id' ];
  52. $online_settlement = model('order')->getInfo([
  53. [ 'order_status', '=', 10 ],
  54. [ 'is_settlement', '=', 0 ],
  55. [ 'store_id', '=', $store_id ],
  56. [ 'finish_time', '<=', $end_time ],
  57. [ 'pay_type', '<>', 'OFFLINE_PAY' ],//todo 支付方式应该都可以
  58. ], 'sum(pay_money) as order_money, sum(refund_money) as refund_money, sum(commission) as commission');
  59. $offline_settlement = model('order')->getInfo([
  60. [ 'order_status', '=', 10 ],
  61. [ 'is_settlement', '=', 0 ],
  62. [ 'store_id', '=', $store_id ],
  63. [ 'finish_time', '<=', $end_time ],
  64. [ 'pay_type', '=', 'OFFLINE_PAY' ],
  65. ], 'sum(pay_money) as offline_order_money, sum(refund_money) as offline_refund_money');
  66. //周期性结算会将所有抽成全部提现
  67. $store_commission = $store[ 'account' ];
  68. $settlement = [
  69. 'settlement_no' => date('YmdHi') . $store_id . rand(1111, 9999),
  70. 'site_id' => $site_id,
  71. 'site_name' => $store[ 'site_name' ],
  72. 'store_id' => $store_id,
  73. 'store_name' => $store[ 'store_name' ],
  74. 'order_money' => $online_settlement[ 'order_money' ] ?? 0,
  75. 'refund_money' => $online_settlement[ 'refund_money' ] ?? 0,
  76. 'commission' => $online_settlement[ 'commission' ] ?? 0,
  77. 'offline_order_money' => $offline_settlement[ 'offline_order_money' ] ?? 0,
  78. 'offline_refund_money' => $offline_settlement[ 'offline_refund_money' ] ?? 0,
  79. 'create_time' => $end_time,
  80. 'start_time' => $start_time,
  81. 'end_time' => $end_time,
  82. 'store_commission' => $store_commission
  83. ];
  84. // 没有可提现金额的时候,关联的提现id为0, 默认认为已经结算提现成功
  85. if ($store_commission > 0) {
  86. //门店主动提现所有分成
  87. $apply_params = array (
  88. 'site_id' => $site_id,
  89. 'store_id' => $store_id,
  90. 'money' => $store_commission,
  91. 'settlement_type' => $period_type
  92. );
  93. $withdraw_result = $store_withdraw_model->apply($apply_params);
  94. $withdraw_id = $withdraw_result[ 'data' ];
  95. $settlement[ 'withdraw_id' ] = $withdraw_id;
  96. }
  97. $store_settlement_id = model('store_settlement')->add($settlement);
  98. model('order')->update([ 'store_settlement_id' => $store_settlement_id, 'is_settlement' => 1 ], [
  99. [ 'order_status', '=', 10 ],
  100. [ 'is_settlement', '=', 0 ],
  101. [ 'store_id', '=', $store_id ],
  102. [ 'finish_time', '<=', $end_time ],
  103. // ['pay_type', 'not in', OrderCommon::$online_pay_type],
  104. ]);
  105. }
  106. model('shop')->update([ 'store_settlement_time' => $end_time ], [ [ 'site_id', '=', $site_id ] ]);
  107. model('store_settlement')->commit();
  108. return $this->success();
  109. } catch (\Exception $e) {
  110. model('store_settlement')->rollback();
  111. return $this->error($e->getMessage());
  112. }
  113. }
  114. /**
  115. * getSettlementInfo 获取详情
  116. * @param $condition
  117. * @param string $fields
  118. * @return array
  119. */
  120. public function getSettlementInfo($condition, $fields = '*')
  121. {
  122. $res = model('store_settlement')->getInfo($condition, $fields);
  123. return $this->success($res);
  124. }
  125. /**
  126. * 修改结算记录
  127. * @param
  128. */
  129. public function editSettlement($data, $condition)
  130. {
  131. $res = model('store_settlement')->update($data, $condition);
  132. return $this->success($res);
  133. }
  134. /**
  135. * 获取店铺待结算订单金额
  136. */
  137. public function getStoreSettlementData($condition = [])
  138. {
  139. $money_info = model('order')->getInfo($condition, 'sum(order_money) as order_money, sum(refund_money) as refund_money, sum(shop_money) as shop_money, sum(platform_money) as platform_money, sum(refund_shop_money) as refund_shop_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission');
  140. if (empty($money_info) || $money_info == null) {
  141. $money_info = [
  142. 'order_money' => 0,
  143. 'refund_money' => 0,
  144. 'shop_money' => 0,
  145. 'platform_money' => 0,
  146. 'refund_shop_money' => 0,
  147. 'refund_platform_money' => 0,
  148. 'commission' => 0
  149. ];
  150. }
  151. return $money_info;
  152. }
  153. /**
  154. * 获取店铺结算周期结算分页列表
  155. * @param array $condition
  156. * @param int $page
  157. * @param int $page_size
  158. * @param string $order
  159. * @param string $field
  160. * @return array
  161. */
  162. public function getStoreSettlementPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  163. {
  164. $list = model('store_settlement')->pageList($condition, $field, $order, $page, $page_size);
  165. return $this->success($list);
  166. }
  167. }