예제 #1
0
        public ActionResult Create([Bind(Include = "Id,Name,Phone,Email,CompanyID")] Client client)
        {
            if (ModelState.IsValid)
            {
                db.Clients.Add(client);
                db.SaveChanges();
                return(RedirectToAction("Index", new { CompanyId = client.CompanyID }));
            }

            return(View(client));
        }
예제 #2
0
        public ActionResult Create(VendorViewModel vvm)
        {
            if (ModelState.IsValid)
            {
                Vendor vendor = new Vendor(vvm);
                db.Vendors.Add(vendor);
                db.SaveChanges();
                return(RedirectToAction("Index", new { companyId = vendor.CompanyId }));
            }

            return(View(vvm));
        }
예제 #3
0
        public ActionResult Create([Bind(Include = "Name,CompanyId")] Shape shape)
        {
            if (ModelState.IsValid)
            {
                db.Shapes.Add(shape);
                db.SaveChanges();
                return(RedirectToAction("Index", new { shape.CompanyId }));
            }
            ViewBag.CompanyId   = shape.CompanyId;
            ViewBag.CompanyName = db._Companies.Find(shape.CompanyId)?.Name;

            return(View(shape));
        }
예제 #4
0
        public ActionResult Create([Bind(Include = "Id,Code,Desc,Market,Multiplier,CompanyId")] MetalCode metalCode)
        {
            if (ModelState.IsValid)
            {
                db.MetalCodes.Add(metalCode);
                db.SaveChanges();
                return(RedirectToAction("Index", new { metalCode.CompanyId }));
            }
            ViewBag.CompanyId   = metalCode.CompanyId;
            ViewBag.CompanyName = db._Companies.Find(metalCode.CompanyId)?.Name;

            return(View(metalCode));
        }
예제 #5
0
        public ActionResult Create([Bind(Include = "Id,Name,PackagingCost,FinishingCost,CompanyId,bUseLaborTable")] JewelryType jewelryType)
        {
            if (ModelState.IsValid)
            {
                db.JewelryTypes.Add(jewelryType);
                db.SaveChanges();
                return(RedirectToAction("Index", new { companyId = jewelryType.CompanyId }));
            }
            ViewBag.CompanyId      = jewelryType.CompanyId;
            ViewBag.CompanyName    = db._Companies.Find(jewelryType.CompanyId)?.Name;
            ViewBag.CompanyHasLTIs = db.LaborTable.Where(lti => lti.CompanyId == jewelryType.CompanyId).Count() == 0;

            return(View(jewelryType));
        }
예제 #6
0
        public ActionResult Create(CollectionModel cm)
        {
            Collection collection = new Collection(cm);

            if (ModelState.IsValid)
            {
                db.Collections.Add(collection);
                db.SaveChanges();
                return(RedirectToAction("Index", new { CompanyId = collection.CompanyId }));
            }

            ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name", collection.CompanyId);
            return(View(cm));
        }
예제 #7
0
        public ActionResult Create(PresenterViewModel pvm)
        {
            if (ModelState.IsValid)
            {
                pvm.Location.Phone = GetNormalizedPhone(pvm.Phone);
                db.Presenters.Add(pvm.Location);
                SavePresenters(pvm.Location.Id, pvm.contacts);
                db.SaveChanges();
                return(RedirectToAction("Index", new { companyId = pvm.Location.CompanyId }));
            }

            pvm.Location.Company = db.FindCompany(pvm.Location.CompanyId);
            ViewBag.CompanyName  = pvm.Location.Name;
            return(View(pvm));
        }
예제 #8
0
        public ActionResult EditUser(EditUserViewModel evm)
        {
            OJewelryDB           db   = new OJewelryDB();
            ApplicationDbContext sec  = new ApplicationDbContext();
            ApplicationUser      user = sec.Users.Find(evm.UserId);
            var UserManager           = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(sec));


            if (ModelState.IsValid)
            {
                // Update the companies user can access
                UpdateCompaniesUsers(user, db, evm.Companies);
                // Update Role
                UpdateRoles(sec, user, evm.RoleId);
                db.SaveChanges();
                //sec.SaveChanges();
                return(RedirectToAction("UserList"));
            }
            evm.UserName  = user.UserName;
            ViewBag.Roles = sec.Roles.ToList();
            evm.Companies.Clear();
            foreach (Company c in db.Companies) // should be company left outer joined to users by id, exclude Managers, admins
            {
                CompanyAuthorizedUser cau = new CompanyAuthorizedUser()
                {
                    bIncluded   = false, // based on users for this company
                    CompanyId   = c.Id,
                    CompanyName = c.Name
                };
                evm.Companies.Add(cau);
            }
            return(View(evm));
        }
        public ActionResult Index(MarkupModel mm)
        {
            int i = 0;

            foreach (Markup m in mm.markups)
            {
                if (m.ratio < 0)
                {
                    ModelState.AddModelError($"markups[{i}].ratio", "The Markup (%) cannot be tess than 0. ");
                }
                if (m.margin < 0)
                {
                    ModelState.AddModelError($"markups[{i}].margin", "The Margin (%) cannot be less than 0. ");
                }
                if (m.margin > 100)
                {
                    ModelState.AddModelError($"markups[{i}].margin", "The Margin (%) cannot be more than 100. ");
                }
                if (m.multiplier < 0)
                {
                    ModelState.AddModelError($"markups[{i}].multiplier", "The Multiplier (%) cannot be less than 0. ");
                }
                if (m.ratio != 0 && m.margin != 0)
                {
                    ModelState.AddModelError($"markups[{i}].ratio", "You can only use one of Markup and Margin. ");
                }
                i++;
            }
            if (ModelState.IsValid)
            {
                // Save in Company
                Company       company       = db.FindCompany(mm.CompanyId);
                List <Markup> markupsToSave = new List <Markup>();
                foreach (Markup m in mm.markups)
                {
                    switch (m.State)
                    {
                    case MMState.Added:
                    case MMState.Clean:
                    case MMState.Dirty:
                        m.State = MMState.Dirty;
                        markupsToSave.Add(m);
                        break;

                    default:
                        break;
                    }
                }
                company.markup = JsonConvert.SerializeObject(markupsToSave);
                db.SaveChanges();
                return(RedirectToAction("Index", "Companies"));
            }
            else
            {
                return(View(mm));
            }
        }
예제 #10
0
        public ActionResult Create([Bind(Include = "Id,CompanyId,VendorId,Name,Desc,Price,Weight,Qty,Note")] Finding finding)
        {
            Finding existingFinding = db.Findings.Where(f => f.CompanyId == finding.CompanyId && f.Name == finding.Name).FirstOrDefault();

            if (existingFinding != null)
            {
                ModelState.AddModelError("Name", $"A finding named '{finding.Name}' already exists. ");
            }
            if (ModelState.IsValid)
            {
                db.Findings.Add(finding);
                db.SaveChanges();
                return(RedirectToAction("Edit", new { id = finding.Id }));
            }

            ViewBag.VendorId    = new SelectList(db.Vendors.Where(v => v.CompanyId == finding.CompanyId && (v.Type.Type & vendorTypeEnum.Finding) == vendorTypeEnum.Finding), "Id", "Name", finding.VendorId);
            ViewBag.CompanyName = db._Companies.Find(finding.CompanyId)?.Name;
            return(View(finding));
        }
예제 #11
0
        public ActionResult Create([Bind(Include = "Id,CompanyId,VendorId,Name,Desc,CtWt,StoneSize,ShapeId,Price,SettingCost,Qty,Note,ParentHandle,Title,Label,Tags")] Stone stone)
        {
            stone.Name = stone.Name.Trim();
            Stone existingStone = db.Stones.Where(s => s.CompanyId == stone.CompanyId && s.Name == stone.Name && s.StoneSize == stone.StoneSize && s.ShapeId == stone.ShapeId).FirstOrDefault();

            if (existingStone != null)
            {
                Shape shape = db.Shapes.Find(stone.ShapeId);
                ModelState.AddModelError("Name", $"A stone with {stone.Name}/{stone.StoneSize}/{shape.Name} already exists. ");
            }
            if (ModelState.IsValid)
            {
                db.Stones.Add(stone);
                db.SaveChanges();
                return(RedirectToAction("Index", new { companyId = stone.CompanyId }));
            }

            ViewBag.CompanyId   = stone.CompanyId;
            ViewBag.ShapeId     = new SelectList(db.Shapes.Where(s => s.CompanyId == stone.CompanyId), "Id", "Name", stone.ShapeId);
            ViewBag.VendorId    = new SelectList(db.Vendors.Where(v => v.CompanyId == stone.CompanyId && ((v.Type.Type & vendorTypeEnum.Stone) == vendorTypeEnum.Stone)), "Id", "Name", stone.VendorId);
            ViewBag.CompanyName = db._Companies.Find(stone.CompanyId)?.Name;
            return(View(stone));
        }
예제 #12
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            OJewelryDB           db  = new OJewelryDB();
            ApplicationDbContext sec = new ApplicationDbContext();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    UpdateCompaniesUsers(user, db, model.Companies);
                    UpdateRoles(sec, user, model.RoleId);
                    db.SaveChanges();
                    return(RedirectToAction("UserList", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            ViewBag.Roles = sec.Roles.ToList();
            List <Company> allCompanies = db.Companies.ToList();

            foreach (CompanyAuthorizedUser cau in model.Companies) // should be company left outer joined to users by id, exclude Managers, admins
            {
                cau.CompanyName = allCompanies.First(c => c.Id == cau.CompanyId).Name;
            }
            return(View(model));
        }
예제 #13
0
        public ActionResult Memo(MemoViewModel m)
        {
            ModelState.Clear();
            OJewelryDB dc = new OJewelryDB();
            // populate style data
            Style sdb = dc.Styles.Find(m.style.Id);

            m.style.Name = sdb.StyleName;
            //m.style.Num = sdb.StyleNum;
            m.style.Qty = sdb.Quantity;

            if (m.SendReturnMemoRadio == 1)
            {
                if (m.NewExistingPresenterRadio == 2)
                {
                    //Memo a new presenter
                    if (String.IsNullOrEmpty(m.PresenterName))
                    {
                        ModelState.AddModelError("Presenter Name", "Name is required for new Presenters.");
                    }
                    if (String.IsNullOrEmpty(m.PresenterEmail) && String.IsNullOrEmpty(m.PresenterPhone))
                    {
                        ModelState.AddModelError("Presenter Contact Info", "Phone or Email is required for new Presenters.");
                    }
                    Presenter p = new Presenter()
                    {
                        CompanyId = m.CompanyId,
                        Name      = m.PresenterName,
                        Email     = m.PresenterEmail,
                        Phone     = m.PresenterPhone,
                    };
                    dc.Presenters.Add(p);
                    m.PresenterId = p.Id;
                }
                else
                {
                    // Memo an existing presenter - nothing to validate in this case as they just selected a Presenter from the list
                }
                // create new memo, reduce inventory
                sdb.Quantity -= m.SendQty;
                String note = "Sending " + m.SendQty.ToString() + " items to " + m.PresenterName + " on " + DateTime.Now.ToString();
                Memo   mo   = new Memo()
                {
                    Quantity    = m.SendQty,
                    Date        = DateTime.Now,
                    Notes       = note,
                    PresenterID = m.PresenterId,
                    StyleID     = m.style.Id,
                };
                dc.Memos.Add(mo);
                if (m.SendQty > m.style.Qty)
                {
                    ModelState.AddModelError("Send Quantity", "You cannot memo more items than you have in inventory.");
                }
                if (m.SendQty < 1)
                {
                    ModelState.AddModelError("Send Quantity", "You can only memo a positive number of items.");
                }
            }
            else
            {
                //Return Items from Presenter
                // iterate thru the memos to take items back. Increase the inventory as appropriate. If all items are returned, delete the memo
                foreach (MemoModel memo in m.Memos)
                {
                    if (memo.ReturnQty < 0)
                    {
                        ModelState.AddModelError("Return Style", "You can only return a positive number to inventory.");
                    }
                    if (memo.ReturnQty > 0)
                    {
                        if (memo.ReturnQty > memo.Quantity)
                        {
                            ModelState.AddModelError("Return Style", "You can't return more items than were memo'd out.");
                        }
                        // update db
                        Memo mdb = dc.Memos.Find(memo.Id);
                        if (mdb.Quantity == memo.ReturnQty)
                        {
                            // remove the row and remove item from collection
                            dc.Memos.Remove(mdb);
                        }
                        else
                        {
                            // decrease the amount
                            mdb.Quantity -= memo.ReturnQty;
                        }
                        sdb.Quantity += memo.ReturnQty;
                    }
                    // ReturnQty is 0, no action
                }
            }
            if (ModelState.IsValid)
            {
                // Save changes, go to clientlist
                dc.SaveChanges();
                //return ClientList();
                //return View(m);
            }
            return(Memo(m.style.Id));
        }
예제 #14
0
        public async Task <ActionResult> Edit(StyleViewModel svm)
        {
            CheckForNameAndNumberUniqueness(svm);
            int i;

            // Check special cases in Model
            CheckModelState(svm);
            //
            TraceModelStateErrors(svm.Style.StyleName);
            // Save the Style and all edited components; add the new ones and remove the deleted ones
            if (ModelState.IsValid)
            {
                bool bUseLaborTable = svm.Style.JewelryType.bUseLaborTable;
                svm.Style.JewelryType = null; // hack to avoid maismatch of JewelryTypeId error!!!

                if (db.Entry(svm.Style).State != EntityState.Added)
                {
                    db.Entry(svm.Style).State = EntityState.Modified;
                }
                // Iterate thru the components
                // Castings
                if (svm.Castings != null)
                {
                    i = -1;
                    foreach (CastingComponent c in svm.Castings)
                    {
                        i++;
                        Casting      casting;
                        StyleCasting sc;
                        try
                        {
                            ValidCasting(c);
                        }
                        catch (OjMissingCastingException e)
                        {
                            ModelState.AddModelError("Castings[" + i + "].Name", e.Message);
                            continue;
                        }

                        switch (c.State)
                        {
                        case SVMStateEnum.Added:
                            casting = new Casting(c);
                            // add a new link
                            casting.Id = -i;
                            db.Castings.Add(casting);
                            sc = new StyleCasting()
                            {
                                CastingId = casting.Id,
                                StyleId   = svm.Style.Id,
                            };
                            db.StyleCastings.Add(sc);
                            break;

                        case SVMStateEnum.Deleted:
                            sc = db.StyleCastings.Where(x => x.StyleId == svm.Style.Id && x.CastingId == c.Id)
                                 .SingleOrDefault();
                            casting = db.Castings.Find(c.Id);
                            db.Castings.Remove(casting);
                            db.StyleCastings.Remove(sc);
                            break;

                        case SVMStateEnum.Dirty:
                        case SVMStateEnum.Fixed:
                            casting = db.Castings.Find(c.Id);
                            casting.Set(c);

                            /*
                             * // Update the Syle-Casting Link
                             * sc = db.StyleCastings.Where(x => x.StyleId == svm.Style.Id && x.CastingId == c.Id).SingleOrDefault();
                             */
                            break;

                        case SVMStateEnum.Unadded:
                            break;

                        default:
                            break;
                        }
                    }
                }
                // Stones
                if (svm.Stones != null)
                {
                    i = -1;
                    foreach (StoneComponent sc in svm.Stones)
                    {
                        i++;
                        //Stone stone;
                        StyleStone ss;
                        int        stoneId = 0;
                        try
                        {
                            stoneId = ValidStone(svm.CompanyId, sc);
                            sc.Id   = stoneId;
                        }
                        catch (OjInvalidStoneComboException e)
                        {
                            ModelState.AddModelError("Stones[" + i + "].Name", e.Message);
                            continue;
                        }
                        catch (OjMissingStoneException)
                        {
                            if (sc.Name == null)
                            {
                                ModelState.AddModelError("Stones[" + i + "].Name", "You must enter a stone!! ");
                            }
                            if (sc.ShId == null)
                            {
                                ModelState.AddModelError("Stones[" + i + "].ShId", "You must enter a shape!! ");
                            }
                            if (sc.SzId == null)
                            {
                                ModelState.AddModelError("Stones[" + i + "].SzId", "You must enter a size!! ");
                            }
                            continue;
                        }

                        switch (sc.State)
                        {
                        case SVMStateEnum.Added:
                            //stone = new Stone(sc);
                            //db.Stones.Add(stone);
                            ss = new StyleStone()
                            {
                                StyleId = svm.Style.Id,
                                StoneId = stoneId,
                                Qty     = (int)sc.Qty
                            };
                            db.StyleStones.Add(ss);
                            break;

                        case SVMStateEnum.Deleted:
                            ss = db.StyleStones.Where(x => x.Id == sc.linkId).SingleOrDefault();
                            //db.Stones.Remove(ss.Stone);
                            db.StyleStones.Remove(ss);
                            break;

                        case SVMStateEnum.Dirty:
                        case SVMStateEnum.Fixed:
                            //stone = db.Stones.Find(sc.Id);
                            //stone.Set(sc);
                            ss = db.StyleStones.Where(x => x.Id == sc.linkId).SingleOrDefault();
                            //ss = db.StyleStones.Where(x => x.StyleId == svm.Style.Id && x.Id == sc.Id).SingleOrDefault();
                            ss.Qty     = (int)sc.Qty;
                            ss.StoneId = stoneId;
                            break;

                        case SVMStateEnum.Unadded:
                        default:
                            break;
                        }
                    }
                }
                // Findings
                if (svm.Findings != null)
                {
                    i = -1;
                    foreach (FindingsComponent c in svm.Findings)
                    {
                        i++;
                        StyleFinding fc;
                        try
                        {
                            ValidFinding(c, i);
                        }
                        catch (OjMissingFindingException e)
                        {
                            ModelState.AddModelError("Findings[" + i + "].Id", e.Message);
                            continue;
                        }
                        switch (c.State)
                        {
                        case SVMStateEnum.Added:
                            /*
                             * component = new Component(c);
                             * db.Components.Add(component);
                             */
                            fc = new StyleFinding()
                            {
                                StyleId   = svm.Style.Id,
                                FindingId = c.Id ?? 0,
                                Qty       = c.Qty
                            };

                            db.StyleFindings.Add(fc);
                            break;

                        case SVMStateEnum.Deleted:
                            fc = db.StyleFindings.Where(x => x.Id == c.linkId).SingleOrDefault();
                            //db.Findings.Remove(fc.Finding);
                            db.StyleFindings.Remove(fc);
                            break;

                        case SVMStateEnum.Dirty:
                        case SVMStateEnum.Fixed:
                            /*
                             * component = db.Components.Find(c.Id);
                             * component.Set(c);
                             */
                            //finding.Set(c); // Dont change the finding, just the link!!!
                            fc           = db.StyleFindings.Where(x => x.Id == c.linkId).SingleOrDefault();
                            fc.FindingId = c.Id ?? 0;
                            fc.Qty       = c.Qty;
                            break;

                        case SVMStateEnum.Unadded:     // No updates
                        default:
                            break;
                        }
                    }
                }
                // Labors
                if (svm.Labors != null && bUseLaborTable == false)
                {
                    i = -1;
                    foreach (LaborComponent lc in svm.Labors)
                    {
                        i++;
                        Labor      labor;
                        StyleLabor sl;
                        try
                        {
                            ValidLabor(lc);
                        }
                        catch (OjMissingLaborException e)
                        {
                            ModelState.AddModelError("Labors[" + i + "].Name", e.Message);
                            continue;
                        }

                        switch (lc.State)
                        {
                        case LMState.Added:
                            AddLabor(lc, svm, i);
                            break;

                        case LMState.Deleted:
                            sl = db.StyleLabors.Where(x => x.StyleId == svm.Style.Id && x.LaborId == lc.Id).Single();
                            RemoveLabor(sl);
                            break;

                        case LMState.Fixed:
                        case LMState.Dirty:
                            if (lc.Id <= 0)
                            {
                                AddLabor(lc, svm, i);
                            }
                            else
                            {
                                labor = db.Labors.Find(lc.Id);
                                labor.Set(lc);
                            }

                            /*
                             * sl = db.StyleLabors.Where(x => x.StyleId == svm.Style.Id && x.LaborId == c.Id).Single();
                             */
                            break;

                        case LMState.Unadded:     // No updates
                        default:
                            break;
                        }
                    }
                }
                // Labor Table
                if (svm.LaborItems != null && bUseLaborTable == true)
                {
                    i = -1;
                    foreach (LaborItemComponent lic in svm.LaborItems)
                    {
                        i++;
                        StyleLaborTableItem sl;
                        try
                        {
                            ValidLaborItem(lic);
                        }
                        catch (OjMissingLaborException e)
                        {
                            ModelState.AddModelError("LaborsItems[" + i + "].Name", e.Message);
                            continue;
                        }

                        switch (lic.State)
                        {
                        case LMState.Added:
                            AddLaborItem(lic, svm, i);
                            break;

                        case LMState.Deleted:
                            sl = db.StyleLaborItems.Where(x => x.Id == lic.linkId).SingleOrDefault();
                            RemoveLaborItem(sl);
                            break;

                        case LMState.Dirty:
                        case LMState.Fixed:
                            if (lic.Id <= 0)
                            {
                                AddLaborItem(lic, svm, i);
                            }
                            else
                            {
                                //laborItem = db.LaborTable.Find(lic.laborItemId);
                                sl              = db.StyleLaborItems.Where(x => x.Id == lic.linkId).SingleOrDefault();
                                sl.Qty          = lic.Qty.GetValueOrDefault();
                                sl.LaborTableId = lic.laborItemId.GetValueOrDefault();
                                //laborItem.Set(lic);
                            }

                            /*
                             * sl = db.StyleLabors.Where(x => x.StyleId == svm.Style.Id && x.LaborId == c.Id).Single();
                             */
                            break;

                        case LMState.Unadded:     // No updates
                        default:
                            break;
                        }
                        db.Entry(lic._laborItem).State = EntityState.Detached;
                    }
                }

                // Misc
                if (svm.Miscs != null)
                {
                    Misc      misc;
                    StyleMisc sm;
                    i = -1;
                    foreach (MiscComponent c in svm.Miscs)
                    {
                        i++;
                        try
                        {
                            ValidMisc(c);
                        }
                        catch (OjMissingMiscException e)
                        {
                            ModelState.AddModelError("Miscs[" + i + "].Name", e.Message);
                            continue;
                        }

                        switch (c.State)
                        {
                        case SVMStateEnum.Added:
                            AddMisc(c, svm, i);
                            break;

                        case SVMStateEnum.Deleted:
                            sm = db.StyleMiscs.Where(x => x.StyleId == svm.Style.Id && x.MiscId == c.Id).Single();
                            RemoveMisc(sm);
                            break;

                        case SVMStateEnum.Dirty:
                        case SVMStateEnum.Fixed:
                            if (c.Id <= 0)
                            {
                                AddMisc(c, svm, i);
                            }
                            else
                            {
                                misc = db.Miscs.Find(c.Id);
                                misc.Set(c);
                            }

                            /*
                             * sm = db.StyleMiscs.Where(x => x.StyleId == svm.Style.Id && x.MiscId == c.Id).Single();
                             */
                            break;

                        case SVMStateEnum.Unadded:     // No updates
                        default:
                            break;
                        }
                    }

                    if (svm.SVMState == SVMStateEnum.Added)
                    {
                        db.Styles.Add(svm.Style);
                    }
                } // false
            }
            if (ModelState.IsValid)
            {
                if (true) // if the modelstate only has validation errors on "Clean" components, then allow the DB update
                {
                    // Save changes, go to Home
                    try
                    {
                        db.SaveChanges(); // need the styleId for the image name
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError($"Error saving style {svm.Style.StyleName}, msg: {e.Message}");
                    }
                    Trace.TraceInformation("Operation: {0}, svmId:{1}", svm.SVMOp.ToString(), svm.Style.Id);
                    await SaveImageInStorage(db, svm);

                    db.Entry(svm.Style);
                    db.SaveChanges();
                    if (svm.SVMOp == SVMOperation.Create)
                    {
                        // Redurect to Edit
                        return(RedirectToAction("Edit", new { id = svm.Style.Id }));
                    }
                    if (svm.SVMOp != SVMOperation.Print)
                    {
                        // Redurect to Edit
                        return(RedirectToAction("Index", new { CollectionId = svm.Style.CollectionId }));
                    }
                    else
                    {
                        return(Print(svm.Style.Id));
                    }
                }
            }
            Collection co = db.Collections.Find(svm.Style.CollectionId);

            svm.CompanyId         = co.CompanyId;
            svm.Style.JewelryType = db.JewelryTypes.Find(svm.Style.JewelryTypeId);

            //svm.Style.
            string markup = db.FindCompany(svm.CompanyId).markup;

            if (markup == null)
            {
                markup = "[]";
            }
            svm.markups = JsonConvert.DeserializeObject <List <Markup> >(markup);
            svm.PopulateDropDownData(db);
            svm.PopulateDropDowns(db);
            if (svm.SVMOp == SVMOperation.Create)
            {
                svm.LookupComponents(db);
            }
            else
            {
                svm.RepopulateComponents(db); // iterate thru the data and repopulate the links
            }
            ViewBag.CollectionId = new SelectList(db.Collections.Where(x => x.CompanyId == co.CompanyId), "Id", "Name", svm.Style.CollectionId);
            //ViewBag.JewelryTypeId = new SelectList(db.JewelryTypes.Where(x => x.CompanyId == co.CompanyId), "Id", "Name", svm.Style.JewelryTypeId);
            ViewBag.MetalWtUnitId = new SelectList(db.MetalWeightUnits.OrderBy(mwu => mwu.Unit), "Id", "Unit", svm.Style.MetalWtUnitId);
            // iterate thru modelstate errors, display on page
            return(View(svm));
        }
        public ActionResult Index(LaborTableModel ltm)
        {
            List <Vendor> vendors;

            if (ModelState.IsValid)
            {
                int i = -1;
                //db.Entry(cvm.company).State = EntityState.Modified;

                foreach (LaborItem li in ltm.Labors)
                {
                    i++;
                    switch (li.State)
                    {
                    case LMState.Added:
                        li.CompanyId = ltm.CompanyId;
                        db.LaborTable.Add(li);
                        li.State = LMState.Dirty;
                        break;

                    case LMState.Deleted:
                        // Make sure its not used. If it is, reset to "Dirty" and add an error message
                        if (db.StyleLaborItems.Where(sli => sli.LaborTableId == li.Id).Count() > 0)
                        {
                            li.State = LMState.Dirty;
                            ModelState.SetModelValue($"Labors[{i}].State", new ValueProviderResult("Dirty", "", System.Globalization.CultureInfo.InvariantCulture));
                            ModelState.AddModelError($"Labors[{i}].Name", $"You cannot delete {li.Name}: it is used by the following style(s):{StylesInUse(li)}");
                            break;
                        }
                        db.Entry(li).State = EntityState.Deleted;
                        db.LaborTable.Remove(li);
                        break;

                    case LMState.Dirty:
                        db.Entry(li).State = EntityState.Modified;
                        break;

                    case LMState.Unadded:
                    case LMState.Clean:
                    case LMState.Fixed:
                        break;
                    }
                }
            }
            vendors = db.Vendors.Where(v => v.CompanyId == ltm.CompanyId).ToList();
            if (ModelState.IsValid)
            {
                db.SaveChanges();

                int    CompanyId   = ltm.CompanyId;
                string CompanyName = ltm.CompanyName;

                ModelState.Clear();
                ltm = new LaborTableModel()
                {
                    Labors      = db.LaborTable.Where(l => l.CompanyId == CompanyId).ToList(),
                    CompanyId   = CompanyId,
                    CompanyName = CompanyName,
                };
                foreach (LaborItem li in ltm.Labors)
                {
                    li.selectList = new SelectList(vendors, "Id", "Name", li.VendorId);
                }
                ltm.bHasVendors = vendors.Count != 0;
                return(RedirectToAction("Index", "Companies"));
            }
            foreach (LaborItem li in ltm.Labors)
            {
                li.selectList = new SelectList(vendors, "Id", "Name", li.VendorId);
            }
            ltm.bHasVendors = vendors.Count != 0;
            return(View(ltm));
        }