public void CalculateCost(ICostChangeableCallInfo callInfo)
        {
            if (callInfo == null)
            {
                throw new ArgumentNullException("Source terminal is null");
            }

            var contract = GetActiveContractForTerminal(callInfo.Source, callInfo.Started);

            if (contract == null)
            {
                throw new ArgumentException(String.Format("No active contract is for terminal {0} on {1}", callInfo.Source.TerminalNumber, callInfo.Started));
            }

            var plan = GetActiveBillingPlanForContract(contract, callInfo.Started);

            if (plan == null)
            {
                throw new ArgumentException(String.Format("No plan is active for contract"));
            }

            DateTime periodStart = new DateTime(Math.Max(contract.ContractStartDate.Ticks, callInfo.Started.AddMonths(-1).Ticks));
            var      previous    = GetCallInfos(
                x => x.Started >= periodStart &&
                System.Data.Entity.DbFunctions.AddMilliseconds(
                    x.Started,
                    System.Data.Entity.DbFunctions.DiffMilliseconds(x.Duration, TimeSpan.Zero))
                < callInfo.Started);

            callInfo.Cost = plan.CalculateCost(callInfo, previous);
        }
 public void Add(ICostChangeableCallInfo model)
 {
     if (model == null)
     {
         throw new ArgumentNullException("model", "");
     }
     this.CalculateCost(model);
     using (IBillingRepository <ICallInfo> repository = new CallInfoRepository())
     {
         var entity = Mapper.Mapper.DTOtoEntity(model);
         repository.Add(entity);
         repository.SaveChanges();
     }
 }