Пример #1
0
        public JsonResult ModalEdit(CPL form)
        {
            if (!AuthorizationProvider.IsPropertyEditor())
            {
                return(Json("denied", JsonRequestBehavior.AllowGet));
            }

            try
            {
                if (ModelState.IsValid)
                {
                    PropertyProvider dataProvider = new PropertyProvider(_dbContext);

                    // make the date to PST time zone to store in DB using UTC
                    if (form.ListingStartDate != null)
                    {
                        form.ListingStartDate = form.ListingStartDate.Value.Date.AddHours(11);
                    }
                    if (form.PendingContractDate != null)
                    {
                        form.PendingContractDate = form.PendingContractDate.Value.Date.AddHours(11);
                    }
                    if (form.PendingOnboardingDate != null)
                    {
                        form.PendingOnboardingDate = form.PendingOnboardingDate.Value.Date.AddHours(11);
                    }

                    bool managementFeeChanged = false;
                    bool titleChanged         = false;
                    if (!dataProvider.PropertyExist(form.PropertyCode)) // new property
                    {
                        titleChanged         = true;
                        managementFeeChanged = true;
                        form.PropertyCode    = form.PropertyCode.ToUpper();
                        // if entity state is EntityState.UnAttached, CreatedDate won't be created. we set it explicitly here just to be sure
                        form.CreatedDate = ConversionHelper.EnsureUtcDate(DateTime.Now.Date);
                        form.CreatedBy   = ClaimsPrincipal.Current.Identity.Name;
                        dataProvider.Create(form);
                    }
                    else // updating property
                    {
                        var property = dataProvider.Retrieve(form.PropertyCode);
                        if (property != null)
                        {
                            titleChanged         = string.Compare(property.AirBnBHomeName, form.AirBnBHomeName, true) != 0;
                            managementFeeChanged = property.Ownership != form.Ownership;

                            if (string.Compare(property.PropertyStatus, form.PropertyStatus, true) != 0)
                            {
                                setInactiveTimestamp(form);
                            }

                            dataProvider.Update(form.PropertyCode, form);
                        }
                    }
                    dataProvider.Commit();

                    // update PropertyTitleHistory table if title has changed
                    if (titleChanged)
                    {
                        var titleProvider = new PropertyTitleHistoryProvider(_dbContext);
                        if (!titleProvider.Exist(form.PropertyCode, form.AirBnBHomeName))
                        {
                            var titleHistory = new PropertyTitleHistory()
                            {
                                PropertyCode  = form.PropertyCode,
                                PropertyTitle = form.AirBnBHomeName,
                                EffectiveDate = ConversionHelper.EnsureUtcDate(DateTime.UtcNow)
                            };
                            titleProvider.Create(titleHistory);
                            titleProvider.Commit();
                        }
                    }

                    // obsolete: Property fee has moved to property fee table
                    if (managementFeeChanged)
                    {
                        // TODO: add a record to PropertyFee table for new management fee
                    }

                    ViewBag.Accounts = (new AirbnbAccountProvider(_dbContext)).AggregatedAccounts();
                    return(Json(form.PropertyCode, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                // TODO: log
                this.ModelState.AddModelError("", ex);
            }
            return(Json(string.Empty, JsonRequestBehavior.AllowGet));
        }