Skip to content

Instantly share code, notes, and snippets.

@thePunderWoman
Created June 11, 2013 16:35
Show Gist options
  • Save thePunderWoman/5758409 to your computer and use it in GitHub Desktop.
Save thePunderWoman/5758409 to your computer and use it in GitHub Desktop.
Update to Set Pricing
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."); }
}
// 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.");
}
// 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) {
if (sale_end < DateTime.Now) {
// expired sale - delete
deletables.Add(tmpPoint);
}
if (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;
updated = true;
} else {
deletables.Add(tmpPoint);
}
}
}
if (!updated) {
db.CustomerPricings.InsertOnSubmit(this);
}
if (deletables.Count > 0) {
db.CustomerPricings.DeleteAllOnSubmit(deletables);
}
db.SubmitChanges();
SimplePricing pricePoint = new SimplePricing {
cust_id = this.cust_id,
partID = this.partID,
price = this.price,
isSale = this.isSale,
sale_start = this.sale_start.ToString(),
sale_end = this.sale_end.ToString()
};
return pricePoint;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment