private void OnSaveInfo() { try { MethodInvoker method = delegate { List <QuanLySoHoaDon> qlshdList = new List <QuanLySoHoaDon>(); DataTable dt = dgSoHoaDon.DataSource as DataTable; foreach (DataRow row in dt.Rows) { QuanLySoHoaDon qlshd = new QuanLySoHoaDon(); qlshd.SoHoaDon = Convert.ToInt32(row["SoHoaDon"]); qlshd.DaXuat = false; qlshd.XuatTruoc = true; qlshdList.Add(qlshd); } Result result = HoaDonXuatTruocBus.InsertQuanLySoHoaDon(qlshdList, _fromDate, _toDate); if (!result.IsOK) { MsgBox.Show(this.Text, result.GetErrorAsString("HoaDonXuatTruocBus.InsertInsertQuanLySoHoaDonService"), IconType.Error); Utility.WriteToTraceLog(result.GetErrorAsString("HoaDonXuatTruocBus.InsertQuanLySoHoaDon")); this.DialogResult = System.Windows.Forms.DialogResult.Cancel; } }; if (InvokeRequired) { BeginInvoke(method); } else { method.Invoke(); } } catch (Exception e) { MsgBox.Show(this.Text, e.Message, IconType.Error); Utility.WriteToTraceLog(e.Message); } }
public static Result CheckInvoiceExistCode(int soHoaDon) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); QuanLySoHoaDon qlshd = db.QuanLySoHoaDons.SingleOrDefault <QuanLySoHoaDon>(q => q.SoHoaDon == soHoaDon && (q.DaXuat == true || q.XuatTruoc == true) && q.NgayBatDau.Value >= Global.NgayThayDoiSoHoaDonSauCung); if (qlshd == null) { result.Error.Code = ErrorCode.NOT_EXIST; } else { result.Error.Code = ErrorCode.EXIST; } } 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 InsertInvoice(Invoice invoice, List <InvoiceDetail> addedDetails) { Result result = new Result(); MMOverride db = null; try { db = new MMOverride(); string desc = string.Empty; using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew)) { invoice.InvoiceGUID = Guid.NewGuid(); db.Invoices.InsertOnSubmit(invoice); db.SubmitChanges(); 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.InvoiceGUID.ToString(), invoice.InvoiceCode, invoice.InvoiceDate.ToString("dd/MM/yyyy HH:mm:ss"), invoice.TenNguoiMuaHang, invoice.TenDonVi, invoice.DiaChi, invoice.SoTaiKhoan, htttStr, invoice.Notes, !invoice.ChuaThuTien); if (addedDetails != null && addedDetails.Count > 0) { desc += "- Chi tiết hóa đơn:\n"; foreach (InvoiceDetail detail in addedDetails) { detail.InvoiceDetailGUID = Guid.NewGuid(); detail.InvoiceGUID = invoice.InvoiceGUID; db.InvoiceDetails.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}', Loại: '{6}'\n", detail.InvoiceDetailGUID.ToString(), detail.TenDichVu, detail.DonViTinh, detail.SoLuong, detail.DonGia, detail.ThanhTien, detail.Loai); } db.SubmitChanges(); } //Update Exported Invoice if (invoice.ReceiptGUIDList != null && invoice.ReceiptGUIDList.Trim() != string.Empty) { string[] keys = invoice.ReceiptGUIDList.Split(','); foreach (string key in keys) { Receipt receipt = db.Receipts.SingleOrDefault <Receipt>(r => r.ReceiptGUID.ToString() == key); if (receipt != null) { receipt.IsExportedInVoice = true; } } } int soHoaDon = Convert.ToInt32(invoice.InvoiceCode); 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 dịch vụ"; 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 DeleteInvoices(List <string> invoiceKeys, List <string> nodeList) { 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 invoiceKeys) { Invoice invoice = db.Invoices.SingleOrDefault <Invoice>(i => i.InvoiceGUID.ToString() == key); if (invoice != null) { invoice.DeletedDate = DateTime.Now; invoice.DeletedBy = Guid.Parse(Global.UserGUID); invoice.Status = (byte)Status.Deactived; if (invoice.Notes == null || invoice.Notes.Trim() == string.Empty) { invoice.Notes = nodeList[index]; } else { invoice.Notes += string.Format(" - {0}", nodeList[index]); } //Update Exported Invoice if (invoice.ReceiptGUIDList != null && invoice.ReceiptGUIDList.Trim() != string.Empty) { string[] keys = invoice.ReceiptGUIDList.Split(','); foreach (string receiptGUID in keys) { Receipt receipt = db.Receipts.SingleOrDefault <Receipt>(r => r.ReceiptGUID.ToString() == receiptGUID); if (receipt != null) { receipt.IsExportedInVoice = false; } } } int soHoaDon = Convert.ToInt32(invoice.InvoiceCode); DateTime fromDate = Global.MinDateTime; DateTime toDate = Global.MaxDateTime; var ngayThayDoi = (from n in db.NgayBatDauLamMoiSoHoaDons where n.KiHieu.ToLower() == invoice.KiHieu && n.MauSo.ToLower() == invoice.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; } } string htttStr = Utility.ParseHinhThucThanhToanToStr((PaymentType)invoice.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", invoice.InvoiceGUID.ToString(), invoice.InvoiceCode, invoice.InvoiceDate.ToString("dd/MM/yyyy HH:mm:ss"), invoice.TenNguoiMuaHang, invoice.TenDonVi, invoice.DiaChi, invoice.SoTaiKhoan, htttStr, nodeList[index], !invoice.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 dịch vụ"; 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); }