| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * 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;
- 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()
- {
- $configs = \app\backend\modules\income\Income::current()->getItems();
- $type = \YunShop::request()->income_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'] = $swich->share_page_deail;
- $incomeModel = Income::getIncomesList($search)->where('amount','>',0)->where('member_id', \YunShop::app()->getMemberId())->paginate($this->pageSize);
- if ($swich->share_page_deail) {
- $incomeModel = $this->OrderUserDetails($incomeModel);
- }
- 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;
- }
- }
|