BonusController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Cache;
  10. use common\helpers\Date;
  11. use common\helpers\Form;
  12. use common\helpers\LoggerTool;
  13. use common\helpers\Tool;
  14. use common\helpers\user\Balance;
  15. use common\helpers\user\Info;
  16. use common\helpers\user\Perf;
  17. use common\models\CalcBonus;
  18. use common\models\CalcBonusBSDefault;
  19. use common\models\CalcBonusBT;
  20. use common\models\CalcBonusFL;
  21. use common\models\Countries;
  22. use common\models\Currency;
  23. use common\models\DealType;
  24. use common\models\DeclarationLevel;
  25. use common\models\EliteLevel;
  26. use common\models\FlowBonus;
  27. use common\models\FlowCF;
  28. use common\models\FlowGaragePoints;
  29. use common\models\FlowLX;
  30. use common\models\FlowReconsumePoints;
  31. use common\models\FlowTourismPoints;
  32. use common\models\FlowVillaPoints;
  33. use common\models\FlowWallet;
  34. use common\models\forms\TransferForm;
  35. use common\models\forms\UploadForm;
  36. use common\models\forms\WithdrawForm;
  37. use common\models\InvoiceAudit;
  38. use common\models\PerfMonth;
  39. use common\models\PerfPeriod;
  40. use common\models\Period;
  41. use common\models\DecRole;
  42. use common\models\EmployLevel;
  43. use common\models\FlowExchangePoints;
  44. use common\models\RegType;
  45. use common\models\ScoreMonth;
  46. use common\models\Transfer;
  47. use common\models\Uploads;
  48. use common\models\UserBonus;
  49. use common\models\UserInfo;
  50. use common\models\UserPerformance;
  51. use common\models\UserWallet;
  52. use common\models\UserRelation;
  53. use common\models\Withdraw;
  54. use frontendApi\modules\v1\models\User;
  55. use Yii;
  56. use yii\web\HttpException;
  57. use yii\web\UploadedFile;
  58. class BonusController extends BaseController {
  59. public $modelClass = CalcBonus::class;
  60. /**
  61. * 我的账户
  62. * @return mixed
  63. * @throws \yii\web\HttpException
  64. */
  65. public function actionIndex() {
  66. $userId = \Yii::$app->user->id;
  67. $data = UserBonus::findUseSlaves()->select('BONUS')->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  68. if (!$data) {
  69. $data = [
  70. 'BONUS' => 0,
  71. ];
  72. }
  73. $data['CASH'] = 0;
  74. $cashWallet = UserWallet::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'CASH');
  75. if($cashWallet){
  76. $data['CASH'] = $cashWallet['CASH'];
  77. }
  78. $user = User::getEnCodeInfo($userId);
  79. // 账户币种
  80. $country = Countries::getById($user['COUNTRY_ID']);
  81. $currency = Currency::getById($country['LOCAL_CURRENCY_ID']);
  82. // 绩效奖金
  83. $userPerformance = UserPerformance::getAmountByUserId($userId);
  84. $data['userPerformanceBonus'] = $userPerformance['AMOUNTS'] ?? 0;
  85. $wallet[] = ['walletType' => 'bonus', 'walletName' => Yii::t('app', 'memberBonus'), 'amount' => Tool::formatPrice($data['BONUS'])];//会员奖金
  86. $wallet[] = ['walletType' => 'cash', 'walletName' => Yii::t('app', 'memberEcoin'), 'amount' => Tool::formatPrice($data['CASH']), 'coin' => $currency['CODE']];//会员余额
  87. $wallet[] = ['walletType' => 'userPerformanceBonus', 'walletName' => Yii::t('app', 'userPerformanceBonus'), 'amount' => Tool::formatPrice($data['userPerformanceBonus']), 'coin' => $currency['CODE']];//绩效奖金
  88. $dealSwitch = isset(Cache::getSystemConfig()['dealSwitch']) ? Cache::getSystemConfig()['dealSwitch']['VALUE'] : '';
  89. return static::notice(['wallet' => $wallet,'dealSwitch'=>$dealSwitch]);
  90. }
  91. /**
  92. * 团队查询
  93. * @return mixed
  94. * @throws \yii\web\HttpException
  95. */
  96. public function actionTeams() {
  97. $userId = \Yii::$app->user->id;
  98. $period = Period::instance();
  99. // 查询最新一期未挂网期数
  100. $periodNum = $period->getTeamsPeriodNum();
  101. $month = $period->getTeamsYearMonth($periodNum);
  102. // 判断此业绩期是否已经计算完成
  103. if ($period['IS_PREPARE'] != 4) {
  104. return static::notice(['user' => [],'team'=>[]]);
  105. }
  106. $userInfo = User::getEnCodeInfo($userId);
  107. $data = PerfMonth::fetchMonthPerf($month, $userId);
  108. $user[0] = [
  109. 'number' => $userInfo['USER_NAME'],
  110. 'name' => $userInfo['REAL_NAME'],
  111. 'user_perf' => 0, // 个人业绩
  112. 'team_perf' => 0, // 团队新增累计业绩
  113. 'total_perf' => 0, // 合计业绩
  114. 'perf_status' => '0', // 0 未达标 1为已达标
  115. 'perf_status_name' => 'Fail',
  116. ];
  117. if (!empty($data)) {
  118. $userCheck = PerfMonth::checkStatus($data['PV_PCS']+$data['PV_PSS']);
  119. $user[0]['user_perf'] = $data['PV_PCS'];
  120. $user[0]['team_perf'] = $data['PV_PSS'];
  121. $user[0]['total_perf'] = $data['PV_PCS']+$data['PV_PSS'];
  122. $user[0]['perf_status'] = $userCheck ? '1' : $user[0]['perf_status'];
  123. $user[0]['perf_status_name'] = $userCheck ? 'Pass' : $user[0]['perf_status_name'];
  124. }
  125. $teamInfo = [];
  126. $calcAt = PerfMonth::find()->select(['CREATED_AT'])->where('CALC_MONTH=:CALC_MONTH', ['CALC_MONTH'=>$month])->asArray()->one();
  127. if(!$calcAt){
  128. $periodStartTime = $period->nowPeriodArr['START_TIME'];
  129. return static::notice(['user' => $user,'team'=>[],'calcAt' => $periodStartTime]);
  130. }
  131. // 查询此用户的推荐(开拓)团队一级信息
  132. $relation = UserRelation::getChildrenWithDeepAndLayer($userId, 1, 1, $periodNum);
  133. $userStatusFlag = false;
  134. if ($relation) {
  135. // 循环一级开拓用户
  136. foreach($relation as $k=>$v) {
  137. // 获取此用户预计算月业绩
  138. $relationPerf = PerfMonth::fetchMonthPerf($month, $v['USER_ID']);
  139. if (empty($relationPerf)) {
  140. $relationPerf['PV_PCS'] = 0;
  141. $relationPerf['PV_PSS'] = 0;
  142. }
  143. $relationCheck = PerfMonth::checkStatus($relationPerf['PV_PCS']+$relationPerf['PV_PSS']);
  144. $teamInfo[]['perf_status'] = $relationCheck ? '1' : '0';
  145. if ($relationCheck) {
  146. $userStatusFlag = true; // 只要有一个达标,则个人就达标
  147. $teamInfo[$k]['number'] = $v['USER_NAME'];
  148. $teamInfo[$k]['name'] = $v['REAL_NAME'];
  149. $teamInfo[$k]['user_perf'] = $relationPerf['PV_PCS'];
  150. $teamInfo[$k]['team_perf'] = $relationPerf['PV_PSS'];
  151. $teamInfo[$k]['total_perf'] = $relationPerf['PV_PCS']+$relationPerf['PV_PSS'];
  152. $teamInfo[$k]['perf_status'] = '1';
  153. $teamInfo[$k]['perf_status_name'] = 'Pass';
  154. } else {
  155. $teamInfo[$k]['number'] = $v['USER_NAME'];
  156. $teamInfo[$k]['name'] = $v['REAL_NAME'];
  157. $teamInfo[$k]['user_perf'] = $relationPerf['PV_PCS'];
  158. $teamInfo[$k]['team_perf'] = $relationPerf['PV_PSS'];
  159. $teamInfo[$k]['total_perf'] = $relationPerf['PV_PCS']+$relationPerf['PV_PSS'];
  160. $teamInfo[$k]['perf_status'] = '0';
  161. $teamInfo[$k]['perf_status_name'] = 'Fail';
  162. }
  163. }
  164. }
  165. if ($userStatusFlag === true) {
  166. $user[0]['perf_status'] = '1';
  167. $user[0]['perf_status_name'] = 'Pass';
  168. }
  169. return static::notice(['user' => $user,'team'=>$teamInfo,'calcAt' => $calcAt['CREATED_AT']]);
  170. }
  171. /**
  172. * 交易记录
  173. * @return mixed
  174. * @throws \yii\web\HttpException
  175. */
  176. public function actionWalletFlow(){
  177. $walletType = \Yii::$app->request->get('walletType');
  178. if(!in_array($walletType,['bonus', 'cash', 'userPerformanceBonus'])) {
  179. return static::notice('walletType error',400);
  180. }
  181. $dealType = \Yii::$app->request->get('dealType');
  182. $createAt = \Yii::$app->request->get('createAt');
  183. $remark = \Yii::$app->request->get('remark');
  184. // 获取可以查看几期流水
  185. $showFlowPeriodNum = Cache::getSystemConfig()['showFlowPeriodNum']['VALUE'];
  186. $periodArr = Period::getNearlyPeriodNum($showFlowPeriodNum);
  187. $condition = ' AND USER_ID=:USER_ID AND PERIOD_NUM>=:PERIOD_NUM_MIN AND PERIOD_NUM<=:PERIOD_NUM_MAX';
  188. $params = [':USER_ID' => \Yii::$app->user->id,':PERIOD_NUM_MIN' => min($periodArr), ':PERIOD_NUM_MAX'=> max($periodArr)];
  189. if($dealType){
  190. if($dealType==1){//增加
  191. $condition.=' AND IS_INCR=1 AND DEAL_TYPE_IS_PRESET=0';
  192. }elseif ($dealType==2){//扣除
  193. $condition.=' AND IS_INCR=0 AND DEAL_TYPE_IS_PRESET=0';
  194. }else if ($walletType != 'cash') {
  195. $condition.=' AND DEAL_TYPE_ID=:DEAL_TYPE';
  196. $params[':DEAL_TYPE'] = $dealType;
  197. }
  198. }
  199. if ($createAt) {
  200. $createAtStart = $createAt[0] ?? '';
  201. $createAtEnd = $createAt[1] ?? '';
  202. if ($createAtStart) {
  203. $condition .= " AND CREATED_AT>:CREATED_START";
  204. $params[':CREATED_START'] = Date::utcToTime($createAtStart);
  205. }
  206. if ($createAtEnd) {
  207. $condition .= " AND CREATED_AT<:CREATED_END";
  208. $params[':CREATED_END'] = Date::utcToTime($createAtEnd)+86399;
  209. }
  210. }
  211. if($remark){
  212. $condition .= " AND REMARK LIKE :REMARK";
  213. $params[':REMARK'] = '%'.$remark.'%';
  214. }
  215. $data = [];
  216. $dealLists=[];
  217. $dealTypes=[];
  218. if($walletType == 'bonus') {
  219. $dealLists = FlowBonus::find()->groupBy('DEAL_TYPE_ID')->select('DEAL_TYPE_ID')->where('USER_ID=:USER_ID',[':USER_ID'=>\Yii::$app->user->id])->asArray()->all();
  220. $data = FlowBonus::lists($condition, $params, [
  221. 'useSlaves' => true,
  222. 'select' => 'AMOUNT,TOTAL,IS_INCR,REMARK,REMARK_IS_SHOW,PERIOD_NUM,CALC_MONTH,CREATED_AT,DEAL_TYPE_ID,DEAL_TYPE_IS_PRESET',
  223. 'orderBy' => 'CREATED_AT DESC,SORT DESC',
  224. ]);
  225. }elseif ($walletType == 'cash'){
  226. $data = FlowWallet::lists($condition, $params, [
  227. 'select' => 'AMOUNT,TOTAL,IS_INCR,REMARK,PERIOD_NUM,CALC_MONTH,CREATED_AT',
  228. 'orderBy' => 'CREATED_AT DESC',
  229. ]);
  230. }else if ($walletType == 'userPerformanceBonus') {
  231. $condition = ' AND USER_ID=:USER_ID AND STATUS_ID<20 AND AMOUNTS>0';
  232. $params = [':USER_ID' => \Yii::$app->user->id];
  233. $data = UserPerformance::lists($condition, $params, [
  234. 'select' => 'ID,USER_ID,AMOUNTS,ORIGINAL,EXPIRED_AT',
  235. 'orderBy' => 'EXPIRED_AT ASC, CREATED_AT ASC',
  236. ]);
  237. } else {
  238. return static::notice('walletType error',400);
  239. }
  240. if($data && $walletType == 'bonus') {
  241. foreach ($data['list'] as $key => $value) {
  242. if ($value['DEAL_TYPE_IS_PRESET'] == 0) {
  243. $data['list'][$key]['DEAL_TYPE_NAME'] = $value['AMOUNT'] > 0 ? Yii::t('app', 'increase') : Yii::t('app', 'reduce');
  244. } else {
  245. $data['list'][$key]['DEAL_TYPE_NAME'] = DealType::getAllTypesForShow()[$value['DEAL_TYPE_ID']]['TYPE_NAME'] ?? '';
  246. }
  247. if ($value['REMARK_IS_SHOW'] == 0) $data['list'][$key]['REMARK'] = '';
  248. }
  249. }
  250. if ($dealLists && $walletType != 'userPerformanceBonus') {
  251. foreach ($dealLists as $key=>$value){
  252. if(!$value['DEAL_TYPE_ID']) continue;
  253. $dealType = DealType::getAllTypesForShow()[$value['DEAL_TYPE_ID']];
  254. if($dealType['IS_PRESET']==0){
  255. $dealTypes['1'] = Yii::t('app', 'increase');// 增加
  256. $dealTypes['2'] = Yii::t('app', 'reduce');// 扣除
  257. }else{
  258. $dealLists[$key]['DEAL_TYPE_NAME'] = $dealType['TYPE_NAME']??'';
  259. $dealTypes[$value['DEAL_TYPE_ID']] = $dealLists[$key]['DEAL_TYPE_NAME'];
  260. }
  261. }
  262. }
  263. $data['dealTypes'] = $dealTypes;
  264. unset($dealTypes);
  265. return static::notice($data);
  266. }
  267. /**
  268. * 最新奖金
  269. * @return mixed
  270. * @throws \yii\db\Exception
  271. * @throws \yii\web\HttpException
  272. */
  273. public function actionNew(){
  274. if(!$periodNum = \Yii::$app->request->get('periodNum')) {
  275. $periodNum = Period::sentMaxPeriodNum();
  276. }
  277. //是否近期期数
  278. $showFlowPeriodNum = Cache::getSystemConfig()['showFlowPeriodNum']['VALUE'];
  279. $periodArr = Period::getNearlySendPeriodNum($showFlowPeriodNum);
  280. if(!in_array($periodNum,$periodArr)) {
  281. return static::notice(Yii::t('app', 'cannotViewThisPeriod'), 400);
  282. }
  283. // 查询奖金
  284. $data = User::find()
  285. ->from(User::tableName().' AS U')
  286. ->join('LEFT JOIN', CalcBonus::tableName() . ' AS B','B.USER_ID = U.ID')
  287. ->select('U.ID AS USER_ID, U.USER_NAME, U.LAST_DEC_LV, B.LAST_EMP_LV, B.LAST_ELITE_LV, B.IS_ACTIVE, B.BONUS_E, B.BONUS_PE, B.BONUS_SE, B.BONUS_LB, B.BONUS_ST, B.BONUS_WB, B.BONUS_CAR, B.BONUS_TOTAL, B.BONUS_PB')
  288. ->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => \Yii::$app->user->id,':PERIOD_NUM'=>$periodNum])
  289. ->asArray()
  290. ->one();
  291. $result = [
  292. 'userName' => $data['USER_NAME'],
  293. 'period' => $periodNum,
  294. 'decLevel' => Cache::getDecLevelConfig()[($data['LAST_DEC_LV'] ?: DeclarationLevel::getDefaultLevelId())]['LEVEL_NAME'],
  295. 'empLevel' => Cache::getEmpLevelConfig()[($data['LAST_EMP_LV'] ?: EmployLevel::getDefaultLevelId())]['LEVEL_NAME'],
  296. 'eliteLevel' => Cache::getEliteLevelConfig()[($data['LAST_ELITE_LV'] ?: EliteLevel::getDefaultLevelId())]['LEVEL_NAME'],
  297. 'isActive' => $data['IS_ACTIVE'] ?? 0,
  298. 'eliteBonus' => Tool::formatPrice($data['BONUS_E']),
  299. 'proEliteBonus' => Tool::formatPrice($data['BONUS_PE']),
  300. 'superEliteBonus' => Tool::formatPrice($data['BONUS_SE']),
  301. 'leaderShipBonus' => Tool::formatPrice($data['BONUS_LB']),
  302. 'stockistBonus' => Tool::formatPrice($data['BONUS_ST']),
  303. 'welcomeBonus' => Tool::formatPrice($data['BONUS_WB']),
  304. 'carBonus' => Tool::formatPrice($data['BONUS_CAR']),
  305. 'totalBonus' => Tool::formatPrice($data['BONUS_TOTAL']),
  306. 'userPerformanceBonus' => Tool::formatPrice($data['BONUS_PB']),
  307. ];
  308. return static::notice($result);
  309. }
  310. /**
  311. * 往期奖金
  312. * @return mixed
  313. * @throws \yii\web\HttpException
  314. */
  315. public function actionOther() {
  316. //获取可以查看几期奖金
  317. $showBonusPeriodNum = Cache::getSystemConfig()['showBonusPeriodNum']['VALUE'];
  318. $calcBonus = CalcBonus::find()->where('USER_ID=:USER_ID AND IS_SENT=1', [':USER_ID' => \Yii::$app->user->id])
  319. ->select('PERIOD_NUM,CALC_MONTH,LAST_DEC_LV,LAST_EMP_LV,LAST_ELITE_LV,BONUS_TOTAL,BONUS_E,BONUS_PE,BONUS_SE,BONUS_LB,BONUS_ST,BONUS_WB,BONUS_CAR,BONUS_PB')
  320. ->limit($showBonusPeriodNum)
  321. ->orderBy('PERIOD_NUM DESC')
  322. ->asArray()
  323. ->all();
  324. $result = [];
  325. foreach ($calcBonus as $data) {
  326. $result[] = [
  327. 'period' => $data['PERIOD_NUM'],
  328. 'eliteBonus' => Tool::formatPrice($data['BONUS_E']),
  329. 'proEliteBonus' => Tool::formatPrice($data['BONUS_PE']),
  330. 'superEliteBonus' => Tool::formatPrice($data['BONUS_SE']),
  331. 'leaderShipBonus' => Tool::formatPrice($data['BONUS_LB']),
  332. 'stockistBonus' => Tool::formatPrice($data['BONUS_ST']),
  333. 'welcomeBonus' => Tool::formatPrice($data['BONUS_WB']),
  334. 'carBonus' => Tool::formatPrice($data['BONUS_CAR']),
  335. 'totalBonus' => Tool::formatPrice($data['BONUS_TOTAL']),
  336. 'userPerformanceBonus' => Tool::formatPrice($data['BONUS_PB']),
  337. ];
  338. }
  339. // 奖金明细开关控制(0 只显示总奖金 1 全部显示)
  340. $flowBonusSwitch = Cache::getSystemConfig()['flowBonusSwitch']['VALUE'] ?? 0;
  341. return static::notice(['tableData' => $result, 'flowBonusSwitch' => $flowBonusSwitch]);
  342. }
  343. /**
  344. * 历史累积奖金
  345. * @return mixed
  346. * @throws \yii\web\HttpException
  347. */
  348. public function actionHistoricalCumulativeBonus()
  349. {
  350. $calcBonus = CalcBonus::find()->where('USER_ID=:USER_ID AND IS_SENT=1', [':USER_ID' => \Yii::$app->user->id])
  351. ->select([
  352. 'IFNULL(SUM(BONUS_E), 0.00) AS BONUS_E',
  353. 'IFNULL(SUM(BONUS_PE), 0.00) AS BONUS_PE',
  354. 'IFNULL(SUM(BONUS_SE), 0.00) AS BONUS_SE',
  355. 'IFNULL(SUM(BONUS_ST), 0.00) AS BONUS_ST',
  356. 'IFNULL(SUM(BONUS_LB), 0.00) AS BONUS_LB',
  357. 'IFNULL(SUM(BONUS_WB), 0.00) AS BONUS_WB',
  358. 'IFNULL(SUM(BONUS_CAR), 0.00) AS BONUS_CAR',
  359. 'IFNULL(SUM(BONUS_PB), 0.00) AS BONUS_PB',
  360. ])
  361. ->asArray()
  362. ->one();
  363. $data = [
  364. ['name' => 'eliteBonus', 'bonus' => $calcBonus['BONUS_E'] ?? '0'],
  365. ['name' => 'proEliteBonus', 'bonus' => $calcBonus['BONUS_PE'] ?? '0'],
  366. ['name' => 'superEliteBonus', 'bonus' => $calcBonus['BONUS_SE'] ?? '0'],
  367. ['name' => 'stockistBonus', 'bonus' => $calcBonus['BONUS_ST'] ?? '0'],
  368. ['name' => 'leaderShipBonus', 'bonus' => $calcBonus['BONUS_LB'] ?? '0'],
  369. ['name' => 'welcomeBonus', 'bonus' => $calcBonus['BONUS_WB'] ?? '0'],
  370. ['name' => 'carBonus', 'bonus' => $calcBonus['BONUS_CAR'] ?? '0'],
  371. ['name' => 'userPerformanceBonus', 'bonus' => $calcBonus['BONUS_PB'] ?? '0'],
  372. ];
  373. return static::notice(['tableData' => $data]);
  374. }
  375. /**
  376. * 实时业绩
  377. * @return mixed
  378. * @throws \yii\web\HttpException
  379. */
  380. public function actionRealTimePerf() {
  381. $userId = \Yii::$app->user->id;
  382. $period = Period::instance();
  383. $newPerf = Perf::getPeriodNewPerf($userId);
  384. $weekData = [['PV_1L' => Tool::formatFrontPerf($newPerf['PV_1L']), 'PV_2L' => Tool::formatFrontPerf($newPerf['PV_2L']), 'PV_3L' => Tool::formatFrontPerf($newPerf['PV_3L'])]];
  385. $monthPerf = Perf::getMonthPerf($userId);
  386. $monthData = [['PV_1L' => Tool::formatFrontPerf($monthPerf['PV_1L']), 'PV_2L' => Tool::formatFrontPerf($monthPerf['PV_2L']), 'PV_3L' => Tool::formatFrontPerf($monthPerf['PV_3L'])]];
  387. $lastMonth = PerfMonth::getMonthPerf($period->getLastMonth()['yearMonth'], $userId);
  388. $lastData = [['PV_1L' => Tool::formatFrontPerf($lastMonth['PV_1L_TOTAL']), 'PV_2L' => Tool::formatFrontPerf($lastMonth['PV_2L_TOTAL']), 'PV_3L' => Tool::formatFrontPerf($lastMonth['PV_3L_TOTAL'])]];
  389. //是否合格
  390. // $lastChkStatus = '';
  391. // $lastMonthPerfChk = Cache::getSystemConfig()['lastMonthPerfChk']['VALUE'];
  392. // $lastArr = [$lastMonth['PV_1L_TOTAL'], $lastMonth['PV_2L_TOTAL'], $lastMonth['PV_3L_TOTAL'], $lastMonth['PV_4L_TOTAL'], $lastMonth['PV_5L_TOTAL']];
  393. // if (array_sum($lastArr) >= $lastMonthPerfChk) {
  394. // $lastChkStatus = '已合格';
  395. // }
  396. //判断大区
  397. // $bigLocation = array_search(max($lastArr), $lastArr) + 1;
  398. return static::notice(['weekData' => $weekData, 'monthData' => $monthData, 'lastData' => $lastData]);
  399. }
  400. /**
  401. * 近十期已挂网的期数
  402. * @return mixed
  403. * @throws \yii\web\HttpException
  404. */
  405. public function actionDecPeriod() {
  406. $showDecPeriodNum = Cache::getSystemConfig()['showDecPeriodNum']['VALUE'];
  407. $calcBonus = Period::find()->where('IS_SENT=:IS_SENT',[':IS_SENT' => Period::SEND_FINISH])->select('PERIOD_NUM,END_TIME')->limit($showDecPeriodNum)->orderBy('PERIOD_NUM DESC')->asArray()->all();
  408. return static::notice($calcBonus);
  409. }
  410. /**
  411. * 报单中心补助明细
  412. * @return mixed
  413. * @throws \yii\web\HttpException
  414. */
  415. public function actionFlowBt() {
  416. $periodNum = \Yii::$app->request->get('periodNum');
  417. $condition = '';
  418. $params = [];
  419. if (!$periodNum) {
  420. return static::notice(Yii::t('app', 'pleaseSelectThePeriod'), 400);
  421. }
  422. if ($periodNum) {
  423. $showDecPeriodNum = Cache::getSystemConfig()['showDecPeriodNum']['VALUE'];
  424. $periodNums = Period::find()->where('IS_SENT=:IS_SENT',[':IS_SENT' => Period::SEND_FINISH])->select('PERIOD_NUM')->limit($showDecPeriodNum)->orderBy('PERIOD_NUM DESC')->asArray()->all();
  425. if(!in_array($periodNum,array_column($periodNums,'PERIOD_NUM'))){
  426. return static::notice(Yii::t('app', 'cannotViewThisPeriod'), 400);
  427. }
  428. $condition .= ' AND PERIOD_NUM=:PERIOD_NUM';
  429. $params[':PERIOD_NUM'] = $periodNum;
  430. }
  431. $condition .= ' AND USER_ID=:USER_ID';
  432. $params[':USER_ID'] = \Yii::$app->user->id;
  433. $data = CalcBonusBT::lists($condition, $params, [
  434. 'select' => 'BT_TYPE,FROM_ORDER_SN,ORDER_TIME,PAY_PV,DELIVERY_AT,TRANSFER_AMOUNT,TRANSFER_AT,AMOUNT',
  435. 'from' => CalcBonusBT::tableName(),
  436. 'orderBy' => 'CREATED_AT DESC',
  437. ]);
  438. if ($data['list']) {
  439. foreach ($data['list'] as $key => $value) {
  440. $data['list'][$key]['BT_TYPE_NAME'] = CalcBonusBT::TYPE_NAME[$value['BT_TYPE']];
  441. foreach ($value as $k=>$item){
  442. if($item==0) $data['list'][$key][$k] ='';
  443. }
  444. }
  445. }
  446. return static::notice($data);
  447. }
  448. /**
  449. * 报单中心货补追溯
  450. * @return mixed
  451. * @throws \yii\web\HttpException
  452. */
  453. public function actionTraceFl() {
  454. $periodNum = \Yii::$app->request->get('periodNum');
  455. $condition = '';
  456. $params = [];
  457. if (!$periodNum) {
  458. return static::notice(Yii::t('app', 'pleaseSelectThePeriod'), 400);
  459. }
  460. if ($periodNum) {
  461. $showDecPeriodNum = Cache::getSystemConfig()['showDecPeriodNum']['VALUE'];
  462. $periodNums = Period::find()->where('IS_SENT=:IS_SENT',[':IS_SENT' => Period::SEND_FINISH])->select('PERIOD_NUM')->limit($showDecPeriodNum)->orderBy('PERIOD_NUM DESC')->asArray()->all();
  463. if(!in_array($periodNum,array_column($periodNums,'PERIOD_NUM'))){
  464. return static::notice(Yii::t('app', 'cannotViewThisPeriod'), 400);
  465. }
  466. $condition .= ' AND PERIOD_NUM=:PERIOD_NUM';
  467. $params[':PERIOD_NUM'] = $periodNum;
  468. }
  469. $condition .= ' AND USER_ID=:USER_ID';
  470. $params[':USER_ID'] = \Yii::$app->user->id;
  471. $data = CalcBonusFL::lists($condition, $params, [
  472. 'select' => 'DEC_SN,DEC_AT,DEC_PV,AMOUNT',
  473. 'from' => CalcBonusFL::tableName(),
  474. 'orderBy' => 'CREATED_AT DESC',
  475. ]);
  476. return static::notice($data);
  477. }
  478. /**
  479. * 查看所传期数的各项奖金
  480. * @return mixed
  481. * @throws \yii\db\Exception
  482. * @throws \yii\web\HttpException
  483. */
  484. public function actionBonusDetail() {
  485. $periodNum = \Yii::$app->request->get('periodNum');
  486. $period = Period::instance();
  487. $periodInfo = $period->setPeriodNum($periodNum);
  488. $yearMonth = $period->getYearMonth($periodNum);
  489. if (!$period->isSent($periodNum)) {
  490. return static::notice(Yii::t('app', 'cannotViewThisPeriod'), 400);
  491. }
  492. $data = CalcBonus::findUseSlaves()->yearMonth($yearMonth)->select('BONUS_QY,BONUS_YC,BONUS_FX,BONUS_LS,BONUS_CF,BONUS_LX,BONUS_FL')->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [':PERIOD_NUM' => $periodNum, ':USER_ID' => \Yii::$app->user->id])->asArray()->one();
  493. return static::notice(['period' => $periodInfo, 'bonus' => $data]);
  494. }
  495. /**
  496. * 奖金流水
  497. * @return mixed
  498. * @throws \yii\web\HttpException
  499. */
  500. public function actionFlowBonus() {
  501. $yearMonth = \Yii::$app->request->get('yearMonth');
  502. if ($yearMonth) {
  503. if (!Date::isYearMonth($yearMonth)) {
  504. return static::notice(Yii::t('app', 'invalidParameter'), 400);
  505. }
  506. } else {
  507. $period = Period::instance();
  508. $yearMonth = $period->getNowYearMonth();
  509. }
  510. $data = FlowBonus::lists(' AND USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id], [
  511. 'useSlaves' => true,
  512. 'select' => 'AMOUNT,TOTAL,IS_INCR,REMARK,REMARK_IS_SHOW,PERIOD_NUM,CALC_MONTH,CREATED_AT',
  513. 'yearMonth' => $yearMonth,
  514. 'orderBy' => 'CREATED_AT DESC',
  515. ]);
  516. foreach ($data['list'] as $key => $value) {
  517. if($value['REMARK_IS_SHOW']==0) $data['list'][$key]['REMARK'] = '';
  518. }
  519. return static::notice($data);
  520. }
  521. /**
  522. * 提现列表
  523. * @return mixed
  524. * @throws \yii\base\Exception
  525. * @throws \yii\web\HttpException
  526. */
  527. public function actionWithdraw() {
  528. $yearMonth = \Yii::$app->request->get('yearMonth');
  529. if ($yearMonth) {
  530. if (!Date::isYearMonth($yearMonth)) {
  531. return static::notice(Yii::t('app', 'invalidParameter'), 400);
  532. }
  533. } else {
  534. $period = Period::instance();
  535. $yearMonth = $period->getNowYearMonth();
  536. }
  537. $data = Withdraw::lists('AND W.USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id], [
  538. 'useSlaves' => true,
  539. 'select' => 'W.ID,W.USER_ID,W.IS_AUTO_WITHDRAW,W.AMOUNT,W.AUDIT_STATUS,W.CREATED_AT,W.PAID_AT,W.PAY_TYPE,W.PAID_FAIL_REMARK,W.INVOICE_ID,IA.AMOUNT INVOICE_AMOUNT,IA.AUDIT_STATUS INVOICE_AUDIT_STATUS,IA.UPLOAD_ID',
  540. 'from' => Withdraw::tableName() . ' AS W',
  541. 'join' => [
  542. ['LEFT JOIN', InvoiceAudit::tableName() . ' AS IA', 'W.ID=IA.WITHDRAW_ID'],
  543. ],
  544. 'yearMonth' => $yearMonth,
  545. 'orderBy' => 'W.CREATED_AT DESC',
  546. //'with' => 'openBank',
  547. ]);
  548. $auditStatus = array_column(\Yii::$app->params['auditStatus'], null, 'value');
  549. foreach ($data['list'] as $key => $value) {
  550. $baseInfo = Info::baseInfo($value['USER_ID']);
  551. $data['list'][$key]['USER_NAME'] = $baseInfo['USER_NAME'];
  552. $data['list'][$key]['REAL_NAME'] = $baseInfo['REAL_NAME'];
  553. $data['list'][$key]['STATUS_NAME'] = Withdraw::STATUS_NAME[$value['AUDIT_STATUS']];
  554. $data['list'][$key]['INVOICE_STATUS_NAME'] = isset($value['INVOICE_AUDIT_STATUS'])?$auditStatus[$value['INVOICE_AUDIT_STATUS']]['label']:'未传发票';
  555. }
  556. return static::notice($data);
  557. }
  558. /**
  559. * 提交提现申请
  560. * @return mixed
  561. * @throws \yii\db\Exception
  562. * @throws \yii\web\HttpException
  563. */
  564. public function actionWithdrawAdd() {
  565. if (\Yii::$app->request->isPost) {
  566. $formModel = new WithdrawForm();
  567. $formModel->scenario = 'addByUser';
  568. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->add()) {
  569. return static::notice(Yii::t('app', 'withdrawalApplicationHasBeenSubmitted'), 400);
  570. } else {
  571. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  572. }
  573. } else {
  574. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  575. }
  576. }
  577. /**
  578. * 提现退回
  579. * @return mixed
  580. * @throws \yii\db\Exception
  581. * @throws \yii\web\HttpException
  582. */
  583. public function actionWithdrawBack() {
  584. if (\Yii::$app->request->isPost) {
  585. $formModel = new WithdrawForm();
  586. $formModel->scenario = 'backByUser';
  587. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->backByUser()) {
  588. return static::notice(Yii::t('app', 'withdrawHasBeenBacked'));
  589. } else {
  590. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  591. }
  592. } else {
  593. return static::notice(Yii::t('app', 'illegalRequest'), 400);
  594. }
  595. }
  596. /**
  597. * 判断并获取提现的会员信息
  598. * @return mixed
  599. * @throws \yii\web\HttpException
  600. */
  601. public function actionChkWithdrawUser() {
  602. $uid = \Yii::$app->user->id;
  603. if (!Info::isVerified($uid)) {
  604. return static::notice(Yii::t('app', 'withdrawDoesNotAllowedOfAuthentication'), 400);
  605. }
  606. $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $uid], 'IS_BIND,IS_BIND_MAIN,IS_AUTO_WITHDRAW,REG_TYPE,TRANSFER_PROP');
  607. if ($userInfo['IS_BIND'] == 1 && $userInfo['IS_BIND_MAIN'] == 0) {
  608. return static::notice(Yii::t('app', 'withdrawDoesNotAllowedOfSubsidiaryMember'), 400);
  609. }
  610. if ($userInfo['IS_AUTO_WITHDRAW'] == 1) {
  611. return static::notice(Yii::t('app', 'autoWithdrawHasBeenOpened'), 400);
  612. }
  613. if (!Withdraw::allowWithdraw()) {
  614. return static::notice(Yii::t('app', 'withdrawNotAllowOfDate'), 400);
  615. }
  616. if (Withdraw::hasThisMonthWithdraw($uid)) {
  617. return static::notice(Yii::t('app', 'withdrawAllowOnceOfMonth'), 400);
  618. }
  619. if (Withdraw::existWaitAudit($uid)) {
  620. return static::notice(Yii::t('app', 'withdrawRecordHasNotVerify'), 400);
  621. }
  622. //是否显示服务协议
  623. $regType = RegType::findOneAsArray('ID=:ID', [':ID' => $userInfo['REG_TYPE']], 'IS_PACT');
  624. $path = \Yii::getAlias('@common/runtime/datas/pact.php');
  625. if (!file_exists($path)) {
  626. $oneData = '';
  627. } else {
  628. $oneData = include $path;
  629. }
  630. $isCanTransferProp = Cache::getSystemConfig()['isCanTransferProp']['VALUE'];
  631. if ($isCanTransferProp == 0) {
  632. $userInfo['WITHDRAW_PROP'] = 100;
  633. } else {
  634. $userInfo['WITHDRAW_PROP'] = 100 - $userInfo['TRANSFER_PROP'];
  635. }
  636. return static::notice(['userInfo' => $userInfo, 'isPact' => $regType['IS_PACT'], 'content' => $oneData['CONTENT']]);
  637. }
  638. /**
  639. * 归集附属会员奖金
  640. * @return mixed
  641. * @throws \yii\db\Exception
  642. * @throws \yii\web\HttpException
  643. */
  644. public function actionCollectBind() {
  645. $transferBonusForm = new TransferBonusForm();
  646. if ($totals=$transferBonusForm->collectBind(\Yii::$app->user->id)) {
  647. return static::notice(Yii::t('app', 'notionalPoolingAmountFinished') . $totals);
  648. } else {
  649. if($totals==0){
  650. return static::notice(Yii::t('app', 'notionalPoolingAmountFinished') . $totals);
  651. }else{
  652. return static::notice(Form::formatErrorsForApi($transferBonusForm->getErrors()), 400);
  653. }
  654. }
  655. }
  656. /**
  657. * 上传发票之前
  658. * @return mixed
  659. * @throws \yii\web\HttpException
  660. */
  661. public function actionInvoiceBeforeAdd() {
  662. $id = \Yii::$app->request->get('id');
  663. $withdraw = Withdraw::findOneAsArray('ID=:ID', [':ID' => $id]);
  664. if (!$withdraw) {
  665. return static::notice(Yii::t('app', 'dataDoesNotExists'), 400);
  666. }
  667. $uploadInvoiceTip = Cache::getSystemConfig()['uploadInvoiceTip']['VALUE'];
  668. if ($withdraw['AUDIT_STATUS'] == Withdraw::STATUS_APPLIED) {
  669. return static::notice(['addInvoiceTips' => $uploadInvoiceTip]);
  670. } else {
  671. return static::notice(Yii::t('app', 'withdrawDoesNotUploadInvoice'), 400);
  672. }
  673. }
  674. /**
  675. * 上传发票
  676. * @return mixed
  677. * @throws \yii\db\Exception
  678. * @throws \yii\web\HttpException
  679. */
  680. public function actionInvoiceAdd() {
  681. $id = \Yii::$app->request->get('id');
  682. $withdraw = Withdraw::findOneAsArray('ID=:ID', [':ID' => $id]);
  683. if (!$withdraw) {
  684. return static::notice(Yii::t('app', 'dataDoesNotExists'), 400);
  685. }
  686. if (\Yii::$app->request->isPost) {
  687. $formModel = new UploadForm();
  688. $formModel->scenario = 'invoiceFront';
  689. $formModel->file = UploadedFile::getInstanceByName('file');
  690. $formModel->withdrawId = $withdraw['ID'];
  691. $formModel->remark = '提现'.$withdraw['SN'].'发票';
  692. //$formModel->token = \Yii::$app->request->post('uploadToken');
  693. $formModel->token = \Yii::$app->request->request('uploadToken');
  694. if ($formModel->file && $formModel->upload()) {
  695. return static::notice(Yii::t('app', 'successfully'));
  696. } else {
  697. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  698. }
  699. }
  700. }
  701. /**
  702. * 显示上传的发票
  703. * @return mixed
  704. * @throws \yii\web\HttpException
  705. */
  706. public function actionInvoiceShow() {
  707. $id = \Yii::$app->request->get('id');
  708. $uploads = Uploads::findOneAsArray('ID=:ID', [':ID' => $id],'URL');
  709. if (!$uploads) {
  710. return static::notice(Yii::t('app', 'dataDoesNotExists'), 400);
  711. }
  712. return static::notice($uploads['URL']);
  713. }
  714. /**
  715. * 获取可用余额
  716. * @return mixed
  717. * @throws \yii\web\HttpException
  718. */
  719. public function actionAvailableBalance() {
  720. return static::notice(Balance::getAvailableBalance(\Yii::$app->user->id));
  721. }
  722. /**
  723. * 查看业绩
  724. * @return mixed
  725. * @throws \yii\web\HttpException
  726. */
  727. public function actionPerf() {
  728. $yearMonth = \Yii::$app->request->get('yearMonth');
  729. if ($yearMonth) {
  730. if (!Date::isYearMonth($yearMonth)) {
  731. return static::notice(Yii::t('app', 'invalidParameter'), 400);
  732. }
  733. } else {
  734. $period = Period::instance();
  735. $yearMonth = $period->getNowYearMonth();
  736. }
  737. $data = PerfPeriod::lists('AND P.USER_ID=:USER_ID AND PN.IS_SENT=1', [':USER_ID' => \Yii::$app->user->id], [
  738. 'select' => 'P.*',
  739. 'yearMonth' => $yearMonth,
  740. 'from' => PerfPeriod::tableName() . ' AS P',
  741. 'join' => [
  742. ['LEFT JOIN', Period::tableName() . ' AS PN', 'P.PERIOD_NUM=PN.PERIOD_NUM']
  743. ],
  744. 'orderBy' => 'P.PERIOD_NUM DESC',
  745. ]);
  746. $data['request']['yearMonth'] = $yearMonth;
  747. return static::notice($data);
  748. }
  749. /**
  750. * 转账列表
  751. * @return mixed
  752. * @throws \yii\web\HttpException
  753. */
  754. public function actionTransferList() {
  755. $type = \Yii::$app->request->get('type', 'out');
  756. if ($type == 'out') {
  757. $condition = ' AND T.FROM_UID=:USER_ID';
  758. } else {
  759. $condition = ' AND T.TO_UID=:USER_ID';
  760. }
  761. $params = [
  762. ':USER_ID' => \Yii::$app->user->id,
  763. ];
  764. $data = Transfer::lists($condition, $params, [
  765. 'select' => 'FUI.USER_NAME AS FROM_USER_NAME, TUI.USER_NAME AS TO_USER_NAME, T.AMOUNT, T.PERIOD_NUM, T.CREATED_AT',
  766. 'from' => Transfer::tableName() . ' AS T',
  767. 'join' => [
  768. ['LEFT JOIN', UserInfo::tableName() . ' AS FUI', 'FUI.USER_ID=T.FROM_UID'],
  769. ['LEFT JOIN', UserInfo::tableName() . ' AS TUI', 'TUI.USER_ID=T.TO_UID'],
  770. ],
  771. ]);
  772. return static::notice($data);
  773. }
  774. /**
  775. * 转账
  776. * @throws \yii\db\Exception
  777. * @throws \yii\web\HttpException
  778. */
  779. public function actionTransferAdd() {
  780. if (\Yii::$app->request->isPost) {
  781. $formModel = new TransferForm();
  782. if ($formModel->load(\Yii::$app->request->post(), '') && $formModel->transfer()) {
  783. return static::notice(Yii::t('app', 'successfully'));
  784. } else {
  785. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  786. }
  787. }
  788. }
  789. /**
  790. * 检查转账资格
  791. * @return mixed
  792. * @throws \yii\web\HttpException
  793. */
  794. public function actionChkTransferUser() {
  795. $uid = \Yii::$app->user->id;
  796. if (!Info::isVerified($uid)) {
  797. return static::notice(Yii::t('app', 'transferDoesNotAllowedOfAuthentication'), 400);
  798. }
  799. $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $uid], 'ALLOW_TRANSFER,TRANSFER_PROP');
  800. if($userInfo['ALLOW_TRANSFER']==0){
  801. return static::notice(Yii::t('app', 'transferDoesNotAllowed'), 400);
  802. }
  803. $isCanTransferProp = Cache::getSystemConfig()['isCanTransferProp']['VALUE'];
  804. if($isCanTransferProp==0){
  805. $userInfo['TRANSFER_PROP']=100;
  806. }
  807. return static::notice(['userInfo' => $userInfo]);
  808. }
  809. /**
  810. * 房产积分列表
  811. * @return mixed
  812. * @throws \yii\web\HttpException
  813. */
  814. public function actionFcPoint() {
  815. $condition = ' AND USER_ID=:USER_ID';
  816. $params = [':USER_ID' => \Yii::$app->user->id];
  817. $yearMonth = \Yii::$app->request->get('yearMonth');
  818. if ($yearMonth) {
  819. if (!Date::isYearMonth($yearMonth)) {
  820. return static::notice(Yii::t('app', 'invalidParameter'), 400);
  821. }
  822. $condition .= ' AND CALC_MONTH=:CALC_MONTH';
  823. $params['CALC_MONTH'] = $yearMonth;
  824. }
  825. $data = ScoreMonth::lists($condition, $params, [
  826. 'from' => ScoreMonth::tableName(),
  827. 'orderBy' => 'ID DESC',
  828. ]);
  829. return static::notice($data);
  830. }
  831. /**
  832. * 查询可用期数
  833. * @return mixed
  834. * @throws HttpException
  835. */
  836. public function actionPeriod() {
  837. $nowTs = time();
  838. // 当前期
  839. $currentPeriod = Period::find()->where('START_TIME<:NOW_TIME', ['NOW_TIME' => $nowTs])->where('END_TIME>=:NOW_TIME', ['NOW_TIME' => $nowTs - 6])->asArray()->one();
  840. $data[] = $currentPeriod['PERIOD_NUM'];
  841. // 上一期是否封期
  842. $prevPeriodNum = $currentPeriod['PERIOD_NUM'] - 1;
  843. $prevPeriod = Period::find()->where('PERIOD_NUM=:PERIOD_NUM AND IS_SENT=0', [':PERIOD_NUM' => $prevPeriodNum])->asArray()->one();
  844. if ($prevPeriod) {
  845. $data[] = $prevPeriod['PERIOD_NUM'];
  846. }
  847. return static::notice(['data' => $data]);
  848. }
  849. }