public Transaction SetDeal(Transaction deal) { Transaction result = null; decimal totalPrice = Quantity * Price; decimal done = Deals.Select(it => it.Quantity * it.Price).Sum(); decimal left = totalPrice - done; decimal required = deal.Quantity * deal.Price; if (left >= required) { deal.MainTransactionId = TransactionId; deal.SetTransactionComplete(); Deals.Add(deal); } else { int leftQuantity = Quantity - Deals.Sum(it => it.Quantity); Transaction part = new Transaction(TransactionId, deal.Initials, deal.Type, deal.Price, true, leftQuantity); Deals.Add(part); result = new Transaction(deal.Initials, deal.Type, deal.Price, (deal.Quantity - leftQuantity)); } CheckCompletion(); return(result); }