Beispiel #1
0
        public ActionResult Shipping(ShippingViewModel model)
        {
            try
            {
                var currentUser = GetAuthenticatedUser();
                var cartList    = _context.Cart.Where(x => x.UserId == currentUser.id).ToList();

                if (cartList.Count == 0)
                {
                    return(Error("سبد خرید شما خالی است."));
                }

                var addressList = _context.UserAddress.Where(x => x.StatusId == UserAddressStatus.Active.Id && x.UserId == currentUser.id).ToList();
                var address     = addressList.Single(x => x.Id == model.addressId);
                if (model.addressId > 0)
                {
                    if (addressList.SingleOrDefault(x => x.Id == model.addressId) == null)
                    {
                        return(Error("آدرس انتخاب شده صحیح نیست."));
                    }
                    else
                    {
                        addressList.ForEach(x => x.MainAddress = false);
                        address.MainAddress = true;
                    }
                }
                else
                {
                    return(Error("لطفا آدرس تحویل سفارش خود را مشخص کنید."));
                }

                if (model.deliveryType > 0)
                {
                    if (model.deliveryType == 1 && string.IsNullOrEmpty(model.time) && model.time.Split(' ').Length != 2)
                    {
                        return(Error("لطفا زمان تحویل سفارش خود را مشخص کنید."));
                    }
                }
                else
                {
                    return(Error("لطفا نحوه تحویل سفارش خود را مشخص کنید."));
                }

                Response couponResponse = null;
                Coupon   coupon         = null;
                if (!string.IsNullOrEmpty(model.coupon))
                {
                    couponResponse = CouponController.CheckCoupon(_context, currentUser, model.coupon, out coupon);
                    if (couponResponse.status != ResponseStatus.Ok)
                    {
                        return(Error(couponResponse.message));
                    }
                }

                var invoiceDetailList = new List <InvoiceDetail>();
                foreach (var item in cartList)
                {
                    var totalPrice    = item.Product.Price - item.Product.Discount;
                    var invoiceDetail = new InvoiceDetail()
                    {
                        ProductId        = item.ProductId,
                        ProductFeatureId = item.ProductFeatureId,
                        Count            = item.Count,
                        Price            = totalPrice,
                        StatusId         = InvoiceDetailStatus.Accepted.Id,
                        CreateUserId     = currentUser.id,
                        ModifyUserId     = currentUser.id,
                        CreateDate       = GetDatetime(),
                        ModifyDate       = GetDatetime(),
                        CreateIp         = GetCurrentIp(),
                        ModifyIp         = GetCurrentIp()
                    };
                    invoiceDetailList.Add(invoiceDetail);
                }

                var price = invoiceDetailList.Sum(x => x.Count * x.Price);

                if (coupon != null)
                {
                    if (coupon.TypeId == CouponType.Amount.Id)
                    {
                        price = price - coupon.Value;
                    }
                    else if (coupon.TypeId == CouponType.Percentage.Id)
                    {
                        price = price - ((price / 100) * coupon.Value);
                    }

                    if (price < 0)
                    {
                        price = 0;
                    }
                }

                var keys             = new string[] { BaseInformationKey.OrderGapDay, BaseInformationKey.MinDeliveryFreePrice, BaseInformationKey.DeliveryPrice, BaseInformationKey.DeliveryManCity };
                var baseInformations = _context.BaseInformation.Where(x => keys.Any(y => y == x.Key));

                var GapDay = int.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.OrderGapDay)?.Value ?? AsefianMetadata.DefaultGapDay);
                var MinDeliveryFreePrice = long.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.MinDeliveryFreePrice)?.Value ?? AsefianMetadata.DefaultMinDeliveryFreePrice);
                var DeliveryPrice        = long.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.DeliveryPrice)?.Value ?? AsefianMetadata.DefaultDeliveryPrice);

                if (model.deliveryType == DeliveryType.DeliveryMan.Id)
                {
                    var deliveryManCity = baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.DeliveryManCity)?.Value.Split('-').Select(x => int.Parse(x)).ToArray() ?? new int[] { };
                    if (deliveryManCity.Count(x => x == address.CityId) == 0)
                    {
                        return(Error("برای آدرس مورد نظر شما امکان ارسال با پیک فراهم نیست."));
                    }
                }

                var deliveryPrice = DeliveryPrice;
                if (price >= MinDeliveryFreePrice)
                {
                    deliveryPrice = 0;
                }

                DateTime?deliveryDate = null;
                if (!string.IsNullOrEmpty(model.time))
                {
                    deliveryDate = DateUtility.GetDateTime(model.time.Split(' ')[0]);
                }

                if (deliveryDate != null && DateTime.Today.AddDays(GapDay) > deliveryDate)
                {
                    return(Error("تاریخ وارد شده برای تحویل سفارش صحیح نیست."));
                }

                var invoice = new Invoice()
                {
                    UserId          = currentUser.id,
                    InvoiceStatusId = InvoiceStatus.Registered.Id,
                    PaymentTypeId   = PaymentType.Unknown.Id,
                    InvoiceNo       = GetNewInvoiceNo(),
                    Price           = price,
                    UnpaidPrice     = price,
                    DeliveryPrice   = deliveryPrice,
                    DeliveryTypeId  = model.deliveryType,
                    AddressId       = model.addressId,
                    DeliveryDate    = deliveryDate,
                    DeliveryTime    = model.time?.Split(' ')[1],
                    CouponId        = coupon?.Id,
                    CreateUserId    = currentUser.id,
                    ModifyUserId    = currentUser.id,
                    CreateDate      = GetDatetime(),
                    ModifyDate      = GetDatetime(),
                    CreateIp        = GetCurrentIp(),
                    ModifyIp        = GetCurrentIp(),

                    DetailList = invoiceDetailList
                };

                _context.Cart.RemoveRange(cartList);
                _context.Invoice.Add(invoice);

                var invoiceLog = new InvoiceLog()
                {
                    Invoice      = invoice,
                    StatusId     = InvoiceStatus.Registered.Id,
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };
                _context.InvoiceLog.Add(invoiceLog);

                _context.SaveChanges();

                return(Success("سفارش شما با موفقیت ثبت شد.", new
                {
                    id = invoice.Id
                }));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }
Beispiel #2
0
        public ActionResult SubmitPayment(SubmitPaymentViewModel model)
        {
            var currentUser = GetAuthenticatedUser();

            if (model.paymentType <= 0)
            {
                TempData["SubmitPaymentError"] = "انتخاب نوع پرداخت اجباری است.";
                return(Redirect("/invoice/payment/" + model.id));
            }

            var invoice = _context.Invoice.Where(x => x.InvoiceStatusId == InvoiceStatus.Registered.Id && x.Id == model.id && x.UserId == currentUser.id).SingleOrDefault();

            if (invoice == null)
            {
                return(HttpNotFound());
            }

            if (invoice.InvoiceStatusId == InvoiceStatus.InProgress.Id)
            {
                return(Redirect("/invoice/detail/" + model.id));
            }

            if (invoice.CouponId != null && !string.IsNullOrEmpty(model.coupon))
            {
                Response couponResponse = null;

                couponResponse = CouponController.CheckCoupon(_context, currentUser, model.coupon, out Coupon coupon);
                if (couponResponse.status != ResponseStatus.Ok)
                {
                    TempData["SubmitPaymentError"] = "انتخاب نوع پرداخت اجباری است.";
                    return(Redirect("/invoice/payment/" + model.id));
                }

                if (coupon.TypeId == CouponType.Amount.Id)
                {
                    invoice.Price -= coupon.Value;
                }
                else if (coupon.TypeId == CouponType.Percentage.Id)
                {
                    invoice.Price -= ((invoice.Price / 100) * coupon.Value);
                }

                if (invoice.Price < 0)
                {
                    invoice.Price = 0;
                }

                invoice.CouponId = coupon.Id;
            }

            invoice.InvoiceStatusId = InvoiceStatus.InProgress.Id;
            invoice.PaymentTypeId   = model.paymentType;
            invoice.ModifyUserId    = currentUser.id;
            invoice.ModifyDate      = GetDatetime();
            invoice.ModifyIp        = GetCurrentIp();

            var invoiceLog = new InvoiceLog()
            {
                InvoiceId    = invoice.Id,
                StatusId     = InvoiceStatus.InProgress.Id,
                CreateUserId = currentUser.id,
                CreateDate   = GetDatetime(),
                CreateIp     = GetCurrentIp()
            };

            _context.InvoiceLog.Add(invoiceLog);

            _context.SaveChanges();

            return(Redirect("/invoice/detail/" + model.id));
        }