private void SavePyramidValues(Pyramid pyramid, CheckoutVM model) { pyramid.PBV += model.SubTotal; decimal sales = 0; foreach (var p in model.Details) { sales += (0.23M - p.Discount) * p.Price * p.Quantity / 100M; } pyramid.SaleBonus += sales; db.SaveChanges(); }
public async Task <int> Create(BPRegVM rec) { //има ли такъв мейл var exists = await db.BPs.AnyAsync(bp => bp.EMail == rec.EMail); exists = exists || await db.SiteUsers.AnyAsync(u => u.Username == rec.EMail); if (exists) { return(1); } int?leaderId = null; if (!rec.LeaderId.HasValue) { var leader = db.BPs.Where(b => b.CountryId == rec.CountryId && b.SiteUser.Pyramids.Any(p => !p.PyramidParentId.HasValue)).OrderBy(o => Guid.NewGuid()).Take(1); if (leader.Count() == 1) { leaderId = leader.ToArray()[0].SiteUserId; } } else { leaderId = rec.LeaderId.Value; } int?leaderPyramidId = null; if (leaderId.HasValue) { leaderPyramidId = db.Pyramids.First(p => p.SiteUserId == leaderId.Value).PyramidId; } var siteUser = new SiteUser() { LevelId = 2, Password = MD5.ConvertToMD5(rec.Password), Username = rec.EMail }; db.SiteUsers.Add(siteUser); db.SaveChanges(); var BP = new BP() { Active = false, Address = rec.Address, BPName = rec.BPName, CountryId = rec.CountryId, EMail = rec.EMail, Files = rec.Files, FileName = rec.FileName, PayPal = rec.PayPal, SiteUserId = siteUser.SiteUserId }; db.BPs.Add(BP); var pyramid = new Pyramid() { PyramidParentId = leaderPyramidId, SiteUserId = siteUser.SiteUserId }; db.Pyramids.Add(pyramid); db.SaveChanges(); var customer = db.Customers.FirstOrDefault(c => c.Username == rec.EMail); if (customer == null) { db.Customers.Add( new Customer() { Address = rec.Address, City = "", BPId = BP.BPId, CountryId = rec.CountryId, CustomerName = rec.BPName, Password = MD5.ConvertToMD5(rec.Password), Username = rec.EMail } ); } else { customer.BPId = rec.BPId; } db.SaveChanges(); return(0); }