public ActionResult NhapSach(NHAXUATBAN nxb, FormCollection f) { PHIEUNHAP pn = new PHIEUNHAP(); pn.MaNXB = int.Parse(f["MaNXB"].ToString()); String[] temp = f["NgayNhap"].ToString().Split('-'); DateTime date = new DateTime(int.Parse(temp[2]), int.Parse(temp[1]), int.Parse(temp[0])); pn.NgayNhap = date; pn.TrangThai = false; db.PHIEUNHAPs.Add(pn); db.SaveChanges(); int?TongTien = 0; foreach (CT_PhieuNhapViewModel ct in Session["DS_Sach_Nhap"] as List <CT_PhieuNhapViewModel> ) { CT_PHIEUNHAP ctpx = new CT_PHIEUNHAP(); ctpx.MaPN = pn.MaPN; ctpx.MaSach = ct.MaSach; ctpx.SLNhap = ct.SLNhap; ctpx.DonGia = ct.DonGia; ctpx.ThanhTien = ctpx.SLNhap * ctpx.DonGia; TongTien += ctpx.ThanhTien; db.CT_PHIEUNHAP.Add(ctpx); } pn.TongTien = TongTien; db.SaveChanges(); CONGNO_NXB congno = new CONGNO_NXB(); congno.MaNXB = nxb.MaNXB; congno.ThoiGian = new DateTime(int.Parse(temp[2]), int.Parse(temp[1]), int.Parse(temp[0])); congno.TienDaTra = 0; congno.TienNo = pn.TongTien; db.CONGNO_NXB.Add(congno); db.SaveChanges(); Session.Clear(); return(RedirectToAction("Index")); }
public ActionResult CongNoNXB(FormCollection f) { String[] temp = f["datepicker"].ToString().Split('-'); DateTime date = new DateTime(int.Parse(temp[2]), int.Parse(temp[1]), int.Parse(temp[0])); List <CONGNO_NXB> lst_congno_nxb = new List <CONGNO_NXB>(); List <NHAXUATBAN> lst_nxb = new List <NHAXUATBAN>(); lst_nxb = db.NHAXUATBANs.ToList(); foreach (NHAXUATBAN n in lst_nxb) { CONGNO_NXB congno_nxb = new CONGNO_NXB(); congno_nxb = db.CONGNO_NXB.Where(x => x.ThoiGian <= date.Date && x.MaNXB == n.MaNXB).OrderByDescending(x => x.ThoiGian).FirstOrDefault(); if (congno_nxb != null) { congno_nxb.TienNo = db.CONGNO_NXB.Where(x => x.ThoiGian <= date.Date && x.MaNXB == n.MaNXB).Sum(x => x.TienNo - x.TienDaTra); lst_congno_nxb.Add(congno_nxb); } } ViewBag.NgayCongNo = date.ToString("dd/MM/yyyy"); return(View(lst_congno_nxb)); }
public ActionResult LuuCSDL(SachViewModel sachVM) { /** * Lưu Ngày nhập và NXB */ PHIEUNHAP pn = new PHIEUNHAP(); pn.NgayNhap = sachVM.NgayNhap; pn.MaNXB = sachVM.MaNXB; pn.TrangThai = true; var addedPN = entities.PHIEUNHAPs.Add(pn); entities.SaveChanges(); int tongTien = 0; /** * Lưu CT_Phiếu Nhập */ foreach (var ct in (List <SachViewModel>)Session["listSach"]) { int thanhTien = ct.GiaNhap * ct.SLNhap; tongTien += thanhTien; // Add ct phieu nhap CT_PHIEUNHAP ctpn = new CT_PHIEUNHAP(); ctpn.MaPN = pn.MaPN; ctpn.MaSach = ct.MaSach; ctpn.SLNhap = ct.SLNhap; ctpn.DonGia = ct.GiaNhap; ctpn.ThanhTien = thanhTien; entities.CT_PHIEUNHAP.Add(ctpn); } /** * Update tồn kho */ foreach (var ton in (List <SachViewModel>)Session["listSach"]) { TONKHO tonkho = new TONKHO(); tonkho.MaSach = ton.MaSach; tonkho.ThoiGian = sachVM.NgayNhap; tonkho.SLTon = ton.SLNhap; entities.TONKHOes.Add(tonkho); entities.SaveChanges(); } /** * Update tổng tiền */ addedPN.TongTien = tongTien; /** * Update công nợ NXB */ CONGNO_NXB cnNXB = new CONGNO_NXB(); cnNXB.MaNXB = sachVM.MaNXB; cnNXB.ThoiGian = sachVM.NgayNhap; cnNXB.TienNo = tongTien; cnNXB.TienDaTra = 0; entities.CONGNO_NXB.Add(cnNXB); entities.SaveChanges(); Session.Clear(); return(RedirectToAction("Index")); }