private tblphieunhapkho GetDataPhieuNhap()
 {
     tblphieunhapkho obj = new tblphieunhapkho();
     string sidnhacc = "";
     try { sidnhacc = cbNhaCC.SelectedValue.ToString().Trim(); }
     catch { }
     if (sidnhacc.Trim().Equals("") == true)
     {
         MessageBox.Show("Nhà cung cấp chưa được chọn.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         cbNhaCC.Focus();
         return null;
     }
     if (txtID.Text.Equals("-1") == true) { obj.id = Guid.NewGuid().ToString(); }
     else { obj.id = txtID.Text.Trim(); }
     try { obj.tienthanhtoan = Convert.ToDouble(txtTienThanhToan.Text.Trim()); }
     catch { }
     try { obj.chietkhau = Convert.ToDouble(txtChietKhau.Text.Trim()); }
     catch { }
     obj.ngaytao = dpkNgayNhap.Value;
     obj.ghichu = txtGhiChu.Text.Trim();
     obj.noinhap = txtNoiNhap.Text.Trim();
     obj.id_nguoicap = sidnhacc;
     obj.noinhap = "";
     return obj;
 }
        private List <tienthanhtoanphieunhap> GetListTienThanhToan()
        {
            tblphieunhapkhoBL ctrphieunhap = new tblphieunhapkhoBL();
            tblphieunhapkho   objphieunhap = new tblphieunhapkho();

            objphieunhap = ctrphieunhap.GetByID(sidpn);
            try
            {
                List <tienthanhtoanphieunhap> lst = new List <tienthanhtoanphieunhap>();
                for (int i = 1; i < c1FlexGrid1.Rows.Count; i++)
                {
                    tienthanhtoanphieunhap obj = new tienthanhtoanphieunhap();
                    if (c1FlexGrid1[i, "tt"].ToString().Trim().Equals("0") == false)
                    {
                        if (c1FlexGrid1[i, "tt"].ToString().Trim().Equals("1") == true)
                        {
                            obj.id = Guid.NewGuid().ToString();
                        }
                        else if (c1FlexGrid1[i, "tt"].ToString().Trim().Equals("2") == true)
                        {
                            obj.id = c1FlexGrid1[i, "id"].ToString().Trim();
                        }
                        if (sidpn.Trim().Equals("") == true)
                        {
                            MessageBox.Show("Không lấy được thông tin hóa đơn.\nVui lòng tắt chương trình và khởi động lại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            c1FlexGrid1.Select(i, c1FlexGrid1.Cols["tientt"].Index);
                            return(null);
                        }
                        try { obj.tientt = Convert.ToDouble(c1FlexGrid1[i, "tientt"].ToString().Trim()); }
                        catch
                        {
                            MessageBox.Show("Tiền thanh toán nhập không đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            c1FlexGrid1.Select(i, c1FlexGrid1.Cols["tientt"].Index);
                            return(null);
                        }
                        if (obj.tientt == 0)
                        {
                            MessageBox.Show("Tiền thanh toán phải lớn hơn 0", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            c1FlexGrid1.Select(i, c1FlexGrid1.Cols["tientt"].Index);
                            return(null);
                        }
                        obj.idpn   = sidpn;
                        obj.ngaytt = Convert.ToDateTime(c1FlexGrid1[i, "ngaytt"].ToString().Trim());
                        if (objphieunhap.ngaytao == obj.ngaytt)
                        {
                            MessageBox.Show("Bản ghi này đã tồn tại.\nVui lòng nhập lại ngày thanh toán", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            c1FlexGrid1.Select(i, c1FlexGrid1.Cols["ngaytt"].Index);
                            return(null);
                        }
                        obj.ghichu = c1FlexGrid1[i, "ghichu"].ToString().Trim();
                        lst.Add(obj);
                    }
                }
                return(lst);
            }
            catch { return(null); }
        }
 private void HienThiTT()
 {
     try
     {
         tblphieunhapkho obj = new tblphieunhapkho();
         tblphieunhapkhoBL ctr = new tblphieunhapkhoBL();
         obj = ctr.GetByID(txtID.Text.Trim());
         dpkNgayNhap.Value = obj.ngaytao;
         cbNhaCC.SelectedValue = obj.id_nguoicap;
         txtChietKhau.Text = obj.chietkhau.ToString("N0", CultureInfo.InvariantCulture);
         txtTienThanhToan.Text = obj.tienthanhtoan.ToString("N0", CultureInfo.InvariantCulture);
         dpkNgayNhap.Value = obj.ngaytao;
         txtNoToaTruoc.Text = ctr.GetTienConNo(obj.id_nguoicap, obj.ngaytao).ToString("N0", CultureInfo.InvariantCulture);
         txtGhiChu.Text = obj.ghichu.Trim();
         txtNoiNhap.Text = obj.noinhap.Trim();
     }
     catch
     {
         HienThiDSMatHang();
     }
 }
        private void HienThiTTPhieuNhap()
        {
            tblphieunhapkho   objpn = new tblphieunhapkho();
            tblphieunhapkhoBL ctrpn = new tblphieunhapkhoBL();

            objpn = ctrpn.GetByID(sidpn);
            try{
                nhacungcap   objnhacc = new nhacungcap();
                nhacungcapBL ctrnhacc = new nhacungcapBL();
                objnhacc         = ctrnhacc.GetByID(objpn.id_nguoicap.Trim());
                lbDiaChi.Text    = objnhacc.diachi;
                lbDienThoai.Text = objnhacc.dienthoai;
                lbNgayXuat.Text  = objpn.ngaytao.ToString("dd/MM/yyyy HH:mm:ss");
                lbTenKH.Text     = objnhacc.ten;
            }
            catch {
                lbDiaChi.Text    = "-/-";
                lbDienThoai.Text = "-/-";
                lbNgayXuat.Text  = "-/-";
                lbTenKH.Text     = "-/-";
            }
        }
 private void Save()
 {
     string kq = "";
     tienthanhtoanphieunhapBL ctrtientt = new tienthanhtoanphieunhapBL();
     tblmathangBL ctrmathang = new tblmathangBL();
     tblphieunhapkhoBL ctrphieu = new tblphieunhapkhoBL();
     tblphieunhapkho objphieu = new tblphieunhapkho();
     objphieu = GetDataPhieuNhap();
     tblhangnhapkhoBL ctrmathangnhap = new tblhangnhapkhoBL();
     List<tblhangnhapkho> lstmathang = new List<tblhangnhapkho>();
     lstmathang = GetListMatHang();
     if (objphieu != null && lstmathang != null)
     {
         if (txtID.Text.Trim().Equals("-1") == true)
         {
             kq = ctrphieu.Insert(objphieu);
             if (kq.Trim().Equals("") == true)
             {
                 tienthanhtoanphieunhap objtientt = new tienthanhtoanphieunhap();
                 objtientt.id = Guid.NewGuid().ToString().Trim();
                 objtientt.idpn = objphieu.id;
                 objtientt.ngaytt = objphieu.ngaytao;
                 objtientt.tientt = objphieu.tienthanhtoan;
                 ctrtientt.Insert(objtientt);
             }
         }
         else
         {
             kq = ctrphieu.Update(objphieu);
             tienthanhtoanphieunhap objtientt = new tienthanhtoanphieunhap();
             objtientt = ctrtientt.GetByIDPNvsNgayTT(objphieu.id, objphieu.ngaytao);
             if (objtientt != null)
             {
                 objtientt.tientt = objphieu.tienthanhtoan;
                 ctrtientt.Update(objtientt);
             }
             else
             {
                 objtientt = new tienthanhtoanphieunhap();
                 objtientt.id = Guid.NewGuid().ToString().Trim();
                 objtientt.idpn = objphieu.id;
                 objtientt.ngaytt = objphieu.ngaytao;
                 objtientt.tientt = objphieu.tienthanhtoan;
                 ctrtientt.Insert(objtientt);
             }
             _ctrlog.Append(Data.use, "Sửa phiếu nhập kho của nhà cung cấp: " + cbNhaCC.Text.Trim()
                                + " xuất ngày: " + dpkNgayNhap.Value.ToString("dd/MM/yyyy").Trim()
                                + "; id: " + objphieu.id
                                + ";\nChi tiết: Tổng tiền hàng: " + txtTongTien.Text + " - Nợ cũ: " + txtNoToaTruoc.Text
                                + " - Tiền thanh toán: " + txtTienThanhToan.Text + " - Tổng nợ mới: " + txtConNo.Text);
         }
         if (kq.Trim().Equals("") == true)
         {
             if (lstmathang != null)
             {
                 if (objphieu != null && lstmathang.Count > 0)
                 {
                     for (int i = 0; i < lstmathang.Count; i++)
                     {
                         tblhangnhapkho temp = new tblhangnhapkho();
                         temp = ctrmathangnhap.GetByID(lstmathang[i].id);
                         if (temp == null)
                         {
                             lstmathang[i].id_phieunhapkho = objphieu.id;
                             kq = ctrmathangnhap.Insert(lstmathang[i]);
                             if (kq.Trim().Equals("") == false)
                             {
                                 ctrphieu.Delete(objphieu.id);
                                 break;
                             }
                             else //Thêm số lượng vào trong kho
                             {
                                 tblmathang objmathang = new tblmathang();
                                 objmathang = ctrmathang.GetByID(lstmathang[i].id_mathang.Trim());
                                 objmathang.soluong = objmathang.soluong + lstmathang[i].soluong;
                                 objmathang.gianhap = lstmathang[i].gianhap;
                                 ctrmathang.Update(objmathang);
                             }
                         }
                         else
                         {
                             lstmathang[i].id_phieunhapkho = objphieu.id;
                             kq = ctrmathangnhap.Update(lstmathang[i]);
                             if (kq.Trim().Equals("") == false)
                             {
                                 break;
                             }
                             else //Cập nhật lại số lượng còn trong kho
                             {
                                 tblmathang objmathang = new tblmathang();
                                 objmathang = ctrmathang.GetByID(lstmathang[i].id_mathang.Trim());
                                 objmathang.soluong = (objmathang.soluong - temp.soluong) + lstmathang[i].soluong;
                                 objmathang.gianhap = lstmathang[i].gianhap;
                                 ctrmathang.Update(objmathang);
                             }
                         }
                     }
                     if (kq.Trim().Equals("") == false)
                     {
                         MessageBox.Show("Lỗi cập nhật phiếu nhập hàng.\nChi tiết lỗi: " + kq, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                         return;
                     }
                     else
                     {
                         if (txtID.Text.Trim().Equals("-1") == true)
                         {
                             txtID.Text = objphieu.id.Trim();
                             _ctrlog.Append(Data.use, "Thêm mới phiếu nhập hàng của nhà cung cấp: " + cbNhaCC.Text.Trim()
                                 + " xuất ngày: " + dpkNgayNhap.Value.ToString("dd/MM/yyyy").Trim()
                                 + "; id: " + objphieu.id
                                 + ";\nChi tiết: Tổng tiền hàng: " + txtTongTien.Text + " - Nợ cũ: " + txtNoToaTruoc.Text
                                 + " - Tiền thanh toán: " + txtTienThanhToan.Text + " - Tổng nợ mới: " + txtConNo.Text);
                         }
                     }
                 }
             }
         }
         else
         {
             MessageBox.Show("Lỗi cập nhật phiếu nhập hàng.\nChi tiết lỗi: " + kq, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
             return;
         }
     }
     if (kq.Trim().Equals("") == true && lstmathang != null)
     {
         MessageBox.Show("Cập nhật phiếu nhập hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         HienThiTT();
         HienThiDSMatHang();
         ultraToolbarsManager1.Tools["btn_Del"].SharedProps.Visible = true;
     }
 }
 /// <summary>
 /// Cập nhật dữ liệu vào bảng: tblphieunhapkho
 /// </summary>
 /// <param name="obj">objtblphieunhapkho</param>
 /// <returns>Trả về trắng: Cập nhật thành công; Trả về khác trắng: Cập nhật không thành công</returns>
 public string Update(tblphieunhapkho objtblphieunhapkho)
 {
     return(objtblphieunhapkho.Update(objtblphieunhapkho));
 }
 /// <summary>
 /// Thêm mới dữ liệu vào bảng: tblphieunhapkho
 /// </summary>
 /// <param name="obj">objtblphieunhapkho</param>
 /// <returns>Trả về trắng: Thêm mới thành công; Trả về khác trắng: Thêm mới không thành công</returns>
 public string Insert(tblphieunhapkho objtblphieunhapkho)
 {
     return(objtblphieunhapkhoDA.Insert(objtblphieunhapkho));
 }
 public tblphieunhapkhoBL()
 {
     objtblphieunhapkhoDA = new tblphieunhapkho();
 }
 private string GenerateWorksheetTKCongNo(WorksheetCollection sheets, DateTime dtdenngay, DataTable dtnhacc, ProgressBar progressBar)
 {
     try
     {
         tblphieunhapkhoBL        ctrpn          = new tblphieunhapkhoBL();
         tblphieunhapkho          objpn          = new tblphieunhapkho();
         tblhangnhapkhoBL         ctrhangnhapkho = new tblhangnhapkhoBL();
         tienthanhtoanphieunhapBL ctrtiendatt    = new tienthanhtoanphieunhapBL();
         Worksheet sheet = sheets.Add("TKCongNoNhapHang");
         sheet.Table.DefaultRowHeight    = 15F;
         sheet.Table.ExpandedColumnCount = 5;
         sheet.Table.ExpandedRowCount    = 5 + dtnhacc.Rows.Count;
         sheet.Table.FullColumns         = 1;
         sheet.Table.FullRows            = 1;
         sheet.Table.Columns.Add(36);
         sheet.Table.Columns.Add(193);
         sheet.Table.Columns.Add(149);
         sheet.Table.Columns.Add(191);
         sheet.Table.Columns.Add(159);
         // -----------------------------------------------
         #region Add Header
         WorksheetRow Row0 = sheet.Table.Rows.Add();
         Row0.Height        = 20;
         Row0.AutoFitHeight = false;
         WorksheetCell cell;
         cell             = Row0.Cells.Add();
         cell.StyleID     = "s75";
         cell.Data.Type   = DataType.String;
         cell.Data.Text   = "TỔNG HỢP CÔNG NỢ NHẬP HÀNG";
         cell.MergeAcross = 4;
         // -----------------------------------------------
         WorksheetRow Row1 = sheet.Table.Rows.Add();
         Row1.AutoFitHeight = false;
         cell             = Row1.Cells.Add();
         cell.StyleID     = "s76";
         cell.Data.Type   = DataType.String;
         cell.Data.Text   = "(Số liệu tính đến ngày " + dtdenngay.ToString("dd/MM/yyyy") + ")";
         cell.MergeAcross = 4;
         // -----------------------------------------------
         WorksheetRow Row2 = sheet.Table.Rows.Add();
         Row2.AutoFitHeight = false;
         cell             = Row2.Cells.Add();
         cell.StyleID     = "m83608040";
         cell.Data.Type   = DataType.String;
         cell.Data.Text   = "Đơn vị tính: VNĐ";
         cell.MergeAcross = 4;
         // -----------------------------------------------
         WorksheetRow Row3 = sheet.Table.Rows.Add();
         Row3.AutoFitHeight = false;
         Row3.Cells.Add("Stt", DataType.String, "s67");
         Row3.Cells.Add("Nhà cung cấp", DataType.String, "s67");
         Row3.Cells.Add("Điện thoại", DataType.String, "s67");
         Row3.Cells.Add("Địa chỉ", DataType.String, "s67");
         Row3.Cells.Add("Tổng tiền còn nợ", DataType.String, "s67");
         #endregion
         // -----------------------------------------------
         #region Add Content
         for (int i = 0; i < dtnhacc.Rows.Count; i++)
         {
             Application.DoEvents();
             progressBar.PerformStep();
             Application.DoEvents();
             string sidnhacc = "";
             try { sidnhacc = dtnhacc.Rows[i]["id"].ToString().Trim(); }
             catch { sidnhacc = "-/-"; }
             string stennhacc = "";
             try { stennhacc = dtnhacc.Rows[i]["ten"].ToString().Trim(); }
             catch { stennhacc = "-/-"; }
             string sdt = "";
             try { sdt = dtnhacc.Rows[i]["dienthoai"].ToString().Trim(); }
             catch { sdt = "-/-"; }
             string sdiachi = "";
             try { sdiachi = dtnhacc.Rows[i]["diachi"].ToString().Trim(); }
             catch { sdiachi = "-/-"; }
             if (sidnhacc.Trim().Equals("") == false)
             {
                 // Lấy hóa đơn gần với ngày thống kê nhất
                 objpn = new tblphieunhapkho();
                 Application.DoEvents();
                 progressBar.PerformStep();
                 Application.DoEvents();
                 objpn = ctrpn.GetNewFirstByNgayTaovsIDKH(dtdenngay.AddDays(1).ToString("yyyy/MM/dd").Trim(), sidnhacc);
                 if (objpn != null)
                 {
                     double tienmuahang = 0;
                     try { tienmuahang = ctrhangnhapkho.GetTongTienByIDPN(objpn.id); }
                     catch { }
                     //double tientt = 0;
                     //tientt = objpn.tienthanhtoan;
                     double chietkhau = 0;
                     chietkhau = objpn.chietkhau;
                     double tiendatt = 0;
                     try { tiendatt = ctrtiendatt.GetTienDaThanhToan(objpn.id); }
                     catch { }
                     double tienconnotoatruoc = 0;
                     try { tienconnotoatruoc = ctrpn.GetTienConNo(objpn.id_nguoicap, objpn.ngaytao); }
                     catch { }
                     double tienconno = 0;
                     tienconno = ((tienmuahang - tiendatt - chietkhau) + tienconnotoatruoc);
                     Application.DoEvents();
                     progressBar.PerformStep();
                     Application.DoEvents();
                     WorksheetRow Row = sheet.Table.Rows.Add();
                     Row.Cells.Add((i + 1).ToString(), DataType.Number, "s68");
                     Row.Cells.Add(stennhacc, DataType.String, "s69");
                     Row.Cells.Add(sdt, DataType.String, "s69");
                     Row.Cells.Add(sdiachi, DataType.String, "s69");
                     Row.Cells.Add(tienconno.ToString().Trim(), DataType.Number, "s70");
                 }
             }
         }
         #endregion
         // -----------------------------------------------
         #region Tính tổng các dòng
         WorksheetRow Row7 = sheet.Table.Rows.Add();
         Row7.AutoFitHeight = false;
         cell             = Row7.Cells.Add();
         cell.StyleID     = "m83608060";
         cell.Data.Type   = DataType.String;
         cell.Data.Text   = "TỔNG CỘNG";
         cell.MergeAcross = 3;
         cell             = Row7.Cells.Add();
         cell.StyleID     = "s74";
         cell.Data.Type   = DataType.Number;
         cell.Formula     = "=SUM(R[-" + dtnhacc.Rows.Count.ToString() + "]C:R[-1]C)";
         #endregion
         // -----------------------------------------------
         //  Options
         // -----------------------------------------------
         sheet.Options.Selected                     = true;
         sheet.Options.ProtectObjects               = false;
         sheet.Options.ProtectScenarios             = false;
         sheet.Options.PageSetup.Header.Margin      = 0.3F;
         sheet.Options.PageSetup.Footer.Margin      = 0.3F;
         sheet.Options.PageSetup.PageMargins.Bottom = 0.75F;
         sheet.Options.PageSetup.PageMargins.Left   = 0.7F;
         sheet.Options.PageSetup.PageMargins.Right  = 0.7F;
         sheet.Options.PageSetup.PageMargins.Top    = 0.75F;
         sheet.Options.Print.HorizontalResolution   = 300;
         sheet.Options.Print.VerticalResolution     = 0;
         sheet.Options.Print.ValidPrinterInfo       = true;
         return("");
     }
     catch (Exception ex) { return(ex.Message); }
 }