public JsonResult AddCircleDocument2()
        {
            HttpFileCollectionBase files = Request.Files;
            var      doc         = Request.Form["doc"].ToString();
            var      ids         = Request.Form["Ids"].ToString();
            Document docObj      = JsonConvert.DeserializeObject <Document>(doc);
            var      organizeIds = JsonConvert.DeserializeObject <List <int> >(ids);

            var resp = new Response();

            List <byte[]> fileBytes = new List <byte[]>();

            int i = 0;

            foreach (string fileName in Request.Files)
            {
                if (fileName.Contains("otherFile"))
                {
                    HttpPostedFileBase file = Request.Files[fileName];
                    docObj.DocumentAttachment.ToList()[i].AttachmentBinary = ConverToBytes(file);
                    docObj.DocumentAttachment.ToList()[i].MimeCode         = ConvertContentType(Path.GetExtension(docObj.DocumentAttachment.ToList()[i].AttachmentName));
                    i++;
                }

                if (fileName.Contains("mainFile"))
                {
                    HttpPostedFileBase file = Request.Files[fileName];
                    var mainFile            = ConverToBytes(file);
                    docObj.MainAttachmentBinary = mainFile;
                    docObj.MimeCode             = ConvertContentType(Path.GetExtension(docObj.MainAttachmentName));
                }
            }

            foreach (var item in organizeIds)
            {
                docObj.ReceiverOrganizationId = item;
                resp = docService.AddCircleDocument(docObj);
                if (resp.Status)
                {
                    var postResponse = "";
                    //resp = new Response();
                    try
                    {
                        var docResponse = docService.GetDocumentWithChild(resp.ResponseObject.Id);

                        if (docResponse.Status)
                        {
                            var    processID = string.Empty;
                            var    document  = (Document)docResponse.ResponseObject;
                            byte[] pdfBytes  = document.MainAttachmentBinary;
                            string pdfBase64 = Convert.ToBase64String(pdfBytes);
                            var    receive   = docService.GetOrganizationById(document.ReceiverOrganizationId).ResponseObject;

                            RequestSendDocOut source = new RequestSendDocOut()
                            {
                                MessageID                      = document.Id.ToString(),
                                To                             = receive.Url.ToString(),
                                ID                             = document.No,
                                CorrespondenceDate             = document.Date,
                                Subject                        = document.Subject,
                                SecretCode                     = document.Secret,
                                SpeedCode                      = document.Speed,
                                SenderGivenName                = document.SenderName,
                                SenderFamilyName               = document.SenderSurname,
                                SenderDeptID                   = document.Organization.Code,
                                SenderMinistryID               = document.Organization.Code.Substring(0, 2),
                                SenderJobTitle                 = document.SenderPosition,
                                ReceiverGivenName              = document.ReceiverName,
                                ReceiverFamilyName             = document.ReceiverSurname,
                                ReceiverDeptID                 = receive.Code.ToString(),
                                ReceiverJobTitle               = document.ReceiverPosition,
                                ReceiverMinistryID             = receive.Code.ToString().Substring(0, 2),
                                Attachment                     = "-",
                                SendDate                       = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss", new CultureInfo("en-US")),
                                Description                    = document.Description,
                                MainLetterBinaryObjectMimeCode = ConvertContentType(Path.GetExtension(document.MainAttachmentName)),
                                MainLetterBinaryObject         = pdfBase64,
                                References                     = new List <Reference>(),
                                Attachments                    = new List <Attachment>(),
                            };
                            if (document.DocumentReference.Count == 0)
                            {
                                Reference reference = new Reference();
                                source.References.Add(reference);
                            }
                            else
                            {
                                foreach (var refer in document.DocumentReference)
                                {
                                    Reference reference = new Reference()
                                    {
                                        ID = refer.ReferenceBookNo,
                                        CorrespondenceDate = refer.ReferenceBookDate,
                                        Subject            = refer.ReferenceBookSubject,
                                    };

                                    source.References.Add(reference);
                                }
                            }


                            if (document.DocumentAttachment.Count == 0)
                            {
                                Attachment attachment = new Attachment();
                                source.Attachments.Add(attachment);
                            }
                            else
                            {
                                foreach (var att in document.DocumentAttachment)
                                {
                                    Attachment attachment = new Attachment()
                                    {
                                        AttachmentBinaryObject         = Convert.ToBase64String(att.AttachmentBinary),
                                        AttachmentBinaryObjectMimeCode = ConvertContentType(Path.GetExtension(att.AttachmentName)),
                                    };
                                    source.Attachments.Add(attachment);
                                }
                            }


                            var xml = XMLCreation.RequestSendDocument(source);
                            postResponse = postXMLData(document.Organization.Url, xml);

                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.LoadXml(postResponse);

                            var error = xmlDoc.GetElementsByTagName("ErrorDetail");

                            if (error.Count > 0)
                            {
                                for (int j = 0; j < error.Count; j++)
                                {
                                    resp.Status = false;

                                    ErrorDetail errorDetail = new ErrorDetail()
                                    {
                                        ErrorCode        = error[j].ChildNodes[0].InnerXml,
                                        ErrorDescription = error[j].ChildNodes[1].InnerXml,
                                    };

                                    resp.Description    = error[j].ChildNodes[1].InnerXml + Environment.NewLine;
                                    resp.ResponseObject = errorDetail;
                                }
                            }
                            else
                            {
                                var pID = xmlDoc.GetElementsByTagName("ProcessID");
                                if (pID.Count > 0)
                                {
                                    processID = pID[0].InnerXml.ToString();
                                }

                                resp = docService.UpdateDocumentStatus(resp.ResponseObject.Id, processID, "ส่งหนังสือรอตอบรับ");
                                resp.ResponseObject = null;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        resp.Status      = false;
                        resp.Description = ex.Message;
                    }
                }
                else
                {
                    return(Json(resp, JsonRequestBehavior.AllowGet));
                }
            }

            return(Json(resp, JsonRequestBehavior.AllowGet));
        }
        //ส่งหนังสือ
        public static string RequestSendDocument(RequestSendDocOut source)
        {
            XNamespace soap        = "http://schemas.xmlsoap.org/soap/envelope/";
            XNamespace wsa         = "http://www.w3.org/2005/08/addressing";
            XNamespace ram         = "urn:th:gov:egif:data:standard:ReusableAggregateBusinessInformationEntity:1";
            XNamespace rsm         = "urn:th:gov:egif:data:standard:GovernmentDocument:1.0";
            XElement   soapElement = new XElement(soap + "Envelope", new XAttribute(XNamespace.Xmlns + "SOAP-ENV", soap),
                                                  new XElement(soap + "Header",
                                                               new XElement(wsa + "MessageID", source.MessageID ?? "", new XAttribute(XNamespace.Xmlns + "wsa", wsa)),
                                                               new XElement(wsa + "To", source.To ?? "", new XAttribute(XNamespace.Xmlns + "wsa", wsa))),
                                                  new XElement(soap + "Body", new XAttribute("ID", "Body"),
                                                               new XElement("CorrespondenceLetterOutboundRequest",
                                                                            new XElement(rsm + "GovernmentDocument", new XAttribute(XNamespace.Xmlns + "ram", ram), new XAttribute(XNamespace.Xmlns + "rsm", rsm),
                                                                                         new XElement(ram + "CorrespondenceLetter", new XAttribute("ID", "Letter"))
                                                                                         ))));

            XElement[] array =
            {
                new XElement(ram + "ID",                                                              source.ID ?? ""),
                new XElement(ram + "CorrespondenceDate",                                              source.CorrespondenceDate.ToString() ?? ""),
                new XElement(ram + "Subject",                                                         source.Subject ?? ""),
                new XElement(ram + "SecretCode",                                                      source.SecretCode ?? ""),
                new XElement(ram + "SpeedCode",                                                       source.SpeedCode ?? ""),
                new XElement(ram + "SenderParty",
                             new XElement(ram + "GivenName",                                          source.SenderGivenName ?? ""),
                             new XElement(ram + "FamilyName",                                         source.SenderFamilyName ?? ""),
                             new XElement(ram + "JobTitle",                                           source.SenderJobTitle ?? ""),
                             new XElement(ram + "MinistryOrganization",
                                          new XElement(ram + "ID",                                    source.SenderMinistryID ?? "")),
                             new XElement(ram + "DepartmentOrganization",
                                          new XElement(ram + "ID",                                    source.SenderDeptID ?? ""))),
                new XElement(ram + "ReceiverParty",
                             new XElement(ram + "GivenName",                                          source.ReceiverGivenName ?? ""),
                             new XElement(ram + "FamilyName",                                         source.ReceiverFamilyName ?? ""),
                             new XElement(ram + "JobTitle",                                           source.ReceiverJobTitle ?? ""),
                             new XElement(ram + "MinistryOrganization",
                                          new XElement(ram + "ID",                                    source.ReceiverMinistryID ?? "")),
                             new XElement(ram + "DepartmentOrganization",
                                          new XElement(ram + "ID",                                    source.ReceiverDeptID ?? ""))),
                new XElement(ram + "Attachment",                                                      source.Attachment ?? ""),
                new XElement(ram + "SendDate",                                                        source.SendDate ?? ""),
                new XElement(ram + "Description",                                                     source.Description ?? ""),
                new XElement(ram + "MainLetterBinaryObject",
                             new XAttribute("mimeCode",                                               source.MainLetterBinaryObjectMimeCode ?? ""),
                             source.MainLetterBinaryObject)
            };
            soapElement.Element(soap + "Body")
            .Element("CorrespondenceLetterOutboundRequest")
            .Element(rsm + "GovernmentDocument")
            .Element(ram + "CorrespondenceLetter")
            .Add(array);

            foreach (var attach in source.Attachments)
            {
                XElement attachment = new XElement(ram + "AttachmentBinaryObject",
                                                   new XAttribute("mimeCode", attach.AttachmentBinaryObjectMimeCode ?? ""),
                                                   attach.AttachmentBinaryObject);
                soapElement.Element(soap + "Body")
                .Element("CorrespondenceLetterOutboundRequest")
                .Element(rsm + "GovernmentDocument")
                .Element(ram + "CorrespondenceLetter")
                .Element(ram + "MainLetterBinaryObject")
                .AddAfterSelf(attachment);
            }

            foreach (var reference in source.References)
            {
                XElement attachment = new XElement(ram + "ReferenceCorrespondence",
                                                   new XElement(ram + "ID", reference.ID ?? ""),
                                                   new XElement(ram + "CorrespondenceDate", reference.CorrespondenceDate ?? ""),
                                                   new XElement(ram + "Subject", reference.Subject ?? ""));


                soapElement.Element(soap + "Body")
                .Element("CorrespondenceLetterOutboundRequest")
                .Element(rsm + "GovernmentDocument")
                .Element(ram + "CorrespondenceLetter")
                .Element(ram + "ReceiverParty")
                .AddAfterSelf(attachment);
            }

            return(Util.ConvertXmlFormat(soapElement));
        }