Exemplo n.º 1
0
 // GET: phieunhaps/Details/5
 public ActionResult Details(string id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     phieunhap phieunhap = db.phieunhaps.Find(id);
     if (phieunhap == null)
     {
         return HttpNotFound();
     }
     phieunhapVM phieunhapVM = new phieunhapVM();
     phieunhapVM.phieunhap = phieunhap;
     var ctphieunhaps = db.ctphieunhaps.Include(p => p.phieunhap).Where(s=>s.maso == id);
     phieunhapVM.ctphieunhap = ctphieunhaps.ToList();
     phieunhapVM.nxb = db.nxbs.Find(phieunhapVM.phieunhap.manxb);
     return View(phieunhapVM);
 }
Exemplo n.º 2
0
        public ActionResult Phieunhap(phieunhapVM p)
        {
            try
            {
                if (string.IsNullOrEmpty(p.phieunhap.nguoigiao.Trim()))
                    ModelState.AddModelError("phieunhap.nguoigiao", "Người giao không được để trống");
                if (string.IsNullOrEmpty(p.nxb.manxb.Trim()))
                    ModelState.AddModelError("nxb.manxb", "Mã nhà xuất bản không được để trống");
                if (string.IsNullOrEmpty(p.nxb.tennxb.Trim()))
                    ModelState.AddModelError("nxb.tennxb", "Tên nhà xuất bản không được để trống");
                if (string.IsNullOrEmpty(p.nxb.sdt.Trim()))
                    ModelState.AddModelError("nxb.sdt", "Số điện thoại không được để trống");
                if (string.IsNullOrEmpty(p.nxb.dchi.Trim()))
                    ModelState.AddModelError("nxb.dchi", "Địa chỉ không được để trống");
                if ((p.ctphieunhap == null))
                    ModelState.AddModelError("ctphieunhap", "Không có dữ liệu để nhập");
                if (ModelState.IsValid)
                {
                    ///nxb
                    nxb _nxb = db.nxbs.FirstOrDefault<nxb>(nxb => nxb.manxb == p.nxb.manxb.Trim());
                    if (_nxb == null)
                    {
                        _nxb = new nxb { manxb = p.nxb.manxb.Trim(), tennxb = p.nxb.tennxb.Trim(), sdt = p.nxb.sdt.Trim(), dchi = p.nxb.dchi.Trim(), tonkho = p.nxb.tonkho };
                        db.nxbs.Add(_nxb);
                    }
                    else
                    {
                        _nxb.tennxb = p.nxb.tennxb.Trim();
                        _nxb.sdt = p.nxb.sdt.Trim();
                        _nxb.dchi = p.nxb.dchi.Trim();
                        _nxb.tonkho = p.nxb.tonkho + _nxb.tonkho ;
                        db.Entry(_nxb).State = System.Data.Entity.EntityState.Modified;
                    }
                    ////sach
                    //foreach (var i in p.sach)
                    //    {
                    //        sach _sach = db.saches.Find(i.masach);
                    //        if (_sach != null)
                    //        //{
                    //            //_sach = new sach { masach = i.masach, tensach = i.tensach, linhvuc = i.linhvuc, sluong = i.sluong };
                    //            //db.saches.Add(_sach);
                    //        //}
                    //        //else
                    //        {
                    //            _sach = new sach { masach = i.masach, tensach = i.tensach, linhvuc = i.linhvuc, sluong = i.sluong + _sach.sluong };
                    //            db.Entry(_sach).State = System.Data.Entity.EntityState.Modified;
                    //        }
                    //    }

                    ///phieunhap
                    phieunhap _phieunhap = new phieunhap { maso = p.phieunhap.maso.Trim(), manxb = p.phieunhap.manxb.Trim(), nguoigiao = p.phieunhap.nguoigiao.Trim(), tgian = p.phieunhap.tgian, tongtien = p.phieunhap.tongtien };
                    db.phieunhaps.Add(_phieunhap);
                        foreach (var j in p.ctphieunhap)
                        {
                        j.ton = j.soluong;
                            j.tienno = j.thanhtien;
                            db.ctphieunhaps.Add(j);
                            sach _sach = db.saches.FirstOrDefault<sach>(sach => sach.masach == j.masach.Trim());
                            _sach.sluong = j.soluong + _sach.sluong ;
                            db.Entry(_sach).State = System.Data.Entity.EntityState.Modified;
                        }
                    db.SaveChanges();
                    return RedirectToAction("Success");
                }
                ViewBag.maso = DateTime.Now.ToString("yyyMMddhhmm");
                ViewBag.tgian = DateTime.Now.ToShortDateString();
                ViewBag.linhvuc = new SelectList(db.linhvucs, "malv", "tenlv");
                ViewBag.masach = new SelectList(db.saches, "masach", "tensach");
                return View(p);
            }
            catch (Exception e)
            {
                return RedirectToAction("Error");
            }
        }