//------------------------------------------------  RETIRAR PRODUTO --------------------------------------
        //------------------------------------------------ ANULAR VENDA
        public void anularVenda(int cod_venda, bool isBalcao)
        {
            string []mesas =   mesasDaVenda(cod_venda);
              for (int i = 0; i < mesas.Length; i++)
            {
                new Banco().disponibilizarMesa(codMesaPelaDescricao(mesas[i]));
              }
            DataTable compl = new DataTable();
            DataTable conjCompleto = new DataTable();
            string completos = "select cod_completo from VendaCompleta where cod_venda = " + cod_venda;
            new NpgsqlDataAdapter(completos, Conectar()).Fill(conjCompleto);

            double valor = 0;
            for (int i = 0; i < conjCompleto.Rows.Count; i++)
            {
                string vlr = "select valorUnitarioCompleto from completo where cod_completo = " + Convert.ToInt32(conjCompleto.Rows[i].ItemArray.GetValue(0));
                DataTable vl = new DataTable();
                new NpgsqlDataAdapter(vlr, Conectar()).Fill(vl);
                double info = Convert.ToDouble(vl.Rows[0].ItemArray.GetValue(0));
                double quantidade = new BancoInformacao().quantidadeCompletaByCodigo(Convert.ToInt32(conjCompleto.Rows[i].ItemArray.GetValue(0)));
                valor+= ( info * quantidade);

                string completo = "update Completo set cancelado = true where cod_completo  = "
                    + Convert.ToInt32(conjCompleto.Rows[i].ItemArray.GetValue(0));

                DataTable dt = new DataTable();
                new NpgsqlDataAdapter(completo, Conectar()).Fill(dt);
            }
            double valorSomado;

            double comissao= new Banco().getValorComissao();
            if (new Banco().comissaoIsPct())
            {
                valorSomado = ((comissao / 100) *valor) + valor;
                valorSomado = ((comissao / 100) * valor);
            }
            else
                valorSomado = valor + comissao;

            string vendas = "update venda set subValor = " + new Tratamento().retornaValorEscrito(valor).Replace(',', '.')
                + ", cancelado = true, aberta = false, horario = current_time , valorComissao = "
                + new Tratamento().retornaValorEscrito(comissao).Replace(',', '.')
                + ", valorReal = " + new Tratamento().retornaValorEscrito(valorSomado).Replace(',', '.') + " where cod_venda = " + cod_venda;
            new NpgsqlDataAdapter(vendas, Conectar()).Fill(compl);
        }
        public void gerarComandaCozinha(Completa[] produto, string[] mesa, bool reimpresso)
        {
            try
            {
                this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("comanda", 304, 2000);
                printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
                Reporter mr = new Reporter();
                if (File.Exists(endereco))
                    File.Delete(endereco);

                mr.Output = endereco;
                mr.StartJob();
                //tamanho Interno 23

                Comanda cc = new Comanda(venda.cod_venda, true);
                string pont = "|---------------------|";
                int line = 1;
                //

                string cozinha = new Banco().carregaComandaCozinha();
                mr.PrintText(line++, 01, "|----" + cozinha + "----|");//13

                mr.PrintText(line, 01, "| Data : " + DateTime.Now.ToShortDateString()); mr.PrintText(line++, 23, "|");
                mr.PrintText(line, 01, "| Hora : " + DateTime.Now.ToShortTimeString()); mr.PrintText(line++, 23, "|");
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line, 01, "|"); mr.PrintText(line++, 23, "|");
                //produto
                int ii = produto.Length;

                while (ii-- > 0)
                {
                    if (reimpresso)
                    {
                        mr.PrintText(line, 1, "| - REIMPRESSAO "); mr.PrintText(line++, 23, "|");
                    }
                    mr.PrintText(line, 1, "| - " + produto[ii].segmentoImprimir); mr.PrintText(line++, 23, "|");

                    string tam = new BancoInformacao().tamanhoDescricaoByCodigo(produto[ii].produto[0].cod_tamanho);
                    if (tam.Length > 14) tam = tam.Substring(0, 14);
                    mr.PrintText(line, 1, "| Especif. - " + tam); mr.PrintText(line++, 23, "|");
                    for (int j = 0; j < produto[ii].produto.Length; j++)
                    {
                        string prod = "Item - " + new Banco().preencherNomeProdctAll(produto[ii].produto[j].cod_produto);
                        if (prod.Length > 16) prod = prod.Substring(0, 16);
                        mr.PrintText(line, 01, "| " + prod); mr.PrintText(line++, 23, "|");
                        mr.PrintText(line, 1, "| PRECO UNITARIO - " + produto[ii].valorUnitario); mr.PrintText(line++, 23, "|");
                        if (produto[ii].produto[j].porcentagem != 1)
                        {
                            mr.PrintText(line, 01, "|");
                            mr.PrintText(line, 6, " -- " + (produto[ii].produto[j].porcentagem * 100) + " %");
                            mr.PrintText(line++, 23, "|");
                        }
                    }
                    mr.PrintText(line, 1, "|Quantidade - " + produto[ii].quantidade.ToString("0.000")); mr.PrintText(line++, 23, "|");
                    mr.PrintText(line, 1, "|Valor Item - " + (produto[ii].valorUnitario * produto[ii].quantidade).ToString("0.00")); mr.PrintText(line++, 23, "|");
                    //   mr.PrintText(line, 01, "|"); mr.PrintText(line++, 17, "|");
                    string noticia = "";
                    int u = 0;
                    string nota = produto[ii].getNoticia(); string obs = "obs. "; int limite = 16;
                    while (u < nota.Length)
                    {
                        if (noticia.Length % limite == 0 && u >= limite)
                        {
                            mr.PrintText(line, 01, obs + noticia); mr.PrintText(line++, 23, "|"); noticia = ""; obs = ""; limite = 22;
                        }
                        noticia += produto[ii].getNoticia().Substring(u++, 1);
                    }
                    mr.PrintText(line, 01, "|" + noticia); mr.PrintText(line++, 23, "|"); noticia = "";
                    mr.PrintText(line++, 01, "| --  -   --   -  --  |");
                }
                //produto
                mr.PrintText(line++, 01, pont);
                if (produto[0].garconImprimir.Length > 12) produto[0].garconImprimir = produto[0].garconImprimir.Substring(0, 12);
                string garc = "| GARCOM -" + produto[0].garconImprimir;
                mr.PrintText(line, 01, garc); mr.PrintText(line++, 23, "|");

                mr.PrintText(line, 01, "| " + mesa[mesa.Length - 1]); mr.PrintText(line++, 23, "|");
                mr.PrintText(line++, 01, pont);

                mr.PrintJob();
                mr.EndJob();
                lerArquivo(new Banco().getCodImpressoraByTipo(new BancoConsulta().cod_tipoPeloNome(new Banco().segmentoDoProduto(produto[0].produto[0].cod_produto))));
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message+ "info add:  "+ ee.Data);
            }
        }
        public void novoMetodo(Completa[] produto, string[] mesa)
        {
            try
            {
                Reporter mr = new Reporter();
                EpsonCodes mer = new EpsonCodes();

                if (File.Exists(endereco))
                    File.Delete(endereco);

                mr.Output = endereco;
                mr.StartJob();
                Comanda cc = new Comanda(venda.cod_venda, true);
                string pont = "|---------------------------|";
                int line = 1;
                int limite = 29;
                mr.PrintText(line++, 01, "|----------COZINHA----------|");

                mr.PrintText(line, 01, "|Data:" + DateTime.Now.ToShortDateString()); mr.PrintText(line++, limite, "|");
                mr.PrintText(line, 01, "|Hora:" + DateTime.Now.ToShortTimeString()); mr.PrintText(line++, limite, "|");
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line, 01, "|"); mr.PrintText(line++, limite, "|");
                //produto
                int ii = produto.Length;

                while (ii-- > 0)
                {
                    mr.PrintText(line, 1, "| - " + produto[ii].segmentoImprimir); mr.PrintText(line++, limite, "|");
                    string tam = new BancoInformacao().tamanhoDescricaoByCodigo(produto[ii].produto[0].cod_tamanho);
                    if (tam.Length > 18) tam = tam.Substring(0, 18);
                    mr.PrintText(line, 1, "|Tamanho- " + tam); mr.PrintText(line++, limite, "|");
                    for (int j = 0; j < produto[ii].produto.Length; j++)
                    {
                        string prod = new Banco().preencherNomeProdctAll(produto[ii].produto[j].cod_produto);
                        if (prod.Length > 27) prod = prod.Substring(0, 27);
                        mr.PrintText(line, 01, "|" + prod); mr.PrintText(line++, limite, "|");
                        mr.PrintText(line, 01, "|"); mr.PrintText(line, 6, "--" + (produto[ii].produto[j].porcentagem * 100) + " %"); mr.PrintText(line++, limite, "|");
                    }

                    mr.PrintText(line, 1, "|Quantidade - " + produto[ii].quantidade); mr.PrintText(line++, limite, "|");
                    string noticia = "";
                    int u = 0;
                    string nota = produto[ii].getNoticia(); string obs = "obs. "; int limit = limite -7;
                    while (u < nota.Length)
                    {
                        if (noticia.Length % limit == 0 && u >= limit)
                        {
                            mr.PrintText(line, 01, obs + noticia); mr.PrintText(line++, limite, "|"); noticia = ""; obs = ""; limit = limite-1;
                        }
                        noticia += produto[ii].getNoticia().Substring(u++, 1);
                    }
                    mr.PrintText(line, 01, "|" + noticia); mr.PrintText(line++, limite, "|"); noticia = "";
                    mr.PrintText(line++, 01, "| -- - -- - --  |");
                }
                //produto
                mr.PrintText(line++, 01, pont);
                if (produto[0].garconImprimir.Length > 15) produto[0].garconImprimir = produto[0].garconImprimir.Substring(0, 15);
                string garc = "|GARCON -" + produto[0].garconImprimir;
                mr.PrintText(line, 01, garc); mr.PrintText(line++, limite, "|");

                mr.PrintText(line, 01, "| " + mesa[mesa.Length - 1]); mr.PrintText(line++, limite, "|");
                mr.PrintText(line++, 01, pont);

                mr.PrintJob();
                mr.EndJob();

                ImprimirBematech(endereco, new Banco().getCodImpressoraByTipo(new BancoConsulta().cod_tipoPeloNome(new Banco().segmentoDoProduto(produto[0].produto[0].cod_produto))));

            }
            catch { }
        }
        public void imprimirComanda(int cod_impressora, VendaFull vend )
        {
            try
            {

                impressora = new Banco().impressora(cod_impressora);

                   Comanda cc = new Comanda(vend.cod_venda);

                //
                string pont = "|----------------------------------------------|";
                string linha = "| ID da VENDA : " + vend.cod_venda; linha = linha.PadRight(47, ' '); linha += "|";
                string[] VETOR1 = new string[vend.Completos.Length];
                string[] VETOR2 = new string[vend.Completos.Length];

                   int ii = 0; int i = vend.Completos.Length;
                   while (ii < i)
                   {
                       string ID = (ii + 1).ToString("000");

                       string CODPRODUTO = "" + vend.Completos[ii].produto[0].cod_produto.ToString("000"); // 3
                       string PRODUTO  = new Banco().preencherNomeProdctAll(vend.Completos[ii].produto[0].cod_produto); // 3
                       if (vend.Completos[ii].produto.Length > 1) PRODUTO = "mista " + PRODUTO; //18

                       if(PRODUTO.Length > 18 )    PRODUTO = PRODUTO.Substring(0, 18);
                       else                        PRODUTO = PRODUTO.PadRight(18,' ');

                       string TAMANHO = new BancoInformacao().tamanhoDescricaoByCodigo(vend.Completos[ii].produto[0].cod_tamanho); // 9
                       if(TAMANHO.Length > 9 )    TAMANHO = TAMANHO.Substring(0, 9);
                       else                        TAMANHO = TAMANHO.PadRight(9,' ');
                       linha = "| "+ID+ " - "+ CODPRODUTO + "   , "+ PRODUTO + " | " + TAMANHO + " |"; //48
                       VETOR1[ii] = linha;

                       string QUANTIDADE =  vend.Completos[ii].quantidade.ToString("0.000").PadRight(6, ' ');//6

                       string UNITARIO = (vend.Completos[ii].valorUnitario).ToString("0.00").PadRight(8, ' ');//8
                       string VALORSOMADO = ( vend.Completos[ii].quantidade * vend.Completos[ii].valorUnitario).ToString("0.00");

                       linha = ("|    "+ QUANTIDADE + "   X   "+ UNITARIO+ "        = "+ VALORSOMADO+"").PadRight(47,' ')+ "|";//48
                       VETOR2[ii] = linha;
                       ii++;
                   }
                   string COMISSAO = "";
                   if (vend.valorComissao > 0)
                   {
                       COMISSAO = "COUVERT : " + String.Format("{0:C}", vend.valorComissao) + " |";
                       COMISSAO = "|" + COMISSAO.PadLeft(47, ' ');

                   }
                   string VALORCOMANDA;
                   VALORCOMANDA = "VALOR TOTAL : " + String.Format("{0:C}", vend.valorSomado) + " |";
                   VALORCOMANDA = "|" + VALORCOMANDA.PadLeft(47, ' ');

                   //rotina GARCON
                   List<String> garcons = new List<string>();
                   for (int k = 0; k < vend.Completos.Length; k++)
                       for (int l = 0; l < vend.Completos[k].garcons.Length; l++)
                       {
                           bool can = true;
                           for (int m = 0; m < garcons.Count; m++)
                               if (garcons[m] == vend.Completos[k].garcons[l].nome)
                                   can = false;
                           if (can) garcons.Add(vend.Completos[k].garcons[l].nome);
                       }

                   int n = 0; string GARCON = "| GARCOM - ";
                   while (n < garcons.Count)
                       GARCON += garcons[n++] + " - ";
                   if (GARCON.Length > 47) GARCON = GARCON.Substring(0, 47);
                   GARCON = GARCON.PadRight(47, ' ') + "|";
                   string MESA = "";
                   for (int l = 0; l < vend.mesa.Length && l < 7; l++)
                       MESA += "| " + vend.mesa[l] + " ";
                   MESA = MESA.PadRight(47, ' ') + "|";
                   string CAIXA = "| CAIXA : " + new BancoVenda().nomeVendedor(vend.cod_caixa);
                   CAIXA = CAIXA.PadRight(47, ' ') + "|";
                   string HORARIOVENDA = "| Abertura : " + vend.horario + "  - Final : " + DateTime.Now.ToShortTimeString();
                   string LIMPO = "|";
                   LIMPO = LIMPO.PadRight(47, ' ') + "|";
                   HORARIOVENDA = HORARIOVENDA.PadRight(47, ' ') + "|";
                   string SUBLINADA = "|";
                   SUBLINADA = SUBLINADA.PadRight(47, '_') + "|";

                   //IMRESSAO
                   MP2032.ConfiguraModeloImpressora(Convert.ToInt16(impressora.Rows[0].ItemArray.GetValue(2)));
                   MP2032.IniciaPorta(impressora.Rows[0].ItemArray.GetValue(3).ToString());//se for internet a porta é o IP

                  MP2032.AjustaLarguraPapel(48);
                   MP2032.ImprimeBmpEspecial(Application.StartupPath + "logo.bmp", -1, -1, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(cc.empresa, 0);
                   inserirLinhaImpressao(cc.telefone, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(cc.titulo, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao("| ID | CODIGO |      DESCRICAO     | CATEGORIA |", 0);
                   inserirLinhaImpressao("|    QTD Unid  X  VALOR Unit       = SUB-TOTAL |", 0);
                   inserirLinhaImpressao(pont, 0);
                   for (int h = 0; h < VETOR1.Length; h++)
                   {
                       inserirLinhaImpressao(VETOR1[h], 0);
                       inserirLinhaImpressao(VETOR2[h], 0);
                   }
                   inserirLinhaImpressao(pont, 0);
                   if(vend.valorComissao >0)
                       inserirLinhaImpressao(COMISSAO, 0);
                   //IMPRESSAO DESDE VALOR ATE O FIM
                   inserirLinhaImpressao(VALORCOMANDA, 1);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(GARCON, 0);
                   inserirLinhaImpressao(MESA, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(CAIXA, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(HORARIOVENDA, 0);
                   inserirLinhaImpressao(pont, 0);
                   inserirLinhaImpressao(LIMPO, 0);
                   inserirLinhaImpressao(cc.mensagem, 0);
                   inserirLinhaImpressao(LIMPO, 0);
                   inserirLinhaImpressao(cc.cidade, 0);
                   inserirLinhaImpressao(cc.progNome, 0);
                   inserirLinhaImpressao(cc.progTelefone, 0);
                   inserirLinhaImpressao(SUBLINADA, 0);
                MP2032.AcionaGuilhotina(1);
                MP2032.FechaPorta();

            }
               catch { }
        }
        public void gerarComandaNaoFiscal(string formaPagamento,double dinheiro, double troco)
        {
            int qtdLinha = 30 + (venda.Completos.Length * 2);
            qtdLinha *= 10;
            qtdLinha += 70;
            this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("comanda", 304, qtdLinha);
                        printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            try
            {
                Reporter mr = new Reporter();
                //quem vai colocar em italico e talz
                EpsonCodes mer = new EpsonCodes();
                if (File.Exists(endereco))
                    File.Delete(endereco);
                mr.Output = endereco;
                mr.StartJob();
                //--------------------------------------------------------------------------------
                Comanda cc = new Comanda(venda.cod_venda);
                string pont = "|-----------------------------------|";
                int line = 1; int i = 0; int ii = 0;
                mr.PrintText(line++, 02, cc.empresa);
                //o \n serve como paper feed na impressora...
                mr.PrintText(line++, 01, cc.telefone);
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line++, 01, cc.fixCenter("Cupom Nao Fiscal", 1));
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line, 01, "ID da VENDA : " + venda.cod_venda); mr.PrintText(line++, 37, "|");
                mr.PrintText(line++, 01, pont);
                //data e hora
                mr.PrintText(line++, 01,  cc.fixCenter("Data : "+DateTime.Now.ToShortDateString()+" Hora : "+DateTime.Now.ToShortTimeString(),1));
                mr.PrintText(line++, 01, pont);

                mr.PrintText(line++, 01, "| ID |COD |     DESCRICAO    |CATEG |");//id casa 1  - cod casa 7 -  desc - casa 12 - categ casa 32
                mr.PrintText(line++, 01, "|    QTD Un X VL Un   =  SUB-TOTAL  |");// qtd - casa 6  - X na casa 7 - val Uni - casa 8 - sub total casa 18
                mr.PrintText(line++, 01, pont);

                i = venda.Completos.Length;
                while (ii < i)
                {
                    mr.PrintText(line, 01, "|"); mr.PrintText(line, 02, "" + new Tratamento().makeId(ii + 1)); mr.PrintText(line, 06, "-");
                    mr.PrintText(line, 07, "" + venda.Completos[ii].produto[0].cod_produto); mr.PrintText(line, 11, "-");
                    string prod = new Banco().preencherNomeProdctAll(venda.Completos[ii].produto[0].cod_produto);
                    if (venda.Completos[ii].produto.Length > 1) prod = "mist " + prod;
                    if (prod.Length > 16) prod = prod.Substring(0, 16);
                    mr.PrintText(line, 12, prod);
                    mr.PrintText(line, 28, "-");
                    string tam = new BancoInformacao().tamanhoDescricaoByCodigo(venda.Completos[ii].produto[0].cod_tamanho);
                    if (tam.Length > 7) tam = tam.Substring(0, 7); mr.PrintText(line, 29, tam);
                    mr.PrintText(line++, 37, "|");

                    mr.PrintText(line, 01, "|"); mr.PrintText(line, 08, "" + venda.Completos[ii].quantidade); mr.PrintText(line, 11, "X");

                    mr.PrintText(line, 15, new Tratamento().retornaValorEscritoCo(venda.Completos[ii].valorUnitario));
                    mr.PrintText(line, 23, "=");

                    if ((venda.Completos[ii].valorUnitario * venda.Completos[ii].quantidade) >= 100)
                        mr.PrintText(line, 26, new Tratamento().retornaValorEscritoCo((venda.Completos[ii].valorUnitario * venda.Completos[ii++].quantidade)));
                    else
                        mr.PrintText(line, 25, new Tratamento().retornaValorEscritoCo((venda.Completos[ii].valorUnitario * venda.Completos[ii++].quantidade)));
                    mr.PrintText(line++, 37, "|");
                }
                mr.PrintText(line++, 01, pont);
                if (venda.valorComissao > 0)
                {
                    mr.PrintText(line++, 01, cc.fixRightPont("COUVERT : " + new Tratamento().retornaValorEscrito(venda.valorComissao), 1));

                }

                mr.PrintText(line++, 01, pont);
                                mr.PrintText(line++, 01,cc.fixCenter( "FORMA DE PAGAMENTO : "+formaPagamento, 1));

                mr.PrintText(line, 01, "|"); mr.PrintText(line, 18, ("TOTAL RS : " + new Tratamento().retornaValorEscrito(venda.valorSomado)));
                mr.PrintText(line++, 37, "|");
                mr.PrintText(line, 01, "|"); mr.PrintText(line, 18, ("DINHEIRO : " + new Tratamento().retornaValorEscrito(dinheiro)));
                mr.PrintText(line++, 37, "|");
                mr.PrintText(line, 01, "|"); mr.PrintText(line, 18, ("TROCO    : " + new Tratamento().retornaValorEscrito(troco)));
                mr.PrintText(line++, 37, "|");
                mr.PrintText(line++, 01, pont);
                //--------------------------------

                mr.PrintText(line, 01, "|"); mr.PrintText(line++, 37, "|");
                mr.PrintText(line++, 01, cc.mensagem);
                mr.PrintText(line, 01, "|"); mr.PrintText(line++, 37, "|");
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line++, 01, cc.cidade);
                mr.PrintText(line++, 01, pont);
                mr.PrintText(line++, 01, cc.progNome);
                mr.PrintText(line++, 01, cc.progTelefone);
                mr.PrintText(line++, 01, "|___________________________________|");

                mr.PrintJob();
                mr.EndJob();
                lerArquivo(1);

            }
            catch { }
        }
        public VendaFull carregaVenda(int cod_venda)
        {
            Completa[] conjCompleto;

            DataTable dttVenda = new DataTable();
            //--------CARREGAR MESA ------------------
            DataTable dttMesa = new DataTable();
            NpgsqlDataAdapter a = new NpgsqlDataAdapter
                    (" select m.descricao from mesa m " +
                    " inner join vendaMesa vm on (m.cod_mesa = vm.cod_mesa) " +
                    " inner join venda v on (v.cod_venda = vm.cod_venda) " +
                    " where  v.aberta = true and m.status = false and v.cod_venda = " + cod_venda, Conectar());
            a.Fill(dttMesa);
            string[] mesa = new string[dttMesa.Rows.Count];
            for (int j = 0; j < dttMesa.Rows.Count; j++)
                mesa[j] = dttMesa.Rows[j].ItemArray.GetValue(0).ToString();

            //-------------------------------------------------------------------------------------
            DataTable numero = new DataTable();
            string queryC = "select count(vc.cod_completo) from vendacompleta vc inner join completo c on (c.cod_completo = vc.cod_completo)"
                +" where cod_venda =" + cod_venda+" and c.cancelado  = false ";
            new NpgsqlDataAdapter(queryC, Conectar()).Fill(numero);

            conjCompleto = new Completa[Convert.ToInt32(numero.Rows[0].ItemArray.GetValue(0))];
            string queryComplet = "select c.cod_completo from completo c inner join vendaCompleta vc on (vc.cod_completo = c.cod_completo)" +
                                  " inner join venda v on (v.cod_venda = vc.cod_venda)" +
                                  " where v.cod_venda = " + cod_venda+ " and c.cancelado = false";
            numero = new DataTable();
            new NpgsqlDataAdapter(queryComplet, Conectar()).Fill(numero);

            for (int j = 0; j < conjCompleto.Length; j++)
            {
                //-----------GARCON DO PRODUTO ---------------------------------------------------------

                string garconA = "select g.cod_garcon,sum(quantidade) from garcon g inner join garconCompleto gc on(g.cod_garcon = gc.cod_garcon) " +
                    "inner join completo c on(c.cod_completo = gc.cod_completo) where c.cod_completo = "
                    + (int)numero.Rows[j].ItemArray.GetValue(0) +" and cancelado = false "
                    + " group by g.cod_garcon";
                DataTable garc = new DataTable();

                new NpgsqlDataAdapter(garconA, Conectar()).Fill(garc);

                string querySubProduto = "select p.cod_produto,cp.porcentagem , c.valorUnitarioCompleto ,cp.cod_tamanho  " +
            " from completo c inner join completoProduto cp on (c.cod_completo = cp.cod_completo)" +
            " inner join produto p on( p.cod_produto = cp.cod_produto) where c.cod_completo = " + (int)numero.Rows[j].ItemArray.GetValue(0)
            + " and c.cancelado = false ";
                DataTable sub = new DataTable();
                new NpgsqlDataAdapter(querySubProduto, Conectar()).Fill(sub);
                GarconFisico[] gf = new GarconFisico[garc.Rows.Count];
                for (int k = 0; k < garc.Rows.Count; k++)
                {
                    gf[k] = new GarconFisico();
                    gf[k].setGarcon(Convert.ToInt16(garc.Rows[k].ItemArray.GetValue(0)), Convert.ToInt16(garc.Rows[k].ItemArray.GetValue(1)));
                }

                Produto[] subProdutos = new Produto[sub.Rows.Count];
                for (int k = 0; k < sub.Rows.Count; k++)
                {
                    double teste = new BancoInformacao().quantidadeCompletaByCodigo
                        ( Convert.ToInt32( numero.Rows[j].ItemArray.GetValue(0).ToString()));
                    subProdutos[k] = new Produto();
                    subProdutos[k].setLoad(
                        Convert.ToInt32(sub.Rows[k].ItemArray.GetValue(0)),
                        Convert.ToDouble(sub.Rows[k].ItemArray.GetValue(1)),
                        Convert.ToDouble(sub.Rows[k].ItemArray.GetValue(2)),
                        Convert.ToInt32(sub.Rows[k].ItemArray.GetValue(3)),
                        //preciso fazer saber a quantidade de produtos

                        new Banco().isImpressoProduto(Convert.ToInt16(sub.Rows[k].ItemArray.GetValue(0)))
                        );

                }
                conjCompleto[j] = new Completa(subProdutos,
                    (int)numero.Rows[j].ItemArray.GetValue(0),gf, valorItemCompleto((int)numero.Rows[j].ItemArray.GetValue(0)));
            }
            //---------------------------------------------------------------
            string query = "select subValor, dataVenda,to_char(horario, 'HH24:MI'), cod_caixa, aberta,impressao, cod_pagamento,  valorReal from venda where cod_venda =" + cod_venda;
            numero = new DataTable();
            new NpgsqlDataAdapter(query, Conectar()).Fill(numero);

            double valor = 0;
            for (int i = 0; i < conjCompleto.Length; i++)
            {
                string vlr = "select valorUnitarioCompleto from completo where cod_completo = "
                    + (conjCompleto[i].cod_completo);
                DataTable vl = new DataTable();
                new NpgsqlDataAdapter(vlr, Conectar()).Fill(vl);
                double info = Convert.ToDouble(vl.Rows[0].ItemArray.GetValue(0));
                double quantidade = new BancoInformacao().quantidadeCompletaByCodigo( conjCompleto[i].cod_completo);
                valor += (info * quantidade);

            }

            try
            {
                VendaFull saida =
                    new VendaFull(
                        conjCompleto,
                        cod_venda,
                        (int)numero.Rows[0].ItemArray.GetValue(3),
                        valor,
                    numero.Rows[0].ItemArray.GetValue(2).ToString(),
                    numero.Rows[0].ItemArray.GetValue(1).ToString(),
                    mesa,new Banco().comissaoIsPct(),
                    new Banco().getValorComissao(),
                    (double)numero.Rows[0].ItemArray.GetValue(7));

                saida.super = retornoSuper(cod_venda);
                return saida;
            }
            catch
            {
                VendaFull saida = new VendaFull(conjCompleto, cod_venda, 1, 0, null, null, mesa
                    , new Banco().comissaoIsPct(), new Banco().getValorComissao(), (double)numero.Rows[0].ItemArray.GetValue(7));
                saida.super = retornoSuper(cod_venda);
                return saida;
            }
        }
        private void btEscolhaProduto_Click(object sender, EventArgs e)
        {
            try
            {
                int cod_tamanho = new Banco().cod_tamanhoDoEscolhido(Convert.ToInt16(mtCodigo.Text), cbTamanho.Text);//cod_tamanho
                int [] conjuntoCodProduto = new int[qtdProdutos()];//codigos dos produtos
                conjuntoCodProduto = preencherCodigosProduto(conjuntoCodProduto);
                if(!codigosAceitos(conjuntoCodProduto))
                    return;
                //garantido que os codigos são aceitos
                double valor = Convert.ToDouble(mtValor.Text);//valor de entrada do produto

                try
                {
                    if (mesas.Equals(null)) { }
                }
                catch
                {
                    mesas = new string[1];
                    mesas[0] = new Banco().trazerNomeMesa(cod_venda);
                }//mesas
                int Xcod_garcon;
                if (new Banco().isVendaBalcao(cod_venda))                    Xcod_garcon = new Banco().cod_garconBalcao();
                else                     Xcod_garcon = new Banco().codGarconByNome(getGarcon());
                //garcons preenchidos
                double XquantidadeProduto = Convert.ToDouble( tbQuantidade.Text.Replace('.',',') );// quantidade do produto inserido

                Produto []produto = new Produto[conjuntoCodProduto.Length];
                double [] porcentagem = new Banco().valorPctByDescricao(cbMista.Text);
                for (int i = 0; i < conjuntoCodProduto.Length;i++ )
                {
                    produto[i] = new Produto();

                    produto[i].setLoad(conjuntoCodProduto[i] ,porcentagem[i] ,valor
                        ,cod_tamanho,new Banco().isImpressoProduto(conjuntoCodProduto[i]))  ;
                }
                int cod_completo = 1;

                if ((produto.Length==1) && (new BancoInformacao().existeProdutoNaVenda(cod_venda,produto[0].cod_produto,cod_tamanho,valor)))
                {
                    cod_completo = new BancoInformacao().codCompletoExistente(cod_venda, produto[0].cod_produto, cod_tamanho, valor);
                    new BancoInformacao().addqtdGarconCompleto(Xcod_garcon, cod_completo, XquantidadeProduto);
                }
                else {
                cod_completo =    new BancoInformacao().criarCompleto(cod_venda, Xcod_garcon, valor
                            , XquantidadeProduto, produto[0].impresso, false, produto);
                    //aqui ele deve criar um novo produto
                }

                Completa cc = new BancoVenda().getCompleta(cod_completo, false);
                cc.quantidade = XquantidadeProduto;
                cc.garconImprimir = getGarcon();
                new Banco().inserePrecoCompraEmCompleto(cod_completo, new Banco().valorCompraDoProduto(cc.produto[0].cod_produto, cc.produto[0].cod_tamanho));
                cc.segmentoImprimir = new Banco().segmentoDoProduto(Convert.ToInt16( mtCodigo.Text));
                if (tbNoticia.TextLength > 0) cc.setNoticia(tbNoticia.Text);
                if (cc.needImpress)
                {
                    listaProd[new BancoVenda().codSegmentoByProduto(cc.produto[0].cod_produto) - 1].Add(cc);
                    bool can = true;
                    for (int y = 0; y < conjGarc.ToArray().Length && can; y++)
                        if (conjGarc.ToArray()[y] == getGarcon())
                            can = !can;
                    if (can)
                        conjGarc.Add(getGarcon());
                }

               // if(MessageBox.Show("DESEJA ACRESCENTAR OUTRO ITEM","INFORMAÇÂO",
               //     MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1
               //     ,MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
            //    if (MessageBox.Show("ACRESCENTAR OUTRO ITEM??", "CONFIRME SUA OPCAO", MessageBoxButtons.YesNo) == DialogResult.Yes)
                MessageBox.Show("PRODUTO INSERIDO", "CONFIRMAÇÃO", MessageBoxButtons.OK,MessageBoxIcon.Information);
                {
                    limpaCampoCodigoProduto(); this.Focus(); mtCodigo.Visible = true; mtCodigo.Focus();
                    tbQuantidade.Text = 1.ToString("0.00");
                    this.mtCodigo_KeyPress(sender,new KeyPressEventArgs ('\r'));
                }

                    if (!(new Banco().isVendaBalcao(cod_venda)))
                    {
                        for (int j = 0; j < listaProd.Length; j++)
                            if (listaProd[j].Count > 0)
                            {
                                new Impressao(new BancoVenda().carregaVenda(cod_venda)).gerarComandaCozinha(listaProd[j].ToArray(), mesas, false);
                             //   new Impressao(true, new BancoVenda().carregaVenda(cod_venda)).novoMetodo(listaProd[j].ToArray(), mesas);
                            }
                    }
                    else
                    {
                        try
                        {
                            VendaFull f = (new BancoVenda().carregaVenda(cod_venda));
                            Pagamento rec = new Pagamento(f.cod_venda,f.subValor,f.valorComissao, f.valorSomado, f.mesa, true);
                            this.Visible = false;
                            rec.ShowDialog();
                            if (rec.encerrou)
                            {
                                for (int j = 0; j < listaProd.Length; j++)
                                    if (listaProd[j].Count > 0)
                                    {
                                       // new Impressao(new BancoVenda().carregaVenda(cod_venda)).gerarComandaInterna(listaProd[j].ToArray(), mesas);
                                        new Impressao( new BancoVenda().carregaVenda(cod_venda)).gerarComandaCozinha(listaProd[j].ToArray(), mesas,false);
                                    }

                                MessageBox.Show("VENDA REALIZADA COM SUCESSO", "ATENDIMENDO BALCAO");

                            }
                            else
                                new BancoVenda().anularVenda(cod_venda, true);

                            this.Visible = true;

                        }    catch { }
                    } this.Close();

            }
            catch { };
        }