示例#1
0
        // GET: Report_Agency/Create
        public ActionResult Create()
        {
            ViewBag.Agency_id = null;
            Report_Agency report_Agency = new Report_Agency();

            report_Agency = (Report_Agency)Session["PhieuBaoCao"];

            /*var lstAgencyid = (from c in db.Debt_Agency
             *                  where ! (from d in db.Debt_Agency
             *                           where d.debt <= 0
             *                           select d.Agency_id).Contains(c.Agency_id)
             *                  select c.Agency_id).Distinct().ToList();*/
            var lstAgencydb = db.Agencies;
            List <Debt_Agency> lstDebtAgency = new List <Debt_Agency>();

            foreach (Agency agen in lstAgencydb)
            {
                Debt_Agency debtdaily = db.Debt_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => (m.Agency_id == agen.Agency_id));
                lstDebtAgency.Add(debtdaily);
            }

            List <Agency> lstAgencyUI = new List <Agency>();

            foreach (Debt_Agency debtdailyitem in lstDebtAgency)
            {
                if (debtdailyitem.debt > 0)
                {
                    Agency agenSelect = db.Agencies.SingleOrDefault(n => n.Agency_id == debtdailyitem.Agency_id);
                    lstAgencyUI.Add(agenSelect);
                }
            }
            ViewBag.Agency_id = new SelectList(lstAgencyUI, "Agency_id", "Agency_name");
            return(View());
        }
示例#2
0
        // GET: Debt_Agency
        public ActionResult Index()
        {
            List <Debt_Agency> list = null;
            var debt_agency         = (from c in db.Debt_Agency group c by c.Agency_id into grp select new { Agency_id = grp.Key, updatedate = grp.Max(x => x.update_date) }).ToArray();

            if (debt_agency != null)
            {
                list = new List <Debt_Agency>();
                foreach (var item in debt_agency)
                {
                    Debt_Agency agency = new Debt_Agency();
                    agency = (from a in db.Debt_Agency where a.Agency_id == item.Agency_id && a.update_date == item.updatedate select a).Include(a => a.Agency).FirstOrDefault();
                    list.Add(agency);
                }
            }
            return(View(list));
        }
示例#3
0
        public ActionResult ThemChiTietPhieuBaoCao(FormCollection chitiet)
        {
            //
            Report_Agency report_Agency = new Report_Agency();

            report_Agency = (Report_Agency)Session["PhieuBaoCao"];
            var lstBookid = (from b in db.Inventory_Agency
                             where b.Agency_id == report_Agency.Agency_id
                             select b.Book_id).Distinct().ToList();
            List <Book> lstBooks = new List <Book>();

            foreach (var item in lstBookid)
            {
                Book book = db.Books.SingleOrDefault(n => n.Book_id == item);
                lstBooks.Add(book);
            }
            ViewBag.sach = new SelectList(lstBooks, "Book_id", "Book_name");
            //
            ViewBag.loi = null;
            if (Request.Form["add"] != null)
            {
                if (ModelState.IsValid)
                {
                    bool check = true;
                    foreach (var ctpbc in (List <Detail_Report_Agency>)Session["ctphieubaocao"])
                    {
                        if (ctpbc.Book_id == Int32.Parse(chitiet["sach"].ToString()))
                        {
                            check       = false;
                            ViewBag.loi = "Đã tồn tại sách trong phiếu";
                            break;
                        }
                    }
                    if (check)
                    {
                        if (chitiet["sach"] == null)
                        {
                            ViewBag.loi = "Không tồn tại sách để báo cáo";
                            goto baoloi;
                        }
                        var              sach        = db.Books.Find(Int32.Parse(chitiet["sach"].ToString()));
                        int              soluong     = Int32.Parse(chitiet["soluong"].ToString());
                        Report_Agency    idagency    = (Report_Agency)Session["PhieuBaoCao"];
                        Inventory_Agency tonkhodaily = db.Inventory_Agency.OrderByDescending(m => m.id).FirstOrDefault(m => (m.Agency_id == idagency.Agency_id && m.Book_id == (int)sach.Book_id));
                        if (sach == null)
                        {
                            ViewBag.loi = "Mã sách không tồn tại";
                            goto baoloi;
                        }
                        else
                        {
                            if (tonkhodaily == null)
                            {
                                ViewBag.loi = "Không có tồn kho";
                                goto baoloi;
                            }
                            else
                            {
                                if (soluong > 0)
                                {
                                    if (soluong <= (tonkhodaily.deliver_quantity - tonkhodaily.repay_quantity))
                                    {
                                        Detail_Report_Agency ctpbc = new Detail_Report_Agency();
                                        ctpbc.Report_id = (db.Report_Agency.Max(u => (int?)u.Report_id) != null ? db.Report_Agency.Max(u => u.Report_id) : 0) + 1;
                                        ctpbc.Book_id   = Int32.Parse(chitiet["sach"].ToString());
                                        ctpbc.quantity  = Int32.Parse(chitiet["soluong"].ToString());
                                        //((List<Int32>)Session["BookID"]).Add(ctpbc.Book_id);
                                        ((List <Detail_Report_Agency>)Session["ctphieubaocao"]).Add(ctpbc);
                                    }
                                    else
                                    {
                                        ViewBag.loi = "Vượt quá số lượng báo cáo";
                                        goto baoloi;
                                    }
                                }
                                else
                                {
                                    ViewBag.loi = "Nhập số lượng lớn hơn 0";
                                    goto baoloi;
                                }
                            }
                        }
                        return(RedirectToAction("ThemChiTietPhieuBaoCao"));
                    }
                    else
                    {
                    }
                }
            }
            else if (Request.Form["create"] != null)
            {
                if (ModelState.IsValid)
                {
                    if (((List <Detail_Report_Agency>)Session["ctphieubaocao"]).Count == 0)
                    {
                        ViewBag.loi = "Không được để phiếu trống";
                        goto baoloi;
                    }
                    double        tongTien     = 0;
                    double?       temptongtien = 0;
                    Report_Agency test         = new Report_Agency();
                    test = (Report_Agency)Session["PhieuBaoCao"];
                    var luu = db.Report_Agency.Add(test);
                    db.SaveChanges();
                    foreach (var ctpbc in (List <Detail_Report_Agency>)Session["ctphieubaocao"])
                    {
                        temptongtien += ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Export.ToString());;
                        tongTien      = double.Parse(temptongtien.ToString());
                        //tongSoLuong += ctpn.SoLuongNhap;
                        Detail_Report_Agency ctPhieuBaoCao = new Detail_Report_Agency();
                        ctPhieuBaoCao = ctpbc;
                        db.Detail_Report_Agency.Add(ctPhieuBaoCao);
                        //db.SaveChanges();

                        //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.Book_id == (int)ctpbc.Book_id && n.Agency_id == (int)ctpbc.Report_Agency.Agency_id);
                        tonkhodaily.UpdatedDate = DateTime.Now;
                        if (tonkhodaily1 != null)
                        {
                            if (tonkhodaily1.repay_quantity == null)
                            {
                                tonkhodaily.Book_id          = ctpbc.Book_id;
                                tonkhodaily.Agency_id        = (int)luu.Agency_id;
                                tonkhodaily.repay_quantity   = ctpbc.quantity;
                                tonkhodaily.deliver_quantity = tonkhodaily1.deliver_quantity;
                            }
                            else
                            {
                                tonkhodaily.Book_id          = ctpbc.Book_id;
                                tonkhodaily.Agency_id        = (int)luu.Agency_id;
                                tonkhodaily.repay_quantity   = tonkhodaily1.repay_quantity + ctpbc.quantity;
                                tonkhodaily.deliver_quantity = tonkhodaily1.deliver_quantity;
                            }
                        }
                        //else
                        //{
                        //    tonkhodaily.Book_id = ctpbc.Book_id;
                        //    tonkhodaily.Agency_id = (int)luu.Agency_id;
                        //    tonkhodaily.repay_quantity = ctpbc.quantity;
                        //    tonkhodaily.deliver_quantity = ctpbc.quantity;
                        //}
                        db.Inventory_Agency.Add(tonkhodaily);
                        //db.SaveChanges();

                        //cập nhật báo cáo đến NXB
                        int        idNXB = int.Parse(db.Books.Find(ctpbc.Book_id).NXB_id.ToString());
                        Report_NXB a     = db.Report_NXB.FirstOrDefault(m => m.status == 0 && m.NXB_id == idNXB);
                        if (a == null)
                        {
                            Report_NXB reportNXB = new Report_NXB();
                            reportNXB.NXB_id      = idNXB;
                            reportNXB.update_date = DateTime.Now;
                            reportNXB.status      = 0;
                            reportNXB.total       = ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Import.ToString());
                            db.Report_NXB.Add(reportNXB);
                            db.SaveChanges();
                        }
                        else
                        {
                            a.total      += ctpbc.quantity * double.Parse(db.Books.Find(ctpbc.Book_id).Cost_Import.ToString());
                            a.update_date = DateTime.Now;
                            db.Report_NXB.Attach(a);
                            db.Entry(a).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                    //Lưu phiếu - tính tổng tiền phiếu
                    //addedPhieuNhap.TongSoLuong = tongSoLuong;
                    luu.Total = tongTien;
                    db.Report_Agency.Attach(luu);
                    db.Entry(luu).State = EntityState.Modified;

                    //cập nhật công nợ đại lý
                    Debt_Agency debt  = new Debt_Agency();
                    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 = (int)luu.Agency_id;
                        debt.debt      = (double)(debt1.debt - luu.Total);
                        debt.repay     = Double.Parse(luu.Total.ToString());
                    }
                    //else
                    //{
                    //    debt.Agency_id = (int)luu.Agency_id;
                    //    debt.debt = (double)luu.Total;
                    //}
                    db.Debt_Agency.Add(debt);

                    db.SaveChanges();


                    Session["ctphieubaocao"] = null;
                    Session["PhieuBaoCao"]   = null;

                    return(RedirectToAction("Create"));
                }
            }
baoloi:
            return(View());
        }
示例#4
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());
        }