jh-admin/addon/dividemoney/shop/controller/Bill.php

165 lines
6.3 KiB
PHP

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\dividemoney\shop\controller;
use addon\dividemoney\model\DividemoneyBill;
use addon\dividemoney\model\OrderPay;
use addon\dividemoney\model\OrderCreate;
use app\model\system\Cron;
use app\shop\controller\BaseShop;
use think\facade\Cache;
class Bill extends BaseShop
{
/***
* 获取佣金列表
* @return array|mixed
*/
public function lists()
{
$startTime = mktime(0, 0, 0, date('m'), date('d') - 15, date('Y'));
if (request()->isAjax()) {
$bill = new DividemoneyBill();
$search_text = input('keywords', '');
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$status = input('status', 'all');
$condition = [];
$condition[] = ['s.site_id', '=', $this->site_id];
if ($search_text) {
$condition[] = ['b.out_trade_no|b.order_id', 'like', '%' . $search_text . '%'];
}
if ($status != 'all') {
if ($status == 2) {
$condition[] = ['b.states', 'in', [2, 3]];
} elseif ($status == 'falitime') {
$condition[] = ['b.create_time', '<=', $startTime];
$condition[] = ['b.states', 'in', [2, 3]];
} else {
$condition[] = ['b.states', '=', $status];
}
}
//下单时间
$start_time = input('start_time', '');
$end_time = input('end_time', '');
if ($start_time && $end_time) {
$condition[] = ['b.create_time', 'between', [date_to_time($start_time), date_to_time($end_time)]];
} elseif (!$start_time && $end_time) {
$condition[] = ['b.create_time', '<=', date_to_time($end_time)];
} elseif ($start_time && !$end_time) {
$condition[] = ['b.create_time', '>=', date_to_time($start_time)];
}
$res = $bill->getPageList($condition, $page, $page_size);
return $res;
}
$info = event('getDivideAccounts', ['site_id' => $this->site_id]);
$data = [];
if ($info) {
foreach ($info as $v) {
foreach ($v as $item) {
if (isset($data[$item['realname']])) {
$data[$item['realname']]['fee_commission'] += $item['fee_commission'];
} else {
$data[$item['realname']] = $item;
}
}
}
}
$where = [
['site_id', '=', $this->site_id],
['states', 'in', [0, 2, 3]],
];
$count = model('dividemoney_bill')->getCount($where, 'id');
$this->assign('isSettlementState', $count);
//结算失败订单
$failWhere = array_merge($where, [['create_time', '<', $startTime], ['is_order_account_locking', '=', 0]]);
$failMoney = model('dividemoney_bill', false)->getSum($failWhere, 'amount');
$this->assign('faliMoney', $failMoney);
//待结算
$normalWhere = array_merge($where, [['create_time', '>=', $startTime]]);
$normalMoney = model('dividemoney_bill')->getSum($normalWhere, 'amount');
$this->assign('normalMoney', $normalMoney);
$this->assign('a', array_values($data));
$this->assign('accounts', array_values($data));
$this->assign('count', array_sum(array_column(array_values($data), 'fee_commission')));
return $this->fetch('lists/lists');
}
/***
* 发起结算
* @return array
*/
public function settlement()
{
$order_id = input('order_id');
$states = input('states', 0);
try {
if ($order_id) {
return (new OrderPay())->startSplitAccount($order_id, '', $states, true);
}
} catch (\Exception $e) {
return $e->getMessage();
}
return success(0, '刷新成功');
}
/***
* 发起重新结算
* @return void
*/
public function batchSettlement()
{
$startTime = mktime(0, 0, 0, date('m'), date('d') - 15, date('Y'));
$where = [
['site_id', '=', $this->site_id],
['states', 'in', [2, 3]],
['create_time', '<', $startTime]
];
if (!Cache::get('batchSettlement' . $this->site_id)) {
$res = model('dividemoney_bill')->getList($where, 'order_id');
$cron = new Cron();
foreach ($res as $key => $order_info) {
$cron->addCron(1, 1, '重启执行自动分账', 'ResetCronOrderDividemoney', time() + $key * 10, $order_info['order_id']);
}
}
Cache::set('batchSettlement' . $this->site_id, 1, 7200);
return success(0, '刷新成功');
}
/***
* 发起支付
* @return array|mixed
*/
public function pay()
{
$money = input('money', 0);
$pay = new OrderCreate();
if (request()->isAjax()) {
$return = $pay->rechargeOrderCreate($money, $this->uid, $this->site_id);
$qrcode = qrcode($return['data']['data']['qrcode'], 'upload/qrcode/pay/', $return['data']['out_trade_no'], 16);
$return = [
'qrcode' => 'data:image/png;base64,' . base64_encode(file_get_contents($qrcode)),
'out_trade_no' => $return['data']['out_trade_no'],
];
return $return;
}
$qrcode = '';
$out_trade_no = '';
if ($money > 0) {
$res = $pay->OrderCreate($money, $this->site_id, config('accounts.default_site_id', 1));
$qrcode = qrcode($res['data']['data']['qrcode'], 'upload/qrcode/pay/', $res['data']['out_trade_no'], 16);
$qrcode = 'data:image/png;base64,' . base64_encode(file_get_contents($qrcode));
$out_trade_no = $res['data']['out_trade_no'];
}
$this->assign('qrcode', $qrcode);
$this->assign('out_trade_no', $out_trade_no);
return $this->fetch('pay/pay');
}
}