Beispiel #1
0
        public ActionResult Edit(BookingFormStub model, bool print = false)
        {
            if (model.ListRentPackageText != null && model.ListRentPackageText != "")
            {
                model.ListRentPackageItem = new JavaScriptSerializer().Deserialize <List <RentPackageFormStub> >(model.ListRentPackageText);
            }
            List <rent_package> pakets;
            RentPackageFormStub rentPackageFS;

            rent dbItem = RepoRent.FindByPk(model.Id);

            if (model.Status != RentStatus.CANCEL)
            {
                ModelState.Remove("CancelNotes");
            }

            if (ModelState.IsValid)
            {
                CustomPrincipal user = User as CustomPrincipal;

                //process customer
                customer cust;
                if (model.IdCustomer.HasValue == false)
                {
                    cust = model.CreateNewCustomer(user.IdOwner.Value);
                    RepoCustomer.Save(cust);
                    model.IdCustomer = cust.id;
                }
                else
                {
                    cust = RepoCustomer.FindByPk(model.IdCustomer.Value);
                    model.UpdateCustomer(cust);

                    RepoCustomer.Save(cust);
                }

                //save to db
                model.UpdateDbObject(dbItem, user);
                RepoRent.Save(dbItem);

                //save rent_package
                pakets = dbItem.rent_package.ToList();
                foreach (rent_package paket in pakets.ToList())
                {
                    RepoRent.DeleteRentPackage(paket);
                }

                if (model.ListRentPackageItem != null && model.ListRentPackageItem.Count() > 0)
                {
                    foreach (RentPackageFormStub single in model.ListRentPackageItem)
                    {
                        RepoRent.SaveListPackage(single.GetDbObject(model.Id, single.IdCarPackage));
                    }
                }



                //rent_package idCarPackage;
                //foreach (RentPackageFormStub Single in model.ListRentPackageItem)
                //{
                //    idCarPackage = pakets.Where(n => n.id_rent == Single.IdRent).FirstOrDefault();
                //    foreach (rent_package paket in pakets)
                //    {
                //        if (idCarPackage.id_car_package != paket.id_car_package)
                //        {
                //            pakets.Remove(paket);
                //            //rentPackageFS.SetDbObject(paket);
                //            //RepoRent.SaveListPackage(paket);
                //        }
                //    }
                //}

                //foreach (rent_package paket in pakets)
                //{
                //    //idCarPackage = pakets.Where(n => n.id_car_package == paket.id_car_package).FirstOrDefault();
                //    rentPackageFS = model.ListRentPackageItem.Where(n => n.IdRent == paket.id_rent).FirstOrDefault();
                //    if (rentPackageFS != null)
                //    {
                //        rentPackageFS.SetDbObject(paket);
                //        RepoRent.SaveListPackage(paket);
                //    }
                //}

                //message
                string template = HttpContext.GetGlobalResourceObject("MyGlobalMessage", "EditSuccess").ToString();
                this.SetMessage("Booking " + model.Name, template);

                //print flag
                if (print)
                {
                    TempData["idPrint"] = dbItem.id;
                }

                return(RedirectToAction("Index"));
            }
            else
            {
                model.Code = dbItem.code;
                FillModelOptions(model);

                ViewBag.name = "Booking " + dbItem.customer.name;

                return(View("Form", model));
            }
        }
Beispiel #2
0
        public ActionResult Create(BookingFormStub model, bool print = false)
        {
            if (model.Status != RentStatus.CANCEL)
            {
                ModelState.Remove("CancelNotes");
            }
            if (model.IdCarPackage == Guid.Empty)
            {
                ModelState.Remove("IdCarPackage");
            }

            if (model.ListRentPackageText != null)
            {
                model.ListRentPackageItem = new JavaScriptSerializer().Deserialize <List <RentPackageFormStub> >(model.ListRentPackageText);
            }
            ModelState.Remove("PriceDay");
            //ModelState.Remove("Price");

            if (ModelState.IsValid)
            {
                CustomPrincipal user = User as CustomPrincipal;

                //melengkapi data model
                owner owner = RepoOwner.FindByPk(user.IdOwner.Value);
                model.Code = RepoRent.GenerateRentCode(owner);

                customer cust;
                if (model.IdCustomer.HasValue == false)
                {
                    cust = model.CreateNewCustomer(user.IdOwner.Value);
                    RepoCustomer.Save(cust);
                    model.IdCustomer = cust.id;
                }
                else
                {
                    cust = RepoCustomer.FindByPk(model.IdCustomer.Value);
                    model.UpdateCustomer(cust);

                    RepoCustomer.Save(cust);
                }

                //save to db
                rent dbItem = model.GetDbObjectOnCreate(user.Identity.Name, user.IdOwner.Value);
                //RepoRent.Save(dbItem);

                Guid idRentPackage = RepoRent.Save(dbItem);

                if (model.ListRentPackageItem != null && model.ListRentPackageItem.Count() > 0)
                {
                    foreach (RentPackageFormStub single in model.ListRentPackageItem)
                    {
                        RepoRent.SaveListPackage(single.GetDbObject(idRentPackage, single.IdCarPackage));
                    }
                }

                //message
                string template = HttpContext.GetGlobalResourceObject("MyGlobalMessage", "CreateSuccess").ToString();
                this.SetMessage("Booking " + model.Name, template);

                //print flag
                if (print)
                {
                    TempData["idPrint"] = dbItem.id;
                }

                return(RedirectToAction("Index"));
            }
            else
            {
                FillModelOptions(model);

                return(View("Form", model));
            }
        }