private Response <OrderLocal> GetOrder(OrderLocal local)
        {
            Response <OrderLocal> response = new Response <OrderLocal>();

            try
            {
                var apiGateway = new ApiGateway();
                var result     = apiGateway.InvokeApi <Order, Order>(string.Format(ConfigurationManager.AppSettings["BraspagUrlConsult"], local.Payment.PaymentId), null, "GET");
                var order      = result.Entity;
                if (result.StatusCode == StatusCode.OK)
                {
                    response.Entity          = local;
                    response.Entity.Customer = order.Customer;
                    response.Entity.Payment  = order.Payment;
                }
                else
                {
                    response.Entity     = null;
                    response.StatusCode = result.StatusCode;
                    response.Messages   = result.Messages;
                }
            }
            catch (Exception ex)
            {
                response.ReponseException(ex);
            }

            return(response);
        }
        public Response <OrderLocal> Save(OrderLocal local)
        {
            Response <OrderLocal> response = new Response <OrderLocal>();

            try
            {
                string id = DateTime.Now.ToString("yyyyMMddHHhhmm");
                local.Id      = id;
                local.DtOrder = DateTime.Now;

                var order = new Order()
                {
                    MerchantOrderId = id,
                    Customer        = local.Customer,
                    Payment         = local.Payment
                };

                var apiGateway = new ApiGateway();
                var result     = apiGateway.InvokeApi <Order, Order>(ConfigurationManager.AppSettings["BraspagUrlAuthor"], order, "POST");
                order = result.Entity;
                if (result.StatusCode == StatusCode.OK && result.Entity.Payment.ReasonCode == 0)
                {
                    local.Status             = OrderStatus.Criado;
                    local.Payment.PaymentId  = result.Entity.Payment.PaymentId;
                    local.Payment.CreditCard = null;
                    var rep = Resolver.Get <IOrderRepository>();
                    local           = rep.Add(local);
                    response.Entity = local;
                }
                else
                {
                    response.Messages.Add(result.Entity.Payment.ReasonMessage);
                    response.StatusCode = StatusCode.InternalServerError;
                }
            }
            catch (Exception ex)
            {
                response.ReponseException(ex);
                //cancelar Transacao
                if (!string.IsNullOrEmpty(local.Payment.PaymentId))
                {
                    string paymentId    = local.Payment.PaymentId;
                    int    amount       = local.Payment.Amount;
                    var    resultCancel = new ApiGateway().InvokeApi <Order, CancelOrder>(string.Format(ConfigurationManager.AppSettings["BraspagUrlCancel"], paymentId, amount), null, "PUT");
                }
            }
            return(response);
        }
Пример #3
0
        //[ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Status,Address,Instructions,PickedBy,DeliveryCost,Comments")] OrderLocal order)
        {
            var response = OrderService.EditOrder_Admin(order);

            if (response.Status)
            {
                return(RedirectToAction("Index"));
            }
            ModelState.AddModelError(string.Empty, response.Error);
            var refMOdel = new EditOrderModel
            {
                Order       = OrderService.GetOrderDetails(order.Id.ToString()),
                OrderStatus = OrderHistoryEnu.GetAllOrderStatus(),
                Riders      = UserService.GetUsersByype("0")
            };

            return(View(refMOdel));
        }
Пример #4
0
        public static UpdateOrderResponse EditOrder_Admin(OrderLocal source)
        {
            var response = new UpdateOrderResponse {
            };

            using (var dbContext = new DeliversEntities())
            {
                var dbOrder = dbContext.Orders.FirstOrDefault(o => o.Id == source.Id);
                if (dbOrder != null)
                {
                    var canEditOrder = true;
                    #region EDIT
                    if (dbOrder.Status == OrderHistoryEnu.Deliverd.Value || dbOrder.Status == OrderHistoryEnu.PickedUp.Value)
                    {
                        canEditOrder = false;
                    }

                    #region STATUS
                    if (dbOrder.Status != source.Status)
                    {
                        var dbOrderStatus      = OrderService.GetOrderCurrentStatus(source.Id.ToString());
                        var updatedOrderStatus = OrderHistoryEnu.GetOrderStatus(source.Status);

                        if (updatedOrderStatus.Order - dbOrderStatus.Order > 1)
                        {
                            response.Status = false;
                            response.Error  = "Invalid order status";
                            return(response);
                        }

                        // UPDATE STATUS
                        ChangeOrderStatus(new ChangeOrderStatusRequesrModel {
                            OrderId   = source.Id.ToString(),
                            NewStatus = source.Status,
                            UserId    = null,
                            Comments  = source.Comments
                        });
                    }

                    #endregion

                    var newHist = new OrderHistory
                    {
                        OrderId   = dbOrder.Id,
                        DateTime  = CommonService.GetSystemTime(),
                        IsCurrent = false,
                        Status    = OrderHistoryEnu.OrderEdited.Value,
                        Comments  = source.Comments
                    };
                    var isAddHis = false;
                    var comm     = "";


                    if (!string.IsNullOrEmpty(source.Address) && source.Address.ToLower() != dbOrder.Address.ToLower())
                    {
                        if (canEditOrder)
                        {
                            comm            = "address changed from '" + dbOrder.Address + "' TO '" + source.Address + "' & ";
                            dbOrder.Address = source.Address;
                            isAddHis        = true;
                        }
                        else
                        {
                            response.Status = false;
                            response.Error  = "Can not change address in current status: " + dbOrder.Status;
                            return(response);
                        }
                    }
                    if (!string.IsNullOrEmpty(source.Instructions) && source.Instructions.ToLower() != dbOrder.Instructions.ToLower())
                    {
                        dbOrder.Instructions = source.Instructions;
                    }
                    if (!string.IsNullOrEmpty(source.PickedBy) && source.PickedBy.ToLower() != dbOrder.PickedBy.ToLower())
                    {
                        dbOrder.PickedBy = source.PickedBy;
                    }
                    if (source.DeliveryCost != dbOrder.DeliveryCost)
                    {
                        if (canEditOrder)
                        {
                            comm                 = comm + "delivery cost changed from " + dbOrder.DeliveryCost + " TO " + source.DeliveryCost + ".";
                            isAddHis             = true;
                            dbOrder.DeliveryCost = source.DeliveryCost;
                        }
                        else
                        {
                            response.Status = false;
                            response.Error  = "Can not change delivery cost in current status: " + dbOrder.Status;
                            return(response);
                        }
                    }

                    if (isAddHis)
                    {
                        newHist.Comments = comm;
                        dbContext.OrderHistories.Add(newHist);
                    }

                    dbContext.SaveChanges();
                    response.Status = true;
                    response.Error  = "";
                    return(response);

                    #endregion
                }
                else
                {
                    response.Status = false;
                    response.Error  = "Invalid order Id";
                    return(response);
                }
            }
        }