优惠券订单及其他脚本

order_balance_month.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hy
  5. * Date: 2019-04-12
  6. * Time: 13:53
  7. */
  8. #佣金结算脚本 每月20号结算上个月
  9. require_once 'DB_PDO.class.php';
  10. require_once 'conf.class.php';
  11. date_default_timezone_set('PRC');
  12. set_time_limit(0);
  13. ini_set('memory_limit','2048M');
  14. #连接数据库
  15. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  16. #查询上月份结算信息
  17. $stime = date('Y-m-01', strtotime('-1 month')).' 00:00:00';
  18. $etime = date('Y-m-01').' 00:00:00';
  19. $sql = "SELECT user_id,sum(rebate) as money from `order_rebate` where order_balance_at>=:stime and order_balance_at<:etime and status=2 and maintain_power<3 group by user_id ";
  20. echo "beginTime:".$stime." -> endTime:".$etime."\n";
  21. $stmt = $_PDO->prepare($sql);
  22. $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
  23. $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  24. if( empty($rebateInfo) ){
  25. exit('未获取到上月结算信息'."\n");
  26. }
  27. $totalcount = count($rebateInfo);
  28. $success = 0;
  29. $fail = 0;
  30. $already = 0;
  31. foreach( $rebateInfo as $k=>$v){
  32. //判断是否已结算过
  33. $sql = "SELECT * from account_detail where type=:type and user_id=:user_id and create_at>:etime";
  34. $stmt = $_PDO->prepare($sql);
  35. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':etime'=>$etime));
  36. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  37. if( !empty($res) ){
  38. $already++;
  39. echo "用户已结算过user_id:".$v['user_id']."\n";
  40. continue;
  41. }
  42. $sql = "SELECT sum(rebate) as money from account_detail where type=:type and user_id=:user_id and balance_at>=:stime and balance_at<:etime and state_of_embodiment=1";
  43. $stmt = $_PDO->prepare($sql);
  44. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>3,':stime' => $stime,':etime'=>$etime));
  45. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  46. $money = $v['money'];
  47. # 上次月结算负值特殊处理
  48. $sql = "SELECT rebate from account_detail where type=:type and user_id=:user_id order by id desc limit 1";
  49. $stmt = $_PDO->prepare($sql);
  50. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1));
  51. $last_info = $stmt->fetch(PDO::FETCH_ASSOC);
  52. $last_balance = 0;
  53. if( !empty($last_info) ){
  54. $last_balance = $last_info['rebate'];
  55. }
  56. if( !empty($res)){ //减去预结算的金额, 作为本月结算
  57. $money = $money - $res['money'];
  58. $already_money = $res['money'];
  59. }else{
  60. $already_money = 0;
  61. }
  62. if($last_balance < 0){
  63. $money = $money + $last_balance;
  64. }
  65. $_PDO->beginTransaction();
  66. try{
  67. $sql = "INSERT INTO account_detail(user_id,type,rebate,balance_at,order_total_rebate,order_total_price,last_month_debt) values(:user_id,:type,:rebate,:balance_at,:order_total_rebate,:order_total_price,:last_month_debt)";
  68. $stmt = $_PDO->prepare($sql);
  69. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':rebate'=>$money,':balance_at'=>$stime,':order_total_rebate'=>$v['money'],':order_total_price'=>$already_money,':last_month_debt'=>$last_balance));
  70. $res = $_PDO->lastinsertid();
  71. //echo "插入结果:".$res."\n";
  72. if($res){
  73. #月结成功,把上个月未打款的日结算变为‘已记入尾款’
  74. $sql = "UPDATE account_detail set state_of_embodiment=3 where type=:type and user_id=:user_id and balance_at>=:stime and balance_at<:etime and state_of_embodiment=0";
  75. $stmt = $_PDO->prepare($sql);
  76. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>3,':stime' => $stime,':etime'=>$etime));
  77. $upres = $stmt->rowCount();
  78. $_PDO->commit();
  79. $success++;
  80. }else{
  81. $_PDO->rollback();
  82. $fail++;
  83. }
  84. }catch(PDOException $e){
  85. $_PDO->rollback();
  86. echo "err_msg:".$e->getMessage()." user_id".$v['user_id']."\n";
  87. $fail++;
  88. continue;
  89. }
  90. }
  91. echo "成功结算笔数:".$success."\n";
  92. echo "已结算过笔数:".$already."\n";
  93. echo "失败结算笔数:".$fail."\n";