public void AtualizaOrdem(ML_Order o, Order oML, NSAADMEntities n1)
        {
            try
            {

                if (o.ML_FeedbackBuyer != null)
                {
                    o.ML_FeedbackBuyer.Load();
                }

                if (o.ML_FeedbackSeller != null)
                {
                    o.ML_FeedbackSeller.Load();
                }

                if (o.ML_Payment != null)
                {
                    o.ML_Payment.Load();
                }

                if (oML.status != null)
                {
                    o.status = oML.status;
                }

                if (oML.status_detail != null)
                {
                    o.status_detail = oML.status_detail.description;
                }

                AtualizaFeedBackBuyer(o, oML, n1);
                AtualizaFeedBackSeller(o, oML, n1);
                AtualizaShipping(o, oML, n1);
                ML_Payment p = o.ML_Payment.FirstOrDefault();
                if (p != null) AtualizaPagamento(p, oML.payments.First(), n1);

            }
            catch (Exception ex)
            {
                throw new Exception("Erro na rotina AtualizaOrdem(ML_Order o, Order oML)", ex);
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the ML_Order EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToML_Order(ML_Order mL_Order)
 {
     base.AddObject("ML_Order", mL_Order);
 }
 /// <summary>
 /// Create a new ML_Order object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 public static ML_Order CreateML_Order(global::System.Decimal id)
 {
     ML_Order mL_Order = new ML_Order();
     mL_Order.id = id;
     return mL_Order;
 }
 /// <summary>
 /// Inclui ou Altera uma ML_Order no banco de dados.
 /// </summary>
 /// <param name="o">Objeto ML_Order que vai ser inserido ou alterado</param>
 /// <param name="n">Objeto NSAADMEntities que vai ser usado para fazer a inclusão ou alteração.</param>
 public void GravaOrdem(ML_Order o, NSAADMEntities n)
 {
 }
        private void AtualizaShipping(ML_Order o, Order oML, NSAADMEntities n)
        {
            try
            {
                if (oML.shipping.id != null)
                {
                    o.ML_Shipping.Load();

                    ML_Shipping ship;

                    ship = o.ML_Shipping.FirstOrDefault();

                    if (ship == null)
                    {

                        ship = ConverteShipping(oML, n);
                        o.ML_Shipping.Add(ship);

                    }
                    else
                    {
                        ship.status = oML.shipping.status;
                        ship.shipment_type = oML.shipping.shipment_type;
                        ship.currency_id = oML.shipping.currency_id;
                        ship.cost = oML.shipping.cost;

                    }

                }
            }
            catch (Exception ex)
            {

                throw new Exception(String.Format("Erro na rotina AtualizaShipping. {0} oML.id = {1} {0}", Environment.NewLine, oML.id), ex);
            }
        }
        private void AtualizaFeedBackSeller(ML_Order o, Order oML, NSAADMEntities n)
        {
            try
            {
                if (o.ML_FeedbackSeller.Count > 0)
                {
                    if (RetornaFeedBackSeller(oML) != null)
                    {
                        ML_FeedbackSeller MLs = o.ML_FeedbackSeller.First();
                        Sale s = RetornaFeedBackSeller(oML);

                        MLs.date_created = s.date_created;
                        MLs.fulfilled = s.fulfilled.ToString();
                        MLs.rating = s.rating;
                        o.ML_FeedbackSeller.Add(MLs);
                    }
                }
                else
                {
                    if (RetornaFeedBackSeller(oML) != null)
                    {

                        ML_FeedbackSeller MLs1 = new ML_FeedbackSeller();
                        Sale s1 = RetornaFeedBackSeller(oML);

                        MLs1.date_created = s1.date_created;
                        MLs1.fulfilled = s1.fulfilled.ToString();
                        MLs1.rating = s1.rating;
                        MLs1.id_order = o.id;

                        o.ML_FeedbackSeller.Add(MLs1);
                    }
                }
            }
            catch (Exception ex)
            {

                throw new Exception("Erro na rotina AtualizaFeedBackSeller", ex);
            }
        }
        private void AtualizaFeedBackBuyer(ML_Order o, Order oML, NSAADMEntities n)
        {
            try
            {
                if (o.ML_FeedbackBuyer.Count > 0)
                {
                    if (RetornaFeedBackPurchase(oML) != null)
                    {
                        ML_FeedbackBuyer b = o.ML_FeedbackBuyer.First();
                        Purchase p = RetornaFeedBackPurchase(oML);

                        b.date_created = p.date_created;
                        b.fulfilled = p.fulfilled.ToString();
                        b.rating = p.rating;

                    }

                }
                else
                {
                    if (RetornaFeedBackPurchase(oML) != null)
                    {

                        ML_FeedbackBuyer b1 = new ML_FeedbackBuyer();
                        Purchase p1 = RetornaFeedBackPurchase(oML);

                        b1.date_created = p1.date_created;
                        b1.fulfilled = p1.fulfilled.ToString();
                        b1.rating = p1.rating;
                        b1.id_order = o.id;

                        n.ML_FeedbackBuyer.AddObject(b1);
                        o.ML_FeedbackBuyer.Add(b1);

                    }

                }
            }
            catch (Exception ex)
            {

                throw new Exception("Erro na rotina AtualizaFeedBackBuyer.", ex);
            }
        }
        public ML_Order ConverteOrdem(Order o, NSAADMEntities n)
        {
            try
            {

                ControlaUsuario ControlaUsu = new ControlaUsuario();
                ControlaItem ControlaIt = new ControlaItem();
                ControlaEndereco ControlEnd = new ControlaEndereco();
                ConstruirEF cf = new ConstruirEF();

                ML_Order ord = new ML_Order();

                //CONVERTENDO COMPRADOR E VENDEDOR
                ML_Usuario buy = ControlaUsu.RetonarUsuario(o.buyer.id, n);
                if (buy == null)
                {
                    buy = ConverteUsuario(o.buyer);
                }

                ML_Usuario sel = ControlaUsu.RetonarUsuario(o.seller.id, n);
                if (sel == null)
                {
                    sel = ConverteUsuario(o.seller);
                }

                ord.ML_Usuario = sel;
                ord.ML_Usuario1 = buy;

                //ITENS DA ORDEM
                ML_OrderItem oi;
                foreach (OrderItem item in o.order_items)
                {
                    oi = new ML_OrderItem();
                    oi.currency_id = item.currency_id;
                    oi.quantity = item.quantity;
                    oi.unit_price = item.unit_price;

                    ML_Item mitem = ControlaIt.RetonarItem(o.order_items[0].item.id, n);
                    if (mitem == null)
                    {
                        mitem = ConverteItem(o.order_items[0].item);
                    }

                    oi.ML_Item = mitem;
                    ord.ML_OrderItem.Add(oi);
                }

                ML_Payment pa;
                foreach (Payment p in o.payments)
                {
                    pa = new ML_Payment();
                    pa.currency_id = p.currency_id;
                    pa.date_created = p.date_created;
                    pa.date_last_updated = p.date_last_updated;
                    pa.status = p.status;
                    pa.transaction_amount = p.transaction_amount;

                    pa.id = p.id;

                    ord.ML_Payment.Add(pa);

                }

                if (o.feedback != null)
                {
                    if (o.feedback.purchase != null)
                    {
                        ML_FeedbackBuyer feeb = new ML_FeedbackBuyer();
                        feeb.date_created = o.feedback.purchase.date_created;
                        feeb.fulfilled = o.feedback.purchase.fulfilled.ToString();
                        feeb.rating = o.feedback.purchase.rating;
                        feeb.id_order = o.id;

                        ord.ML_FeedbackBuyer.Add(feeb);

                    }

                    if (o.feedback != null)
                    {
                        if (o.feedback.sale != null)
                        {
                            ML_FeedbackSeller fees = new ML_FeedbackSeller();
                            fees.date_created = o.feedback.sale.date_created;
                            fees.fulfilled = o.feedback.sale.fulfilled.ToString();
                            fees.rating = o.feedback.sale.rating;
                            fees.id_order = o.id;

                            ord.ML_FeedbackSeller.Add(fees);
                        }
                    }
                }

                ML_Shipping s = new ML_Shipping();
                s = ConverteShipping(o, n);
                if (s != null) ord.ML_Shipping.Add(s);

                //DADOS DA ORDEM
                ord.id = o.id;
                ord.currency_id = o.currency_id;
                ord.date_closed = o.date_closed;
                ord.date_created = o.date_created;
                ord.status = o.status;
                //ord.status_detail = o.status_detail.description;
                ord.total_amount = o.total_amount;

                return ord;

            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Erro na rotina de ConverteOrdem. OrdemID: {0}",o.id), ex);
            }
        }
        public Venda ConverteMLVendaEmVenda(ML_Order o, NSAADM_HMLEntities e)
        {
            String id;
            id = o.id.ToString();

            ML_Shipping mS = o.ML_Shipping.FirstOrDefault();
            ML_FeedbackSeller Fb = o.ML_FeedbackSeller.FirstOrDefault();

            Venda v = new Venda();

            v.data_venda = Convert.ToDateTime(o.date_created);
            v.valor_venda = (decimal)o.total_amount;
            v.valor_desconto = 0;
            v.data_final = Convert.ToDateTime(o.date_closed);

            if (Fb != null)
            {
                v.status = ConvertStatus(o.status, Fb.rating);
            }
            else
            {
                v.status = ConvertStatus(o.status, "");
            }

            if (mS != null)
            {
                v.valor_frete = Convert.ToDecimal(mS.cost);
            }

            v.id_ML = id;

            Cliente c = (from p in e.Clientes where p.idML == id select p).FirstOrDefault();
            if (c == null)
            {
                c = new Cliente();
                c.email = o.ML_Usuario1.email;
                c.nome = o.ML_Usuario1.first_name;
                c.idML = o.id.ToString();
                c.nicknName = o.ML_Usuario1.nickname;
                c.ultimoNome = o.ML_Usuario1.last_name;
            }
            v.Cliente = c;

            ML_OrderItem mo = o.ML_OrderItem.FirstOrDefault();
            Produto pr = (from a in e.Produtoes where a.Descr == mo.ML_Item.title select a).FirstOrDefault();
            if (pr == null)
            {
                pr = new Produto();
                pr.Descr = mo.ML_Item.title;
                pr.qtd = (decimal)mo.quantity;
            }

            VendaProduto vp = new VendaProduto();
            vp.Produto = pr;
            vp.Venda = v;
            vp.qtd = (decimal)mo.quantity;

            v.VendaProdutoes.Add(vp);

            return v;
        }