public void OldCriarKARepresentante(MetadaUnidadeporTrimestre mMetadaUnidadeporTrimestre, List <Model.OrcamentoDetalhado> lstOrcamentoDetalhado)
        {
            var lstOrcamentoporSegmento = (from x in lstOrcamentoDetalhado
                                           group x by string.Format("{0}", x.Segmento.Id));

            foreach (var OrcaSegmento in lstOrcamentoporSegmento)
            {
                MetadaUnidadeporSegmento mMetadaUnidadeporSegmento;
                mMetadaUnidadeporSegmento = RepositoryService.MetadaUnidadeporSegmento.Obter(OrcaSegmento.First().Segmento.Id, mMetadaUnidadeporTrimestre.ID.Value);
                if (mMetadaUnidadeporSegmento == null)
                {
                    mMetadaUnidadeporSegmento      = new MetadaUnidadeporSegmento(RepositoryService.NomeDaOrganizacao, RepositoryService.IsOffline, RepositoryService.Provider);
                    mMetadaUnidadeporSegmento.ID   = Guid.NewGuid();
                    mMetadaUnidadeporSegmento.Nome = mMetadaUnidadeporTrimestre.Nome + " - " + OrcaSegmento.First().Segmento.Name;
                    mMetadaUnidadeporSegmento.UnidadedeNegocios = mMetadaUnidadeporTrimestre.UnidadedeNegocio;
                    mMetadaUnidadeporSegmento.Ano       = mMetadaUnidadeporTrimestre.Ano;
                    mMetadaUnidadeporSegmento.Trimestre = mMetadaUnidadeporTrimestre.Trimestre;
                    //mMetadaUnidadeporSegmento.Segmento = new Lookup(item.Segmento.Id, SDKore.Crm.Util.Utility.GetEntityName<Model.Segmento>());
                    mMetadaUnidadeporSegmento.Segmento         = new Lookup(OrcaSegmento.First().Segmento.Id, SDKore.Crm.Util.Utility.GetEntityName <Model.Segmento>());
                    mMetadaUnidadeporSegmento.MetaporTrimestre = new Lookup(mMetadaUnidadeporTrimestre.ID.Value, SDKore.Crm.Util.Utility.GetEntityName <Model.OrcamentodaUnidadeporTrimestre>());

                    RepositoryService.MetadaUnidadeporSegmento.Create(mMetadaUnidadeporSegmento);
                }

                ServiceMetadaUnidadeporFamilia.Criar(mMetadaUnidadeporTrimestre.MetadaUnidade.Id, mMetadaUnidadeporSegmento, OrcaSegmento.ToList(), OrcaSegmento.First().Segmento.Id);
            }
        }
        public void AtualizarFaturamentoDoSegmento(int ano, int trimestre)
        {
            List <MetadaUnidade> lstMetas = RepositoryService.MetadaUnidade.ListarMetas(ano);

            if (lstMetas.Count != 0)
            {
                DataTable dtMetasSegmento = RepositoryService.MetadaUnidadeporSegmento.ListarMetasSegmentoDW(ano, trimestre, lstMetas);

                foreach (DataRow item in dtMetasSegmento.Rows)
                {
                    UnidadeNegocio mUnidadeNegocio = RepositoryService.UnidadeNegocio.ObterPorChaveIntegracao(item["CD_Unidade_Negocio"].ToString());
                    Segmento       mSegmento       = RepositoryService.Segmento.ObterPor(item["cd_segmento"].ToString());

                    if (mUnidadeNegocio != null && mSegmento != null)
                    {
                        MetadaUnidadeporSegmento itemcapa = RepositoryService.MetadaUnidadeporSegmento.ObterMetasSegmento(mUnidadeNegocio.ID.Value, mSegmento.ID.Value, ano, trimestre, "itbc_metaporsegmentoid");
                        if (itemcapa != null)
                        {
                            itemcapa.MetaRealizada = item.Field <decimal>("vlr");
                            RepositoryService.MetadaUnidadeporSegmento.Update(itemcapa);
                        }
                    }
                }
            }
        }
        public void Criar(Guid metaunidadeId, MetadaUnidadeporSegmento mMetadaUnidadeporSegmento, List <Model.OrcamentoDetalhado> lstOrcamentoDetalhado, Guid SegmentoId)
        {
            var lstorcamentoporsegfamilia = (from x in lstOrcamentoDetalhado
                                             group x by string.Format("{0}/{1}", x.Segmento.Id, x.Familia.Id));

            foreach (var OrcaSegFamilia in lstorcamentoporsegfamilia)
            {
                MetadaUnidadeporFamilia mMetadaUnidadeporFamilia;
                mMetadaUnidadeporFamilia = RepositoryService.MetadaUnidadeporFamilia.Obter(OrcaSegFamilia.First().Familia.Id, mMetadaUnidadeporSegmento.ID.Value);
                if (mMetadaUnidadeporFamilia == null)
                {
                    mMetadaUnidadeporFamilia    = new MetadaUnidadeporFamilia(RepositoryService.NomeDaOrganizacao, RepositoryService.IsOffline, RepositoryService.Provider);
                    mMetadaUnidadeporFamilia.ID = Guid.NewGuid();
                    mMetadaUnidadeporFamilia.UnidadedeNegocio = mMetadaUnidadeporSegmento.UnidadedeNegocios;
                    mMetadaUnidadeporFamilia.Ano            = mMetadaUnidadeporSegmento.Ano;
                    mMetadaUnidadeporFamilia.Trimestre      = mMetadaUnidadeporSegmento.Trimestre;
                    mMetadaUnidadeporFamilia.Segmento       = new Lookup(OrcaSegFamilia.First().Segmento.Id, SDKore.Crm.Util.Utility.GetEntityName <Model.Segmento>());
                    mMetadaUnidadeporFamilia.Familia        = new Lookup(OrcaSegFamilia.First().Familia.Id, SDKore.Crm.Util.Utility.GetEntityName <Model.FamiliaProduto>());
                    mMetadaUnidadeporFamilia.MetadoSegmento = new Lookup(mMetadaUnidadeporSegmento.ID.Value, SDKore.Crm.Util.Utility.GetEntityName <Model.OrcamentodaUnidadeporSegmento>());
                    mMetadaUnidadeporFamilia.Nome           = (mMetadaUnidadeporSegmento.Nome + " - " + OrcaSegFamilia.First().Familia.Name).ToString().Length > 99 ?
                                                              (mMetadaUnidadeporSegmento.Nome + " - " + OrcaSegFamilia.First().Familia.Name).Substring(1, 99)
                        : (mMetadaUnidadeporSegmento.Nome + " - " + OrcaSegFamilia.First().Familia.Name);

                    RepositoryService.MetadaUnidadeporFamilia.Create(mMetadaUnidadeporFamilia);
                }

                ServiceMetadaUnidadeporSubfamilia.Criar(metaunidadeId, mMetadaUnidadeporFamilia, OrcaSegFamilia.ToList(), OrcaSegFamilia.First().Familia.Id);
            }
        }
        public List <MetadaUnidadeporSegmento> ListarValoresPorUnidadeNegocio(Guid unidadeNegocioId, int ano, Domain.Enum.OrcamentodaUnidade.Trimestres?trimestre = null)
        {
            var lista = new List <MetadaUnidadeporSegmento>();

            string fetch = @"<fetch aggregate='true' no-lock='true' >
                              <entity name='itbc_potencial_supervisorporsegmento' >
                                <attribute name='itbc_potencialplanejado' alias='valor_planejado' aggregate='sum' />
                                <attribute name='itbc_potencialrealizado' alias='valor_realizado' aggregate='sum' />
                                <attribute name='itbc_segmentoid' alias='segmento' groupby='true' />
                                <attribute name='itbc_trimestre' alias='trimestre' groupby='true' />
                                <filter type='and' >
                                  <condition attribute='itbc_unidadedenegociosid' operator='eq' value='{1}' />
                                  <condition attribute='itbc_ano' operator='eq' value='{0}' />
                                  {2}
                                </filter>
                              </entity>
                            </fetch>";

            string filterTrimestre = string.Empty;

            if (trimestre.HasValue)
            {
                filterTrimestre = string.Format(@"<condition attribute='itbc_trimestre' operator='eq' value='{0}' />", (int)trimestre.Value);
            }

            fetch = string.Format(fetch, ano, unidadeNegocioId, filterTrimestre);

            var retrieveMultiple = new RetrieveMultipleRequest()
            {
                Query = new FetchExpression(fetch)
            };

            EntityCollection collection = ((RetrieveMultipleResponse)this.Execute(retrieveMultiple)).EntityCollection;

            foreach (var item in collection.Entities)
            {
                var segmento = ((EntityReference)((AliasedValue)item.Attributes["segmento"]).Value);

                var potencial = new MetadaUnidadeporSegmento(OrganizationName, IsOffline, Provider)
                {
                    MetaPlanejada = ((Money)((AliasedValue)item.Attributes["valor_planejado"]).Value).Value,
                    MetaRealizada = ((Money)((AliasedValue)item.Attributes["valor_realizado"]).Value).Value,
                    Segmento      = new SDKore.DomainModel.Lookup(segmento.Id, segmento.Name, segmento.LogicalName),
                    Trimestre     = ((OptionSetValue)((AliasedValue)item.Attributes["trimestre"]).Value).Value
                };

                lista.Add(potencial);
            }

            return(lista);
        }