bztang-admin/app/frontend/modules/finance/controllers/IncomeController.php

489 lines
20 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Created by PhpStorm.
* Author:
* Date: 2017/3/27
* Time: 下午10:15
*/
namespace app\frontend\modules\finance\controllers;
use app\common\models\MemberShopInfo;
use app\common\components\ApiController;
use app\common\facades\Setting;
use app\common\models\Income;
use app\common\services\finance\IncomeService;
use app\frontend\modules\member\models\MemberBankCard;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use app\common\models\Order;
use Yunshop\Commission\models\CommissionOrder;
use Yunshop\ShareholderDividend\models\ShareholderDividendModel;
use Yunshop\TeamDividend\models\TeamDividendModel;
class IncomeController extends ApiController
{
protected $pageSize = 20;
/**
* 收入提现页面提现按钮控制todo 需要修改 2018-06-29
*
* @return \Illuminate\Http\JsonResponse
*/
public function getIncomeWithdrawMode()
{
$incomeWithdrawMode = (new IncomeService())->withdrawButton(request()->income_type);
if ($incomeWithdrawMode) {
return $this->successJson('获取数据成功!', $incomeWithdrawMode);
}
return $this->errorJson('未检测到数据!');
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function getIncomeCount()
{
//todo 为了获取一个数据重复调用此方法,效率地下,需要重构 2018-01-05-YITIAN
$status = \YunShop::request()->status;
$incomeModel = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId())->get();
if ($status !== null && $status >= '0') {
$incomeModel = $incomeModel->where('status', $status);
}
$config = \app\common\modules\shop\ShopConfig::current()->get('plugin');
$incomeData['total'] = [
'title' => '推广收入',
'type' => 'total',
'type_name' => '推广佣金',
'income' => $incomeModel->sum('amount')
];
foreach ($config as $key => $item) {
$typeModel = $incomeModel->where('incometable_type', $item['class']);
$incomeData[$key] = [
'title' => $item['title'],
'ico' => $item['ico'],
'type' => $item['type'],
'type_name' => $item['title'],
'income' => $typeModel->sum('amount')
];
if ($item['agent_class']) {
$agentModel = $item['agent_class']::{$item['agent_name']}(\YunShop::app()->getMemberId());
if ($item['agent_status']) {
$agentModel = $agentModel->where('status', 1);
}
//推广中心显示
if (!$agentModel) {
$incomeData[$key]['can'] = false;
} else {
$agent = $agentModel->first();
if ($agent) {
$incomeData[$key]['can'] = true;
} else {
$incomeData[$key]['can'] = false;
}
}
} else {
$incomeData[$key]['can'] = true;
}
}
if ($incomeData) {
return $this->successJson('获取数据成功!', $incomeData);
}
return $this->errorJson('未检测到数据!');
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function getIncomeList(){
$uid = \YunShop::app()->getMemberId();
$configs = \app\backend\modules\income\Income::current()->getItems();
$type = \YunShop::request()->income_type;
$contentType = request()->input('content_type');
$search = [];
foreach ($configs as $key => $config) {
if ($config['type'] == $type) {
$search['type'] = $config['class'];
break;
}
}
$swich = \app\common\models\MemberRelation::uniacid()->select('share_page_deail')->first();
$search['select'] = 1;//$swich->share_page_deail;
// 获取明细列表
// $incomeModel = Income::getIncomesList($search)
// ->where('amount','>',0)
// ->where('member_id', \YunShop::app()->getMemberId())
// ->paginate($this->pageSize);
$incomeModel = Income::uniacid()
->select([
'yz_member_income.id',
'yz_member_income.create_month',
'yz_member_income.incometable_type',
'yz_member_income.type_name',
'yz_member_income.amount',
'yz_member_income.created_at',
'yz_member_income.detail',
'yz_member_income.incometable_id',
'yz_team_dividend.type as team_dividend_type',
'yz_commission_order.level_type as commission_type',
'yz_shareholder_dividend.income_type as shareholder_dividend_type',
])
->leftJoin('yz_team_dividend', function ($join){
$join->on('yz_team_dividend.id', 'yz_member_income.incometable_id')
->where('yz_member_income.dividend_code', 2);
})
->leftJoin('yz_commission_order', function ($join) {
$join->on('yz_commission_order.id', 'yz_member_income.incometable_id')
->where('yz_member_income.dividend_code', 1);
})
->leftJoin('yz_shareholder_dividend', function ($join) {
$join->on('yz_shareholder_dividend.id', 'yz_member_income.incometable_id')
->where('yz_member_income.dividend_code', 64);
})
->when($contentType != 'all',function($query) use ($contentType){
if($contentType == 'cultural_space') {
// 文创空间相关
$query->where(function($subQuery){
$subQuery->where(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',2)->whereIn('yz_team_dividend.type',[4,5,6]);
})->orWhere(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',1)->where('yz_commission_order.level_type',1);
})->orWhere(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',64)->where('yz_shareholder_dividend.income_type',1);
})->orWhere(function($threeQuery){
$threeQuery->whereNotIn('yz_member_income.dividend_code',[1,2,64]);
});
});
}
else if($contentType == 'broker') {
// 非文创空间相关
$query->where(function($subQuery){
$subQuery->where(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',2)->whereNotIn('yz_team_dividend.type',[4,5,6]);
})->orWhere(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',1)->where('yz_commission_order.level_type',0);
})->orWhere(function($threeQuery){
$threeQuery->where('yz_member_income.dividend_code',64)->where('yz_shareholder_dividend.income_type',0);
})->orWhere(function($threeQuery){
$threeQuery->whereNotIn('yz_member_income.dividend_code',[1,2,64,201]);
});
});
}
})
->where('yz_member_income.member_id', $uid)
->when(!empty($search['type']),function($query) use ($search){
$query->where('yz_member_income.incometable_type', $search['type']);
})
->orderBy('yz_member_income.id', 'desc')
->paginate($this->pageSize);
if ($swich->share_page_deail) $incomeModel = $this->OrderUserDetails($incomeModel);
// 循环处理
if($incomeModel){
$incomeModel = $incomeModel->toArray();
foreach($incomeModel['data'] as &$incomeItem){
if($incomeItem['commission_type'] == 1) $incomeItem['type_name'] = '生态发展';
else if($incomeItem['team_dividend_type'] == 4) $incomeItem['type_name'] = '生态建设';
else if($incomeItem['team_dividend_type'] == 5) $incomeItem['type_name'] = '生态贡献';
else if($incomeItem['team_dividend_type'] == 6) $incomeItem['type_name'] = '生态服务';
else if($incomeItem['shareholder_dividend_type'] == 1) $incomeItem['type_name'] = '权证兑换';//'加权收益';
// switch($incomeItem['incometable_type']){
// // 分销商相关处理
// case 'Yunshop\Commission\models\CommissionOrder':
// // 0=普通分销商订单1=文创分销商订单
//
// $commissionLevelType = CommissionOrder::where('id',$incomeItem['incometable_id'])->value('level_type');
// if($commissionLevelType == 1) $incomeItem['type_name'] = '生态发展';
// break;
// // 经销商相关处理
// case 'Yunshop\TeamDividend\models\TeamDividendModel':
// // 分红类型0=分红佣金1=平级奖2=感恩奖励3=额外分红4=生态建设5=生态贡献(生态建设平级奖)6=生态服务
// $teamDividendType = TeamDividendModel::where('id',$incomeItem['incometable_id'])->value('type');
// if($teamDividendType == 4) $incomeItem['type_name'] = '生态建设';
// else if($teamDividendType == 5) $incomeItem['type_name'] = '生态贡献';
// else if($teamDividendType == 6) $incomeItem['type_name'] = '生态服务';
// break;
// // 股东分红相关处理
// case 'Yunshop\ShareholderDividend\models\ShareholderDividendModel':
// $shareholderDividendType = ShareholderDividendModel::where('id',$incomeItem['incometable_id'])->value('income_type');
// if($shareholderDividendType == 1) $incomeItem['type_name'] = '加权收益';
// break;
// }
}
}
// 输入成功
if ($incomeModel) return $this->successJson('获取数据成功!', $incomeModel);
return $this->errorJson('未检测到数据!');
}
public function OrderUserDetails($incomeModel)
{
$set = [];
if ($incomeModel) {
$income = $incomeModel->toArray();
}
foreach ($income['data'] as $key => $value) {
if ($value['incometable_type'] == 'Yunshop\Mryt\common\models\OrderTeamAward') { //团队管理奖
$teamIds[] = $value['incometable_id'];
}
if ($value['incometable_type'] == 'Yunshop\Micro\common\models\MicroShopBonusLog') { //微店分红
$microIds [] = $value['incometable_id'];
}
}
if ($teamIds) {
if (class_exists('Yunshop\Mryt\common\models\OrderTeamAward')) {
$teamModel = \Yunshop\Mryt\common\models\OrderTeamAward::whereIn('id', $teamIds)->get();
foreach ($teamModel as $key => $value) {
foreach ($income['data'] as $k => $v) {
if ($value['incometable_id'] == $v->id and $v['incometable_type'] == 'Yunshop\Mryt\common\models\OrderTeamAward') {
$income['data'][$k]['order_sn'] = $value->log_id;
}
}
$set[] = $value->log_id;
}
}
}
if ($microIds) {
if (class_exists('Yunshop\Micro\common\models\MicroShopBonusLog')) {
$microModel = \Yunshop\Micro\common\models\MicroShopBonusLog::whereIn('id', $microIds)->get();
foreach ($microModel as $key => $value) {
foreach ($income['data'] as $k => $v) {
if ($value['incometable_id'] == $v->id and $v['incometable_type'] == 'Yunshop\Micro\common\models\MicroShopBonusLog') {
$income['data'][$k]['order_sn'] = $value->order_sn;
}
}
$set[] = $value->order_sn;
}
}
}
$detail = array_column($income['data'], 'detail');
foreach ($detail as $key => $value) {
if ($value) {
$arr = json_decode($value);
$set[] = $arr->order->data[0]->value;
$income['data'][$key]['order_sn'] = $arr->order->data[0]->value;
}
unset($income['data'][$key]['detail']);
}
$order = Order::whereIn('order_sn', $set)->get();
$income['data'] = collect($income['data'])->map(function ($item) use ($order) {
if ($item['order_sn']) {
foreach ($order as $key => $value) {
if ($value->order_sn == $item['order_sn']) {
$item['type_name'] = '会员ID' . $value->uid . '【' . $value->belongsToMember->nickname . '】' . $item['type_name'];
}
}
}
return $item;
});
return $income;
}
/**
* @return \Illuminate\Http\JsonResponse|string
*/
public function getDetail()
{
$data = "";
$id = \YunShop::request()->id;
$detailModel = Income::getDetailById($id)->first();
if ($detailModel) {
if ($detailModel->detail != '') {
$data .= $detailModel->detail;
//TODO 防止数据库json未转义缺少斜杆 后期修改 时间段在2018年10月18号到10月30号出现乱码问题原因经销商和分销存入数据库未转义
$pattern1 = '/\\\u[\d|\w]{4}/';
preg_match($pattern1, $data, $exists);
if (empty($exists)) {
$pattern2 = '/(u[\d|\w]{4})/';
$data = preg_replace($pattern2, '\\\$1', $data);
}
return $this->successJson('成功', json_decode($data, true));
}
return $this->successJson('成功', []);
}
return $this->errorJson('未检测到数据!');
}
public function getLangTitle($data)
{
$lang = Setting::get('shop.lang');
$langData = $lang[$lang['lang']];
$titleType = '';
foreach ($langData as $key => $item) {
$names = explode('_', $key);
foreach ($names as $k => $name) {
if ($k == 0) {
$titleType = $name;
} else {
$titleType .= ucwords($name);
}
}
if ($data == $titleType) {
return $item[$key];
}
}
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function getSearchType()
{
$configs = \app\backend\modules\income\Income::current()->getItems();
foreach ($configs as $key => $config) {
if ($config['type'] == 'balance') {
continue;
}
$searchType[] = [
'title' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $config['title'],
'type' => $config['type']
];
}
if ($searchType) {
return $this->successJson('获取数据成功!', $searchType);
}
return $this->errorJson('未检测到数据!');
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function getWithdraw()
{
$incomeSet = \Setting::get('withdraw.income');
$config = \app\backend\modules\income\Income::current()->getItems();
Log::info('获取提现数据');
foreach ($config as $key => $item) {
if ($item['type'] == 'balance') {
continue;
}
$set[$key] = \Setting::get('withdraw.' . $key);
$set[$key]['roll_out_limit'] = $set[$key]['roll_out_limit'] ? $set[$key]['roll_out_limit'] : 0;
$set[$key]['poundage_rate'] = $set[$key]['poundage_rate'] ? $set[$key]['poundage_rate'] : 0;
$incomeModel = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId());
$incomeModel = $incomeModel->where('status', '0');
$incomeModel = $incomeModel->where('incometable_type', $item['class']);
$amount = $incomeModel->sum('amount');
$poundage = $incomeModel->sum('amount') / 100 * $set[$key]['poundage_rate'];
$poundage = sprintf("%.2f", $poundage);
//劳务税
$servicetax = 0;
if ($incomeSet['servicetax_rate'] && ($item['type'] != 'StoreCashier')) {
$servicetax = ($amount - $poundage) / 100 * $incomeSet['servicetax_rate'];
$servicetax = sprintf("%.2f", $servicetax);
}
Log::info($this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title']);
if (($amount > 0) && (bccomp($amount, $set[$key]['roll_out_limit'], 2) != -1)) {
$type_id = '';
foreach ($incomeModel->get() as $ids) {
$type_id .= $ids->id . ",";
}
$incomeData[] = [
'type' => $item['class'],
'key_name' => $item['type'],
'type_name' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title'],
'type_id' => rtrim($type_id, ','),
'income' => $incomeModel->sum('amount'),
'poundage' => $poundage,
'poundage_rate' => $set[$key]['poundage_rate'],
'servicetax' => $servicetax,
'servicetax_rate' => $incomeSet['servicetax_rate'] ? $incomeSet['servicetax_rate'] : 0,
'can' => true,
'roll_out_limit' => $set[$key]['roll_out_limit'],
'selected' => true,
];
} else {
$incomeData[] = [
'type' => $item['class'],
'key_name' => $item['type'],
'type_name' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title'],
'type_id' => '',
'income' => $incomeModel->sum('amount'),
'poundage' => $poundage,
'poundage_rate' => $set[$key]['poundage_rate'],
'servicetax' => $servicetax,
'servicetax_rate' => $incomeSet['servicetax_rate'] ? $incomeSet['servicetax_rate'] : 0,
'can' => false,
'roll_out_limit' => $set[$key]['roll_out_limit'],
'selected' => false,
];
}
}
if ($incomeData) {
return $this->successJson('获取数据成功!', $incomeData);
}
return $this->errorJson('未检测到数据!');
}
/**
* @param $type
* @param $typeId
*/
public function setIncomeAndOrder($type, $typeId)
{
static::setIncome($type, $typeId);
// static::setCommissionOrder($type, $typeId);
$configs = \app\backend\modules\income\Income::current()->getItems();
foreach ($configs as $config) {
if (isset($config['name']) && ($type == $config['class'])) {
$income = \Yunshop\Commission\models\Income::whereIn('id', explode(',', $typeId))->get();
foreach ($income as $item) {
$config['class']::{$config['name']}([$config['value'] => 1], ['id' => $item->incometable_id]);
}
}
}
}
/**
* @param $type
* @param $typeId
*/
public function setIncome($type, $typeId)
{
Log::info('setIncome');
$request = Income::updatedWithdraw($type, $typeId, '1');
}
private function getMemberAlipaySet()
{
$array = MemberShopInfo::select('alipay', 'alipayname')->where('member_id', \YunShop::app()->getMemberId())->first();
if ($array && $array['alipay'] && $array['alipayname']) {
return true;
}
return false;
}
}