Exemple #1
0
        private void bbDownload_Click(object sender, EventArgs e)
        {
            if (cbLoja.SelectedItem == null)
            {
                MessageBox.Show("Obrigatório selecionar loja.", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbLoja.Focus();
                return;
            }
            if (string.IsNullOrEmpty((cbLoja.SelectedItem as TRegistro_LojaVirtual).UserName))
            {
                MessageBox.Show("Não existe USERNAME cadastrado para loja.", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty((cbLoja.SelectedItem as TRegistro_LojaVirtual).ApiKey))
            {
                MessageBox.Show("Não existe APIKEY cadastrado para loja.", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MagentoService api    = new MagentoService();
            string         sessao = string.Empty;

            try
            {
                sessao = api.login((cbLoja.SelectedItem as TRegistro_LojaVirtual).UserName, (cbLoja.SelectedItem as TRegistro_LojaVirtual).ApiKey);
                filters         mf  = new filters();
                complexFilter[] cpf = new complexFilter[0];
                if (cbStatus.SelectedIndex > 0)
                {
                    complexFilter mcpf = new complexFilter();
                    mcpf.key = "status";
                    associativeEntity mas = new associativeEntity();
                    mas.key    = "=";
                    mas.value  = cbStatus.SelectedValue.ToString();
                    mcpf.value = mas;
                    Array.Resize(ref cpf, cpf.Length + 1);
                    cpf[cpf.Length - 1] = mcpf;
                }
                if (!string.IsNullOrEmpty(dt_ini.Text.SoNumero()))
                {
                    complexFilter mcpf = new complexFilter();
                    mcpf.key = "created_at";
                    associativeEntity mas = new associativeEntity();
                    mas.key    = "from";
                    mas.value  = DateTime.Parse(dt_ini.Text).ToString("yyyy-MM-dd") + " 00:00:00";
                    mcpf.value = mas;
                    Array.Resize(ref cpf, cpf.Length + 1);
                    cpf[cpf.Length - 1] = mcpf;
                }
                mf.complex_filter = cpf;
                salesOrderListEntity[] lista = api.salesOrderList(sessao, mf);
                if (lista.Length > 0)
                {
                    TList_Pedido lPedido = new TList_Pedido();
                    lista.ToList().ForEach(p =>
                    {
                        object obj = new TCD_Pedido().BuscarEscalar(
                            new TpBusca[]
                        {
                            new TpBusca()
                            {
                                vNM_Campo = "a.Nr_PedidoOrigem",
                                vOperador = "=",
                                vVL_Busca = "'" + p.increment_id.Trim() + "'"
                            }
                        }, "a.nr_pedido");
                        lPedido.Add(new TRegistro_Pedido()
                        {
                            Nr_PedidoOrigem = p.increment_id,
                            Vl_totalpedido  = decimal.Divide(int.Parse(p.grand_total.SoNumero()), 10000),
                            StatusMagento   = p.status,
                            NM_Clifor       = p.firstname.Trim() + " " + p.lastname.Trim(),
                            Nr_pedido       = obj == null ? 0 : decimal.Parse(obj.ToString()),
                            DT_Pedido       = DateTime.Parse(p.created_at)
                        });
                    });
                    bsPedLoja.DataSource = lPedido;
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message.Trim(), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            finally
            {
                if (!string.IsNullOrWhiteSpace(sessao))
                {
                    api.endSession(sessao);
                }
            }
        }