private async Task UpgradeLead(SaleProcess lastProcess, string upgradeTo, Contact contact, User user) { var newOperation = new Operation(); switch (upgradeTo) { case "Opportunity": newOperation.Opportunity = true; break; case "Quote": newOperation.Quote = true; break; case "Invoice": newOperation.Invoice = true; break; } newOperation.Date = DateTime.Now; await _operationsRepository.Add(newOperation, user); var newProcess = new SaleProcess { Operation = newOperation, OperationId = newOperation.Id, Contact = contact, ContactId = contact.Id, OrderId = lastProcess.OrderId ?? null, Index = lastProcess.Index + 1 }; await _context.SaleProcess.AddAsync(newProcess); }
public async Task<Unit> Handle(StartSaleProcessCommand request, CancellationToken cancellationToken) { var user = await _userAccessor.GetLoggedUser(); request.Contact.Status = "Lead"; var operation = new Operation(); operation.Lead = true; operation.Source = "Former Client"; operation.Date = DateTime.Now; await _operationsRepository.Add(operation, user); var saleProcess = new SaleProcess { Contact = request.Contact, ContactId = request.Contact.Id, Operation = operation, OperationId = operation.Id, OrderId = null, Index = 0 }; _context.SaleProcess.Add(saleProcess); request.Contact.CurrentSale.Add(saleProcess); var success = await _context.SaveChangesAsync() > 0; if (success) return Unit.Value; throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(CloseOrderCommand request, CancellationToken cancellationToken) { var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetLoggedUsername()); request.Order.Closed = true; request.Order.DateOrderClosed = DateTime.Now; //add revenue (only sale orders) if (request.Order.Type) { Operation operation = new Operation(); operation.Revenue = request.Order.Price; await _operationsRepository.Add(operation, user); } var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(AddOrderCommand request, CancellationToken cancellationToken) { var user = await _userAccessor.GetLoggedUser(); int orderNumber = setOrderNumber(); var order = new Order { Id = Guid.NewGuid(), OrderNumber = orderNumber, UserId = user.Id, ClientId = request.Client.Id, Client = request.Client, Type = request.Type, Closed = false, Product = request.Product, Amount = request.Amount, Price = request.Price, DateOrderOpened = DateTime.Now, Notes = request.Notes, }; _context.Orders.Add(order); //add the order to the client request.Client.Orders.Append(order); //register this operation var operation = new Operation(); operation.Order = true; operation.Date = order.DateOrderOpened; await _operationsRepository.Add(operation, user); // await operation.addOperation(operation, _context, user); //get operations which belong to the currently managed sale process IQueryable <SaleProcess> saleProcess = _context.SaleProcess.Where(x => x.ContactId == request.Client.Id); //handle the order in case there is a sale process open already if (saleProcess.Count() > 0 && order.Type) { await handleSaleProcess(order.Id, saleProcess); } var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task <Unit> Handle(AddLeadCommand request, CancellationToken cancellationToken) { var contact = new Contact { Id = Guid.NewGuid(), Name = request.Contact.Name, Type = "Client", Company = request.Contact.Company, PhoneNumber = request.Contact.PhoneNumber, DateAdded = DateTime.Now, Email = request.Contact.Email, Notes = request.Contact.Notes, Status = "Lead", Source = request.Contact.Source, Premium = false, SuccessfulDeals = 0 }; _context.Contacts.Add(contact); var userAccess = new UserContact { Id = Guid.NewGuid(), User = request.User, UserId = request.User.Id, Contact = contact, ContactId = contact.Id, DateAdded = request.Contact.DateAdded }; _context.UserContacts.Add(userAccess); var operation = new Operation(); operation.Lead = true; operation.Source = request.Contact.Source; operation.Date = contact.DateAdded; var saveOperation = await _operationsRepository.Add(operation, request.User); if (!saveOperation) { throw new Exception("Problem saving operation"); } var saleProcess = new SaleProcess { Contact = contact, ContactId = contact.Id, Operation = operation, OperationId = operation.Id, OrderId = null, Index = 0 }; _context.SaleProcess.Add(saleProcess); contact.CurrentSale.Add(saleProcess); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public async Task SaveOperation(OperationSaveRequest source) { var entity = _mapper.Map <Operation>(source); await _repository.Add(entity); }