Exemple #1
0
        public void StartStopTransaction(int transactionId)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var trans = context.Transactions.Where(w => w.Id == transactionId).FirstOrDefault();

            if (trans == null)
            {
                return;
            }

            trans.State = trans.State == 0 ? 2 : 0;

            EnumTransactionDateType type = (EnumTransactionDateType)context.TransactionDates
                                           .Where(w => w.TransactionId == transactionId).OrderByDescending(o => o.Date)
                                           .Select(s => s.Type).FirstOrDefault();

            TransactionDate transactionDate = new TransactionDate();

            transactionDate.Date          = DateTime.Now;
            transactionDate.TransactionId = transactionId;
            transactionDate.Type          = type == EnumTransactionDateType.Start ? (int)EnumTransactionDateType.Stop : (int)EnumTransactionDateType.Start;

            context.TransactionDates.Add(transactionDate);
            context.SaveChangesAsync();
        }
        public List <CombinedItemDto> GetCombinedItems(string searchTerm)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var query = (from ip in context.ItemPrices
                         join i in context.Items on ip.ItemId equals i.Id
                         join ig in context.ItemGroups on i.ItemGroupId equals ig.Id
                         where (ip.Code.ToLower().Contains(searchTerm) || i.Name.ToLower().Contains(searchTerm)) &&
                         i.IsDeleted == false && ip.IsDeleted == false
                         select new CombinedItemDto()
            {
                BufferDuration = ip.BufferDuration,
                Name = i.Name,
                ItemGroupName = ig.Name,
                Duration = ip.Duration,
                Price = ip.Price,
                PriceCode = ip.Code,
                IsUnlimited = ip.IsUnlimited
            }).ToList();

            foreach (var item in query)
            {
                item.DurationStr = item.IsUnlimited ? L("IsUnlimited") : item.Duration.ToString();
            }

            return(query);
        }
        public List <CustomerDto> SearchCustomer(string searchTerm)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);


            var query = (from c in context.Customers
                         join cg in context.CustomerGroups on c.CustomerGroupId equals cg.Id
                         where c.FirstName.ToLower().Contains(searchTerm) || c.LastName.ToLower().Contains(searchTerm) || c.MailAddress.Contains(searchTerm)
                         orderby c.Id descending

                         select new CustomerDto
            {
                Id = c.Id,
                BirthDate = c.BirthDate,
                CardId = c.CardId,
                CreationTime = c.CreationTime,
                CustomerGroupId = c.CustomerGroupId,
                CustomerGroupName = cg.Name,
                FirstName = c.FirstName,
                IsDeleted = c.IsDeleted,
                LastName = c.LastName,
                MailAddress = c.MailAddress,
                PhoneNumber = c.PhoneNumber
            });

            var res = query.ToList();

            return(res);
        }
        public override Task <PagedResultDto <ItemDto> > GetAll(PagedItemResultRequestDto input)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var count = context.Customers.Count();

            var query = (from c in context.Items
                         join cg in context.ItemGroups on c.ItemGroupId equals cg.Id
                         where c.IsDeleted == false
                         orderby c.Id descending

                         select new ItemDto
            {
                Id = c.Id,
                CreationTime = c.CreationTime,
                ItemGroupId = c.ItemGroupId,
                ItemGroupName = cg.Name,
                Name = c.Name,
                IsDeleted = c.IsDeleted,
                Type = c.Type
            }).PageBy(input);

            var res = query.ToList();

            var output = new PagedResultDto <ItemDto>
            {
                TotalCount = count,
                Items      = res
            };

            return(Task.FromResult(output));
        }
        public override Task <ItemDto> Update(ItemEditDto input)
        {
            var itemPrices = input.ItemPrices;

            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            if (string.IsNullOrEmpty(input.UnlimitedItemCode) == false)
            {
                var unlimitedItemPrice = context.ItemPrices.Where(w => w.ItemId == input.Id && w.IsUnlimited).FirstOrDefault();

                if (unlimitedItemPrice == null)
                {
                    unlimitedItemPrice             = new ItemPrice();
                    unlimitedItemPrice.ItemId      = input.Id;
                    unlimitedItemPrice.Code        = input.UnlimitedItemCode;
                    unlimitedItemPrice.IsUnlimited = true;
                    context.ItemPrices.Add(unlimitedItemPrice);
                }
                else if (unlimitedItemPrice.Code != input.UnlimitedItemCode)
                {
                    unlimitedItemPrice.Code = input.UnlimitedItemCode;
                }
            }
            else
            {
                var unlimitedItemPrice = context.ItemPrices.Where(w => w.ItemId == input.Id && w.IsUnlimited).FirstOrDefault();

                if (unlimitedItemPrice != null)
                {
                    unlimitedItemPrice.IsDeleted = true;
                }
            }

            foreach (var _itemPrice in itemPrices)
            {
                var dbItem = context.ItemPrices.Where(w => w.Id == _itemPrice.Id).FirstOrDefault();

                if (dbItem != null)
                {
                    ObjectMapper.Map(_itemPrice, dbItem);
                    context.Entry(dbItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
                else
                {
                    var mappedPrice = ObjectMapper.Map <ItemPrice>(_itemPrice);
                    context.ItemPrices.Add(mappedPrice);
                }
            }

            context.SaveChanges();

            var res = base.Update(input);

            return(res);
        }
        public Task <List <ItemPriceDto> > GetItemPrices(int itemId)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            List <ItemPrice> itemPrices = context.ItemPrices.Where(w => w.ItemId == itemId && w.IsUnlimited == false && w.IsDeleted == false).ToList();

            var res = ObjectMapper.Map <List <ItemPriceDto> >(itemPrices);

            return(Task.FromResult <List <ItemPriceDto> >(res));
        }
Exemple #7
0
        public override async Task <TransactionDto> Create(CreateTransactionDto input)
        {
            try
            {
                TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
                TimePlayZeroDbContext        context = fac.CreateDbContext(null);

                Transaction transaction = new Transaction();
                transaction.CreationTime = DateTime.Now;
                transaction.Duration     = input.Duration;
                transaction.CustomerName = input.CustomerName;
                transaction.PhoneNumber  = input.PhoneNumber;
                transaction.Price        = input.Price;

                transaction.State         = input.State;
                transaction.ItemPriceCode = input.ItemPriceCode;
                transaction.ItemPriceId   = input.ItemPriceId;
                transaction.CustomerId    = input.CustomerId;
                transaction.Description   = input.Description;
                transaction.IsUnlimited   = input.IsUnlimited;
                context.Transactions.Add(transaction);
                int x = await context.SaveChangesAsync();

                TransactionDate transactionDate = new TransactionDate();
                transactionDate.Date          = DateTime.Now;
                transactionDate.Type          = (int)EnumTransactionDateType.Start;
                transactionDate.TransactionId = transaction.Id;

                context.TransactionDates.Add(transactionDate);

                int y = await context.SaveChangesAsync();

                var res = MapToEntityDto(transaction);

                res.TotalPrice = transaction.Price;
                res.StartDate  = transactionDate.Date;

                if (input.IsUnlimited)
                {
                    res.DurationStr = L("IsUnlimited");
                }
                else
                {
                    res.DurationStr = res.Duration.ToString();
                }
                return(res);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #8
0
        public CreateTransactionDto GetItemPrice(string code)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            DateTime dtStart = DateTime.Now.AddSeconds(30);

            var query = (from ip in context.ItemPrices
                         join i in context.Items on ip.ItemId equals i.Id
                         where ip.Code == code
                         select new
            {
                ItemPriceId = ip.Id,
                Description = i.Name,
                Price = ip.Price,
                ItemPriceCode = code,
                State = 0,
                Duration = ip.Duration,
                BufferDuration = ip.BufferDuration,
                ItemType = i.Type,
                IsUnlimited = ip.IsUnlimited,
                ItemId = ip.ItemId
            }).FirstOrDefault();

            if (query != null)
            {
                CreateTransactionDto dto = new CreateTransactionDto();
                dto.Description    = query.Description;
                dto.Duration       = query.Duration;
                dto.ItemPriceCode  = query.ItemPriceCode;
                dto.ItemPriceId    = query.ItemPriceId;
                dto.Price          = query.Price;
                dto.BufferDuration = query.BufferDuration;
                dto.IsUnlimited    = query.IsUnlimited;
                dto.DurationStr    = query.IsUnlimited ? L("IsUnlimited") : query.Duration.ToString();

                if (query.IsUnlimited && query.ItemType == (int)EnumItemType.PeriodBased)
                {
                    var firstItemPrice = context.ItemPrices.Where(w => w.ItemId == query.ItemId && w.IsUnlimited == false).OrderBy(o => o.Duration).FirstOrDefault();

                    dto.Duration       = firstItemPrice.Duration;
                    dto.BufferDuration = firstItemPrice.BufferDuration;
                    dto.Price          = firstItemPrice.Price;
                }

                return(dto);
            }
            else
            {
                return(null);
            }
        }
Exemple #9
0
        public void FinishItem(int transactionId)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var tran = context.Transactions.Where(w => w.Id == transactionId).FirstOrDefault();

            if (tran != null)
            {
                tran.State = 1;
                context.SaveChanges();
            }
        }
        public override async Task <ItemDto> Get(EntityDto <int> input)
        {
            var itemDto = await base.Get(input);

            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var unlimitedCode = context.ItemPrices.Where(w => w.ItemId == itemDto.Id && w.IsUnlimited && w.IsDeleted == false).Select(s => s.Code).FirstOrDefault();

            itemDto.UnlimitedItemCode = unlimitedCode;

            return(itemDto);
        }
Exemple #11
0
        public override async Task <ItemGroupDto> Create(CreateItemGroupDto input)
        {
            CheckCreatePermission();

            var ItemGroup = ObjectMapper.Map <ItemGroup>(input);

            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            context.ItemGroups.Add(ItemGroup);
            context.SaveChanges();

            await CurrentUnitOfWork.SaveChangesAsync();

            return(MapToEntityDto(ItemGroup));
        }
        public override async Task <ItemDto> Create(CreateItemDto input)
        {
            try
            {
                CheckCreatePermission();

                var item = ObjectMapper.Map <Item>(input);

                var itemPrices = input.ItemPrices;

                TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
                TimePlayZeroDbContext        context = fac.CreateDbContext(null);

                context.Items.Add(item);
                context.SaveChanges();
                if (string.IsNullOrEmpty(input.UnlimitedItemCode) == false)
                {
                    ItemPrice itemPrice = new ItemPrice();
                    itemPrice.Code         = input.UnlimitedItemCode;
                    itemPrice.IsUnlimited  = true;
                    itemPrice.CreationTime = DateTime.Now;
                    itemPrice.ItemId       = item.Id;
                    context.ItemPrices.Add(itemPrice);
                    context.SaveChanges();
                }

                foreach (var _itemPrice in itemPrices)
                {
                    var mappedPrice = ObjectMapper.Map <ItemPrice>(_itemPrice);

                    mappedPrice.ItemId = item.Id;
                    context.Add(mappedPrice);
                }

                context.SaveChanges();

                await CurrentUnitOfWork.SaveChangesAsync();

                return(MapToEntityDto(item));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public override Task <PagedResultDto <CustomerDto> > GetAll(PagedCustomerResultRequestDto input)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var count = context.Customers.Count();

            var query = (from c in context.Customers
                         join cg in context.CustomerGroups on c.CustomerGroupId equals cg.Id
                         orderby c.Id descending

                         select new CustomerDto
            {
                Id = c.Id,
                BirthDate = c.BirthDate,
                CardId = c.CardId,
                CreationTime = c.CreationTime,
                CustomerGroupId = c.CustomerGroupId,
                CustomerGroupName = cg.Name,
                FirstName = c.FirstName,
                IsDeleted = c.IsDeleted,
                LastName = c.LastName,
                MailAddress = c.MailAddress,
                PhoneNumber = c.PhoneNumber
            }).PageBy(input);

            var res = query.ToList();

            var output = new PagedResultDto <CustomerDto>
            {
                TotalCount = count,
                Items      = res
            };

            return(Task.FromResult(output));
        }
Exemple #14
0
        public override async Task <PagedResultDto <TransactionDto> > GetAll(PagedItemResultRequestDto input)
        {
            try
            {
                TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
                TimePlayZeroDbContext        context = fac.CreateDbContext(null);

                var count = context.Transactions.Where(w => w.State == 0).Count();

                var query = (from t in context.Transactions
                             join ip in context.ItemPrices on t.ItemPriceId equals ip.Id
                             join i in context.Items on ip.ItemId equals i.Id

                             where t.State == 0
                             orderby t.Id descending

                             select new TransactionDto
                {
                    Id = t.Id,
                    ItemId = i.Id,
                    CreationTime = t.CreationTime,
                    State = t.State,
                    CustomerId = t.CustomerId,
                    Description = t.Description,
                    IsDeleted = t.IsDeleted,
                    ItemPriceCode = t.ItemPriceCode,
                    ItemPriceId = t.ItemPriceId,
                    PhoneNumber = t.PhoneNumber,
                    TotalPrice = t.Price == 0 ? ip.Price : t.Price,
                    CustomerName = t.CustomerName,
                    BufferDuration = ip.BufferDuration,
                    ItemType = i.Type,
                    Price = ip.Price,
                    Duration = t.Duration,
                    TransactionDates = t.TransactionDates,
                    IsUnlimited = t.IsUnlimited
                }).PageBy(input);

                var res = query.ToList();

                foreach (var item in res)
                {
                    var dates = item.TransactionDates.OrderBy(o => o.Date).ToList();

                    item.StartDate = dates.Select(s => s.Date).FirstOrDefault();

                    var    chunks      = dates.ChunkBy(2);
                    double totalSecond = 0;
                    foreach (var pair in chunks)
                    {
                        if (pair.Count == 2)
                        {
                            totalSecond += (pair[1].Date - pair[0].Date).TotalSeconds;
                        }
                        else
                        {
                            totalSecond += (DateTime.Now - pair[0].Date).TotalSeconds;
                        }
                    }

                    double totalMin = totalSecond / 60;

                    if (item.ItemType == (int)EnumItemType.PeriodBased)
                    {
                        if (totalMin > (double)(item.BufferDuration + item.Duration)) //Update next item
                        {
                            var nextPrice = context.ItemPrices.Where(w => w.Duration > totalMin && w.ItemId == item.ItemId)
                                            .OrderBy(o => o.Duration).FirstOrDefault();

                            if (nextPrice == null)
                            {
                                nextPrice = context.ItemPrices.Where(w => w.ItemId == item.ItemId)
                                            .OrderByDescending(o => o.Duration).FirstOrDefault();
                            }

                            if (nextPrice != null)
                            {
                                item.ItemPriceId   = nextPrice.Id;
                                item.ItemPriceCode = nextPrice.Code;

                                item.BufferDuration = nextPrice.BufferDuration;
                                item.TotalPrice     = nextPrice.Price;
                                item.Duration       = nextPrice.Duration;

                                await Update(item);
                            }
                        }
                    }
                    else if (item.ItemType == (int)EnumItemType.TimeBased)
                    {
                        if (totalMin > 1)
                        {
                            if (totalMin >= item.Duration)
                            {
                                var nextPrice = context.ItemPrices.Where(w => w.Duration > totalMin && w.ItemId == item.ItemId)
                                                .OrderBy(o => o.Duration).FirstOrDefault();

                                if (nextPrice != null)
                                {
                                    nextPrice.Price
                                }
                            }
                            else
                            {
                                item.TotalPrice = (int)((int)totalMin * item.Price);
                            }
                        }
                    }

                    item.DurationStr = item.IsUnlimited ? L("IsUnlimited") : item.Duration.ToString();
                }

                var output = new PagedResultDto <TransactionDto>
                {
                    TotalCount = count,
                    Items      = res
                };

                return(await Task.FromResult(output));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }