private async Task <CommandResult> FinishOrderAsync(OrderEntity orderEntity, DateTime sellDate) { using (var transaction = transactionScope.BeginTransaction()) { var settingEntity = query.Single <SettingEntity>(x => x.Id == SettingConsts.Current); settingEntity.InvoiceLastNumber++; var now = DateTime.Now; orderEntity.Finish = now; orderEntity.OrderStatusId = (int)OrderStatusEnum.COMPLETED; store.Update(orderEntity); var licenceEntity = new LicenceEntity { Id = Guid.NewGuid(), CustomerId = orderEntity.CustomerId, ProductId = orderEntity.ProductId, ValidFrom = now, ValidTo = now.AddYears(1), Quantity = 1, }; store.Create(licenceEntity); var tokenEntity = new TokenEntity { Id = Guid.NewGuid(), CustomerId = orderEntity.CustomerId, Active = true, Created = now, Data = Tokenizer.Token(), Expires = now.AddHours(SettingConsts.TokenPeriod), }; store.Create(tokenEntity); var invoiceEntity = new InvoiceEntity { LicenceId = licenceEntity.Id, ExposureDate = sellDate, SellDate = sellDate, PaymentDays = 0, PaymentDate = sellDate, PaymentTypeId = (int)PaymentTypeEnum.Cash, Number = InvoiceNumberBuilder.Build(settingEntity.InvoiceLastNumber, now), }; store.Create(invoiceEntity); // update settings store.Update(settingEntity); // Send mail with token link await _mailService.SendTokenAsync(tokenEntity.Id); // Send mail with invoice await _mailService.SendInvoiceAsync(invoiceEntity.LicenceId); transaction.Commit(); return(CommandResult.Ok); } }
public async Task <CommandResult <PaymentUpdateModel> > RemitanceAsync(PaymentUpdateModel model) { try { if (Validate(model)) { using (var transaction = transactionScope.BeginTransaction()) { var now = DateTime.Now; var productContract = await query.Get <ProductEntity>().Where(x => x.Id == model.Product.Id).SelectProductContract().SingleAsync(); var customerEntity = await _customerService.CreateAsync(model.Customer); // Create order var orderEntity = new OrderEntity { Id = Guid.NewGuid(), CustomerId = customerEntity.Id, ProductId = model.Product.Id, Start = now, OrderStatusId = (int)OrderStatusEnum.PENDING, Token = Tokenizer.Token(), OrderTypeId = (int)OrderTypeEnum.REMITANCE, Quantity = 1, }; await store.CreateAsync(orderEntity); // Create pro forma invoice var lastNumber = query.Get <ProFormaEntity>().Min(x => (int?)x.Number) ?? 0; var proFormaEntity = new ProFormaEntity { OrderId = orderEntity.Id, ExposureDate = now, Number = lastNumber + 1, FullNumber = InvoiceNumberBuilder.Build(lastNumber + 1, now), }; await store.CreateAsync(proFormaEntity); // Send mail with pro forma await _mailService.SendProFormaAsync(orderEntity.Id); transaction.Commit(); return(new CommandResult <PaymentUpdateModel>(model)); } } return(new CommandResult <PaymentUpdateModel>(model, Localization.Resource.Validation_Summary_Error)); } catch (Exception e) { log.Error(nameof(RemitanceAsync), model, e); return(new CommandResult <PaymentUpdateModel>(model, e)); } }