IncomeController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/27
  6. * Time: 下午10:15
  7. */
  8. namespace app\frontend\modules\finance\controllers;
  9. use app\common\models\MemberShopInfo;
  10. use app\common\components\ApiController;
  11. use app\common\facades\Setting;
  12. use app\common\models\Income;
  13. use app\common\services\finance\IncomeService;
  14. use app\frontend\modules\member\models\MemberBankCard;
  15. use Illuminate\Support\Facades\Config;
  16. use Illuminate\Support\Facades\Log;
  17. use app\common\models\Order;
  18. class IncomeController extends ApiController
  19. {
  20. protected $pageSize = 20;
  21. /**
  22. * 收入提现页面,提现按钮控制,todo 需要修改 2018-06-29
  23. *
  24. * @return \Illuminate\Http\JsonResponse
  25. */
  26. public function getIncomeWithdrawMode()
  27. {
  28. $incomeWithdrawMode = (new IncomeService())->withdrawButton(request()->income_type);
  29. if ($incomeWithdrawMode) {
  30. return $this->successJson('获取数据成功!', $incomeWithdrawMode);
  31. }
  32. return $this->errorJson('未检测到数据!');
  33. }
  34. /**
  35. * @return \Illuminate\Http\JsonResponse
  36. */
  37. public function getIncomeCount()
  38. {
  39. //todo 为了获取一个数据重复调用此方法,效率地下,需要重构 2018-01-05-YITIAN
  40. $status = \YunShop::request()->status;
  41. $incomeModel = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId())->get();
  42. if ($status !== null && $status >= '0') {
  43. $incomeModel = $incomeModel->where('status', $status);
  44. }
  45. $config = \app\common\modules\shop\ShopConfig::current()->get('plugin');
  46. $incomeData['total'] = [
  47. 'title' => '推广收入',
  48. 'type' => 'total',
  49. 'type_name' => '推广佣金',
  50. 'income' => $incomeModel->sum('amount')
  51. ];
  52. foreach ($config as $key => $item) {
  53. $typeModel = $incomeModel->where('incometable_type', $item['class']);
  54. $incomeData[$key] = [
  55. 'title' => $item['title'],
  56. 'ico' => $item['ico'],
  57. 'type' => $item['type'],
  58. 'type_name' => $item['title'],
  59. 'income' => $typeModel->sum('amount')
  60. ];
  61. if ($item['agent_class']) {
  62. $agentModel = $item['agent_class']::{$item['agent_name']}(\YunShop::app()->getMemberId());
  63. if ($item['agent_status']) {
  64. $agentModel = $agentModel->where('status', 1);
  65. }
  66. //推广中心显示
  67. if (!$agentModel) {
  68. $incomeData[$key]['can'] = false;
  69. } else {
  70. $agent = $agentModel->first();
  71. if ($agent) {
  72. $incomeData[$key]['can'] = true;
  73. } else {
  74. $incomeData[$key]['can'] = false;
  75. }
  76. }
  77. } else {
  78. $incomeData[$key]['can'] = true;
  79. }
  80. }
  81. if ($incomeData) {
  82. return $this->successJson('获取数据成功!', $incomeData);
  83. }
  84. return $this->errorJson('未检测到数据!');
  85. }
  86. /**
  87. * @return \Illuminate\Http\JsonResponse
  88. */
  89. public function getIncomeList()
  90. {
  91. $configs = \app\backend\modules\income\Income::current()->getItems();
  92. $type = \YunShop::request()->income_type;
  93. $search = [];
  94. foreach ($configs as $key => $config) {
  95. if ($config['type'] == $type) {
  96. $search['type'] = $config['class'];
  97. break;
  98. }
  99. }
  100. $swich = \app\common\models\MemberRelation::uniacid()->select('share_page_deail')->first();
  101. $search['select'] = $swich->share_page_deail;
  102. $incomeModel = Income::getIncomesList($search)->where('amount','>',0)->where('member_id', \YunShop::app()->getMemberId())->paginate($this->pageSize);
  103. if ($swich->share_page_deail) {
  104. $incomeModel = $this->OrderUserDetails($incomeModel);
  105. }
  106. if ($incomeModel) {
  107. return $this->successJson('获取数据成功!', $incomeModel);
  108. }
  109. return $this->errorJson('未检测到数据!');
  110. }
  111. public function OrderUserDetails($incomeModel)
  112. {
  113. $set = [];
  114. if ($incomeModel) {
  115. $income = $incomeModel->toArray();
  116. }
  117. foreach ($income['data'] as $key => $value) {
  118. if ($value['incometable_type'] == 'Yunshop\Mryt\common\models\OrderTeamAward') { //团队管理奖
  119. $teamIds[] = $value['incometable_id'];
  120. }
  121. if ($value['incometable_type'] == 'Yunshop\Micro\common\models\MicroShopBonusLog') { //微店分红
  122. $microIds [] = $value['incometable_id'];
  123. }
  124. }
  125. if ($teamIds) {
  126. if (class_exists('Yunshop\Mryt\common\models\OrderTeamAward')) {
  127. $teamModel = \Yunshop\Mryt\common\models\OrderTeamAward::whereIn('id', $teamIds)->get();
  128. foreach ($teamModel as $key => $value) {
  129. foreach ($income['data'] as $k => $v) {
  130. if ($value['incometable_id'] == $v->id and $v['incometable_type'] == 'Yunshop\Mryt\common\models\OrderTeamAward') {
  131. $income['data'][$k]['order_sn'] = $value->log_id;
  132. }
  133. }
  134. $set[] = $value->log_id;
  135. }
  136. }
  137. }
  138. if ($microIds) {
  139. if (class_exists('Yunshop\Micro\common\models\MicroShopBonusLog')) {
  140. $microModel = \Yunshop\Micro\common\models\MicroShopBonusLog::whereIn('id', $microIds)->get();
  141. foreach ($microModel as $key => $value) {
  142. foreach ($income['data'] as $k => $v) {
  143. if ($value['incometable_id'] == $v->id and $v['incometable_type'] == 'Yunshop\Micro\common\models\MicroShopBonusLog') {
  144. $income['data'][$k]['order_sn'] = $value->order_sn;
  145. }
  146. }
  147. $set[] = $value->order_sn;
  148. }
  149. }
  150. }
  151. $detail = array_column($income['data'], 'detail');
  152. foreach ($detail as $key => $value) {
  153. if ($value) {
  154. $arr = json_decode($value);
  155. $set[] = $arr->order->data[0]->value;
  156. $income['data'][$key]['order_sn'] = $arr->order->data[0]->value;
  157. }
  158. unset($income['data'][$key]['detail']);
  159. }
  160. $order = Order::whereIn('order_sn', $set)->get();
  161. $income['data'] = collect($income['data'])->map(function ($item) use ($order) {
  162. if ($item['order_sn']) {
  163. foreach ($order as $key => $value) {
  164. if ($value->order_sn == $item['order_sn']) {
  165. $item['type_name'] = '会员ID' . $value->uid . '【' . $value->belongsToMember->nickname . '】' . $item['type_name'];
  166. }
  167. }
  168. }
  169. return $item;
  170. });
  171. return $income;
  172. }
  173. /**
  174. * @return \Illuminate\Http\JsonResponse|string
  175. */
  176. public function getDetail()
  177. {
  178. $data = "";
  179. $id = \YunShop::request()->id;
  180. $detailModel = Income::getDetailById($id)->first();
  181. if ($detailModel) {
  182. if ($detailModel->detail != '') {
  183. $data .= $detailModel->detail;
  184. //TODO 防止数据库json未转义缺少斜杆 后期修改 时间段在2018年10月18号到10月30号出现乱码问题,原因:经销商和分销存入数据库未转义
  185. $pattern1 = '/\\\u[\d|\w]{4}/';
  186. preg_match($pattern1, $data, $exists);
  187. if (empty($exists)) {
  188. $pattern2 = '/(u[\d|\w]{4})/';
  189. $data = preg_replace($pattern2, '\\\$1', $data);
  190. }
  191. return $this->successJson('成功', json_decode($data, true));
  192. }
  193. return $this->successJson('成功', []);
  194. }
  195. return $this->errorJson('未检测到数据!');
  196. }
  197. public function getLangTitle($data)
  198. {
  199. $lang = Setting::get('shop.lang');
  200. $langData = $lang[$lang['lang']];
  201. $titleType = '';
  202. foreach ($langData as $key => $item) {
  203. $names = explode('_', $key);
  204. foreach ($names as $k => $name) {
  205. if ($k == 0) {
  206. $titleType = $name;
  207. } else {
  208. $titleType .= ucwords($name);
  209. }
  210. }
  211. if ($data == $titleType) {
  212. return $item[$key];
  213. }
  214. }
  215. }
  216. /**
  217. * @return \Illuminate\Http\JsonResponse
  218. */
  219. public function getSearchType()
  220. {
  221. $configs = \app\backend\modules\income\Income::current()->getItems();
  222. foreach ($configs as $key => $config) {
  223. if ($config['type'] == 'balance') {
  224. continue;
  225. }
  226. $searchType[] = [
  227. 'title' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $config['title'],
  228. 'type' => $config['type']
  229. ];
  230. }
  231. if ($searchType) {
  232. return $this->successJson('获取数据成功!', $searchType);
  233. }
  234. return $this->errorJson('未检测到数据!');
  235. }
  236. /**
  237. * @return \Illuminate\Http\JsonResponse
  238. */
  239. public function getWithdraw()
  240. {
  241. $incomeSet = \Setting::get('withdraw.income');
  242. $config = \app\backend\modules\income\Income::current()->getItems();
  243. Log::info('获取提现数据');
  244. foreach ($config as $key => $item) {
  245. if ($item['type'] == 'balance') {
  246. continue;
  247. }
  248. $set[$key] = \Setting::get('withdraw.' . $key);
  249. $set[$key]['roll_out_limit'] = $set[$key]['roll_out_limit'] ? $set[$key]['roll_out_limit'] : 0;
  250. $set[$key]['poundage_rate'] = $set[$key]['poundage_rate'] ? $set[$key]['poundage_rate'] : 0;
  251. $incomeModel = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId());
  252. $incomeModel = $incomeModel->where('status', '0');
  253. $incomeModel = $incomeModel->where('incometable_type', $item['class']);
  254. $amount = $incomeModel->sum('amount');
  255. $poundage = $incomeModel->sum('amount') / 100 * $set[$key]['poundage_rate'];
  256. $poundage = sprintf("%.2f", $poundage);
  257. //劳务税
  258. $servicetax = 0;
  259. if ($incomeSet['servicetax_rate'] && ($item['type'] != 'StoreCashier')) {
  260. $servicetax = ($amount - $poundage) / 100 * $incomeSet['servicetax_rate'];
  261. $servicetax = sprintf("%.2f", $servicetax);
  262. }
  263. Log::info($this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title']);
  264. if (($amount > 0) && (bccomp($amount, $set[$key]['roll_out_limit'], 2) != -1)) {
  265. $type_id = '';
  266. foreach ($incomeModel->get() as $ids) {
  267. $type_id .= $ids->id . ",";
  268. }
  269. $incomeData[] = [
  270. 'type' => $item['class'],
  271. 'key_name' => $item['type'],
  272. 'type_name' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title'],
  273. 'type_id' => rtrim($type_id, ','),
  274. 'income' => $incomeModel->sum('amount'),
  275. 'poundage' => $poundage,
  276. 'poundage_rate' => $set[$key]['poundage_rate'],
  277. 'servicetax' => $servicetax,
  278. 'servicetax_rate' => $incomeSet['servicetax_rate'] ? $incomeSet['servicetax_rate'] : 0,
  279. 'can' => true,
  280. 'roll_out_limit' => $set[$key]['roll_out_limit'],
  281. 'selected' => true,
  282. ];
  283. } else {
  284. $incomeData[] = [
  285. 'type' => $item['class'],
  286. 'key_name' => $item['type'],
  287. 'type_name' => $this->getLangTitle($key) ? $this->getLangTitle($key) : $item['title'],
  288. 'type_id' => '',
  289. 'income' => $incomeModel->sum('amount'),
  290. 'poundage' => $poundage,
  291. 'poundage_rate' => $set[$key]['poundage_rate'],
  292. 'servicetax' => $servicetax,
  293. 'servicetax_rate' => $incomeSet['servicetax_rate'] ? $incomeSet['servicetax_rate'] : 0,
  294. 'can' => false,
  295. 'roll_out_limit' => $set[$key]['roll_out_limit'],
  296. 'selected' => false,
  297. ];
  298. }
  299. }
  300. if ($incomeData) {
  301. return $this->successJson('获取数据成功!', $incomeData);
  302. }
  303. return $this->errorJson('未检测到数据!');
  304. }
  305. /**
  306. * @param $type
  307. * @param $typeId
  308. */
  309. public function setIncomeAndOrder($type, $typeId)
  310. {
  311. static::setIncome($type, $typeId);
  312. // static::setCommissionOrder($type, $typeId);
  313. $configs = \app\backend\modules\income\Income::current()->getItems();
  314. foreach ($configs as $config) {
  315. if (isset($config['name']) && ($type == $config['class'])) {
  316. $income = \Yunshop\Commission\models\Income::whereIn('id', explode(',', $typeId))->get();
  317. foreach ($income as $item) {
  318. $config['class']::{$config['name']}([$config['value'] => 1], ['id' => $item->incometable_id]);
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * @param $type
  325. * @param $typeId
  326. */
  327. public function setIncome($type, $typeId)
  328. {
  329. Log::info('setIncome');
  330. $request = Income::updatedWithdraw($type, $typeId, '1');
  331. }
  332. private function getMemberAlipaySet()
  333. {
  334. $array = MemberShopInfo::select('alipay', 'alipayname')->where('member_id', \YunShop::app()->getMemberId())->first();
  335. if ($array && $array['alipay'] && $array['alipayname']) {
  336. return true;
  337. }
  338. return false;
  339. }
  340. }