예제 #1
0
        public IActionResult Create(CreateSaleOrderViewModel model)
        {
            try
            {
                var result = ValidateCreate(model);
                if (result.Any())
                {
                    return(BadRequest(new
                    {
                        messages = result
                    }));
                }
                var order = CalculateOrder(model);

                order.CustomerAddress = model.customer_address;
                order.CustomerEmail   = model.customer_email;
                order.CustomerName    = model.customer_name;
                order.CustomerPhone   = model.customer_phone;
                order.ReceiverAddress = model.receiver_address;
                order.ReceiverName    = model.receiver_name;
                order.ReceiverPhone   = model.receiver_phone;
                order.ShipType        = model.ship_type;
                order.ShipDate        = model.ship_date;
                order.ShipTime        = model.ship_time;
                order.PaymentType     = model.payment_type;
                order.Message         = model.message;
                order.Note            = model.note;

                _context.SaleOrders.Add(order);
                _context.SaveChanges();

                var mailResult = Gmail.SendEmail("Có đơn hàng mới",
                                                 $"<p><b>{order.CustomerName}</b> đã đặt 1 đơn hàng</p>" +
                                                 $"<p>SĐT: <b>{order.CustomerPhone}</b></p>" +
                                                 $"<p>Email: <b>{order.CustomerEmail}</b></p>" +
                                                 $"<p><a href='{App.Instance.BaseAddress}admin/order/{order.Id}'>Xem chi tiết</a></p>",
                                                 App.Instance.OwnerMails);

                return(Ok(ToOrderResponse(order)));
            }
            catch (Exception e)
            {
                return(Error(new
                {
                    message = "Có lỗi xảy ra. Vui lòng thử lại.",
                    data = e,
                }));
            }
        }
예제 #2
0
        private IEnumerable <string> ValidateCreate(CreateSaleOrderViewModel model)
        {
            var listMess = new List <string>();

            if (string.IsNullOrWhiteSpace(model.customer_name))
            {
                listMess.Add("Thiếu tên người mua");
            }

            if (string.IsNullOrWhiteSpace(model.customer_phone) &&
                string.IsNullOrWhiteSpace(model.customer_email))
            {
                listMess.Add("Vui lòng để lại thông tin liên lạc");
            }
            if (string.IsNullOrWhiteSpace(model.customer_address))
            {
                listMess.Add("Thiếu địa chỉ người mua");
            }

            if (!string.IsNullOrWhiteSpace(model.receiver_name) ||
                !string.IsNullOrWhiteSpace(model.receiver_phone) ||
                !string.IsNullOrWhiteSpace(model.receiver_address))
            {
                if (string.IsNullOrEmpty(model.receiver_phone))
                {
                    listMess.Add("Thiếu số điện thoại người nhận");
                }
                if (string.IsNullOrEmpty(model.receiver_address))
                {
                    listMess.Add("Thiếu địa chỉ người nhận");
                }
            }

            if ((!model.payment_type.Equals("cod") && !model.payment_type.Equals("transfer")) ||
                (string.IsNullOrWhiteSpace(model.ship_type)) ||
                model.ship_date == null || string.IsNullOrWhiteSpace(model.ship_time))
            {
                listMess.Add("Có lỗi xảy ra");
            }

            var checkCart = ValidateCart(model);

            listMess.AddRange(checkCart);
            return(listMess);
        }