Example #1
0
        protected Renta CreateRenta(int modelId, int priceId, int beat, int count = 1)
        {
            var sin = BillingBlocked(modelId, s => s.Wallet, s => s.Character, s => s.Passport, s => s.Scoring);

            if (count == 0)
            {
                count = 1;
            }
            var price = Get <Price>(p => p.Id == priceId,
                                    p => p.Sku.Nomenklatura.Specialisation.ProductType,
                                    p => p.Sku.Corporation.Wallet,
                                    s => s.Shop.Wallet,
                                    s => s.Sin.Character);

            if (price == null)
            {
                throw new BillingException("Персональное предложение не найдено");
            }
            if (price.Confirmed)
            {
                throw new Exception("Персональным предложением уже воспользовались");
            }
            if (price.Sin.Character.Model != modelId)
            {
                throw new Exception("Персональное предложение заведено на другого персонажа");
            }
            var dateTill = price.DateCreated.AddMinutes(_settings.GetIntValue(SystemSettingsEnum.price_minutes));

            if (dateTill < DateTime.Now.ToUniversalTime())
            {
                throw new BillingException($"Персональное предложение больше не действительно, оно истекло {dateTill:HH:mm:ss}");
            }
            var allowed = SkuAllowed(price.ShopId, price.SkuId);

            if (allowed == null)
            {
                throw new BillingException("Sku недоступно для продажи в данный момент");
            }
            price.FinalPrice *= count;
            if (sin.Wallet.Balance - price.FinalPrice < 0)
            {
                throw new BillingException("Недостаточно средств");
            }

            price.Sku.Count -= count;
            var instantConsume = price.Sku.Nomenklatura.Specialisation.ProductType.InstantConsume;
            var anon           = GetAnon(sin.Character.Model);
            var gmdescript     = BillingHelper.GetGmDescription(sin.Passport, price.Sku, anon);
            var renta          = new Renta
            {
                BasePrice      = price.BasePrice,
                Sin            = sin,
                CurrentScoring = price.CurrentScoring,
                Sku            = price.Sku,
                DateCreated    = DateTime.Now.ToUniversalTime(),
                Discount       = price.Discount,
                ShopComission  = price.ShopComission,
                ShopPrice      = price.ShopPrice,
                ShopId         = price.ShopId,
                Shop           = price.Shop,
                HasQRWrite     = instantConsume ? false : BillingHelper.HasQrWrite(price.Sku.Nomenklatura.Code),
                PriceId        = priceId,
                Secret         = gmdescript,
                LifeStyle      = price.Sku.Nomenklatura.Lifestyle,
                Count          = count,
                FullPrice      = price.Sku.Nomenklatura.Specialisation.ProductType.Alias == ProductTypeEnum.Charity.ToString(),
                BeatId         = beat
            };

            Add(renta);
            price.Confirmed = true;
            SaveContext();
            ProcessBuyScoring(sin, price.Sku, price.Shop);
            var mir = GetMIR();

            ProcessRenta(renta, mir, sin, true);
            SaveContext();
            if (instantConsume)
            {
                var erService = new EreminService();
                erService.ConsumeFood(renta.Id, (Lifestyles)renta.LifeStyle, modelId).GetAwaiter().GetResult();
            }
            return(renta);
        }