Пример #1
0
 //Thêm phiếu xuất
 public bool Insert(PhieuXuat inp)
 {
     try
     {
         Model.PhieuXuat newpx = new Model.PhieuXuat();
         newpx.MaPhieuXuat = inp.MaPhieuXuat;
         newpx.NgayXuat    = inp.NgayXuat;
         newpx.NhanVien    = inp.MaNhanVien;
         newpx.KhachHang   = inp.MaKhachHang;
         newpx.DiaChiNhan  = inp.DiaChiGiaoHang;
         newpx.HoaDon      = inp.MaHoaDon;
         newpx.TongTien    = inp.TongTien;
         db.PhieuXuats.Add(newpx);
         int kq = db.SaveChanges();
         if (kq > 0)
         {
             foreach (PhieuXuatCT ct in inp.DSChiTiet)
             {
                 //Thêm vào chi tiết phiếu xuất
                 Model.PhieuXuat_CT newct = new Model.PhieuXuat_CT();
                 newct.MaPhieuXuat     = ct.MaPhieuXuat;
                 newct.MaSanPham       = ct.MaSanPham;
                 newct.SoLuongYeuCau   = ct.SoLuongYeuCau;
                 newct.SoLuongThucXuat = ct.SoLuongThuc;
                 newct.DonGia          = ct.DonGia;
                 db.PhieuXuat_CT.Add(newct);
                 db.SaveChanges();
                 //Cộng vào số lượng tồn
                 IEnumerable <Model.SanPham> cnsp = from sp in db.SanPhams
                                                    where sp.MaSanPham == newct.MaSanPham
                                                    select sp;
                 Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                 sanpham.SoLuongTon = sanpham.SoLuongTon - ct.SoLuongThuc;
                 db.SaveChanges();
             }
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #2
0
 //Sửa phiếu xuất
 public bool Update(PhieuXuat inp)
 {
     try
     {
         IEnumerable <Model.PhieuXuat> dspx = from px in db.PhieuXuats
                                              where px.MaPhieuXuat == inp.MaPhieuXuat
                                              select px;
         Model.PhieuXuat phieuxuat = dspx.ElementAtOrDefault(0);
         if (phieuxuat != null)
         {
             phieuxuat.NhanVien   = inp.MaNhanVien;
             phieuxuat.KhachHang  = inp.MaKhachHang;
             phieuxuat.NgayXuat   = inp.NgayXuat;
             phieuxuat.DiaChiNhan = inp.DiaChiGiaoHang;
             phieuxuat.HoaDon     = inp.MaHoaDon;
             phieuxuat.TongTien   = inp.TongTien;
             db.SaveChanges();
             //Xoá chi tiết phiếu xuất cũ
             IEnumerable <Model.PhieuXuat_CT> dsctc = from ctc in db.PhieuXuat_CT
                                                      where ctc.MaPhieuXuat == inp.MaPhieuXuat
                                                      select ctc;
             if (dsctc.Count() > 0)
             {
                 foreach (Model.PhieuXuat_CT ctc in dsctc)
                 {
                     //cộng số lượng tồn
                     Model.QuanLiBanHangEntities db2  = new Model.QuanLiBanHangEntities();
                     IEnumerable <Model.SanPham> cnsp = from sp in db2.SanPhams
                                                        where sp.MaSanPham == ctc.MaSanPham
                                                        select sp;
                     Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                     sanpham.SoLuongTon = sanpham.SoLuongTon + ctc.SoLuongThucXuat;
                     db2.SaveChanges();
                     //xoá chi tiết phiếu
                     db.PhieuXuat_CT.Remove(ctc);
                 }
                 db.SaveChanges();
             }
             //Nhập lại chi tiết phiếu
             foreach (PhieuXuatCT ct in inp.DSChiTiet)
             {
                 Model.PhieuXuat_CT newct = new Model.PhieuXuat_CT();
                 newct.MaPhieuXuat     = ct.MaPhieuXuat;
                 newct.MaSanPham       = ct.MaSanPham;
                 newct.SoLuongYeuCau   = ct.SoLuongYeuCau;
                 newct.SoLuongThucXuat = ct.SoLuongThuc;
                 newct.DonGia          = ct.DonGia;
                 db.PhieuXuat_CT.Add(newct);
                 db.SaveChanges();
                 //Trừ vào số lượng tồn
                 IEnumerable <Model.SanPham> cnsp = from sp in db.SanPhams
                                                    where sp.MaSanPham == newct.MaSanPham
                                                    select sp;
                 Model.SanPham sanpham = cnsp.ElementAtOrDefault(0);
                 sanpham.SoLuongTon = sanpham.SoLuongTon - ct.SoLuongThuc;
                 db.SaveChanges();
             }
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }