示例#1
0
        public ActionResult ThemChiTietPhieuXuat()
        {
            var         lstb   = db.Books;
            List <Book> lstbtk = new List <Book>();

            foreach (var item in lstb)
            {
                Inventory_Book tonkhosach = db.Inventory_Book.OrderByDescending(m => m.id).FirstOrDefault(m => m.Book_id == item.Book_id);
                if (tonkhosach != null)
                {
                    if (tonkhosach.Quantity > 0)
                    {
                        Book bk = db.Books.FirstOrDefault(m => m.Book_id == tonkhosach.Book_id);
                        lstbtk.Add(bk);
                    }
                }
            }

            ViewBag.sach = new SelectList(lstbtk, "Book_id", "Book_name");
            //var phieuxuat = (Bill_Export)TempData["PhieuXuat"];
            return(View());
        }
示例#2
0
        public ActionResult ThemChiTietPhieuXuat(FormCollection chitiet)
        {
            ViewBag.sach = new SelectList(db.Books, "Book_id", "Book_name");
            ViewBag.loi  = null;
            if (Request.Form["add"] != null)
            {
                if (ModelState.IsValid)
                {
                    bool check = true;
                    foreach (var ctpx in (List <Detail_Bill_Export>)Session["ctphieuxuat"])
                    {
                        if (ctpx.Book_id == Int32.Parse(chitiet["sach"].ToString()))
                        {
                            check       = false;
                            ViewBag.loi = "Sách đã được thêm vào phiếu trước đó";
                            break;
                        }
                    }
                    if (check)
                    {
                        //kiểm tra trong kho có sách đó hay không
                        if (chitiet["sach"] == null)
                        {
                            ViewBag.loi = "Không tồn tại sách để xuất";
                            goto baoloi;
                        }
                        //khai báo để tìm nợ của đại lý đó
                        Bill_Export test1 = (Bill_Export)Session["PhieuXuat"];
                        Debt_Agency test2 = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => m.Agency_id == (int)test1.Agency_id);
                        //lấy số lượng nhâp từ form và lấy tồn kho của sách vừa nhập
                        var            sach    = db.Books.Find(Int32.Parse(chitiet["sach"].ToString()));
                        int            soluong = Int32.Parse(chitiet["soluong"].ToString());
                        Inventory_Book tonkho1 = db.Inventory_Book.OrderByDescending(m => m.id).FirstOrDefault(m => m.Book_id == (int)sach.Book_id);

                        //kiểm tra mã sách
                        if (sach == null)
                        {
                            ViewBag.loi = "Mã sách không tồn tại";
                            goto baoloi;
                        }
                        else
                        {
                            //kiểm tra tồn kho
                            if (tonkho1 == null)
                            {
                                ViewBag.loi = "Không có tồn kho";
                                goto baoloi;
                            }
                            else
                            {
                                //nếu tòn kho <= 0
                                if (tonkho1.Quantity <= 0)
                                {
                                    ViewBag.loi = "Sách đã hết trong kho";
                                    goto baoloi;
                                }
                                else
                                {
                                    //kiểm tra điều kiện vượt quá số lượng trong kho
                                    if (tonkho1.Quantity < soluong)
                                    {
                                        ViewBag.loi = "Số lượng sách không đủ để xuất";
                                        goto baoloi;
                                    }
                                    else
                                    {
                                        //kiểm tra số lượng nhập vào phải lớn 0
                                        if (soluong > 0)
                                        {
                                            Detail_Bill_Export ctpx = new Detail_Bill_Export();
                                            ctpx.Bill_Export_id = (db.Bill_Export.Max(u => (int?)u.Bill_Export_id) != null ? db.Bill_Export.Max(u => u.Bill_Export_id) : 0) + 1;// db.Bill_Export.Count() + 1;
                                            ctpx.Book_id        = Int32.Parse(chitiet["sach"].ToString());
                                            ctpx.Quantity       = Int32.Parse(chitiet["soluong"].ToString());
                                            ctpx.Cost           = double.Parse(db.Books.Find(ctpx.Book_id).Cost_Export.ToString());
                                            ctpx.Total          = ctpx.Quantity * ctpx.Cost;
                                            //kiểm tra tổng tiền của phiếu có lớn hơn số nợ hay không
                                            double checktien = 0;
                                            foreach (var checktest in (List <Detail_Bill_Export>)Session["ctphieuxuat"])
                                            {
                                                checktien += checktest.Total;
                                            }
                                            checktien += ctpx.Total;
                                            if (test2 == null || (test2 != null && test2.debt > 0 && test2.debt >= checktien) || test2.debt == 0)
                                            {
                                                ((List <Detail_Bill_Export>)Session["ctphieuxuat"]).Add(ctpx);
                                            }
                                            else
                                            {
                                                ViewBag.loi = "Vượt quá số nợ cho phép, mức nợ hiện tại là: " + test2.debt;
                                                goto baoloi;
                                            }
                                        }
                                        else
                                        {
                                            ViewBag.loi = "Nhập số lượng lớn hơn 0";
                                            goto baoloi;
                                        }
                                    }
                                }
                            }
                        }
                        return(RedirectToAction("ThemChiTietPhieuXuat"));
                    }
                    else
                    {
                    }
                }
            }
            else if (Request.Form["create"] != null)
            {
                if (ModelState.IsValid)
                {
                    if (((List <Detail_Bill_Export>)Session["ctphieuxuat"]).Count == 0)
                    {
                        ViewBag.loi = "Không được để phiếu trống";
                        goto baoloi;
                    }
                    double      tongTien     = 0;
                    double?     temptongtien = 0;
                    Bill_Export test         = new Bill_Export();
                    test = (Bill_Export)Session["PhieuXuat"];
                    var luu = db.Bill_Export.Add(test);
                    db.SaveChanges();

                    /*foreach (var ctpx in (List<Detail_Bill_Export>)Session["ctphieuxuat"])
                     * {
                     *  temptongtien += (ctpx.Cost * ctpx.Quantity);
                     *  tongTien = double.Parse(temptongtien.ToString());
                     *  //tongSoLuong += ctpx.SoLuongXuat;
                     * }
                     * Debt_Agency debt1 = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => m.Agency_id == (int)luu.Agency_id);
                     * if (debt1 == null || (debt1 != null && debt1.debt >= 0 && debt1.debt >= tongTien))
                     * {*/
                    foreach (var ctpx in (List <Detail_Bill_Export>)Session["ctphieuxuat"])
                    {
                        temptongtien += (ctpx.Cost * ctpx.Quantity);
                        tongTien      = double.Parse(temptongtien.ToString());
                        //tongSoLuong += ctpx.SoLuongXuat;

                        //lưu chi tiết phiếu xuất
                        Detail_Bill_Export ctPhieuXuat = new Detail_Bill_Export();
                        ctPhieuXuat = ctpx;
                        db.Detail_Bill_Export.Add(ctPhieuXuat);
                        //db.SaveChanges();

                        //cập nhật tồn kho công ty
                        Inventory_Book tonkho = new Inventory_Book();
                        tonkho.UpdatedDate = DateTime.Now;
                        Inventory_Book tonkho1 = db.Inventory_Book.OrderByDescending(m => m.id).FirstOrDefault(m => m.Book_id == (int)ctpx.Book_id);
                        //if (tonkho1 != null)
                        //{
                        tonkho.Book_id  = ctpx.Book_id;
                        tonkho.Quantity = tonkho1.Quantity - ctpx.Quantity;
                        //}
                        //Tạm thời để đó

                        /* else
                         * {
                         *   tonkho.Book_id = ctpx.Book_id;
                         *   tonkho.Quantity = ctpx.Quantity;
                         * }*/
                        db.Inventory_Book.Add(tonkho);


                        //cập nhật tồn kho đại lý
                        Inventory_Agency tonkhodaily  = new Inventory_Agency();
                        Inventory_Agency tonkhodaily1 = db.Inventory_Agency.OrderByDescending(n => n.id).FirstOrDefault(n => (n.Agency_id == (int)luu.Agency_id && n.Book_id == (int)ctpx.Book_id));
                        //tonkhodaily.repay_quantity = tonkhodaily1.repay_quantity;
                        tonkhodaily.UpdatedDate = DateTime.Now;
                        if (tonkhodaily1 != null)
                        {
                            tonkhodaily.Book_id          = ctpx.Book_id;
                            tonkhodaily.Agency_id        = luu.Agency_id;
                            tonkhodaily.deliver_quantity = tonkhodaily1.deliver_quantity + ctpx.Quantity;
                            tonkhodaily.repay_quantity   = tonkhodaily1.repay_quantity;
                        }
                        else
                        {
                            tonkhodaily.Book_id          = ctpx.Book_id;
                            tonkhodaily.Agency_id        = luu.Agency_id;
                            tonkhodaily.deliver_quantity = ctpx.Quantity;
                            tonkhodaily.repay_quantity   = 0;
                        }
                        db.Inventory_Agency.Add(tonkhodaily);
                    }
                    //lưu phiếu xuất
                    //addedPhieuXuat.TongSoLuong = tongSoLuong;

                    Debt_Agency debt1 = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => m.Agency_id == (int)luu.Agency_id);
                    if (debt1 == null || (debt1 != null && debt1.debt > 0 && debt1.debt >= tongTien) || debt1.debt == 0)
                    {
                        luu.Total = tongTien;
                        db.Bill_Export.Attach(luu);
                        db.Entry(luu).State = EntityState.Modified;
                        db.SaveChanges();
                        //cập nhật công nợ Đại Lý
                        Debt_Agency debt = new Debt_Agency();

                        //Tạm thời để đó
                        //Debt_Agency debt1 = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => m.Agency_id == (int)luu.Agency_id);
                        debt.update_date = DateTime.Now;
                        if (debt1 != null)
                        {
                            debt.Agency_id = luu.Agency_id;
                            debt.debt      = debt1.debt + luu.Total;
                        }
                        else
                        {
                            debt.Agency_id = luu.Agency_id;
                            debt.debt      = luu.Total;
                        }
                        db.Debt_Agency.Add(debt);

                        db.SaveChanges();

                        Session["ctphieuxuat"] = null;
                        Session["PhieuXuat"]   = null;
                        return(RedirectToAction("Create"));
                    }
                    else
                    {
                        ViewBag.loi = "Vượt quá số nợ cho phép, hãy bấm hủy phiếu để tạo lại!";
                        goto baoloi;
                        //return RedirectToAction("ThemChiTietPhieuXuat");
                    }
                }
            }
baoloi:
            return(View());
        }
        public ActionResult ThemChiTietPhieuNhap(FormCollection chitiet)
        {
            //test
            Bill_Import bill = new Bill_Import();

            bill = (Bill_Import)Session["PhieuNhap"];
            var lstBookid = (from b in db.Books
                             where b.NXB_id == bill.NXB_id
                             select b).ToList();

            ViewBag.sach = new SelectList(lstBookid, "Book_id", "Book_name");
            //
            ViewBag.loi = null;
            if (Request.Form["add"] != null)
            {
                if (ModelState.IsValid)
                {
                    bool check = true;
                    foreach (var ctpn in (List <Detail_Bill_Import>)Session["ctphieunhap"])
                    {
                        if (ctpn.Book_id == Int32.Parse(chitiet["sach"].ToString()))
                        {
                            check       = false;
                            ViewBag.loi = "Sách đã được thêm vào phiếu trước đó";
                            break;
                        }
                    }
                    if (check)
                    {
                        if (chitiet["sach"] == null)
                        {
                            ViewBag.loi = "Không có sách của NXB này";
                            goto baoloi;
                        }
                        var sach = db.Books.Find(Int32.Parse(chitiet["sach"].ToString()));
                        if (sach == null)
                        {
                            ViewBag.loi = "Mã sách không tồn tại";
                            goto baoloi;
                        }
                        else
                        {
                            int soluong = Int32.Parse(chitiet["soluong"].ToString());
                            if (soluong > 0)
                            {
                                Detail_Bill_Import ctpn = new Detail_Bill_Import();
                                ctpn.Bill_Import_id = (db.Bill_Import.Max(u => (int?)u.Bill_Import_id) != null ? db.Bill_Import.Max(u => u.Bill_Import_id) : 0) + 1;// db.Bill_Import.Count() + 1;
                                ctpn.Book_id        = Int32.Parse(chitiet["sach"].ToString());
                                ctpn.Quantity       = soluong;
                                ctpn.Cost           = db.Books.Find(ctpn.Book_id).Cost_Import;
                                ctpn.Total          = ctpn.Quantity * ctpn.Cost;
                                //((List<Int32>)Session["BookID"]).Add(ctpn.Book_id);
                                ((List <Detail_Bill_Import>)Session["ctphieunhap"]).Add(ctpn);
                            }
                            else
                            {
                                ViewBag.loi = "Nhập số lượng lớn hơn 0";
                                goto baoloi;
                            }
                        }
                        return(RedirectToAction("ThemChiTietPhieuNhap"));
                    }
                    else
                    {
                        //không làm gì để nó return View();
                    }
                }
            }
            else if (Request.Form["create"] != null)
            {
                if (ModelState.IsValid)
                {
                    if (((List <Detail_Bill_Import>)Session["ctphieunhap"]).Count == 0)
                    {
                        ViewBag.loi = "Không được để phiếu trống";
                        goto baoloi;
                    }
                    double      tongTien     = 0;
                    double?     temptongtien = 0;
                    Bill_Import test         = new Bill_Import();
                    test = (Bill_Import)Session["PhieuNhap"];
                    var luu = db.Bill_Import.Add(test);
                    db.SaveChanges();
                    foreach (var ctpn in (List <Detail_Bill_Import>)Session["ctphieunhap"])
                    {
                        temptongtien += (ctpn.Cost * ctpn.Quantity);
                        tongTien      = double.Parse(temptongtien.ToString());
                        //tongSoLuong += ctpn.SoLuongNhap;
                        Detail_Bill_Import ctPhieuNhap = new Detail_Bill_Import();
                        ctPhieuNhap = ctpn;
                        db.Detail_Bill_Import.Add(ctPhieuNhap);
                        //db.SaveChanges();

                        //cập nhật tồn kho công ty
                        Inventory_Book tonkho = new Inventory_Book();
                        tonkho.UpdatedDate = DateTime.Now;
                        Inventory_Book tonkho1 = db.Inventory_Book.OrderByDescending(m => m.id).FirstOrDefault(m => m.Book_id == (int)ctpn.Book_id);
                        if (tonkho1 != null)
                        {
                            tonkho.Book_id  = ctpn.Book_id;
                            tonkho.Quantity = tonkho1.Quantity + ctpn.Quantity;
                        }
                        else
                        {
                            tonkho.Book_id  = ctpn.Book_id;
                            tonkho.Quantity = ctpn.Quantity;
                        }
                        db.Inventory_Book.Add(tonkho);
                    }

                    //addedPhieuNhap.TongSoLuong = tongSoLuong;
                    luu.Total = tongTien;
                    db.Bill_Import.Attach(luu);
                    db.Entry(luu).State = EntityState.Modified;
                    db.SaveChanges();
                    //cập nhật công nợ NXB
                    Debt_NXB debt = new Debt_NXB();
                    debt.update_date = DateTime.Now;
                    Debt_NXB debt1 = db.Debt_NXB.OrderByDescending(m => m.id).FirstOrDefault(m => m.NXB_id == (int)luu.NXB_id);
                    if (debt1 != null)
                    {
                        debt.NXB_id = luu.NXB_id;
                        debt.debt   = debt1.debt + luu.Total;
                    }
                    else
                    {
                        debt.NXB_id = luu.NXB_id;
                        debt.debt   = luu.Total;
                    }
                    db.Debt_NXB.Add(debt);

                    db.SaveChanges();


                    Session["ctphieunhap"] = null;
                    Session["PhieuNhap"]   = null;

                    return(RedirectToAction("Create"));
                }
            }
baoloi:
            return(View());
        }