ChannelSettle.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <?php
  2. /**
  3. * 渠道结算接口控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\common\library\MakeReportGo;
  8. use app\guildapi\controller\Guild;
  9. use think\Db;
  10. use think\Config;
  11. use app\common\model\Setting;
  12. use think\Env;
  13. use think\Exception;
  14. use app\common\library\MakeReport;
  15. class ChannelSettle extends Guild
  16. {
  17. protected $nowTime;
  18. protected $payRequestLimit = 5; //重复下单的限制时间
  19. protected $redis; //redis的句柄对象
  20. /**
  21. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  22. */
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->nowTime = NOW_TIMESTAMP;
  27. $this->redis = \think\Cache::store('default')->handler();
  28. }
  29. /**
  30. * 我的结算列表
  31. * @return [type] [description]
  32. */
  33. public function index()
  34. {
  35. //判断当前账号是否有此功能
  36. if (!in_array($this->_channelLevel, [1])) {
  37. $this->jsonResult('', 0, '您无权限使用该功能');
  38. }
  39. $list_rows = $this->input('list_rows', 10);
  40. $page = $this->input('page', 1);
  41. $orderid = $this->input('orderid', '', 'trim'); //收入账号
  42. $begin_time = $this->input('begin_time', '', 'trim');
  43. $end_time = $this->input('end_time', '', 'trim');
  44. $download = $this->input('download', 0, 'intval');
  45. $condition = [];
  46. $condition['channel_id'] = $this->_channelId;
  47. $condition['settle_type'] = 1; //专服
  48. if ($orderid) {
  49. $condition['orderid'] = $orderid;
  50. }
  51. //申请开始时间和结束时间不为空时
  52. if ($begin_time != '' && $end_time != '') {
  53. $condition['create_time'] = [
  54. ['>=', strtotime($begin_time)],
  55. ['<=', strtotime($end_time . ' 23:59:59')],
  56. ];
  57. } //开始时间不为空时
  58. elseif ($begin_time != '') {
  59. $condition['create_time'] = ['>=', strtotime($begin_time)];
  60. } //结束时间不为空时
  61. elseif ($end_time != '') {
  62. $condition['create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  63. }
  64. // var_dump($condition);
  65. if ($download) {
  66. $sql = model('ChannelDivideSettle')
  67. ->where($condition)
  68. ->field("id,orderid,channel_id,total_amount,total_divide_amt,remark,create_time,first_audit_status,second_audit_status")
  69. ->order("id desc")
  70. ->fetchSql(true)
  71. ->select();
  72. // echo $sql;
  73. // if ((new MakeReport())->addTask('guild.ChannelSettleIndex', $sql, 'cps' . session('guild_info')['id'])) {
  74. if ((new MakeReportGo())->addTask('guild.ChannelSettleIndex', $sql, 'cps' . session('guild_info')['id'])) {
  75. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  76. } else {
  77. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  78. }
  79. }
  80. $settleList = model('ChannelDivideSettle')
  81. ->where($condition)
  82. ->field("id,orderid,channel_id,total_amount,total_divide_amt,remark,create_time,first_audit_status,second_audit_status")
  83. ->order("id desc")
  84. ->paginate(['list_rows' => $list_rows, 'page' => $page])
  85. ->toArray();
  86. foreach ($settleList["data"] as &$val) {
  87. if ($val['first_audit_status'] == 1 && $val['second_audit_status'] == 1) { //审核通过
  88. $val['status'] = 1;
  89. } else if ($val['first_audit_status'] == 2 || $val['second_audit_status'] == 2) { //审核不通过
  90. $val['status'] = 2;
  91. } else { //审核中
  92. $val['status'] = 0;
  93. }
  94. unset($val['first_audit_status']);
  95. unset($val['second_audit_status']);
  96. }
  97. $this->jsonResult($settleList, 20000, '获取列表成功');
  98. }
  99. /**
  100. * 结算明细数据
  101. */
  102. public function getSettleDetail()
  103. {
  104. if ($this->request->isPost()) {
  105. //判断当前账号是否有结算权限
  106. if (!in_array($this->_channelLevel, [1])) {
  107. $this->jsonResult('', 0, '您无权限进行结算相关操作');
  108. } else {
  109. $settleId = input('settleId', 0, 'intval');
  110. $download = input('download', 0, 'intval');
  111. if (!$settleId) {
  112. $this->jsonResult('', 0, '请选择结算单ID');
  113. }
  114. $where = array();
  115. $where['channel_id'] = $this->_channelId;
  116. $where['id'] = $settleId;
  117. $settleInfo = model("ChannelDivideSettle")->where($where)->field("id,orderid,total_amount,total_divide_amt,coupon_amount_amt,actual_amount")->find();
  118. if (!$settleInfo) {
  119. $this->jsonResult('', 0, '该结算单不存在或您不能查看');
  120. }
  121. $list_rows = input('list_rows', 10, 'intval');
  122. $page = input('page', 1, 'intval');
  123. $where = array();
  124. $where['p.settle_id'] = $settleId;
  125. // var_dump($where);
  126. $settleDetInfo = model("ChannelDivideSettleDet")->alias('p')
  127. ->join('cy_game g', 'p.game_id = g.id', 'left')
  128. ->field("p.game_id AS game_id,g.name as game_name,p.pay_amt,p.ratio,p.divide_amt,p.coupon_amount_amt,p.actual_amount")
  129. ->where($where)
  130. ->select();
  131. $where = array();
  132. $where['p.settle_id'] = $settleId;
  133. if ($download) {
  134. $sql = model("ChannelDivideSettleDetPay")->alias('p')
  135. ->join('cy_game g', 'p.game_id = g.id', 'left')
  136. ->field("p.orderid,p.username,p.create_time,p.game_id AS game_id,g.name as game_name,p.amount AS amount,p.divide_amt,p.ratio,p.serverid,p.servername,p.rolename,p.coupon_amount,p.actual_amount,p.real_coin")
  137. ->where($where)
  138. ->order("p.id desc")
  139. ->fetchSql(true)
  140. ->select();
  141. // echo $sql;
  142. // if ((new MakeReport())->addTask('guild.ChannelSettleDetail', $sql, 'cps' . session('guild_info')['id'])) {
  143. if ((new MakeReportGo())->addTask('guild.ChannelSettleDetail', $sql, 'cps' . session('guild_info')['id'])) {
  144. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  145. } else {
  146. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  147. }
  148. }
  149. $detPaylist = model("ChannelDivideSettleDetPay")->alias('p')
  150. ->join('cy_game g', 'p.game_id = g.id', 'left')
  151. ->field("p.orderid,p.username,p.create_time,p.game_id AS game_id,g.name as game_name,p.amount AS amount,p.divide_amt,p.ratio,p.serverid,p.servername,p.rolename,p.coupon_amount,p.actual_amount,p.real_coin")
  152. ->where($where)
  153. ->order("p.id desc")
  154. ->paginate(['list_rows' => $list_rows, 'page' => $page])
  155. ->toArray();
  156. // var_dump($detPaylist);
  157. $retData['settle'] = $settleInfo;
  158. $retData['settleDet'] = $settleDetInfo;
  159. $retData['detPaylist'] = $detPaylist;
  160. $this->jsonResult($retData, 20000, '获取结算详情成功');
  161. }
  162. exit;
  163. } else {
  164. $this->jsonResult('', 0, '非法请求');
  165. exit;
  166. }
  167. }
  168. /**
  169. * 可结算数据
  170. */
  171. public function settle()
  172. {
  173. if ($this->request->isPost()) {
  174. //判断当前账号是否有结算权限
  175. if (!in_array($this->_channelLevel, [1])) {
  176. $this->jsonResult('', 0, '您无权限进行结算相关操作');
  177. } else {
  178. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type")->where(['channel_id' => $this->_channelId, 'apply_status' => 1])->find();
  179. if (empty($channelInfoApply)) {
  180. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行结算1');
  181. }
  182. $channelInfo = model("Channel")->field("id,name,level,cps_settle_period,mcps_settle_period,status")->where(['id' => $this->_channelId])->find();
  183. if (empty($channelInfo)) {
  184. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  185. }
  186. if ($channelInfo['cps_settle_period'] == 2) {
  187. $this->lastTime = strtotime(date('Y-m-d')) - 1;
  188. } else {
  189. $this->lastTime = strtotime(date('Y-m-d', (time() - ((date('w', time()) == 0 ? 7 : date('w', time())) - 1) * 24 * 3600))) - 1;
  190. }
  191. $list_rows = input('list_rows', 10);
  192. $page = input('page', 1);
  193. $where = array();
  194. $where['p.status'] = 1;
  195. $where['p.create_time'] = array('elt', $this->lastTime);
  196. //所有下级渠道(包括自己)
  197. $channelIds = get_child_channel_arr($this->_channelId);
  198. array_push($channelIds, $this->_channelId);
  199. $where['p.channel_id'] = ['in', $channelIds];
  200. $where['p.settle_id'] = array('eq', 0);
  201. $where['g.id'] = ['in', model('game')->getTakingGame($this->_channelId)];
  202. // var_dump($where);
  203. $GameChannelData = Db::table('cy_pay')->alias('p')
  204. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  205. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  206. ->field("p.gameid AS game_id,g.name as game_name,g.channel_split_ratio,count(*) as pay_cnt,SUM(p.amount) AS pay_amt,d.id as ratio_id,d.ratio,SUM(p.real_amount) AS pay_real_amt,SUM(p.real_ptb) AS pay_real_ptb,SUM(p.coupon_amount) as coupon_amount_amt,SUM(p.real_coin) as pay_real_coin,SUM(p.amount-p.coupon_amount) as actual_amount")
  207. ->where($where)
  208. ->group('p.gameid')
  209. ->select();
  210. $retData = array();
  211. $total_amt = $divide_amt = $total_cnt = $total_coupon_amount_amt = $actual_amount = 0;
  212. foreach ($GameChannelData as &$det) {
  213. if (intval($det['ratio_id'])) {
  214. $ratio_id = $det['ratio_id'];
  215. $ratio = floatval($det['ratio']);
  216. } else {
  217. $ratio_id = 0;
  218. $ratio = floatval($det['channel_split_ratio']);
  219. }
  220. //if(!$ratio){
  221. // $this->jsonResult('', 0, '您尚有游戏未配置分成比例,请先联系平台设置');
  222. //}
  223. //else{
  224. $det['ratio_id'] = $ratio_id;
  225. $det['ratio'] = $ratio;
  226. $det['amount_amt'] = floatval($det['pay_amt']-$det['coupon_amount_amt']);
  227. $total_cnt += floatval($det['pay_cnt']);
  228. $total_amt += floatval($det['pay_amt']);
  229. $total_coupon_amount_amt += floatval($det['coupon_amount_amt']);
  230. $divide_amt += floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  231. $det['divide_amt'] = floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  232. $actual_amount += floatval($det['pay_amt']-$det['coupon_amount_amt']);
  233. $det['actual_amount'] = floatval($det['pay_amt']-$det['coupon_amount_amt']);
  234. //}
  235. }
  236. $settle = array();
  237. $settle['total_amount'] = $total_amt;
  238. $settle['divide_amount'] = $divide_amt;
  239. $settle['total_cnt'] = $total_cnt;
  240. $settle['total_coupon_amount_amt'] = $total_coupon_amount_amt;
  241. $detPaylist = Db::table('cy_pay')->alias('p')
  242. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  243. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  244. ->field("p.orderid AS orderid,p.gameid AS game_id,g.name as game_name,p.amount AS amount,round((p.amount-p.coupon_amount)*(case when d.id then d.ratio else g.channel_split_ratio end)/100,2) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.serverid,p.servername,p.rolename,p.userid,p.username,p.create_time,p.coupon_amount,round(p.amount-p.coupon_amount,2) as actual_amount,p.real_coin")
  245. ->where($where)
  246. ->order("p.id desc")
  247. ->paginate(['list_rows' => $list_rows, 'page' => $page])
  248. ->toArray();
  249. // var_dump($detPaylist);
  250. $retData['settle'] = $settle;
  251. $retData['settleDet'] = $GameChannelData;
  252. $retData['detPaylist'] = $detPaylist;
  253. $this->jsonResult($retData, 20000, '获取结算列表成功');
  254. }
  255. exit;
  256. } else {
  257. $this->jsonResult('', 0, '非法请求');
  258. exit;
  259. }
  260. }
  261. /**
  262. * 结算处理
  263. */
  264. public function doSettle()
  265. {
  266. if ($this->request->isPost()) {
  267. $total_amount = $this->input('total_amount', 0, 'floatval'); //流水
  268. $divide_amount = $this->input('divide_amount', 0, 'floatval'); //分成金额
  269. $total_cnt = $this->input('total_cnt', 0, 'intval'); //结算订单笔数
  270. $remark = $this->input('remark', '', 'trim'); //备注
  271. if (!$total_amount || !$divide_amount || !$total_cnt) {
  272. $this->jsonResult('', 0, '没有可结算单的订单');
  273. } else if ($divide_amount < 100) {
  274. $this->jsonResult('', 0, '结算金额不能小于100元!');
  275. }
  276. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type")->where(['channel_id' => $this->_channelId, 'apply_status' => 1])->find();
  277. if (empty($channelInfoApply)) {
  278. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行结算');
  279. }
  280. //外放的无法结算
  281. if (isset(session('guild_info')['roles'][0]) && session('guild_info')['roles'][0] == 4) {
  282. $this->jsonResult('', 0, '您无权限使用该功能');
  283. }
  284. $orderid = 'ZFS' . makeOrderid();
  285. $channelInfo = model("Channel")->field("id,name,level,cps_settle_period,mcps_settle_period,status")->where(['id' => $this->_channelId])->find();
  286. if (empty($channelInfo)) {
  287. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  288. }
  289. if ($channelInfo['cps_settle_period'] == 2) { //日结
  290. $this->lastTime = strtotime(date('Y-m-d')) - 1;
  291. if (!(date('Hi') >= '0200' && date('Hi') < '2359')) {
  292. $this->jsonResult('', 0, '不在结算时间内,请在每日02:00 ~ 23:59分内提交结算申请!');
  293. }
  294. } else { //周结
  295. $this->lastTime = strtotime(date('Y-m-d', (time() - ((date('w', time()) == 0 ? 7 : date('w', time())) - 1) * 24 * 3600))) - 1;
  296. //if(date("w")<>1 || !(date('Hi') >= '0200' && date('Hi') < '2359')){
  297. //$this->jsonResult('', 0, '不在结算时间内,请在每周一02:00~23:59 内提交结算申请!');
  298. //}
  299. if (!(date('Hi') >= '0000' && date('Hi') < '2359')) {
  300. $this->jsonResult('', 0, '不在结算时间内,请在下周每日00:00~23:59 内提交结算申请!');
  301. }
  302. }
  303. $where = array();
  304. $where['p.status'] = 1;
  305. $where['p.create_time'] = array('elt', $this->lastTime);
  306. //所有下级渠道(包括自己)
  307. $channelIds = get_child_channel_arr($this->_channelId);
  308. array_push($channelIds, $this->_channelId);
  309. $where['p.channel_id'] = ['in', $channelIds];
  310. $where['p.settle_id'] = array('eq', 0);
  311. $where['g.id'] = ['in', model('game')->getTakingGame($this->_channelId)];
  312. // var_dump($where);
  313. $GameChannelData = Db::table('cy_pay')->alias('p')
  314. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  315. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  316. ->field("p.gameid AS game_id,g.name as game_name,g.channel_split_ratio,min(p.create_time) as begin_time,max(p.create_time) as end_time,count(*) as pay_cnt,SUM(p.amount) AS pay_amt,d.id as ratio_id,d.ratio,SUM(p.real_amount) AS pay_real_amt,SUM(p.real_ptb) AS pay_real_ptb,SUM(p.coupon_amount) as coupon_amount_amt,SUM(p.real_coin) as pay_real_coin,SUM(p.amount-p.coupon_amount) as actual_amount")
  317. ->where($where)
  318. ->group('p.gameid')
  319. ->select();
  320. $retData = array();
  321. // echo Db::table('cy_pay')->getLastSql()."----sql-----------<br>";
  322. $totalAmount = $divideAmount = $totalCnt =$total_coupon_amount_amt = $actual_amount = 0;
  323. $beginTime = time();
  324. $endTime = 0;
  325. foreach ($GameChannelData as &$det) {
  326. if (intval($det['ratio_id'])) {
  327. $ratio_id = $det['ratio_id'];
  328. $ratio = floatval($det['ratio']);
  329. } else {
  330. $ratio_id = 0;
  331. $ratio = floatval($det['channel_split_ratio']);
  332. }
  333. //if(!$ratio){
  334. // $this->jsonResult('', 0, '您尚有游戏未配置分成比例,请先联系平台设置');
  335. //}
  336. //else{
  337. $det['ratio_id'] = $ratio_id;
  338. $det['ratio'] = $ratio;
  339. $totalCnt += floatval($det['pay_cnt']);
  340. $totalAmount += floatval($det['pay_amt']);
  341. $total_coupon_amount_amt += floatval($det['coupon_amount_amt']);
  342. $divideAmount += floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  343. $det['divide_amt'] = floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  344. $actual_amount += floatval($det['pay_amt']-$det['coupon_amount_amt']);
  345. $det['actual_amount'] = floatval($det['pay_amt']-$det['coupon_amount_amt']);
  346. if ($beginTime > $det['begin_time']) {
  347. $beginTime = $det['begin_time'];
  348. }
  349. if ($endTime < $det['end_time']) {
  350. $endTime = $det['end_time'];
  351. }
  352. //}
  353. }
  354. reset($GameChannelData);
  355. if (!$totalAmount || !$divideAmount || !$totalCnt) {
  356. $this->jsonResult('', 0, '您已没有可结算的订单');
  357. }
  358. /*
  359. if($totalAmount<>$total_amount || $totalCnt<>$total_cnt || $divideAmount<>$divide_amount){
  360. $this->jsonResult('', 0, '结算数据与后台计算数据不一致,请重新核对后再提交');
  361. }
  362. */
  363. if (abs($totalAmount - $total_amount) > 0.01 || $totalCnt <> $total_cnt || abs($divideAmount - $divide_amount) > 0.01) {
  364. $this->jsonResult('', 0, '结算数据与后台计算数据不一致,请重新核对后再提交');
  365. }
  366. //指定时间内,禁止重复下单
  367. if (!requestDuplicateCheck('channel_settle_duplicate_' . $this->_channelId, $this->payRequestLimit)) {
  368. $this->jsonResult('', 0, '结算请求过多,请于' . $this->payRequestLimit . 's以后,再次进行结算操作');
  369. }
  370. $detPaylist = Db::table('cy_pay')->alias('p')
  371. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  372. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  373. ->field("p.id as payid,p.orderid AS orderid,p.gameid AS game_id,p.userid,p.username,g.name as game_name,g.channel_split_ratio,p.channel_id,p.amount AS amount,((p.amount-p.coupon_amount)*(case when d.id then d.ratio else g.channel_split_ratio end)/100) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.create_time,p.serverid,p.servername,p.rolename,p.coupon_amount,round(p.amount-p.coupon_amount,2) as actual_amount,p.real_coin")
  374. ->where($where)
  375. ->select();
  376. // 启动事务
  377. Db::startTrans();
  378. try {
  379. $settleData = array();
  380. $settleData['orderid'] = $orderid;
  381. $settleData['settle_type'] = 1;
  382. $settleData['channel_id'] = $this->_channelId;
  383. $settleData['channel_name'] = $this->_channelName;
  384. $settleData['total_amount'] = $totalAmount;
  385. $settleData['total_divide_amt'] = $divideAmount;
  386. $settleData['coupon_amount_amt'] = $total_coupon_amount_amt;
  387. $settleData['actual_amount'] = $actual_amount;
  388. $settleData['begin_time'] = $beginTime;
  389. $settleData['end_time'] = $endTime;
  390. $settleData['remark'] = $remark;
  391. $settleData['create_time'] = time();
  392. $settleData['first_audit_status'] = 1;
  393. $settleData['first_audit_time'] = time();
  394. if ($channelInfo['cps_settle_period'] == 2) {
  395. $settleData['settle_period'] = 2;
  396. } else {
  397. $settleData['settle_period'] = 1;
  398. }
  399. $insertSettleId = model('ChannelDivideSettle')->insertGetId($settleData);
  400. if (!$insertSettleId) {
  401. throw new Exception("添加结算单失败");
  402. }
  403. while (list($key, $val) = @each($GameChannelData)) {
  404. $detData = array();
  405. $detData['settle_id'] = $insertSettleId;
  406. $detData['divide_id'] = $val['ratio_id'];
  407. $detData['begin_time'] = $val['begin_time'];
  408. $detData['end_time'] = $val['end_time'];
  409. $detData['game_id'] = $val['game_id'];
  410. $detData['channel_id'] = $this->_channelId;
  411. $detData['ratio'] = $val['ratio'];
  412. $detData['pay_cnt'] = $val['pay_cnt'];
  413. $detData['pay_amt'] = floatval($val['pay_amt']);
  414. $detData['pay_real_amt'] = floatval($val['pay_real_amt']);
  415. $detData['pay_real_ptb'] = floatval($val['pay_real_ptb']);
  416. $detData['divide_amt'] = floatval($val['divide_amt']);
  417. $detData['coupon_amount_amt'] = floatval($val['coupon_amount_amt']);
  418. $detData['actual_amount'] = $val['actual_amount'];
  419. $detData['pay_real_coin'] = $val['pay_real_coin'];
  420. $detData['create_time'] = time();
  421. ${'insertDet' . $val['game_id'] . 'ID'} = model('ChannelDivideSettleDet')->insertGetId($detData);
  422. if (!${'insertDet' . $val['game_id'] . 'ID'}) {
  423. throw new Exception("添加结算单明细失败");
  424. }
  425. }
  426. $orderIds = array();
  427. while (list($key, $val) = @each($detPaylist)) {
  428. // echo ${'insertDet'.$val['game_id'].'ID'}."---insertDet".$val['game_id']."ID'--------<br>";
  429. $orderIds[] = $val['payid'];
  430. $detPayData['settle_id'] = $insertSettleId;
  431. $detPayData['settle_det_id'] = ${'insertDet' . $val['game_id'] . 'ID'};
  432. $detPayData['orderid'] = $val['orderid'];
  433. $detPayData['game_id'] = $val['game_id'];
  434. $detPayData['channel_id'] = $val['channel_id'];
  435. $detPayData['ratio'] = $val['ratio'];
  436. $detPayData['userid'] = $val['userid'];
  437. $detPayData['username'] = $val['username'];
  438. $detPayData['amount'] = floatval($val['amount']);
  439. $detPayData['real_amount'] = floatval($val['real_amount']);
  440. $detPayData['real_ptb'] = floatval($val['real_ptb']);
  441. $detPayData['divide_amt'] = floatval($val['divide_amt']);
  442. $detPayData['coupon_amount'] = floatval($val['coupon_amount']);
  443. $detPayData['serverid'] = trim($val['serverid']);
  444. $detPayData['servername'] = trim($val['servername']);
  445. $detPayData['rolename'] = trim($val['rolename']);
  446. $detPayData['create_time'] = $val['create_time'];
  447. $detPayData['actual_amount'] = $val['actual_amount'];
  448. $detPayData['real_coin'] = $val['real_coin'];
  449. $detPayData['add_time'] = time();
  450. // var_dump($detPayData);
  451. $insertDetPayId = model('ChannelDivideSettleDetPay')->insertGetId($detPayData);
  452. if (!$insertDetPayId) {
  453. throw new Exception("添加结算订单明细失败");
  454. }
  455. }
  456. $updPayData = array();
  457. $updPayData['settle_channel_id'] = $this->_channelId;
  458. $updPayData['settle_id'] = $insertSettleId;
  459. $updPayData['settle_update_time'] = time();
  460. /*
  461. $gameids = model('Game')->where(['game_kind'=>1])->column('id');
  462. $where['p.gameid']= array('IN',$gameids);
  463. */
  464. unset($where['g.id']);
  465. $where['p.id'] = array('IN', $orderIds);
  466. $updResult = model('Pay')->alias('p')->where($where)->update($updPayData);
  467. if (!$updResult) {
  468. throw new Exception("更新订单为已结算失败");
  469. }
  470. $this->redis->del('channel_settle_duplicate_' . $this->_channelId);
  471. // 提交事务
  472. Db::commit();
  473. } catch (\Exception $e) {
  474. // 回滚事务
  475. Db::rollback();
  476. $this->jsonResult('', 0, '结算申请生成失败' . $e->getMessage());
  477. }
  478. $result['orderid'] = $orderid;
  479. $template = '有新的专服结算申请:会长"' . $this->_channelName . '" 发起结算申请,订单号:' . $orderid . ' 。请及时安排处理,时间:' . date('Y-m-d H:i:s');
  480. $ddurl = Env::get('operat_url');
  481. curlDD($template, $ddurl, true);
  482. $this->jsonResult($result, 20000, '结算申请生成成功');
  483. exit;
  484. } else {
  485. $this->jsonResult('', 0, '非法请求');
  486. exit;
  487. }
  488. }
  489. /**
  490. * 未结算订单
  491. */
  492. public function unsettle()
  493. {
  494. if ($this->request->isPost()) {
  495. //判断当前账号是否有结算权限
  496. if (!in_array($this->_channelLevel, [1])) {
  497. $this->jsonResult('', 0, '您无权限进行结算相关操作');
  498. } else {
  499. $list_rows = input('list_rows', 10);
  500. $page = input('page', 1);
  501. $orderid = $this->input('orderid', '', 'trim'); //订单编号
  502. $game_id = $this->input('game_id', 0, 'intval'); //游戏ID
  503. $begin_time = $this->input('begin_time', '', 'trim');
  504. $end_time = $this->input('end_time', '', 'trim');
  505. $download = $this->input('download', 0, 'intval');
  506. $where = array();
  507. $where['p.status'] = 1;
  508. if ($orderid) {
  509. $where['p.orderid'] = $orderid;
  510. }
  511. if ($game_id) {
  512. if (in_array($game_id, model('game')->getTakingGame($this->_channelId))) {
  513. $where['p.gameid'] = $game_id;
  514. } else {
  515. $this->jsonResult('', 20000, '获取未结算订单列表成功');
  516. }
  517. } else {
  518. $where['p.gameid'] = ['in', model('game')->getTakingGame($this->_channelId)];
  519. }
  520. //申请开始时间和结束时间不为空时
  521. if ($begin_time != '' && $end_time != '') {
  522. $where['p.create_time'] = [
  523. ['>=', strtotime($begin_time)],
  524. ['<=', strtotime($end_time . ' 23:59:59')],
  525. ];
  526. } //开始时间不为空时
  527. elseif ($begin_time != '') {
  528. $where['p.create_time'] = ['>=', strtotime($begin_time)];
  529. } //结束时间不为空时
  530. elseif ($end_time != '') {
  531. $where['p.create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  532. }
  533. //所有下级渠道(包括自己)
  534. $channelIds = get_child_channel_arr($this->_channelId);
  535. array_push($channelIds, $this->_channelId);
  536. $where['p.channel_id'] = ['in', $channelIds];
  537. $where['p.settle_id'] = array('eq', 0);
  538. // var_dump($where);
  539. $GameChannelData = Db::table('cy_pay')->alias('p')
  540. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  541. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  542. ->field("p.gameid AS game_id,g.name as game_name,g.channel_split_ratio,count(*) as pay_cnt,SUM(p.pay_amount) AS pay_amt,d.id as ratio_id,d.ratio,SUM(p.pay_amount) AS pay_real_amt,SUM(p.real_ptb) AS pay_real_ptb,SUM(p.coupon_amount) as coupon_amount_amt")
  543. ->where($where)
  544. ->group('p.gameid')
  545. ->select();
  546. $retData = array();
  547. $total_amt = $divide_amt = $total_cnt = $total_coupon_amount_amt = $actual_amount = 0;
  548. foreach ($GameChannelData as &$det) {
  549. if (intval($det['ratio_id'])) {
  550. $ratio_id = $det['ratio_id'];
  551. $ratio = floatval($det['ratio']);
  552. } else {
  553. $ratio_id = 0;
  554. $ratio = floatval($det['channel_split_ratio']);
  555. }
  556. //if(!$ratio){
  557. // $this->jsonResult('', 0, '您尚有游戏未配置分成比例,请先联系平台设置');
  558. //}
  559. //else{
  560. $det['ratio_id'] = $ratio_id;
  561. $det['ratio'] = $ratio;
  562. $total_cnt += floatval($det['pay_cnt']);
  563. $total_amt += floatval($det['pay_amt']);
  564. $total_coupon_amount_amt += floatval($det['coupon_amount_amt']);
  565. $divide_amt += floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  566. $det['divide_amt'] = floatval(intval(($det['pay_amt']-$det['coupon_amount_amt']) * $det['ratio']) / 100);
  567. $actual_amount += floatval($det['pay_amt']-$det['coupon_amount_amt']);
  568. $det['actual_amount'] = floatval($det['pay_amt']-$det['coupon_amount_amt']);
  569. //}
  570. }
  571. $settle = array();
  572. $settle['total_amount'] = priceFormat($total_amt);
  573. $settle['divide_amount'] = priceFormat($divide_amt);
  574. $settle['total_cnt'] = priceFormat($total_cnt);
  575. $settle['total_coupon_amount_amt'] = priceFormat($total_coupon_amount_amt);
  576. $settle['actual_amount'] = priceFormat($actual_amount);
  577. if ($download) {
  578. $sql = Db::table('cy_pay')->alias('p')
  579. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  580. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  581. ->field("p.orderid AS orderid,p.gameid AS game_id,g.name as game_name,p.amount AS amount,round((p.pay_amount-p.coupon_amount)*(case when d.id then d.ratio else g.channel_split_ratio end)/100,2) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.serverid,p.servername,p.rolename,p.userid,p.username,p.create_time,p.coupon_amount,round((p.pay_amount-p.coupon_amount),2) as actual_amount")
  582. ->where($where)
  583. ->order("p.id desc")
  584. ->fetchSql(true)
  585. ->select();
  586. // echo $sql;
  587. // if ((new MakeReport())->addTask('guild.ChannelSettleUnsettle', $sql, 'cps' . session('guild_info')['id'])) {
  588. if ((new MakeReportGo())->addTask('guild.ChannelSettleUnsettle', $sql, 'cps' . session('guild_info')['id'])) {
  589. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  590. } else {
  591. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  592. }
  593. }
  594. $detPaylist = Db::table('cy_pay')->alias('p')
  595. ->join('cy_game g', 'p.gameid = g.id and g.game_kind=1', 'inner')
  596. ->join('nw_game_channel_divide d', 'p.gameid = d.game_id and d.channel_id=' . $this->_channelId, 'left')
  597. ->field("p.orderid AS orderid,p.gameid AS game_id,g.name as game_name,p.amount AS amount,round((p.pay_amount-p.coupon_amount)*(case when d.id then d.ratio else g.channel_split_ratio end)/100,2) AS divide_amt,case when d.id then d.id else '0' end as ratio_id,case when d.id then d.ratio else g.channel_split_ratio end as ratio,p.real_amount AS real_amount,p.real_ptb AS real_ptb,p.serverid,p.servername,p.rolename,p.userid,p.username,p.create_time,p.coupon_amount,round((p.pay_amount-p.coupon_amount),2) as actual_amount")
  598. ->where($where)
  599. ->order("p.id desc")
  600. ->paginate(['list_rows' => $list_rows, 'page' => $page])
  601. ->toArray();
  602. // var_dump($detPaylist);
  603. $retData['settle'] = $settle;
  604. // $retData['settleDet'] = $GameChannelData;
  605. $retData['detPaylist'] = $detPaylist;
  606. $this->jsonResult($retData, 20000, '获取未结算订单列表成功');
  607. }
  608. exit;
  609. } else {
  610. $this->jsonResult('', 0, '非法请求');
  611. exit;
  612. }
  613. }
  614. }