Exemplo n.º 1
0
        // Sending Orders of the customer to respective suppliers
        public bool SendCustomerOrders(int dayTotalHours, string subjectLine, int dayTotalMinutes, Boolean isHourBased, string orderFromEmail, string supportEmail)
        {
            //ResendEmail();
            // Getting
            List <Supplier> supplierList = _supplierService.GetSuppliersForSendingOrder();

            if (supplierList == null || supplierList.Count <= 0)
            {
                return(false);
            }

            foreach (Supplier supplier in supplierList)
            {
                if (supplier.SupplierOrderPolicies == null || supplier.SupplierOrderPolicies.Count <= 0)
                {
                    continue;
                }
                Boolean sendOrder = true;

                if (sendOrder != true)
                {
                    continue;
                }

                List <Customer_Order> customerOrderList = _customerOrderService.GetNewOrders(supplier.SupplierId);
                Logger.Info($"Pending Orders For {supplier.Name} = {customerOrderList.Count}");
                foreach (Customer_Order customerOrder in customerOrderList)
                {
                    try
                    {
                        if (!supplier.SupplierOrderPolicies.ToList()[0].SendOrderViaEmail || (string.IsNullOrEmpty(supplier.SupplierOrderPolicies.ToList()[0].OrderEmailAddress)))
                        {
                            continue;
                        }

                        var role = customerOrder.Customer.UserRolesInCustomers.FirstOrDefault(i => i.AspNetUserId == customerOrder.CreatedByUserId) ??
                                   customerOrder.Customer.UserRolesInCustomers.FirstOrDefault(i => i.AspNetRole.Name == "Admin");

                        var user = role?.AspNetUser;
                        if (user == null)
                        {
                            continue;
                        }

                        string[] orderSubjctline = subjectLine.Split(',');
                        var      emailSubject    = user.LanguageCode == LanguageCode.English ? orderSubjctline[0] : orderSubjctline[1];

                        Boolean supplierEmployeeLimitForOrder = true;
                        if (customerOrder.Customer.NoOfEmployee < supplier.SupplierOrderPolicies.ToList()[0].MinEmployeeLimitForOrder)
                        {
                            supplierEmployeeLimitForOrder = false;

                            if (string.IsNullOrEmpty(supplier.InternalSupportEmailForSpecialSituations))
                            {
                                continue;
                            }
                        }

                        var response = SaveCaseStatus(customerOrder.ForSupplierId, customerOrder.FromCustomerId, customerOrder.FromDepartmentId, user.Id, supplierEmployeeLimitForOrder, customerOrder.Id);
                        if (response != null && response.IsSuccess)
                        {
                            var emailHtmlBody = "";
                            if (customerOrder.IsJson == null || customerOrder.IsJson == false)
                            {
                                Logger.Info($"Orders# {customerOrder.Id} = is XML");
                                XmlDocument xmlDoc = new XmlDocument();
                                xmlDoc.LoadXml(customerOrder.OrderXML);

                                //XmlNode XmlOrder = xmlDoc.SelectSingleNode("//Order");
                                XmlNode xmlOrderDetail = xmlDoc.SelectSingleNode("//OrderDetail");


                                XmlElement fieldNode = xmlDoc.CreateElement("Field");
                                xmlOrderDetail.AppendChild(fieldNode);

                                //Creating Child Node of the Key
                                XmlElement childKey = xmlDoc.CreateElement("Key");
                                //childKey.Attributes.Append(xmlDoc.CreateAttribute("name", "trackTid"));
                                childKey.InnerText = "CaseId";

                                fieldNode.AppendChild(childKey);

                                //Creating Child Node of the Value
                                XmlElement childValue = xmlDoc.CreateElement("Value");
                                childValue.InnerText = response.CaseId.ToString();

                                fieldNode.AppendChild(childValue);

                                customerOrder.OrderXML = xmlDoc.OuterXml;
                                emailHtmlBody          = new XSLTransformHelper().CreateHTML(customerOrder.OrderXML, user.LanguageCode);
                            }
                            else
                            {
                                Logger.Info($"Orders# {customerOrder.Id} = is XML");
                                emailHtmlBody = _formService.ConvertToHTML(customerOrder.OrderXML, response.CaseId, customerOrder.Id).Message;
                            }

                            Logger.Info("Orders# {0} HTML = " + emailHtmlBody, customerOrder.Id);

                            if (!string.IsNullOrEmpty(emailHtmlBody))
                            {
                                HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                                doc.LoadHtml(emailHtmlBody);

                                var tbl   = doc.GetElementbyId("tbl");
                                var nodes = tbl.SelectNodes("tr/td");
                                foreach (var node in nodes)
                                {
                                    if (node.InnerText.Contains(response.CaseId.ToString()))
                                    {
                                        node.Attributes.Append("name", "trackTid");
                                    }
                                }

                                emailHtmlBody = doc.DocumentNode.InnerHtml;
                            }

                            // department email, and support email.
                            orderFromEmail = customerOrder.OrderEmail.Equals(supportEmail)
                                ? orderFromEmail
                                : customerOrder.Department.InternalEmail;
                            var attachments = GetEmailAttachment(customerOrder);
                            Logger.Info(String.Format("Orders# {0} has attachments = " + attachments?.Count,
                                                      customerOrder.Id));
                            if (supplierEmployeeLimitForOrder)
                            {
                                string recipientEmail = (string.IsNullOrEmpty(customerOrder.OrderEmail)
                                    ? supplier.SupplierOrderPolicies.ToList()[0].OrderEmailAddress
                                    : customerOrder.OrderEmail);
                                ////get email attachments

                                MailMessageResponse message = SaveMailMessage(user.Id, supplier.Name, emailSubject,
                                                                              emailHtmlBody, user.InternalEmail, recipientEmail, response.CaseId,
                                                                              customerOrder.FromCustomerId, customerOrder.FromDepartmentId);
                                if (customerOrder.CustomerOrderAttachments.Count > 0)
                                {
                                    SaveCustomerAttachmentsInMailMessage(customerOrder.CustomerOrderAttachments,
                                                                         message.MessageId);
                                }

                                SendEmail(user, emailHtmlBody, recipientEmail,
                                          customerOrder.Customer.ChannelId.ToString(), orderFromEmail, user.LanguageCode,
                                          supplier.Name, customerOrder.Customer.CustomerName, attachments);

                                var html = _formService.GetOrderQuestionsHtml(customerOrder.Id);

                                SendOrderToPrimaryEmail(user, emailSubject, html, response.CaseId,
                                                        supplier.Name, attachments, customerOrder.Customer.ChannelId.ToString(),
                                                        customerOrder.FromCustomerId, customerOrder.FromDepartmentId,
                                                        customerOrder.CustomerOrderAttachments);
                            }
                            else
                            {
                                MailMessageResponse message = SaveMailMessage(user.Id, supplier.Name, emailSubject,
                                                                              emailHtmlBody, user.InternalEmail,
                                                                              supplier.InternalSupportEmailForSpecialSituations, response.CaseId,
                                                                              customerOrder.FromCustomerId, customerOrder.FromDepartmentId);
                                if (customerOrder.CustomerOrderAttachments.Count > 0)
                                {
                                    SaveCustomerAttachmentsInMailMessage(customerOrder.CustomerOrderAttachments,
                                                                         message.MessageId);
                                }

                                // Sending Email to Supplier
                                SendEmail(user, emailHtmlBody, supplier.InternalSupportEmailForSpecialSituations,
                                          customerOrder.Customer.ChannelId.ToString(), orderFromEmail, user.LanguageCode,
                                          supplier.Name, customerOrder.Customer.CustomerName, attachments);

                                var html = _formService.GetOrderQuestionsHtml(customerOrder.Id);

                                SendOrderToPrimaryEmail(user, emailSubject, html, response.CaseId,
                                                        supplier.Name, attachments, customerOrder.Customer.ChannelId.ToString(),
                                                        customerOrder.FromCustomerId, customerOrder.FromDepartmentId,
                                                        customerOrder.CustomerOrderAttachments);
                            }

                            customerOrder.Status          = OrderStatus.Sent;
                            customerOrder.SentDateTimeUtc = DateTime.UtcNow;

                            _customerOrderService.SaveCustomerOrder(customerOrder);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Fatal(ex, $"Orders #{customerOrder.Id} has exception");
                    }
                }
            }

            return(true);
        }