/// <summary> /// modify quote's IsBuildOrder to true, and add a new order record /// </summary> /// <param name="orderId"></param> /// <param name="model"></param> /// <returns></returns> public async Task <Operate> AddQuoteToOrder(int orderId, OrderInfoModel model, string name, string rootUrl) { var result = new Operate(); var orderIdForLog = 0; try { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { if (string.IsNullOrWhiteSpace(model.ContactEmail)) { result.Status = -1; result.Message = "Email address is required"; return(result); } if (!EmailHelper.IsValidEmail(model.ContactEmail)) { result.Status = -1; result.Message = "Email address is not valid"; return(result); } //get customer info var customerId = model.CustomerId; var customer = await _customerRepository.GetById(customerId); if (customer == null) { result.Status = -1; result.Message = "User does not exist"; return(result); } var quote = await _orderRepositroy.GetOrderById(orderId); //no quote if (quote == null) { result.Status = -1; result.Message = "Quote does not exist"; return(result); } //if build order if (quote.IsBuildOrder ?? false) { result.Status = -1; result.Message = "Quote has been placed order"; return(result); } if (string.IsNullOrWhiteSpace(model.PurchaseCode)) { result.Status = -1; result.Message = "Purchase Order number is empty"; return(result); } //change is build order for quote quote.IsBuildOrder = true; quote.EditTime = DateTime.UtcNow; quote.EditBy = name; await _orderRepositroy.AddOrUpdate(quote); var orderModel = model.ToOrderModel(); //purchase code or contract number orderModel.PurchaseType = customer.PurchaseType; orderModel.PurchaseNumber = model.PurchaseCode; //set order id orderModel.Id = 0; orderModel.ContactEmail = model.ContactEmail; orderModel.Mode = ((int)(Constants.OrderMode.Order)).ToString(); //create quote code var orderNumber = await _orderNumberRepository.GetOrderNumberByMode(((int)(Constants.OrderMode.Order)).ToString()); var orderCode = OrderCodeHelper.GenerateOrderCode(orderNumber.Prefix, orderNumber.CurrentNumber ?? 0); //set order code orderModel.OrderCode = orderCode; //set quote code orderModel.QuoteCode = quote.QuoteCode; //save order var order = orderModel.ToOrderModel(); order.CreateBy = name; order.CreateTime = DateTime.UtcNow; order.CompanyName = customer.CompanyName; order.CustomerContactName = customer.ContactName; order.ManagerId = customer.ManagerId; order.ManagerName = customer.Manager?.Name; await _orderRepositroy.AddOrUpdate(order); orderIdForLog = order.Id; //add order address var orderAddr = model.ToOrderAddrModel(); if (orderAddr == null) { result.Status = -1; result.Message = "Delivery address is reqired"; return(result); } orderAddr.Id = 0; orderAddr.OrderId = order.Id; orderAddr.CreateBy = name; orderAddr.CreateTime = DateTime.UtcNow; await _orderAddrRepository.AddOrUpdate(orderAddr); //add order product var orderProducts = model.ToOrderProductsModel(); if (orderProducts == null || orderProducts.Count <= 0) { result.Status = -1; result.Message = Constants.ErrorOrderHasNoProduct; return(result); } foreach (var product in orderProducts) { product.Id = 0; product.OrderId = order.Id; product.CreateBy = name; product.CreateTime = DateTime.UtcNow; await _orderProductRepository.AddOrUpdate(product); } //add order document if (customer.PoDocMandatory ?? false) { var orderDoc = model.ToOrderDocModel(); if (orderDoc == null) { result.Status = -1; result.Message = "PO Doc is required"; return(result); } orderDoc.Id = 0; orderDoc.OrderId = order.Id; orderDoc.CreateBy = name; orderDoc.CreateTime = DateTime.UtcNow; await _orderDocRepository.AddOrUpdate(orderDoc); } //add order history var history = new OrderHistory { Id = 0, CreateTime = DateTime.UtcNow, CreateBy = name, TableName = "Order", FieldName = "", OldValue = quote.Id.ToString(), NewValue = order.Id.ToString(), Note = Constants.OrderHistoryQuoteToOrder, Operator = Constants.OrderHistoryAddOperator, OrderId = order.Id }; await _orderHistoryRepository.AddOrUpdate(history); //TODO: SEND EMAIL OR OTHER TASK scope.Complete(); BackgroundJob.Enqueue(() => CreateOrderPdfAndSendEmail(rootUrl, customer.ManagerId ?? 0, model, order, name, "sendOrder")); } } catch (Exception ex) { //add order history var history = new OrderHistory { Id = 0, CreateTime = DateTime.UtcNow, CreateBy = name, TableName = "", FieldName = "", OldValue = "", NewValue = "", Note = Constants.OrderHistoryAddQuote, Operator = Constants.OrderHistoryErrorOperator, OrderId = orderIdForLog }; await _orderHistoryRepository.AddOrUpdate(history); result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLogForOrder("OrderService", "AddQuoteToOrder", ex, orderIdForLog); } return(result); }
/// <summary> /// create order by [name] /// </summary> /// <param name="model"></param> /// <param name="name"></param> /// <returns></returns> public async Task <Operate> AddQuote(OrderInfoModel model, string name, string rootUrl) { var result = new Operate(); var orderId = 0; try { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { if (string.IsNullOrWhiteSpace(model.ContactEmail)) { result.Status = -1; result.Message = "Email address is required"; return(result); } if (!EmailHelper.IsValidEmail(model.ContactEmail)) { result.Status = -1; result.Message = "Email address is not valid"; return(result); } //get customer info var customerId = model.CustomerId; var customer = await _customerRepository.GetById(customerId); if (customer == null) { result.Status = -3; result.Message = "User does not exist"; return(result); } if (customer.ManagerId == 0 || customer.ManagerId == null || customer.Manager == null) { result.Status = -4; result.Message = "Manager needed,PLease contact your adminstrator"; return(result); } var orderModel = model.ToOrderModel(); //set order id orderModel.Id = 0; orderModel.ContactEmail = orderModel.ContactEmail; orderModel.ContactName = orderModel.ContactName; orderModel.ContactPhone = orderModel.ContactPhone; orderModel.Mode = ((int)(Constants.OrderMode.Quote)).ToString(); //create quote code var quoteNumber = await _orderNumberRepository.GetOrderNumberByMode(((int)(Constants.OrderMode.Quote)).ToString()); var quoteCode = OrderCodeHelper.GenerateOrderCode(quoteNumber.Prefix, quoteNumber.CurrentNumber ?? 0); //set quote code orderModel.QuoteCode = quoteCode; //save order var order = orderModel.ToOrderModel(); order.CreateBy = name; order.CreateTime = DateTime.UtcNow; order.CompanyName = customer.CompanyName; order.CustomerContactName = customer.ContactName; order.ManagerId = customer.ManagerId; order.ManagerName = customer.Manager?.Name; var orderAddr = model.ToOrderAddrModel(); await _orderRepositroy.AddOrUpdate(order); orderId = order.Id; //add order address if (orderAddr != null) { orderAddr.Id = 0; orderAddr.OrderId = order.Id; orderAddr.CreateBy = name; orderAddr.CreateTime = DateTime.UtcNow; await _orderAddrRepository.AddOrUpdate(orderAddr); } //add order product var orderProducts = model.ToOrderProductsModel(); if (orderProducts == null || orderProducts.Count <= 0) { result.Status = -1; result.Message = Constants.ErrorOrderHasNoProduct; return(result); } foreach (var product in orderProducts) { product.Id = 0; product.OrderId = order.Id; product.CreateBy = name; product.CreateTime = DateTime.UtcNow; await _orderProductRepository.AddOrUpdate(product); } //add order history var history = new OrderHistory { Id = 0, CreateTime = DateTime.UtcNow, CreateBy = name, TableName = "", FieldName = "", OldValue = "", NewValue = "", Note = Constants.OrderHistoryAddQuote, Operator = Constants.OrderHistoryAddOperator, OrderId = order.Id }; await _orderHistoryRepository.AddOrUpdate(history); scope.Complete(); //await SendEmailById(order.Id, rootUrl, "sendQuote"); //create pdf and send email BackgroundJob.Enqueue(() => CreateQuotePdfAndSendEmail(rootUrl, customer.ManagerId ?? 0, model, order, name, "sendQuote")); } } catch (Exception ex) { //add order history var history = new OrderHistory { Id = 0, CreateTime = DateTime.UtcNow, CreateBy = name, TableName = "", FieldName = "", OldValue = "", NewValue = "", Note = Constants.OrderHistoryAddQuote, Operator = Constants.OrderHistoryErrorOperator, OrderId = orderId }; await _orderHistoryRepository.AddOrUpdate(history); result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLogForOrder("OrderService", "AddQuote", ex, orderId); } return(result); }