Project files for the Acumatica Import for United Rentals
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

114 строки
4.8 KiB

  1. using System.IO;
  2. using System.Net;
  3. using System;
  4. using System.Collections.Generic;
  5. using Independentsoft.Exchange;
  6. using uri_import.FClasses;
  7. using Acumatica.Default_20_200_001.Model;
  8. using Acumatica.Manufacturing_21_200_001.Model;
  9. namespace uri_import.EClasses
  10. {
  11. public class Ex365
  12. {
  13. private NetworkCredential _credential;
  14. private Service _service;
  15. private StandardFolder _folder;
  16. public NetworkCredential Credential { get { return _credential; } set { _credential = value; } }
  17. public Service service { get { return _service; } set { _service = value; } }
  18. public StandardFolder Folder { get { return _folder; } set { _folder = value; } }
  19. public Ex365() { }
  20. public Ex365(string username, string password)
  21. {
  22. _credential = new NetworkCredential(username, password);
  23. _service = new Service("https://outlook.office365.com/ews/Exchange.asmx", _credential);
  24. }
  25. public Ex365(string username, string password, string url)
  26. {
  27. _credential = new NetworkCredential(username, password);
  28. _service = new Service(url, _credential);
  29. }
  30. public Ex365(string username, string password, string url, StandardFolder folder)
  31. {
  32. _credential = new NetworkCredential(username, password);
  33. _service = new Service(url, _credential);
  34. Folder inbox = _service.GetFolder(folder);
  35. }
  36. public List<EmailAttachment> GetPOs(string filter, bool hasAttachment = true)
  37. {
  38. int index = 1;
  39. List<EmailAttachment> list = new List<EmailAttachment>();
  40. IsEqualTo restriction = new IsEqualTo(MessagePropertyPath.HasAttachments, hasAttachment);
  41. Contains subFilter = new Contains(MessagePropertyPath.Subject, "United Rentals, Inc:", ContainmentMode.Prefixed, ContainmentComparison.IgnoreCase);
  42. ItemShape itemShape = new ItemShape(ShapeType.Id);
  43. FindItemResponse response = _service.FindItem(StandardFolder.Inbox, itemShape, subFilter);
  44. for (int i = 0; i < response.Items.Count; i++)
  45. {
  46. if (response.Items[i] is Message)
  47. {
  48. ItemId itemId = response.Items[i].ItemId;
  49. Message message = service.GetMessage(itemId);
  50. Console.WriteLine(message.Subject);
  51. IList<AttachmentInfo> attachmentsInfo = message.Attachments;
  52. for (int j = 0; j < attachmentsInfo.Count; j++)
  53. {
  54. Attachment attachment = _service.GetAttachment(attachmentsInfo[j].Id);
  55. if (attachment is FileAttachment && attachment.Name.ToUpper().EndsWith(".PDF"))
  56. {
  57. EmailAttachment ea = new EmailAttachment();
  58. FileAttachment fileAttachment = (FileAttachment)attachment;
  59. ea.Content = fileAttachment.Content;
  60. ea.FileName = fileAttachment.Name.ToUpper().Replace(".PDF", string.Format("{0}.PDF", index));
  61. index = index + 1;
  62. ea.Sender = message.From.EmailAddress;
  63. list.Add(ea);
  64. }
  65. }
  66. }
  67. }
  68. return list;
  69. }
  70. }
  71. public class EmailSettings
  72. {
  73. public string Username { get; set; }
  74. public string Password { get; set; }
  75. public string Url { get; set; }
  76. public StandardFolder Folder { get; set; }
  77. }
  78. public class EmailAttachment
  79. {
  80. private List<ProductionOrder> _productionOrders;
  81. private List<string> _errors;
  82. private bool _released = false;
  83. private bool _linked = false;
  84. public EmailAttachment()
  85. {
  86. _productionOrders = new List<ProductionOrder>();
  87. _errors = new List<string>();
  88. }
  89. public byte[] Content { get; set; }
  90. public string Sender { get; set; }
  91. public string FileName { get; set; }
  92. public string FilePath
  93. {
  94. get
  95. {
  96. string path = System.IO.Path.Combine(
  97. FileActions.ExecFile,
  98. "attachments",
  99. this.FileName);
  100. return path;
  101. }
  102. }
  103. public SalesOrder SalesOrder { get; set; }
  104. public string SalesOrderNumber { get; set; }
  105. public List<ProductionOrder> ProductionOrders { get { return _productionOrders; } set { _productionOrders = value; } }
  106. public bool Released { get{return _released;} set{_released = value;}}
  107. public bool Linked { get{return _linked;} set{_linked = value;}}
  108. public List<string> Errors { get { return _errors; } set { _errors = value; } }
  109. }
  110. }