Exemplo n.º 1
0
        public ActionResult Create(T_RequestList data)
        {
            ViewBag.RL_CategoryID = new SelectList(_db.T_Category, "CategoryID", "CA_NameCategory");
            ViewBag.RL_TypeID     = new SelectList(_db.T_Type, "TypeID", "TY_NameType");
            ViewBag.RL_PlaceID    = new SelectList(_db.T_Place, "PlaceID", "PL_NamePlace");
            var chk = _db.T_Requisition.FirstOrDefault(x => x.RequisitionID == data.RL_RequisitionID);

            if (data.RL_Amount < 1)
            {
                ModelState.AddModelError("RL_Amount", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else if (data.RL_OnStart < 1)
            {
                ModelState.AddModelError("RL_OnStart", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else if (data.RL_Price < 1)
            {
                ModelState.AddModelError("RL_Price", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    if (chk != null)
                    {
                        if (data.ImageUpload != null)
                        {
                            string fileName  = Path.GetFileNameWithoutExtension(data.ImageUpload.FileName);
                            string extension = Path.GetExtension(data.ImageUpload.FileName);
                            fileName            = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                            data.RL_PictureKhru = "~/Images/" + fileName;
                            data.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/Images/"), fileName));
                        }

                        var str = DateTime.Now.ToString("yy");

                        var TmpCODE = "";
                        for (int i = 0; i < data.RL_Amount; i++)
                        {
                            TmpCODE = data.RL_CategoryID + "." + data.RL_TypeID + "." + (data.RL_OnStart + i) + "/" + str;
                        }

                        var           _VALUE = _db.T_RequestList.ToList();
                        List <string> list   = new List <string>();
                        List <string> ListCK = new List <string>();
                        int           p      = 0;
                        foreach (var item in _VALUE)
                        {
                            p++;
                            list.Add(item.RL_CategoryID + "." + item.RL_TypeID + "." + (item.RL_OnStart + p) + "/" + chk.Re_DateRequi.Value.ToString("yy"));
                        }

                        for (int i = 0; i < data.RL_Amount; i++)
                        {
                            ListCK.Add(data.RL_CategoryID + "." + data.RL_TypeID + "." + (data.RL_OnStart + i) + "/" + chk.Re_DateRequi.Value.ToString("yy"));
                        }

                        int X = 0;
                        foreach (var item in list)
                        {
                            foreach (var item2 in ListCK)
                            {
                                if (item2 == item)
                                {
                                    X++;
                                }
                            }
                        }

                        if (X == 0)
                        {
                            _db.T_RequestList.Add(data);
                            _db.SaveChanges();
                            Session["Result"] = "okC";
                            return(RedirectToAction("Details", "Requisition", new { RequisitionID = data.RL_RequisitionID }));
                        }
                        else
                        {
                            ModelState.AddModelError("RL_OnStart", "เลขครุภัณฑ์ซ้ำ กรุณาตรวจสอบอีกครั้ง");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("RL_RequisitionID", "ไม่พบข้อมูลเลขการเบิก");
                    }
                }
            }

            return(View(data));
        }
Exemplo n.º 2
0
        public ActionResult Edit(T_RequestList data)
        {
            ViewBag.RL_CategoryID = new SelectList(_db.T_Category, "CategoryID", "CA_NameCategory", data.RL_CategoryID);
            ViewBag.RL_TypeID     = new SelectList(_db.T_Type, "TypeID", "TY_NameType", data.RL_TypeID);
            ViewBag.RL_PlaceID    = new SelectList(_db.T_Place, "PlaceID", "PL_NamePlace", data.RL_PlaceID);
            var value = _db.T_RequestList.Where(a => a.RL_CategoryID == data.RL_CategoryID && a.RL_TypeID == data.RL_TypeID &&
                                                a.RL_RequisitionID != data.RL_RequisitionID).ToList();

            List <string> Tmp_CHK = new List <string>();
            List <string> Add_CHK = new List <string>();

            foreach (var item in value)
            {
                for (int i = 0; i < item.RL_Amount; i++)
                {
                    Tmp_CHK.Add(item.RL_CategoryID + item.RL_TypeID + i);
                }
            }

            for (int i = 0; i < data.RL_Amount; i++)
            {
                Add_CHK.Add(data.RL_CategoryID + data.RL_TypeID + i);
            }

            int P = 0;

            foreach (var Lopdb in Tmp_CHK)
            {
                foreach (var lopCn in Add_CHK)
                {
                    if (Lopdb == lopCn)
                    {
                        P++;
                    }
                }
            }
            if (data.RL_Amount < 1)
            {
                ModelState.AddModelError("RL_Amount", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else if (data.RL_OnStart < 1)
            {
                ModelState.AddModelError("RL_OnStart", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else if (data.RL_Price < 1)
            {
                ModelState.AddModelError("RL_Price", "กรุณากรอกข้อมูล ให้มากกว่า 0");
            }
            else
            {
                if (P == 0)
                {
                    if (data.ImageUpload != null)
                    {
                        string fileName  = Path.GetFileNameWithoutExtension(data.ImageUpload.FileName);
                        string extension = Path.GetExtension(data.ImageUpload.FileName);
                        fileName            = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                        data.RL_PictureKhru = "~/Images/" + fileName;
                        data.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/Images/"), fileName));
                    }

                    if (ModelState.IsValid)
                    {
                        _db.Entry(data).State = EntityState.Modified;
                        _db.SaveChanges();
                        Session["Result"] = "okE";
                        return(RedirectToAction("Details", "Requisition", new { RequisitionID = data.RL_RequisitionID }));
                    }
                }

                ModelState.AddModelError("RL_OnStart", "ข้อมูลนี้มีอยู่ในฐานข้อมูลแล้ว  กรุณาตรวจสอบอีกครั้ง");
            }

            return(View(data));
        }