291 lines
12 KiB
C#
291 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using Payroll.BO;
|
|
using Payroll.BO;
|
|
using System.Globalization;
|
|
using Ease.CoreV35.Model;
|
|
using Ease.CoreV35.Utility;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Payroll.UI
|
|
{
|
|
public partial class fUploadAttendanceData : Form
|
|
{
|
|
#region Private Declaration and Constructor
|
|
private ConfigurationManager _Logic = null;
|
|
DataTable _dtRawData = null;
|
|
DataTable _dtErrorInfo = null;
|
|
AttnRawData _attnRawData = null;
|
|
string filePath = string.Empty;
|
|
string fileName = string.Empty;
|
|
|
|
public fUploadAttendanceData()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Method(s)
|
|
|
|
private void PrepareData()
|
|
{
|
|
CultureInfo provider = CultureInfo.InvariantCulture;
|
|
_dtRawData = new DataTable("AttenRawData");
|
|
_dtRawData.Columns.Add("Date");
|
|
_dtRawData.Columns.Add("Time");
|
|
_dtRawData.Columns.Add("CardNo");
|
|
|
|
_dtErrorInfo = new DataTable("ErrorInfo");
|
|
_dtErrorInfo.Columns.Add("LineNo");
|
|
_dtErrorInfo.Columns.Add("Data");
|
|
_dtErrorInfo.Columns.Add("ErrorText");
|
|
|
|
StreamReader sr = null;
|
|
try
|
|
{
|
|
string dateFormat = ConfigurationManager.GetStringValue("carddata", "dateformat", EnumConfigurationType.Logic);
|
|
string timeFormat = ConfigurationManager.GetStringValue("carddata", "timeformat", EnumConfigurationType.Logic);
|
|
int dateStart = ConfigurationManager.GetIntValue("carddata", "datestart", EnumConfigurationType.Logic); //Convert.ToInt32(children[0].Value);
|
|
int dateEnd = ConfigurationManager.GetIntValue("carddata", "dateend", EnumConfigurationType.Logic); //Convert.ToInt32(children[0].Value);
|
|
int dateLength = dateEnd - dateStart + 1;
|
|
|
|
int timeStart = ConfigurationManager.GetIntValue("carddata", "timestart", EnumConfigurationType.Logic);
|
|
int timeEnd = ConfigurationManager.GetIntValue("carddata", "timeend", EnumConfigurationType.Logic);
|
|
int timeLength = timeEnd - timeStart + 1;
|
|
|
|
int cardNoStart = ConfigurationManager.GetIntValue("carddata", "cardnostart", EnumConfigurationType.Logic);
|
|
int cardNoEnd = ConfigurationManager.GetIntValue("carddata", "cardnoend", EnumConfigurationType.Logic);
|
|
int cardNoLength = cardNoEnd - cardNoStart + 1;
|
|
|
|
sr = new StreamReader(fileName);
|
|
string lineData = string.Empty;
|
|
int lineNo = 1;
|
|
|
|
while ((lineData = sr.ReadLine()) != null)
|
|
{
|
|
|
|
DateTime date = DateTime.ParseExact(Global.StringFuncions.Mid(lineData, dateStart, dateLength), dateFormat, provider);
|
|
DateTime time = DateTime.ParseExact(Global.StringFuncions.Mid(lineData, timeStart, timeLength), timeFormat, provider);
|
|
string cardNo = Global.StringFuncions.Mid(lineData, cardNoStart, cardNoLength);
|
|
|
|
if (date.Day <= 31 && date.Day > 0 && date.Month < 13 && date.Month > 0 && date.Year > 1970 && date.Year < 2050 && cardNo.Length == cardNoLength)
|
|
{
|
|
DataRow oRow = _dtRawData.NewRow();
|
|
oRow["Date"] = date.ToString("dd MMM yyyy");
|
|
oRow["Time"] = date.ToString("dd MMM yyyy ") + time.ToString("HH:mm:ss");
|
|
oRow["CardNo"] = cardNo;
|
|
_dtRawData.Rows.Add(oRow);
|
|
}
|
|
else
|
|
{
|
|
DataRow oErrRow = _dtErrorInfo.NewRow();
|
|
oErrRow["LineNo"] = lineNo;
|
|
oErrRow["Data"] = lineData;
|
|
oErrRow["ErrorText"] = "Invalid Data";
|
|
_dtErrorInfo.Rows.Add(oErrRow);
|
|
}
|
|
|
|
lineNo++;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Failed to upload data", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
finally
|
|
{
|
|
if (sr != null)
|
|
{
|
|
sr.Close();
|
|
sr.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Event(s)
|
|
|
|
#region OnLoad Event
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
pnlDataPreParation.Visible = true;
|
|
pnlErrList.Visible = false;
|
|
btnSave.Enabled = false;
|
|
|
|
base.OnLoad(e);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Button Event(s)
|
|
|
|
private void btnBrowse_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
openFileDialog.FileName = string.Empty;
|
|
openFileDialog.Filter = "Text files (*.txt)|*.txt";
|
|
openFileDialog.Multiselect = false;
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
fileName = openFileDialog.SafeFileName;
|
|
filePath = openFileDialog.FileName;
|
|
txtFileName.Text = filePath;
|
|
}
|
|
else
|
|
return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.InnerException == null)
|
|
MessageBox.Show(ex.Message, "Failed to browse file", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
else
|
|
MessageBox.Show(ex.InnerException.Message, "Failed to browse file", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void btnUpload_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (fileName != null && fileName != string.Empty)
|
|
{
|
|
PrepareData();
|
|
pnlDataPreParation.Visible = false;
|
|
if (_dtErrorInfo.Rows.Count <= 0)
|
|
{
|
|
lblFirstPart.Text = "File uploaded with no error. You can save data now.";
|
|
lblFirstPart.TextAlign = ContentAlignment.MiddleCenter;
|
|
lblErrorText.Visible = false;
|
|
lblSecondPart.Visible = false;
|
|
btnSave.Enabled = true;
|
|
}
|
|
pnlErrList.Visible = true;
|
|
btnUpload.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Please select a valid text file to upload data.", "Data Upload Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.InnerException == null)
|
|
MessageBox.Show(ex.Message, "Failed to upload", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
else
|
|
MessageBox.Show(ex.InnerException.Message, "Failed to upload", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void lblErrorText_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
|
|
{
|
|
lblErrorText.ForeColor = Color.Red;
|
|
if (_dtErrorInfo != null)
|
|
{
|
|
fErrorList errList = new fErrorList();
|
|
if (errList.ShowDlg(_dtErrorInfo))
|
|
{
|
|
btnSave.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
btnSave.Enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (MessageBox.Show("Do you want to save the information?", "Save Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
ObjectsTemplate<AttnRawData> attnRawDatas = new ObjectsTemplate<AttnRawData>();
|
|
List<string> _errorCardsNo = new List<string>();
|
|
ObjectsTemplate<AccessCard> _errorAccCards = new ObjectsTemplate<AccessCard>();
|
|
_attnRawData = new AttnRawData();
|
|
_attnRawData.Save(_dtRawData, ref _errorCardsNo, ref _errorAccCards);
|
|
|
|
if (_errorAccCards.Count > 0 || _errorCardsNo.Count > 0)
|
|
{
|
|
if (MessageBox.Show("Data Saved with some error.\nWould you like to view those erros?", "Save Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
string errorText = string.Empty;
|
|
string invAccCards = string.Empty;
|
|
string invCardsNo = string.Empty;
|
|
if (_errorAccCards.Count > 0)
|
|
{
|
|
string prefix = "No employee found under this cards: \n";
|
|
foreach (AccessCard ac in _errorAccCards)
|
|
{
|
|
if (invAccCards == string.Empty)
|
|
invAccCards = prefix + ac.CardNumber;
|
|
else
|
|
invAccCards = invAccCards + ", " + ac.CardNumber;
|
|
}
|
|
}
|
|
if (_errorCardsNo.Count > 0)
|
|
{
|
|
string prefix = "No card information found under this cards no: \n";
|
|
foreach (string cn in _errorCardsNo)
|
|
{
|
|
if (invCardsNo == string.Empty)
|
|
invCardsNo = prefix + cn;
|
|
else
|
|
invCardsNo = invCardsNo + ", " + cn;
|
|
}
|
|
}
|
|
if (invAccCards != string.Empty)
|
|
errorText = invAccCards + ".";
|
|
|
|
if (invCardsNo != string.Empty)
|
|
{
|
|
if (errorText != string.Empty)
|
|
errorText = errorText + "\n\n" + invCardsNo + ".";
|
|
else
|
|
errorText = invCardsNo + ".";
|
|
}
|
|
|
|
MessageBox.Show(errorText, "Save Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
else
|
|
MessageBox.Show("Data Saved successfully.", "Save Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
this.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.InnerException == null)
|
|
MessageBox.Show(ex.Message, "Failed to save", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
else
|
|
MessageBox.Show(ex.InnerException.Message, "Failed to save", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
}
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void fUploadAttendanceData_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |