/// <summary> /// Acrescenta um item a uma composição /// </summary> /// <param name="item"></param> /// <param name="idComposicao"></param> public void ComposiçãoItemSave(CpuCoefGeral item, int idComposicao) { using (IDbConnection cnn = new MySqlConnection(LoadConnectionString())) { cnn.Execute("INSERT INTO ComposicaoItem (ComposicaoId, InsumoId, Quantidade) VALUES (@ComposicaoId, @InsumoId, @Quantidade)", new { ComposicaoId = idComposicao, InsumoId = item.Insumo.Id, item.Coeficiente }); item.IdCoeficiente = (int)cnn.ExecuteScalar("SELECT last_insert_rowid()"); } }
/// <summary> /// Carrega os insumos/composições salvos no orçamento /// </summary> /// <param name="revisao"></param> /// <returns></returns> public ObservableCollection <IInsumoGeral> InsumosOrcamentoLista(int revisao) { using (IDbConnection cnn = new MySqlConnection(LoadConnectionString())) { // Localiza todos os insumos da base do orçamento IEnumerable <InsumoGeral> insumos = cnn.Query <InsumoGeral>(@"SELECT ins.idOrcInsumos as id, ins.idRefInsumos as IdBaseReferencia, ins.Descricao as Descrição, ins.Unidade, ins.idTipoInsumos as Tipo, preco.Preco as ValorUnitario, ref.CodigoRef as CodigoRef FROM orcinsumos as ins INNER JOIN refprecos as preco ON ins.idRefInsumos = preco.idRefInsumos LEFT JOIN refinsumos as ref on ins.idRefInsumos = ref.idRefInsumos WHERE ins.idOrcRevisa = @Id AND ins.idTipoInsumos>0;", new { Id = revisao }); // Localiza todos os insumos da base do orçamento IEnumerable <CpuGeral> cpus = cnn.Query <CpuGeral>(@"SELECT ins.idOrcInsumos as id, ins.idRefInsumos as IdBaseReferencia, ins.Descricao as Descrição, ins.Unidade, ins.idTipoInsumos as Tipo, preco.Preco as ValorUnitario, ref.CodigoRef as CodigoRef FROM orcinsumos as ins INNER JOIN refprecos as preco ON ins.idRefInsumos = preco.idRefInsumos LEFT JOIN refinsumos as ref on ins.idRefInsumos = ref.idRefInsumos WHERE ins.idOrcRevisa = @Id AND ins.idTipoInsumos=0;", new { Id = revisao }); // Localiza os coeficientes das cpus IEnumerable <ItensCpuOrc> coefs = cnn.Query <ItensCpuOrc>(@"SELECT coef.* FROM orcinsumoscoeficientes as coef INNER JOIN orcinsumos as ins ON coef.idOrcInsumos = ins.idOrcInsumos WHERE idOrcRevisa = @Id;", new { Id = revisao }); List <IInsumoGeral> listaGeral = new List <IInsumoGeral>(); //listaGeral.; listaGeral.AddRange(insumos.ToList()); listaGeral.AddRange(cpus.ToList()); // Processa as cpus foreach (var item in listaGeral) { // Verifica se o item é do tipo cpu if (item.Tipo == 0) { List <ItensCpuOrc> coefsCpu = coefs.Where(x => x.idOrcInsumos == item.Id).ToList(); foreach (ItensCpuOrc coefCpu in coefsCpu) { CpuCoefGeral cpuCoefTemp = new CpuCoefGeral() { Coeficiente = coefCpu.Coeficiente, IdCoeficiente = coefCpu.idOrcInsumosCoeficientes, Insumo = listaGeral.Where(x => x.Id == coefCpu.idOrcInsumosItem).First() }; ((CpuGeral)item).Itens.Add(cpuCoefTemp); } } } return(new ObservableCollection <IInsumoGeral>(listaGeral)); } }
private void ExecuteEditarCpuAdicionarItem(object obj) { OrcamentoItem selecionado = ItemOrcamentoSelecionado as OrcamentoItem; if (selecionado.Item.GetType() == typeof(CpuGeral)) { Localizar LocalizarInsumo = new Localizar(); LocalizarInsumo.ShowDialog(); IInsumoGeral ItemSelecionado = ((LocalizarViewModel)LocalizarInsumo.DataContext).InsumoSelecionado; if (ItemSelecionado != null) { CpuCoefGeral NovoItem = new CpuCoefGeral(ItemSelecionado); ((CpuGeral)selecionado.Item).AdicionarItem(NovoItem); } } }