优惠券订单及其他脚本

finance_export.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. define( "ROOT_PATH", dirname(dirname(__FILE__)) );
  3. require_once ROOT_PATH.'/DB_PDO.class.php';
  4. require_once ROOT_PATH.'/confv2.class.php';
  5. require_once ROOT_PATH.'/PHPExcel-1.8/Classes/PHPExcel.php';
  6. require_once ROOT_PATH.'/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
  7. require_once ROOT_PATH.'/PHPExcel-1.8/Classes/PHPExcel/Reader/Excel5.php';
  8. require_once ROOT_PATH.'/PHPMailer/ClassPhpMailer.php';
  9. /* 月底猎豆预估报表发给财务 */
  10. date_default_timezone_set('PRC');
  11. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  12. $month = date('Y-m');
  13. $stime = date('Y-m-01', strtotime($month));
  14. $etime = date('Y-m-d 23:59:59', strtotime($month.' +1 month -1 day'));
  15. $sql = " select left(`create_time`, 10) as day,sum(`pub_share_pre_fee`) as money from `liedou_taoke_orders` where trade_id in(
  16. select order_id from `order_rebate` where status>0 and order_create_at>='{$stime}' and order_create_at<'{$etime}' and `maintain_power`<3
  17. ) group by day order by day ";
  18. $stmt = $_PDO->prepare($sql);
  19. $stmt->execute();
  20. $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. foreach ($result as $k => &$v) {
  22. $v['money'] = $v['money'] * 100; //转成分
  23. #佣金
  24. $v['rebate'] = get_rebate( $v['day'] );
  25. $v['rebate_profit'] = get_rebate_profit( $v['day'] );
  26. $v['remain'] = $v['money'] - $v['rebate'] - $v['rebate_profit'];
  27. $v['remain_rate'] = $v['remain'] / $v['money'];
  28. }
  29. $m = date('m', strtotime($month));
  30. $indexKey = ['day', 'money', 'rebate', 'rebate_profit', 'remain', 'remain_rate'];
  31. $title = ['日期', '总佣金(分)', '分配佣金(分)', '分配津贴(分)', '系统留存(分)', '留存占比'];
  32. $filename = '猎豆'. $m .'月份预估佣金统计_'.date('Y-m-d').'.xlsx';
  33. export_excel($result, $filename, $indexKey, $title);
  34. $to = 'hao.sun@kuxuan-inc.com';
  35. $subject = '猎豆'.$m.'月份预估佣金明细';
  36. $body = '猎豆'.$m.'月份预估佣金明细统计Excel报表,详情请查看附件。';
  37. $attachment = './'.$filename;
  38. $re = sendMail($to, $subject, $body, $attachment);
  39. var_dump($re);
  40. function get_rebate( $day ){
  41. $eday = $day . ' 23:59:59';
  42. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  43. $sql = " select sum(rebate) as rebate from order_rebate where status>0 and order_create_at>='{$day}' and order_create_at<='{$eday}' and maintain_power<3 ";
  44. $stmt = $_PDO->prepare($sql);
  45. $stmt->execute();
  46. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  47. if( empty($res) ){
  48. return false;
  49. }
  50. return $res['rebate'];
  51. }
  52. function get_rebate_profit( $day ){
  53. $eday = $day . ' 23:59:59';
  54. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  55. $sql = " select sum(rebate) as rebate from order_rebate_profit where status>0 and order_create_at>='{$day}' and order_create_at<='{$eday}' and maintain_power<3 ";
  56. $stmt = $_PDO->prepare($sql);
  57. $stmt->execute();
  58. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  59. if( empty($res) ){
  60. return false;
  61. }
  62. return $res['rebate'];
  63. }
  64. /**
  65. * 导出excel
  66. * @param $data
  67. * @param string
  68. */
  69. function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  70. if( !is_array($indexKey)) return false;
  71. $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK');
  72. //初始化PHPExcel()
  73. $objPHPExcel = new PHPExcel();
  74. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  75. //接下来就是写数据到表格里面去
  76. $objActSheet = $objPHPExcel->getActiveSheet();
  77. foreach($title as $k=>$item){
  78. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  79. }
  80. $startRow = 2;
  81. foreach ($data as $row) {
  82. foreach ($indexKey as $key => $value){
  83. //这里是设置单元格的内容
  84. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  85. }
  86. $startRow++;
  87. }
  88. $objWriter->save('./'.$filename);
  89. return;
  90. }
  91. /**
  92. * @method sendMail 发送邮件
  93. * @param $to
  94. * @param $subject
  95. * @param string $body
  96. * @param null $attachment
  97. * @return bool|string
  98. * @throws \phpmailerException
  99. */
  100. function sendMail($to, $subject, $body = '', $attachment = null) { //$to 收件者 $subject主题 $body 内容 $attachment附件
  101. //邮件服务器配置
  102. $detail = array(
  103. "smpt" => "smtp.exmail.qq.com",
  104. "account" => "ldscript@kuxuan-inc.com",
  105. "pwd" => "7W2nJ2n3yDxFtDsM",
  106. );
  107. $title = "";
  108. $mail = new PHPMailer(); //PHPMailer对象
  109. $mail->CharSet = 'utf-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
  110. $mail->Encoding = "base64";
  111. $mail->IsSMTP(); // 设定使用SMTP服务
  112. $mail->SMTPDebug = 0; // 关闭SMTP调试功能
  113. $mail->SMTPAuth = true; // 启用 SMTP 验证功能
  114. $mail->SMTPSecure = 'ssl'; // 使用安全协议
  115. $mail->Host = $detail['smpt']; // SMTP 服务器
  116. $mail->Port = "465"; // SMTP服务器的端口号
  117. $mail->Username = $detail['account']; // SMTP服务器用户名
  118. $mail->Password = $detail['pwd']; // SMTP服务器密码
  119. $mail->Subject = $subject; //邮件标题
  120. $mail->SetFrom($detail['account'], $title);
  121. $mail->MsgHTML($body);
  122. $mail->AddAddress($to);
  123. $mail->AddAddress("xiaohua.hou@kuxuan-inc.com");
  124. $mail->AddAddress("song.shen@kuxuan-inc.com");
  125. // $mail->AddAddress("wanlu.zhang@kuxuan-inc.com");
  126. $mail->AddAddress("zhaozhao@kuxuan-inc.com");
  127. $mail->AddAttachment($attachment);
  128. echo "\n发送...";
  129. $rs = $mail->Send() ? true : $mail->ErrorInfo;
  130. var_dump($rs);
  131. return $rs;
  132. }