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; }
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(); }
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; }