public string BillProject(int projectId, List <InvoiceUserRequest> invoiceUserRequest) { var billProjectCommand = new BillProjectCommand { ProjectId = projectId, ByUserId = _currentUser.Id, InvoiceUserRequest = invoiceUserRequest.Where(i => i.Hours > 0).ToList() }; _cqrs.Execute(billProjectCommand); return(billProjectCommand.Result); }
public IEnumerable <IEvent <ProjectTimeAggregateRoot, int> > Execute(BillProjectCommand command) { CheckUserBillingRights(command.ProjectId, command.ByUserId); if (!command.InvoiceUserRequest.Any()) { throw new ArgumentException("Invoice is empty."); } var invoiceNum = InvoiceNum(command.ProjectId); command.Result = invoiceNum; foreach (var userInvoiceInfo in command.InvoiceUserRequest) { if (_projectService.GetProjectUserInfo(userInvoiceInfo.UserId, command.ProjectId) == null) { throw new ArgumentException(String.Format("User {0} is not associated with project {1}.", userInvoiceInfo.UserId, command.ProjectId)); } var maxBillableHours = State.MaxBillableHours(userInvoiceInfo.UserId); if (maxBillableHours < userInvoiceInfo.Hours) { throw new ArgumentException(String.Format("Max {0} hours could be billed for user {1}.", maxBillableHours, userInvoiceInfo.UserId)); } var sum = State.CalculateInvoiceSum(userInvoiceInfo.UserId, userInvoiceInfo.Hours); var ev = new InvoiceEvent { At = DateTimeOffset.Now, EventType = InvoiceEventType.Billing, Hours = userInvoiceInfo.Hours, InvoiceSum = sum, ProjectId = command.ProjectId, UserId = userInvoiceInfo.UserId, RegisteredAt = DateTimeOffset.Now, RegisteredByUserId = command.ByUserId, InvoiceNum = invoiceNum }; yield return(ev); } }