protected void gridDonHang_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { using (var scope = new TransactionScope()) { try { if (e.NewValues["TrangThai"] == "0") { return; } int IDPhieuGiaoHang = int.Parse(e.Keys["IDPhieuGiaoHang"].ToString()); int IDKhachHang = int.Parse(gridDonHang.GetRowValuesByKeyValue(e.Keys["IDPhieuGiaoHang"], "KhachHangID").ToString()); int pheduyet = Int32.Parse(e.NewValues["TrangThai"].ToString()); var KH = DBDataProvider.DB.khKhachHangs.Where(x => x.IDKhachHang == IDKhachHang).FirstOrDefault(); var PhieuGH = DBDataProvider.DB.ghPhieuGiaoHangs.Where(x => x.IDPhieuGiaoHang == IDPhieuGiaoHang).FirstOrDefault(); var PhieuGiaoHangCT = DBDataProvider.DB.ghPhieuGiaoHangChiTiets.Where(x => x.PhieuGiaoHangID == IDPhieuGiaoHang).ToList(); if (pheduyet == 1) { // duyệt đơn hàng if (KH != null && PhieuGH != null) { PhieuGH.CongNoHienTai = KH.CongNo; #region nhật ký công nợ khNhatKyCongNo nhatky = new khNhatKyCongNo(); nhatky.NgayNhap = DateTime.Now; nhatky.DienGiai = "Duyệt giao hàng đại lý"; nhatky.NoDau = KH.CongNo; nhatky.NhapHang = PhieuGH.ConLai; nhatky.TraHang = 0; nhatky.NoCuoi = KH.CongNo + PhieuGH.ConLai; nhatky.ThanhToan = 0; nhatky.NhanVienID = Formats.IDUser(); nhatky.SoPhieu = PhieuGH.MaPhieu; nhatky.IDKhachHang = IDKhachHang; DBDataProvider.DB.khNhatKyCongNos.InsertOnSubmit(nhatky); DBDataProvider.DB.SubmitChanges(); #endregion foreach (var prod in PhieuGiaoHangCT) { var HH = DBDataProvider.DB.hhHangHoas.Where(x => x.IDHangHoa == prod.HangHoaID).FirstOrDefault(); //ghi thẻ kho #region thẻ kho kTheKho thekho = new kTheKho(); thekho.NgayNhap = DateTime.Now; thekho.DienGiai = "Duyệt giao hàng #" + PhieuGH.MaPhieu; thekho.Nhap = 0; thekho.Xuat = prod.SoLuong; thekho.Ton = HH.TonKho - prod.SoLuong; thekho.HangHoaID = HH.IDHangHoa; thekho.NhanVienID = Formats.IDUser(); DBDataProvider.DB.kTheKhos.InsertOnSubmit(thekho); #endregion } KH.CongNo += PhieuGH.ConLai; // cộng công nợ KH.TongTienHang += PhieuGH.ConLai; KH.LanCuoiMuaHang = DateTime.Now; PhieuGH.TrangThai = 1;// duyệt thành công PhieuGH.SoDonHangTrongNam = DBDataProvider.SoDonHangTrongNam_GiaoHang(); PhieuGH.STTDonHang = DBDataProvider.STTPhieuGiaoHang_DaiLy(IDKhachHang); } } else { // hủy đơn hàng if (KH != null && PhieuGH != null) { // cộng tồn kho foreach (var prod in PhieuGiaoHangCT) { var HH = DBDataProvider.DB.hhHangHoas.Where(x => x.IDHangHoa == prod.HangHoaID).FirstOrDefault(); HH.TonKho += prod.SoLuong; } PhieuGH.TrangThai = 2;// hủy đơn hàng PhieuGH.STTDonHang = 0; PhieuGH.SoDonHangTrongNam = 0; PhieuGH.CongNoHienTai = KH.CongNo; } } PhieuGH.NgayDuyet = DateTime.Now; DBDataProvider.DB.SubmitChanges(); scope.Complete(); gridDonHang.CancelEdit(); e.Cancel = true; //cbpInfoImport.JSProperties["cp_Reset"] = true; } catch (Exception ex) { throw ex; } } }
protected void Save() { using (var scope = new TransactionScope()) { try { if (listReceiptProducts.Count > 0) { double TongTien = 0; int TongSoLuong = 0; foreach (var prod in listReceiptProducts) { TongTien += prod.ThanhTien; TongSoLuong += prod.SoLuong; } string MaPhieu = null, strMaPhieu = "PX"; string MAX = (DBDataProvider.DB.ghPhieuGiaoHangs.Count() + 1).ToString(); for (int i = 1; i < (7 - MAX.Length); i++) { strMaPhieu += "0"; } MaPhieu = strMaPhieu + MAX; int IDkhachHang = Int32.Parse(ccbNhaCungCap.Value.ToString()); var KH = DBDataProvider.DB.khKhachHangs.Where(x => x.IDKhachHang == IDkhachHang).FirstOrDefault(); ghPhieuGiaoHang giaohang = new ghPhieuGiaoHang(); giaohang.NgayTao = DateTime.Now; giaohang.MaPhieu = MaPhieu; giaohang.NhanVienID = Formats.IDUser(); giaohang.GhiChuGiaoHang = memoGhiChu.Text; giaohang.KhachHangID = IDkhachHang; giaohang.NgayGiao = Formats.ConvertToDateTime(dateNgayNhap.Text); giaohang.NguoiGiao = ""; giaohang.DiaChiGiaoHang = KH.DiaChi; giaohang.DaXoa = 0; giaohang.TTThanhToan = 0; //0 chưa thanh toán. 1 đã thanh toán giaohang.TrangThai = 3; // chưa duyệt, 1 đã duyệt, 3 bán hàng giaohang.TongSoLuong = TongSoLuong; giaohang.DienThoai = KH.DienThoai; giaohang.TongTien = TongTien; giaohang.NgayDuyet = DateTime.Parse(dateNgayNhap.Text); giaohang.GiamGia = Convert.ToDouble(spGiamGia.Number); giaohang.ThanhToan = Convert.ToDouble(spKhachHangThoan.Number); // khách thanh toán giaohang.ConLai = TongTien - (Convert.ToDouble(spGiamGia.Number) + Convert.ToDouble(spKhachHangThoan.Number)); // (-1)*Convert.ToDouble(spTienTraKhach.Number); giaohang.STTDonHang = DBDataProvider.STTPhieuGiaoHang_DaiLy(IDkhachHang); giaohang.SoDonHangTrongNam = DBDataProvider.SoDonHangTrongNam_GiaoHang(); giaohang.CongNoHienTai = KH.CongNo; DBDataProvider.DB.ghPhieuGiaoHangs.InsertOnSubmit(giaohang); DBDataProvider.DB.SubmitChanges(); int IDPhieuGiaoHang = giaohang.IDPhieuGiaoHang; hiddenFields["IDPhieuMoi"] = IDPhieuGiaoHang; foreach (var prod in listReceiptProducts) { // insert phiếu giao hàng chi tiết ghPhieuGiaoHangChiTiet chitiet = new ghPhieuGiaoHangChiTiet(); chitiet.PhieuGiaoHangID = IDPhieuGiaoHang; chitiet.HangHoaID = prod.IDHangHoa; chitiet.TonKho = prod.TonKho; chitiet.SoLuong = prod.SoLuong; chitiet.GiaBan = prod.GiaBan; chitiet.GiaVon = prod.GiaVon; chitiet.ThanhTien = prod.ThanhTien; DBDataProvider.DB.ghPhieuGiaoHangChiTiets.InsertOnSubmit(chitiet); // trừ tạm tồn kho. var TonKhoBanDau = DBDataProvider.DB.hhHangHoas.Where(x => x.IDHangHoa == prod.IDHangHoa).FirstOrDefault(); if (TonKhoBanDau != null) { TonKhoBanDau.TonKho -= prod.SoLuong; var HH = DBDataProvider.DB.hhHangHoas.Where(x => x.IDHangHoa == prod.IDHangHoa).FirstOrDefault(); //ghi thẻ kho #region thẻ kho kTheKho thekho = new kTheKho(); thekho.NgayNhap = DateTime.Now; thekho.DienGiai = "Bán hàng #" + giaohang.MaPhieu; thekho.Nhap = 0; thekho.Xuat = prod.SoLuong; thekho.Ton = HH.TonKho; thekho.GiaThoiDiem = prod.GiaBan; thekho.HangHoaID = HH.IDHangHoa; thekho.NhanVienID = Formats.IDUser(); DBDataProvider.DB.kTheKhos.InsertOnSubmit(thekho); #endregion } } if (Convert.ToDouble(spTienTraKhach.Number) < 0) { // cập nhật công nợ #region nhật ký công nợ khNhatKyCongNo nhatky = new khNhatKyCongNo(); nhatky.NgayNhap = DateTime.Now; nhatky.DienGiai = "Bán hàng"; nhatky.NoDau = KH.CongNo; nhatky.NhapHang = TongTien; nhatky.TraHang = 0; nhatky.GiamGia = Convert.ToDouble(spGiamGia.Number); nhatky.NoCuoi = KH.CongNo += (-1) * Convert.ToDouble(spTienTraKhach.Number); nhatky.ThanhToan = Convert.ToDouble(spKhachHangThoan.Number); nhatky.MaPhieu = DBDataProvider.STTPhieuGiaoHang_DaiLy(IDkhachHang); nhatky.NhanVienID = Formats.IDUser(); nhatky.SoPhieu = giaohang.MaPhieu; nhatky.IDKhachHang = IDkhachHang; DBDataProvider.DB.khNhatKyCongNos.InsertOnSubmit(nhatky); DBDataProvider.DB.SubmitChanges(); #endregion // KH.CongNo += (-1) * Convert.ToDouble(spTienTraKhach.Number); // cộng công nợ } else { #region nhật ký công nợ khNhatKyCongNo nhatky = new khNhatKyCongNo(); nhatky.NgayNhap = DateTime.Now; nhatky.DienGiai = "Bán hàng"; nhatky.NoDau = KH.CongNo; nhatky.NhapHang = TongTien; nhatky.GiamGia = Convert.ToDouble(spGiamGia.Number); nhatky.TraHang = 0; nhatky.NoCuoi = KH.CongNo; nhatky.ThanhToan = Convert.ToDouble(spKhachHangThoan.Number); nhatky.MaPhieu = DBDataProvider.STTPhieuGiaoHang_DaiLy(IDkhachHang); nhatky.NhanVienID = Formats.IDUser(); nhatky.SoPhieu = giaohang.MaPhieu; nhatky.IDKhachHang = IDkhachHang; DBDataProvider.DB.khNhatKyCongNos.InsertOnSubmit(nhatky); DBDataProvider.DB.SubmitChanges(); #endregion } KH.TongTienHang += TongTien; KH.LanCuoiMuaHang = DateTime.Now; DBDataProvider.DB.SubmitChanges(); scope.Complete(); Reset(); cbpInfoImport.JSProperties["cp_Reset"] = true; } else { throw new Exception("Danh sách hàng hóa trống !!"); ccbBarcode.Text = ""; ccbBarcode.Value = ""; ccbBarcode.Focus(); } } catch (Exception ex) { throw ex; } } }