예제 #1
0
        public JsonResult SaveRevenue(ReservationRevenueModel form)
        {
            if (!AuthorizationProvider.CanEditRevenue())
            {
                return(Forbidden());
            }

            try
            {
                // treat checkin and checkout date as Hawaii time zone and covert it to UTC by adding 11 hours.
                if (form.PayoutDate != null)
                {
                    form.PayoutDate = ConversionHelper.ToUtcFromUs(form.PayoutDate.Value);
                }
                if (form.CheckinDate != null)
                {
                    form.CheckinDate = ConversionHelper.ToUtcFromUs(form.CheckinDate.Value);
                }

                var dataProvider = new ReservationRevenueProvider(_dbContext);
                if (form.ReservationId == 0) // new reservation
                {
                    if (dataProvider.GetKey(form) != 0)
                    {
                        Response.StatusCode = (int)System.Net.HttpStatusCode.Conflict; // code = 409
                        return(Json(string.Empty, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        dataProvider.Create(form);
                    }
                }
                else // updating reservation
                {
                    dataProvider.Update(form.ReservationId, form);
                }

                dataProvider.Commit(); // ReservationId will be filled for new reservation by EF

                // get the reservation ID
                if (form.ReservationId == 0)
                {
                    form.ReservationId = dataProvider.GetKey(form);
                }

                var provider = new OwnerPayoutProvider(_dbContext);
                provider.UpdateOwnerPayoutMatchStatus(form.OwnerPayoutId);

                return(Json(form.ReservationId.ToString(), JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                var    innerErrorMessage = ex.InnerException != null ? ex.InnerException.Message : string.Empty;
                string message           = string.Format("Saving Reservation {0} fails. {1},{2}", form.ReservationId.ToString(), ex.Message, innerErrorMessage);
                return(InternalError(message, string.Empty, ex));
            }
        }