Beispiel #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            INV_Item iNV_Item = db.INV_Item.Find(id);

            try
            {
                if (iNV_Item.IsLock)
                {
                    iNV_Item.IsActive        = false;
                    db.Entry(iNV_Item).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else
                {
                    List <INV_StockHistory> StockHistory = db.INV_StockHistory.Where(i => i.ItemID == iNV_Item.ItemID).ToList();
                    db.INV_StockHistory.RemoveRange(StockHistory);
                    db.SaveChanges();
                    db.INV_Item.Remove(iNV_Item);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "You can not Delete this Item.");
                //return RedirectToAction("Index");
                return(View(iNV_Item));
            }
            return(RedirectToAction("Index"));
        }
        public ActionResult Edit(FormCollection frm, int?id)
        {
            INV_Item updateItem = new INV_Item();

            updateItem.ItemName     = frm["ItemName"];
            updateItem.ItemCode     = frm["ItemCode"];
            updateItem.CategoryId   = Convert.ToInt32(frm["CategoryId"]);
            updateItem.DepartmentId = Convert.ToInt32(Session["DepartmentId"]);

            if (frm["IsWarranty"] == "Y")
            {
                updateItem.IsWarranty         = true;
                updateItem.WarrantyDuration   = Convert.ToInt32(frm["WarrantyDuration"]);
                updateItem.WarrantyFromDate   = DateTime.ParseExact(frm["WarrantyFromDate"], "yyyy-MM-dd", null);
                updateItem.WarrantyFromDateBS = frm["WarrantyFromDateBS"];
                updateItem.WarrantyToDate     = DateTime.ParseExact(frm["WarrantyToDate"], "yyyy-MM-dd", null);
                updateItem.WarrantyToDateBS   = frm["WarrantyToDateBS"];
            }
            else
            {
                updateItem.IsWarranty         = false;
                updateItem.WarrantyDuration   = 0;
                updateItem.WarrantyFromDate   = null;
                updateItem.WarrantyFromDateBS = null;
                updateItem.WarrantyToDate     = null;
                updateItem.WarrantyToDateBS   = null;
            }

            updateItem.LastUpdatedDate = DateTime.Now;
            updateItem.LastUpdatedBy   = Convert.ToInt32(Session["UserId"]);

            db.UpdateItem(updateItem, (int)id);
            return(RedirectToAction("Index"));
        }
        public ActionResult Create(FormCollection frm)
        {
            INV_Item saveItem = new INV_Item();

            saveItem.ItemName     = frm["ItemName"];
            saveItem.ItemCode     = frm["ItemCode"];
            saveItem.CategoryId   = Convert.ToInt32(frm["CategoryId"]);
            saveItem.DepartmentId = Convert.ToInt32(Session["DepartmentId"]);

            if (frm["IsWarranty"] == "Y")
            {
                saveItem.IsWarranty         = true;
                saveItem.WarrantyDuration   = Convert.ToInt32(frm["WarrantyDuration"]);
                saveItem.WarrantyFromDate   = DateTime.ParseExact(frm["FromDate"], "yyyy-MM-dd", null);
                saveItem.WarrantyFromDateBS = frm["FromDateBS"];
                saveItem.WarrantyToDate     = DateTime.ParseExact(frm["ToDate"], "yyyy-MM-dd", null);
                saveItem.WarrantyToDateBS   = frm["ToDateBS"];
            }
            else
            {
                saveItem.IsWarranty         = false;
                saveItem.WarrantyDuration   = 0;
                saveItem.WarrantyFromDate   = null;
                saveItem.WarrantyFromDateBS = null;
                saveItem.WarrantyToDate     = null;
                saveItem.WarrantyToDateBS   = null;
            }

            saveItem.EnteredDate = DateTime.Now;
            saveItem.EnteredBy   = Convert.ToInt32(Session["UserId"]);

            db.AddUsers(saveItem);
            return(RedirectToAction("Index"));
        }
Beispiel #4
0
        // GET: INV_Item/Create
        public ActionResult Create()
        {
            ViewBag.CompanyID  = new SelectList(db.SYS_Company, "CompanyID", "CompanyName");
            ViewBag.UnitID     = new SelectList(db.INV_Unit, "UnitID", "Unit");
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName");
            ViewBag.CategoryID = new SelectList(db.INV_Category, "CategoryID", "CategoryName");

            INV_Item _iNV_Item = new INV_Item();

            return(View("Edit", _iNV_Item));
        }
Beispiel #5
0
        // GET: INV_Item/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            INV_Item iNV_Item = db.INV_Item.Find(id);

            if (iNV_Item == null)
            {
                return(HttpNotFound());
            }
            return(View(iNV_Item));
        }
Beispiel #6
0
        public int AddUsers(INV_Item saveItem)
        {
            string sql = "insert into INV_Item(ItemName,ItemCode,CategoryId,DepartmentId,IsWarranty,WarrantyDuration," +
                         "WarrantyFromDate,WarrantyFromDateBS,WarrantyToDate,WarrantyToDateBS,EnteredDate,EnteredBy,LastUpdatedDate" +
                         ",LastUpdatedBy,IsDeleted,DeletedBy,DeletedDate)" +
                         " values(@ItemName,@ItemCode,@CategoryId,@DepartmentId,@IsWarranty,@WarrantyDuration," +
                         "@WarrantyFromDate,@WarrantyFromDateBS,@WarrantyToDate,@WarrantyToDateBS,@EnteredDate,@EnteredBy,null" +
                         ",0,0,0,null)";

            using (var db = DbHelper.GetDBConnection())
            {
                int id = db.Query <int>(sql, saveItem).SingleOrDefault();
                db.Close();
                return(id);
            }
        }
Beispiel #7
0
        // GET: INV_Item/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            INV_Item iNV_Item = db.INV_Item.Find(id);

            iNV_Item.Remarks = "";
            if (iNV_Item == null)
            {
                return(HttpNotFound());
            }
            ViewBag.CompanyID  = new SelectList(db.SYS_Company, "CompanyID", "CompanyName", iNV_Item.CompanyID);
            ViewBag.UnitID     = new SelectList(db.INV_Unit, "UnitID", "Unit", iNV_Item.UnitID);
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_Item.UserID);
            ViewBag.CategoryID = new SelectList(db.INV_Category, "CategoryID", "CategoryName", iNV_Item.CategoryID);
            return(View(iNV_Item));
        }
Beispiel #8
0
        public bool UpdateItem(INV_Item updateItem, int Id)
        {
            string sql = " Update INV_Item set ItemName=@ItemName, ItemCode=@ItemCode, CategoryId=@CategoryId," +
                         " IsWarranty=@IsWarranty, WarrantyDuration=@WarrantyDuration, WarrantyFromDate=@WarrantyFromDate," +
                         " WarrantyFromDateBS=@WarrantyFromDateBS, WarrantyToDate=@WarrantyToDate, WarrantyToDateBS=@WarrantyToDateBS," +
                         " LastUpdatedDate=@LastUpdatedDate, LastUpdatedBy=@LastUpdatedBy where IsDeleted=0 and ItemId= " + Id;

            using (var db = DbHelper.GetDBConnection())
            {
                var lst = db.Execute(sql, updateItem);
                db.Close();
                if (lst > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #9
0
        public ActionResult Create([Bind(Include = "ItemID,CompanyID,ItemName,UnitID,UserID,IsConfigurable,IsLock,Quantity,MinStockLimit,Created,Modified,Remarks,RejectedQuantity,CategoryID,ItemCode")] INV_Item iNV_Item)
        {
            #region Validation
            if (string.IsNullOrEmpty(iNV_Item.ItemName))
            {
                ModelState.AddModelError("ItemName", "Item is required");
            }
            if (!string.IsNullOrEmpty(iNV_Item.ItemName))
            {
                if (db.INV_Item.Where(I => I.ItemName == iNV_Item.ItemName && I.CategoryID == iNV_Item.CategoryID).Count() > 0)
                {
                    ModelState.AddModelError("ItemNameDuplicate", iNV_Item.ItemName + " Already added.");
                }
            }
            #endregion Validation

            if (ModelState.IsValid)
            {
                iNV_Item.Created   = DateTime.Now;
                iNV_Item.Modified  = DateTime.Now;
                iNV_Item.CompanyID = 4;

                #region Generate Item Code
                if (iNV_Item.CategoryID != null && iNV_Item.CategoryID > 0)
                {
                    Int32  NextNumber    = db.INV_Item.Where(i => i.CategoryID == iNV_Item.CategoryID).Count() + 1;
                    String PrefixForCode = db.INV_Category.Where(i => i.CategoryID == iNV_Item.CategoryID).FirstOrDefault().CategoryShortName;
                    iNV_Item.ItemCode = PrefixForCode + "-" + NextNumber;
                }
                else
                {
                    Int32  NextNumber    = db.INV_Item.Count() + 1;
                    String PrefixForCode = "ITM";
                    iNV_Item.ItemCode = PrefixForCode + "-" + NextNumber;
                }


                #endregion Generate Item Code

                if (Session["UserID"] != null)
                {
                    iNV_Item.UserID = Convert.ToInt16(Session["UserID"].ToString());
                }
                db.INV_Item.Add(iNV_Item);
                db.SaveChanges();

                #region Update INV_StockHistory
                int newPK = iNV_Item.ItemID;
                INV_StockHistory _iNV_StockHistory = new INV_StockHistory();
                _iNV_StockHistory.ItemID          = iNV_Item.ItemID;
                _iNV_StockHistory.OperationTypeID = 6;
                _iNV_StockHistory.Quantity        = iNV_Item.Quantity;
                _iNV_StockHistory.UserID          = iNV_Item.UserID;
                _iNV_StockHistory.FinYearID       = CommonConfig.GetFinYearID();
                _iNV_StockHistory.Created         = DateTime.Now;
                _iNV_StockHistory.Modified        = DateTime.Now;
                db.INV_StockHistory.Add(_iNV_StockHistory);
                db.SaveChanges();
                #endregion Update INV_StockHistory

                return(RedirectToAction("Index"));
            }

            ViewBag.CompanyID  = new SelectList(db.SYS_Company, "CompanyID", "CompanyName", iNV_Item.CompanyID);
            ViewBag.UnitID     = new SelectList(db.INV_Unit, "UnitID", "Unit", iNV_Item.UnitID);
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_Item.UserID);
            ViewBag.CategoryID = new SelectList(db.INV_Category, "CategoryID", "CategoryName", iNV_Item.CategoryID);
            return(View("Edit", iNV_Item));
        }
Beispiel #10
0
        public ActionResult Edit([Bind(Include = "ItemID,CompanyID,ItemName,UnitID,UserID,IsConfigurable,IsLock,Quantity,MinStockLimit,Created,Modified,Remarks,RejectedQuantity,CategoryID,ItemCode")] INV_Item iNV_Item)
        {
            if (iNV_Item.ItemID > 0)
            {
                if (iNV_Item.Remarks == null || iNV_Item.Remarks == "")
                {
                    ViewBag.CompanyID  = new SelectList(db.SYS_Company, "CompanyID", "CompanyName", iNV_Item.CompanyID);
                    ViewBag.UnitID     = new SelectList(db.INV_Unit, "UnitID", "Unit", iNV_Item.UnitID);
                    ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_Item.UserID);
                    ViewBag.CategoryID = new SelectList(db.INV_Category, "CategoryID", "CategoryName", iNV_Item.CategoryID);
                    ModelState.AddModelError("", "Enter Remarks");
                    return(View(iNV_Item));
                }
            }
            if (ModelState.IsValid)
            {
                db.Entry(iNV_Item).State = EntityState.Modified;
                iNV_Item.Modified        = DateTime.Now;
                iNV_Item.CompanyID       = 4;
                if (Session["UserID"] != null)
                {
                    iNV_Item.UserID = Convert.ToInt16(Session["UserID"].ToString());
                }
                #region Generate Item Code

                if (iNV_Item.ItemID <= 0)
                {
                    if (iNV_Item.CategoryID != null && iNV_Item.CategoryID > 0)
                    {
                        Int32  NextNumber    = db.INV_Item.Where(i => i.CategoryID == iNV_Item.CategoryID).Count() + 1;
                        String PrefixForCode = db.INV_Category.Where(i => i.CategoryID == iNV_Item.CategoryID).FirstOrDefault().CategoryShortName;
                        iNV_Item.ItemCode = PrefixForCode + "-" + NextNumber;
                    }
                    else
                    {
                        Int32  NextNumber    = db.INV_Item.Count() + 1;
                        String PrefixForCode = "ITM";
                        iNV_Item.ItemCode = PrefixForCode + "-" + NextNumber;
                    }
                }

                #endregion Generate Item Code

                db.SaveChanges();
                #region Update INV_StockHistory
                INV_StockHistory _iNV_StockHistoryOld = db.INV_StockHistory.Where(IS => IS.ItemID == iNV_Item.ItemID).FirstOrDefault();
                if (_iNV_StockHistoryOld != null)
                {
                    _iNV_StockHistoryOld.ItemID          = iNV_Item.ItemID;
                    _iNV_StockHistoryOld.OperationTypeID = 6;
                    _iNV_StockHistoryOld.Quantity        = iNV_Item.Quantity;
                    _iNV_StockHistoryOld.UserID          = iNV_Item.UserID;
                    _iNV_StockHistoryOld.FinYearID       = CommonConfig.GetFinYearID();
                    _iNV_StockHistoryOld.Modified        = DateTime.Now;
                    db.Entry(_iNV_StockHistoryOld).State = EntityState.Modified;
                    db.SaveChanges();
                }
                #endregion Update INV_StockHistory
                return(RedirectToAction("Index"));
            }
            ViewBag.CompanyID  = new SelectList(db.SYS_Company, "CompanyID", "CompanyName", iNV_Item.CompanyID);
            ViewBag.UnitID     = new SelectList(db.INV_Unit, "UnitID", "Unit", iNV_Item.UnitID);
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_Item.UserID);
            ViewBag.CategoryID = new SelectList(db.INV_Category, "CategoryID", "CategoryName", iNV_Item.CategoryID);
            return(View(iNV_Item));
        }
Beispiel #11
0
        public ActionResult RertunNewItem(INV_IssueReturnViewModal iNV_IssueReturnViewModal)
        {
            try
            {
                if (iNV_IssueReturnViewModal.IssueReturnItems.Count <= 0)
                {
                    TempData["errorReturn"] = "Select Item.";
                    return(View(iNV_IssueReturnViewModal));
                }

                #region Generate IssueReturnNo
                String _NewIssueReturnNo = CommonConfig.GetNextNumber("Return");
                #endregion Generate IssueReturnNo

                INV_IssueReturn iNV_IssueReturn = new INV_IssueReturn();
                iNV_IssueReturn.CompanyID = CommonConfig.GetCompanyID();

                if (Session["UserID"] != null)
                {
                    iNV_IssueReturn.IssueReturnByUserID = Convert.ToInt16(Session["UserID"].ToString());
                }
                iNV_IssueReturn.IssueReturnToUserID = iNV_IssueReturnViewModal.IssueReturnToUserID;
                iNV_IssueReturn.Created             = DateTime.Now;
                iNV_IssueReturn.Modified            = DateTime.Now;
                iNV_IssueReturn.Remarks             = "Return";
                iNV_IssueReturn.IssueReturnDate     = DateTime.Now;
                iNV_IssueReturn.IssueReturnNo       = _NewIssueReturnNo;
                iNV_IssueReturn.FinYearID           = CommonConfig.GetFinYearID();
                iNV_IssueReturn.ReturnIssueNo       = iNV_IssueReturnViewModal.IssueReturnNo;

                db.INV_IssueReturn.Add(iNV_IssueReturn);
                //db.SaveChanges();

                string Err = "";
                TempData["errorReturn"] = "";
                if (iNV_IssueReturnViewModal.IssueReturnItems != null)
                {
                    foreach (var item in iNV_IssueReturnViewModal.IssueReturnItems)
                    {
                        INV_Item inv_Item = new INV_Item();
                        inv_Item = db.INV_Item.Where(i => i.ItemID == item.ItemID).FirstOrDefault();

                        INV_StockHistory _StockHistoryNew = new INV_StockHistory();
                        _StockHistoryNew = db.INV_StockHistory.Where(w => w.IssueNumber == iNV_IssueReturnViewModal.IssueReturnNo && w.ItemID == item.ItemID).FirstOrDefault();

                        List <INV_StockHistory> _receivedStockHistory = new List <INV_StockHistory>();
                        _receivedStockHistory = db.INV_StockHistory.Where(w => w.IssueNumber == iNV_IssueReturnViewModal.IssueReturnNo && w.ItemID == item.ItemID && w.Remarks == "Return" && w.OperationTypeID == 9).ToList();

                        if (_StockHistoryNew != null)
                        {
                            if (_StockHistoryNew.Quantity < (item.Quantity + _receivedStockHistory.Sum(i => i.Quantity)))
                            {
                                if (Err == "")
                                {
                                    Err = Err + "You can not return more than issue. " + inv_Item.ItemName;
                                }
                                else
                                {
                                    Err = Err + ", You can not return more than issue. " + inv_Item.ItemName;
                                }
                            }
                        }

                        if (Session["UserID"] != null)
                        {
                            item.UserID = Convert.ToInt16(Session["UserID"].ToString());
                        }

                        INV_StockHistory new_INV_StockHistory = new INV_StockHistory();
                        new_INV_StockHistory.ItemID          = item.ItemID;
                        new_INV_StockHistory.OperationTypeID = 9;
                        new_INV_StockHistory.ReferenceID     = _NewIssueReturnNo;
                        new_INV_StockHistory.Quantity        = item.Quantity;
                        new_INV_StockHistory.UserID          = item.UserID;
                        new_INV_StockHistory.Created         = DateTime.Now;
                        new_INV_StockHistory.Modified        = DateTime.Now;
                        new_INV_StockHistory.Remarks         = "Return";
                        new_INV_StockHistory.FinYearID       = CommonConfig.GetFinYearID();
                        new_INV_StockHistory.IssueNumber     = iNV_IssueReturnViewModal.IssueReturnNo;
                        new_INV_StockHistory.ReturnNumber    = _NewIssueReturnNo;
                        db.INV_StockHistory.Add(new_INV_StockHistory);
                        if (iNV_IssueReturnViewModal.IsRejected)
                        {
                            //inv_Item.Quantity = Convert.ToInt32(inv_Item.RejectedQuantity) + item.Quantity;
                            if (inv_Item.RejectedQuantity == null)
                            {
                                inv_Item.RejectedQuantity = item.Quantity;
                            }
                            else
                            {
                                inv_Item.RejectedQuantity = inv_Item.RejectedQuantity + item.Quantity;
                            }
                        }
                        else
                        {
                            inv_Item.Quantity = inv_Item.Quantity + item.Quantity;
                        }
                    }
                    if (Err != "")
                    {
                        TempData["errorReturn"] = Err;
                        ViewData["errorReturn"] = TempData["errorReturn"];
                        return(View(iNV_IssueReturnViewModal));
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                //exception handiling
            }
            return(Json("failure", JsonRequestBehavior.AllowGet));
        }
Beispiel #12
0
        public ActionResult SaveForAssmeble(INV_IssueReturnViewModal iNV_IssueReturnViewModal)
        {
            try
            {
                if (iNV_IssueReturnViewModal.IssueReturnItems.Count <= 0)
                {
                    TempData["errorIssue"] = "Select Item.";
                    return(View(iNV_IssueReturnViewModal));
                }

                #region Generate IssueReturnNo
                String _NewIssueReturnNo = CommonConfig.GetNextNumber("Issue");
                #endregion Generate IssueReturnNo

                INV_IssueReturn iNV_IssueReturn = new INV_IssueReturn();
                if (Session["UserID"] != null)
                {
                    iNV_IssueReturn.IssueReturnByUserID = Convert.ToInt16(Session["UserID"].ToString());
                }
                iNV_IssueReturn.CompanyID           = CommonConfig.GetCompanyID();
                iNV_IssueReturn.IssueReturnToUserID = iNV_IssueReturnViewModal.IssueReturnToUserID;
                iNV_IssueReturn.Created             = DateTime.Now;
                iNV_IssueReturn.Modified            = DateTime.Now;
                iNV_IssueReturn.Remarks             = "Issue for Assemble";
                iNV_IssueReturn.IssueReturnDate     = DateTime.Now;
                iNV_IssueReturn.IssueReturnNo       = _NewIssueReturnNo;
                iNV_IssueReturn.FinYearID           = CommonConfig.GetFinYearID();

                db.INV_IssueReturn.Add(iNV_IssueReturn);
                db.SaveChanges();

                string Err = "";
                TempData["errorIssue"] = "";
                if (iNV_IssueReturnViewModal.IssueReturnItems != null)
                {
                    foreach (var item in iNV_IssueReturnViewModal.IssueReturnItems)
                    {
                        INV_Item inv_Item = new INV_Item();
                        inv_Item = db.INV_Item.Where(i => i.ItemID == item.ItemID).FirstOrDefault();
                        if (inv_Item.Quantity - item.Quantity < 0)
                        {
                            if (Err == "")
                            {
                                Err = Err + "Check Stock for " + inv_Item.ItemName;
                            }
                            else
                            {
                                Err = Err + ", Check Stock for " + inv_Item.ItemName;
                            }
                        }

                        if (Session["UserID"] != null)
                        {
                            item.UserID = Convert.ToInt16(Session["UserID"].ToString());
                        }

                        INV_StockHistory new_INV_StockHistory = new INV_StockHistory();
                        new_INV_StockHistory.ItemID          = item.ItemID;
                        new_INV_StockHistory.OperationTypeID = 8;
                        new_INV_StockHistory.ReferenceID     = _NewIssueReturnNo;
                        new_INV_StockHistory.Quantity        = item.Quantity;
                        new_INV_StockHistory.UserID          = item.UserID;
                        new_INV_StockHistory.Created         = DateTime.Now;
                        new_INV_StockHistory.Modified        = DateTime.Now;
                        new_INV_StockHistory.Remarks         = "Issue";
                        new_INV_StockHistory.FinYearID       = CommonConfig.GetFinYearID();

                        new_INV_StockHistory.IssueNumber = _NewIssueReturnNo;
                        db.INV_StockHistory.Add(new_INV_StockHistory);

                        inv_Item.Quantity = inv_Item.Quantity - item.Quantity;
                    }
                    if (Err != "")
                    {
                        TempData["errorIssue"] = Err;
                        return(View(iNV_IssueReturnViewModal));
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                //exception handiling
            }
            return(Json("failure", JsonRequestBehavior.AllowGet));
        }
        public ActionResult Create(INV_ItemConfiguration iNV_ItemConfiguration)
        {
            //if (db.INV_ItemConfiguration.Where(I => I.MainItemID == iNV_ItemConfiguration.MainItemID).Count() > 0)
            //{
            //    ModelState.AddModelError("ItemNameDuplicate", "Selected item is already configured.");
            //}

            if (iNV_ItemConfiguration.ItemConfigurationID > 0)
            {
                if (iNV_ItemConfiguration.Remarks == null || iNV_ItemConfiguration.Remarks == "")
                {
                    var items =
                        db.INV_Item
                        .Where(i => i.IsLock == true && i.IsConfigurable == true)
                        .Select(s => new
                    {
                        ItemID   = s.ItemID,
                        ItemName = s.ItemName + " - " + s.ItemCode
                    })
                        .ToList();
                    iNV_ItemConfiguration.INV_Items = db.INV_ItemConfiguration.Where(ic => ic.MainItemID == iNV_ItemConfiguration.MainItemID).ToList();
                    ViewBag.MainItemID = new SelectList(items, "ItemID", "ItemName", iNV_ItemConfiguration.MainItemID);
                    ViewBag.SubItemID  = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
                    ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_ItemConfiguration.UserID);
                    ModelState.AddModelError("", "Enter Remarks");
                    return(View(iNV_ItemConfiguration));
                }
            }

            if (iNV_ItemConfiguration.INV_Items != null)
            {
                if (ModelState.IsValid)
                {
                    iNV_ItemConfiguration.Modified = DateTime.Now;
                    if (Session["UserID"] != null)
                    {
                        iNV_ItemConfiguration.UserID = Convert.ToInt16(Session["UserID"].ToString());
                    }
                    var ToRemovedbINV_ItemConfiguration = db.INV_ItemConfiguration.Where(i => i.MainItemID == iNV_ItemConfiguration.MainItemID).ToList();
                    db.INV_ItemConfiguration.RemoveRange(ToRemovedbINV_ItemConfiguration);
                    db.SaveChanges();
                    String Err = "";
                    List <INV_ItemConfiguration> newINV_ItemConfiguration = new List <INV_ItemConfiguration>();
                    foreach (var item in iNV_ItemConfiguration.INV_Items)
                    {
                        if (item.Qunatity <= 0)
                        {
                            INV_Item _Subitem = db.INV_Item.Find(item.SubItemID);
                            if (_Subitem != null)
                            {
                                if (Err == "")
                                {
                                    Err = "Enter Valid Quantity for " + _Subitem.ItemName;
                                }
                                else
                                {
                                    Err += ", Enter Valid Quantity for " + _Subitem.ItemName;
                                }
                            }
                        }
                        INV_ItemConfiguration new_INV_InvoiceItem = new INV_ItemConfiguration();
                        new_INV_InvoiceItem.MainItemID = iNV_ItemConfiguration.MainItemID;
                        new_INV_InvoiceItem.SubItemID  = item.SubItemID;
                        new_INV_InvoiceItem.Qunatity   = item.Qunatity;
                        new_INV_InvoiceItem.Created    = DateTime.Now;
                        new_INV_InvoiceItem.Modified   = DateTime.Now;
                        new_INV_InvoiceItem.UserID     = iNV_ItemConfiguration.UserID;
                        new_INV_InvoiceItem.Remarks    = iNV_ItemConfiguration.Remarks;
                        newINV_ItemConfiguration.Add(new_INV_InvoiceItem);
                    }
                    if (Err != "")
                    {
                        var items =
                            db.INV_Item
                            .Where(i => i.IsLock == true && i.IsConfigurable == true)
                            .Select(s => new
                        {
                            ItemID   = s.ItemID,
                            ItemName = s.ItemName + " - " + s.ItemCode
                        })
                            .ToList();

                        ViewBag.MainItemID      = new SelectList(items, "ItemID", "ItemName");
                        ViewBag.SubItemID       = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
                        ViewBag.UserID          = new SelectList(db.SEC_User, "UserID", "UserName");
                        TempData["errorConfig"] = Err;
                        return(View(iNV_ItemConfiguration));
                    }

                    db.INV_ItemConfiguration.AddRange(newINV_ItemConfiguration);
                    db.SaveChanges();
                }
            }
            var itemsa =
                db.INV_Item
                .Where(i => i.IsLock == true && i.IsConfigurable == true)
                .Select(s => new
            {
                ItemID   = s.ItemID,
                ItemName = s.ItemName + " - " + s.ItemCode
            })
                .ToList();

            ViewBag.MainItemID = new SelectList(itemsa, "ItemID", "ItemName");
            ViewBag.SubItemID  = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName");
            INV_ItemConfigurationViewModal iNV_ItemConfigurationViewModal = new INV_ItemConfigurationViewModal();

            ViewData["errorConfig"] = TempData["errorConfig"];
            return(View("Edit", iNV_ItemConfigurationViewModal));
        }
        public ActionResult Edit(INV_ItemConfigurationViewModal iNV_ItemConfigurationViewModal)
        {
            if (iNV_ItemConfigurationViewModal.ItemConfigurationID > 0)
            {
                if (iNV_ItemConfigurationViewModal.Remarks == null || iNV_ItemConfigurationViewModal.Remarks == "")
                {
                    var items =
                        db.INV_Item
                        .Where(i => i.IsLock == true && i.IsConfigurable == true)
                        .Select(s => new
                    {
                        ItemID   = s.ItemID,
                        ItemName = s.ItemName + " - " + s.ItemCode
                    })
                        .ToList();
                    iNV_ItemConfigurationViewModal.SubItems = db.INV_ItemConfiguration.Where(ic => ic.MainItemID == iNV_ItemConfigurationViewModal.MainItemID).ToList();
                    ViewBag.MainItemID = new SelectList(items, "ItemID", "ItemName", iNV_ItemConfigurationViewModal.MainItemID);
                    ViewBag.SubItemID  = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
                    ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_ItemConfigurationViewModal.UserID);
                    ModelState.AddModelError("", "Enter Remarks");
                    return(View(iNV_ItemConfigurationViewModal));
                }
            }

            if (iNV_ItemConfigurationViewModal.SubItems.Count > 0)
            {
                if (ModelState.IsValid)
                {
                    INV_ItemConfiguration iNV_ItemConfiguration = db.INV_ItemConfiguration.Find(iNV_ItemConfigurationViewModal.ItemConfigurationID);

                    db.Entry(iNV_ItemConfiguration).State = EntityState.Modified;
                    iNV_ItemConfiguration.Modified        = DateTime.Now;
                    if (Session["UserID"] != null)
                    {
                        iNV_ItemConfiguration.UserID = Convert.ToInt16(Session["UserID"].ToString());
                    }
                    String Err = "";
                    TempData["errorIssue"] = "";
                    List <INV_ItemConfiguration> newINV_ItemConfiguration = new List <INV_ItemConfiguration>();
                    foreach (var item in iNV_ItemConfiguration.INV_Items)
                    {
                        if (item.Qunatity <= 0)
                        {
                            INV_Item _Subitem = db.INV_Item.Find(item.SubItemID);
                            if (_Subitem != null)
                            {
                                if (Err == "")
                                {
                                    Err = "Enter Valid Quantity for " + _Subitem.ItemName;
                                }
                                else
                                {
                                    Err += ", Enter Valid Quantity for " + _Subitem.ItemName;
                                }
                            }
                        }
                        INV_ItemConfiguration new_INV_InvoiceItem = new INV_ItemConfiguration();
                        new_INV_InvoiceItem.MainItemID = iNV_ItemConfiguration.MainItemID;
                        new_INV_InvoiceItem.SubItemID  = item.SubItemID;
                        new_INV_InvoiceItem.Qunatity   = item.Qunatity;
                        new_INV_InvoiceItem.Created    = DateTime.Now;
                        new_INV_InvoiceItem.Modified   = DateTime.Now;

                        INV_ItemConfiguration dbINV_ItemConfiguration = db.INV_ItemConfiguration.Where(i => i.MainItemID == item.MainItemID & i.SubItemID == item.SubItemID).FirstOrDefault();
                        if (dbINV_ItemConfiguration == null)
                        {
                            newINV_ItemConfiguration.Add(new_INV_InvoiceItem);
                        }
                    }
                    if (Err != "")
                    {
                        var items =
                            db.INV_Item
                            .Where(i => i.IsLock == true && i.IsConfigurable == true)
                            .Select(s => new
                        {
                            ItemID   = s.ItemID,
                            ItemName = s.ItemName + " - " + s.ItemCode
                        })
                            .ToList();

                        ViewBag.MainItemID      = new SelectList(items, "ItemID", "ItemName");
                        ViewBag.SubItemID       = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
                        ViewBag.UserID          = new SelectList(db.SEC_User, "UserID", "UserName");
                        TempData["errorConfig"] = Err;
                        return(View(iNV_ItemConfiguration));
                    }
                    db.INV_ItemConfiguration.AddRange(newINV_ItemConfiguration);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.MainItemID = new SelectList(db.INV_Item.Where(i => i.IsLock == true && i.IsConfigurable == true), "ItemID", "ItemName");
            ViewBag.SubItemID  = new SelectList(db.INV_Item.Where(i => i.IsLock == true), "ItemID", "ItemName");
            ViewBag.UserID     = new SelectList(db.SEC_User, "UserID", "UserName", iNV_ItemConfigurationViewModal.UserID);
            return(RedirectToAction("Index"));
        }
        public JsonResult AddInvoice(INV_InvoiceViewModal inv_InvoiceViewModal)
        {
            string validMsg = "";

            try
            {
                if (inv_InvoiceViewModal == null)
                {
                    //error meesage or expception handle
                }
                else if (inv_InvoiceViewModal.INV_InvoiceItems == null)
                {
                    //error meesage or expception handle
                }
                else
                {
                    if (inv_InvoiceViewModal.InvoiceID > 0)
                    {
                        int invoiceId = inv_InvoiceViewModal.InvoiceID;
                        foreach (var item in inv_InvoiceViewModal.INV_InvoiceItems)
                        {
                            var checkExist = db.INV_InvoiceItem.Any(e => e.InvoiceID == invoiceId &&
                                                                    e.ItemID == item.ItemID &&
                                                                    e.Quantity == item.Quantity);
                            if (checkExist == false)
                            {
                                var newINV_InvoiceItemAdd = new INV_InvoiceItem()
                                {
                                    InvoiceID    = invoiceId,
                                    ItemID       = item.ItemID,
                                    Quantity     = item.Quantity,
                                    Created      = DateTime.Now,
                                    Modified     = DateTime.Now,
                                    Remarks      = item.Remarks,
                                    PricePerUnit = item.PricePerUnit
                                };
                                db.INV_InvoiceItem.Add(newINV_InvoiceItemAdd);
                            }
                        }
                        inv_InvoiceViewModal.INV_InvoiceItems.ForEach(e =>
                        {
                            e.InvoiceID     = invoiceId;
                            e.InvoiceItemID = db.INV_InvoiceItem.Where(t => t.InvoiceID == invoiceId && t.ItemID == e.ItemID && t.Quantity == e.Quantity).FirstOrDefault().InvoiceItemID;
                        });
                        List <int>             getAllList  = inv_InvoiceViewModal.INV_InvoiceItems.Select(e => e.InvoiceItemID).ToList();
                        List <INV_InvoiceItem> removeRange = db.INV_InvoiceItem.Where(t => t.InvoiceID == invoiceId && !getAllList.Contains(t.InvoiceItemID)).Select(t => t).ToList();

                        db.INV_InvoiceItem.RemoveRange(removeRange);

                        INV_Invoice get_invoice = db.INV_Invoice.Where(e => e.InvoiceID == invoiceId).FirstOrDefault();

                        INV_InvoiceHistory Add_INV_InvoiceHistory = new INV_InvoiceHistory()
                        {
                            CompanyID      = get_invoice.CompanyID,
                            PartyID        = get_invoice.PartyID,
                            UserID         = get_invoice.UserID,
                            Amount         = get_invoice.Amount,
                            AmountReceived = get_invoice.AmountReceived,
                            StatusID       = get_invoice.StatusID,
                            Created        = get_invoice.Created,
                            Remarks        = get_invoice.Remarks,
                            InvoiceDate    = get_invoice.InvoiceDate,
                            InvoiceNo      = get_invoice.InvoiceNo,
                            PONo           = get_invoice.PONo,
                            AmountPending  = get_invoice.AmountPending,
                            FinYearID      = get_invoice.FinYearID,
                            CGST           = get_invoice.CGST,
                            CGSTAmount     = get_invoice.CGSTAmount,
                            SGST           = get_invoice.SGST,
                            SGSTAmount     = get_invoice.SGSTAmount,
                            IGST           = get_invoice.IGST,
                            IGSTAmount     = get_invoice.IGSTAmount,
                            IsLocal        = get_invoice.IsLocal,
                            //IsActive = get_invoice.IsActive==null? true : get_invoice.IsActive,//ask to kamal
                            Casar       = get_invoice.Casar,
                            TotalAmount = get_invoice.TotalAmount,
                            Operation   = "Invoice",
                            InvoiceID   = invoiceId
                        };
                        db.INV_InvoiceHistory.Add(Add_INV_InvoiceHistory);

                        if (Session["UserID"] != null)
                        {
                            get_invoice.UserID = Convert.ToInt16(Session["UserID"].ToString());
                        }

                        get_invoice.Amount         = inv_InvoiceViewModal.Amount;
                        get_invoice.AmountReceived = inv_InvoiceViewModal.AmountReceived;
                        get_invoice.StatusID       = 1;
                        get_invoice.Modified       = DateTime.Now;
                        get_invoice.Remarks        = inv_InvoiceViewModal.Remarks;
                        get_invoice.InvoiceDate    = inv_InvoiceViewModal.InvoiceDate;//ask to kamal
                        get_invoice.InvoiceNo      = get_invoice.InvoiceNo;
                        get_invoice.PONo           = inv_InvoiceViewModal.PONo;
                        get_invoice.AmountPending  = inv_InvoiceViewModal.AmountPending;
                        get_invoice.CGST           = inv_InvoiceViewModal.CGST;
                        get_invoice.CGSTAmount     = inv_InvoiceViewModal.CGSTAmount;
                        get_invoice.SGST           = inv_InvoiceViewModal.SGST;
                        get_invoice.SGSTAmount     = inv_InvoiceViewModal.SGSTAmount;
                        get_invoice.IGST           = inv_InvoiceViewModal.IGST;
                        get_invoice.IGSTAmount     = inv_InvoiceViewModal.IGSTAmount;
                        get_invoice.IsLocal        = inv_InvoiceViewModal.IsLocal;
                        get_invoice.Casar          = inv_InvoiceViewModal.Casar;
                        get_invoice.TotalAmount    = inv_InvoiceViewModal.TotalAmount;
                    }
                    else
                    {
                        INV_Invoice new_INV_Invoice = new INV_Invoice();
                        new_INV_Invoice.CompanyID = CommonConfig.GetCompanyID();
                        new_INV_Invoice.StatusID  = CommonConfig.GetStatusPending();
                        new_INV_Invoice.PartyID   = inv_InvoiceViewModal.PartyID;
                        if (Session["UserID"] != null)
                        {
                            new_INV_Invoice.UserID = Convert.ToInt16(Session["UserID"].ToString());
                        }

                        new_INV_Invoice.Amount         = inv_InvoiceViewModal.Amount;
                        new_INV_Invoice.AmountReceived = inv_InvoiceViewModal.AmountReceived;
                        new_INV_Invoice.StatusID       = CommonConfig.GetStatusPending();
                        new_INV_Invoice.Created        = DateTime.Now;
                        new_INV_Invoice.Modified       = DateTime.Now;
                        new_INV_Invoice.Remarks        = inv_InvoiceViewModal.Remarks;
                        new_INV_Invoice.InvoiceDate    = DateTime.Now;

                        #region Generate InvoiceNo
                        String _NewInvoiceNo = CommonConfig.GetNextNumber("Invoice");
                        #endregion Generate InvoiceNo
                        new_INV_Invoice.InvoiceNo = _NewInvoiceNo.ToString();

                        new_INV_Invoice.PONo          = inv_InvoiceViewModal.PONo;
                        new_INV_Invoice.AmountPending = 0;
                        new_INV_Invoice.FinYearID     = CommonConfig.GetFinYearID();
                        new_INV_Invoice.CGST          = inv_InvoiceViewModal.CGST == 0 ? null : inv_InvoiceViewModal.CGST;
                        new_INV_Invoice.CGSTAmount    = inv_InvoiceViewModal.CGST == 0 ? null : inv_InvoiceViewModal.CGSTAmount;
                        new_INV_Invoice.SGST          = inv_InvoiceViewModal.SGST == 0 ? null : inv_InvoiceViewModal.SGST;
                        new_INV_Invoice.SGSTAmount    = inv_InvoiceViewModal.SGST == 0 ? null : inv_InvoiceViewModal.SGSTAmount;
                        new_INV_Invoice.IGST          = inv_InvoiceViewModal.IGST == 0 ? null : inv_InvoiceViewModal.IGST;
                        new_INV_Invoice.IGSTAmount    = inv_InvoiceViewModal.IGST == 0 ? null : inv_InvoiceViewModal.IGSTAmount;
                        new_INV_Invoice.IsLocal       = inv_InvoiceViewModal.IsLocal;
                        new_INV_Invoice.IsActive      = true;
                        new_INV_Invoice.Casar         = inv_InvoiceViewModal.Casar;
                        new_INV_Invoice.TotalAmount   = inv_InvoiceViewModal.TotalAmount;

                        db.INV_Invoice.Add(new_INV_Invoice);

                        int newInvoiceId = new_INV_Invoice.InvoiceID;
                        List <INV_InvoiceItem> newList_INV_InvoiceItem = new List <INV_InvoiceItem>();
                        foreach (var item in inv_InvoiceViewModal.INV_InvoiceItems)
                        {
                            INV_InvoiceItem new_INV_InvoiceItem = new INV_InvoiceItem();
                            new_INV_InvoiceItem.InvoiceItemID = item.InvoiceItemID;
                            new_INV_InvoiceItem.InvoiceID     = newInvoiceId;
                            new_INV_InvoiceItem.ItemID        = item.ItemID;
                            new_INV_InvoiceItem.Quantity      = item.Quantity;
                            new_INV_InvoiceItem.Created       = DateTime.Now;
                            new_INV_InvoiceItem.Modified      = DateTime.Now;
                            new_INV_InvoiceItem.Remarks       = item.Remarks;
                            new_INV_InvoiceItem.PricePerUnit  = item.PricePerUnit;

                            #region INV_ItemPrice
                            INV_ItemPrice _iNV_ItemPrice = new INV_ItemPrice();
                            _iNV_ItemPrice = db.INV_ItemPrice.Where(M => M.ItemID == item.ItemID && M.PurchasePrice == item.PricePerUnit).OrderByDescending(o => o.Created).FirstOrDefault();
                            if (_iNV_ItemPrice == null)
                            {
                                _iNV_ItemPrice               = new INV_ItemPrice();
                                _iNV_ItemPrice.ItemID        = item.ItemID;
                                _iNV_ItemPrice.PurchasePrice = item.PricePerUnit;
                                _iNV_ItemPrice.Created       = DateTime.Now;
                                _iNV_ItemPrice.Modified      = DateTime.Now;
                                _iNV_ItemPrice.FinYearID     = CommonConfig.GetFinYearID();
                                if (Session["UserID"] != null)
                                {
                                    _iNV_ItemPrice.UserID = Convert.ToInt16(Session["UserID"].ToString());
                                }
                                db.INV_ItemPrice.Add(_iNV_ItemPrice);
                            }
                            #endregion INV_ItemPrice

                            INV_Item _INV_Item = new INV_Item();
                            _INV_Item = db.INV_Item.Where(i => i.ItemID == item.ItemID).FirstOrDefault();
                            if (_INV_Item != null)
                            {
                                if (_INV_Item.Quantity - item.Quantity < 0)
                                {
                                    validMsg += "<br>Insufficient Stock for " + _INV_Item.ItemName;
                                    ModelState.AddModelError("Insufficient Stock", "Insufficient Stock for " + _INV_Item.ItemName);
                                    //return Json("failure", JsonRequestBehavior.AllowGet);
                                }
                                _INV_Item.Quantity = _INV_Item.Quantity - item.Quantity;


                                INV_StockHistory new_INV_StockHistory = new INV_StockHistory();
                                new_INV_StockHistory.ItemID          = item.ItemID;
                                new_INV_StockHistory.OperationTypeID = 8;
                                new_INV_StockHistory.ReferenceID     = _NewInvoiceNo.ToString();
                                new_INV_StockHistory.Quantity        = item.Quantity;
                                if (Session["UserID"] != null)
                                {
                                    new_INV_StockHistory.UserID = Convert.ToInt16(Session["UserID"].ToString());
                                }
                                new_INV_StockHistory.Created   = DateTime.Now;
                                new_INV_StockHistory.Modified  = DateTime.Now;
                                new_INV_StockHistory.Remarks   = "Issue";
                                new_INV_StockHistory.FinYearID = CommonConfig.GetFinYearID();

                                new_INV_StockHistory.IssueNumber = _NewInvoiceNo;
                                db.INV_StockHistory.Add(new_INV_StockHistory);
                            }
                            newList_INV_InvoiceItem.Add(new_INV_InvoiceItem);
                        }
                        db.INV_InvoiceItem.AddRange(newList_INV_InvoiceItem);
                    }
                }
                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                    return(Json("Sucess", JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(validMsg, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json("failure", JsonRequestBehavior.AllowGet));
            }
        }