/* Sale * 0. Get info about purchase from program (Customer, Number of Shares, Total (money)) * 1. Create empty operation * 2.1 Get Customer balance info * 2.2 - Customer balance amount * 3. Create blocked money * 4.1 Get Seller shares number info * 4.2 - Seller shares number * 5. Create blocked shares number // after that action purchase can't cancel * 6.1 Get Seller balance info * 6.2 + Seller balance amount * 7.1 Get Customer shares number info * 7.2 + Customer shares number * 8. Fill operation columns * 9. Remove blocked money * 10. Remove blocked shares number */ public void Deal(int customerId, int shareId, int requiredSharesNumber) { OperationEntity operation = null; SharesNumberEntity customerSharesNumber = null; SharesNumberEntity sellerSharesNumber = null; ClientEntity customer = null; ClientEntity seller = null; BalanceEntity customerBalance = null; BalanceEntity sellerBalance = null; ShareEntity share = null; BlockedMoneyEntity blockedMoney = null; bool blockedMoneyFlag = false; BlockedSharesNumberEntity blockedSharesNumber = null; bool blockedSharesNumberFlag = false; decimal customerBalanceAmount = 0M; decimal total = 0M; int sellerSharesNumberNumber = 0; operation = CreateOperation(); try { sellerSharesNumber = SearchSharesNumberForBuy(shareId, requiredSharesNumber); // search required shares on stock exchange seller = sellerSharesNumber.Client; share = sellerSharesNumber.Share; customerBalance = SearchBalanceByClientId(customerId); customer = customerBalance.Client; sellerBalance = SearchBalanceByClientId(seller.Id); // get total CheckShareAndShareTypeStatuses(share); total = share.Type.Cost * requiredSharesNumber; // Blocked money customerBalanceAmount = customerBalance.Amount; if (customerBalanceAmount < total) { throw new ArgumentException("Customer don't have enough money."); } blockedMoneyFlag = ChangeBalance(customerBalance, customerBalanceAmount - total); blockedMoney = CreateBlockedMoney(new BlockedMoneyRegistrationInfo() { Operation = operation, ClientBalance = customerBalance, Total = total }); // Blocked shares Number sellerSharesNumberNumber = sellerSharesNumber.Number; blockedSharesNumberFlag = ChangeSharesNumber(sellerSharesNumber, sellerSharesNumberNumber - requiredSharesNumber); blockedSharesNumber = CreateBlockedSharesNumber(new BlockedSharesNumberRegistrationInfo() { ClientSharesNumber = sellerSharesNumber, Operation = operation, //Share = sellerSharesNumber.Share, //ShareTypeName = sellerSharesNumber.Share.Type.Name, //Cost = sellerSharesNumber.Share.Type.Cost, Number = requiredSharesNumber }); } catch (Exception e) { RemoveOperation(operation); if (blockedMoneyFlag) { ChangeBalance(customerBalance, customerBalanceAmount + total); if (blockedMoney != null) { RemoveBlockedMoney(blockedMoney); } } if (blockedSharesNumberFlag) { ChangeSharesNumber(sellerSharesNumber, sellerSharesNumberNumber + requiredSharesNumber); if (blockedSharesNumber != null) { RemoveBlockedSharesNumber(blockedSharesNumber); } } throw new ArgumentException($"Deal was broken cause: {e.Message}"); } if (sellerSharesNumber.Number == 0) { RemoveSharesNumber(sellerSharesNumber); } ChangeBalance(sellerBalance, sellerBalance.Amount + total); customerSharesNumber = SearchOrCreateSharesNumberForAddition(customer, share); ChangeSharesNumber(customerSharesNumber, customerSharesNumber.Number + requiredSharesNumber); FillOperationColumns(blockedMoney, blockedSharesNumber); RemoveBlockedMoney(blockedMoney); RemoveBlockedSharesNumber(blockedSharesNumber); }
public static Operation[] GetOperations(DateTime from, DateTime to) { var ret = new List <Operation>(); using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select o.ID, o.[Date], o.[Description], o.Operation, o.CurrentBalance, o.Notes, o.Exception," + " o.AutoEntityID, o.ForceEntityID, o.ForceTypeID " //+ " auto_oe.ID as AutoEntityID, auto_oe.[Description] as AutoEntityDesc, auto_oe.[Place] as AutoEntityPlace," //+ " auto_ot.ID as AutoEntityTypeID, auto_ot.[Description] as AutoEntityTypeDesc," //+ " forced_oe.ID as ForcedEntityID, forced_oe.[Description] as ForcedEntityDesc, forced_oe.[Place] as ForcedEntityPlace," //+ " forced_oeot.ID as ForcedEntityTypeID, forced_oeot.[Description] as ForcedEntityTypeDesc," //+ " forced_ot.ID as ForcedTypeID, forced_ot.[Description] as ForcedTypeDesc" + " from Operations o with(nolock)" //+ " left join OperationEntities auto_oe with(nolock) on auto_oe.ID = o.AutoEntityID" //+ " left join OperationTypes auto_ot with(nolock) on auto_ot.ID = auto_oe.IDType" //+ " left join OperationEntities forced_oe with(nolock) on forced_oe.ID = o.ForceEntityID" //+ " left join OperationTypes forced_oeot with(nolock) on forced_oeot.ID = forced_oe.IDType" //+ " left join OperationTypes forced_ot with(nolock) on forced_ot.ID = o.ForceTypeID" + " where o.[Date] between @from and @to" + " order by o.[Date] asc"; cmd.Parameters.AddWithValue("@from", from); cmd.Parameters.AddWithValue("@to", to); foreach (DataRow row in doSelect(cmd).Rows) { var id = Convert.ToInt32(row["ID"]); var date = Convert.ToDateTime(row["Date"]); var desc = Convert.ToString(row["Description"]); var op = Convert.ToDouble(row["Operation"]); var curr = Convert.ToDouble(row["CurrentBalance"]); var notes = Convert.ToString(row["Notes"]); var exception = Convert.ToBoolean(row["Exception"]); OperationEntity autoEntity = null; OperationEntity forcedEntity = null; OperationType forcedType = null; if (row["AutoEntityID"] != DBNull.Value) { autoEntity = Keeper.Entities[Convert.ToInt32(row["AutoEntityID"])]; } if (row["ForceEntityID"] != DBNull.Value) { forcedEntity = Keeper.Entities[Convert.ToInt32(row["ForceEntityID"])]; } if (row["ForceTypeID"] != DBNull.Value) { forcedType = Keeper.Types[Convert.ToInt32(row["ForceTypeID"])]; } var operation = new Operation(id, date, desc, new OperationValue(op), new OperationValue(curr), autoEntity, notes, exception, forcedEntity, forcedType); ret.Add(operation); } } return(ret.ToArray()); }
public void RemoveOperation(OperationEntity operation) { this.operationTableRepository.Remove(operation.Id); this.operationTableRepository.SaveChanges(); }
public void Add(OperationEntity entity) { this.dBContext.Operations.Add(entity); }