public static bool SendContract(int offerId)
        {
            var offer = OffersDAL.GetOffer(offerId);

            if (offer == null)
            {
                return(false);
            }
            var ad = AdsDAL.GetAd(offer.AdId);

            if (ad == null)
            {
                return(false);
            }
            var sender = SM.GetPersonalAreaUser();

            if (sender.Id == 0 || ad.SenderId != sender.Id)
            {
                return(false);
            }
            //var notificationsUserId = User.SystemNotificationsUserId;
            var messageText = "Подтвердите контракт с " + sender.CompanyNameWithTypeOfOwnership + (sender.Town != null ? ", " + sender.TownName : "") + " по объявлению " + ad.Name + ".";
            var message     = new Message
            {
                SenderId    = ad.SenderId,
                RecipientId = offer.SenderId,
                Text        = messageText,
                IsReviewContractNotification = true,
                IsContractReviewed           = false,
                OfferId = offerId,
                AdId    = ad.Id
            };

            if (AddMessage(message) > 0)
            {
                OffersDAL.ChangeOfferContractStatus(offer.Id, (int)ContractStatuses.Sent);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static bool ReviewContract(int offerId, bool isAccept, bool isByAdCreator)
        {
            if (!MessagesDAL.MarkContractAsReviewed(offerId))
            {
                return(false);
            }
            var offer = OffersDAL.GetOffer(offerId);

            if (offer == null)
            {
                return(false);
            }
            var ad = AdsDAL.GetAd(offer.AdId);

            if (ad == null)
            {
                return(false);
            }
            var sender = SM.GetPersonalAreaUser();

            if (offer.SenderId != sender.Id && ad.SenderId != sender.Id)
            {
                return(false);
            }
            if (isAccept)
            {
                AdsDAL.ChangeAdStatusToFinishedAndActiveUntilDateToNow(offer.AdId);
                OffersDAL.ChangeOfferContractStatus(offer.Id, (int)ContractStatuses.Accepted);
                OffersDAL.UpdateOfferContractSendDate(offer.Id, DateTime.Now.ToUniversalTime());
                //var notificationsUserId = User.SystemNotificationsUserId;
                var messageText = "Контракт c " + sender.CompanyNameWithTypeOfOwnership + " по объявлению " + ad.Name + " подтвержден.";
                var message     = new Message
                {
                    SenderId    = offer.SenderId,
                    RecipientId = ad.SenderId,
                    Text        = messageText,
                    AdId        = ad.Id,
                    OfferId     = offer.Id
                };
                AddMessage(message);
                var offers = OffersDAL.GetOffers(ad.Id).Where(o => o.ContractStatus != ContractStatuses.Accepted).ToList();
                if (offers.Any())
                {
                    var adSender = UsersDAL.GetUser(ad.SenderId);
                    if (adSender != null)
                    {
                        adSender.Town = TownsDAL.GetTown(adSender.CityId);
                        var notificationsUserId = MContract.Models.User.SystemNotificationsUserId;
                        foreach (var o in offers)
                        {
                            messageText = "По объявлению " + ad.Name + " организатора " + adSender.CompanyNameWithTypeOfOwnership + (adSender.Town != null ? ", " + adSender.TownName : "") +
                                          " заключен контракт с другим участником.";
                            message = new Message
                            {
                                SenderId    = notificationsUserId,
                                RecipientId = o.SenderId,
                                Text        = messageText,
                                AdId        = ad.Id
                            };
                            AddMessage(message);
                        }
                    }
                }
            }
            else
            {
                //удаляем предложение, только если отменяет контракт участник, подавший это предложение, а не организатор
                //если отменяет контракт организатор
                if (isByAdCreator)
                {
                    OffersDAL.ChangeOfferContractStatus(offer.Id, (int)ContractStatuses.Declined);
                }
                else                //а если контракт отменяет участник, подавший предложение
                {
                    OffersDAL.DeleteOffer(offerId);
                }

                //var notificationsUserId = User.SystemNotificationsUserId;
                var messageText = "Контракт c " + sender.CompanyNameWithTypeOfOwnership + " по объявлению " + ad.Name + " отменен.";
                var message     = new Message
                {
                    SenderId    = offer.SenderId,
                    RecipientId = ad.SenderId,
                    Text        = messageText,
                    AdId        = ad.Id,
                    OfferId     = offer.Id
                };
                AddMessage(message);
            }
            return(true);
        }