Exemple #1
0
        /// <summary>
        /// luu chi tiet don hang
        /// </summary>
        private void LuuChiTietKiemKeKho()
        {
            cl          = new Server_Client.Client();
            this.client = cl.Connect(Luu.IP, Luu.Ports);
            Entities.ChiTietKiemKeKho[] luu = LuuThongTinChiTietKiemKeKho(dgvInsertOrder, "Insert", txtSochungtu.Text, txtDiengiai.Text);
            clientstrem = cl.SerializeHepper(this.client, "ThemchiTietKiemKeKho", luu);
            int bao = 0;

            bao = (int)cl.DeserializeHepper(clientstrem, bao);
        }
Exemple #2
0
        /// <summary>
        /// luu chi tiet don hang
        /// </summary>
        private void LuuChiTietDonHang()
        {
            cl          = new Server_Client.Client();
            this.client = cl.Connect(Luu.IP, Luu.Ports);
            Entities.ChiTietDonDatHang[] luu = LuuChiTietKhoHang(dgvInsertOrder, "Insert", txtSodonhang.Text.ToUpper());
            clientstrem = cl.SerializeHepper(this.client, "ChiTietDonDatHang", luu);
            int bao = 0;

            bao = (int)cl.DeserializeHepper(clientstrem, bao);
        }
        /// <summary>
        /// luu chi tiet don hang
        /// </summary>
        private void LuuChiTietDonHang()
        {
            cl          = new Server_Client.Client();
            this.client = cl.Connect(Luu.IP, Luu.Ports);
            Entities.ChiTietHoaDonNhap[] luu = LuuChiTietHoaDonNhap(dgvInsertOrder, "Insert", txtSodonhang.Text.ToUpper());
            clientstrem = cl.SerializeHepper(this.client, "ChiTietHoaDonNhap", luu);
            int bao = 0;

            bao = (int)cl.DeserializeHepper(clientstrem, bao);
            if (bao == 1)
            {
                int k = dgvInsertOrder.RowCount;
                Entities.ChiTietKhoHangTheoHoaHonNhap[] khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[k];
                ArrayList arr = new ArrayList();
                if (k > 0)
                {
                    for (int x = 0; x < k; x++)
                    {
                        Entities.ChiTietKhoHangTheoHoaHonNhap kho = new Entities.ChiTietKhoHangTheoHoaHonNhap();
                        kho.Hanhdong   = "Insert";
                        kho.Makho      = cbxKhoHang.SelectedValue.ToString();
                        kho.Mahanghoa  = dgvInsertOrder.Rows[x].Cells[1].Value.ToString();
                        kho.Soluong    = System.Convert.ToInt32(dgvInsertOrder.Rows[x].Cells[3].Value.ToString());
                        kho.Ngaynhap   = DateTime.Now;
                        kho.Ngayhethan = 0;
                        kho.Ghichu     = txtDiengiai.Text;
                        kho.Deleted    = false;
                        arr.Add(kho);
                    }
                    int n = arr.Count;
                    if (n == 0)
                    {
                        khohang = null;
                    }
                    khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[n];
                    for (int j = 0; j < n; j++)
                    {
                        khohang[j] = (Entities.ChiTietKhoHangTheoHoaHonNhap)arr[j];
                    }
                }
                else
                {
                    khohang = null;
                }
                LuuChiTietVaoKhoHang(khohang);
            }
            else
            {
                MessageBox.Show("Chưa thêm vào kho");
            }
        }
        /// <summary>
        /// luu chi tiet hoa don nhap vao kho hang
        /// </summary>
        private void LuuChiTietVaoKhoHang(Entities.ChiTietKhoHangTheoHoaHonNhap[] kho)
        {
            cl          = new Server_Client.Client();
            this.client = cl.Connect(Luu.IP, Luu.Ports);
            clientstrem = cl.SerializeHepper(this.client, "ThemChiTietKhoHang", kho);
            int bao = 0;

            bao = (int)cl.DeserializeHepper(clientstrem, bao);
            if (bao == 1)
            {
                MessageBox.Show("Đã thêm vào kho");
            }
            else
            {
                MessageBox.Show("Chưa thêm vào kho");
            }
        }
 public bool DeleteCongNo()
 {
     try
     {
         string maXoa = "";
         cl = new Server_Client.Client();
         // gán TCPclient
         this.client1 = cl.Connect(Luu.IP, Luu.Ports);
         // khởi tạo biến truyền vào với hàm khởi tạo
         Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
         foreach (Entities.SoDuCongNo item in this.hienthi)
         {
             if (item.MaDoiTuong.Equals(maNCCDELETE0))
             {
                 maXoa = item.MaSoDuCongNo;
             }
         }
         if (maXoa.Equals(""))
         {
             pt[0] = new Entities.SoDuCongNo("Delete", ma);
         }
         else
         {
             pt[0] = new Entities.SoDuCongNo("Delete", maXoa);
         }
         // khởi tạo mảng đối tượng để hứng giá trị
         clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
         bool kt = false;
         kt = (bool)cl.DeserializeHepper(clientstrem, kt);
         if (kt == true)
         {
             SelectData();
         }
         return(kt);
     }
     catch
     {
         return(false);
     }
 }
        public void SelectData()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbdoituong.SelectedIndex == 0)
                    loaiDoiTuong = false;
                else
                    loaiDoiTuong = true;
                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                // đổ mảng đối tượng vào datagripview
                hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                // nếu null
                if (hienthi == null)
                {
                    hienthi = new Entities.SoDuCongNo[0];
                    lbtrangthai.Text = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    toolStrip1.Enabled = dtgvcongno.Enabled = true;
                    return;
                }
                year = cbbnam.Text;
                month = cbbthang.Text;
                int sotang = 0;
                bool ktsd = false;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = hienthi[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi[i].NgayKetChuyen.Month.ToString();
                    // nếu năm tháng trong bản ghi - năm tháng hiện tại và loại đối tượng
                    if (namkt == year && thangkt == month && hienthi[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        // nếu trạng thái = false
                        if (hienthi[i].TrangThai == false)
                        {
                            lbtrangthai.Text = "Kỳ này chưa đóng";
                            lbtrangthai.ForeColor = Color.Black;
                            toolStrip1.Enabled = dtgvcongno.Enabled = true;
                        }
                        else
                        {
                            lbtrangthai.Text = "Kỳ này đã khóa";
                            lbtrangthai.ForeColor = Color.Red;
                            toolStrip1.Enabled = dtgvcongno.Enabled = false;
                        }
                        ma = hienthi[i].MaSoDuCongNo;
                        sq[sotang] = hienthi[i];
                        sotang++;
                        ktsd = true;
                    }
                }
                // nếu không có đối tượng nào đúng
                if (ktsd == false)
                {
                    lbtrangthai.Text = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    toolStrip1.Enabled = dtgvcongno.Enabled = true;
                }
                // nếu không có bản ghi nào tồn tại
                if (sotang != 0)
                {
                    hienthi = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi[i] = new Entities.SoDuCongNo("", sq[i].MaDoiTuong, sq[i].TenDoiTuong, sq[i].DiaChi, sq[i].SoDuDauKy);
                    }

                }
                else
                {
                    hienthi = new Entities.SoDuCongNo[0];
                    dtgvcongno.DataSource = hienthi;
                    return;
                }
                dtgvcongno.DataSource = hienthi;
                new Common.Utilities().CountDatagridview(dtgvcongno);
            }
            catch
            {
            }
            finally
            {
                fix();
            }
        }
 private void LuuChiTietTraLaiNhaCungCap()
 {
     try
     {
         cl = new Server_Client.Client();
         this.client = cl.Connect(Luu.IP, Luu.Ports);
         Entities.ChiTietTraLaiNhaCungCap[] luu = ChiTietTraLaiNhaCungCap(dgvXemthongtin, "Insert", txtSodonhang.Text, cbxKhoHang.SelectedValue.ToString());
         clientstrem = cl.SerializeHepper(this.client, "ChiTietTraLaiNhaCungCap", luu);
         int bao = 0;
         bao = (int)cl.DeserializeHepper(clientstrem, bao);
         if (bao >= 1)
         { this.Close(); }
         else
         { DeleteData2(txtSodonhang.Text); MessageBox.Show("Thất bại"); }
     }
     catch (Exception ex)
     { string s = ex.Message; }
 }
 /// <summary>
 /// save
 /// </summary>
 private void LuuChiTietDonHang()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     if (XoaTheoHoaDon("DonDatHang", txtSodonhang.Text.ToUpper()) != 0)
     {
         Entities.ChiTietDonDatHang[] luu = LuuChiTietHangHoaTheoDonDatHang(dgvInsertOrder, "Insert", txtSodonhang.Text.ToUpper());
         clientstrem = cl.SerializeHepper(this.client, "ChiTietDonDatHang", luu);
         int bao = 0;
         bao = (int)cl.DeserializeHepper(clientstrem, bao);
         if (bao == 0)
         { DeleteData(txtSodonhang.Text); MessageBox.Show("Thất bại"); }
         else
         {
             this.Close();
         }
     }
     else
     { MessageBox.Show("Chưa thêm hàng hóa vào đơn hàng này"); }
 }
 /// <summary>
 /// luu chi tiet hoa don nhap vao kho hang
 /// </summary>
 private void LuuChiTietVaoKhoHang(Entities.ChiTietKhoHangTheoHoaHonNhap[] kho)
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     clientstrem = cl.SerializeHepper(this.client, "ThemChiTietKhoHang", kho);
     int bao = 0;
     bao = (int)cl.DeserializeHepper(clientstrem, bao);
     if (bao == 1)
     { MessageBox.Show("Đã thêm vào kho"); }
     else
     { MessageBox.Show("Chưa thêm vào kho"); }
 }
        private void dtgvcongno_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            this.maNCCDELETE0 = dtgvcongno[3, e.RowIndex].Value.ToString();
            //string maNCCDELETE2 = dtgvcongno[2, e.RowIndex].Value.ToString();
            if (i < 0)
            {
                return;
            }
            try
            {
                string month1 = datesv.Month.ToString();
                // kiểm tra tháng hiên tại và tháng được chọn
                if (month1 != month)
                {
                    MessageBox.Show("Không phải kỳ hiện tại", "Hệ thống cảnh báo");
                    return;
                }

                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                // đổ mảng đối tượng vào datagripview
                hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                // nếu obj khác null
                if (hienthi != null)
                {
                    for (int j = 0; j < hienthi.Length; j++)
                    {
                        string monthkytruoc = (Convert.ToInt32(month) - 1).ToString();
                        string mm           = hienthi[j].NgayKetChuyen.Month.ToString();
                        // kiểm tra kỳ trước của tháng đang chọn
                        if (mm == monthkytruoc)
                        {
                            MessageBox.Show("Số dư đầu kỳ là kết chuyển từ kỳ trước - không thể thay đổi", "Hệ thống cảnh báo");
                            SelectData();
                            return;
                        }
                    }
                }
                if (MessageBox.Show("Bạn chắc chắn muốn xóa không?", "Hệ thống cảnh báo", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    // nếu kỳ trc không tồn tại
                    tsslma.Text        = dtgvcongno["MaDoiTuong", i].Value.ToString();
                    tsslten.Text       = dtgvcongno["TenDoiTuong", i].Value.ToString();
                    tssldiachi.Text    = dtgvcongno["DiaChi", i].Value.ToString();
                    tsslsodudauky.Text = dtgvcongno["SoDuDauKy", i].Value.ToString();
                    sono = Convert.ToDouble(dtgvcongno["SoDuDauKy", i].Value.ToString());
                    if (dtgvcongno.RowCount >= 1)
                    {
                        if (DeleteCongNo())
                        {
                            if (cbbdoituong.SelectedIndex == 0)
                            {
                                Entities.KhachHang kh = new Entities.KhachHang("UpdateDuNoKH", tsslma.Text, sono.ToString());
                                UpdateDuNo(kh);
                            }
                            else
                            {
                                Entities.NhaCungCap kh = new Entities.NhaCungCap("UpdateDuNo", tsslma.Text, sono.ToString());
                                UpdateDuNoNCC(kh);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Xóa thất bại - xin thử lại sau", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                fix();
            }
        }
        public void SelectData()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbdoituong.SelectedIndex == 0)
                    loaiDoiTuong = false;
                else
                    loaiDoiTuong = true;
                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                // đổ mảng đối tượng vào datagripview
                hienthi1 = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi1);
                if (hienthi1 == null)
                {
                    lbtrangthai.Text = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    tsslthuchien.Enabled = true;
                    return;
                }
                year = cbbnam.Text;
                month = cbbthang.Text;
                int sotang = 0;
                bool ktsd = false;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi1.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = hienthi1[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi1[i].NgayKetChuyen.Month.ToString();

                    if (namkt == year && thangkt == month && hienthi1[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        if (hienthi1[i].TrangThai == false)
                        {
                            lbtrangthai.Text = "Kỳ này chưa đóng";
                            lbtrangthai.ForeColor = Color.Black;
                            tsslthuchien.Enabled = true;
                        }
                        else
                        {
                            lbtrangthai.Text = "Kỳ này đã khóa";
                            lbtrangthai.ForeColor = Color.Red;
                            tsslthuchien.Enabled = false;
                        }
                        ma = hienthi1[i].MaSoDuCongNo;
                        sq[sotang] = hienthi1[i];
                        sotang++;
                        ktsd = true;
                    }

                }
                if (ktsd == false)
                {
                    lbtrangthai.Text = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    tsslthuchien.Enabled = true;
                }
                if (sotang != 0)
                {
                    hienthi = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi[i] = sq[i];
                    }
                }
                else
                    hienthi = null;
            }
            catch { }
            finally { }
        }
 public bool DeleteCongNo()
 {
     try
     {
         string maXoa = "";
         cl = new Server_Client.Client();
         // gán TCPclient
         this.client1 = cl.Connect(Luu.IP, Luu.Ports);
         // khởi tạo biến truyền vào với hàm khởi tạo
         Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
         foreach (Entities.SoDuCongNo item in this.hienthi)
         {
             if (item.MaDoiTuong.Equals(maNCCDELETE0))
             {
                 maXoa = item.MaSoDuCongNo;
             }
         }
         if (maXoa.Equals(""))
         {
             pt[0] = new Entities.SoDuCongNo("Delete", ma);
         }
         else
         {
             pt[0] = new Entities.SoDuCongNo("Delete", maXoa);
         }
         // khởi tạo mảng đối tượng để hứng giá trị
         clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
         bool kt = false;
         kt = (bool)cl.DeserializeHepper(clientstrem, kt);
         if (kt == true)
         {
             SelectData();
         }
         return kt;
     }
     catch
     {
         return false;
     }
 }
 private void LuuChiTietDonHang()
 {
     try
     {
         if (XoaTheoHoaDon("HoaDonNhap", txtSodonhang.Text.ToUpper()) != 0)
         {
             cl = new Server_Client.Client();
             this.client = cl.Connect(Luu.IP, Luu.Ports);
             Entities.ChiTietHoaDonNhap[] luu = LuuChiTietHoaDonNhap(dgvInsertOrder, "Insert", txtSodonhang.Text);
             clientstrem = cl.SerializeHepper(this.client, "ChiTietHoaDonNhap", luu);
             int bao = 0;
             bao = (int)cl.DeserializeHepper(clientstrem, bao);
             if (bao != 0)
             {
                 int k = dgvInsertOrder.RowCount;
                 Entities.ChiTietKhoHangTheoHoaHonNhap[] khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[k];
                 ArrayList arr = new ArrayList();
                 if (k > 0)
                 {
                     for (int x = 0; x < k; x++)
                     {
                         Entities.ChiTietKhoHangTheoHoaHonNhap kho = new Entities.ChiTietKhoHangTheoHoaHonNhap();
                         kho.Hanhdong = "Insert";
                         kho.Makho = cbxKhoHang.SelectedValue.ToString();
                         kho.Mahanghoa = dgvInsertOrder.Rows[x].Cells[1].Value.ToString();
                         kho.Soluong = int.Parse(dgvInsertOrder.Rows[x].Cells[3].Value.ToString());
                         kho.Ngaynhap = Date.Date;
                         //string ngay = new Common.Utilities().KiemTraDinhDangNgayThangNam("ThangNgayNam", dgvInsertOrder.Rows[x].Cells[12].Value.ToString(), '/');
                         //kho.Ngayhethan = DateTime.Parse(ngay);
                         kho.Ngayhethan = Utils.StringToDateTime(dgvInsertOrder.Rows[x].Cells[12].Value.ToString());
                         kho.Ghichu = txtDiengiai.Text;
                         kho.Deleted = false;
                         kho.Gia = float.Parse(dgvInsertOrder.Rows[x].Cells[4].Value.ToString());
                         arr.Add(kho);
                     }
                     int n = arr.Count;
                     if (n == 0)
                     { khohang = null; }
                     khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[n];
                     for (int j = 0; j < n; j++)
                     { khohang[j] = (Entities.ChiTietKhoHangTheoHoaHonNhap)arr[j]; }
                 }
                 else
                 { khohang = null; }
                 LuuChiTietVaoKhoHang(khohang);
             }
             else
             { DeleteData(txtSodonhang.Text); MessageBox.Show("Thất bại"); }
         }
         else
         { MessageBox.Show("Chưa thêm hàng hóa vào đơn nhập hàng này"); }
     }
     catch (Exception ex)
     { string s = ex.Message; }
 }
 private void LuuChiTietVaoKhoHang(Entities.ChiTietKhoHangTheoHoaHonNhap[] kho)
 {
     try
     {
         cl = new Server_Client.Client();
         this.client = cl.Connect(Luu.IP, Luu.Ports);
         clientstrem = cl.SerializeHepper(this.client, "ThemChiTietKhoHang", kho);
         int bao = 0;
         bao = (int)cl.DeserializeHepper(clientstrem, bao);
         if (bao == 1 || bao == -1)
         {
             if (bao == -1)
             {
                 lblSodonhang.Text = "Mã Đơn Nhập:";
             }
             if (Double.Parse(txtTongThanhToan.Text.Replace(",", "")) > Double.Parse(txtThanhtoanngay.Text.Replace(",", "")))
             {
                 update(txtManhacungcap.Text, capnhat());
             }
             else if (Double.Parse(txtThanhtoanngay.Text.Replace(",", "")) == Double.Parse(txtTongThanhToan.Text))
             {
                 lbtinhtrang.Visible = true;
                 lbtinhtrang.Text = "Đã thanh toán";
                 toolStripStatus_Thanhtoan.Enabled = false;
             }
             else
             {
                 lbtinhtrang.Visible = true;
                 lbtinhtrang.Text = "Chưa thanh toán hết";
                 toolStripStatus_Thanhtoan.Enabled = true;
             }
         }
         else
         {
             lblSodonhang.Text = "Mã đơn nhập::";
         }
     }
     catch { }
 }
Exemple #15
0
        public void SelectSoDuCongNo()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbdoituong.SelectedIndex == 0)
                    loaiDoiTuong = false;
                else
                    loaiDoiTuong = true;
                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                Entities.CongNo[] hienthi = new Entities.CongNo[1];
                // đổ mảng đối tượng vào datagripview
                hienthi1 = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                if (hienthi1 == null)
                {
                    hienthi1 = new Entities.SoDuCongNo[0];
                    return;
                }
                year = cbbnam.Text;
                month = cbbthang.Text;
                int sotang = 0;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi1.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = hienthi1[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi1[i].NgayKetChuyen.Month.ToString();

                    if (namkt == year && thangkt == month && hienthi1[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        sq[sotang] = hienthi1[i];
                        sotang++;
                    }

                }
                if (sotang != 0)
                {
                    hienthi1 = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi1[i] = new Entities.SoDuCongNo("", sq[i].MaSoDuCongNo, sq[i].MaDoiTuong, sq[i].TenDoiTuong, sq[i].DiaChi, sq[i].SoDuDauKy);
                    }
                }
                else
                    hienthi1 = new Entities.SoDuCongNo[0];
            }
            catch
            {
            }
            finally
            {

            }
        }
        public void SelectData()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbdoituong.SelectedIndex == 0)
                {
                    loaiDoiTuong = false;
                }
                else
                {
                    loaiDoiTuong = true;
                }
                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                // đổ mảng đối tượng vào datagripview
                hienthi1 = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi1);
                if (hienthi1 == null)
                {
                    lbtrangthai.Text      = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    tsslthuchien.Enabled  = true;
                    return;
                }
                year  = cbbnam.Text;
                month = cbbthang.Text;
                int  sotang = 0;
                bool ktsd   = false;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi1.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt   = hienthi1[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi1[i].NgayKetChuyen.Month.ToString();

                    if (namkt == year && thangkt == month && hienthi1[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        if (hienthi1[i].TrangThai == false)
                        {
                            lbtrangthai.Text      = "Kỳ này chưa đóng";
                            lbtrangthai.ForeColor = Color.Black;
                            tsslthuchien.Enabled  = true;
                        }
                        else
                        {
                            lbtrangthai.Text      = "Kỳ này đã khóa";
                            lbtrangthai.ForeColor = Color.Red;
                            tsslthuchien.Enabled  = false;
                        }
                        ma         = hienthi1[i].MaSoDuCongNo;
                        sq[sotang] = hienthi1[i];
                        sotang++;
                        ktsd = true;
                    }
                }
                if (ktsd == false)
                {
                    lbtrangthai.Text      = "Kỳ này chưa đóng";
                    lbtrangthai.ForeColor = Color.Black;
                    tsslthuchien.Enabled  = true;
                }
                if (sotang != 0)
                {
                    hienthi = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi[i] = sq[i];
                    }
                }
                else
                {
                    hienthi = null;
                }
            }
            catch { }
            finally { }
        }
        private void tsslthuchien_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {
                bool   loaidoituong = false;
                string monthhientai = datesv.Month.ToString();
                string namhientai   = datesv.Year.ToString();
                if (cbbdoituong.SelectedIndex == 0)
                {
                    loaidoituong = false;
                }
                else
                {
                    loaidoituong = true;
                }

                if (hienthi != null)
                {
                    string month = hienthi[0].NgayKetChuyen.Month.ToString();
                    string year  = hienthi[0].NgayKetChuyen.Year.ToString();
                    if (month == monthhientai && year == namhientai)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            if (month == hienthi[i].NgayKetChuyen.Month.ToString() && year == hienthi[i].NgayKetChuyen.Year.ToString() && hienthi[i].LoaiDoiTuong == loaidoituong)
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            MessageBox.Show("Bạn phải kết chuyển kỳ trước - Kiểm tra lại", "Hệ thống cảnh báo");
                            return;
                        }
                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbdoituong.SelectedIndex);
                        if (frmCongNo.mangchitiet.ToArray().Length == 0)
                        {
                            frmCongNo.mangchitiet    = new List <Entities.SoDuCongNo>();
                            frmCongNo.mangchitiet[0] = new Entities.SoDuCongNo("UpdateTrangThai", "");
                            System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                            {
                                if (giatri == System.Windows.Forms.DialogResult.No)
                                {
                                    return;
                                }
                            }
                        }
                        cl           = new Server_Client.Client();
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {
                            SelectData();
                        }
                        else
                        {
                            MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc  = "";
                        if ((datesv.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc  = (datesv.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (datesv.Month - 1).ToString();
                            yearkytruoc  = datesv.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {
                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbdoituong.SelectedIndex);
                            if (frmCongNo.mangchitiet.ToArray().Length == 0)
                            {
                                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                                {
                                    if (giatri == System.Windows.Forms.DialogResult.No)
                                    {
                                        return;
                                    }
                                }
                            }
                            cl           = new Server_Client.Client();
                            this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                            clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                            bool kt = false;
                            kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                            if (kt == true)
                            {
                                SelectData();
                            }
                            else
                            {
                                MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Không phải kỳ hiện tại - không thể kết chuyển", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
                else
                {
                    string month = cbbthang.Text;
                    string year  = cbbnam.Text;

                    if (hienthi1 != null)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi1.Length; i++)
                        {
                            if (month == hienthi1[i].NgayKetChuyen.Month.ToString() && year == hienthi1[i].NgayKetChuyen.Year.ToString())
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            MessageBox.Show("Bạn phải kết chuyển kỳ trước - Kiểm tra lại", "Hệ thống cảnh báo");
                            return;
                        }
                    }

                    if (month == monthhientai && year == namhientai)
                    {
                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbdoituong.SelectedIndex);
                        if (frmCongNo.mangchitiet.ToArray().Length == 0)
                        {
                            System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                            {
                                if (giatri == System.Windows.Forms.DialogResult.No)
                                {
                                    MessageBox.Show("Không phát sinh Công nợ nào", "Hệ thống cảnh báo");
                                    return;
                                }
                            }
                        }
                        cl           = new Server_Client.Client();
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {
                            SelectData();
                        }
                        else
                        {
                            MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc  = "";
                        if ((datesv.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc  = (datesv.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (datesv.Month - 1).ToString();
                            yearkytruoc  = datesv.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {
                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbdoituong.SelectedIndex);
                            if (frmCongNo.mangchitiet.ToArray().Length == 0)
                            {
                                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                                {
                                    if (giatri == System.Windows.Forms.DialogResult.No)
                                    {
                                        return;
                                    }
                                }
                            }
                            cl           = new Server_Client.Client();
                            this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                            clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                            bool kt = false;
                            kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                            if (kt == true)
                            {
                                SelectData();
                            }
                            else
                            {
                                MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Không phải kỳ hiện tại - không thể kết chuyển", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                this.Enabled = true;
            }
        }
        private void dtgvcongno_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
            this.maNCCDELETE0 = dtgvcongno[3, e.RowIndex].Value.ToString();
            //string maNCCDELETE2 = dtgvcongno[2, e.RowIndex].Value.ToString();
            if (i < 0)
                return;
            try
            {
                string month1 = datesv.Month.ToString();
                // kiểm tra tháng hiên tại và tháng được chọn
                if (month1 != month)
                {
                    MessageBox.Show("Không phải kỳ hiện tại", "Hệ thống cảnh báo");
                    return;
                }

                cl = new Server_Client.Client();
                // gán TCPclient
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                // khởi tạo biến truyền vào với hàm khởi tạo
                Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                pt[0] = new Entities.SoDuCongNo("Select");
                // khởi tạo mảng đối tượng để hứng giá trị
                clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                // đổ mảng đối tượng vào datagripview
                hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                // nếu obj khác null
                if (hienthi != null)
                {
                    for (int j = 0; j < hienthi.Length; j++)
                    {
                        string monthkytruoc = (Convert.ToInt32(month) - 1).ToString();
                        string mm = hienthi[j].NgayKetChuyen.Month.ToString();
                        // kiểm tra kỳ trước của tháng đang chọn
                        if (mm == monthkytruoc)
                        {
                            MessageBox.Show("Số dư đầu kỳ là kết chuyển từ kỳ trước - không thể thay đổi", "Hệ thống cảnh báo");
                            SelectData();
                            return;
                        }
                    }
                }
                if (MessageBox.Show("Bạn chắc chắn muốn xóa không?", "Hệ thống cảnh báo", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {

                    // nếu kỳ trc không tồn tại
                    tsslma.Text = dtgvcongno["MaDoiTuong", i].Value.ToString();
                    tsslten.Text = dtgvcongno["TenDoiTuong", i].Value.ToString();
                    tssldiachi.Text = dtgvcongno["DiaChi", i].Value.ToString();
                    tsslsodudauky.Text = dtgvcongno["SoDuDauKy", i].Value.ToString();
                    sono = Convert.ToDouble(dtgvcongno["SoDuDauKy", i].Value.ToString());
                    if (dtgvcongno.RowCount >= 1)
                    {
                        if (DeleteCongNo())
                        {
                            if (cbbdoituong.SelectedIndex == 0)
                            {
                                Entities.KhachHang kh = new Entities.KhachHang("UpdateDuNoKH", tsslma.Text, sono.ToString());
                                UpdateDuNo(kh);
                            }
                            else
                            {
                                Entities.NhaCungCap kh = new Entities.NhaCungCap("UpdateDuNo", tsslma.Text, sono.ToString());
                                UpdateDuNoNCC(kh);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Xóa thất bại - xin thử lại sau", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                fix();
            }
        }
 /// <summary>
 /// luu chi tiet don hang
 /// </summary>
 private void LuuChiTietTraLaiNhaCungCap()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     Entities.ChiTietTraLaiNhaCungCap[] luu = ChiTietTraLaiNhaCungCap(dgvXemthongtin, "Insert", txtSodonhang.Text.ToUpper());
     clientstrem = cl.SerializeHepper(this.client, "ChiTietTraLaiNhaCungCap", luu);
     int bao = 0;
     bao = (int)cl.DeserializeHepper(clientstrem, bao);
 }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                double.Parse(tsslsodudauky.Text);
            }
            catch
            {
                MessageBox.Show("Số dư đầu kỳ không được nhập ký tự", "Hệ thống cảnh báo");
                tsslsodudauky.Focus();
                return;
            }
            try
            {
                // kiểm tra giá trị trong dtgv
                if (CheckDataGridViewInsert() == true)
                {
                    string month1 = datesv.Month.ToString();
                    //nếu tháng hiện tại không = tháng đang xử lý
                    if (month1 != month)
                    {
                        MessageBox.Show("Không phải kỳ hiện tại - xin kiểm tra lại", "Hệ thống cảnh báo");
                        return;
                    }

                    cl = new Server_Client.Client();
                    // gán TCPclient
                    this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                    // khởi tạo biến truyền vào với hàm khởi tạo
                    Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                    pt[0] = new Entities.SoDuCongNo("Select");
                    // khởi tạo mảng đối tượng để hứng giá trị
                    clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                    // đổ mảng đối tượng vào datagripview
                    hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                    // nếu obj khác null
                    if (hienthi != null)
                    {
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            string monthkytruoc = (Convert.ToInt32(month) - 1).ToString();
                            string mm = hienthi[i].NgayKetChuyen.Month.ToString();
                            // kiểm tra kỳ trước có tồn tại số dư hay không
                            if (mm == monthkytruoc && hienthi[i].MaDoiTuong == tsslma.Text)
                            {
                                MessageBox.Show("Số dư đầu kỳ là kết chuyển từ kỳ trước - không thể thay đổi", "Hệ thống cảnh báo");
                                return;
                            }
                        }
                    }
                    //nếu không tồn tại số dư kỳ trước
                    try
                    {
                        bool loaidt = false;
                        if (cbbdoituong.SelectedIndex == 0)
                            loaidt = false;
                        else
                            loaidt = true;

                        string masd = ProID("SoDuCongNo");
                        cl = new Server_Client.Client();
                        // gán TCPclient
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        // khởi tạo biến truyền vào với hàm khởi tạo
                        Entities.SoDuCongNo[] pt1 = new Entities.SoDuCongNo[1];
                        pt1[0] = new Entities.SoDuCongNo("Insert", 0, masd, tsslma.Text, tsslten.Text, tssldiachi.Text, tsslsodudauky.Text, datesv, "0", loaidt, false);
                        // khởi tạo mảng đối tượng để hứng giá trị
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt1);
                        // đổ mảng đối tượng vào datagripview
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {

                            if (cbbdoituong.SelectedIndex == 0)
                            {
                                Entities.KhachHang kh = new Entities.KhachHang("UpdateDuNo", tsslma.Text, tsslsodudauky.Text);
                                UpdateDuNo(kh);
                            }
                            else
                            {
                                Entities.NhaCungCap kh = new Entities.NhaCungCap("UpdateDuNoNCC", tsslma.Text, tsslsodudauky.Text);
                                UpdateDuNoNCC(kh);
                            }
                            tsslsodudauky.Text = tsslten.Text = tssldiachi.Text = "";
                            tsslma.Text = "<F4>-Tra cứu";
                            SelectData();
                            tsslma.Focus();
                        }
                        else
                        {
                            MessageBox.Show("Thêm số dư đầu kỳ thất bại - xin thử lại sau","Hệ thống cảnh báo");
                            return;
                        }
                    }
                    catch
                    {
                    }

                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// luu chi tiet don hang
        /// </summary>
        private void LuuChiTietKiemKeKho(string hangDong)
        {
            try
            {
                cl = new Server_Client.Client();
                this.client = cl.Connect(Luu.IP, Luu.Ports);
                Entities.ChiTietKiemKeKho[] luu = LuuThongTinChiTietKiemKeKho(dgvInsertOrder, hangDong, txtSochungtu.Text, txtDiengiai.Text);
                if (hangDong.Equals("Update"))
                {
                    // Cap nhat trang thai(isChange) cho ChiTietKiemKeKho
                    luu = GetChange(luu, this.ctTemp);
                }

                clientstrem = cl.SerializeHepper(this.client, "ThemchiTietKiemKeKho", luu);
                int bao = 0;
                bao = (int)cl.DeserializeHepper(clientstrem, bao);

                if (bao >= 1)
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Chưa thêm hàng hóa vào đơn này");
                }
            }
            catch
            {
            }
        }
        private void tsslthuchien_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {
                bool loaidoituong=false;
                string monthhientai = datesv.Month.ToString();
                string namhientai = datesv.Year.ToString();
                if(cbbdoituong.SelectedIndex==0)
                {
                    loaidoituong = false;
                }
                else
                    loaidoituong = true;

                if (hienthi != null)
                {
                    string month = hienthi[0].NgayKetChuyen.Month.ToString();
                    string year = hienthi[0].NgayKetChuyen.Year.ToString();
                    if (month == monthhientai && year == namhientai)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            if (month == hienthi[i].NgayKetChuyen.Month.ToString() && year == hienthi[i].NgayKetChuyen.Year.ToString() && hienthi[i].LoaiDoiTuong==loaidoituong)
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            MessageBox.Show("Bạn phải kết chuyển kỳ trước - Kiểm tra lại", "Hệ thống cảnh báo");
                            return;
                        }
                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbdoituong.SelectedIndex);
                        if (frmCongNo.mangchitiet.ToArray().Length == 0)
                        {
                            frmCongNo.mangchitiet = new List<Entities.SoDuCongNo>();
                            frmCongNo.mangchitiet[0] = new Entities.SoDuCongNo("UpdateTrangThai", "");
                            System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                            {
                                if (giatri == System.Windows.Forms.DialogResult.No)
                                {
                                    return;
                                }
                            }
                        }
                        cl = new Server_Client.Client();
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {
                            SelectData();
                        }
                        else
                        {
                            MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc = "";
                        if ((datesv.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc = (datesv.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (datesv.Month - 1).ToString();
                            yearkytruoc = datesv.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {

                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbdoituong.SelectedIndex);
                            if (frmCongNo.mangchitiet.ToArray().Length == 0)
                            {
                                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                                {
                                    if (giatri == System.Windows.Forms.DialogResult.No)
                                    {
                                        return;
                                    }
                                }
                            }
                            cl = new Server_Client.Client();
                            this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                            clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                            bool kt = false;
                            kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                            if (kt == true)
                            {
                                SelectData();
                            }
                            else
                            {
                                MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Không phải kỳ hiện tại - không thể kết chuyển", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
                else
                {
                    string month = cbbthang.Text;
                    string year = cbbnam.Text;

                    if (hienthi1 != null)
                    {
                        int soluong = 0;
                        for (int i = 0; i < hienthi1.Length; i++)
                        {
                            if (month == hienthi1[i].NgayKetChuyen.Month.ToString() && year == hienthi1[i].NgayKetChuyen.Year.ToString())
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            MessageBox.Show("Bạn phải kết chuyển kỳ trước - Kiểm tra lại", "Hệ thống cảnh báo");
                            return;
                        }
                    }

                    if (month == monthhientai && year == namhientai)
                    {

                        frmCongNo a = new frmCongNo(namhientai, monthhientai, cbbdoituong.SelectedIndex);
                        if (frmCongNo.mangchitiet.ToArray().Length == 0)
                        {
                            System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                            {
                                if (giatri == System.Windows.Forms.DialogResult.No)
                                {
                                    MessageBox.Show("Không phát sinh Công nợ nào", "Hệ thống cảnh báo");
                                    return;
                                }
                            }
                        }
                        cl = new Server_Client.Client();
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {
                            SelectData();
                        }
                        else
                        {
                            MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                        }
                    }
                    else
                    {
                        string monthkytruoc = "";
                        string yearkytruoc = "";
                        if ((datesv.Month - 1) == 0)
                        {
                            monthkytruoc = "12";
                            yearkytruoc = (datesv.Year - 1).ToString();
                        }
                        else
                        {
                            monthkytruoc = (datesv.Month - 1).ToString();
                            yearkytruoc = datesv.Year.ToString();
                        }
                        if (month == monthkytruoc && year == yearkytruoc)
                        {

                            frmCongNo a = new frmCongNo(yearkytruoc, monthkytruoc, cbbdoituong.SelectedIndex);
                            if (frmCongNo.mangchitiet.ToArray().Length == 0)
                            {
                                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show("Không phát sinh Công nợ nào - Bạn xác nhận kết chuyển không?", "Thông Báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                                {
                                    if (giatri == System.Windows.Forms.DialogResult.No)
                                    {
                                        return;
                                    }
                                }
                            }
                            cl = new Server_Client.Client();
                            this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                            Entities.SoDuCongNo[] pt = frmCongNo.mangchitiet.ToArray();
                            clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                            bool kt = false;
                            kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                            if (kt == true)
                            {
                                SelectData();
                            }
                            else
                            {
                                MessageBox.Show("Thất bại - xin hãy kiểm tra lại", "Hệ thống cảnh báo");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Không phải kỳ hiện tại - không thể kết chuyển", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                this.Enabled = true;
            }
        }
 public void SelectData()
 {
     try
     {
         bool loaiDoiTuong = false;
         if (cbbdoituong.SelectedIndex == 0)
         {
             loaiDoiTuong = false;
         }
         else
         {
             loaiDoiTuong = true;
         }
         cl = new Server_Client.Client();
         // gán TCPclient
         this.client1 = cl.Connect(Luu.IP, Luu.Ports);
         // khởi tạo biến truyền vào với hàm khởi tạo
         Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
         pt[0] = new Entities.SoDuCongNo("Select");
         // khởi tạo mảng đối tượng để hứng giá trị
         clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
         // đổ mảng đối tượng vào datagripview
         hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
         // nếu null
         if (hienthi == null)
         {
             hienthi               = new Entities.SoDuCongNo[0];
             lbtrangthai.Text      = "Kỳ này chưa đóng";
             lbtrangthai.ForeColor = Color.Black;
             toolStrip1.Enabled    = dtgvcongno.Enabled = true;
             return;
         }
         year  = cbbnam.Text;
         month = cbbthang.Text;
         int  sotang = 0;
         bool ktsd   = false;
         Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi.Length];
         for (int i = 0; i < sq.Length; i++)
         {
             string namkt   = hienthi[i].NgayKetChuyen.Year.ToString();
             string thangkt = hienthi[i].NgayKetChuyen.Month.ToString();
             // nếu năm tháng trong bản ghi - năm tháng hiện tại và loại đối tượng
             if (namkt == year && thangkt == month && hienthi[i].LoaiDoiTuong == loaiDoiTuong)
             {
                 // nếu trạng thái = false
                 if (hienthi[i].TrangThai == false)
                 {
                     lbtrangthai.Text      = "Kỳ này chưa đóng";
                     lbtrangthai.ForeColor = Color.Black;
                     toolStrip1.Enabled    = dtgvcongno.Enabled = true;
                 }
                 else
                 {
                     lbtrangthai.Text      = "Kỳ này đã khóa";
                     lbtrangthai.ForeColor = Color.Red;
                     toolStrip1.Enabled    = dtgvcongno.Enabled = false;
                 }
                 ma         = hienthi[i].MaSoDuCongNo;
                 sq[sotang] = hienthi[i];
                 sotang++;
                 ktsd = true;
             }
         }
         // nếu không có đối tượng nào đúng
         if (ktsd == false)
         {
             lbtrangthai.Text      = "Kỳ này chưa đóng";
             lbtrangthai.ForeColor = Color.Black;
             toolStrip1.Enabled    = dtgvcongno.Enabled = true;
         }
         // nếu không có bản ghi nào tồn tại
         if (sotang != 0)
         {
             hienthi = new Entities.SoDuCongNo[sotang];
             for (int i = 0; i < sotang; i++)
             {
                 hienthi[i] = new Entities.SoDuCongNo("", sq[i].MaDoiTuong, sq[i].TenDoiTuong, sq[i].DiaChi, sq[i].SoDuDauKy);
             }
         }
         else
         {
             hienthi = new Entities.SoDuCongNo[0];
             dtgvcongno.DataSource = hienthi;
             return;
         }
         dtgvcongno.DataSource = hienthi;
         new Common.Utilities().CountDatagridview(dtgvcongno);
     }
     catch
     {
     }
     finally
     {
         fix();
     }
 }
 /// <summary>
 /// luu chi tiet don hang
 /// </summary>
 private void LuuChiTietKhachHangTraLai()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     Entities.ChiTietKhachHangTraLai[] luu = LuuChiTietKhachHangTraLai(dgvXemthongtin, "Insert", txtSodonhang.Text.ToUpper());
     clientstrem = cl.SerializeHepper(this.client, "ChiTietKhachHangTraLai", luu);
 }
 /// <summary>
 /// luu chi tiet don hang
 /// </summary>
 private void LuuChiTietKiemKeKho()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     Entities.ChiTietKiemKeKho[] luu = LuuThongTinChiTietKiemKeKho(dgvInsertOrder, "Insert",txtSochungtu.Text,txtDiengiai.Text);
     clientstrem = cl.SerializeHepper(this.client, "ThemchiTietKiemKeKho", luu);
     int bao = 0;
     bao = (int)cl.DeserializeHepper(clientstrem, bao);
 }
        public void SelectData()
        {
            try
            {
                cl = new Server_Client.Client();
                this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                Entities.ChiTietKhachHangTraLai[] ctk = new Entities.ChiTietKhachHangTraLai[1];
                ctk[0] = new Entities.ChiTietKhachHangTraLai("Select");
                clientstrem = cl.SerializeHepper(this.client1, "ChiTietKhachHangTraLai", ctk);
                ctk = (Entities.ChiTietKhachHangTraLai[])cl.DeserializeHepper(clientstrem, ctk);
                dgvXemthongtin.DataSource = ctk;

            }
            catch
            {
            }
        }
 /// <summary>
 /// luu chi tiet don hang
 /// </summary>
 private void LuuChiTietDonHang()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     Entities.ChiTietHoaDonNhap[] luu = LuuChiTietHoaDonNhap(dgvInsertOrder, "Insert", txtSodonhang.Text.ToUpper());
     clientstrem = cl.SerializeHepper(this.client, "ChiTietHoaDonNhap", luu);
     int bao = 0;
     bao = (int)cl.DeserializeHepper(clientstrem, bao);
     if (bao == 1)
     {
         int k = dgvInsertOrder.RowCount;
         Entities.ChiTietKhoHangTheoHoaHonNhap[] khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[k];
         ArrayList arr = new ArrayList();
         if (k > 0)
         {
             for (int x = 0; x < k; x++)
             {
                 Entities.ChiTietKhoHangTheoHoaHonNhap kho = new Entities.ChiTietKhoHangTheoHoaHonNhap();
                 kho.Hanhdong = "Insert";
                 kho.Makho = cbxKhoHang.SelectedValue.ToString();
                 kho.Mahanghoa = dgvInsertOrder.Rows[x].Cells[1].Value.ToString();
                 kho.Soluong = System.Convert.ToInt32(dgvInsertOrder.Rows[x].Cells[3].Value.ToString());
                 kho.Ngaynhap = DateTime.Now;
                 kho.Ngayhethan = 0;
                 kho.Ghichu = txtDiengiai.Text;
                 kho.Deleted = false;
                 arr.Add(kho);
             }
             int n = arr.Count;
             if (n == 0)
             { khohang = null; }
             khohang = new Entities.ChiTietKhoHangTheoHoaHonNhap[n];
             for (int j = 0; j < n; j++)
             { khohang[j] = (Entities.ChiTietKhoHangTheoHoaHonNhap)arr[j]; }
         }
         else
         { khohang = null;}
         LuuChiTietVaoKhoHang(khohang);
     }
     else
     { MessageBox.Show("Chưa thêm vào kho"); }
 }
 /// <summary>
 /// luu chi tiet don hang
 /// </summary>
 private void LuuChiTietDonHang()
 {
     cl = new Server_Client.Client();
     this.client = cl.Connect(Luu.IP, Luu.Ports);
     Entities.ChiTietDonDatHang[] luu = LuuChiTietKhoHang(dgvInsertOrder, "Insert", txtSodonhang.Text.ToUpper());
     clientstrem = cl.SerializeHepper(this.client, "ChiTietDonDatHang", luu);
     int bao = 0;
     bao = (int)cl.DeserializeHepper(clientstrem, bao);
 }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                double.Parse(tsslsodudauky.Text);
            }
            catch
            {
                MessageBox.Show("Số dư đầu kỳ không được nhập ký tự", "Hệ thống cảnh báo");
                tsslsodudauky.Focus();
                return;
            }
            try
            {
                // kiểm tra giá trị trong dtgv
                if (CheckDataGridViewInsert() == true)
                {
                    string month1 = datesv.Month.ToString();
                    //nếu tháng hiện tại không = tháng đang xử lý
                    if (month1 != month)
                    {
                        MessageBox.Show("Không phải kỳ hiện tại - xin kiểm tra lại", "Hệ thống cảnh báo");
                        return;
                    }

                    cl = new Server_Client.Client();
                    // gán TCPclient
                    this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                    // khởi tạo biến truyền vào với hàm khởi tạo
                    Entities.SoDuCongNo[] pt = new Entities.SoDuCongNo[1];
                    pt[0] = new Entities.SoDuCongNo("Select");
                    // khởi tạo mảng đối tượng để hứng giá trị
                    clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt);
                    // đổ mảng đối tượng vào datagripview
                    hienthi = (Entities.SoDuCongNo[])cl.DeserializeHepper1(clientstrem, hienthi);
                    // nếu obj khác null
                    if (hienthi != null)
                    {
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            string monthkytruoc = (Convert.ToInt32(month) - 1).ToString();
                            string mm           = hienthi[i].NgayKetChuyen.Month.ToString();
                            // kiểm tra kỳ trước có tồn tại số dư hay không
                            if (mm == monthkytruoc && hienthi[i].MaDoiTuong == tsslma.Text)
                            {
                                MessageBox.Show("Số dư đầu kỳ là kết chuyển từ kỳ trước - không thể thay đổi", "Hệ thống cảnh báo");
                                return;
                            }
                        }
                    }
                    //nếu không tồn tại số dư kỳ trước
                    try
                    {
                        bool loaidt = false;
                        if (cbbdoituong.SelectedIndex == 0)
                        {
                            loaidt = false;
                        }
                        else
                        {
                            loaidt = true;
                        }

                        string masd = ProID("SoDuCongNo");
                        cl = new Server_Client.Client();
                        // gán TCPclient
                        this.client1 = cl.Connect(Luu.IP, Luu.Ports);
                        // khởi tạo biến truyền vào với hàm khởi tạo
                        Entities.SoDuCongNo[] pt1 = new Entities.SoDuCongNo[1];
                        pt1[0] = new Entities.SoDuCongNo("Insert", 0, masd, tsslma.Text, tsslten.Text, tssldiachi.Text, tsslsodudauky.Text, datesv, "0", loaidt, false);
                        // khởi tạo mảng đối tượng để hứng giá trị
                        clientstrem = cl.SerializeHepper(this.client1, "CongNo", pt1);
                        // đổ mảng đối tượng vào datagripview
                        bool kt = false;
                        kt = (bool)cl.DeserializeHepper(clientstrem, kt);
                        if (kt == true)
                        {
                            if (cbbdoituong.SelectedIndex == 0)
                            {
                                Entities.KhachHang kh = new Entities.KhachHang("UpdateDuNo", tsslma.Text, tsslsodudauky.Text);
                                UpdateDuNo(kh);
                            }
                            else
                            {
                                Entities.NhaCungCap kh = new Entities.NhaCungCap("UpdateDuNoNCC", tsslma.Text, tsslsodudauky.Text);
                                UpdateDuNoNCC(kh);
                            }
                            tsslsodudauky.Text = tsslten.Text = tssldiachi.Text = "";
                            tsslma.Text        = "<F4>-Tra cứu";
                            SelectData();
                            tsslma.Focus();
                        }
                        else
                        {
                            MessageBox.Show("Thêm số dư đầu kỳ thất bại - xin thử lại sau", "Hệ thống cảnh báo");
                            return;
                        }
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }