Exemplo n.º 1
0
        public string Clone(int partID = 0, int newPartID = 0, string upc = "", bool categories = false, bool relatedParts = false, bool attributes = false, bool content = false, bool vehicles = false, bool prices = false)
        {
            CurtDevDataContext db = new CurtDevDataContext();
            List<string> messages = new List<string>();
            // Validate the partID and shortDesc fields
            if (partID == 0) { messages.Add("You must enter a part number."); }
            if (newPartID == 0) { messages.Add("You must enter a part number."); }
            if (upc.Trim().Length == 0) { messages.Add("You must enter a UPC."); }

            int existing_part = 0;
            // Make sure we don't have a product with this partID
            existing_part = (from p in db.Parts
                             where p.partID.Equals(newPartID)
                             select p).Count();
            if (existing_part != 0) { messages.Add("This part number exists."); }

            #region clone part
            if (messages.Count == 0) { // No errors, add the part
                try {
                    ConvertedPart cp = ProductModels.GetPart(partID);
                    Part new_part = new Part {
                        partID = newPartID,
                        classID = cp.pClass,
                        dateAdded = DateTime.Now,
                        dateModified = DateTime.Now,
                        featured = cp.featured,
                        oldPartNumber = cp.oldPartNumber,
                        priceCode = cp.priceCode,
                        shortDesc = cp.shortDesc,
                        status = cp.status,
                        ACESPartTypeID = cp.ACESPartTypeID
                    };
                    db.Parts.InsertOnSubmit(new_part);
                    db.SubmitChanges();
                    messages.Add("Part Added Successfully");

                    try {
                        ProductModels.SaveAttribute(0,new_part.partID, "UPC", upc);
                        messages.Add("UPC Added Successfully");
                    } catch (Exception e) {
                        messages.Add(e.Message);
                    }

                    #region clone categories
                    if (categories) {
                        try {
                            List<CatParts> new_catparts = new List<CatParts>();
                            List<int> catparts = db.CatParts.Where(x => x.partID == partID).Select(x => x.catID).Distinct().ToList();
                            foreach (int catpart in catparts) {
                                CatParts ncp = new CatParts {
                                    catID = catpart,
                                    partID = new_part.partID
                                };
                                new_catparts.Add(ncp);
                            }
                            db.CatParts.InsertAllOnSubmit(new_catparts);
                            db.SubmitChanges();
                            messages.Add("Categories Cloned Successfully");
                        } catch {
                            messages.Add("There was a problem cloning the categories.");
                        }
                    }
                    #endregion

                    #region clone Related Parts
                    if (relatedParts) {
                        try {
                            List<RelatedPart> new_relparts = new List<RelatedPart>();
                            List<RelatedPart> relparts = db.RelatedParts.Where(x => x.partID == partID).ToList<RelatedPart>();
                            foreach (RelatedPart relpart in relparts) {
                                RelatedPart nrp = new RelatedPart {
                                    relatedID = relpart.relatedID,
                                    rTypeID = relpart.rTypeID,
                                    partID = new_part.partID
                                };
                                new_relparts.Add(nrp);
                            }
                            db.RelatedParts.InsertAllOnSubmit(new_relparts);
                            db.SubmitChanges();
                            messages.Add("Related Parts Cloned Successfully");
                        } catch {
                            messages.Add("There was a problem cloning the related parts.");
                        }
                    }
                    #endregion

                    #region clone Attributes
                    if (attributes) {
                        try {
                            List<PartAttribute> new_attrs = new List<PartAttribute>();
                            List<PartAttribute> attributelist = db.PartAttributes.Where(x => x.partID == partID).Where(x => x.field.ToLower() != "upc").ToList<PartAttribute>();
                            foreach (PartAttribute attribute in attributelist) {
                                PartAttribute attr = new PartAttribute {
                                    value = attribute.value,
                                    field = attribute.field,
                                    partID = new_part.partID,
                                    sort = attribute.sort
                                };
                                new_attrs.Add(attr);
                            }
                            db.PartAttributes.InsertAllOnSubmit(new_attrs);
                            db.SubmitChanges();
                            messages.Add("Attributes Cloned Successfully");
                        } catch {
                            messages.Add("There was a problem cloning the attributes.");
                        }
                    }
                    #endregion

                    #region clone Content
                    if (content) {
                        try {
                            List<ContentBridge> new_content = new List<ContentBridge>();
                            List<ContentBridge> contents = (from cb in db.ContentBridges
                                                            where cb.partID == partID
                                                            select cb).ToList<ContentBridge>();
                            foreach (ContentBridge cont in contents) {
                                Content c = db.Contents.Where(x => x.contentID.Equals(cont.contentID)).FirstOrDefault();
                                Content new_c = new Content {
                                    cTypeID = c.cTypeID,
                                    text = c.text
                                };
                                db.Contents.InsertOnSubmit(new_c);
                                db.SubmitChanges();
                                ContentBridge cb = new ContentBridge {
                                        partID = new_part.partID,
                                        contentID = new_c.contentID
                                };
                                db.ContentBridges.InsertOnSubmit(cb);
                                db.SubmitChanges();
                            }
                            messages.Add("Contents Cloned Successfully");
                        } catch {
                            messages.Add("There was a problem cloning the contents.");
                        }
                    }
                    #endregion

                    #region clone Vehicles
                    if (vehicles) {
                        try {
                            List<VehiclePart> vehiclelist = db.VehicleParts.Where(x => x.partID == partID).ToList<VehiclePart>();
                            foreach (VehiclePart vp in vehiclelist) {
                                VehiclePart vehiclepart = new VehiclePart {
                                    partID = new_part.partID,
                                    vehicleID = vp.vehicleID,
                                    drilling = vp.drilling,
                                    installTime = vp.installTime,
                                    exposed = vp.exposed
                                };
                                db.VehicleParts.InsertOnSubmit(vehiclepart);
                                db.SubmitChanges();

                                List<VehiclePartAttribute> new_vpattr = new List<VehiclePartAttribute>();
                                List<VehiclePartAttribute> vpattrs = db.VehiclePartAttributes.Where(x => x.vPartID == vp.vPartID).ToList<VehiclePartAttribute>();
                                foreach (VehiclePartAttribute vpa in vpattrs) {
                                    VehiclePartAttribute new_vpa = new VehiclePartAttribute {
                                        vPartID = vehiclepart.vPartID,
                                        value = vpa.value,
                                        field = vpa.field,
                                        sort = vpa.sort
                                    };
                                    new_vpattr.Add(new_vpa);
                                };
                                db.VehiclePartAttributes.InsertAllOnSubmit(new_vpattr);
                                db.SubmitChanges();
                                messages.Add("Vehicles Cloned Successfully");
                            }
                        } catch {
                            messages.Add("There was a problem cloning the vehicles.");
                        }
                    }
                    #endregion

                    #region clone Prices
                    if (prices) {
                        try {
                            List<Price> new_prices = new List<Price>();
                            List<Price> pricelist = db.Prices.Where(x => x.partID == partID).ToList<Price>();
                            foreach (Price prc in pricelist) {
                                Price price = new Price {
                                    priceType = prc.priceType,
                                    price1 = prc.price1,
                                    partID = new_part.partID,
                                    enforced = prc.enforced
                                };
                                new_prices.Add(price);
                            }
                            db.Prices.InsertAllOnSubmit(new_prices);
                            db.SubmitChanges();
                            messages.Add("Prices Cloned Successfully");
                        } catch {
                            messages.Add("There was a problem cloning the prices.");
                        }
                    }
                    #endregion

                    ImportImages(new_part.partID);

                    db.indexPart(new_part.partID);
                    messages.Add("Part Cloned Successfully.");

                } catch (Exception e) {
                    messages.Add(e.Message);
                }
            } else {
                messages.Add("Part Clone Failed.");
            }
            #endregion

            return Newtonsoft.Json.JsonConvert.SerializeObject(messages);
        }
Exemplo n.º 2
0
        public static string DeleteVehiclePartAttribute(int attrID = 0)
        {
            try {
                // Validate
                if (attrID <= 0) { throw new Exception("Attribute ID is invalid."); }

                // Variable Declaration
                CurtDevDataContext db = new CurtDevDataContext();
                VehiclePartAttribute vpa = new VehiclePartAttribute();

                vpa = (from p in db.VehiclePartAttributes
                      where p.vpAttrID.Equals(attrID)
                      select p).FirstOrDefault<VehiclePartAttribute>();
                int pid = vpa.vPartID;
                db.VehiclePartAttributes.DeleteOnSubmit(vpa);
                db.SubmitChanges();
                updateVehicleAttributeSort(getVehiclePartAttributeIDs(pid));
                db.indexPart(vpa.VehiclePart.partID);
                UpdatePart(vpa.VehiclePart.partID);
                return "";
            } catch (Exception e) {
                return e.Message;
            }
        }
Exemplo n.º 3
0
        public static VehiclePartAttribute AddVehiclePartAttribute(int vPartID = 0, string field = "", string value = "")
        {
            // Validate fields
            if (vPartID <= 0) { throw new Exception("Vehicle Part ID is invalid."); }
            if (field.Length <= 0) { throw new Exception("Field is invalid."); }
            if (value.Length <= 0) { throw new Exception("Value is invalid."); }

            // Declare objects
            CurtDevDataContext db = new CurtDevDataContext();
            VehiclePartAttribute vpa = new VehiclePartAttribute {
                vPartID = vPartID,
                field = field,
                value = value,
                sort = (db.VehiclePartAttributes.Where(x => x.vPartID.Equals(vPartID)).OrderByDescending(x => x.sort).Select(x => x.sort).FirstOrDefault() + 1)
            };

            VehiclePart v = (from vp in db.VehicleParts
                             where vp.vPartID == vPartID
                             select vp).FirstOrDefault<VehiclePart>();

            if (field.ToLower().Contains("install")) {
                Match match = Regex.Match(value, @"\d+");
                if (match.Success) {
                    string result = match.Groups[0].Value;
                    int duration = Convert.ToInt32(result);
                    v.installTime = duration;
                }
            }

            if (field.ToLower().Contains("visibility")) {
                v.exposed = value;
            };

            if (field.ToLower().Contains("drill")) {
                if (value.ToLower() == "no") {
                    v.drilling = "No drilling required";
                } else {
                    v.drilling = "Drilling required";
                }
            };

            // Commit to database
            db.VehiclePartAttributes.InsertOnSubmit(vpa);
            db.SubmitChanges();
            updateVehicleAttributeSort(getVehiclePartAttributeIDs(vPartID));
            UpdatePart(vpa.VehiclePart.partID);
            //db.indexPart(partID);
            // Return PartAttribute
            return vpa;
        }
Exemplo n.º 4
0
        public static FullVehicle CarryOverPart(int vehicleID = 0, int partID = 0)
        {
            try {
                CurtDevDataContext db = new CurtDevDataContext();
                CarryOverInfo latest = GetLatestVehiclePart(vehicleID, partID);
                VehiclePart latestvp = db.VehicleParts.Where(x => x.vehicleID == vehicleID).Where(x => x.partID == partID).First<VehiclePart>();
                List<VehiclePartAttribute> vpas = db.VehiclePartAttributes.Where(x => x.vPartID == latestvp.vPartID).ToList<VehiclePartAttribute>();
                List<VehiclePartAttribute> newvpas = new List<VehiclePartAttribute>();
                double year = latest.year + 1;
                int yearID = 0;

                try {
                    yearID = db.Years.Where(x => x.year1 == year).Select(x => x.yearID).First();
                } catch {
                    Year y = new Year {
                        year1 = year
                    };
                    db.Years.InsertOnSubmit(y);
                    db.SubmitChanges();
                    yearID = y.yearID;
                };

                try {
                    YearMake ym = db.YearMakes.Where(x => x.yearID.Equals(yearID)).Where(x => x.makeID.Equals(latest.makeID)).First<YearMake>();
                } catch {
                    YearMake ym = new YearMake {
                        yearID = yearID,
                        makeID = latest.makeID
                    };
                    db.YearMakes.InsertOnSubmit(ym);
                    db.SubmitChanges();
                }

                int vID = Vehicle.GetVehicleID(yearID, latest.makeID, latest.modelID, latest.styleID);

                if (vID == 0) {
                    Vehicles v = new Vehicles {
                        yearID = yearID,
                        makeID = latest.makeID,
                        modelID = latest.modelID,
                        styleID = latest.styleID,
                        dateAdded = DateTime.Now
                    };
                    db.Vehicles.InsertOnSubmit(v);
                    db.SubmitChanges();
                    vID = v.vehicleID;
                }
                List<VehiclePart> parts = new List<VehiclePart>();

                foreach (int partnum in latest.partids) {
                    if (db.VehicleParts.Where(x => x.partID.Equals(partnum)).Where(x => x.vehicleID.Equals(vID)).Count() == 0) {
                        VehiclePart vp = new VehiclePart {
                            vehicleID = vID,
                            partID = partnum,
                            drilling = latestvp.drilling,
                            exposed = latestvp.exposed,
                            installTime = latestvp.installTime
                        };
                        parts.Add(vp);
                    }
                }

                db.VehicleParts.InsertAllOnSubmit(parts);
                db.SubmitChanges();

                foreach (VehiclePart part in parts) {
                    newvpas = new List<VehiclePartAttribute>();
                    foreach (VehiclePartAttribute vpa in vpas) {
                        VehiclePartAttribute newvpa = new VehiclePartAttribute {
                            vPartID = part.vPartID,
                            field = vpa.field,
                            value = vpa.value
                        };
                        newvpas.Add(newvpa);
                    };
                    db.VehiclePartAttributes.InsertAllOnSubmit(newvpas);
                    db.SubmitChanges();
                };

                UpdatePart(partID);
                return Vehicle.GetFullVehicle(vID);

            } catch (Exception e) {
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 5
0
 internal static VehiclePartAttribute GetVehiclePartAttribute(int vpAttrID = 0)
 {
     CurtDevDataContext db = new CurtDevDataContext();
     VehiclePartAttribute attribute = new VehiclePartAttribute();
     attribute = (from a in db.VehiclePartAttributes
                  where a.vpAttrID.Equals(vpAttrID)
                   select a).FirstOrDefault<VehiclePartAttribute>();
     return attribute;
 }