Ejemplo n.º 1
0
        public List<SimplePricing> GetAll(string key)
        {
            Authenticate(key);
            List<int> statuses = new List<int> { 800, 900 };
            CurtDevDataContext db = new CurtDevDataContext();

            List<SimplePricing> allparts = new List<SimplePricing>();
            allparts = (from c in db.Parts
                        join cpr in db.CustomerPricings on c.partID equals cpr.partID into PricingTemp
                        from cp in PricingTemp.Where(x => x.cust_id == this.cust_id).DefaultIfEmpty()
                        join cir in db.CartIntegrations on c.partID equals cir.partID into IntegrationTemp
                        from ci in IntegrationTemp.Where(x => x.custID == this.cust_id).DefaultIfEmpty()
                        where statuses.Contains(c.status)
                        select new SimplePricing {
                            cust_id = this.cust_id,
                            partID = c.partID,
                            custPartID = (ci.custPartID == null) ? 0 : ci.custPartID,
                            price = (cp.price == null) ? 0 : cp.price,
                            isSale = cp.isSale == null ? 0 : cp.isSale,
                            sale_start = cp.sale_start == null ? "" : Convert.ToDateTime(cp.sale_start).ToString(),
                            sale_end = cp.sale_end == null ? "" : Convert.ToDateTime(cp.sale_end).ToString(),
                            msrp = ((c.Prices.Any(x => x.priceType.Equals("List"))) ? c.Prices.Where(x => x.priceType.Equals("List")).Select(x => x.price1).FirstOrDefault() : 0),
                            map = ((c.Prices.Any(x => x.priceType.Equals("eMap"))) ? c.Prices.Where(x => x.priceType.Equals("eMap")).Select(x => x.price1).FirstOrDefault() : 0),
                        }).AsParallel().ToList();

            return allparts.OrderBy(x => x.partID).ToList<SimplePricing>();
        }
Ejemplo n.º 2
0
        public string GetUnintegratedPartsJSON()
        {
            CurtDevDataContext db = new CurtDevDataContext();
            List<int> statuses = new List<int> { 800, 900 };
            List<APIPart> parts = new List<APIPart>();

            parts = (from p in db.Parts
                     join c in db.Classes on p.classID equals c.classID into ClassTemp
                     from c in ClassTemp.DefaultIfEmpty()
                     join ci in db.CartIntegrations on p.partID equals ci.partID into IntegrationTemp
                     from cit in IntegrationTemp.Where(x => x.custID.Equals(this.custID)).DefaultIfEmpty()
                     where statuses.Contains(p.status) && (cit.referenceID == null || cit.custPartID == 0)
                     orderby p.partID
                     select new APIPart {
                         partID = p.partID,
                         status = p.status,
                         dateModified = Convert.ToDateTime(p.dateModified).ToString(),
                         dateAdded = Convert.ToDateTime(p.dateAdded).ToString(),
                         shortDesc = "CURT " + p.shortDesc + " #" + p.partID.ToString(),
                         oldPartNumber = p.oldPartNumber,
                         listPrice = String.Format("{0:C}", (from prices in p.Prices
                                                             where prices.priceType.Equals("List")
                                                             select prices.price1 != null ? prices.price1 : (decimal?)0).FirstOrDefault<decimal?>()),
                         pClass = (c != null) ? c.class1 : "",
                         priceCode = p.priceCode,
                         relatedCount = db.RelatedParts.Where(x => x.partID.Equals(p.partID)).Select(x => new { relatedID = x.relatedID }).Count(),
                         attributes = (from pa in p.PartAttributes
                                       orderby pa.sort
                                       select new APIAttribute { key = pa.field, value = pa.value }).ToList<APIAttribute>(),
                         content = (from co in p.ContentBridges
                                    orderby co.contentID
                                    select new APIAttribute { key = co.Content.ContentType.type, value = co.Content.text }).OrderBy(x => x.key).ToList<APIAttribute>(),
                         videos = (from pv in p.PartVideos
                                   orderby pv.vTypeID
                                   select new APIVideo { videoID = pv.pVideoID, youTubeVideoID = pv.video, isPrimary = pv.isPrimary, typeID = pv.vTypeID, type = pv.videoType.name, typeicon = pv.videoType.icon }).ToList<APIVideo>(),
                         pricing = (from pr in p.Prices
                                    select new APIAttribute { key = pr.priceType, value = pr.price1.ToString() }).OrderBy(x => x.key).ToList<APIAttribute>(),
                         reviews = (from r in p.Reviews
                                    where r.active.Equals(true) && r.approved.Equals(true) && r.cust_id.Equals(this.custID)
                                    orderby r.createdDate descending
                                    select new APIReview { reviewID = r.reviewID, partID = (r.partID != null) ? (int)r.partID : 0, rating = r.rating, subject = r.subject, review_text = r.review_text, email = r.email, name = r.name, createdDate = String.Format("{0:MM/dd/yyyy hh:mm tt}", r.createdDate) }).Take(10).ToList<APIReview>(),
                         averageReview = (from r in p.Reviews
                                          where r.active.Equals(true) && r.approved.Equals(true) && r.cust_id.Equals(this.custID)
                                          select (double?)r.rating).Average() ?? 0.0,
                         images = (from pi in p.PartImages
                                   select new APIImage {
                                       imageID = pi.imageID,
                                       sort = pi.sort,
                                       path = pi.path,
                                       height = pi.height,
                                       width = pi.width,
                                       size = db.PartImageSizes.Where(x => x.sizeID.Equals(pi.sizeID)).Select(x => x.size).FirstOrDefault<string>(),
                                       partID = pi.partID
                                   }).OrderBy(x => x.sort).ToList<APIImage>()
                     }).AsParallel().Distinct().ToList<APIPart>();
            return JsonConvert.SerializeObject(parts);
        }
Ejemplo n.º 3
0
        public ReturnableCustomer Get()
        {
            CurtDevDataContext db = new CurtDevDataContext();

            ReturnableCustomer cust = (from c in db.Customers
                             where c.email.Equals(this.email) && c.customerID.Equals(this.customerID)
                             select new ReturnableCustomer {
                                 customerID = (c.customerID != null)? Convert.ToInt32(c.customerID) : 0,
                                 email = c.email,
                                 name = c.name,
                                 key = c.APIKey.ToString(),
                                 website = c.website
                             }).FirstOrDefault<ReturnableCustomer>();

            return cust;
        }
Ejemplo n.º 4
0
        public List<CartIntegration> GetAll(string key)
        {
            Authenticate(key);
            CurtDevDataContext db = new CurtDevDataContext();
            List<int> statuses = new List<int> { 800, 900 };

            List<CartIntegration> all = (from c in db.Parts
                                         join ci in db.CartIntegrations on c.partID equals ci.partID into IntegrationTemp
                                         from cit in IntegrationTemp.Where(x => x.custID.Equals(this.custID)).DefaultIfEmpty()
                                         where statuses.Contains(c.status)
                                         select new CartIntegration {
                                             custID = this.custID,
                                             partID = c.partID,
                                             custPartID = (cit.custPartID == null) ? 0 : cit.custPartID,
                                             referenceID = (cit.referenceID == null) ? 0 : cit.referenceID
                                         }).AsParallel().OrderBy(x => x.partID).ToList<CartIntegration>();

            return all;
        }
Ejemplo n.º 5
0
        public void RemoveSale(string key)
        {
            Authenticate(key);

            if (this.partID == 0) { throw new Exception("Invalid reference to part."); }
            if (this.price == 0) { throw new Exception("Invalid price point."); }

            CurtDevDataContext db = new CurtDevDataContext();

            CustomerPricing point = db.CustomerPricings.Where(x => x.partID.Equals(this.partID) && x.cust_id.Equals(this.cust_id) && x.isSale.Equals(1) && x.price.Equals(this.price)).FirstOrDefault<CustomerPricing>();
            db.CustomerPricings.DeleteOnSubmit(point);

            db.SubmitChanges();
        }
Ejemplo n.º 6
0
 private bool checkMap()
 {
     CurtDevDataContext db = new CurtDevDataContext();
     Price map = db.Prices.Where(x => x.priceType.ToUpper().Equals("eMap") && x.partID.Equals(this.partID)).FirstOrDefault();
     if (this.price < (map.price1 / 2) && map.enforced) {
         return false;
     }
     return true;
 }
Ejemplo n.º 7
0
 internal decimal GetMap()
 {
     var db = new CurtDevDataContext();
     return db.Prices.Where(x => x.partID.Equals(this.partID) && x.priceType.ToUpper().Equals("eMap")).Select(x => x.price1).FirstOrDefault<decimal>();
 }
Ejemplo n.º 8
0
        /*protected int GetCustomerReference() {
            CurtDevDataContext db = new CurtDevDataContext();
            return db.Customers.Where(x => x.customerID.Equals(this.cust_id)).Select(x => x.cust_id).FirstOrDefault<int>();
        }*/
        internal void Authenticate(string key)
        {
            if (key == null || key.Length == 0) { throw new Exception("Invalid API key."); }
            if (this.cust_id == 0) { throw new Exception("Invalid customerID"); }

            Guid api_key = Guid.Parse(key);

            CurtDevDataContext db = new CurtDevDataContext();
            Customer cust = db.Customers.Where(x => x.customerID.Equals(this.cust_id) & x.APIKey.Equals(api_key)).FirstOrDefault<Customer>();
            if (cust == null || cust.cust_id == 0) {
                // We need to attempt to auth against a CustomerUsers Key
                var customerID = (from cu in db.CustomerUsers
                            join c in db.Customers on cu.cust_ID equals c.cust_id
                            join ak in db.ApiKeys on cu.id equals ak.user_id
                            join akt in db.ApiKeyTypes on ak.type_id equals akt.id
                            where !akt.type.ToUpper().Equals("AUTHENITCATION") && ak.api_key.Equals(api_key)
                            && c.customerID.Equals(this.cust_id)
                            select c.customerID).FirstOrDefault<int?>();
                if (customerID == null || customerID == 0) {
                    throw new Exception("Denied Access.");
                }
            }
        }
Ejemplo n.º 9
0
        public void SetAllToMap(string key)
        {
            Authenticate(key);

            var ctx = new CurtDevDataContext();

            // Delete existing records
            ctx.ExecuteCommand(
                "delete from CustomerPricing where cust_id = {0}", this.cust_id);

            // Repopulate from the Price table
            ctx.ExecuteCommand(
                "insert into CustomerPricing(cust_id, partID, price, isSale) select {0},p.partID, p.price,0 from Price p where p.priceType = 'Map'", this.cust_id);
        }
Ejemplo n.º 10
0
        public SimplePricing Set(string key)
        {
            Authenticate(key);
            CurtDevDataContext db = new CurtDevDataContext();

            // Validate required fields
            if (this.price == 0) { throw new Exception("Price failed to validate against null or zero."); }
            if (this.partID == 0) { throw new Exception("Part Number failed to validate against null or zero."); }
            if (this.isSale == 1) {
                if (this.sale_start == null || this.sale_start < DateTime.Today.AddDays(-1)) { throw new Exception("If record is going to marked as sale, sale start is required and cannot be in the past."); }
                if (this.sale_end == null || this.sale_end < DateTime.Now || this.sale_end <= this.sale_start) { throw new Exception("If record is going to marked as sale, sale end is required, cannot be in the past, and cannot be sooner or equal to the sale start."); }
            } else {
                this.sale_start = null;
                this.sale_end = null;
            }

            // Make sure the price point isn't set lower than map
            if (!checkMap()) {
                throw new Exception("You may not set the price point lower than map price.");
            }
            CustomerPricing newpricing = new CustomerPricing {
                cust_id = this.cust_id,
                partID = this.partID,
                price = this.price,
                isSale = this.isSale,
                sale_start = this.sale_start,
                sale_end = this.sale_end
            };

            // Attempt to get a CustomerPricing record for this customerID and partID
            List<CustomerPricing> tmpPoints = db.CustomerPricings.Where(x => x.cust_id.Equals(this.cust_id) && x.partID.Equals(this.partID)).ToList<CustomerPricing>();
            bool updated = false;
            List<CustomerPricing> deletables = new List<CustomerPricing>();
            foreach (CustomerPricing tmpPoint in tmpPoints) {
                bool deleted = false;
                if (tmpPoint.sale_end != null && tmpPoint.sale_end < DateTime.Now) {
                    // expired sale - delete
                    deletables.Add(tmpPoint);
                    deleted = true;
                }
                if (!deleted && this.isSale == tmpPoint.isSale) {
                    if (!updated) {
                        tmpPoint.price = this.price;
                        tmpPoint.isSale = this.isSale;
                        tmpPoint.sale_start = this.sale_start;
                        tmpPoint.sale_end = this.sale_end;
                        newpricing.cust_price_id = tmpPoint.cust_price_id;
                        updated = true;
                    } else {
                        deletables.Add(tmpPoint);
                    }
                }
            }
            if (!updated) {
                db.CustomerPricings.InsertOnSubmit(newpricing);
            }
            if (deletables.Count > 0) {
                db.CustomerPricings.DeleteAllOnSubmit(deletables);
            }
            db.SubmitChanges();

            CustomerPricing currentPrice = db.CustomerPricings.Where(x => x.cust_price_id.Equals(newpricing.cust_price_id)).FirstOrDefault<CustomerPricing>();

            SimplePricing pricePoint = new SimplePricing {
                cust_id = currentPrice.cust_id,
                partID = currentPrice.partID,
                price = currentPrice.price,
                isSale = currentPrice.isSale,
                sale_start = currentPrice.sale_start.ToString(),
                sale_end = currentPrice.sale_end.ToString()
            };
            return pricePoint;
        }
Ejemplo n.º 11
0
        public XDocument GetUnintegratedPartsXML()
        {
            XDocument xml = new XDocument();
            XElement parts = new XElement("Parts");

            List<int> statuses = new List<int> { 800, 900 };
            CurtDevDataContext db = new CurtDevDataContext();

            List<XElement> partlist = (from p in db.Parts
                                       join c in db.Classes on p.classID equals c.classID into ClassTemp
                                       from c in ClassTemp.DefaultIfEmpty()
                                       join ci in db.CartIntegrations on p.partID equals ci.partID into IntegrationTemp
                                       from cit in IntegrationTemp.Where(x => x.custID.Equals(this.custID)).DefaultIfEmpty()
                                       where statuses.Contains(p.status) && (cit.referenceID == null || cit.custPartID == 0)
                                       orderby p.partID
                                       select new XElement("Part",
                                                   new XAttribute("partID", p.partID),
                                                   new XAttribute("status", p.status),
                                                   new XAttribute("dateModified", Convert.ToDateTime(p.dateModified).ToString()),
                                                   new XAttribute("dateAdded", Convert.ToDateTime(p.dateAdded).ToString()),
                                                   new XAttribute("shortDesc", "CURT " + p.shortDesc + " #" + p.partID.ToString()),
                                                   new XAttribute("oldPartNumber", (p.oldPartNumber != null) ? p.oldPartNumber : ""),
                                                   new XAttribute("listPrice", String.Format("{0:C}", (from prices in p.Prices
                                                                                                       where prices.priceType.Equals("List")
                                                                                                       select prices.price1 != null ? prices.price1 : (decimal?)0).FirstOrDefault<decimal?>())),
                                                   new XAttribute("pClass", (c != null) ? c.class1 : ""),
                                                   new XAttribute("priceCode", (p.priceCode != null) ? p.priceCode : 0),
                                                   new XAttribute("relatedCount", db.RelatedParts.Where(x => x.partID == p.partID).Select(x => new { relatedID = x.relatedID }).Count()),
                                                   new XElement("Attributes", (from pa in p.PartAttributes
                                                                               orderby pa.sort
                                                                               select new XElement(XmlConvert.EncodeName(pa.field), pa.value)).ToList<XElement>()),
                                                   new XElement("Content", (from co in p.ContentBridges
                                                                            orderby co.Content.ContentType.type, co.contentID
                                                                            select new XElement(XmlConvert.EncodeName(co.Content.ContentType.type), co.Content.text)).ToList<XElement>()),
                                                   new XElement("Videos", (from vp in p.PartVideos
                                                                           orderby vp.vTypeID
                                                                           select new XElement("Video", vp.video,
                                                                               new XAttribute("videoID", vp.pVideoID),
                                                                               new XAttribute("isPrimary", vp.isPrimary),
                                                                               new XAttribute("type", vp.videoType.name),
                                                                               new XAttribute("icon", vp.videoType.icon ?? ""),
                                                                               new XAttribute("typeID", vp.vTypeID)
                                                                           )).ToList<XElement>()),
                                                   new XElement("Pricing", (from pr in p.Prices
                                                                            orderby pr.priceType
                                                                            select new XElement(XmlConvert.EncodeName(pr.priceType), pr.price1.ToString())).ToList<XElement>()),
                                                   new XElement("Reviews",
                                                       new XAttribute("averageReview", (from r in p.Reviews
                                                                                        where r.active.Equals(true) && r.approved.Equals(true) && r.cust_id.Equals(this.custID)
                                                                                        select (double?)r.rating).Average() ?? 0.0),
                                                                           (from r in p.Reviews
                                                                            where r.active.Equals(true) && r.approved.Equals(true) && r.cust_id.Equals(this.custID)
                                                                            orderby r.createdDate descending
                                                                            select new XElement("Review",
                                                                                new XAttribute("reviewID", r.reviewID),
                                                                                new XElement("createdDate", String.Format("{0:MM/dd/yyyy hh:mm tt}", r.createdDate)),
                                                                                new XElement("rating", r.rating),
                                                                                new XElement("name", r.name),
                                                                                new XElement("email", r.email),
                                                                                new XElement("subject", r.subject),
                                                                                new XElement("review_text", r.review_text))).Take(10).ToList<XElement>()),
                                                   new XElement("Images",
                                                                       (from pin in p.PartImages
                                                                        group pin by pin.sort into pi
                                                                        orderby pi.Key
                                                                        select new XElement("Index",
                                                                                            new XAttribute("name", pi.Key.ToString()),
                                                                                            (from pis in db.PartImages
                                                                                             join pig in db.PartImageSizes on pis.sizeID equals pig.sizeID
                                                                                             where pis.partID.Equals(p.partID) && pis.sort.Equals(pi.Key)
                                                                                             orderby pis.sort, pis.sizeID
                                                                                             select new XElement(pig.size,
                                                                                                                 new XAttribute("imageID", pis.imageID),
                                                                                                                 new XAttribute("path", pis.path),
                                                                                                                 new XAttribute("height", pis.height),
                                                                                                                 new XAttribute("width", pis.width)
                                                                                             )).ToList<XElement>()
                                                                    )).ToList<XElement>())
                                               )).AsParallel().ToList<XElement>();
            parts.Add(partlist);
            xml.Add(parts);
            return xml;
        }
Ejemplo n.º 12
0
        internal void Authenticate(string key)
        {
            if (key == null || key.Length == 0) { throw new Exception("Invalid API key."); }
            if (this.custID == 0) { throw new Exception("Invalid customerID"); }
            Guid api_key;
            try {
                api_key = Guid.Parse(key);
            } catch {
                throw new Exception("Invalid API key.");
            }

            CurtDevDataContext db = new CurtDevDataContext();
            Customer cust = db.Customers.Where(x => x.customerID.Equals(this.custID) & x.APIKey.Equals(api_key)).FirstOrDefault<Customer>();
            if (cust == null || cust.cust_id == 0) {
                throw new Exception("Denied Access.");
            }
        }
Ejemplo n.º 13
0
        public CartIntegration Set(string key)
        {
            Authenticate(key);
            CurtDevDataContext db = new CurtDevDataContext();

            // Validate required fields

            // This isn't working because the customer needs to be able to unlink
            // if (this.custPartID == 0) { throw new Exception("Customer Part ID failed to validate against null or zero."); }
            //  - ajn
            if (this.partID == 0) { throw new Exception("Part Number failed to validate against null or zero."); }

            // Attempt to get a CustomerPricing record for this customerID and partID
            List<CartIntegration> tmpIntegrations = db.CartIntegrations.Where(x => x.custID.Equals(this.custID) && x.partID.Equals(this.partID)).ToList<CartIntegration>();
            CartIntegration newintegration = new CartIntegration();
            List<CartIntegration> deleteables = new List<CartIntegration>();
            bool updated = false;
            foreach (CartIntegration tmpIntegration in tmpIntegrations) {
                if (tmpIntegration.custPartID == 0) {
                    deleteables.Add(tmpIntegration);
                } else {
                    if (!updated && this.custPartID > 0) {
                        tmpIntegration.custPartID = this.custPartID;
                        updated = true;
                    } else {
                        deleteables.Add(tmpIntegration);
                    }
                }
            }
            if (!updated && this.custPartID > 0) {
                newintegration = this;
                newintegration.custID = this.custID;
                db.CartIntegrations.InsertOnSubmit(newintegration);
            }
            if (deleteables.Count > 0) {
                db.CartIntegrations.DeleteAllOnSubmit(deleteables);
            }
            db.SubmitChanges();

            CartIntegration cartIntegration = new CartIntegration {
                custID = this.custID,
                partID = newintegration.partID,
                custPartID = newintegration.custPartID,
                referenceID = newintegration.referenceID
            };
            return cartIntegration;
        }