StoreAccount.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 think\facade\Db;
  13. class StoreAccount extends BaseModel
  14. {
  15. public $period_types = [ 1, 2, 3 ];//转账周期类型1.天 2. 周 3. 月
  16. public $from_type = [
  17. 'order' => [
  18. 'type_name' => '门店结算',
  19. 'type_url' => '',
  20. ],
  21. 'refund' => [
  22. 'type_name' => '订单退款',
  23. 'type_url' => '',
  24. ],
  25. 'withdraw' => [
  26. 'type_name' => '提现',
  27. 'type_url' => '',
  28. ],
  29. ];
  30. /**
  31. * 获取门店转账设置
  32. */
  33. public function getStoreWithdrawConfig($site_id)
  34. {
  35. $config = new Config();
  36. $res = $config->getStoreWithdrawConfig($site_id);
  37. return $res;
  38. }
  39. /**
  40. * 获取门店待结算订单金额
  41. */
  42. public function getWaitSettlementInfo($store_id)
  43. {
  44. $money_info = model('order')->getInfo([
  45. [ 'store_id', '=', $store_id ],
  46. [ 'order_status', '=', 10 ],
  47. [ 'store_settlement_id', '=', 0 ]
  48. ], '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');
  49. if (empty($money_info) || $money_info == null) {
  50. $money_info = [
  51. 'order_money' => 0,
  52. 'refund_money' => 0,
  53. 'shop_money' => 0,
  54. 'platform_money' => 0,
  55. 'refund_shop_money' => 0,
  56. 'refund_platform_money' => 0,
  57. 'commission' => 0
  58. ];
  59. }
  60. return $money_info;
  61. }
  62. /**
  63. * 门店账户记录操作
  64. * @param $params
  65. */
  66. public function addStoreAccount($params)
  67. {
  68. $site_id = $params[ 'site_id' ];
  69. $store_id = $params[ 'store_id' ];
  70. $account_data = $params[ 'account_data' ];
  71. $remark = $params[ 'remark' ];
  72. $from_type = $params[ 'from_type' ];
  73. $related_id = $params[ 'related_id' ];
  74. $is_limit = $params[ 'is_limit' ] ?? 1;//是否限制不能小于0
  75. model('store_account')->startTrans();
  76. try {
  77. //账户检测
  78. $store_account = Db::name('store')->where([
  79. [ 'store_id', '=', $store_id ],
  80. [ 'site_id', '=', $site_id ]
  81. ])->field('account')->lock(true)->find();
  82. $account_new_data = round((float) $store_account[ 'account' ] + (float) $account_data, 2);
  83. if ($is_limit == 1 && (float) $account_new_data < 0) {
  84. model('store_account')->rollback();
  85. $msg = '账户余额不足';
  86. return $this->error('', $msg);
  87. }
  88. //添加记录
  89. $type_info = $this->from_type[ $from_type ];
  90. $data = array (
  91. 'site_id' => $site_id,
  92. 'store_id' => $store_id,
  93. 'account_data' => $account_data,
  94. 'from_type' => $from_type,
  95. 'type_name' => $type_info[ 'type_name' ],
  96. 'create_time' => time(),
  97. 'remark' => $remark,
  98. 'related_id' => $related_id,
  99. );
  100. model('store_account')->add($data);
  101. //账户更新
  102. model('store')->update([
  103. 'account' => $account_new_data
  104. ], [
  105. [ 'store_id', '=', $store_id ]
  106. ]);
  107. model('store_account')->commit();
  108. return $this->success();
  109. } catch (\Exception $e) {
  110. model('store_account')->rollback();
  111. return $this->error('', $e->getMessage());
  112. }
  113. }
  114. /**
  115. * 获取账户分页列表
  116. * @param array $condition
  117. * @param int $page
  118. * @param int $page_size
  119. * @param string $order
  120. * @param string $field
  121. * @return array|\multitype
  122. */
  123. public function getStoreAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc,id desc', $field = '*', $alias = 'a', $join = [])
  124. {
  125. $list = model('store_account')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  126. return $this->success($list);
  127. }
  128. /**
  129. * 获取账户列表
  130. * @param array $condition
  131. * @param string $field
  132. * @param string $order
  133. * @param null $limit
  134. * @return array|\multitype
  135. */
  136. public function getStoreAccountList($condition = [], $field = '*', $order = '', $limit = null)
  137. {
  138. $list = model('store_account')->getList($condition, $field, $order, '', '', '', $limit);
  139. return $this->success($list);
  140. }
  141. }