Exemple #1
0
        private async Task VerifyPeriod(AuthorizationByOverdraftParams authorizationByOverdraftParams,
                                        List <Overdraft> overdrafts)
        {
            var middlewareManagerPeriodDetail = new MiddlewareManager <PeriodDetail>(new BaseRecordManager <PeriodDetail>(),
                                                                                     new PeriodDetailValidator());

            //PeriodDetails
            var periodDetailIDs = overdrafts.Select(p => p.PeriodDetailID);
            var periodDetail    = (await middlewareManagerPeriodDetail.FindByExpressionAsync(p =>
                                                                                             periodDetailIDs.Contains(p.ID),
                                                                                             authorizationByOverdraftParams.IdentityWorkID,
                                                                                             new string[] { "Period" })).FirstOrDefault();

            if (periodDetail != null && periodDetail.PeriodStatus == PeriodStatus.Calculating)
            {
                if (periodDetail.PeriodFiscalYear == PeriodFiscalYear.Final)
                {
                    var middlewareManagerPeriod = new MiddlewareManager <Period>(new BaseRecordManager <Period>(),
                                                                                 new PeriodValidator());

                    var periodToCreate = periodDetail.Period;
                    periodToCreate.CreationDate       = DateTime.Now;
                    periodToCreate.InitialDate        = periodDetail.Period.InitialDate.AddYears(1);
                    periodToCreate.FinalDate          = periodDetail.Period.FinalDate.AddYears(1);
                    periodToCreate.FiscalYear         = periodDetail.Period.FiscalYear + 1;
                    periodToCreate.ID                 = Guid.NewGuid();
                    periodToCreate.user               = authorizationByOverdraftParams.user;
                    periodToCreate.IsActualFiscalYear = true;
                    periodToCreate.IsFiscalYearClosed = false;
                    periodToCreate.Timestamp          = DateTime.Now;
                    periodToCreate.PeriodDetails      = null;
                    periodToCreate.PeriodType         = null;

                    //Crea el siguiente periodo
                    await middlewareManagerPeriod.CreateAsync(new List <Period> {
                        periodToCreate
                    },
                                                              authorizationByOverdraftParams.IdentityWorkID);
                }
            }
            else
            {
                throw new CotorraException(102, "102", "El periodo que se intenta autorizar no está en status calculando.", null);
            }
        }
Exemple #2
0
        /// <summary>
        /// Payroll authorization by overdrafts
        /// </summary>
        /// <returns></returns>
        public async Task <List <Overdraft> > AuthorizationByOverdraftAsync(AuthorizationByOverdraftParams authorizationByOverdraftParams)
        {
            List <Overdraft> overdraftToCalculate = null;

            try
            {
                var middlewareManagerOverdraft = new MiddlewareManager <Overdraft>(new BaseRecordManager <Overdraft>(),
                                                                                   new OverdraftValidator());
                var overdrafts = await middlewareManagerOverdraft.FindByExpressionAsync(p =>
                                                                                        authorizationByOverdraftParams.OverdraftIDs.Contains(p.ID),
                                                                                        authorizationByOverdraftParams.IdentityWorkID);

                using (var connection = new SqlConnection(ConnectionManager.ConfigConnectionString))
                {
                    if (connection.State != ConnectionState.Open)
                    {
                        await connection.OpenAsync();
                    }

                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "AuthorizeByOverdrafts";

                        DataTable dtGuidList = new DataTable();
                        dtGuidList.Columns.Add("ID", typeof(string));
                        overdrafts.Select(p => p.ID).ForEach(p =>
                        {
                            dtGuidList.Rows.Add(p);
                        });
                        SqlParameter param = new SqlParameter("@OverdraftIDs", SqlDbType.Structured)
                        {
                            TypeName = "dbo.guidlisttabletype",
                            Value    = dtGuidList
                        };
                        command.Parameters.Add(param);
                        command.Parameters.AddWithValue("@InstanceId", authorizationByOverdraftParams.InstanceID);
                        command.Parameters.AddWithValue("@company", authorizationByOverdraftParams.IdentityWorkID);
                        command.Parameters.AddWithValue("@user", authorizationByOverdraftParams.user);

                        //Execute SP de autorización
                        await command.ExecuteNonQueryAsync();
                    }
                }

                //Encontrar los empleados para realizar el cálculo
                var employeeIds = overdrafts.Select(p => p.EmployeeID);

                //11. Calculation overdrafts (Fire and Forget)
                await new OverdraftCalculationManager().CalculationByEmployeesAsync(employeeIds,
                                                                                    authorizationByOverdraftParams.IdentityWorkID, authorizationByOverdraftParams.InstanceID,
                                                                                    authorizationByOverdraftParams.user);
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Ocurrió un error al autorizar la nómina: {ex.ToString()}");
                throw new CotorraException(70000, "70000", "Ocurrió un error al autorizar la nómina", ex);
            }


            return(overdraftToCalculate);
        }