示例#1
0
        /// <summary>
        /// Nhận dữ liệu từ máy quẹt thẻ va hien thi thong tin nhan vien len grid
        /// </summary>
        private void ReceiveData()
        {
            try
            {
                SetCom = new Protocal.Call();
                short  temp_short  = 1;
                string temp_string = "4800";
                //			SetCom.OpenCom(ref temp_short, ref temp_string);
                SetCom.OpenCom(ref temp_short);
                // thông số của máy quẹt thẻ
                string temp_string14 = "00";
                // Độ trễ khi máy quẹt thẻ trừ bỏ đi một bản ghi
                int temp_int2 = 500;

                int    RecordCount = 0;
                string temp        = "";
                //			xptMessage.TableModel.Rows.Clear();
                temp = SetCom.ReceiveData(ref temp_string14, ref temp_int2);
                if (temp.Length < 20)
                {
                    RecordCount = 0;
                }
                else
                {
                    try
                    {
                        RecordCount = int.Parse(temp.Substring(26, 4)) + 1;
                    }
                    catch (Exception)
                    {
                        //ko lam gi  khi du lieu bi sai
                    }
                }
                ///Lấy tung ban ghi theo stack cho đến hết các bản ghi từ máy quẹt thẻ
                while (RecordCount > 0)
                {
                    string   str1       = string.Empty;
                    string   CardID     = string.Empty;
                    string   CheckIn    = string.Empty;
                    DateTime WorkingDay = DateTime.Now;
                    try
                    {
                        str1 = temp.Substring(temp.Length - 39, 39);
                        // mã thẻ
                        CardID = str1.Substring(4, 6);
                        //giờ quet the
                        CheckIn = str1.Substring(20, 4);
                        //ngày làm việc
                        WorkingDay = ConvertString2Day("20" + str1.Substring(12, 6));
                        WriteLog.WriteTimeLog(CardID, WorkingDay.ToString("yyyyMMdd"), CheckIn);
                    }
                    catch
                    {
                    }
                    // Lấy thông tin nhân viên dựa trên mã thẻ vừa quẹt
                    string EmployeeName    = "";
                    string DepartmentName  = "";
                    string PictureFileName = "";

                    string DefaultPicturePath = Application.StartupPath + "/IMAGES/noimage3.jpg";

                    int employeeID = 0;
                    //lay thong tin nhan vien theo ma the nhan duoc tu may doc the
                    DataSet dataSet = pp3750.GetEmployeeInfo(CardID);

                    DataTable dbtable = dataSet.Tables[0];

                    if (dbtable.Rows.Count == 0) //ko co nhan vien nao co ma the nay`
                    {
                        ///MessageBox.Show((" Thẻ "+CardID+ " không hợp lệ !!!"),"Thông báo");
                    }
                    else //co nhan vien trung ma the
                    {
                        foreach (DataRow dataRow in dbtable.Rows)
                        {
                            // Lấy mã thẻ của nhân viên vừa quẹt
                            employeeID = (int)dataRow["EmployeeID"];

                            if (dataRow["Picture"] != DBNull.Value) //trong database co duong dan anh
                            {
                                PictureFileName = Utils.settings.PicturePath + '\\' + dataRow["Picture"].ToString();
                            }
                            else //database chua co duong dan anh nhan vien, lay anh mac dinh
                            {
                                PictureFileName = DefaultPicturePath;
                            }

                            EmployeeName   = dataRow["EmployeeName"].ToString();
                            DepartmentName = dataRow["DepartmentName"].ToString();
                            DateTime timeInOut = ConvertString2Time(CheckIn);

                            try
                            {
                                //Hien thi thong tin nhan vien len Grid
                                PopulateEmployee(count, EmployeeName, DepartmentName, CardID, WorkingDay, timeInOut,
                                                 PictureFileName);
                            }
                            catch
                            {
                                //Hien thi thong tin nhan vien len Grid voi anh mac dinh
                                PopulateEmployee(count, EmployeeName, DepartmentName, CardID, WorkingDay, timeInOut,
                                                 DefaultPicturePath);
                            }
                        }

                        /// Lấy giờ quẹt thẻ
                        String TimeInOut = ConvertString2Time(CheckIn).ToShortTimeString();

                        /// Kiểm tra thời gian quẹt thẻ là giờ vào hay ra
                        string TimeIn = pp3750.GetTimeIn(employeeID, WorkingDay);

                        /// Lưu dữ liệu quẹt thẻ vào cơ sở dữ liệu
                        pp3750.SaveCardData(WorkingDay.ToString(), employeeID, TimeInOut, TimeIn);
                    }
                    count++;
                    RecordCount--;
                    temp = SetCom.ReceiveData(ref temp_string14, ref temp_int2);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }