using CAPI.Custom.Data; using CAPI.Custom.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CAPI.Custom.Services.Quotes { public class QuoteItemService : SurfaceChildItemService { public QuoteItemService(QuotesContext db = null) { if (db.IsNotNull()) this.db = db; } public override IQueryable GetByParentId(int parentId) => base.GetByParentId(parentId) .OrderBy(p => p.Sequence) .ThenBy(p => p.SupplierSequence) .ThenBy(p => p.Supplier); public QuoteItem Recalculate(QuoteItem quoteItem) { var stored = db.QuoteItems.Find(quoteItem.recId); bool marginChanged = stored.Margin != quoteItem.Margin; bool sellingPriceChanged = stored.SellingPrice != quoteItem.SellingPrice; if (marginChanged) { // set new margin stored.Margin = quoteItem.Margin; // calculate new selling price stored.SellingPrice = BusinessUtils.GetSellingPrice( costPrice: stored.CostPrice, newMargin: quoteItem.Margin); } else if (sellingPriceChanged) { // set new selling price stored.SellingPrice = quoteItem.SellingPrice; // calculate new margin stored.Margin = BusinessUtils.GetMargin( costPrice: stored.CostPrice, newSellingPrice: quoteItem.SellingPrice); } stored.QuantitySelected = quoteItem.QuantitySelected; db.SaveChanges(); return db.QuoteItems.Find(quoteItem.recId); } } }