Exemplo n.º 1
0
        public async Task <JsonResult> Save(PeriodDTO data)
        {
            //Get the period type base template
            var periodType = (await clientPT.FindAsync(x => x.ID == data.PeriodTypeId,
                                                       SessionModel.CompanyID, new String[] { })).FirstOrDefault();

            //First update data of template period type
            periodType.PaymentDays          = data.PaymentDays;
            periodType.SeventhDays          = (data.SeventhDayPosition != (int)WeeklySeventhDay.None ? 1 : 0);
            periodType.SeventhDayPosition   = data.SeventhDayPosition.ToString();
            periodType.FortnightPaymentDays = data.FortnightPaymentDays;

            if (!data.IsEditing)
            {
                //Enable the first fiscal year
                var resultID = Guid.NewGuid();
                var year     = data.Year;

                var initialDate = data.InitialDate;
                var finalDate   = new DateTime(year, 12, 31);

                //create period (fiscal year)
                var Periods = new List <Period>
                {
                    new Period()
                    {
                        ID                   = resultID,
                        Active               = true,
                        company              = SessionModel.CompanyID,
                        Timestamp            = DateTime.UtcNow,
                        InstanceID           = SessionModel.InstanceID,
                        Description          = $"Periodo del Ejercicio {year}",
                        CreationDate         = DateTime.Now,
                        Name                 = $"{year}",
                        InitialDate          = initialDate,
                        FinalDate            = finalDate,
                        FiscalYear           = data.Year,
                        IsActualFiscalYear   = year == DateTime.Now.Year,
                        IsFiscalYearClosed   = false,
                        PeriodTypeID         = periodType.ID,
                        ExtraordinaryPeriod  = periodType.ExtraordinaryPeriod,
                        FortnightPaymentDays = periodType.FortnightPaymentDays,
                        MonthCalendarFixed   = periodType.MonthCalendarFixed,
                        PaymentDayPosition   = periodType.PaymentDayPosition,
                        PaymentDays          = periodType.PaymentDays,
                        PaymentPeriodicity   = periodType.PaymentPeriodicity,
                        PeriodTotalDays      = periodType.PeriodTotalDays,
                        StatusID             = 1,
                        SeventhDayPosition   = periodType.SeventhDayPosition,
                        SeventhDays          = periodType.SeventhDays
                    }
                };

                await clientPeriod.CreateAsync(Periods, SessionModel.CompanyID);

                var newPeriodDetails = await clientPD.FindAsync(x => x.PeriodID == resultID, SessionModel.CompanyID, new String[] { });

                for (int i = 0; i < newPeriodDetails.Count(); i++)
                {
                    var pd = newPeriodDetails[i];
                    if (pd.Number < data.CurrentPeriod)
                    {
                        pd.PeriodStatus = PeriodStatus.NA;
                    }
                    else if (pd.Number == data.CurrentPeriod)
                    {
                        pd.PeriodStatus = PeriodStatus.Calculating;
                    }
                    else if (pd.Number > data.CurrentPeriod)
                    {
                        pd.PeriodStatus = PeriodStatus.Open;
                    }
                }

                await clientPD.UpdateAsync(newPeriodDetails, SessionModel.CompanyID);
            }

            //Update period type
            await clientPT.UpdateAsync(new List <PeriodType>() { periodType }, SessionModel.CompanyID);

            //Recalculate
            await RecalculateAsync(periodType);

            return(Json("OK"));
        }