<?php
include("CheckSession.php");
?>
<?php

require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/FileConfig.php");
require_once("class/FileOperation.php");

$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);

$querySql = $_POST['querySql'];

$querySql1 = "select * from Customer,AdminUser ".$querySql;
$db->query($querySql1);



//產製檔案，組成電話字串
$TelStr = "";
$TelTempStr = "";
$TelCount = 0;
while($myrow = $db->fetch_array())
{  
  if((str_indexof($myrow['CMemo'],"21") >= 0)
     || (str_indexof($myrow['CMemo'],"43") >= 0)
     || (str_indexof($myrow['CMemo'],"44") >= 0)
     || (str_indexof($myrow['CMemo'],"46") >= 0)
     || (str_indexof($myrow['CMemo'],"47") >= 0))
  {
  	continue;
  }   
  
  if($myrow['CGreenTel'] != null && $myrow['CGreenTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CGreenTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CGreenTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }  
  if($myrow['CBlackTel'] != null && $myrow['CBlackTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CBlackTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CBlackTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }
  if($myrow['CRedTel'] != null && $myrow['CRedTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CRedTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CRedTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }
}
if($TelCount % 20 != 0)
	$TelStr = $TelStr.$TelTempStr;

date_default_timezone_set('Asia/Taipei');
$t=time();
$NowDate = date("YmdHis");
$fp = fopen($_FILECONFIG['UsedDir'].$_SESSION['username']."_BuyTimes_".$NowDate.".txt", 'w');
fwrite($fp, $TelStr);
fclose($fp);

$fp = fopen($_FILECONFIG['BackUpDir'].$_SESSION['username']."_BuyTimes_".$NowDate.".txt", 'w');
fwrite($fp, $TelStr);
fclose($fp);

$db->close_db();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
//寫入資料庫
$insertSql = "INSERT INTO FileData (CMemo, CNewFileName,COrgFileName,CCreateDateTime,CUpdateDateTime,CCreateAcc,CRealCreator)"
	              ."VALUES ('".$db->mysql_real_escape_string($_SESSION['username']."_BuyTimes_".$NowDate)."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['username']."_BuyTimes_".$NowDate.".txt")."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['username']."_BuyTimes_".$NowDate.".txt")."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['userId'])."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['userId'])."')";
	              
$db->query($insertSql);
$db->close_db();

echo '<script type="text/javascript">';
echo 'alert("製作檔案成功!!!");';
echo 'window.close();';
echo '</script>';
exit();
?>