public void updateSellerBalance(double sellerCost)
        {
            string         UNameId        = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault();
            PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.UserNameId == UNameId).FirstOrDefault();

            paymentBalance.Balance = paymentBalance.Balance - sellerCost;
        }
 public IActionResult ApproveStatus(int[] Ids)
 {
     foreach (int Id in Ids)
     {
         PaymentHistory paymentHistory = _unitOfWork.PaymentHistory.GetAll().Where(a => a.Id == Id).FirstOrDefault();
         paymentHistory.Status = SD.PaymentStatusApproved;
         PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.UserNameId == paymentHistory.UserNameId).FirstOrDefault();
         paymentBalance.Balance = paymentBalance.Balance + paymentHistory.Amount;
         _unitOfWork.Save();
     }
     return(View());
 }
        public IActionResult AddOrdersManually(OrderVM orderVM)
        {
            string uNameId = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault();

            ViewBag.uNameId = uNameId;
            PaymentBalance paymentBalance = userBalance(uNameId);
            OrderVM        orderVM2       = new OrderVM()
            {
                Orders      = new Order(),
                ProductList = _unitOfWork.Product.GetAll().Where(a => a.AvailableForSellers && !a.OOSForSellers).
                              OrderBy(a => a.ProductName).Select(i => new SelectListItem
                {
                    Text  = i.ProductName,
                    Value = i.Id.ToString()
                }),
                StoresList = _unitOfWork.UserStoreName.GetAll().Where(q => q.UserNameId == uNameId).Select(i => new SelectListItem
                {
                    Text  = i.StoreName,
                    Value = i.Id.ToString()
                }),
                StatesList = SD.States
            };

            if (ModelState.IsValid)
            {
                orderVM.Orders.Cost = returnCost(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                ViewBag.ShowMsg     = 1;
                if (!paymentBalance.AllowNegativeBalance && paymentBalance.Balance < orderVM.Orders.Cost)
                {
                    ViewBag.InsufficientFunds = true;
                    ViewBag.failed            = false;
                    return(View(orderVM2));
                }
                ViewBag.InsufficientFunds = false;
                if (isStoreAuthenticated(orderVM) && orderVM.Orders.UsDate <= DateTime.Now)
                {
                    _unitOfWork.Order.Add(orderVM.Orders);
                    updateInventory(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                    updateSellerBalance(orderVM.Orders.Cost);
                    updateWarehouseBalance(orderVM.Orders.Quantity);
                    _unitOfWork.Save();
                    ViewBag.failed = false;
                }
                else
                {
                    ViewBag.failed = true;
                }
            }
            return(View(orderVM2));
        }
Exemple #4
0
        public IActionResult Delete(int id)
        {
            var objFromDb = _unitOfWork.ReturnLabel.Get(id);

            if (objFromDb == null)
            {
                return(Json(new { success = false, message = "Error While Deleting" }));
            }
            PaymentBalance payment = _unitOfWork.PaymentBalance.GetAll().Where(a => a.UserNameId == returnUserNameId()).FirstOrDefault();

            payment.Balance = payment.Balance + SD.shipping_cost;
            _unitOfWork.ReturnLabel.Remove(objFromDb);
            _unitOfWork.Save();
            return(Json(new { success = true, message = "Delete Successfull, " + SD.shipping_cost + "$ Added Back To Your Balance!" }));
        }
Exemple #5
0
 public IActionResult EditAllow(int[] Ids)
 {
     foreach (int Id in Ids)
     {
         PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.Id == Id).FirstOrDefault();
         if (paymentBalance.AllowNegativeBalance)
         {
             paymentBalance.AllowNegativeBalance = false;
         }
         else
         {
             paymentBalance.AllowNegativeBalance = true;
         }
         _unitOfWork.Save();
     }
     return(View());
 }
Exemple #6
0
        public IActionResult AddReturnLabel(ReturnLabelVM returnLabelVM)
        {
            ViewBag.InvalidQuantity = false;
            ViewBag.ShowMsg         = false;
            ReturnLabelVM returnLabelVM2 = new ReturnLabelVM()
            {
                returnLabel = new ReturnLabel(),
                OrderList   = _unitOfWork.Order.GetAll().Where(i => i.UserNameId == returnUserNameId()).Select(i => new SelectListItem
                {
                    Text  = i.Id + "-" + i.CustName + "-Quantity: " + i.Quantity,
                    Value = i.Id.ToString()
                })
            };

            returnLabelVM2.returnLabel.UserNameId = returnUserNameId();
            if (ModelState.IsValid)
            {
                int quantity = _unitOfWork.Order.GetAll().Where(a => a.Id == returnLabelVM.returnLabel.OrderId).Select(a => a.Quantity).FirstOrDefault();
                if (returnLabelVM.returnLabel.ReturnQuantity > quantity)
                {
                    ViewBag.InvalidQuantity = true;
                }
                else
                {
                    PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.UserNameId == returnUserNameId()).
                                                    FirstOrDefault();
                    if (paymentBalance.Balance < SD.shipping_cost)
                    {
                        ViewBag.InsufficientFunds = true;
                        ViewBag.ShowMsg           = true;
                        ViewBag.InvalidQuantity   = false;
                        return(View(returnLabelVM2));
                    }
                    paymentBalance.Balance  = paymentBalance.Balance - SD.shipping_cost;
                    ViewBag.InvalidQuantity = false;
                    _unitOfWork.ReturnLabel.Add(returnLabelVM.returnLabel);
                    ViewBag.ReturnCost = SD.shipping_cost;
                    _unitOfWork.Save();
                }
                ViewBag.ShowMsg = true;
            }
            ViewBag.InsufficientFunds = false;
            return(View(returnLabelVM2));
        }
        public IActionResult AddRefund(RefundVM refundVM)
        {
            bool errAmount = false;

            if (ModelState.IsValid)
            {
                Order order = _unitOfWork.Order.GetAll().Where(a => a.Id == refundVM.refund.OrderId).FirstOrDefault();
                if (refundVM.refund.FullRefund)
                {
                    refundVM.refund.RefundQuantity = order.Quantity;
                }
                if (refundVM.refund.RefundQuantity > order.Quantity || refundVM.refund.RefundQuantity <= 0)
                {
                    ViewBag.ErrAmount = true;
                    errAmount         = true;
                }
                else
                {
                    ViewBag.ErrAmount = false;
                }
                if (!errAmount)
                {
                    bool           ownByWarehouse = _unitOfWork.Product.GetAll().Where(a => a.Id == order.ProductId).Select(a => a.OwnByWarehouse).FirstOrDefault();
                    PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.UserNameId == order.UserNameId).FirstOrDefault();
                    // add refund amount to seller balance
                    double costPerOne = order.Cost / order.Quantity;
                    paymentBalance.Balance = paymentBalance.Balance + costPerOne * refundVM.refund.RefundQuantity;
                    PaymentBalance warehousePaymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.IsWarehouseBalance).FirstOrDefault();
                    //remove from warehouse if its his product
                    if (ownByWarehouse)
                    {
                        double productCost = _unitOfWork.Product.GetAll().Where(a => a.Id == order.ProductId).Select(a => a.Cost).FirstOrDefault();
                        warehousePaymentBalance.Balance = warehousePaymentBalance.Balance + refundVM.refund.RefundQuantity * productCost;
                    }
                    //if charge warehouse for shipping
                    if (refundVM.refund.ChargeWarehouse)
                    {
                        warehousePaymentBalance.Balance = warehousePaymentBalance.Balance + refundVM.refund.RefundQuantity * SD.shipping_cost;
                    }
                    if (refundVM.refund.RefundQuantity == order.Quantity)
                    {
                        order.OrderStatus = SD.OrderStatusFullRefund;
                    }
                    else
                    {
                        order.OrderStatus = SD.OrderStatusPartialRefund;
                    }
                    _unitOfWork.Order.update(order);
                    _unitOfWork.Refund.Add(refundVM.refund);
                    _unitOfWork.Save();
                }

                ViewBag.ShowMsg = 1;


                //return RedirectToAction(nameof(Index));
            }
            RefundVM refundVM2 = new RefundVM()
            {
                refund     = new Refund(),
                OrdersList = _unitOfWork.Order.GetAll().Where(a => a.Id == refundVM.refund.OrderId).
                             Select(i => new SelectListItem
                {
                    Text  = i.CustName + "- Id: " + i.Id + " Quantity: " + i.Quantity,
                    Value = i.Id.ToString()
                })
            };

            return(View(refundVM2));
        }
Exemple #8
0
        public void updateWarehouseBalance(int quantity)
        {
            PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.IsWarehouseBalance).FirstOrDefault();

            paymentBalance.Balance = paymentBalance.Balance - (quantity * SD.shipping_cost);
        }
Exemple #9
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName       = Input.Email,
                    Name           = Input.Name,
                    Email          = Input.Email,
                    Role           = Input.Role,
                    EmailConfirmed = true
                };
                var result = _userManager.CreateAsync(user, Input.Password).GetAwaiter().GetResult();
                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");


                    if (user.Role == null)
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_Users);
                    }
                    else
                    {
                        await _userManager.AddToRoleAsync(user, user.Role);
                    }

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        if (user.Role == null)//user is signing in and not by admin
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            return(LocalRedirect(returnUrl));
                        }
                        else
                        {
                            if (Input.Role == SD.Role_Users)
                            {
                                //System.Threading.Thread.Sleep(1000);
                                string userNameId =
                                    _unitOfWork.ApplicationUser.GetAll().Where(a => a.Email == Input.Email && a.Name == Input.Name &&
                                                                               a.Role == SD.Role_Users).Select(a => a.Id).FirstOrDefault();
                                PaymentBalance paymentBalance = new PaymentBalance();
                                paymentBalance.UserNameId           = userNameId;
                                paymentBalance.Balance              = 0;
                                paymentBalance.IsWarehouseBalance   = false;
                                paymentBalance.AllowNegativeBalance = false;
                                _unitOfWork.PaymentBalance.Add(paymentBalance);
                                _unitOfWork.Save();
                            }
                            else if (Input.Role == SD.Role_Warehouse)
                            {
                                bool exist = _unitOfWork.PaymentBalance.GetAll().Any(a => a.IsWarehouseBalance);
                                if (!exist)
                                {
                                    //System.Threading.Thread.Sleep(1000);
                                    string userNameId =
                                        _unitOfWork.ApplicationUser.GetAll().Where(a => a.Email == Input.Email && a.Name == Input.Name &&
                                                                                   a.Role == SD.Role_Warehouse).Select(a => a.Id).FirstOrDefault();
                                    PaymentBalance paymentBalance = new PaymentBalance();
                                    paymentBalance.UserNameId           = userNameId;
                                    paymentBalance.Balance              = 0;
                                    paymentBalance.IsWarehouseBalance   = true;
                                    paymentBalance.AllowNegativeBalance = true;
                                    _unitOfWork.PaymentBalance.Add(paymentBalance);
                                    _unitOfWork.Save();
                                }
                            }
                            //admin is registering a new user
                            return(RedirectToAction("Index", "User", new { Area = "Admin" }));
                        }
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public IActionResult AddOrdersExtension(OrderVM orderVM)
        {
            string uNameId = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault();

            ViewBag.uNameId = uNameId;
            int  processedLines    = 0;
            bool InsufficientFunds = false;

            if (ModelState.IsValid)
            {
                ViewBag.ShowMsg = 1;
                string allOrders = orderVM.AllOrder;
                for (int i = 0; i < 3; i++)
                {
                    if (allOrders.Length > 2)
                    {
                        if (allOrders[(allOrders.Length - 1)].Equals('\r') || allOrders[(allOrders.Length - 1)].Equals('\n') ||
                            allOrders[(allOrders.Length - 1)].Equals('\"'))
                        {
                            allOrders = allOrders.Remove(allOrders.Length - 1);
                        }
                    }
                }
                var    ordersList  = allOrders.Split(new string[] { "\"\r\n" }, StringSplitOptions.None);
                string failedLines = "";
                int    lineNum     = 0;
                foreach (var order in ordersList)
                {
                    initializeVM(orderVM);
                    lineNum++;
                    try
                    {
                        var orderDetails = order.Split(new string[] { "\t" }, StringSplitOptions.None);
                        orderVM.Orders.ProductId   = getProductIdByName(orderDetails[0]);
                        orderVM.Orders.UserNameId  = returnUserNameId();
                        orderVM.Orders.StoreNameId = getStoreNameId(orderDetails[1]);
                        orderVM.Orders.Quantity    = Int32.Parse(orderDetails[3]);
                        orderVM.Orders.Cost        = returnCost(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                        orderVM.Orders.UsDate      = DateTime.Parse(orderDetails[2]);
                        addAddressDetailsToVM(orderDetails[4], orderVM);
                        //remove diacritics and comma
                        orderVM.Orders.CustName    = RemoveDiacritics(orderVM.Orders.CustName).Replace(",", "");
                        orderVM.Orders.CustStreet1 = RemoveDiacritics(orderVM.Orders.CustStreet1).Replace(",", "");
                        if (orderVM.Orders.CustStreet2.Length > 0)
                        {
                            orderVM.Orders.CustStreet2 = RemoveDiacritics(orderVM.Orders.CustStreet2).Replace(",", "");
                        }
                        PaymentBalance paymentBalance = userBalance(uNameId);
                        if (!paymentBalance.AllowNegativeBalance && paymentBalance.Balance < orderVM.Orders.Cost)
                        {
                            InsufficientFunds = true;
                            if (failedLines.Length == 0)
                            {
                                failedLines = orderVM.Orders.CustName;
                            }
                            else
                            {
                                failedLines = failedLines + "," + orderVM.Orders.CustName;
                            }
                            continue;
                        }
                        if (isStoreAuthenticated(orderVM) && orderVM.Orders.ProductId > 0 &&
                            orderVM.Orders.UsDate <= DateTime.Now && Enumerable.Range(1, 100).Contains(orderVM.Orders.Quantity) &&
                            orderVM.Orders.CustName.Length > 0 && orderVM.Orders.CustStreet1.Length > 0 &&
                            Enumerable.Range(5, 10).Contains(orderVM.Orders.CustZipCode.Length) &&
                            orderVM.Orders.CustCity.Length > 1 && orderVM.Orders.CustState.Length == 2)
                        {
                            _unitOfWork.Order.Add(orderVM.Orders);
                            updateInventory(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                            updateWarehouseBalance(orderVM.Orders.Quantity);
                            updateSellerBalance(orderVM.Orders.Cost);
                            _unitOfWork.Save();
                            processedLines++;
                        }
                        else
                        {
                            if (failedLines.Length == 0)
                            {
                                failedLines = orderVM.Orders.CustName;
                            }
                            else
                            {
                                failedLines = failedLines + "," + orderVM.Orders.CustName;
                            }
                        }
                    }
                    catch
                    {
                        if (failedLines.Length == 0)
                        {
                            failedLines = orderVM.Orders.CustName;
                        }
                        else
                        {
                            failedLines = failedLines + "," + orderVM.Orders.CustName;
                        }
                    }
                }
                // if(failedLines.Length == 0 )
                //{
                if (InsufficientFunds)
                {
                    ViewBag.InsufficientFunds = InsufficientFunds;
                    if (processedLines == 0)
                    {
                        ViewBag.failed = "Insufficient Funds! No Orders were processed!";
                    }
                    else if (processedLines == 1)
                    {
                        ViewBag.failed = "Insufficient Funds! Only One Order was Processed Successfully!" +
                                         "*failed Orders*: " + failedLines;
                    }
                    else
                    {
                        ViewBag.failed = "Insufficient Funds! Only " + processedLines + " Orders were Processed Successfully!" +
                                         "*failed Orders*: " + failedLines;
                    }
                }
                else if (failedLines.Length > 0)
                {
                    ViewBag.InsufficientFunds = false;
                    if (processedLines == 0)
                    {
                        ViewBag.failed = "Pay Attention: An error occured! No Orders were processed!";
                    }
                    else if (processedLines == 1)
                    {
                        ViewBag.failed = "Pay Attention: An error occured! Only One Order was Processed Successfully!" +
                                         "*failed Orders*: " + failedLines;
                    }
                    else
                    {
                        ViewBag.failed = "Pay Attention: An error occured Only " + processedLines + " Orders were Processed Successfully!" +
                                         "*failed Orders*: " + failedLines;
                    }
                }
                else
                {
                    ViewBag.InsufficientFunds = false;
                    ViewBag.failed            = "";
                }
                ViewBag.ShowMsg        = 1;
                ViewBag.ProcessedLines = processedLines;
                return(View(orderVM));

                //}
                //return RedirectToAction(nameof(Index));
            }
            return(View(orderVM.Orders));
        }
        public IActionResult UpdateOrder(OrderVM orderVM)
        {
            string uNameId = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault();

            ViewBag.uNameId = uNameId;
            OrderVM orderVM2 = new OrderVM()
            {
                Orders      = new Order(),
                ProductList = _unitOfWork.Product.GetAll().Where(a => a.AvailableForSellers && !a.OOSForSellers).
                              OrderBy(a => a.ProductName).Select(i => new SelectListItem
                {
                    Text  = i.ProductName,
                    Value = i.Id.ToString()
                }),
                StoresList = _unitOfWork.UserStoreName.GetAll().Where(q => q.UserNameId == uNameId).Select(i => new SelectListItem
                {
                    Text  = i.StoreName,
                    Value = i.Id.ToString()
                }),
                StatesList = SD.States,
                StatusList = SD.StatusAcceptOrCancel
            };

            if (ModelState.IsValid)
            {
                orderVM.Orders.Cost = returnCost(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                ViewBag.ShowMsg     = 1;
                if (isStoreAuthenticated(orderVM) && orderVM.Orders.UsDate <= DateTime.Now)
                {
                    int oldQuantity = _unitOfWork.Order.GetAll().Where(a => a.Id == orderVM.Orders.Id)
                                      .Select(a => a.Quantity).FirstOrDefault();
                    int oldProductId = _unitOfWork.Order.GetAll().Where(a => a.Id == orderVM.Orders.Id)
                                       .Select(a => a.ProductId).FirstOrDefault();
                    double oldCost = _unitOfWork.Order.GetAll().Where(a => a.Id == orderVM.Orders.Id)
                                     .Select(a => a.Cost).FirstOrDefault();
                    string oldStatus = _unitOfWork.Order.GetAll().Where(a => a.Id == orderVM.Orders.Id)
                                       .Select(a => a.OrderStatus).FirstOrDefault();
                    PaymentBalance paymentBalance = userBalance(uNameId);
                    if (!paymentBalance.AllowNegativeBalance && paymentBalance.Balance < (orderVM.Orders.Cost - oldCost))
                    {
                        ViewBag.InsufficientFunds = true;
                        ViewBag.failed            = false;
                        return(View(orderVM2));
                    }
                    bool fail = false;
                    try
                    {
                        //changed to cancel
                        if (orderVM.Orders.OrderStatus != oldStatus && orderVM.Orders.OrderStatus == SD.OrderStatusCancelled)
                        {
                            updateInventory(oldProductId, oldQuantity * (-1));
                            updateWarehouseBalance(oldQuantity * (-1));
                            updateSellerBalance(oldCost * (-1));
                            // if it's a cancellation - we dont want any change but the cancellation it self
                            orderVM.Orders             = _unitOfWork.Order.GetAll().Where(a => a.Id == orderVM.Orders.Id).FirstOrDefault();
                            orderVM.Orders.OrderStatus = SD.OrderStatusCancelled;
                        }
                        //change from cancel
                        else if (orderVM.Orders.OrderStatus != oldStatus && orderVM.Orders.OrderStatus != SD.OrderStatusCancelled && oldStatus == SD.OrderStatusCancelled)
                        {
                            updateInventory(orderVM.Orders.ProductId, orderVM.Orders.Quantity);
                            updateWarehouseBalance(orderVM.Orders.Quantity);
                            updateSellerBalance(orderVM.Orders.Cost);
                        }
                        //status didnt change
                        else if (orderVM.Orders.OrderStatus == oldStatus && orderVM.Orders.OrderStatus != SD.OrderStatusCancelled)
                        {
                            if (oldQuantity != orderVM.Orders.Quantity)
                            {
                                updateInventory(orderVM.Orders.ProductId, (orderVM.Orders.Quantity - oldQuantity));
                                updateWarehouseBalance(orderVM.Orders.Quantity - oldQuantity);
                                updateSellerBalance(orderVM.Orders.Cost - oldCost);
                            }
                        }
                        _unitOfWork.Order.update(orderVM.Orders);
                    }
                    catch
                    {
                        fail = true;
                    }
                    if (!fail)
                    {
                        _unitOfWork.Save();
                    }
                    ViewBag.failed = false;
                }
                else
                {
                    ViewBag.failed = true;
                }
            }
            ViewBag.InsufficientFunds = false;
            return(View(orderVM2));
        }
        public void AddBalanceToWarehouse(double Amount)
        {
            PaymentBalance paymentBalance = _unitOfWork.PaymentBalance.GetAll().Where(a => a.IsWarehouseBalance).FirstOrDefault();

            paymentBalance.Balance = paymentBalance.Balance + Amount;
        }