You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
ymww_backend/application/admin/controller/shopro/commission/Order.php

175 lines
6.2 KiB

<?php
namespace app\admin\controller\shopro\commission;
use think\Db;
use think\exception\HttpResponseException;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\commission\Order as OrderModel;
use app\admin\model\shopro\commission\Reward as RewardModel;
use addons\shopro\service\commission\Reward as RewardService;
class Order extends Common
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new OrderModel();
}
/**
* 查看
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$list = $this->model->sheepFilter()->with(['buyer', 'agent', 'order', 'rewards.agent', 'order_item'])->paginate($this->request->param('list_rows', 10));
$list = $list->toArray();
// 统计数据
$count = [
'total' => $list['total'],
'total_amount' => 0,
'total_commission' => 0,
'total_commission_cancel' => 0,
'total_commission_accounted' => 0,
'total_commission_back' => 0,
'total_commission_pending' => 0
];
$orders = $this->model->sheepFilter()->with(['rewards'])->select();
collection($orders)->each(function ($order) use (&$count) {
$count['total_amount'] += $order['amount'];
foreach ($order['rewards'] as $reward) {
$count['total_commission'] += $reward['commission'];
switch ($reward['status']) {
case RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED:
$count['total_commission_accounted'] += $reward['commission'];
break;
case RewardModel::COMMISSION_REWARD_STATUS_BACK:
$count['total_commission_back'] += $reward['commission'];
break;
case RewardModel::COMMISSION_REWARD_STATUS_PENDING:
$count['total_commission_pending'] += $reward['commission'];
break;
case RewardModel::COMMISSION_REWARD_STATUS_CANCEL:
$count['total_commission_cancel'] += $reward['commission'];
break;
}
}
});
$this->success('', null, [
'list' => $list,
'count' => $count
]);
}
/**
* 结算佣金
*
* @return Response
*/
public function confirm()
{
$params = $this->request->only(['commission_reward_id', 'commission_order_id']);
try {
Db::transaction(function () use ($params) {
$rewardService = new RewardService('admin');
if (isset($params['commission_reward_id'])) {
return $rewardService->runCommissionReward($params['commission_reward_id']);
} elseif (isset($params['commission_order_id'])) {
return $rewardService->runCommissionRewardByOrder($params['commission_order_id']);
}
});
} catch (HttpResponseException $e) {
$data = $e->getResponse()->getData();
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
$this->error($message);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功');
}
/**
* 取消结算
*
* @return Response
*/
public function cancel()
{
$params = $this->request->only(['commission_reward_id', 'commission_order_id']);
try {
Db::transaction(function () use ($params) {
$rewardService = new RewardService('admin');
if (isset($params['commission_reward_id'])) {
return $rewardService->cancelCommissionReward($params['commission_reward_id']);
} elseif (isset($params['commission_order_id'])) {
return $rewardService->backCommissionRewardByOrder($params['commission_order_id']);
}
});
} catch (HttpResponseException $e) {
$data = $e->getResponse()->getData();
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
$this->error($message);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功');
}
/**
* 退回已结算佣金
*/
public function back()
{
$params = $this->request->only(['commission_reward_id', 'commission_order_id', 'deduct_order_money']);
try {
Db::transaction(function () use ($params) {
$rewardService = new RewardService('admin');
if (isset($params['commission_reward_id'])) {
return $rewardService->backCommissionReward($params['commission_reward_id']);
} elseif (isset($params['commission_order_id'])) {
return $rewardService->backCommissionRewardByOrder($params['commission_order_id'], $params['deduct_order_money']);
}
});
} catch (HttpResponseException $e) {
$data = $e->getResponse()->getData();
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
$this->error($message);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success('操作成功');
}
/**
* 修改待结算佣金
*/
public function edit($id = null)
{
$params = $this->request->only(['commission_reward_id', 'commission']);
$reward = RewardModel::get($params['commission_reward_id']);
if (!$reward) {
$this->error(__('No Results were found'));
}
$reward->commission = $params['commission'];
$result = $reward->save();
if ($result) {
$this->success('操作成功');
}
$this->error('操作失败');
}
}