public InvoiceSetupScheduleDto Map(InvoiceSetupScheduleInfoApiModel invoiceSetupScheduleApiModel)
        {
            InvoiceSetupScheduleDto invoiceSetupScheduleDto = new InvoiceSetupScheduleDto();

            invoiceSetupScheduleDto.StartDate   = invoiceSetupScheduleApiModel.StartDate.ToDateTime().Value;
            invoiceSetupScheduleDto.EndDate     = invoiceSetupScheduleApiModel.EndDate.ToDateTime().Value;
            invoiceSetupScheduleDto.PaymentDays = invoiceSetupScheduleApiModel.PaymentDays;
            invoiceSetupScheduleDto.Frequency   = invoiceSetupScheduleApiModel.Frequency;
            return(invoiceSetupScheduleDto);
        }
        public List <InvoiceSetup> Create(Guid invoiceSetupId, InvoiceSetupScheduleDto invoiceSetupScheduleDto)
        {
            var invoiceSetupList = _faciTechDbContext.InvoiceSetup
                                   .Include(e => e.InvoiceSetupItems)
                                   .Where(e => e.Id == invoiceSetupId || e.ParentInvoiceSetupId == invoiceSetupId)
                                   .ToList();
            var parentInvoiceSetup = invoiceSetupList
                                     .Where(e => e.Id == invoiceSetupId)
                                     .FirstOrDefault();

            //Ignore the past one
            var existingchildInvoiceSetupList = invoiceSetupList
                                                .Where(e => e.ParentInvoiceSetupId == invoiceSetupId && e.InvoiceDate > DateTime.Today)
                                                .ToList();

            var childInvoiceSetupList = PrepareChildInvoiceSetup(invoiceSetupScheduleDto, parentInvoiceSetup);


            var childIdList = existingchildInvoiceSetupList.Select(e => e.Id).ToList();

            _faciTechDbContext.Schedule.Where(e => childIdList.Contains(e.Id));

            //Remove
            foreach (var existingchildInvoiceSetup in existingchildInvoiceSetupList)
            {
                _faciTechDbContext.InvoiceSetup.Remove(existingchildInvoiceSetup);
                foreach (var item in existingchildInvoiceSetup.InvoiceSetupItems)
                {
                    _faciTechDbContext.InvoiceSetupItem.Remove(item);
                }
            }
            //Add
            foreach (var newInvoiceSetupSchedule in childInvoiceSetupList)
            {
                if (newInvoiceSetupSchedule.InvoiceDate > DateTime.Today)
                {
                    _faciTechDbContext.InvoiceSetup.Add(newInvoiceSetupSchedule);
                    _faciTechDbContext.Schedule.Add(new Schedule()
                    {
                        Id              = Guid.NewGuid(),
                        Date            = newInvoiceSetupSchedule.InvoiceDate.Value,
                        ExecutionStatus = 0,
                        ScheduleTarget  = "InvoiceSchedule",
                        ScheduledItemId = newInvoiceSetupSchedule.Id
                    });
                }
            }
            _faciTechDbContext.SaveChanges();
            var schedule = Get(invoiceSetupId);

            return(schedule);
        }
        private List <InvoiceSetup> PrepareChildInvoiceSetup(InvoiceSetupScheduleDto invoiceSetupScheduleDto, InvoiceSetup parentInvoiceSetup)
        {
            List <InvoiceSetup> childInvoiceSetupList = new List <InvoiceSetup>();
            DateTime            scheduleDate          = invoiceSetupScheduleDto.StartDate;

            while (scheduleDate <= invoiceSetupScheduleDto.EndDate)
            {
                InvoiceSetup childInvoiceSetup = new InvoiceSetup();
                childInvoiceSetup.Id                   = Guid.NewGuid();
                childInvoiceSetup.InvoiceDate          = scheduleDate;
                childInvoiceSetup.ParentInvoiceSetupId = parentInvoiceSetup.Id;
                childInvoiceSetup.PayByDate            = scheduleDate.AddDays(invoiceSetupScheduleDto.PaymentDays);
                childInvoiceSetup.CommunityId          = parentInvoiceSetup.CommunityId;

                childInvoiceSetupList.Add(childInvoiceSetup);

                var computeFunction = frequencyRepository.Get(invoiceSetupScheduleDto.Frequency).Item3;
                scheduleDate = computeFunction(scheduleDate);
            }
            return(childInvoiceSetupList);
        }