public partial class HCCPReader : ServiceBase
{
DateTime _lastFileTime = DateTime.MinValue;
protected DateTime _lastFTPTime = DateTime.MinValue;
protected DateTime _lastUploadTime = DateTime.MinValue;
protected DateTime _lastBankUploadTime = DateTime.MinValue;
System.Timers.Timer _newTimer = new System.Timers.Timer();
public HCCPReader()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
_lastFileTime = DateTime.MinValue;
fswUpload.Path = Common.GetUploadSettlementPath();
fswUpload.EnableRaisingEvents = true;
fswFtp.Path = Common.GetFTPPath();
fswFtp.EnableRaisingEvents = true;
fswBankNotify.Path = Common.GetUploadBankPath();
fswBankNotify.EnableRaisingEvents=true;
_newTimer.Interval =3600000;
_newTimer.Elapsed += new
System.Timers.ElapsedEventHandler(ServiceTimer_Tick);
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop
your service.
fswUpload.EnableRaisingEvents = false;
fswFtp.EnableRaisingEvents = false;
fswBankNotify.EnableRaisingEvents = false;
}
private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
SunFile.Clear();
Clear.ClearDirectory();
}
private void fswFtp_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastFTPTime)
{
_lastFTPTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
//column 10
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z" ||
_record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName (" +
_fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
else
{
_lastFTPTime = DateTime.MinValue;
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswFtp_Changed", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
private void fswUpload_Changed(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastFTPTime)
{
_lastFTPTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
//column 10
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z" ||
_record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName (" +
_fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
else
{
_lastFTPTime = DateTime.MinValue;
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswUpload_Changed", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
private void fswFtp_Created(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z"
|| _record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName
(" + _fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if (Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage = Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage = Settlement.Inserts(_dataExcel, true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswFtp_Created", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
private void fswUpload_Created(object sender, FileSystemEventArgs e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
string[] _arrayFileName = e.Name.Split('_');
Decimal _fileTotal = 0;
Decimal _totalAmount = 0;
try
{
_fileTotal = Convert.ToDecimal(_arrayFileName[3]);
}
catch
{
_fileTotal = -1;
TReadingLog.Add(e.Name, "Wrong Template", "File name is not same
with file name template.");
}
if (_fileTotal != -1)
{
foreach (DataRow _record in _dataExcel.Rows)
{
if (_record[0].ToString() == "S" || _record[0].ToString() == "Z"
|| _record[0].ToString() == "T" || _record[0].ToString() == "P")
{
_totalAmount += Convert.ToDecimal(_record[11]);
}
}
if (_fileTotal != _totalAmount)
{
TReadingLog.Add(e.Name, "Diference Amount", "Total on FileName
(" + _fileTotal.ToString() +
") is diffrent with file content (" + _totalAmount.ToString() +
".");
}
else
{
Status _insertMessage = new Status();
if(Common.IsInActiveDate(_arrayFileName[2]))
{
_insertMessage=Settlement.Inserts(_dataExcel);
}
else
{
_insertMessage=Settlement.Inserts(_dataExcel,true);
}
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name,"Success", "Data settlement has been
inserted. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name,"Failed", "Data settlement failed to be
inserted. " + _insertMessage.Remark);
}
}
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswUpload_Created", _error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
private void fswBankNotify_Changed(object sender, FileSystemEventArgs
e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
if (_fswFileTime > _lastBankUploadTime)
{
_lastBankUploadTime = _fswFileTime;
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
int _tries = 0;
while (!_isFileComplete && _tries < 15)
{
System.Threading.Thread.Sleep(1000);
_tries++;
_isFileComplete = Common.IsFileCompete(e.FullPath);
}
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
Status _insertMessage = new Status();
_insertMessage = BankNotification.Process(_dataExcel);
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data bank notification
berhasil diinput. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data bank notification gagal
diinput. " + _insertMessage.Remark);
}
FileInfo _fileInfo = new FileInfo(e.FullPath);
TTempBankNotification.UpdateStatus(_fileInfo.DirectoryName,
_insertMessage);
File.Delete(e.FullPath);
}
else
{
_lastFTPTime = DateTime.MinValue;
}
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswBankNotify_Changed",
_error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
private void fswBankNotify_Created(object sender, FileSystemEventArgs
e)
{
DateTime _fswFileTime = File.GetLastAccessTime(e.FullPath);
DataTable _dataExcel = new DataTable();
try
{
bool _isFileComplete = Common.IsFileCompete(e.FullPath);
if (_isFileComplete)
{
//here sistem to read excel file
_dataExcel = Reading.GetDataExcel(e.FullPath);
Status _insertMessage = new Status();
_insertMessage = BankNotification.Process(_dataExcel);
if (_insertMessage.StatusValue)
{
TReadingLog.Add(e.Name, "Success", "Data bank notification berhasil
diinput. " + _insertMessage.Remark);
}
else
{
TReadingLog.Add(e.Name, "Failed", "Data bank notification gagal
diinput. " + _insertMessage.Remark);
}
FileInfo _fileInfo= new FileInfo(e.FullPath);
TTempBankNotification.UpdateStatus(_fileInfo.DirectoryName,
_insertMessage);
File.Delete(e.FullPath);
}
}
catch (Exception _error)
{
TLog.Add("HCCPReader.HCCPReader.fswBankNotify_Created",
_error.Message);
}
finally
{
_dataExcel.Dispose();
}
}
}