FinanceService.php 21 KB

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