ReportController.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\report;
  8. use app\jobs\LgCashJob;
  9. use app\models\AccountLog;
  10. use app\models\Cash;
  11. use app\models\Goods;
  12. use app\models\Lg;
  13. use app\models\Md;
  14. use app\models\MdGoods;
  15. use app\models\Option;
  16. use app\models\ReportCommitIntegral;
  17. use app\models\ReportForm;
  18. use app\models\ReportInfo;
  19. use app\models\ReportPool;
  20. use app\models\ReportPoolDetail;
  21. use app\models\ReportPoolLevel;
  22. use app\models\ReportSetting;
  23. use app\models\SaasUser;
  24. use app\models\Store;
  25. use app\models\User;
  26. use app\modules\admin\controllers\adopt\FoodTableNumber;
  27. use app\modules\admin\controllers\adopt\Land;
  28. use app\modules\admin\controllers\adopt\LandCat;
  29. use app\modules\admin\controllers\BaseController;
  30. use app\modules\admin\models\adopt\AdoptGoodsForm;
  31. use app\modules\admin\models\adopt\DeviceForm;
  32. use app\modules\admin\models\GoodsForm;
  33. use app\modules\admin\models\MdGoodsForm;
  34. use app\plugins\adopt\models\AdoptCat;
  35. use app\plugins\adopt\models\AdoptGoods;
  36. use app\plugins\adopt\models\AdoptSetting;
  37. use app\utils\Alipay\Alipay;
  38. use app\utils\LgApi;
  39. use app\utils\OrderNo;
  40. use app\utils\QrCode;
  41. use yii\base\BaseObject;
  42. use yii\helpers\Json;
  43. class ReportController extends BaseController
  44. {
  45. /**
  46. * 全域分红配置
  47. * @return \yii\web\Response
  48. */
  49. public function actionSaveReportSetting() {
  50. $store_id = get_store_id();
  51. // print_r($store_id);
  52. $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
  53. if (!$report_setting){
  54. $report_setting = new ReportSetting();
  55. }
  56. $form_name = post_params('form_name', '');
  57. $form = post_params('form', '');
  58. $is_share_self = post_params('is_share_self', 0);
  59. $is_auto_send = post_params('is_auto_send', 0);
  60. $auto_send_day = post_params('auto_send_day', 0);
  61. $dividend_day = post_params('dividend_day', 0);
  62. $reporr_limit = post_params('reporr_limit', 0);
  63. $third_commission = post_params('third_commission', 0);
  64. $second_commission = post_params('second_commission', 0);
  65. $first_commission = post_params('first_commission', 0);
  66. $report_setting->store_id = $store_id;//表单名称
  67. $report_setting->form_name = $form_name;//表单名称
  68. $report_setting->is_share_self = $is_share_self;//是否分销内购
  69. $report_setting->is_auto_send = $is_auto_send;//是否自动发放
  70. $report_setting->auto_send_day = $auto_send_day;//自动发放天数
  71. $report_setting->dividend_day = $dividend_day;//分红周期
  72. $report_setting->reporr_limit = $reporr_limit;//每天限制提交报单次数
  73. $report_setting->third_commission = $third_commission;//三级分销
  74. $report_setting->second_commission = $second_commission;//二级分销
  75. $report_setting->first_commission = $first_commission;//一级分销
  76. $report_setting->updated_at = time();
  77. if (!$report_setting->save()) {
  78. return $this->asJson([
  79. 'code' => 1,
  80. 'msg' => $report_setting->errors
  81. ]);
  82. }else{
  83. //处理form数据
  84. // var_dump($form);die();
  85. if (is_array($form)){
  86. // print_r(Json::decode($form));die;
  87. ReportForm::updateAll(['is_delete' => 1], ['store_id'=>$store_id,'report_setting_id' => $report_setting->id,'is_delete'=> 0]);
  88. // $form = Json::decode($form);
  89. foreach ($form as $key => $value) {
  90. $form_data = new ReportForm();
  91. $form_data->store_id = $store_id;
  92. $form_data->report_setting_id = $report_setting->id;
  93. $form_data->name = $value['name'];
  94. $form_data->type = $value['type'];
  95. $form_data->required = $value['required'];
  96. $form_data->default = $value['default'];
  97. $form_data->tip = $value['tip'];
  98. $form_data->sort = $value['sort'];
  99. $form_data->is_delete = 0;
  100. $form_data->created_at = time();
  101. $form_data->save();
  102. }
  103. }
  104. }
  105. return $this->asJson([
  106. 'code' => 0,
  107. 'msg' => '保存成功'
  108. ]);
  109. }
  110. /**
  111. * 全域分红配置查询
  112. * @return \yii\web\Response
  113. */
  114. public function actionGetReportSetting() {
  115. $store_id = get_store_id();
  116. $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
  117. if (!$report_setting) {
  118. return $this->asJson([
  119. 'code' => 0,
  120. 'msg' => 'success',
  121. 'data' => [
  122. 'first_commission' => '',
  123. 'second_commission' => '',
  124. 'third_commission' => '',
  125. 'dividend_day' => '',
  126. 'auto_send_day' =>'',
  127. 'is_auto_send' => 0,
  128. 'is_share_self' => 0,
  129. 'form_name' => '',
  130. 'reporr_limit' => '',
  131. 'form' => [],
  132. ]
  133. ]);
  134. }
  135. //如果存在的话查询表单内容
  136. $report_form = ReportForm::find()->where(['report_setting_id'=>$report_setting->id,'store_id' => $store_id,'is_delete'=> 0])
  137. ->orderBy('id asc,sort asc')
  138. ->asArray()
  139. ->all()??[];
  140. return $this->asJson([
  141. 'code' => 0,
  142. 'msg' => 'success',
  143. 'data' => [
  144. 'first_commission' => $report_setting->first_commission,
  145. 'second_commission' => $report_setting->second_commission,
  146. 'third_commission' => $report_setting->third_commission,
  147. 'dividend_day' => $report_setting->dividend_day,
  148. 'auto_send_day' => $report_setting->auto_send_day,
  149. 'is_auto_send' => $report_setting->is_auto_send,
  150. 'is_share_self' => $report_setting->is_share_self,
  151. 'form_name' => $report_setting->form_name,
  152. 'reporr_limit' => $report_setting->reporr_limit,
  153. 'form' => $report_form,
  154. ]
  155. ]);
  156. }
  157. /**
  158. * 报单列表展示
  159. */
  160. public function actionList() {
  161. $store_id = get_store_id();
  162. $report_id = get_params('report_id',0);
  163. $start_time = get_params('start_time');
  164. $end_time = get_params('end_time');
  165. $audit_start_time = get_params('audit_start_time');
  166. $audit_end_time = get_params('audit_end_time');
  167. $name = get_params('nickname');
  168. $mobile = get_params('binding');
  169. $status = get_params('status');
  170. $query = ReportInfo::find()->alias('r')
  171. ->leftJoin(['u' => User::tableName()], 'u.id=r.user_id')
  172. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  173. ->where(['r.is_delete' => 0, 'r.store_id' => $store_id]);
  174. if ($start_time) {
  175. $query->andWhere(['>=', 'r.created_at', strtotime($start_time)]);
  176. }
  177. if ($end_time) {
  178. $query->andWhere(['<=', 'r.created_at', strtotime($end_time)]);
  179. }
  180. if ($report_id){
  181. $query->andWhere(['r.id' => $report_id]);
  182. }
  183. if ($audit_start_time) {
  184. $query->andWhere(['>=', 'r.audit_time', strtotime($audit_start_time)]);
  185. }
  186. if ($audit_end_time) {
  187. $query->andWhere(['<=', 'r.audit_time', strtotime($audit_end_time)]);
  188. }
  189. if ($name) {
  190. $query->andWhere([
  191. 'or',
  192. ['like', 'su.name', $name],
  193. ['like', 'u.nickname', $name],
  194. ]);
  195. }
  196. if ($mobile) {
  197. $query->andWhere([
  198. 'or',
  199. ['like', 'su.mobile', $mobile],
  200. ['like', 'u.binding', $mobile],
  201. ]);
  202. }
  203. // if ($audit_start_time) {
  204. // $query->andWhere(['>=', 'c.updated_at', strtotime($audit_start_time)]);
  205. // }
  206. // if ($audit_end_time) {
  207. // $query->andWhere(['<=', 'c.updated_at', strtotime($audit_end_time)]);
  208. // }
  209. if ($status == 0) { //待审核
  210. $query->andWhere(['r.status' => 0]);
  211. }
  212. if ($status == 1) { //已通过
  213. $query->andWhere(['r.status' =>1]);
  214. }
  215. if ($status == 2) { // 已拒绝
  216. $query->andWhere(['r.status' => 2]);
  217. }
  218. $query->distinct()->orderBy('r.status ASC,r.created_at DESC')
  219. ->select([
  220. 'r.*',
  221. 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar',
  222. 'u.nickname', 'u.avatar_url', 'u.binding'
  223. ]);
  224. $pagination = pagination_make($query);
  225. $list = $pagination['list'];
  226. foreach($list as &$value){
  227. //表单内容
  228. $value['audit_info'] = Json::decode($value['audit_info']);
  229. //提交时间
  230. $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  231. //审核时间
  232. $value['audit_time'] = $value['audit_time']?date('Y-m-d H:i:s',$value['audit_time']):'';
  233. }
  234. return $this->asJson([
  235. 'code' => 0,
  236. 'msg' => 'success',
  237. 'data' => [
  238. 'data' => $list,
  239. 'pageNo' => $pagination['pageNo'],
  240. 'totalCount' => $pagination['totalCount']
  241. ],
  242. ]);
  243. }
  244. /**
  245. * 报单新建
  246. */
  247. public function actionAddReport()
  248. {
  249. $store_id = get_store_id();
  250. $user_id = post_params('user_id');
  251. $audit_info = post_params('audit_info');
  252. if (!$user_id) {
  253. return $this->asJson([
  254. 'code' => 1,
  255. 'msg' => '请选择报单用户'
  256. ]);
  257. }
  258. $user = User::findOne($user_id);
  259. if (!$user || empty($user->binding)) {
  260. return $this->asJson([
  261. 'code' => 1,
  262. 'msg' => '报单用户查询失败'
  263. ]);
  264. }
  265. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  266. if (!$saas_user) {
  267. return $this->asJson([
  268. 'code' => 1,
  269. 'msg' => '报单用户查询失败_'
  270. ]);
  271. }
  272. $t = \Yii::$app->db->beginTransaction();
  273. if (!$audit_info) {
  274. return $this->asJson([
  275. 'code' => 1,
  276. 'msg' => '审核信息不能为空'
  277. ]);
  278. }
  279. //判断是否有设置配置
  280. $report_setting = ReportSetting::findOne(['store_id' => $store_id]);
  281. if (!$report_setting->dividend_day){
  282. $t->rollBack();
  283. return $this->asJson([
  284. 'code' => 1,
  285. 'msg' => '请先配置分红周期'
  286. ]);
  287. }
  288. //检查是否有本期分红池
  289. $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
  290. ->andWhere(['<=','start_time',time()])
  291. ->andWhere(['>=','end_time',time()])
  292. ->orderBy('id desc')
  293. ->one();
  294. //如果没有奖金池创建第一个池子
  295. if (!$pool_info) {
  296. $pool_info = new ReportPool();
  297. $pool_info->store_id = $store_id;
  298. // $pool_info->saas_id = get_saas_user_id();
  299. $pool_info->start_time = time();
  300. $pool_info->end_time = time() + $report_setting->dividend_day * 24 * 60 * 60;
  301. $pool_info->created_at = time();
  302. $pool_info_copy = ReportPool::find()->where(['store_id'=>get_store_id()])
  303. ->orderBy('id desc')
  304. ->one();
  305. if ($pool_info_copy){
  306. $pool_info->number = $pool_info_copy->number+1;
  307. }else{
  308. $pool_info->number =1;
  309. }
  310. if (!$pool_info->save()){
  311. $t->rollBack();
  312. return $this->asJson([
  313. 'code' => 1,
  314. 'msg' => '添加失败'
  315. ]);
  316. }
  317. }
  318. //保存信息
  319. $report_info = new ReportInfo();
  320. $report_info->store_id = $store_id;
  321. $report_info->user_id = $user_id;
  322. $report_info->saas_id = $saas_user->id;
  323. $report_info->audit_info = Json::encode($audit_info);
  324. $report_info->status = 0;
  325. $report_info->is_delete = 0;
  326. $report_info->order_no = OrderNo::getOrderNo(OrderNo::REPORT_INFO);
  327. $report_info->pool_id = $pool_info->id;
  328. $report_info->created_at = time();
  329. if (!$report_info->save()) {
  330. $t->rollBack();
  331. return $this->asJson([
  332. 'code' => 1,
  333. 'msg' => $report_info->getErrors()
  334. ]);
  335. }
  336. $t->commit();
  337. return $this->asJson([
  338. 'code' => 0,
  339. 'msg' => '添加成功'
  340. ]);
  341. }
  342. /**
  343. * 报单列表删除
  344. */
  345. public function actionReportDelete()
  346. {
  347. $report_id= get_params('report_id');
  348. $form = ReportInfo::find()
  349. ->where('id = :report_id', [':report_id' => $report_id])
  350. ->andWhere(['store_id' => get_store_id()])
  351. ->one();
  352. $form->is_delete = 1;
  353. if ($form && $form->save()) {
  354. return $this->asJson([
  355. 'code' => 0,
  356. 'msg' => '操作成功',
  357. ]);
  358. }
  359. return $this->asJson([
  360. 'code' => 1,
  361. 'msg' => '操作失败',
  362. ]);
  363. }
  364. /**
  365. * 报单列表通过或拒绝
  366. */
  367. public function actionBatchAudit() {
  368. $report_id = post_params('report_id');
  369. $money = post_params('money');
  370. $status = post_params('status');
  371. if (!in_array($status, [1, 2])) {
  372. return $this->asJson([
  373. 'code' => 1,
  374. 'msg' => '参数错误'
  375. ]);
  376. }
  377. // var_dump($report_id);die;
  378. if (empty($report_id) || !is_array($report_id)) {
  379. return $this->asJson([
  380. 'code' => 1,
  381. 'msg' => '参数错误'
  382. ]);
  383. }
  384. $t = \Yii::$app->db->beginTransaction();
  385. if ($status == 1) {
  386. //审核通过
  387. foreach ($report_id as $value) {
  388. $report_info = ReportInfo::findOne($value);
  389. if (!$report_info){
  390. return $this->asJson([
  391. 'code' => 1,
  392. 'msg' => '该报单不存在'
  393. ]);
  394. }
  395. $report_info->status = 1;
  396. $report_info->bonus_points = $money;
  397. $report_info->audit_time = time();
  398. //审核通过需要根据报单配置进行积分分配
  399. $report_setting = ReportSetting::find()->where(['store_id'=>get_store_id()])->one();
  400. if (!$report_setting){
  401. $t->rollBack();
  402. return $this->asJson([
  403. 'code' => 1,
  404. 'msg' => '请先配置全域分红配置'
  405. ]);
  406. }
  407. //查询本期奖金池
  408. $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
  409. ->andWhere(['<=','start_time',time()])
  410. ->andWhere(['>=','end_time',time()])
  411. ->orderBy('id desc')
  412. ->one();
  413. // print_r($pool_info);die();
  414. if (!$pool_info){
  415. $pool_info = new ReportPool();
  416. $pool_info->store_id = get_store_id();
  417. // $pool_info->saas_id = get_saas_user_id();
  418. $pool_info->start_time = time();
  419. $pool_info->end_time = time() + $report_setting->dividend_day * 24 * 60 * 60;
  420. $pool_info->created_at = time();
  421. $pool_info_copy = ReportPool::find()->where(['store_id'=>get_store_id()])
  422. ->orderBy('id desc')
  423. ->one();
  424. if ($pool_info_copy){
  425. $pool_info->number = $pool_info_copy->number+1;
  426. }else{
  427. $pool_info->number =1;
  428. }
  429. if (!$pool_info->save()){
  430. $t->rollBack();
  431. return $this->asJson([
  432. 'code' => 1,
  433. 'msg' => '添加失败'
  434. ]);
  435. }
  436. }
  437. // print_r($report_id);die;
  438. //生成报单奖金池明细
  439. $pool_detail = new ReportPoolDetail();
  440. $pool_detail->report_id = $value;
  441. $pool_detail->store_id = get_store_id();
  442. $pool_detail->money = $money;
  443. $pool_detail->pool_level_id = 0;
  444. $pool_detail->desc = '奖金池明细';
  445. $pool_detail->created_at = time();
  446. $pool_detail->pool_id = $pool_info->id;
  447. $pool_detail->save();
  448. //执行分红记录逻辑
  449. $this->addProfit($report_setting,$report_info->user_id,get_store_id(),$value,$money,$pool_info->id,1);
  450. //审核通过需要把提交的金额记录起来
  451. $report_commit_integral = ReportCommitIntegral::find()
  452. ->where(['is_delete'=>0,'store_id'=>get_store_id(),'user_id'=>$report_info->user_id])
  453. ->andWhere(['=','money',$money])
  454. ->one();
  455. if (!$report_commit_integral){
  456. $new_commit = new ReportCommitIntegral();
  457. $new_commit->store_id = get_store_id();
  458. $new_commit->report_id = $report_info->id;
  459. $new_commit->user_id = $report_info->user_id;
  460. $new_commit->money = $money;
  461. $new_commit->is_delete = 0;
  462. $new_commit->created_at = time();
  463. $new_commit->save();
  464. }
  465. $report_info->status = 1;
  466. $report_info->audit_time = time();
  467. $report_info->save();
  468. }
  469. }
  470. if ($status == 2){
  471. //审核拒绝
  472. foreach ($report_id as $value) {
  473. $report_info = ReportInfo::findOne($value);
  474. $report_info->status = 2;
  475. $report_info->audit_time = time();
  476. $report_info->save();
  477. }
  478. }
  479. $t->commit();
  480. return $this->asJson([
  481. 'code' => 0,
  482. 'msg' => '审核成功'
  483. ]);
  484. }
  485. /**
  486. * 执行分红逻辑
  487. * type 1是执行通过,2是修改奖励
  488. * @return void
  489. */
  490. public function addProfit($report_setting,$user_id,$store_id,$report_id,$money,$pool_id,$type)
  491. {
  492. $first_commission = $report_setting->first_commission;
  493. $second_commission = $report_setting->second_commission;
  494. $third_commission = $report_setting->third_commission;
  495. //执行分红逻辑
  496. //分销内购
  497. //报单的,肯定获取积分,如果设置了分销比例,对应上级获得分销奖励积分
  498. $user = User::findOne($user_id);
  499. if ($user) {
  500. ReportPoolLevel::addProfitLog($store_id, $pool_id, $money, $user->id, $report_id,'',-1, $type);
  501. $user_parent = User::findOne($user->old_parent_id);
  502. if ($user_parent){
  503. $one_money = bcdiv(bcmul($first_commission,$money), 100, 0);
  504. ReportPoolLevel::addProfitLog($store_id, $pool_id, $one_money, $user_parent->id, $report_id, '', 0, $type);
  505. $user_parent_parent = User::findOne($user_parent->old_parent_id);
  506. if ($user_parent_parent){
  507. //上级
  508. $two_money = bcdiv(bcmul($second_commission,$money), 100,0);
  509. ReportPoolLevel::addProfitLog($store_id, $pool_id, $two_money, $user_parent_parent->id, $report_id, '', 1, $type);
  510. $user_parent_parent_parent = User::findOne($user_parent_parent->old_parent_id);
  511. if ($user_parent_parent_parent){
  512. //上上级
  513. $three_money = bcdiv(bcmul($third_commission,$money), 100,0);
  514. ReportPoolLevel::addProfitLog($store_id, $pool_id, $three_money, $user_parent_parent_parent->id, $report_id, '', 2, $type);
  515. }
  516. }
  517. }
  518. }
  519. }
  520. /**
  521. * 积分记录
  522. */
  523. public function actionIntegralLog(){
  524. $store_id = get_store_id();
  525. $start_time = get_params('start_time');
  526. $end_time = get_params('end_time');
  527. $audit_start_time = get_params('audit_start_time');
  528. $audit_end_time = get_params('audit_end_time');
  529. $name = get_params('nikename');
  530. $mobile = get_params('binding');
  531. $status = get_params('status','-1');
  532. $query = ReportInfo::find()->alias('r')
  533. ->leftJoin(['u' => User::tableName()], 'u.id=r.user_id')
  534. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  535. ->where(['r.is_delete' => 0, 'r.store_id' => $store_id,'r.status'=>1]);
  536. if ($start_time) {
  537. $query->andWhere(['>=', 'r.created_at', strtotime($start_time)]);
  538. }
  539. if ($end_time) {
  540. $query->andWhere(['<=', 'r.created_at', strtotime($end_time)]);
  541. }
  542. if ($audit_start_time) {
  543. $query->andWhere(['>=', 'r.audit_time', strtotime($audit_start_time)]);
  544. }
  545. if ($audit_end_time) {
  546. $query->andWhere(['<=', 'r.audit_time', strtotime($audit_end_time)]);
  547. }
  548. if ($name) {
  549. $query->andWhere([
  550. 'or',
  551. ['like', 'su.name', $name],
  552. ['like', 'u.nickname', $name],
  553. ]);
  554. }
  555. if ($mobile) {
  556. $query->andWhere([
  557. 'or',
  558. ['like', 'su.mobile', $mobile],
  559. ['like', 'u.binding', $mobile],
  560. ]);
  561. }
  562. if ($audit_start_time) {
  563. $query->andWhere(['>=', 'r.updated_at', strtotime($audit_start_time)]);
  564. }
  565. if ($audit_end_time) {
  566. $query->andWhere(['<=', 'r.updated_at', strtotime($audit_end_time)]);
  567. }
  568. if ($status == 1) { //已发放
  569. $query->andWhere(['r.is_send' =>1]);
  570. }
  571. if ($status == 2) { // 未发放
  572. $query->andWhere(['r.is_send' => 0]);
  573. }
  574. $query->distinct()->orderBy('r.status ASC,r.created_at DESC')
  575. ->select([
  576. 'r.*',
  577. 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar',
  578. 'u.nickname', 'u.avatar_url', 'u.binding'
  579. ]);
  580. $pagination = pagination_make($query);
  581. $list = $pagination['list'];
  582. foreach($list as &$value){
  583. //查询分销情况
  584. //查询一级
  585. $report_pool_level_one = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>0])->one();
  586. $user_one = User::findOne($report_pool_level_one->user_id);
  587. $value['first_name'] = $user_one['nickname'];
  588. $value['first_avatar'] = $user_one['avatar_url'];
  589. $value['first_money'] = $report_pool_level_one['money'];
  590. //查询二级
  591. $report_pool_level_two = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>1])->one();
  592. $user_two = User::findOne($report_pool_level_two->user_id);
  593. $value['second_name'] = $user_two['nickname'];
  594. $value['second_avatar'] = $user_two['avatar_url'];
  595. $value['second_money'] = $report_pool_level_two['money'];
  596. //查询三级
  597. $report_pool_level_three = ReportPoolLevel::find()->where(['store_id'=>$store_id,'report_id'=>$value['id'],'level'=>2])->one();
  598. $user_three = User::findOne($report_pool_level_three->user_id);
  599. $value['third_name'] = $user_three['nickname'];
  600. $value['third_avatar'] = $user_three['avatar_url'];
  601. $value['third_money'] = $report_pool_level_three['money'];
  602. //提交时间
  603. $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  604. //审核时间
  605. $value['audit_time'] = $value['audit_time']?date('Y-m-d H:i:s',$value['audit_time']):'';
  606. //发放时间
  607. $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
  608. }
  609. return $this->asJson([
  610. 'code' => 0,
  611. 'msg' => 'success',
  612. 'data' => [
  613. 'data' => $list,
  614. 'pageNo' => $pagination['pageNo'],
  615. 'totalCount' => $pagination['totalCount']
  616. ],
  617. ]);
  618. }
  619. /**
  620. * 修改奖励
  621. *
  622. */
  623. public function actionEditReward(){
  624. $report_id = post_params('report_id');
  625. $money = post_params('money');
  626. if (empty($report_id) || !is_array($report_id)) {
  627. return $this->asJson([
  628. 'code' => 1,
  629. 'msg' => '参数错误'
  630. ]);
  631. }
  632. //print_r($report_id);die();
  633. $t = \Yii::$app->db->beginTransaction();
  634. foreach ($report_id as $value) {
  635. // print_r($value);die;
  636. $report_info = ReportInfo::findOne($value);
  637. if (!$report_info){
  638. return $this->asJson([
  639. 'code' => 1,
  640. 'msg' => '该报单不存在'
  641. ]);
  642. }
  643. if ($report_info->is_send == 1){
  644. return $this->asJson([
  645. 'code' => 1,
  646. 'msg' => '报单已发放奖励'
  647. ]);
  648. }
  649. // print_r($report_info);die;
  650. $report_info->bonus_points = $money;
  651. $report_info->audit_time = time();
  652. $report_info->save();
  653. $report_detail = ReportPoolDetail::find()->where(['report_id'=>$value,'is_delete'=>0])->one();
  654. $report_detail->money = $money;
  655. $report_detail->save();
  656. //审核通过需要根据报单配置进行积分分配
  657. $report_setting = ReportSetting::find()->where(['store_id'=>get_store_id()])->one();
  658. if (!$report_setting){
  659. $t->rollBack();
  660. return $this->asJson([
  661. 'code' => 1,
  662. 'msg' => '请先配置全域分红配置'
  663. ]);
  664. }
  665. //查询本期奖金池
  666. $pool_info = ReportPool::find()->where(['store_id'=>get_store_id(), 'is_send' => 0])
  667. ->andWhere(['<=','start_time',time()])
  668. ->andWhere(['>=','end_time',time()])
  669. ->one();
  670. if (!$pool_info){
  671. return $this->asJson([
  672. 'code' => 1,
  673. 'msg' => '奖金池错误'
  674. ]);
  675. }
  676. //执行分红记录逻辑
  677. $this->addProfit($report_setting,$report_info->user_id,get_store_id(),$value,$money,$pool_info->id,2);
  678. //审核通过需要把提交的金额记录起来
  679. $report_commit_integral = ReportCommitIntegral::find()
  680. ->where(['is_delete'=>0,'store_id'=>get_store_id(),'user_id'=>$report_info->user_id,'report_id'=>$report_info->id])
  681. ->andWhere(['=','money',$money])
  682. ->one();
  683. if (!$report_commit_integral){
  684. $new_commit = new ReportCommitIntegral();
  685. $new_commit->store_id = get_store_id();
  686. $new_commit->report_id = $report_info->id;
  687. $new_commit->user_id = $report_info->user_id;
  688. $new_commit->money = $money;
  689. $new_commit->is_delete = 0;
  690. $new_commit->created_at = time();
  691. $new_commit->save();
  692. }else{
  693. // return $this->asJson([
  694. // 'code' => 1,
  695. // 'msg' => '修改失败'
  696. // ]);
  697. }
  698. }
  699. $t->commit();
  700. return $this->asJson([
  701. 'code' => 0,
  702. 'msg' => '修改成功'
  703. ]);
  704. }
  705. /**
  706. * 报单奖金池
  707. */
  708. public function actionReportBonusPool() {
  709. $store_id = get_store_id();
  710. $start_time = get_params('start_time');
  711. $end_time = get_params('end_time');
  712. $send_start_time = get_params('send_start_time');
  713. $send_end_time = get_params('send_end_time');
  714. $status = get_params('status');
  715. $query = ReportPool::find()->alias('r')
  716. ->where(['r.store_id' => $store_id]);
  717. if ($start_time) {
  718. $query->andWhere(['>=', 'r.start_time', strtotime($start_time)]);
  719. }
  720. if ($end_time) {
  721. $query->andWhere(['<=', 'r.end_time', strtotime($end_time)]);
  722. }
  723. if ($send_start_time) {
  724. $query->andWhere(['>=', 'r.send_time', strtotime($send_start_time)]);
  725. }
  726. if ($send_end_time) {
  727. $query->andWhere(['<=', 'r.send_time', strtotime($send_end_time)]);
  728. }
  729. if ($status == 1) { //已发放
  730. $query->andWhere(['r.is_send' => 1]);
  731. }
  732. if ($status == 2) { //未发放
  733. $query->andWhere(['r.is_send' =>0]);
  734. }
  735. $query->distinct()->orderBy('r.created_at DESC')
  736. ->select([
  737. 'r.*'
  738. ]);
  739. $pagination = pagination_make($query);
  740. $list = $pagination['list'];
  741. foreach($list as &$value){
  742. //表单内容
  743. $value['audit_info'] = Json::decode($value['audit_info']);
  744. //生成时间
  745. $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  746. //开始时间
  747. $value['start_time'] = $value['start_time']?date('Y-m-d H:i:s',$value['start_time']):'';
  748. //结束时间
  749. $value['end_time'] = $value['end_time']?date('Y-m-d H:i:s',$value['end_time']):'';
  750. //发放时间
  751. $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
  752. //查询参与人数
  753. $report_info_num = ReportPoolDetail::find()
  754. ->alias('rp')
  755. ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = rp.report_id')
  756. ->where(['rp.pool_id'=>$value['id'],'rp.is_delete'=>0])
  757. ->andWhere(['=','ri.status',1])
  758. ->groupBy('ri.user_id')
  759. ->count()??0;
  760. $value['report_info_num']= $report_info_num;
  761. //查询累计积分
  762. $report_bonus_sum = ReportPoolLevel::find()
  763. ->where(['pool_id'=>$value['id'],'is_delete'=>0])
  764. ->sum('money')??0;
  765. $value['report_bonus_sum']= $report_bonus_sum;
  766. //查询奖金池金额(是后台设置的)
  767. }
  768. return $this->asJson([
  769. 'code' => 0,
  770. 'msg' => 'success',
  771. 'data' => [
  772. 'data' => $list,
  773. 'pageNo' => $pagination['pageNo'],
  774. 'totalCount' => $pagination['totalCount']
  775. ],
  776. ]);
  777. }
  778. /**
  779. * 报单奖金池提交记录
  780. */
  781. public function actionReportCommitLog() {
  782. $store_id = get_store_id();
  783. $report_id = get_params('report_id');
  784. $info = ReportInfo::find()
  785. ->where(['store_id' => $store_id,'id'=>$report_id])->one();
  786. if (!$info){
  787. return $this->asJson([
  788. 'code' => 0,
  789. 'data' => []
  790. ]);
  791. }
  792. $query = ReportCommitIntegral::find()->where(['user_id'=>$info->user_id,'is_delete'=>0]);
  793. $query->orderBy('money asc')
  794. ->select([
  795. '*'
  796. ]);
  797. $pagination = pagination_make($query);
  798. $list = $pagination['list'];
  799. return $this->asJson([
  800. 'code' => 0,
  801. 'msg' => 'success',
  802. 'data' => [
  803. 'data' => $list,
  804. 'pageNo' => $pagination['pageNo'],
  805. 'totalCount' => $pagination['totalCount']
  806. ],
  807. ]);
  808. }
  809. /**
  810. * 设置奖金池金额
  811. */
  812. public function actionSetBonusPoolMoney()
  813. {
  814. $store_id = get_store_id();
  815. $money = post_params('money',0);
  816. $pool_id = post_params('pool_id',0);
  817. $t = \Yii::$app->db->beginTransaction();
  818. if (!$money) {
  819. return $this->asJson([
  820. 'code' => 1,
  821. 'msg' => '奖金池奖励不能为0'
  822. ]);
  823. }
  824. $report_pool = ReportPool::findOne(['store_id' => $store_id,'id'=>$pool_id]);
  825. if (!$report_pool){
  826. $t->rollBack();
  827. return $this->asJson([
  828. 'code' => 1,
  829. 'msg' => '奖金池不存在'
  830. ]);
  831. }
  832. if ($report_pool->is_send == 1){
  833. $t->rollBack();
  834. return $this->asJson([
  835. 'code' => 1,
  836. 'msg' => '奖金池已发放不能设置金额'
  837. ]);
  838. }
  839. $report_pool->reward_amount = $money;
  840. if (!$report_pool->save()) {
  841. $t->rollBack();
  842. return $this->asJson([
  843. 'code' => 1,
  844. 'msg' => '设置失败'
  845. ]);
  846. }
  847. $t->commit();
  848. return $this->asJson([
  849. 'code' => 0,
  850. 'msg' => '设置成功'
  851. ]);
  852. }
  853. /**
  854. * 奖金池明细
  855. */
  856. public function actionBonusPoolDetail()
  857. {
  858. $store_id = get_store_id();
  859. $pool_id = get_params('pool_id',0);
  860. if (!$pool_id){
  861. return $this->asJson([
  862. 'code' => 0,
  863. 'msg' => '奖金池id不能为空'
  864. ]);
  865. }
  866. //查询奖金池信息
  867. $pool_info = ReportPool::findOne(['store_id' => $store_id,'id'=>$pool_id]);
  868. // print_r($pool_id);die();
  869. //查询参与人数
  870. $report_info_num = ReportPoolDetail::find()
  871. ->alias('rp')
  872. ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = rp.report_id')
  873. ->where(['rp.pool_id'=>$pool_id,'rp.is_delete'=>0])
  874. ->andWhere(['=','ri.status',1])
  875. ->groupBy('ri.user_id')
  876. ->count()??0;
  877. //查询累计积分
  878. $report_bonus_sum = ReportPoolLevel::find()
  879. ->where(['pool_id'=>$pool_id,'is_delete'=>0])
  880. ->sum('money')??0;
  881. //查询发放金额
  882. $report_dividend_money_sum = ReportPoolLevel::find()
  883. ->where(['pool_id'=>$pool_id,'is_delete'=>0])
  884. ->andWhere(['is_send'=>1])
  885. ->sum('dividend_money')??0;
  886. //查询设置金额
  887. $bonus_money = $pool_info->reward_amount??0;
  888. //发放时间
  889. $send_time = $pool_info->send_time?date('Y-m-d H:i:s',$pool_info->send_time):'';
  890. //奖池周期
  891. $cycle_start = date('Y-m-d H:i:s',$pool_info->start_time);
  892. $cycle_end = date('Y-m-d H:i:s',$pool_info->end_time);
  893. //报单名称
  894. //用户名称
  895. //获得积分 每个报单三个用户获得的积分
  896. //奖励金额 奖金池金额 /总积分 * 个人积分 10 / 100 * 5 = 50金额
  897. $query = ReportPoolLevel::find()->alias('r')
  898. ->leftJoin(['ri'=> ReportInfo::tableName()],'ri.id = r.report_id')
  899. ->where(['r.store_id' => $store_id,'r.pool_id'=>$pool_id,'r.is_delete'=>0]);
  900. $query->orderBy('r.created_at DESC')
  901. ->select([
  902. 'r.*','ri.order_no'
  903. ]);
  904. $pagination = pagination_make($query);
  905. $list = $pagination['list'];
  906. foreach($list as &$value){
  907. //生成时间
  908. $value['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  909. //发放时间
  910. $value['send_time'] = $value['send_time']?date('Y-m-d H:i:s',$value['send_time']):'';
  911. //获取用户信息'u.nickname', 'u.avatar_url', 'u.binding
  912. $user_info = User::find()->where(['id'=>$value['user_id']])->asArray()->one();
  913. $value['nickname'] = $user_info['nickname'];
  914. $value['binding']= $user_info['binding'];
  915. $value['avatar_url']= $user_info['avatar_url'];
  916. //查询总积分
  917. $report_bonus_sum = ReportPoolLevel::find()
  918. ->where(['pool_id'=>$value['pool_id'],'is_delete'=>0])
  919. ->sum('money')??0;
  920. //查询获得积分
  921. $level_money = $value['money'];
  922. $reward_amount = 0;
  923. // print_r($value['id']);die;
  924. //奖励计算公式 奖励金额 /总积分 * 个人奖励积分
  925. if ($bonus_money && $report_bonus_sum && $level_money){
  926. //奖励金额计算
  927. // print_r($level_money);die;
  928. $reward_amount = round(($bonus_money / $report_bonus_sum * $level_money ),2) ;
  929. }
  930. $value['reward_amount'] = $reward_amount;
  931. }
  932. return $this->asJson([
  933. 'code' => 0,
  934. 'msg' => 'success',
  935. 'data' => [
  936. 'data' => $list,
  937. 'report_info_num'=>$report_info_num, //查询参与人数
  938. 'report_bonus_sum'=>$report_bonus_sum, //查询累计积分
  939. 'report_dividend_money_sum'=>$report_dividend_money_sum, //查询发放金额
  940. 'bonus_money'=>$bonus_money,//发放前设置金额
  941. 'is_send'=>$pool_info->is_send,//发放前设置金额
  942. 'cycle_start'=>$cycle_start,
  943. 'cycle_end'=>$cycle_end,
  944. 'send_time'=>$send_time,
  945. 'pageNo' => $pagination['pageNo'],
  946. 'totalCount' => $pagination['totalCount']
  947. ],
  948. ]);
  949. }
  950. /**
  951. * 手动发放奖金池
  952. */
  953. public function actionSendBonusPool()
  954. {
  955. $store_id = get_store_id();
  956. $pool_id = get_params('pool_id',0);
  957. //判断是否发放
  958. $pool_info = ReportPool::find()->where(['id'=>$pool_id])->one();
  959. if ($pool_info->is_send == 1){
  960. return $this->asJson([
  961. 'code' => 1,
  962. 'msg' => '奖金池已发放'
  963. ]);
  964. }
  965. //print_r($pool_info);die;
  966. //判断是否设置奖金
  967. if ($pool_info->reward_amount < 0.1){
  968. return $this->asJson([
  969. 'code' => 1,
  970. 'msg' => '奖金池未设置金额'
  971. ]);
  972. }
  973. //判断是否有报单
  974. $report_info_num = ReportInfo::find()->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'status'=>1])->one();
  975. if (!$report_info_num){
  976. return $this->asJson([
  977. 'code' => 1,
  978. 'msg' => '奖金池无报单'
  979. ]);
  980. }
  981. //判断是否到分红周期时间
  982. // $bonus_pool_end = $pool_info->end_time;
  983. // if ($bonus_pool_end > time()){
  984. // return $this->asJson([
  985. // 'code' => 1,
  986. // 'msg' => '奖金池未到发放周期'
  987. // ]);
  988. // }
  989. //发放奖励到每个level中
  990. //查看所有奖励记录的积分
  991. $report_bonus_sum = ReportPoolLevel::find()
  992. ->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'is_send'=>0])
  993. ->sum('money');
  994. $report_level_list = ReportPoolLevel::find()
  995. ->where(['store_id'=>$store_id,'pool_id'=>$pool_id,'is_send'=>0,'is_delete'=>0])
  996. ->asArray()
  997. ->all();
  998. $t = \Yii::$app->db->beginTransaction();
  999. foreach ($report_level_list as $key=>$value){
  1000. // 奖励金额 /总积分 * 个人奖励积分
  1001. //奖励金额计算
  1002. $reward_amount = 0;
  1003. if ($pool_info->reward_amount && $report_bonus_sum && $value['money']){
  1004. //奖励金额计算
  1005. $reward_amount = round(($pool_info->reward_amount/$report_bonus_sum * $value['money']),2);
  1006. }
  1007. //发放奖励到用户表当中记录
  1008. $user = User::findOne($value['user_id']);
  1009. $user->global_money_total += $reward_amount;
  1010. $user->global_money +=$reward_amount;
  1011. if (!$user->save()){
  1012. $t->rollBack();
  1013. return $this->asJson([
  1014. 'code'=>1,
  1015. 'msg'=>'发放失败'
  1016. ]);
  1017. }
  1018. $report_level_info = ReportPoolLevel::findOne($value['id']);
  1019. $report_level_info->is_send = 1;
  1020. $report_level_info->dividend_money = $reward_amount;
  1021. $report_level_info->send_time = time();
  1022. if (!$report_level_info->save()){
  1023. $t->rollBack();
  1024. return $this->asJson([
  1025. 'code'=>1,
  1026. 'msg'=>'发放失败'
  1027. ]);
  1028. }
  1029. }
  1030. //奖金池也设置已完成
  1031. ReportPool::updateAll(['is_send'=>1,'send_time'=>time()],['id'=>$pool_info->id]);
  1032. //报单列表更新已完成
  1033. ReportInfo::updateAll(['is_send'=>1,'send_time'=>time()],['pool_id'=>$pool_info->id,'is_delete'=>0]);
  1034. $t->commit();
  1035. return $this->asJson([
  1036. 'code' => 0,
  1037. 'msg' => '发放成功'
  1038. ]);
  1039. }
  1040. /**
  1041. * 提现列表
  1042. * @return \yii\web\Response
  1043. */
  1044. public function actionCashList() {
  1045. $store_id = get_store_id();
  1046. $start_time = get_params('start_time');
  1047. $end_time = get_params('end_time');
  1048. $status = get_params('status');
  1049. $name = get_params('name');
  1050. // $binding = get_params('binding');
  1051. $audit_start_time = get_params('audit_start_time');
  1052. $audit_end_time = get_params('audit_end_time');
  1053. $query = Cash::find()->alias('c')
  1054. ->leftJoin(['u' => User::tableName()], 'u.id=c.user_id')
  1055. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  1056. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $store_id]);
  1057. if ($start_time) {
  1058. $query->andWhere(['>=', 'c.created_at', strtotime($start_time)]);
  1059. }
  1060. if ($end_time) {
  1061. $query->andWhere(['<=', 'c.created_at', strtotime($end_time)]);
  1062. }
  1063. if ($name) {
  1064. $query->andWhere([
  1065. 'or',
  1066. ['like', 'su.name', $name],
  1067. ['like', 'u.nickname', $name],
  1068. ]);
  1069. }
  1070. // if ($binding) {
  1071. // $query->andWhere([
  1072. // 'or',
  1073. // ['like', 'su.mobile', $binding],
  1074. // ['like', 'u.binding', $binding],
  1075. // ]);
  1076. // }
  1077. if ($audit_start_time) {
  1078. $query->andWhere(['>=', 'c.updated_at', strtotime($audit_start_time)]);
  1079. }
  1080. if ($audit_end_time) {
  1081. $query->andWhere(['<=', 'c.updated_at', strtotime($audit_end_time)]);
  1082. }
  1083. if ($status == Cash::STATUS_APPLY) { //待审核
  1084. $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
  1085. }
  1086. if ($status == Cash::STATUS_CONFIRM) { // 待打款
  1087. $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
  1088. }
  1089. if ($status == Cash::STATUS_GIVEN) { // 已打款
  1090. $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
  1091. }
  1092. if ($status == Cash::STATUS_REFUSE) { // 已拒绝
  1093. $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
  1094. }
  1095. $query->andWhere(['c.cash_type' => 17]);
  1096. $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
  1097. 'c.*', 'su.name new_name', 'su.id saas_user_id', 'su.avatar new_avatar', 'u.nickname', 'u.avatar_url', 'u.binding'
  1098. ]);
  1099. $pagination = pagination_make($query);
  1100. $list = $pagination['list'];
  1101. foreach($list as &$value){
  1102. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  1103. $value['money'] = Cash::getServiceMoney($value);
  1104. $value['status_name'] = Cash::getCashStatusName($value);
  1105. $saas_user = SaasUser::findOne($value['saas_user_id']);
  1106. $value['card_no'] = '';
  1107. // if (intval($value['type']) === Cash::TYPE_BANK) {
  1108. if ($saas_user->withdraw_method) {
  1109. $withdraw_method = json_decode($saas_user->withdraw_method, true);
  1110. if (!empty($withdraw_method)) {
  1111. foreach ($withdraw_method as $method_item) {
  1112. if ($method_item['type'] === 'bank_card') {
  1113. $value['card_no'] = $method_item['card_no'];
  1114. }
  1115. }
  1116. }
  1117. }
  1118. $value['created_at'] = date('Y-m-d H:i:s',$value['created_at']);
  1119. $value['pay_time'] = !empty($value['pay_time']) ?? date('Y-m-d H:i:s',$value['pay_time']);
  1120. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s',$value['updated_at']) : '';
  1121. // }
  1122. }
  1123. return $this->asJson([
  1124. 'code' => 0,
  1125. 'msg' => 'success',
  1126. 'data' => [
  1127. 'data' => $list,
  1128. 'pageNo' => $pagination['pageNo'],
  1129. 'totalCount' => $pagination['totalCount']
  1130. ],
  1131. ]);
  1132. }
  1133. public function actionReportDetail()
  1134. {
  1135. $report_id = get_params('report_id');
  1136. if (!$report_id){
  1137. return $this->asJson([
  1138. 'code' => 1,
  1139. 'msg' => '参数错误',
  1140. ]);
  1141. }
  1142. $report_info = ReportInfo::find()->where(['id'=>$report_id])->asArray()->one();
  1143. $user_info = User::findOne($report_info['user_id']);
  1144. $report_info['created_at'] = date('Y-m-d H:i:s', $report_info['created_at']);
  1145. $report_info['audit_time'] = $report_info['audit_time']?date('Y-m-d H:i:s', $report_info['audit_time']):'';
  1146. if ($report_info['status'] == 0 ){
  1147. //待审核
  1148. $report_info['status_text'] = '待审核';
  1149. }else if ($report_info['status'] == 1 ){
  1150. //审核成功
  1151. $report_info['status_text'] = '审核成功';
  1152. }else if ($report_info['status'] == 2){
  1153. //审核失败
  1154. $report_info['status_text'] = '审核失败';
  1155. }
  1156. return $this->asJson([
  1157. 'code' => 0,
  1158. 'msg' => 'success',
  1159. 'data' => [
  1160. 'report_info'=> $report_info,
  1161. 'nickname'=> $user_info->nickname,
  1162. 'avatar_url'=> $user_info->avatar_url,
  1163. 'binding'=> $user_info->binding,
  1164. ],
  1165. ]);
  1166. }
  1167. private function export($list) {
  1168. $rows = [[
  1169. 'ID',
  1170. '会员信息',
  1171. '会员等级',
  1172. '手机号',
  1173. '申请提现金额(元)',
  1174. '手续费(元)',
  1175. '实际打款(元)',
  1176. '姓名',
  1177. '提现到',
  1178. '账户',
  1179. '身份证号',
  1180. '开户行',
  1181. '支行',
  1182. '状态',
  1183. '申请时间',
  1184. '审核时间',
  1185. ]];
  1186. foreach($list as $item){
  1187. $r = [
  1188. $item['id'],
  1189. $item['nickname'],
  1190. $item['level_name'],
  1191. $item['binding'],
  1192. $item['price'],
  1193. $item['service_money'],
  1194. $item['money'],
  1195. $item['name'],
  1196. Cash::$type[$item['type']] ?? '--',
  1197. $item['mobile'],
  1198. $item['card_no'],
  1199. $item['bank_name'],
  1200. $item['bank_branch'],
  1201. Cash::$status[$item['status']] ?? '',
  1202. date('Y-m-d H:i:s', $item['created_at']),
  1203. $item['updated_at'] <= 0 ? '' : date('Y-m-d H:i:s', $item['updated_at']),
  1204. ];
  1205. $rows[] = $r;
  1206. }
  1207. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  1208. ->addRows($rows)->toBrowser();
  1209. }
  1210. /**
  1211. * 提现申请审核
  1212. * @return \yii\web\Response
  1213. */
  1214. public function actionCashApply()
  1215. {
  1216. $id = post_params('id');
  1217. $status = post_params('status');
  1218. $store_id = get_store_id();
  1219. // if (empty($id) || !is_array($id)) {
  1220. // return $this->asJson([
  1221. // 'code' => 1,
  1222. // 'msg' => '数据格式错误'
  1223. // ]);
  1224. // }
  1225. // print_r($store_id);die;
  1226. $id = [$id];
  1227. $t = \Yii::$app->db->beginTransaction();
  1228. foreach ($id as $value) {
  1229. $cash = Cash::findOne(['id' => $value, 'is_delete' => Cash::IS_DELETE_NO,
  1230. 'store_id' => $store_id]);
  1231. if (!$cash) {
  1232. $t->rollBack();
  1233. return $this->asJson([
  1234. 'code' => 1,
  1235. 'msg' => '提现记录不存在,请刷新重试'
  1236. ]);
  1237. }
  1238. if (!$cash->order_no) {
  1239. $order_no = null;
  1240. while (true) {
  1241. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  1242. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  1243. if (!$exist_order_no) {
  1244. break;
  1245. }
  1246. }
  1247. $cash->order_no = $order_no;
  1248. $cash->save();
  1249. }
  1250. \Yii::$app->cache->set('report_cash_cache_' . $value, $cash->order_no);
  1251. if (!in_array($status, [1, 3])) {
  1252. \Yii::$app->cache->set('report_cash_cache_' . $value, false);
  1253. $t->rollBack();
  1254. return $this->asJson([
  1255. 'code' => 1,
  1256. 'msg' => '提现记录ID: ' . $value . '已审核,请刷新重试'
  1257. ]);
  1258. }
  1259. $cash->status = $status;
  1260. if ($status == Cash::STATUS_REFUSE) {
  1261. $user = User::findOne($cash->user_id);
  1262. $user->global_money += $cash->price;
  1263. if (!$user->save()) {
  1264. return $this->asJson([
  1265. 'code' => 1,
  1266. 'msg' => '网络异常'
  1267. ]);
  1268. }
  1269. // NoticeSend::CashFail($cash->user_id, $user->binding, $cash->price, '提现被驳回', '提现被驳回');
  1270. }
  1271. if (!$cash->save()) {
  1272. $t->rollBack();
  1273. return $this->asJson([
  1274. 'code' => 1,
  1275. 'msg' => '网络异常,请刷新重试'
  1276. ]);
  1277. }
  1278. }
  1279. $t->commit();
  1280. return $this->asJson([
  1281. 'code' => 0,
  1282. 'msg' => '审核成功'
  1283. ]);
  1284. }
  1285. /**
  1286. * 打款
  1287. * @return \yii\web\Response
  1288. */
  1289. public function actionCashConfirm()
  1290. {
  1291. $id = post_params('id');
  1292. $status = post_params('status');
  1293. $store_id = get_store_id();
  1294. $cash = Cash::findOne(['id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
  1295. 'store_id' => $store_id]);
  1296. if (!$cash) {
  1297. return $this->asJson([
  1298. 'code' => 1,
  1299. 'msg' => '提现记录不存在,请刷新重试'
  1300. ]);
  1301. }
  1302. if (!$cash->order_no) {
  1303. $order_no = null;
  1304. while (true) {
  1305. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  1306. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  1307. if (!$exist_order_no) {
  1308. break;
  1309. }
  1310. }
  1311. $cash->order_no = $order_no;
  1312. $cash->save();
  1313. }
  1314. if ($cash->status != 1) {
  1315. return $this->asJson([
  1316. 'code' => 1,
  1317. 'msg' => '操作错误,请刷新重试'
  1318. ]);
  1319. }
  1320. $store = Store::findOne($store_id);
  1321. $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
  1322. if($store->is_platform_transfers == 1){
  1323. $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  1324. }
  1325. $res = [];
  1326. $price = ($cash->price - ($cash->price * ($cash->service_charge / 100)));
  1327. if ($status == Cash::STATUS_GIVEN) { //微信自动打款
  1328. if ((int)$cash->type === 0) {
  1329. $cash->status = Cash::STATUS_GIVEN;
  1330. $cash->pay_time = time();
  1331. $cash->pay_type = Cash::PAY_TYPE_WX;
  1332. $user = User::findOne($cash->user_id);
  1333. $wechat_open_id = $user->wechat_open_id;
  1334. $data = [
  1335. 'partner_trade_no' => $cash->order_no,
  1336. 'openid' => $wechat_open_id,
  1337. 'check_name' => 'NO_CHECK',
  1338. 'amount' => $price * 100,
  1339. 'desc' => '转账'
  1340. ];
  1341. $wechat = \Yii::$app->controller->wechatPay;
  1342. // $res = $wechat->transfer->toBalance($data);
  1343. $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
  1344. //判断是否使用新版本转账 增加转账标识
  1345. $wechat_cash = json_decode($wechat_cash, true);
  1346. if (intval($wechat_cash['is_open']) === 2) {
  1347. if (!$res['code']) {
  1348. $cash->is_platform_transfers = intval($store->is_platform_transfers);
  1349. $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
  1350. $cash->wx_cash_state = $res['data']['state'];
  1351. $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
  1352. $cash->save();
  1353. }
  1354. }
  1355. // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
  1356. } elseif ((int)$cash->type === 1) {
  1357. $order = (object)[
  1358. 'store_id' => $cash->store_id,
  1359. 'order_no' => $cash->order_no,
  1360. 'pay_price' => $price,
  1361. 'name' => $cash->name
  1362. ];
  1363. $result = Alipay::transfer($order, $cash->mobile);
  1364. if (isset($result['code']) && $result['code'] == 1) {
  1365. if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
  1366. $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
  1367. }
  1368. if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
  1369. $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
  1370. }
  1371. if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
  1372. $result['msg'] = '商户余额不足';
  1373. }
  1374. return $this->asJson($result);
  1375. }
  1376. $cash->status = Cash::STATUS_GIVEN;
  1377. $cash->pay_time = time();
  1378. $cash->pay_type = Cash::TYPE_ALIPAY;
  1379. $cash->save();
  1380. return $this->asJson([
  1381. 'code' => 0,
  1382. 'msg' => '成功'
  1383. ]);
  1384. }elseif ((int)$cash->type === 4) {
  1385. $user = User::findOne($cash->user_id);
  1386. $saas = SaasUser::findOne(['mobile' => $user->binding]);
  1387. $lg_info = Lg::find()->where(['user_id'=>$saas->id,'status'=>1,'is_delete'=>0])->one();
  1388. $lgApi = new LgApi($cash->store_id);
  1389. //灵工提现
  1390. $post_data = [
  1391. 'store_id' => $cash->store_id,
  1392. 'outTradeNo' => $cash->order_no,//唯一批次号
  1393. 'accNo' => bcmul($price,100),
  1394. 'amt' => bcmul($price,100),
  1395. 'name' => $lg_info->name,
  1396. 'certCard' => $lg_info->cert_card//身份证号
  1397. ];
  1398. $result = $lgApi->FlexiblePay($post_data);
  1399. if (isset($result['status']) && $result['status'] == 999) {
  1400. return $this->asJson($result);
  1401. }
  1402. $cash->status = 6;//灵工待打款
  1403. $cash->save();
  1404. //灵工提现接口调用后消息队列查询状态
  1405. \queue_push(new LgCashJob(['id'=>$cash->id,'store_id'=>$store_id,'type'=>17,'retry' => 5]), 60);
  1406. return $this->asJson([
  1407. 'code' => 0,
  1408. 'msg' => '成功'
  1409. ]);
  1410. }
  1411. } elseif ($status == Cash::STATUS_HAND) { //手动打款
  1412. if ((int)$cash->type === 3) {
  1413. AccountLog::saveLog($cash->user_id, $price, 2, 1, 0, 0, '全域分佣提现打款');
  1414. }
  1415. $cash->status = Cash::STATUS_GIVEN;
  1416. $cash->pay_time = time();
  1417. $cash->pay_type = Cash::PAY_TYPE_HAND;
  1418. $res['result_code'] = "SUCCESS";
  1419. // NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
  1420. }
  1421. if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
  1422. $cash->save();
  1423. return $this->asJson([
  1424. 'code' => 0,
  1425. 'msg' => '成功'
  1426. ]);
  1427. } else {
  1428. return $this->asJson([
  1429. 'code' => 1,
  1430. 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
  1431. 'data' => $res
  1432. ]);
  1433. }
  1434. }
  1435. }