public async Task <AppTemplateConversionUnit> Insert(AppTemplateConversionUnit appTemplateConversionUnit)
        {
            await _unitOfWork.AppTemplateConversionUnitRepository.Add(appTemplateConversionUnit);

            await _unitOfWork.SaveChangesAsync();

            return(appTemplateConversionUnit);
        }
        public async Task <ApiResponse <bool> > DeleteAppTemplateConversionUnit(AppTemplateConversionUnitDeleteDto dto)
        {
            bool resultDto = true;

            Metadata metadata = new Metadata
            {
                IsValid = true,
                Message = ""
            };

            ApiResponse <bool> response = new ApiResponse <bool>(resultDto);

            try
            {
                AppTemplateConversionUnit appTemplateConversionUnit = await GetById(dto.Id);

                if (appTemplateConversionUnit == null)
                {
                    metadata.IsValid = false;

                    metadata.Message = "Template No existe!!!";

                    response.Meta = metadata;
                    response.Data = resultDto;

                    return(response);
                }



                resultDto = await Delete(dto.Id);

                metadata.IsValid = true;
                metadata.Message = $"Template Eliminado Satisfactoriamente!";

                response.Meta = metadata;
                response.Data = resultDto;

                return(response);
            }
            catch (Exception ex)
            {
                metadata.IsValid = false;

                metadata.Message = ex.InnerException.Message;

                response.Meta = metadata;
                response.Data = resultDto;

                return(response);
            }
        }
        public async Task <AppTemplateConversionUnit> Update(AppTemplateConversionUnit appTemplateConversionUnit)
        {
            var appTemplateConversionUnitFind = await GetById(appTemplateConversionUnit.Id);

            if (appTemplateConversionUnitFind == null)
            {
                throw new Exception("Documento No existe");
            }

            _unitOfWork.AppTemplateConversionUnitRepository.Update(appTemplateConversionUnit);
            await _unitOfWork.SaveChangesAsync();

            return(await GetById(appTemplateConversionUnit.Id));
        }
        public async Task <AppTemplateConversionUnit> CalculateFormulaGeneric(AppTemplateConversionUnit appTemplateConversionUnit, List <AppTemplateConversionUnit> template)
        {
            try
            {
                decimal result = 0;

                AppTemplateConversionUnit res = new AppTemplateConversionUnit();

                if (appTemplateConversionUnit == null)
                {
                    result = 0;
                }
                else
                {
                    string formula = await GetValueFormulaGeneric(appTemplateConversionUnit.Formula, template, appTemplateConversionUnit.Code);

                    DataTable dt        = new DataTable();
                    object    calculate = dt.Compute(formula, "");
                    string    a         = calculate.ToString();
                    result = Convert.ToDecimal(calculate.ToString());

                    appTemplateConversionUnit.Value        = result;
                    appTemplateConversionUnit.FormulaValue = formula;

                    res = appTemplateConversionUnit;
                }



                return(res);
            }
            catch (Exception ex)
            {
                string a = ex.Message;
                return(null);
            }
        }
        public async Task Delete(int id)
        {
            AppTemplateConversionUnit entity = await GetById(id);

            _context.AppTemplateConversionUnit.Remove(entity);
        }
 public void Update(AppTemplateConversionUnit entity)
 {
     _context.AppTemplateConversionUnit.Update(entity);
 }
 public async Task Add(AppTemplateConversionUnit entity)
 {
     await _context.AppTemplateConversionUnit.AddAsync(entity);
 }
        public async Task <ApiResponse <AppTemplateConversionUnitGenericGetDto> > CreateAppTemplateConversionUnit(AppTemplateConversionUnitCreateDto dto)
        {
            AppTemplateConversionUnitGenericGetDto resultDto = new AppTemplateConversionUnitGenericGetDto();

            Metadata metadata = new Metadata
            {
                IsValid = true,
                Message = ""
            };

            ApiResponse <AppTemplateConversionUnitGenericGetDto> response = new ApiResponse <AppTemplateConversionUnitGenericGetDto>(resultDto);


            try
            {
                AppVariables AppVariablesFind = await _appVariablesService.GetById(dto.AppVariableId);

                if (AppVariablesFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Variable no existe";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }


                AppUnits appUnitIdSinceFind = await _appUnitsService.GetById(dto.AppUnitIdSince);

                if (appUnitIdSinceFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Unidad de Medida desde no existe!!";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }

                AppUnits appUnitIdUntilFind = await _appUnitsService.GetById(dto.AppUnitIdUntil);

                if (appUnitIdUntilFind == null)
                {
                    metadata.IsValid = false;
                    metadata.Message = "Unidad de Medida hasta no existe!!";
                    response.Data    = resultDto;
                    response.Meta    = metadata;
                    return(response);
                }


                AppTemplateConversionUnit template = new AppTemplateConversionUnit();

                template.AppVariableId = dto.AppVariableId;


                template.Code           = AppVariablesFind.Code;
                template.Description    = AppVariablesFind.Description;
                template.Formula        = dto.Formula;
                template.Value          = dto.Value;
                template.SumValue       = dto.SumValue;
                template.OrderCalculate = dto.OrderCalculate;
                template.AppUnitIdSince = dto.AppUnitIdSince;
                template.AppUnitIdUntil = dto.AppUnitIdUntil;



                var inserted = await Insert(template);

                resultDto = _mapper.Map <AppTemplateConversionUnitGenericGetDto>(inserted);

                if (appUnitIdSinceFind != null)
                {
                    AppUnitsGetDto appUnitsGetDto = _mapper.Map <AppUnitsGetDto>(appUnitIdSinceFind);
                    resultDto.AppUnitIdSinceGetDto = appUnitsGetDto;
                }

                if (appUnitIdUntilFind != null)
                {
                    AppUnitsGetDto appUnitsGetDto = _mapper.Map <AppUnitsGetDto>(appUnitIdUntilFind);
                    resultDto.AppUnitIdUntilGetDto = appUnitsGetDto;
                }
                if (AppVariablesFind != null)
                {
                    AppVariablesGetDto appVariablesGetDto = _mapper.Map <AppVariablesGetDto>(AppVariablesFind);
                    resultDto.AppVariablesGetDto = appVariablesGetDto;
                }


                response.Data = resultDto;
                response.Meta = metadata;
                return(response);
            }
            catch (Exception ex)
            {
                metadata.IsValid = false;
                metadata.Message = ex.InnerException.Message;
                response.Data    = null;
                response.Meta    = metadata;
                return(response);
            }
        }