Пример #1
0
        public ActionResult CreateNew(int companyId, int collectionId, StyleViewModel svm)
        {
            svm.Style.Collection   = db.Collections.Find(collectionId);
            svm.Style.CollectionId = collectionId;
            svm.CompanyId          = svm.Style.Collection.CompanyId;

            svm.PopulateDropDownData(db);
            svm.PopulateDropDowns(db);
            svm.RepopulateComponents(db); // iterate thru the data and repopulate the links

            ViewBag.CollectionId = new SelectList(db.Collections.Where(x => x.CompanyId == svm.CompanyId), "Id", "Name", svm.Style.CollectionId);
            //ViewBag.JewelryTypeId = new SelectList(db.JewelryTypes.Where(x => x.CompanyId == companyId), "Id", "Name", svm.Style.JewelryTypeId);
            ViewBag.MetalWtUnitId = new SelectList(db.MetalWeightUnits.OrderBy(mwu => mwu.Unit), "Id", "Unit", svm.Style.MetalWtUnitId);
            return(View("Create", svm));
        }
Пример #2
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));
        }