Ejemplo n.º 1
0
        private async Task<SaleHeader> CreateAsync(SaleHeader entity)
        {
            var now = DateTimeOffset.UtcNow;
            var user = Thread.CurrentPrincipal.Identity.Name;

            var start = new DateTimeOffset(now.Year, now.Month, now.Day, 0, 0, 0, 0, TimeSpan.Zero);
            var end = new DateTimeOffset(now.Year, now.Month, now.Day, 23, 59, 59, 999, TimeSpan.Zero);
            var dayNumbers = this.database.SaleHeaders.Where(s => s.Created >= start && s.Created <= end).Select(s => s.DayNumber);
            if (await dayNumbers.AnyAsync())
            {
                entity.DayNumber = (await dayNumbers.MaxAsync()) + 1;
            }
            else
            {
                entity.DayNumber = 1;
            }
            var totalNumbers = this.database.SaleHeaders.Select(s => s.TotalNumber);
            if (await totalNumbers.AnyAsync())
            {
                entity.TotalNumber = (await totalNumbers.MaxAsync()) + 1;
            }
            else
            {
                entity.TotalNumber = 1;
            }
            entity.Status = SaleStatus.Ok;
            entity.Create(this.database.SaleHeaders, now, user);
            entity.NumberText = entity.GenerateSaleNumber();

            if (entity.CustomerContacts != null)
            {
                foreach (var contact in entity.CustomerContacts)
                {
                    contact._SaleHeaderId = entity.Id;
                    contact.Create(this.database.Contacts, now, user);
                }
            }

            if (entity.Items != null)
            {
                foreach (var item in entity.Items)
                {
                    item._SaleHeaderId = entity.Id;
                    item.Status = SaleStatus.Ok;
                    item.Create(this.database.SaleLineItems, now, user);
                }
            }

            await this.database.SaveChangesAsync();
            return entity;
        }
Ejemplo n.º 2
0
        public void TestInitialize()
        {
            TestCleanup();

            var totalNumber = default(int);
            using (var db = new SaleDbContext())
            {
                if (db.SaleHeaders.Any())
                {
                    totalNumber = db.SaleHeaders.Max(s => s.TotalNumber);
                }
            }

            var sales = new List<SaleHeader>(55);

            for (int i = 0; i < sales.Capacity; i++)
            {
                var now = GenerateFakeNow();
                var random = new Random(i);

                var sale = new SaleHeader()
                {
                    DayNumber = sales.Count(s => s.Created.Date == now.Date) + 1,
                    TotalNumber = totalNumber + i + 1,
                    SalesPersonName = GenerateSalesPersonName(i),
                    CustomerName = string.Format("customer {0}", i + 1),
                    Status = GenerateEnumValue<SaleStatus>(),
                };
                sale.NumberText = sale.GenerateSaleNumber();
                sale.CustomerContacts = new[]
                {
                    new Contact()
                    {
                        Method = ContactMethod.Phone,
                        Value = string.Format("contact method phone number {0}", i + 1),
                    },
                    new Contact()
                    {
                        Method = GenerateEnumValue<ContactMethod>(),
                        Value = string.Format("contact method {0}", i + 1),
                    },
                };
                sale.Items = new List<SaleLineItem>();
                var length = random.Next(1, 5);
                for (int j = 0; j < length; j++)
                {
                    var line = new SaleLineItem()
                    {
                        ProductName = GenerateProductName(),
                        ProductDescription = null,
                        Quantity = random.Next(1, 2),
                        UnitPrice = (decimal)((random.NextDouble() + 0.1) * 10000),
                        Status = GenerateEnumValue<SaleStatus>(),
                    };
                    if (j % 2 == 0)
                    {
                        line.DynamicProperties.Add("Certificate", GenerateCertificate());
                        line.DynamicProperties.Add("Cut", GenerateCut());
                        line.DynamicProperties.Add("Caret", GenerateCaret());
                        line.DynamicProperties.Add("Color", GenerateColor());
                        line.DynamicProperties.Add("Clarity", GenerateClarity());
                    }

                    sale.Items.Add(line);
                }

                FillCommonValues(sale, now);

                sales.Add(sale);
            }

            using (var db = new SaleDbContext())
            {
                db.SaleHeaders.AddRange(sales);
                db.SaleLineItems.AddRange(sales.SelectMany(s => s.Items));
                db.Contacts.AddRange(sales.SelectMany(s => s.CustomerContacts));
                db.SaveChanges();
            }
        }