FinanceService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/1
  6. * Time: 下午7:57
  7. */
  8. namespace App\Modules\Finance\Services;
  9. use App\Consts\BillConst;
  10. use App\Modules\Channel\Services\ChannelService;
  11. use App\Modules\Finance\Models\FinancialStat;
  12. use App\Modules\Finance\Models\Payment;
  13. use App\Modules\Finance\Models\WithdrawCash;
  14. use App\Modules\Trade\Services\OrderService;
  15. use DB;
  16. class FinanceService
  17. {
  18. /**
  19. * 昨天,结算、清算 任务数据生成
  20. */
  21. public static function makeDayDataStatistics()
  22. {
  23. $yesterdayStart = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
  24. $yesterdayEnd = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 23:59:59";
  25. /*$yesterdayStart = date('Y-m-d')." 00:00:00";
  26. $yesterdayEnd = date('Y-m-d')." 23:59:59";*/
  27. //$yesterdayEnd = date('Y-m-d', strtotime(date("Y-m-d")))." 00:00:00";
  28. $not_in_pay_merchant_idStr = env('not_in_pay_merchant_id');
  29. $not_in_pay_merchant_id = [];
  30. if ($not_in_pay_merchant_idStr) {
  31. $not_in_pay_merchant_id = explode(',', $not_in_pay_merchant_idStr);
  32. }
  33. $is_test = false;// 测试代码
  34. $channels = ChannelService::getAllChannels();
  35. foreach ($channels as $key=>$channel) {
  36. // print_r($channel->id."=".$channel->name."\n");
  37. $channelId = $channel->id;
  38. if($is_test){
  39. $channelId = 2;
  40. if($key==1){
  41. break;
  42. }
  43. }
  44. $params = [
  45. 'distribution_channel_id' => $channelId,
  46. 'begin_time' => $yesterdayStart,
  47. 'end_time' => $yesterdayEnd,
  48. //'not_in_pay_merchant_id'=>$not_in_pay_merchant_id,
  49. 'is_self_channel' => 1
  50. ];
  51. //$rechargeAmount = OrderService::getAmount($params);
  52. $rechargeAmount = OrderService::getAmountV2($params);
  53. $rechargeAmount = $rechargeAmount ? $rechargeAmount : 0;
  54. \Log::info('recharge_amount:' . $channelId . ':' . $rechargeAmount);
  55. $params_ = [
  56. 'distribution_channel_id' => $channelId,
  57. 'start_time' => $yesterdayStart,
  58. 'end_time' => $yesterdayEnd,
  59. 'is_self_channel' => 1
  60. ];
  61. $rechargeAmountByPayMerchant = OrderService::getRechargeAmountGroupByPayMerchant($params_);
  62. $rechargeAmountByPayMerchants = json_decode(json_encode($rechargeAmountByPayMerchant), true);
  63. \Log::info('rechargeAmountByPayMerchant:' . json_encode($rechargeAmountByPayMerchants));
  64. //dd($rechargeAmount);
  65. //生成结算总额结算单
  66. BillService::addBill($channelId, $rechargeAmount);
  67. BillPayMerchantService::addPayMerchantsBills($rechargeAmountByPayMerchants);
  68. // print_r($channelId."=结算总额=".$rechargeAmount."\n");
  69. $rate = BillService::getRate($channelId);
  70. //生成每日清算明细 Liquidation
  71. $orderByChannels = OrderService::getPayMerchantSourceAmount($params);
  72. // print_r($orderByChannels);
  73. foreach ($orderByChannels as $k => $item) {
  74. // 服务费
  75. $serviceAmount = 0;
  76. if (time() >= strtotime(BillConst::SERVICE_START)) {
  77. $serviceAmount = (float)$item * BillConst::SERVICE_RATE;
  78. // 保留2位截断小数
  79. $serviceAmount = getFloorNum($serviceAmount,2);
  80. }
  81. // 需要减去服务费
  82. $item = (float)$item - $serviceAmount;
  83. //总金额 - (总金额 * 结算比例) = 清算金额
  84. $amountTmp = $item - ($item * $rate);
  85. \Log::info('item:'.$item.' rate:'.$rate.' serviceAmount:'.$serviceAmount.' $amountTmp:'.$amountTmp);
  86. // 保留2位截断小数
  87. $amountTmp = getFloorNum($amountTmp,2);
  88. //添加清算记录
  89. if ($k == config('common.tonglianpay') || $k == config('common.officialpay')) {
  90. //官方,通联,每天自动结算,不用清算数据
  91. } else {
  92. LiquidationService::addLiquidation($channelId, $k, $amountTmp, 0, "每日结算");
  93. }
  94. // //更新清算汇总表数据 LiquidatedStat
  95. LiquidatedStatService::createOrUpdate($k, $amountTmp);
  96. // //更新 支付通道可提现总额 FinanceMerchantStat
  97. FinanceMerchantStatService::createOrUpdate($channelId, $k, $amountTmp);
  98. // print_r($channelId."= 支付渠道=".$k."= 结算=".$item."= 清算=".$amountTmp."\n");
  99. }
  100. // print_r($channelId."=".$channelName."=".$rechargeAmount."\n");
  101. }
  102. }
  103. /**
  104. * 获取渠道账户余额(可提现金额)
  105. * @param $channelId
  106. * @return int
  107. */
  108. public static function getChannelAccountBalance($channelId)
  109. {
  110. $accountBalance = FinancialStat::getEnableWithdrawalAmount($channelId);
  111. return $accountBalance;
  112. }
  113. /**
  114. * 获取所有渠道账户余额总额(可提现金额总额)
  115. */
  116. public static function getChannelAccountBalanceAll()
  117. {
  118. $accountBalance = FinancialStat::getEnableWithdrawalAmount();
  119. return $accountBalance;
  120. }
  121. /**
  122. * @return mixed
  123. * {Number} yesterday_amount_audit_success 昨日已审核金额
  124. * {Number} all_amount_audit_success 所有已审核金额
  125. * {Number} yesterday_amount_apply 昨日申请提现金额
  126. * {Number} all_amount_apply 所有申请提现金额
  127. * {Number} yesterday_amount_audit_fail 昨日审核不通过
  128. * {Number} all_amount_audit_fail 所有审核不通过
  129. * {Number} yesterday_amount_wait 昨日待打款金额
  130. * {Number} all_amount_wait 所有待打款金额
  131. * {Number} yesterday_amount_apply_success 昨日成功提现金额
  132. * {Number} all_amount_apply_success 所有成功提现金额
  133. * {Number} yesterday_amount_apply_person_success 昨日对私成功提现金额
  134. * {Number} all_amount_apply_person_success 所有对私成功提现金额
  135. * {Number} yesterday_amount_apply_company_success 昨日对公成功提现金额
  136. * {Number} all_amount_apply_company_success 所有对公成功提现金额
  137. * {Number} yesterday_amount_apply_success 昨日成功提现金额
  138. * {Number} all_amount_apply_success 所有成功提现金额
  139. * {Number} yesterday_apply_num 昨日累积提现笔数
  140. * {Number} all_apply_num 所有累积提现笔数
  141. * {Number} yesterday_tallage_count 昨日累积提现手续费
  142. * {Number} all_stallage_count 所有累积提现手续费
  143. */
  144. public static function getDataStatisticsWithdrawCash()
  145. {
  146. $startDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
  147. $endDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 23:59:59";
  148. //昨日已审核金额
  149. $yesterday_amount_audit_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrSuccessFailCheckedSuccessList())
  150. ->where('distribution_channel_id', '>', 0)
  151. ->where('updated_at', '>=', $startDay)
  152. ->where('updated_at', '<=', $endDay)
  153. ->sum('amount');
  154. $data['yesterday_amount_audit_success'] = $yesterday_amount_audit_success;
  155. //所有已审核金额
  156. $all_amount_audit_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrSuccessFailCheckedSuccessList())
  157. ->where('distribution_channel_id', '>', 0)
  158. ->sum('amount');
  159. $data['all_amount_audit_success'] = $all_amount_audit_success;
  160. //昨日申请提现金额
  161. $yesterday_amount_apply = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(0))
  162. ->where('distribution_channel_id', '>', 0)
  163. ->where('created_at', '>=', $startDay)
  164. ->where('created_at', '<=', $endDay)
  165. ->sum('amount');
  166. $data['yesterday_amount_apply'] = $yesterday_amount_apply;
  167. //所有申请提现金额 待审核
  168. $all_amount_apply = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(0))
  169. ->where('distribution_channel_id', '>', 0)
  170. ->sum('amount');
  171. $data['all_amount_apply'] = $all_amount_apply;
  172. //昨日审核不通过
  173. $yesterday_amount_audit_fail = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(9))
  174. ->where('distribution_channel_id', '>', 0)
  175. ->where('updated_at', '>=', $startDay)
  176. ->where('updated_at', '<=', $endDay)
  177. ->sum('amount');
  178. $data['yesterday_amount_audit_fail'] = $yesterday_amount_audit_fail;
  179. //所有审核不通过
  180. $all_amount_audit_fail = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(9))
  181. ->where('distribution_channel_id', '>', 0)
  182. ->sum('amount');
  183. $data['all_amount_audit_fail'] = $all_amount_audit_fail;
  184. //昨日待打款金额 待打款
  185. $yesterday_amount_wait = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(10))
  186. ->where('distribution_channel_id', '>', 0)
  187. ->where('updated_at', '>=', $startDay)
  188. ->where('updated_at', '<=', $endDay)
  189. ->sum('amount');
  190. $data['yesterday_amount_wait'] = $yesterday_amount_wait;
  191. //所有待打款金额 待打款
  192. $all_amount_wait = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(10))
  193. ->where('distribution_channel_id', '>', 0)
  194. ->sum('amount');
  195. $data['all_amount_wait'] = $all_amount_wait;
  196. //所有待打款金额 待打款 笔数
  197. $all_amount_wait_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(10))
  198. ->where('distribution_channel_id', '>', 0)
  199. ->count('amount');
  200. $data['all_amount_wait_num'] = $all_amount_wait_num;
  201. //昨日对私打款成功
  202. $yesterday_amount_apply_person_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  203. ->where('distribution_channel_id', '>', 0)
  204. ->where('updated_at', '>=', $startDay)
  205. ->where('updated_at', '<=', $endDay)
  206. ->where('is_company', 0)
  207. ->sum('amount');
  208. $data['yesterday_amount_apply_person_success'] = $yesterday_amount_apply_person_success;
  209. //所有对私打款成功
  210. $all_amount_apply_person_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  211. ->where('distribution_channel_id', '>', 0)
  212. ->where('is_company', 0)
  213. ->sum('amount');
  214. $data['all_amount_apply_person_success'] = $all_amount_apply_person_success;
  215. //昨日对公打款成功
  216. $yesterday_amount_apply_company_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  217. ->where('distribution_channel_id', '>', 0)
  218. ->where('updated_at', '>=', $startDay)
  219. ->where('updated_at', '<=', $endDay)
  220. ->where('is_company', 1)
  221. ->sum('amount');
  222. $data['yesterday_amount_apply_company_success'] = $yesterday_amount_apply_company_success;
  223. //所有对公打款成功
  224. $all_amount_apply_company_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  225. ->where('distribution_channel_id', '>', 0)
  226. ->where('is_company', 1)
  227. ->sum('amount');
  228. $data['all_amount_apply_company_success'] = $all_amount_apply_company_success;
  229. //昨日成功提现金额
  230. $yesterday_amount_apply_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  231. ->where('distribution_channel_id', '>', 0)
  232. ->where('updated_at', '>=', $startDay)
  233. ->where('updated_at', '<=', $endDay)
  234. ->sum('amount');
  235. $data['yesterday_amount_apply_success'] = $yesterday_amount_apply_success;
  236. //所有成功提现金额
  237. $all_amount_apply_success = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  238. ->where('distribution_channel_id', '>', 0)
  239. ->sum('amount');
  240. $data['all_amount_apply_success'] = $all_amount_apply_success;
  241. //昨日累积提现笔数
  242. $yesterday_apply_num = WithdrawCash::where('created_at', '>=', $startDay)
  243. ->where('distribution_channel_id', '>', 0)
  244. ->where('created_at', '<=', $endDay)
  245. ->count('amount');
  246. $data['yesterday_apply_num'] = $yesterday_apply_num;
  247. //所有累积提现笔数
  248. $all_apply_num = WithdrawCash::where('distribution_channel_id', '>', 0)->count('id');
  249. $data['all_apply_num'] = $all_apply_num;
  250. //昨日累积提现手续费
  251. $yesterday_tallage_count = WithdrawCash::
  252. where('distribution_channel_id', '>', 0)
  253. ->where('created_at', '>=', $startDay)
  254. ->where('created_at', '<=', $endDay)
  255. ->sum('tallage');
  256. $data['yesterday_tallage_count'] = $yesterday_tallage_count;
  257. //所有累积提现手续费
  258. $all_stallage_count = WithdrawCash::where('distribution_channel_id', '>', 0)->sum('tallage');
  259. $data['all_stallage_count'] = $all_stallage_count;
  260. return $data;
  261. }
  262. /**
  263. * @return mixed
  264. * {Number} yesterday_payment_count 昨日累积打款金额
  265. * {Number} all_payment_count 所有累积打款金额
  266. */
  267. public static function getDataStatisticsPayment()
  268. {
  269. $startDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
  270. $endDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 23:59:59";
  271. //昨日累积打款金额
  272. $yesterday_payment_count = Payment::
  273. whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  274. ->where('updated_at', '>=', $startDay)
  275. ->where('updated_at', '<=', $endDay)
  276. ->sum('amount');
  277. //所有累积打款金额
  278. $all_payment_count = Payment::
  279. whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  280. ->sum('amount');
  281. $data['yesterday_payment_count'] = $yesterday_payment_count;
  282. $data['all_payment_count'] = $all_payment_count;
  283. return $data;
  284. }
  285. /**
  286. * @return mixed
  287. * {Number} all_amount_apply_success_count 所有累积已提现金额
  288. * {Number} all_amount_apply_count 所有累积可提现金额
  289. */
  290. public static function getDataStatisticsFinancialStat()
  291. {
  292. $startDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
  293. $endDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 23:59:59";
  294. //所有累积已提现金额
  295. //$all_amount_apply_success_count = FinancialStat::sum('accumulative_withdrawal_amount');
  296. $all_amount_apply_success_count = WithdrawCash::where('distribution_channel_id', '>', 0)->sum('amount');
  297. //所有累积可提现金额
  298. $all_amount_apply_count = FinancialStat::sum('enable_withdrawal_amount');
  299. $data['all_amount_apply_success_count'] = $all_amount_apply_success_count;
  300. $data['all_amount_apply_count'] = $all_amount_apply_count;
  301. return $data;
  302. }
  303. /**
  304. * 生成昨日打款,审核报表
  305. * @param $channelId
  306. */
  307. public static function makeYesterdayWithdrawCashStatistics($channelId)
  308. {
  309. $startDay = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day")) . " 00:00:00";
  310. $endDay = date('Y-m-d', strtotime(date("Y-m-d"))) . " 00:00:00";
  311. if (!is_numeric($channelId)) {
  312. return;
  313. }
  314. print_r($channelId . "\n");
  315. $channelName = ChannelService::getChannelNicknameById($channelId);
  316. //["已打款", "手动已打款"]
  317. $yesterday_amount = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  318. ->where('distribution_channel_id', $channelId)
  319. ->where('updated_at', '>=', $startDay)
  320. ->where('updated_at', '<=', $endDay)
  321. ->sum('amount');
  322. //["已打款", "手动已打款"]
  323. $yesterday_amount_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  324. ->where('distribution_channel_id', $channelId)
  325. ->where('updated_at', '>=', $startDay)
  326. ->where('updated_at', '<=', $endDay)
  327. ->count('amount');
  328. $yesterday_amount_person = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  329. ->where('distribution_channel_id', $channelId)
  330. ->where('is_company', 0)
  331. ->where('updated_at', '>=', $startDay)
  332. ->where('updated_at', '<=', $endDay)
  333. ->sum('amount');
  334. //["已打款", "手动已打款"]
  335. $yesterday_amount_person_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  336. ->where('distribution_channel_id', $channelId)
  337. ->where('is_company', 0)
  338. ->where('updated_at', '>=', $startDay)
  339. ->where('updated_at', '<=', $endDay)
  340. ->count('amount');
  341. $yesterday_amount_company = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  342. ->where('distribution_channel_id', $channelId)
  343. ->where('is_company', 1)
  344. ->where('updated_at', '>=', $startDay)
  345. ->where('updated_at', '<=', $endDay)
  346. ->sum('amount');
  347. //["已打款", "手动已打款"]
  348. $yesterday_amount_company_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  349. ->where('distribution_channel_id', $channelId)
  350. ->where('is_company', 1)
  351. ->where('updated_at', '>=', $startDay)
  352. ->where('updated_at', '<=', $endDay)
  353. ->count('amount');
  354. //["已打款", "手动已打款"]
  355. $yesterday_tallage = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(30))
  356. ->where('distribution_channel_id', $channelId)
  357. ->where('updated_at', '>=', $startDay)
  358. ->where('updated_at', '<=', $endDay)
  359. ->sum('tallage');
  360. //["审核不通过"]
  361. $yesterday_amount_audit_fail = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(9))
  362. ->where('distribution_channel_id', $channelId)
  363. ->where('updated_at', '>=', $startDay)
  364. ->where('updated_at', '<=', $endDay)
  365. ->sum('amount');
  366. //["审核不通过"]
  367. $yesterday_amount_audit_fail_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(9))
  368. ->where('distribution_channel_id', $channelId)
  369. ->where('updated_at', '>=', $startDay)
  370. ->where('updated_at', '<=', $endDay)
  371. ->count('amount');
  372. //["打款失败"]
  373. $yesterday_amount_fail = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(40))
  374. ->where('distribution_channel_id', $channelId)
  375. ->where('updated_at', '>=', $startDay)
  376. ->where('updated_at', '<=', $endDay)
  377. ->sum('amount');
  378. //["打款失败"]
  379. $yesterday_amount_fail_num = WithdrawCash::whereIn('status', WithdrawCashService::getWithdrawCashStatusStrList(40))
  380. ->where('distribution_channel_id', $channelId)
  381. ->where('updated_at', '>=', $startDay)
  382. ->where('updated_at', '<=', $endDay)
  383. ->count('amount');
  384. $date = date('Y-m-d', strtotime(date("Y-m-d") . " -1 day"));
  385. $data['distribution_channel_id'] = $channelId;
  386. $data['distribution_channel_name'] = $channelName;
  387. $data['amount'] = $yesterday_amount;
  388. $data['amount_num'] = $yesterday_amount_num;
  389. $data['amount_person'] = $yesterday_amount_person;
  390. $data['amount_person_num'] = $yesterday_amount_person_num;
  391. $data['amount_company'] = $yesterday_amount_company;
  392. $data['amount_company_num'] = $yesterday_amount_company_num;
  393. $data['amount_audit_fail'] = $yesterday_amount_audit_fail;
  394. $data['amount_audit_fail_num'] = $yesterday_amount_audit_fail_num;
  395. $data['amount_fail'] = $yesterday_amount_fail;
  396. $data['amount_fail_num'] = $yesterday_amount_fail_num;
  397. $data['tallage'] = $yesterday_tallage;
  398. $data['date'] = $date;
  399. // print_r($channelId."=".$channelName."=".$yesterday_amount."\n");
  400. $dataDB = PaymentStatisticService::addPaymentStatistics($data);
  401. }
  402. /**
  403. * 用户提现手续费
  404. * @param $channelId
  405. * @param $amount
  406. * @return int
  407. */
  408. public static function getWithdrawCashTallage($channelId, $amount)
  409. {
  410. //TODO 根据条件算出提现扣税金额
  411. $tallage = 0;
  412. return $tallage;
  413. }
  414. }