public void Post()
        {
            var factorItems = new List <FactorItem> {
                FactorItem.CreateFactorItem(Guid.Empty, Guid.Empty, 10)
            };
            var createFactorCommand = new CreateFactorCommand(System.DateTime.Now.AddDays(7), "Factor", "-", factorItems);

            var factorFacade = ServiceLocator.Current.Resolve <IFactorFacade>();

            factorFacade.CreateFactor(createFactorCommand);
        }
Exemple #2
0
 public void InitFactorItem()
 {
     FirstWordPosFactorItem           = new FactorItem <String>();
     FirstWordPosFactorItem.Transform = (x) => posSeg.Cut(x).First().Flag;
     WordLengtFactorItem           = new  FactorItem <int>();
     WordLengtFactorItem.Transform = (x) => x.Length;
     WordCountFactorItem           = new FactorItem <int> ();
     WordCountFactorItem.Transform = (x) => posSeg.Cut(x).Count();
     LastWordFactorItem            = new FactorItem <String>();
     LastWordFactorItem.Transform  = (x) => posSeg.Cut(x).Last().Word;
 }
Exemple #3
0
 public bool Delete(FactorItem entity, bool autoSave = true)
 {
     try
     {
         db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
         if (autoSave)
         {
             return(Convert.ToBoolean(db.SaveChanges()));
         }
         else
         {
             return(false);
         }
     }
     catch
     {
         return(false);
     }
 }
Exemple #4
0
 public bool Add(FactorItem entity, bool autoSave = true)
 {
     try
     {
         db.FactorItems.Add(entity);
         if (autoSave)
         {
             return(Convert.ToBoolean(db.SaveChanges()));
         }
         else
         {
             return(false);
         }
     }
     catch
     {
         return(false);
     }
 }
Exemple #5
0
        public async Task <IActionResult> SubmitUserFactor([FromBody] SubmitUserFactorRequestModel model)
        {
            try
            {
                PreFactor preFactor = await _unitOfWork.PreFactorRepository.GetDbSet().Include(f => f.User).ThenInclude(u => u.Contacts).SingleOrDefaultAsync(f => f.Id == Guid.Parse(model.PreFactorId));

                if (preFactor == null)
                {
                    return(NotFound("preFactor not found"));
                }

                if (preFactor.SubmittedFactorId != null)
                {
                    return(BadRequest("This preFactor already have a submitted factor"));
                }

                Contact contact = preFactor.User.Contacts.SingleOrDefault(c => c.Id == Guid.Parse(model.ContactId));
                if (contact == null)
                {
                    return(NotFound("user contact not found"));
                }

                long totalPrice           = 0;
                List <FactorItem> factors = new List <FactorItem>();
                foreach (FactorItemRequestModel item in model.FactorItems)
                {
                    Product product = await _unitOfWork.ProductRepository.SingleOrDefaultAsync(p => p.Title == item.Product.Title);

                    if (product == null)
                    {
                        return(NotFound(item.Product.Title + " not found"));
                    }

                    FactorItem factorItem = new FactorItem()
                    {
                        Price      = item.Price,
                        Product    = product,
                        TotalPrice = item.Quantity * item.Price
                    };
                    factors.Add(factorItem);
                    totalPrice += factorItem.TotalPrice;
                }
                SubmittedFactor sf = new SubmittedFactor
                {
                    Contact    = contact,
                    FactorDate = model.FactorDate,
                    TotalPrice = totalPrice,
                    Items      = factors,
                    PreFactor  = preFactor,
                    State      = new State(model.State.IsClear),
                    Code       = await CalculateCode(preFactor.User.Phone)
                };
                try
                {
                    _unitOfWork.SubmittedFactorRepository.Insert(sf);
                    preFactor.IsDone = true;
                    _unitOfWork.PreFactorRepository.Update(preFactor);
                    _unitOfWork.Commit();
                    var x = new
                    {
                        sf.Id,
                        sf.Items,
                        sf.State,
                        sf.TotalPrice
                    };
                    return(Ok(x));
                }
                catch (Exception e)
                {
                    _unitOfWork.Rollback();
                    _logger.LogError(e, e.Message);
                    return(Problem("database error"));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Problem(e.Message));
            }
        }
Exemple #6
0
        public object Store()
        {
            int pid     = Convert.ToInt32(HttpContext.Current.Request.Form["Id"]);
            var product = db.Products.Include("Category").Where(p => p.Id == pid).FirstOrDefault();
            var tr      = db.Database.BeginTransaction();

            if (product.Qty == 0)
            {
                return(new { Message = 1 });
            }

            var token = HttpContext.Current.Request.Form["Api_Token"];
            var id    = db.Users.Where(p => p.Api_Token == token).FirstOrDefault().Id;
            var order = db.Factors.Where(p => p.Status == false).Where(p => p.User.Id == id).FirstOrDefault();

            if (order == null)
            {
                order         = new Factor();
                order.Status  = false;
                order.Date    = DateTime.Now;
                order.User    = db.Users.Find(id);
                order.Buyer   = order.User.Fullname;
                order.Address = order.User.Address;
                order.Mobile  = order.User.Mobile;
                //order.User_PostalCode= order.User.PostalCode;
                order.IsAdminShow     = false;
                order.Discount_Amount = 0;

                var detail = new FactorItem();
                detail.Product     = product;
                detail.ProductName = product.Name;
                detail.Qty         = 1;
                detail.UnitPrice   = product.Price - product.Discount;

                order.FactorItems.Add(detail);
                db.Factors.Add(order);
                db.SaveChanges();
            }
            else
            {
                var res = db.FactorItems.Where(p => p.Product.Id == product.Id).Where(p => p.Factor.Id == order.Id).FirstOrDefault();
                if (res != null)
                {
                    if (res.Product.Qty - res.Qty <= 0)
                    {
                        return(new { Message = 2 });
                    }
                    res.Qty++;
                    db.SaveChanges();
                }
                else
                {
                    var detail = new FactorItem();
                    detail.Product     = product;
                    detail.ProductName = product.Name;
                    detail.Qty         = 1;
                    detail.UnitPrice   = product.Price - product.Discount;
                    order.FactorItems.Add(detail);
                    db.SaveChanges();
                }
            }
            tr.Commit();


            return(new { Message = 0 });
        }
        public async Task <IActionResult> Cart()
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var student = await _context.Students.Where(x => x.IdUser == user.Id).AsNoTracking().FirstOrDefaultAsync();

            int count = 0;

            if (HttpContext.Session.GetComplexData <List <string> >("CartItems") != null)
            {
                count = HttpContext.Session.GetComplexData <List <string> >("CartItems").Count;
            }
            if (count != 0)
            {
                if (user == null)
                {
                    return(RedirectToAction("Login", "Account"));
                }

                if (HttpContext.Session.Keys.Contains("CartItems"))
                {
                    var cartItems = HttpContext.Session.GetComplexData <List <string> >("CartItems");
                    var products  = await _context.Costs.Where(x => cartItems.Contains(x.Id)) /*.Include(x => x.OfferItems)*/.ToListAsync();

                    //await Helper.AddOfferToProductsAsync(_context, user, products);

                    var factors = await _context.Factors.Where(x => !x.IsPaid && x.IdUser == user.Id).OrderByDescending(x => x.Date).Include(x => x.FactorItems).ThenInclude(x => x.Cost) /*.ThenInclude(x => x.OfferItems)*/.ToListAsync();

                    // check if there is an unpaid factor with the same items
                    var factor = factors.FirstOrDefault(x => cartItems.All(x.FactorItems.Select(y => y.CostId).Contains) && x.FactorItems.Count == cartItems.Count);

                    if (factor == null)
                    {
                        factor = new Factor
                        {
                            Date   = DateTime.UtcNow,
                            IdUser = user.Id,
                        };

                        // generate random factor code
                        var allFactorCodes = await _context.Factors.Select(x => x.FactorCode).ToListAsync();

                        var random = new Random();
                        do
                        {
                            factor.FactorCode = $"Sahad-{random.Next(10000, 99999)}";
                        } while (allFactorCodes.Contains(factor.FactorCode));

                        await using var transaction = await _context.Database.BeginTransactionAsync();

                        try
                        {
                            await _context.Factors.AddAsync(factor);

                            await _context.SaveChangesAsync();

                            foreach (var product in products)
                            {
                                var factorItem = new FactorItem
                                {
                                    FactorId = factor.Id,
                                    CostId   = product.Id,
                                    Cost     = product,
                                    Price    = product.Value
                                               //UnitCount = 1,
                                               //UnitPrice = product.Price,
                                               //Discount = product.Price - product.PriceWithDiscount
                                };

                                await _context.FactorItems.AddAsync(factorItem);
                            }

                            factor.TotalPrice = (int)factor.FactorItems.Sum(x => x.Cost.Value);

                            _context.Factors.Update(factor);
                            await _context.SaveChangesAsync();

                            await transaction.CommitAsync();
                        }
                        catch (Exception)
                        {
                            await transaction.RollbackAsync();
                        }
                    }
                    else
                    {
                        if (factor.FactorItems?.Count > 0)
                        {
                            factor.TotalPrice = (int)factor.FactorItems.Sum(x => x.Cost.Value);
                        }
                        factor.Date = DateTime.UtcNow;

                        _context.Factors.Update(factor);
                        await _context.SaveChangesAsync();
                    }

                    return(View(factor));
                }

                return(View(new Factor()));
            }
            else
            {
                HttpContext.Session.SetString("selectCost", "NotComplete");
                TempData["selectCost"] = "جهت ادامه ثبت نام، شهریه را انتخاب فرمایید";
                return(RedirectToAction("Costs", "UserProfile"));
            }
        }
        public ActionResult Store()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Redirect("/User/Login?ReturnUrl=/Products/Index?Id=" + Request["Id"]));
            }
            int pid     = Convert.ToInt32(Request["Id"]);
            var product = db.Products.Include("Category").Where(p => p.Id == pid).Where(p => p.Status == true).FirstOrDefault();

            var tr = db.Database.BeginTransaction();

            if (product.Qty == 0)
            {
                return(Redirect("/Products/Index?Id=" + Request["Id"] + "&" + "message=-1"));
            }
            var email = User.Identity.Name;
            var id    = db.Users.Where(p => p.Email == email).FirstOrDefault().Id;
            var order = db.Factors.Where(p => p.Status == false).Where(p => p.User.Id == id).FirstOrDefault();

            if (order == null)
            {
                order                 = new Factor();
                order.Status          = false;
                order.Date            = DateTime.Now;
                order.TotalPrice      = 0;
                order.User            = db.Users.Find(id);
                order.Buyer           = order.User.Fullname;
                order.Address         = order.User.Address;
                order.Mobile          = order.User.Mobile;
                order.IsAdminShow     = false;
                order.PhoneNumber     = order.User.PhoneNumber;
                order.PostalCode      = order.User.PostalCode;
                order.Discount_Amount = 0;
                var detail = new FactorItem();
                detail.Product     = product;
                detail.ProductName = product.Name;
                detail.Qty         = 1;
                detail.UnitPrice   = product.Price - product.Discount;

                order.FactorItems.Add(detail);
                db.Factors.Add(order);
                db.SaveChanges();
            }
            else
            {
                var res = db.FactorItems.Where(p => p.Product.Id == product.Id).Where(p => p.Factor.Id == order.Id).FirstOrDefault();
                if (res != null)
                {
                    if (res.Product.Qty - res.Qty <= 0)
                    {
                        return(Redirect("/Products/Index?Id=" + Request["Id"] + "&" + "message=-1"));
                    }
                    res.Qty++;
                    db.SaveChanges();
                }
                else
                {
                    var detail = new FactorItem();
                    detail.Product     = product;
                    detail.ProductName = product.Name;
                    detail.Qty         = 1;
                    detail.UnitPrice   = product.Price;
                    order.FactorItems.Add(detail);
                    db.SaveChanges();
                }
            }
            tr.Commit();
            return(RedirectToAction("Index"));
        }