public ActionResult Create(SUB_ITEM sub_item)
        {
            if (ModelState.IsValid)
            {
                db.SUB_ITEM.Add(sub_item);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(sub_item);
        }
 public ActionResult Edit(SUB_ITEM sub_item)
 {
     if (ModelState.IsValid)
     {
         db.Entry(sub_item).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(sub_item);
 }
        public ActionResult Edit(string modomultiple, int? page, string id = null)
        {
            int pageIndex = 0;
            int pageSize = 50;
            int PageVendor = 0;
            int pageSubItem = 0;
            int nYear = 0;
            int nMonth = 0;
            int nDay = 0;
            string szPicPath = "";
            string szMsg = "";
            DateTime dDate = DateTime.Now;

            TimelyDepotContext db01 = new TimelyDepotContext();

            List<KeyValuePair<string, string>> listSelector = new List<KeyValuePair<string, string>>();

            IQueryable<VendorItem> qryvendoritem = null;
            IQueryable<SUB_ITEM> qrySubItem = null;
            IQueryable<PRICE> qryForPrice = null;
            IQueryable<DiscountManage> qryDisc = null;
            IQueryable<Deptos> qryDepto = null;
            IQueryable<ClssNos> qryClas = null;
            IQueryable<YearProducts> qryYearProduct = null;

            VendorsContactAddress vendorcontactadress = null;
            VendorItem vendoritem = null;
            SUB_ITEM subitem = null;
            PRICE forprice = null;

            List<VendorItem> vendoritemList = new List<VendorItem>();
            List<SUB_ITEM> subitemList = new List<SUB_ITEM>();
            List<PRICE> forpriceList = new List<PRICE>();

            ITEM item = db.ITEMs.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }

            //Replace new line
            item.DescA = item.DescA.Replace("\r\n", "<br />");
            item.DescB = item.DescB.Replace("\r\n", "<br />");

            //Set the image path
            szPicPath = string.Format("~/Images/timely/Small_Pic/{0}.gif", item.ItemID);
            ViewBag.PicPath = Url.Content(szPicPath);

            listSelector = new List<KeyValuePair<string, string>>();
            //qryvendoritem = db.VendorItems.Where(cutadd => cutadd.ItemId == item.ItemID).OrderBy(cutadd => cutadd.VendorNo);
            qryvendoritem = db.VendorItems.Where(cutadd => cutadd.ItemId == item.ItemID && cutadd.VendorNo != null).OrderBy(cutadd => cutadd.VendorNo);
            if (qryvendoritem.Count() > 0)
            {
                //custSalesContact = qryCusSal.FirstOrDefault<CustomersSalesContact>();
                foreach (var vendoritemHlp in qryvendoritem)
                {
                    vendoritemList.Add(vendoritemHlp);

                    var qryVendor = db01.VendorsContactAddresses.Join(db01.Vendors, vdca => vdca.VendorId, vd => vd.Id, (vdca, vd)
                        => new { vdca, vd }).Where(Nvdca => Nvdca.vd.VendorNo == vendoritemHlp.VendorNo).OrderBy(Nvdca => Nvdca.vdca.CompanyName);
                    if (qryVendor.Count() > 0)
                    {
                        foreach (var itemVendor in qryVendor)
                        {
                            szMsg = string.Format("{0} - {1} -({2})", itemVendor.vdca.CompanyName, itemVendor.vd.VendorNo, vendoritemHlp.Id.ToString());
                            listSelector.Add(new KeyValuePair<string, string>(itemVendor.vd.VendorNo, szMsg));
                            //listSelector.Add(new KeyValuePair<string, string>(vendoritemHlp.Id.ToString(), vendoritemHlp.VendorNo));
                            if (vendoritem == null)
                            {
                                vendoritem = vendoritemHlp;
                            }

                        }
                    }

                }
            }
            else
            {
                //nYear = dDate.Year;
                //nMonth = dDate.Month;
                //nDay = dDate.Day;
                //dDate = new DateTime(nYear, nMonth, nDay, 0, 0, 0);
                //vendoritem = new VendorItem();
                //vendoritem.ItemId = item.ItemID;
                //db.VendorItems.Add(vendoritem);
                //db.SaveChanges();
                //vendoritemList.Add(vendoritem);
            }
            SelectList vendorItemlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.VendorItemList = vendorItemlist;
            ViewBag.VendorItemListHas = listSelector.Count;

            qrySubItem = db.SUB_ITEM.Where(cutadd => cutadd.ItemID == item.ItemID).OrderBy(cutadd => cutadd.Description);
            if (qrySubItem.Count() > 0)
            {
                //custSalesContact = qryCusSal.FirstOrDefault<CustomersSalesContact>();
                foreach (var vendoritemHlp in qrySubItem)
                {
                    subitemList.Add(vendoritemHlp);
                }
            }
            else
            {
                subitem = new SUB_ITEM();
                subitem.ItemID = item.ItemID;
                db.SUB_ITEM.Add(subitem);
                db.SaveChanges();
                subitemList.Add(subitem);
            }

            //Get the setupforprice data
            qryForPrice = db.PRICEs.Where(FrPr => FrPr.Item == item.ItemID).OrderBy(FrPr => FrPr.Qty);
            if (qryForPrice.Count() > 0)
            {
                foreach (var itemForPrice in qryForPrice)
                {
                    forpriceList.Add(itemForPrice);
                }

            }
            else
            {
                forprice = new PRICE();
                forprice.Item = item.ItemID;
                forprice.Discount_Code = "R";
                db.PRICEs.Add(forprice);
                db.SaveChanges();
                forpriceList.Add(forprice);
            }

            //Get the setupcagrdedetail
            SetupChargeDetail setupchargedetail = db.SetupChargeDetails.Where(stchd => stchd.itemid == item.ItemID).FirstOrDefault<SetupChargeDetail>();
            if (setupchargedetail == null)
            {
                setupchargedetail = new SetupChargeDetail();
                setupchargedetail.itemid = item.ItemID;
                setupchargedetail.ReSetupCharge = 0;
                setupchargedetail.ReSetupChargeDiscountCode = "V";
                setupchargedetail.SetUpCharge = 0;
                setupchargedetail.SetupChargeDiscountCode = "V";
                setupchargedetail.RunCharge = 0;
                setupchargedetail.RunChargeDiscountCode = "V";
                db.SetupChargeDetails.Add(setupchargedetail);
                db.SaveChanges();
            }

            //Get the VendorNo select list
            listSelector = new List<KeyValuePair<string, string>>();
            var qryVendorHlp = db.VendorsContactAddresses.Join(db.Vendors, vdca => vdca.VendorId, vd => vd.Id, (vdca, vd)
                => new { vdca, vd }).OrderBy(Nvdca => Nvdca.vdca.CompanyName);
            if (qryVendorHlp.Count() > 0)
            {
                foreach (var itemAllVendors in qryVendorHlp)
                {
                    szMsg = string.Format("{0} - {1}", itemAllVendors.vdca.CompanyName, itemAllVendors.vd.VendorNo);
                    listSelector.Add(new KeyValuePair<string, string>(itemAllVendors.vd.VendorNo, szMsg));
                }
            }
            SelectList vendornolist = new SelectList(listSelector, "Key", "Value");
            ViewBag.VendorNoList = vendornolist;

            //Get theActive/Inactive list
            listSelector = new List<KeyValuePair<string, string>>();
            listSelector.Add(new KeyValuePair<string, string>("Y", "Active"));
            listSelector.Add(new KeyValuePair<string, string>("N", "Inactive"));
            SelectList activeinactivelist = new SelectList(listSelector, "Key", "Value");
            ViewBag.ActiveInactivelist = activeinactivelist;

            //Get the discount code
            listSelector = new List<KeyValuePair<string, string>>();
            qryDisc = db.DiscountManages.OrderBy(dimg => dimg.DiscountName);
            if (qryDisc.Count() > 0)
            {
                foreach (var itemdiscount in qryDisc)
                {
                    listSelector.Add(new KeyValuePair<string, string>(itemdiscount.DiscountName, itemdiscount.DiscountName));
                }
            }
            SelectList discountlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.DiscountList = discountlist;

            //Get the 1st set up
            listSelector = new List<KeyValuePair<string, string>>();
            szMsg = "Yes";
            listSelector.Add(new KeyValuePair<string, string>("1", szMsg));
            szMsg = "No";
            listSelector.Add(new KeyValuePair<string, string>("0", szMsg));
            SelectList yesnolist = new SelectList(listSelector, "Key", "Value");
            ViewBag.YesNoList = yesnolist;

            //if (vendoritem == null)
            //{
            //    vendoritem = new VendorItem();
            //    vendoritem.UpdateDate = DateTime.Now;
            //    vendoritem.LeadTimeHrs = 0;
            //    vendoritem.LeadTimeMin = 0;
            //    vendoritem.LeadTimeSec = 0;
            //}
            //if (vendoritem.UpdateDate == null)
            //{
            //    vendoritem.UpdateDate = DateTime.Now;
            //}
            //if (vendoritem.LeadTimeHrs == null)
            //{
            //    vendoritem.LeadTimeHrs = 0;
            //}
            //if (vendoritem.LeadTimeMin == null)
            //{
            //    vendoritem.LeadTimeMin = 0;
            //}
            //if (vendoritem.LeadTimeSec == null)
            //{
            //    vendoritem.LeadTimeSec = 0;
            //}

            ItemView itemview = new ItemView();
            itemview.item = item;
            itemview.vendoritem = vendoritem;
            itemview.vendorcontactaddress = vendorcontactadress;
            itemview.setupchragedetail = setupchargedetail;

            if (vendoritem == null)
            {
                ViewBag.VendorItemID = 0;
            }
            else
            {
                ViewBag.VendorItemID = vendoritem.Id;
            }

            //Get the Depto No List
            listSelector = new List<KeyValuePair<string, string>>();
            qryDepto = db.Deptos.OrderBy(dimg => dimg.DeptoNo);
            if (qryDepto.Count() > 0)
            {
                foreach (var itemdepto in qryDepto)
                {
                    listSelector.Add(new KeyValuePair<string, string>(itemdepto.DeptoNo, itemdepto.DeptoNo));
                }
            }
            SelectList deptolist = new SelectList(listSelector, "Key", "Value");
            ViewBag.DeptoList = deptolist;

            //Get the Class No List
            listSelector = new List<KeyValuePair<string, string>>();
            qryClas = db.ClssNos.OrderBy(dimg => dimg.ClssNo);
            if (qryClas.Count() > 0)
            {
                foreach (var itemdepto in qryClas)
                {
                    listSelector.Add(new KeyValuePair<string, string>(itemdepto.ClssNo, itemdepto.ClssNo));
                }
            }
            SelectList classlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.ClassList = classlist;

            //Get the Year Product List
            listSelector = new List<KeyValuePair<string, string>>();
            qryYearProduct = db.YearProducts.OrderBy(dimg => dimg.YearofProducts);
            if (qryYearProduct.Count() > 0)
            {
                foreach (var itemdepto in qryYearProduct)
                {
                    listSelector.Add(new KeyValuePair<string, string>(itemdepto.YearofProducts, itemdepto.YearofProducts));
                }
            }
            SelectList yearproductlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.YearProductList = yearproductlist;

            //Set the page
            if (page == null)
            {
                pageIndex = 1;
            }
            else
            {
                pageIndex = Convert.ToInt32(page);
            }

            if (string.IsNullOrEmpty(modomultiple))
            {
                PageVendor = pageIndex;
                pageSubItem = pageIndex;
                //pageSales = pageIndex;
                //pageShipp = pageIndex;
            }
            else
            {
                switch (modomultiple)
                {
                    case "VendorItem":
                        PageVendor = pageIndex;
                        break;
                    case "SubItem":
                        pageSubItem = pageIndex;
                        break;
                    default:
                        break;
                }
            }

            var vendoritemListHlp = vendoritemList.ToPagedList(PageVendor, 1);
            ViewBag.OnePageOfvendoritemData = vendoritemListHlp;
            itemview.vendorsitemList = vendoritemListHlp;

            var subitemListHlp = subitemList.ToPagedList(pageSubItem, pageSize);
            ViewBag.OnePageOfsubitemData = vendoritemListHlp;
            itemview.subitemsList = subitemListHlp;

            var forpriceListHlp = forpriceList.ToPagedList(pageSubItem, pageSize);
            ViewBag.onePageofPriceData = forpriceListHlp;
            itemview.setupforpriceList = forpriceListHlp;

            //Set the active tab
            if (TempData["ActiveTab"] != null)
            {
                ViewBag.ActiveTab = TempData["ActiveTab"].ToString();
            }

            //Set the Edit Vendor action
            if (TempData["EditVendor"] != null)
            {
                if (TempData["EditVendor"].ToString() == "EditVendor")
                {
                    ViewBag.EditVendor = "EditVendor";
                }
            }

            return View(itemview);
        }
        public ActionResult UpdateSubItem(SUB_ITEM subitem)
        {
            if (ModelState.IsValid)
            {
                db.Entry(subitem).State = EntityState.Modified;
                db.SaveChanges();
                //return RedirectToAction("Index");
                return RedirectToAction("Edit", new { id = subitem.ItemID });
            }

            return RedirectToAction("Index");
        }
        public PartialViewResult CreateEditSubItem(string szItemId, int Id = 0)
        {
            SUB_ITEM subitem = null;

            if (Id == 0)
            {
                subitem = new SUB_ITEM();
                subitem.ItemID = szItemId;
                subitem.Sub_ItemID = szItemId;
                db.SUB_ITEM.Add(subitem);
                db.SaveChanges();
            }
            else
            {
                subitem = db.SUB_ITEM.Find(Id);
            }

            //ViewBag.ItemId = szItemId;

            return PartialView(subitem);
        }
        //
        //
        public ActionResult UpdateAddSubitem(string itemID, string subitemId, string description, string onhand, string oSO, string oPO, string partno, string vendorstock, int id = 0)
        {
            double dHlp = 0;
            SUB_ITEM subitem = null;

            if (string.IsNullOrEmpty(onhand))
            {
                onhand = "0";
            }
            if (string.IsNullOrEmpty(oSO))
            {
                oSO = "0";
            }
            if (string.IsNullOrEmpty(oPO))
            {
                oPO = "0";
            }

            if (id == 0)
            {
                if (string.IsNullOrEmpty(onhand))
                {
                    onhand = "0";
                }
                if (string.IsNullOrEmpty(oSO))
                {
                    oSO = "0";
                }
                if (string.IsNullOrEmpty(oPO))
                {
                    oPO = "0";
                }
                if (string.IsNullOrEmpty(vendorstock))
                {
                    vendorstock = "0";
                }

                subitem = new SUB_ITEM();
                subitem.ItemID = itemID;
                subitem.Sub_ItemID = subitemId;
                subitem.Description = description;
                dHlp = Convert.ToDouble(onhand);
                subitem.OnHand_Db = dHlp;
                subitem.OnHand_Cr = 0;
                dHlp = Convert.ToDouble(oSO);
                subitem.OpenSO_Db = dHlp;
                subitem.OpenSO_Cr = 0;
                dHlp = Convert.ToDouble(oPO);
                subitem.OpenPO_Db = dHlp;
                subitem.OpenPO_Cr = 0;
                subitem.PartNo = partno;
                dHlp = Convert.ToDouble(vendorstock);
                subitem.VendorStock = dHlp;
                db.SUB_ITEM.Add(subitem);
                db.SaveChanges();
            }
            else
            {
                subitem = db.SUB_ITEM.Find(id);
                if (subitem != null)
                {
                    subitem.Sub_ItemID = subitemId;
                    subitem.Description = description;
                    dHlp = Convert.ToDouble(onhand);
                    subitem.OnHand_Db = dHlp;
                    subitem.OnHand_Cr = 0;
                    dHlp = Convert.ToDouble(oSO);
                    subitem.OpenSO_Db = dHlp;
                    subitem.OpenSO_Cr = 0;
                    dHlp = Convert.ToDouble(oPO);
                    subitem.OpenPO_Db = dHlp;
                    subitem.OpenPO_Cr = 0;
                    subitem.PartNo = partno;
                    dHlp = Convert.ToDouble(vendorstock);
                    subitem.VendorStock = dHlp;
                    db.Entry(subitem).State = EntityState.Modified;
                    db.SaveChanges();

                    if (string.IsNullOrEmpty(itemID))
                    {
                        itemID = subitem.ItemID;
                    }

                }
            }

            TempData["ActiveTab"] = "1";
            return RedirectToAction("Edit", new { id = itemID });
        }