优惠券订单及其他脚本

estimate_finance_export.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. # 统计月份
  13. $month = date('Y-m', strtotime('-1 month'));
  14. # 收件人
  15. //$email_list = ["song.shen@kuxuan-inc.com"];
  16. $email_list = ['song.shen@kuxuan-inc.com', 'zhaozhao@kuxuan-inc.com'];
  17. $stime = date('Y-m-01', strtotime($month));
  18. $etime = date('Y-m-01', strtotime($month.' +1 month'));
  19. # 佣金
  20. $sql = " select left(`order_balance_at`, 10) as day,sum(rebate) as rebate from `order_rebate` where status=2 and "
  21. ."order_balance_at>='{$stime}' and order_balance_at<'{$etime}' and `maintain_power` <3 group by day order by day;";
  22. $stmt = $_PDO->prepare($sql);
  23. $stmt->execute();
  24. $rebateResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  25. $rebateSum = array_sum(array_column($rebateResult, 'rebate'));
  26. # 津贴
  27. $sql = " select left(`order_balance_at`, 10) as day,sum(rebate) as rebate_profit from `order_rebate_profit` where status=2 and "
  28. ."order_balance_at>='{$stime}' and order_balance_at<'{$etime}' and `maintain_power` <3 group by day order by day;";
  29. $stmt = $_PDO->prepare($sql);
  30. $stmt->execute();
  31. $rebateProfitResult = $stmt->fetchAll(PDO::FETCH_ASSOC);
  32. $rebateProfitResult = array_column($rebateProfitResult, null, 'day');
  33. $rebateProfitSum = array_sum(array_column($rebateProfitResult, 'rebate_profit'));
  34. foreach ($rebateResult as $k => &$v) {
  35. $v['rebate_profit'] = isset($rebateProfitResult[$v['day']]['rebate_profit']) ? $rebateProfitResult[$v['day']]['rebate_profit'] : 0;
  36. $v['remark'] = '';
  37. }
  38. $rebateResult[] = [
  39. 'day' => '',
  40. 'rebate' => $rebateSum,
  41. 'rebate_profit' => $rebateProfitSum,
  42. 'remark' => '单位:分',
  43. ];
  44. $rebateResult[] = [
  45. 'day' => '',
  46. 'rebate' => $rebateSum/100,
  47. 'rebate_profit' => $rebateProfitSum/100,
  48. 'remark' => '单位:元',
  49. ];
  50. $rebateResult[] = [
  51. 'day' => '合计',
  52. 'rebate' => ($rebateSum/100 + $rebateProfitSum/100),
  53. 'rebate_profit' => '',
  54. 'remark' => '',
  55. ];
  56. $m = date('m', strtotime($month));
  57. $indexKey = ['day', 'rebate', 'rebate_profit', 'remark'];
  58. $title = ['日期', '佣金(分)', '津贴(分)', '备注'];
  59. $filename = '猎豆'. $m .'月份预估佣金结算明细_'.date('Y-m-d').'.xlsx';
  60. export_excel($rebateResult, $filename, $indexKey, $title);
  61. $subject = '猎豆'.$m.'月份预估佣金结算明细';
  62. $body = '猎豆'.$m.'月份预估佣金结算明细统计Excel报表,详情请查看附件。';
  63. $attachment = './'.$filename;
  64. $re = sendMail($email_list, $subject, $body, $attachment);
  65. var_dump($re);
  66. /**
  67. * 导出excel
  68. * @param $data
  69. * @param string
  70. */
  71. function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  72. if( !is_array($indexKey)) return false;
  73. $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');
  74. //初始化PHPExcel()
  75. $objPHPExcel = new PHPExcel();
  76. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  77. //接下来就是写数据到表格里面去
  78. $objActSheet = $objPHPExcel->getActiveSheet();
  79. foreach($title as $k=>$item){
  80. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  81. }
  82. $startRow = 2;
  83. foreach ($data as $row) {
  84. foreach ($indexKey as $key => $value){
  85. //这里是设置单元格的内容
  86. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  87. }
  88. $startRow++;
  89. }
  90. $objWriter->save('./'.$filename);
  91. return;
  92. }
  93. /**
  94. * @method sendMail 发送邮件
  95. * @param $to
  96. * @param $subject
  97. * @param string $body
  98. * @param null $attachment
  99. * @return bool|string
  100. * @throws \phpmailerException
  101. */
  102. function sendMail($to, $subject, $body = '', $attachment = null) { //$to 收件者 $subject主题 $body 内容 $attachment附件
  103. //邮件服务器配置
  104. $detail = array(
  105. "smpt" => "smtp.exmail.qq.com",
  106. "account" => "ldscript@kuxuan-inc.com",
  107. "pwd" => "7W2nJ2n3yDxFtDsM",
  108. );
  109. $title = "";
  110. $mail = new PHPMailer(); //PHPMailer对象
  111. $mail->CharSet = 'utf-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
  112. $mail->Encoding = "base64";
  113. $mail->IsSMTP(); // 设定使用SMTP服务
  114. $mail->SMTPDebug = 0; // 关闭SMTP调试功能
  115. $mail->SMTPAuth = true; // 启用 SMTP 验证功能
  116. $mail->SMTPSecure = 'ssl'; // 使用安全协议
  117. $mail->Host = $detail['smpt']; // SMTP 服务器
  118. $mail->Port = "465"; // SMTP服务器的端口号
  119. $mail->Username = $detail['account']; // SMTP服务器用户名
  120. $mail->Password = $detail['pwd']; // SMTP服务器密码
  121. $mail->Subject = $subject; //邮件标题
  122. $mail->SetFrom($detail['account'], $title);
  123. $mail->MsgHTML($body);
  124. foreach ($to as $emailAddress) {
  125. $mail->AddAddress($emailAddress);
  126. }
  127. $mail->AddAttachment($attachment);
  128. echo "\n发送...";
  129. $rs = $mail->Send() ? true : $mail->ErrorInfo;
  130. var_dump($rs);
  131. return $rs;
  132. }