Esempio n. 1
0
        public async Task CreateQuotePdfAndSendEmail(string rootUrl, int managerId, OrderInfoModel model, Order order, string name, string isTripleEmail = null)
        {
            try
            {
                var manager = await _managerRepository.GetById(managerId);

                var orderAddr = model.ToOrderAddrModel();
                var pdfModel  = new PdfQuoteModel
                {
                    RootUrl      = rootUrl,
                    TemplatePath = System.Configuration.ConfigurationManager.AppSettings["Template_JobQuote"],
                    FileName     = string.Format("quote{0}", order.QuoteCode),
                    HeadContact  = "PO Box 1111" + "<br>" + "Burswood WA 6100 " + "<br>" + "T (08) 9470 7300" + "<br>" + "F (08) 9362 6210" + "<br>" + "<br>"
                                   + "E [email protected]" + "<br>" + "www.datanet.com.au" + "<br>" + "<br>" + "ABN: 56 123 914 616",
                    HeadDeliverTo = order.ContactName + (!string.IsNullOrEmpty(order.ContactName) ? "<br>" : "") +
                                    order.CompanyName + (!string.IsNullOrEmpty(order.CompanyName) ? "<br>" : "") +
                                    orderAddr?.Addr1 + (!string.IsNullOrEmpty(orderAddr?.Addr1) ? "<br>" : "") +
                                    orderAddr?.Addr2 + (!string.IsNullOrEmpty(orderAddr?.Addr2) ? "<br>" : "") +
                                    orderAddr?.Addr3 + (!string.IsNullOrEmpty(orderAddr?.Addr3) ? "<br>" : "")
                                    + orderAddr?.State + "     " + orderAddr?.PostCode,
                    HeadQuoteTo        = order.CompanyName,
                    HeadQuoteDate      = order.CreateTime?.AddHours(model.TimeZone).ToString("dd'/'MM'/'yyyy"),
                    HeadQuotation      = order.QuoteCode,
                    HeadAccountManager = order.ManagerName + "<br>" + manager?.Phone + "<br>" + "<a href='mailto:" + manager?.Email + "'>" +
                                         manager?.Email + "</a>",
                    SumSubTotal     = (order.Amount - order.GST)?.ToString("c2", CulturesHelper.Australia),
                    SumGst          = order.GST?.ToString("c2", CulturesHelper.Australia),
                    SumAmount       = order.Amount?.ToString("c2", CulturesHelper.Australia),
                    QuoteDetailList = new List <PdfQuoteDetailModel>()
                    {
                    }
                };
                foreach (var productItem in model.OrderProducts)
                {
                    var product = await _productRepository.GetById(productItem.ProductId);

                    pdfModel.QuoteDetailList.Add(new PdfQuoteDetailModel()
                    {
                        StockCode   = product.Code,
                        Description = product.ShortDesc,
                        Qty         = productItem.Quantity.ToString() + ".00",
                        UnitPrice   = productItem.Price?.ToString("c2", CulturesHelper.Australia),
                        LineTotal   = ((productItem.Quantity * productItem.Price)?.ToString("c2", CulturesHelper.Australia))
                    });
                }
                pdfModel.QuoteDetailList.Add(new PdfQuoteDetailModel()
                {
                    StockCode   = "DELIVERY",
                    Description = "Delivery",
                    Qty         = "1.00",
                    UnitPrice   = model.DeliveryCharge.ToString("c2", CulturesHelper.Australia),
                    LineTotal   = model.DeliveryCharge.ToString("c2", CulturesHelper.Australia)
                });
                order.PDFPath = PdfHelper.GetQuotePdfPath(pdfModel);
                await _orderRepositroy.AddOrUpdate(order);

                // await SendEmailById(order.Id, rootUrl, isTripleEmail);
                var sendEmailResult = await SendEmailById(order.Id, rootUrl, isTripleEmail);

                if (sendEmailResult.Status != 0)
                {
                    throw new Exception(sendEmailResult.Message);
                }
            }
            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    = order.Id
                };
                await _orderHistoryRepository.AddOrUpdate(history);

                Logger.WriteErrorLogForOrder("OrderService", "CreateQuotePdfAndSendEmail", ex, order.Id);
            }
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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);
        }