Exemplo n.º 1
0
        private bool ExportInvoice()
        {
            try
            {
                if (!CheckInfo())
                {
                    return(false);
                }

                HoaDonXuatTruoc invoice = new HoaDonXuatTruoc();
                invoice.SoHoaDon          = _invoiceCode;
                invoice.NgayXuatHoaDon    = dtpkNgay.Value;
                invoice.TenNguoiMuaHang   = cboTenNguoiMuaHang.Text;
                invoice.DiaChi            = txtAddress.Text;
                invoice.TenDonVi          = cboTenDonVi.Text;
                invoice.MaDonVi           = cboMaDonVi.Text;
                invoice.MaSoThue          = txtMaSoThue.Text;
                invoice.SoTaiKhoan        = txtSoTaiKhoan.Text;
                invoice.HinhThucThanhToan = (byte)cboHinhThucThanhToan.SelectedIndex;
                invoice.VAT                = (double)numVAT.Value;
                invoice.CreatedDate        = DateTime.Now;
                invoice.CreatedBy          = Guid.Parse(Global.UserGUID);
                invoice.Status             = (byte)Status.Actived;
                invoice.ChuaThuTien        = !chkDaThuTien.Checked;
                invoice.MauSo              = _mauSo;
                invoice.KiHieu             = _kiHieu;
                invoice.HinhThucNhanHoaDon = raKhachTuLay.Checked ? "Khách tự lấy" : "Gởi qua bưu điện";
                invoice.Notes              = txtGhiChu.Text;

                List <ChiTietHoaDonXuatTruoc> addedDetails = new List <ChiTietHoaDonXuatTruoc>();
                for (int i = 0; i < dgDetail.RowCount - 1; i++)
                {
                    DataGridViewRow        row    = dgDetail.Rows[i];
                    ChiTietHoaDonXuatTruoc detail = new ChiTietHoaDonXuatTruoc();
                    detail.CreatedDate = DateTime.Now;
                    detail.CreatedBy   = Guid.Parse(Global.UserGUID);
                    detail.TenMatHang  = row.Cells["TenMatHang"].Value.ToString();
                    detail.DonViTinh   = row.Cells["DonViTinh"].Value.ToString();

                    int soLuong = 1;
                    if (row.Cells["SoLuong"].Value != null && row.Cells["SoLuong"].Value != DBNull.Value)
                    {
                        soLuong = Convert.ToInt32(row.Cells["SoLuong"].Value);
                    }
                    detail.SoLuong = soLuong;

                    int donGia = 0;
                    if (row.Cells["DonGia"].Value != null && row.Cells["DonGia"].Value != DBNull.Value)
                    {
                        donGia = Convert.ToInt32(row.Cells["DonGia"].Value);
                    }

                    detail.DonGia = donGia;

                    int thanhTien = 0;
                    if (row.Cells["ThanhTien"].Value != null && row.Cells["ThanhTien"].Value != DBNull.Value)
                    {
                        thanhTien = Convert.ToInt32(row.Cells["ThanhTien"].Value);
                    }

                    detail.ThanhTien = thanhTien;

                    addedDetails.Add(detail);
                }

                Result result = HoaDonXuatTruocBus.InsertHoaDonXuatTruoc(invoice, addedDetails, _fromDate, _toDate);
                if (result.IsOK)
                {
                    //Insert thông tin khách hàng
                    ThongTinKhachHang thongTinKhachHang = new ThongTinKhachHang();
                    thongTinKhachHang.TenKhachHang = invoice.TenNguoiMuaHang;
                    //thongTinKhachHang.TenDonVi = invoice.TenDonVi;
                    thongTinKhachHang.MaDonVi = invoice.MaDonVi;
                    //thongTinKhachHang.MaSoThue = invoice.MaSoThue;
                    //thongTinKhachHang.DiaChi = invoice.DiaChi;
                    thongTinKhachHang.SoTaiKhoan        = invoice.SoTaiKhoan;
                    thongTinKhachHang.HinhThucThanhToan = invoice.HinhThucThanhToan;
                    result = ThongTinKhachHangBus.InsertThongTinKhachHang(thongTinKhachHang);
                    if (!result.IsOK)
                    {
                        MsgBox.Show(Application.ProductName, result.GetErrorAsString("ThongTinKhachHangBus.InsertThongTinKhachHang"), IconType.Error);
                        Utility.WriteToTraceLog(result.GetErrorAsString("ThongTinKhachHangBus.InsertThongTinKhachHang"));
                    }

                    _hoaDonXuatTruoc = invoice;
                    if (!_isPrinted)
                    {
                        return(true);
                    }
                    OnPrint(invoice.HoaDonXuatTruocGUID.ToString());
                    return(true);
                }
                else
                {
                    MsgBox.Show(Application.ProductName, result.GetErrorAsString("HoaDonXuatTruocBus.InsertHoaDonXuatTruoc"), IconType.Error);
                    Utility.WriteToTraceLog(result.GetErrorAsString("HoaDonXuatTruocBus.InsertHoaDonXuatTruoc"));
                }
            }
            catch (Exception ex)
            {
                MsgBox.Show(Application.ProductName, ex.Message, IconType.Error);
                Utility.WriteToTraceLog(ex.Message);
            }

            return(false);
        }
Exemplo n.º 2
0
        public static Result GetSoHoaDon()
        {
            Result     result = new Result();
            MMOverride db     = null;

            try
            {
                while (true)
                {
                    string query = string.Format("SELECT Min(SoHoaDon) as SoHoaDon FROM QuanLySoHoaDon WITH(NOLOCK) WHERE XuatTruoc = 'False' AND DaXuat = 'False' AND NgayBatDau >= '{0}'",
                                                 Global.NgayThayDoiSoHoaDonSauCung.ToString("yyyy-MM-dd HH:mm:ss"));
                    result = ExcuteQuery(query);
                    if (!result.IsOK)
                    {
                        return(result);
                    }
                    DataTable dt = result.QueryResult as DataTable;
                    if (dt != null && dt.Rows.Count > 0 && dt.Rows[0][0] != null && dt.Rows[0][0] != DBNull.Value)
                    {
                        result.QueryResult = dt.Rows[0][0];
                    }
                    else
                    {
                        query = string.Format("SELECT MAX(SoHoaDon) as SoHoaDon FROM QuanLySoHoaDon WITH(NOLOCK) WHERE NgayBatDau >= '{0}'",
                                              Global.NgayThayDoiSoHoaDonSauCung.ToString("yyyy-MM-dd HH:mm:ss"));
                        result = ExcuteQuery(query);
                        if (!result.IsOK)
                        {
                            return(result);
                        }

                        dt = result.QueryResult as DataTable;
                        if (dt != null && dt.Rows.Count > 0 && dt.Rows[0][0] != null && dt.Rows[0][0] != DBNull.Value)
                        {
                            result.QueryResult = Convert.ToInt32(dt.Rows[0][0]) + 1;
                        }
                        else
                        {
                            result.QueryResult = Global.SoHoaDonBatDau;
                        }
                    }

                    db = new MMOverride();
                    int  soHoaDon = Convert.ToInt32(result.QueryResult);
                    bool isExist  = false;

                    //Hoa don dich vu
                    Invoice hdhd = db.Invoices.FirstOrDefault <Invoice>(h => Convert.ToInt32(h.InvoiceCode) == soHoaDon &&
                                                                        h.Status == (byte)Status.Actived && h.InvoiceDate >= Global.NgayThayDoiSoHoaDonSauCung);
                    if (hdhd != null)
                    {
                        QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.FirstOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon &&
                                                                                                  q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung);
                        if (qlshd == null)
                        {
                            qlshd = new QuanLySoHoaDon();
                            qlshd.QuanLySoHoaDonGUID = Guid.NewGuid();
                            qlshd.SoHoaDon           = soHoaDon;
                            qlshd.DaXuat             = true;
                            qlshd.XuatTruoc          = false;
                            qlshd.NgayBatDau         = Global.NgayThayDoiSoHoaDonSauCung;
                            db.QuanLySoHoaDons.InsertOnSubmit(qlshd);
                        }
                        else
                        {
                            qlshd.DaXuat = true;
                        }

                        db.SubmitChanges();
                        isExist = true;
                    }

                    //Hoa don thuoc
                    if (!isExist)
                    {
                        HoaDonThuoc hdt = db.HoaDonThuocs.FirstOrDefault <HoaDonThuoc>(h => Convert.ToInt32(h.SoHoaDon) == soHoaDon &&
                                                                                       h.Status == (byte)Status.Actived && h.NgayXuatHoaDon >= Global.NgayThayDoiSoHoaDonSauCung);
                        if (hdt != null)
                        {
                            QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.FirstOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon &&
                                                                                                      q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung);
                            if (qlshd == null)
                            {
                                qlshd = new QuanLySoHoaDon();
                                qlshd.QuanLySoHoaDonGUID = Guid.NewGuid();
                                qlshd.SoHoaDon           = soHoaDon;
                                qlshd.DaXuat             = true;
                                qlshd.XuatTruoc          = false;
                                qlshd.NgayBatDau         = Global.NgayThayDoiSoHoaDonSauCung;
                                db.QuanLySoHoaDons.InsertOnSubmit(qlshd);
                            }
                            else
                            {
                                qlshd.DaXuat = true;
                            }

                            db.SubmitChanges();
                            isExist = true;
                        }
                    }


                    //Hoa don xuat truoc
                    if (!isExist)
                    {
                        HoaDonXuatTruoc hdxt = db.HoaDonXuatTruocs.FirstOrDefault <HoaDonXuatTruoc>(h => Convert.ToInt32(h.SoHoaDon) == soHoaDon &&
                                                                                                    h.Status == (byte)Status.Actived && h.NgayXuatHoaDon >= Global.NgayThayDoiSoHoaDonSauCung);
                        if (hdxt != null)
                        {
                            QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.FirstOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon &&
                                                                                                      q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung);
                            if (qlshd == null)
                            {
                                qlshd = new QuanLySoHoaDon();
                                qlshd.QuanLySoHoaDonGUID = Guid.NewGuid();
                                qlshd.SoHoaDon           = soHoaDon;
                                qlshd.DaXuat             = true;
                                qlshd.XuatTruoc          = true;
                                qlshd.NgayBatDau         = Global.NgayThayDoiSoHoaDonSauCung;
                                db.QuanLySoHoaDons.InsertOnSubmit(qlshd);
                            }
                            else
                            {
                                qlshd.DaXuat = true;
                            }

                            db.SubmitChanges();
                            isExist = true;
                        }
                    }

                    //Hoa don hop dong
                    if (!isExist)
                    {
                        HoaDonHopDong hd = db.HoaDonHopDongs.FirstOrDefault <HoaDonHopDong>(h => Convert.ToInt32(h.SoHoaDon) == soHoaDon &&
                                                                                            h.Status == (byte)Status.Actived && h.NgayXuatHoaDon >= Global.NgayThayDoiSoHoaDonSauCung);
                        if (hd != null)
                        {
                            QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.FirstOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon &&
                                                                                                      q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung);
                            if (qlshd == null)
                            {
                                qlshd = new QuanLySoHoaDon();
                                qlshd.QuanLySoHoaDonGUID = Guid.NewGuid();
                                qlshd.SoHoaDon           = soHoaDon;
                                qlshd.DaXuat             = true;
                                qlshd.XuatTruoc          = false;
                                qlshd.NgayBatDau         = Global.NgayThayDoiSoHoaDonSauCung;
                                db.QuanLySoHoaDons.InsertOnSubmit(qlshd);
                            }
                            else
                            {
                                qlshd.DaXuat = true;
                            }

                            db.SubmitChanges();
                            isExist = true;
                        }
                    }

                    if (!isExist)
                    {
                        break;
                    }
                }
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                result.Error.Code        = (se.Message.IndexOf("Timeout expired") >= 0) ? ErrorCode.SQL_QUERY_TIMEOUT : ErrorCode.INVALID_SQL_STATEMENT;
                result.Error.Description = se.ToString();
            }
            catch (Exception e)
            {
                result.Error.Code        = ErrorCode.UNKNOWN_ERROR;
                result.Error.Description = e.ToString();
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                    db = null;
                }
            }

            return(result);
        }