Project files for the Acumatica Import for United Rentals
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

174 linhas
7.1 KiB

  1. using Acumatica.Auth.Api;
  2. using Acumatica.Default_20_200_001.Api;
  3. using Acumatica.Default_20_200_001.Model;
  4. using RestSharp;
  5. using RestSharp.Serializers.Json;
  6. using Newtonsoft.Json;
  7. using Acumatica.Manufacturing_21_200_001.Api;
  8. using Acumatica.Manufacturing_21_200_001.Model;
  9. using Acumatica.RESTClient.Client;
  10. using Acumatica;
  11. using uri_import.FClasses;
  12. using System;
  13. using System.Collections.Generic;
  14. using uri_import.AClasses;
  15. using System.Net;
  16. using System.Net.Http;
  17. using System.Net.Http.Headers;
  18. using System.Threading.Tasks;
  19. using System.IO;
  20. using uri_import.EClasses;
  21. namespace uri_import.AClasses
  22. {
  23. public class AcumaticaREST
  24. {
  25. public static string PutFile(RestService service, SalesOrder so, string siteURL, string entityName, string orderNbr, string fileName, byte[] file)
  26. {
  27. using (MemoryStream memory = new MemoryStream(file))
  28. {
  29. return service.PutFile(siteURL, "SalesOrder", string.Format("SO/{0}", so.OrderNbr), "PurchaseOrder.pdf", memory);
  30. }
  31. }
  32. public static Configuration Config(string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
  33. {
  34. var authApi = new AuthApi(siteURL,
  35. requestInterceptor: RequestLogger.LogRequest, responseInterceptor: RequestLogger.LogResponse);
  36. return authApi.LogIn(username, password, tenant, branch, locale);
  37. }
  38. public static void APILogout(string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
  39. {
  40. var authApi = new AuthApi(siteURL,
  41. requestInterceptor: RequestLogger.LogRequest, responseInterceptor: RequestLogger.LogResponse);
  42. if (authApi.TryLogout())
  43. {
  44. Console.WriteLine("Logged out successfully.");
  45. }
  46. else
  47. {
  48. Console.WriteLine("An error occured during logout.");
  49. }
  50. }
  51. public static void UploadPurchaseOrder(Configuration config, SalesOrder entity, string filename, byte[] content)
  52. {
  53. try
  54. {
  55. Console.WriteLine(string.Format("Attaching Purchase Order:"));
  56. var soApi = new SalesOrderApi(config);
  57. soApi.PutFile(entity.ID.ToString(), filename, content);
  58. }
  59. catch (Exception ex)
  60. {
  61. Console.WriteLine(ex.Message);
  62. }
  63. }
  64. public static void CreateSalesOrder(ref SalesOrder entity, Configuration config, string filename, byte[] content)
  65. {
  66. try
  67. {
  68. Console.WriteLine(string.Format("Creating Sales Order from PO: {0}", entity.CustomerOrder.Value));
  69. var soApi = new SalesOrderApi(config);
  70. SalesOrder savedSO = soApi.PutEntity(entity, null, null, "Details");
  71. entity.OrderNbr = savedSO.OrderNbr;
  72. entity.ID = savedSO.ID;
  73. //soApi.PutFile(entity.ID.ToString(), filename, content);
  74. }
  75. catch (Exception e)
  76. {
  77. Console.WriteLine(e.Message);
  78. }
  79. }
  80. public static void CreateProductionOrder(Configuration config, ref ProductionOrder entity, string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
  81. {
  82. try
  83. {
  84. Console.WriteLine(string.Format("Creating Production Order from Sales Order: ", entity.SOOrderNbr.Value));
  85. var poApi = new ProductionOrderApi(config);
  86. var cpoApi = new CreateProductionOrderApi(config);
  87. CreateProductionOrder cpo = new CreateProductionOrder();
  88. cpo.CreationOrderType = "RO";
  89. cpo.SOOrderType = "SO";
  90. cpo.SOOrderNbr = entity.SOOrderNbr;
  91. cpoApi.WaitActionCompletion(cpoApi.InvokeAction(new CreateProductionOrderProcessAll(cpo)));
  92. }
  93. catch (Exception e)
  94. {
  95. Console.WriteLine(e.Message);
  96. }
  97. }
  98. public static List<ProductionOrder> ReleaseAndLinkProductionOrders(Configuration config, EmailAttachment entity, string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
  99. {
  100. List<ProductionOrder> list = new List<ProductionOrder>();
  101. try
  102. {
  103. Console.WriteLine(string.Format("Retreiving Production Order from Sales Order: ", entity.SalesOrder.OrderNbr.Value));
  104. var poApi = new ProductionOrderApi(config);
  105. string filter = string.Format("SOOrderNbr eq '{0}'", entity.SalesOrder.OrderNbr.Value);
  106. list = poApi.GetList(null, filter, null, null, null, null);
  107. foreach (ProductionOrder po in list)
  108. {
  109. entity.ProductionOrders.Add(po);
  110. poApi.WaitActionCompletion(poApi.InvokeAction(new CreateLinkedProductionOrders(po)));
  111. entity.Linked = true;
  112. poApi.WaitActionCompletion(poApi.InvokeAction(new ReleaseProductionOrder(po)));
  113. entity.Released = true;
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. Console.WriteLine(e.Message);
  119. }
  120. return list;
  121. }
  122. public static void Test(Configuration config, string siteURL, string username, string password, string tenant = null, string branch = null, string locale = null)
  123. {
  124. // var authApi = new AuthApi(siteURL,
  125. // requestInterceptor: RequestLogger.LogRequest, responseInterceptor: RequestLogger.LogResponse);
  126. try
  127. {
  128. //var configuration = authApi.LogIn(username, password, tenant, branch, locale);
  129. Console.WriteLine("Reading Accounts...");
  130. var accountApi = new AccountApi(config);
  131. var accounts = accountApi.GetList(top: 5);
  132. foreach (var account in accounts)
  133. {
  134. Console.WriteLine("Account Nbr: " + account.AccountCD.Value + ";");
  135. }
  136. Console.WriteLine("Reading Sales Order by Keys...");
  137. var salesOrderApi = new SalesOrderApi(config);
  138. var order = salesOrderApi.GetByKeys(new List<string>() { "SO", "009314" }, null, null, "Details");
  139. Console.WriteLine("Order Total: " + order.OrderTotal.Value);
  140. var shipmentApi = new ShipmentApi(config);
  141. var shipment = shipmentApi.GetByKeys(new List<string>() { "002805" });
  142. Console.WriteLine("ConfirmShipment");
  143. //shipmentApi.WaitActionCompletion(shipmentApi.InvokeAction(new ConfirmShipment(shipment)));
  144. Console.WriteLine("CorrectShipment");
  145. shipmentApi.WaitActionCompletion(shipmentApi.InvokeAction(new CorrectShipment(shipment)));
  146. }
  147. catch (Exception e)
  148. {
  149. Console.WriteLine(e.Message);
  150. }
  151. }
  152. }
  153. }