| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com
- * =========================================================
- */
- namespace addon\store\model;
- use app\model\BaseModel;
- use app\model\store\Store;
- use app\model\system\Pay;
- use think\facade\Cache;
- class StoreWithdraw extends BaseModel
- {
- public $status = [
- 0 => '待审核',
- 1 => '待转账',
- 2 => '已转账',
- -1 => '已拒绝',
- ];
- public $settlement_type = array (
- 'apply' => '申请结算',
- 'day' => '每日结算',
- 'week' => '每周结算',
- 'month' => '每月结算',
- );
- /**
- * 门店账户提现
- * @param $params
- */
- public function apply($params)
- {
- $money = $params[ 'money' ] ?? 0;
- $site_id = $params[ 'site_id' ];
- $store_id = $params[ 'store_id' ];
- $settlement_type = $params[ 'settlement_type' ] ?? 'apply';
- $store_config_model = new Config();
- $config = $store_config_model->getStoreWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
- $is_audit = $config[ 'is_audit' ];
- $is_settlement = $config[ 'is_settlement' ];
- if ($is_settlement == 0)
- return $this->error([], '门店结算未开启');
- $withdraw_least = $config[ 'withdraw_least' ];
- $store_model = new Store();
- $store_condition = array (
- [ 'site_id', '=', $site_id ],
- [ 'store_id', '=', $store_id ]
- );
- $store_info = $store_model->getStoreInfo($store_condition)[ 'data' ] ?? [];
- if (empty($store_info))
- return $this->error([], '找不到可结算的门店');
- if ($settlement_type == 'apply') {
- if ($money < $withdraw_least) {
- return $this->error([], '门店最低结算金额为' . $withdraw_least . '元');
- }
- }
- if ($money > $store_info[ 'account' ]) {
- return $this->error([], '申请结算金额不能大于门店最大可结算金额');
- }
- $bank_type = $store_info[ 'bank_type' ];
- if ($bank_type == 0)
- return $this->error([], '当前门店未配置结算账户,无法结算');
- $transfer_type = array ( 3 => 'bank', 2 => 'alipay', 1 => 'wechatpay' )[ $store_info[ 'bank_type' ] ];//转账方式
- $transfer_type_list = $this->getTransferType($site_id);
- $transfer_type_name = $transfer_type_list[ $transfer_type ] ?? '';
- switch ( $transfer_type ) {
- case 'bank':
- $bank_name = $store_info[ 'bank_type_name' ];
- $realname = $store_info[ 'bank_user_name' ];//户头
- $account_number = $store_info[ 'bank_type_account' ];
- break;
- case 'alipay':
- $realname = $store_info[ 'bank_user_name' ];
- $account_number = $store_info[ 'bank_type_account' ];
- break;
- case 'wechatpay':
- $account_number = $store_info[ 'bank_user_name' ];
- break;
- }
- $data = array (
- 'site_id' => $site_id,
- 'withdraw_no' => $this->createWithdrawNo(),
- 'store_name' => $store_info[ 'store_name' ],
- 'store_id' => $store_id,
- 'transfer_type' => $transfer_type,
- 'transfer_type_name' => $transfer_type_name,
- 'money' => $money,
- 'apply_time' => time(),
- 'status' => 0,
- 'status_name' => $this->status[ 0 ],
- 'realname' => $realname ?? '',
- 'bank_name' => $bank_name ?? '',
- 'account_number' => $account_number ?? '',
- 'settlement_type' => $settlement_type,
- 'settlement_type_name' => $this->settlement_type[ $settlement_type ]
- );
- model('store_withdraw')->startTrans();
- try {
- $withdraw_id = model('store_withdraw')->add($data);
- $store_account_model = new StoreAccount();
- $store_account_data = array (
- 'account_data' => -$money,
- 'site_id' => $site_id,
- 'store_id' => $store_id,
- 'from_type' => 'withdraw',
- 'remark' => '门店结算,金额' . $money,
- 'related_id' => $withdraw_id
- );
- $result = $store_account_model->addStoreAccount($store_account_data);
- if ($result[ 'code' ] < 0) {
- model('store_withdraw')->rollback();
- return $result;
- }
- //增加结算中余额
- model('store')->setInc([ [ 'store_id', '=', $store_id ] ], 'account_apply', $money);
- //结算无需审核的话,就直接结算
- if ($is_audit == 0) {
- $result = $this->agree([ 'withdraw_id' => $withdraw_id, 'site_id' => $site_id, 'store_id' => $store_id ]);
- if ($result[ 'code' ] < 0) {
- model('store_withdraw')->rollback();
- return $result;
- }
- }
- model('store_withdraw')->commit();
- return $this->success($withdraw_id);
- } catch (\Exception $e) {
- model('store_withdraw')->rollback();
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * 同意结算申请
- * @param $condition
- */
- public function agree($params)
- {
- $site_id = $params[ 'site_id' ];
- $store_id = $params[ 'store_id' ] ?? 0;
- $withdraw_id = $params[ 'withdraw_id' ];
- $condition = array (
- [ 'site_id', '=', $site_id ],
- [ 'withdraw_id', '=', $withdraw_id ],
- [ 'status', '=', 0 ]
- );
- if ($store_id > 0) {
- $condition[] = [ 'store_id', '=', $store_id ];
- }
- $info = model('store_withdraw')->getInfo($condition);
- if (empty($info))
- return $this->error();
- $data = array (
- 'status' => 1,
- 'status_name' => $this->status[ 1 ],
- 'audit_time' => time(),
- );
- $result = model('store_withdraw')->update($data, $condition);
- return $this->success();
- }
- /**
- * 拒绝结算申请
- * @param $condition
- */
- public function refuse($params)
- {
- $site_id = $params[ 'site_id' ];
- $store_id = $params[ 'store_id' ] ?? 0;
- $withdraw_id = $params[ 'withdraw_id' ];
- $condition = array (
- [ 'site_id', '=', $site_id ],
- [ 'withdraw_id', '=', $withdraw_id ],
- [ 'status', '=', 0 ]
- );
- if ($store_id > 0) {
- $condition[] = [ 'store_id', '=', $store_id ];
- }
- $info = model('store_withdraw')->getInfo($condition);
- if (empty($info))
- return $this->error();
- model('store_withdraw')->startTrans();
- try {
- $data = array (
- 'status' => -1,
- 'status_name' => $this->status[ -1 ],
- 'refuse_reason' => $params[ 'refuse_reason' ],
- 'audit_time' => time(),
- );
- model('store_withdraw')->update($data, $condition);
- $money = $info[ 'money' ];
- //增加现金余额
- $store_id = $info[ 'store_id' ];
- $store_account_model = new StoreAccount();
- $store_account_data = array (
- 'account_data' => $money,
- 'site_id' => $site_id,
- 'store_id' => $store_id,
- 'from_type' => 'withdraw',
- 'remark' => '门店结算被拒绝,返还金额' . $money,
- 'related_id' => $withdraw_id
- );
- $result = $store_account_model->addStoreAccount($store_account_data);
- if ($result[ 'code' ] < 0) {
- model('store_withdraw')->rollback();
- return $result;
- }
- //减少结算中余额
- model('store')->setDec([ [ 'store_id', '=', $store_id ] ], 'account_apply', $money);
- model('store_withdraw')->commit();
- return $this->success();
- } catch (\Exception $e) {
- model('store_withdraw')->rollback();
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * 结算转账完成
- * @param $id
- */
- public function transferFinish($params)
- {
- $site_id = $params[ 'site_id' ];
- $store_id = $params[ 'store_id' ] ?? 0;
- $withdraw_id = $params[ 'withdraw_id' ];
- $condition = array (
- [ 'site_id', '=', $site_id ],
- [ 'withdraw_id', '=', $withdraw_id ]
- );
- if ($store_id > 0) {
- $condition[] = [ 'store_id', '=', $store_id ];
- }
- $info = model('store_withdraw')->getInfo($condition);
- if (empty($info))
- return $this->error();
- $transfer_time = time();
- model('store_withdraw')->startTrans();
- try {
- $store_id = $info[ 'store_id' ];
- $data = [
- 'status' => 2,
- 'status_name' => $this->status[ 2 ],
- 'transfer_time' => $transfer_time,
- 'voucher_img' => $params[ 'voucher_img' ] ?? '',
- 'voucher_desc' => $params[ 'voucher_desc' ] ?? ''
- ];
- model('store_withdraw')->update($data, $condition);
- $store_condition = array (
- [ 'store_id', '=', $store_id ]
- );
- $money = $info[ 'money' ];
- //增加已结算余额
- model('store')->setInc($store_condition, 'account_withdraw', $money);
- //减少结算中余额
- model('store')->setDec($store_condition, 'account_apply', $money);
- model('store_withdraw')->commit();
- return $this->success();
- } catch (\Exception $e) {
- model('store_withdraw')->rollback();
- return $this->error('', $e->getMessage());
- }
- }
- /**
- * 转账方式
- */
- public function getTransferType($site_id = 0)
- {
- $pay_model = new Pay();
- $transfer_type_list = $pay_model->getTransferType($site_id);
- $data = [];
- foreach ($transfer_type_list as $k => $v) {
- $data[ $k ] = $v;
- }
- return $data;
- }
- /**
- * 结算
- * @param $condition
- */
- public function getStoreWithdrawInfo($condition, $field = '*')
- {
- $info = model('store_withdraw')->getInfo($condition, $field);
- return $this->success($info);
- }
- /**
- * 结算记录
- * @param $condition
- * @param string $field
- */
- public function getStoreWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
- {
- $list = model('store_withdraw')->getList($condition, $field, $order, '', '', '', $limit);
- return $this->success($list);
- }
- /**
- * 获取账户分页列表
- * @param array $condition
- * @param int $page
- * @param int $page_size
- * @param string $order
- * @param string $field
- * @return array|\multitype
- */
- public function getStoreWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'apply_time desc', $field = '*', $alias = 'a', $join = [])
- {
- $list = model('store_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
- return $this->success($list);
- }
- /**
- * 获取结算单数量
- * @param $condition
- * @return array
- */
- public function getStoreWithdrawCount($condition)
- {
- $data = model('store_withdraw')->getCount($condition);
- return $this->success($data);
- }
- /**
- * 获取结算单字段总和
- * @param $condition
- * @return array
- */
- public function getStoreWithdrawSum($condition, $field)
- {
- $data = model('store_withdraw')->getSum($condition, $field);
- return $this->success($data);
- }
- /**
- * 结算详情
- * @param $params
- * @return array
- */
- public function detail($params)
- {
- $withdraw_id = $params[ 'withdraw_id' ];
- $site_id = $params[ 'site_id' ] ?? 0;
- $store_id = $params[ 'store_id' ] ?? 0;
- $condition = array (
- [ 'withdraw_id', '=', $withdraw_id ]
- );
- if ($site_id > 0) {
- $condition[] = [ 'site_id', '=', $site_id ];
- }
- if ($store_id > 0) {
- $condition[] = [ 'store_id', '=', $store_id ];
- }
- $info = model('store_withdraw')->getInfo($condition);
- if (empty($info))
- return $this->error();
- $settlement_type = $info[ 'settlement_type' ];
- //非主动申请的结算,会有周期性结算信息
- if ($settlement_type != 'apply') {
- $settlement_model = new Settlement();
- $settlement_condition = array (
- [ 'withdraw_id', '=', $withdraw_id ]
- );
- $settlement_info = $settlement_model->getSettlementInfo($settlement_condition)[ 'data' ] ?? [];
- if (!empty($settlement_info))
- $info[ 'settlement_info' ] = $settlement_info;
- }
- return $this->success($info);
- }
- /**
- * 翻译
- * @param $data
- */
- // public function translate($data){
- // $settlement_type = $data['settlement_type'] ?? '';
- // if(!empty($settlement_type)){
- // $data['settlement_type_name'] = $settlement_type;
- // }
- //
- // return $data;
- // }
- /**
- * 结算流水号
- */
- private function createWithdrawNo()
- {
- $cache = Cache::get('store_withdraw_no' . time());
- if (empty($cache)) {
- Cache::set('niutk' . time(), 1000);
- $cache = Cache::get('store_withdraw_no' . time());
- } else {
- $cache = $cache + 1;
- Cache::set('store_withdraw_no' . time(), $cache);
- }
- $no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
- return $no;
- }
- }
|