Exemplo n.º 1
0
        /// <summary>
        /// Check if quantity matches with any other converted quantity.
        /// False: means accept the result. (it doesn't mismatch)
        /// True: means do not accept the result. ( it does mismatch )
        /// Use it like !(Mismatch(..,..,..)
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <param name="PassingQuantity">Converting Quantity Type pass and it will be check against the products default quantity in the category table.</param>
        /// <param name="Multiply">Returns the value of multiple to be need to make the conversion.</param>
        /// <returns>Returns false when does not mismatch and returns true when type mismatch like ml. can't be converted into grams.</returns>
        public bool Mismatch(int CategoryID, byte PassingQuantity, out decimal Multiply, bool trackErrors = true)
        {
            using (var db = new JustFoodDBEntities()) {
                // current category qty
                var category = db.Categories.Find(CategoryID);
                if (category != null)
                {
                    var currentQtyTypeFromCategory = category.QtyType;
                    if (currentQtyTypeFromCategory == PassingQuantity)
                    {
                        Multiply = 1;
                        return(false);
                    }
                    var quantityConverter = db.QuantityConversations.FirstOrDefault(n => n.QuantityTypeID == currentQtyTypeFromCategory && n.QuantityConversationID == PassingQuantity);

                    if (quantityConverter != null)
                    {
                        Multiply = quantityConverter.Difference;
                        return(false);
                    }
                }
            }
            if (trackErrors)
            {
                Statics.ErrorCollection.Add(Const.QuantityTypeMismatch);
            }
            Multiply = 0;
            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// To add product to inventory : do not call this method . Instead call InventoryExtention Invetory Add to do all the work.
 /// Add ++ quantity to inventory of the specific product by the account balance info.
 /// </summary>
 /// <param name="accountbalance"></param>
 /// <param name="dbx"></param>
 public void AddProduct(AccountBalance accountbalance, JustFoodDBEntities dbx)
 {
     if (!accountbalance.IsBoughtProduct)
     {
         return;
     }
     if (accountbalance.AddedQuantity > 0)
     {
         Inventory inventory = dbx.Inventories.FirstOrDefault(n => n.CategoryID == accountbalance.CategoryProduct);
         if (inventory != null)
         {
             inventory.Quantity += (int)accountbalance.AddedQuantity;
         }
         else
         {
             // if null
             var invController = new InventoryController();
             invController.Index();
             inventory = dbx.Inventories.FirstOrDefault(n => n.CategoryID == accountbalance.CategoryProduct);
             if (inventory != null)
             {
                 inventory.Quantity += (int)accountbalance.AddedQuantity;
             }
         }
         // dbx.InventoryIns.Add(invtIn);
     }
 }
Exemplo n.º 3
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && User.Identity.IsAuthenticated)
                    {
                        //save last login
                        var  userinfo = new UserInfo();
                        User user     = userinfo.GetUserSession();
                        user.LastLogIn = DateTime.UtcNow;
                        using (var db = new JustFoodDBEntities()) {
                            db.Entry(user).State = EntityState.Detached;
                            db.Entry(user).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Call this method when adding product to inventory.
        /// Add product info into the InventoryIn table and then ++ then quantity in the Inventory Table By product Extension class..
        /// Modify Account Balance for product bought.
        /// Verify account balance info.
        /// Add account balance to db.
        /// Create InventoryIn
        /// Create Product
        /// Add those to database.
        /// Just not save. Save it when all successful.
        /// </summary>
        /// <param name="db">Pass the dbContext</param>
        /// <param name="accountBalance"></param>
        /// <returns></returns>
        public bool InventoryAdd(JustFoodDBEntities db, AccountBalance accountBalance)
        {
            bool result = true;

            accountBalance.Dated           = DateTime.Now;
            accountBalance.AddBy           = userinfo.GetUserSession().UserID;
            accountBalance.IsBoughtProduct = true;
            accountBalance.IsAddedMoney    = false;
            accountBalance.IsExpense       = true;

            if (accountBalance.Amount < 0 || accountBalance.AddedQuantity < 1)
            {
                Statics.ErrorCollection.Add("Amount", "Amount or Quantity can't be negative.");
                result = false;
            }
            //checking if quantity type is valid.
            if (accountBalance.QtyType == null || db.QuantityTypes.Find(accountBalance.QtyType) == null)
            {
                Statics.ErrorCollection.Add("QtyType", "Quantity type is not valid , please select a valid quantity type.");
                result = false;
            }
            var quantity     = accountBalance.AddedQuantity;
            var unitprice    = (accountBalance.Amount / (double)quantity);
            var qtyExtension = new QuantityTypeExtension();

            unitprice = Math.Round(unitprice, 2);
            int userid = userinfo.GetUserID();

            // check if quantity mismatch.
            decimal multiplier = 0;
            var     mismatch   = qtyExtension.Mismatch((int)accountBalance.CategoryProduct, (byte)accountBalance.QtyType, out multiplier);

            if (!mismatch)
            {
                var invtIn = new InventoryIn {
                    CategoryID = (int)accountBalance.CategoryProduct,
                    Quantity   = (decimal)quantity,
                    UnitPrice  = unitprice,
                    AddedBy    = userid,
                    QtyType    = (byte)accountBalance.QtyType,
                    Dated      = DateTime.Now
                };
                var productExt = new ProductExtension();
                //add inventory product in.
                db.InventoryIns.Add(invtIn);
                accountBalance.AddedQuantity = accountBalance.AddedQuantity * multiplier;
                accountBalance.Amount        = accountBalance.Amount * -1; //expense
                db.AccountBalances.Add(accountBalance);
                productExt.AddProduct(accountBalance, db);
            }
            return(result);
        }
Exemplo n.º 5
0
 public AccountBalanceController()
 {
     _Db       = new JustFoodDBEntities();
     _Userinfo = new UserInfo();
 }
Exemplo n.º 6
0
 public UserInfo()
 {
     _Db = new JustFoodDBEntities();
 }
Exemplo n.º 7
0
 /// <summary>
 /// Return all admin emails
 /// </summary>
 /// <returns></returns>
 public string[] GetAdmins()
 {
     using (var db = new JustFoodDBEntities()) {
         return(db.Users.Where(n => n.IsAccessToAdmin).Select(n => n.Email).ToArray());
     }
 }
Exemplo n.º 8
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                //check the code
                var  db   = new JustFoodDBEntities();
                Code code = db.Codes.FirstOrDefault(m => m.Code1 == model.Code);
                if (code == null)
                {
                    ModelState.AddModelError(model.Code, "Your given code is not valid.");
                    return(View(model));
                }


                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);

                    //create new user.
                    if (code.Percentage == null)
                    {
                        code.Percentage = 0;
                    }
                    if (code.Salary == null)
                    {
                        code.Salary = 0;
                    }

                    var user = new User {
                        LogName         = model.UserName,
                        Name            = model.PersonName,
                        IsEmployee      = code.IsEmployee,
                        IsOwner         = code.IsOwner,
                        IsAccessToAdmin = code.IsAccessToAdmin,
                        Percentage      = (double)code.Percentage,
                        Salary          = (double)code.Salary,
                        Email           = model.Email,
                        IsValidEmail    = true
                    };
                    //there is no need to keep the used code.
                    db.Entry(code).State = EntityState.Deleted;
                    db.Users.Add(user);
                    db.SaveChanges();
                    //Roles.CreateRole("");
                    var    roleManager = new RoleManage();
                    string role        = "";
                    if (user.IsAccessToAdmin)
                    {
                        role = RoleNames.Admin;
                    }
                    else if (user.IsEmployee)
                    {
                        role = RoleNames.SalesMan;
                    }
                    roleManager.AddRole(user.LogName, role);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }