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 cbNhaCC_SelectedIndexChanged(object sender, EventArgs e)
 {
     tblphieunhapkhoBL ctrpn = new tblphieunhapkhoBL();
     string sidnhacc = "";
     try { sidnhacc = cbNhaCC.SelectedValue.ToString().Trim(); }
     catch { }
     nhacungcap obj = new nhacungcap();
     nhacungcapBL ctr = new nhacungcapBL();
     obj = ctr.GetByID(sidnhacc);
     try
     {
         try
         {
             txtNoToaTruoc.Text = ctrpn.GetTienConNo(sidnhacc, dpkNgayNhap.Value).ToString("N0", CultureInfo.InvariantCulture);
         }
         catch { txtNoToaTruoc.Text = "0"; }
         txtDiaChi.Text = obj.diachi;
         txtDienThoai.Text = obj.dienthoai.Trim();
         double tongtien = 0;
         try { tongtien = Convert.ToDouble(txtTongTien.Text); }
         catch { }
         double tienconno = 0;
         try { tienconno = Convert.ToDouble(txtNoToaTruoc.Text); }
         catch { }
         double tientt = 0;
         try { tientt = Convert.ToDouble(txtTienThanhToan.Text); }
         catch { }
         double chietkhau = 0;
         try { chietkhau = Convert.ToDouble(txtChietKhau.Text); }
         catch { }
         txtConNo.Text = ((tienconno + tongtien) - (tientt + chietkhau)).ToString("N0", CultureInfo.InvariantCulture);
     }
     catch
     {
         txtDiaChi.Text = "";
         txtDienThoai.Text = "";
         txtTongTien.Text = "0";
     }
 }
 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); }
 }