public static Result GetNgayXuatHoaDon(string soHoaDon) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); HoaDonHopDong hdt = (from i in db.HoaDonHopDongs where i.SoHoaDon == soHoaDon && i.Status == (byte)Status.Deactived && i.NgayXuatHoaDon >= Global.NgayThayDoiSoHoaDonSauCung orderby i.NgayXuatHoaDon descending select i).FirstOrDefault(); if (hdt != null) { result.QueryResult = hdt.NgayXuatHoaDon.Value; } } 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); }
public static Result UpdateDaThuTienInvoice(string hoaDonHopDongGUID, bool daThuTien) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); string desc = string.Empty; using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew)) { HoaDonHopDong invoice = db.HoaDonHopDongs.SingleOrDefault <HoaDonHopDong>(i => i.HoaDonHopDongGUID.ToString() == hoaDonHopDongGUID); if (invoice != null) { invoice.UpdatedDate = DateTime.Now; invoice.UpdatedBy = Guid.Parse(Global.UserGUID); invoice.ChuaThuTien = !daThuTien; string htttStr = Utility.ParseHinhThucThanhToanToStr((PaymentType)invoice.HinhThucThanhToan); desc += string.Format("- Hóa đơn dịch vụ: GUID: '{0}', Mã hóa đơn: '{1}', Ngày xuất HĐ: '{2}', Người mua hàng: '{3}', Tên đơn vị: '{4}', Địa chỉ: '{5}', STK: '{6}', Hình thức thanh toán: '{7}', Ghi chú: '{8}', Đã thu tiền: '{9}'\n", invoice.HoaDonHopDongGUID.ToString(), invoice.SoHoaDon, invoice.NgayXuatHoaDon.Value.ToString("dd/MM/yyyy HH:mm:ss"), invoice.TenNguoiMuaHang, invoice.TenDonVi, invoice.DiaChi, invoice.SoTaiKhoan, htttStr, invoice.Notes, !invoice.ChuaThuTien); //Tracking desc = desc.Substring(0, desc.Length - 1); Tracking tk = new Tracking(); tk.TrackingGUID = Guid.NewGuid(); tk.TrackingDate = DateTime.Now; tk.DocStaffGUID = Guid.Parse(Global.UserGUID); tk.ActionType = (byte)ActionType.Add; tk.Action = "Cập nhật thông tin hóa đơn hợp đồng"; tk.Description = desc; tk.TrackingType = (byte)TrackingType.None; tk.ComputerName = Utility.GetDNSHostName(); db.Trackings.InsertOnSubmit(tk); db.SubmitChanges(); } t.Complete(); } } 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); }
public static Result InsertHoaDonHopDong(HoaDonHopDong hdt, List <ChiTietHoaDonHopDong> addedDetails) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); string desc = string.Empty; using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew)) { hdt.HoaDonHopDongGUID = Guid.NewGuid(); db.HoaDonHopDongs.InsertOnSubmit(hdt); db.SubmitChanges(); string htttStr = Utility.ParseHinhThucThanhToanToStr((PaymentType)hdt.HinhThucThanhToan); desc += string.Format("- Hóa đơn hợp đồng: GUID: '{0}', Mã hóa đơn: '{1}', Ngày xuất HĐ: '{2}', Người mua hàng: '{3}', Tên đơn vị: '{4}', Địa chỉ: '{5}', STK: '{6}', Hình thức thanh toán: '{7}', Ghi chú: '{8}', Đã thu tiền: '{9}'\n", hdt.HoaDonHopDongGUID.ToString(), hdt.SoHoaDon, hdt.NgayXuatHoaDon.Value.ToString("dd/MM/yyyy HH:mm:ss"), hdt.TenNguoiMuaHang, hdt.TenDonVi, hdt.DiaChi, hdt.SoTaiKhoan, htttStr, hdt.Notes, !hdt.ChuaThuTien); if (addedDetails != null && addedDetails.Count > 0) { desc += "- Chi tiết hóa đơn:\n"; foreach (ChiTietHoaDonHopDong detail in addedDetails) { detail.ChiTietHoaDonHopDongGUID = Guid.NewGuid(); detail.HoaDonHopDongGUID = hdt.HoaDonHopDongGUID; db.ChiTietHoaDonHopDongs.InsertOnSubmit(detail); desc += string.Format(" + GUID: '{0}', Dịch vụ: '{1}', ĐVT: '{2}', Số lượng: '{3}', Đơn giá: '{4}', Thành tiền: '{5}'\n", detail.ChiTietHoaDonHopDongGUID.ToString(), detail.TenMatHang, detail.DonViTinh, detail.SoLuong, detail.DonGia, detail.ThanhTien); } db.SubmitChanges(); } //Update Exported Invoice if (hdt.PhieuThuHopDongGUIDList != null && hdt.PhieuThuHopDongGUIDList.Trim() != string.Empty) { string[] pttkeys = hdt.PhieuThuHopDongGUIDList.Split(','); foreach (string pttKey in pttkeys) { PhieuThuHopDong ptt = db.PhieuThuHopDongs.SingleOrDefault <PhieuThuHopDong>(r => r.PhieuThuHopDongGUID.ToString() == pttKey); if (ptt != null) { ptt.IsExported = true; } } } int soHoaDon = Convert.ToInt32(hdt.SoHoaDon); QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.SingleOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon && q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung); if (qlshd != null) { qlshd.DaXuat = true; } else { qlshd = new QuanLySoHoaDon(); qlshd.QuanLySoHoaDonGUID = Guid.NewGuid(); qlshd.SoHoaDon = soHoaDon; qlshd.DaXuat = true; qlshd.XuatTruoc = false; qlshd.NgayBatDau = Global.NgayThayDoiSoHoaDonSauCung; db.QuanLySoHoaDons.InsertOnSubmit(qlshd); } //Tracking desc = desc.Substring(0, desc.Length - 1); Tracking tk = new Tracking(); tk.TrackingGUID = Guid.NewGuid(); tk.TrackingDate = DateTime.Now; tk.DocStaffGUID = Guid.Parse(Global.UserGUID); tk.ActionType = (byte)ActionType.Add; tk.Action = "Thêm thông tin hóa đơn hợp đồng"; tk.Description = desc; tk.TrackingType = (byte)TrackingType.Price; tk.ComputerName = Utility.GetDNSHostName(); db.Trackings.InsertOnSubmit(tk); db.SubmitChanges(); t.Complete(); } } 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); }
public static Result DeleteHoaDonHopDong(List <string> keys, List <string> noteList) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew)) { string desc = string.Empty; int index = 0; foreach (string key in keys) { HoaDonHopDong hdt = db.HoaDonHopDongs.SingleOrDefault <HoaDonHopDong>(i => i.HoaDonHopDongGUID.ToString() == key); if (hdt != null) { hdt.DeletedDate = DateTime.Now; hdt.DeletedBy = Guid.Parse(Global.UserGUID); hdt.Status = (byte)Status.Deactived; if (hdt.Notes == null || hdt.Notes.Trim() == string.Empty) { hdt.Notes = noteList[index]; } else { hdt.Notes += string.Format(" - {0}", noteList[index]); } //Update Exported Invoice if (hdt.PhieuThuHopDongGUIDList != null && hdt.PhieuThuHopDongGUIDList.Trim() != string.Empty) { string[] pttkeys = hdt.PhieuThuHopDongGUIDList.Split(','); foreach (string pttKey in pttkeys) { PhieuThuHopDong ptt = db.PhieuThuHopDongs.SingleOrDefault <PhieuThuHopDong>(r => r.PhieuThuHopDongGUID.ToString() == pttKey); if (ptt != null) { ptt.IsExported = false; } } } int soHoaDon = Convert.ToInt32(hdt.SoHoaDon); DateTime fromDate = Global.MinDateTime; DateTime toDate = Global.MaxDateTime; var ngayThayDoi = (from n in db.NgayBatDauLamMoiSoHoaDons where n.KiHieu.ToLower() == hdt.KiHieu && n.MauSo.ToLower() == hdt.MauSo.ToLower() select n).FirstOrDefault(); if (ngayThayDoi != null) { fromDate = ngayThayDoi.NgayBatDau; var nextThayDoi = (from n in db.NgayBatDauLamMoiSoHoaDons where n.NgayBatDau > ngayThayDoi.NgayBatDau orderby n.NgayBatDau ascending select n).FirstOrDefault(); if (nextThayDoi != null) { toDate = nextThayDoi.NgayBatDau; } QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.SingleOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon && q.NgayBatDau.Value >= fromDate && q.NgayBatDau < toDate); if (qlshd != null) { qlshd.DaXuat = false; } else { qlshd = new QuanLySoHoaDon(); qlshd.QuanLySoHoaDonGUID = Guid.NewGuid(); qlshd.SoHoaDon = soHoaDon; qlshd.DaXuat = false; qlshd.XuatTruoc = false; qlshd.NgayBatDau = fromDate; db.QuanLySoHoaDons.InsertOnSubmit(qlshd); } } string htttStr = Utility.ParseHinhThucThanhToanToStr((PaymentType)hdt.HinhThucThanhToan); desc += string.Format("- GUID: '{0}', Mã hóa đơn: '{1}', Ngày xuất HĐ: '{2}', Người mua hàng: '{3}', Tên đơn vị: '{4}', Địa chỉ: '{5}', STK: '{6}', Hình thức thanh toán: '{7}', Ghi chú: '{8}', Đã thu tiền: '{9}'\n", hdt.HoaDonHopDongGUID.ToString(), hdt.SoHoaDon, hdt.NgayXuatHoaDon.Value.ToString("dd/MM/yyyy HH:mm:ss"), hdt.TenNguoiMuaHang, hdt.TenDonVi, hdt.DiaChi, hdt.SoTaiKhoan, htttStr, noteList[index], !hdt.ChuaThuTien); } index++; } //Tracking desc = desc.Substring(0, desc.Length - 1); Tracking tk = new Tracking(); tk.TrackingGUID = Guid.NewGuid(); tk.TrackingDate = DateTime.Now; tk.DocStaffGUID = Guid.Parse(Global.UserGUID); tk.ActionType = (byte)ActionType.Delete; tk.Action = "Xóa thông tin hóa đơn hợp đồng"; tk.Description = desc; tk.TrackingType = (byte)TrackingType.Price; tk.ComputerName = Utility.GetDNSHostName(); db.Trackings.InsertOnSubmit(tk); db.SubmitChanges(); t.Complete(); } } 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); }
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); }