Project files for the Acumatica Import for United Rentals
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

276 lignes
11 KiB

  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using iTextSharp;
  9. using iTextSharp.text.pdf;
  10. using iTextSharp.text.pdf.parser;
  11. using Acumatica.Default_20_200_001.Model;
  12. namespace uri_import.PClasses
  13. {
  14. public class PDFHelper
  15. {
  16. private static string _note = "<200# USE UPS 3RD PARTY ACCT# 266E8F>\n200# USE LTL ROUTING GUIDE- - - - - - - -828-485-5145\nhttp://routeshipment.com/R9322/\nBill to: United Rentals, INC\nC/O TRANSPORTATION INSIGHT\nPO BOX 23000\nHICKORY, NC 28601\nEMAIL LOADCENTER@UR.COM";
  17. private static List<string> RemoveLine(List<string> list, string contains)
  18. {
  19. string line = list.Find(x => x.ToUpper().Contains(contains.ToUpper()));
  20. if (line != null) list.Remove(line);
  21. return list;
  22. }
  23. private static List<string> RemovePhoneAndFax(List<string> list)
  24. {
  25. string line = list.Find(x => x.ToUpper().Contains("PHONE:"));
  26. if (line != null) list.Remove(line);
  27. line = list.Find(x => x.ToUpper().Contains("FAX"));
  28. if (line != null)
  29. {
  30. int index = list.FindIndex(x => x == line);
  31. Regex regex = new Regex(@"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}");
  32. string faxLine = list[index + 1];
  33. if (regex.IsMatch(faxLine))
  34. {
  35. list.Remove(line);
  36. list.Remove(faxLine);
  37. }
  38. else list.Remove(line);
  39. }
  40. return list;
  41. }
  42. private static SalesOrder CityStateZip(ref List<string> lines, SalesOrder order)
  43. {
  44. int index = 0;
  45. int numIndexes = 2;
  46. Regex rCSZ = new Regex(@"^.*(\s\s\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}).*$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
  47. foreach (string line in lines)
  48. {
  49. if (rCSZ.IsMatch(line))
  50. {
  51. DateTime dt;
  52. index = lines.FindIndex(x => x == line);
  53. if (DateTime.TryParse(lines[index - 4], out dt))
  54. {
  55. numIndexes = 3;
  56. order.ShipToAddress = new Address();
  57. order.ShipToAddress.AddressLine1 = lines[index - 3].Trim();
  58. order.ShipToAddress.AddressLine2 = string.Format("{0}\r\n{1}", lines[index - 2].Trim(), lines[index - 1].Trim());
  59. string[] csz = line.Split(',');
  60. order.ShipToAddress.City = csz[0].Trim();
  61. csz[1] = csz[1].Replace(" ", "|");
  62. string[] sz = csz[1].Trim().Split('|');
  63. order.ShipToAddress.State = sz[0].Trim();
  64. order.ShipToAddress.PostalCode = sz[1].Trim();
  65. break;
  66. }
  67. else
  68. {
  69. order.ShipToAddress = new Address();
  70. order.ShipToAddress.AddressLine1 = lines[index - 2].Trim();
  71. order.ShipToAddress.AddressLine2 = lines[index - 1].Trim();
  72. string[] csz = line.Split(',');
  73. order.ShipToAddress.City = csz[0].Trim();
  74. csz[1] = csz[1].Replace(" ", "|");
  75. string[] sz = csz[1].Trim().Split('|');
  76. order.ShipToAddress.State = sz[0].Trim();
  77. order.ShipToAddress.PostalCode = sz[1].Trim();
  78. break;
  79. }
  80. }
  81. }
  82. string pc = order.ShipToAddress.PostalCode.Value;
  83. pc = pc.Replace("-", "");
  84. int i = 0;
  85. bool result = int.TryParse(pc, out i);
  86. if (result) order.ShipToAddress.Country = "US";
  87. else order.ShipToAddress.Country = "CA";
  88. for (int r = index; r >= numIndexes; r--)
  89. {
  90. lines.RemoveAt(r);
  91. }
  92. string startLine = lines.Find(x => x.Trim().StartsWith("Qty"));
  93. if (startLine != null)
  94. {
  95. index = lines.IndexOf(startLine);
  96. for (int iIndex = index; iIndex >= 0; iIndex--)
  97. {
  98. lines.RemoveAt(iIndex);
  99. }
  100. }
  101. return order;
  102. }
  103. private static SalesOrder GetDetails(SalesOrder order, ref List<string> lines)
  104. {
  105. order.Details = new List<SalesOrderDetail>();
  106. int lrNumb = 1;
  107. for (int i = 0; i < lines.Count; i++)
  108. {
  109. string line = lines[i].Trim().Substring(0, 3).Trim();
  110. int isInt = 0;
  111. if (int.TryParse(line, out isInt))
  112. {
  113. try
  114. {
  115. lines[i] = Regex.Replace(lines[i], @"\s+", "|");
  116. string[] clmns = lines[i].Split('|');
  117. SalesOrderDetail detail = new SalesOrderDetail();
  118. detail.OrderQty = int.Parse(clmns[0].Trim());
  119. detail.InventoryID = clmns[1].Trim();
  120. detail.LineNbr = lrNumb;
  121. detail.RowNumber = lrNumb;
  122. order.Details.Add(detail);
  123. lrNumb = lrNumb + 1;
  124. }
  125. catch { }
  126. }
  127. }
  128. return order;
  129. }
  130. private static SalesOrder FindThePhoneNumber(ref List<string> list, SalesOrder order)
  131. {
  132. bool found = false;
  133. Regex regex = new Regex(@"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}");
  134. foreach (string line in list)
  135. {
  136. string[] segments = line.Split(' ');
  137. foreach (string segment in segments)
  138. {
  139. string s = segment.Trim();
  140. if (regex.IsMatch(s))
  141. {
  142. order.ShipToContact = new DocContact();
  143. order.ShipToContact.Phone1 = s;
  144. found = true;
  145. break;
  146. }
  147. }
  148. if (found)
  149. {
  150. list.Remove(line);
  151. break;
  152. }
  153. }
  154. return order;
  155. }
  156. public static SalesOrder PDFBuildSalesOrder(List<string> lines)
  157. {
  158. SalesOrder order = new SalesOrder();
  159. for (int l = 0; l < lines.Count; l++)
  160. {
  161. lines[l] = lines[l].Trim();
  162. }
  163. lines = RemoveLine(lines, "PURCHASE ORDER");
  164. lines = RemoveLine(lines, "179054");
  165. lines = RemoveLine(lines, "RPM INDUSTRIES LLC");
  166. lines = RemoveLine(lines, "Purchase Order Number");
  167. lines = RemoveLine(lines, "1660 JEFFERSON AVE");
  168. lines = RemoveLine(lines, "WASHINGTON, PA 15301");
  169. lines = RemoveLine(lines, "ORDER DATE:");
  170. lines = RemoveLine(lines, "Phone ");
  171. lines = RemoveLine(lines, "ORDERED BY:");
  172. lines = RemoveLine(lines, "LOCATION #:");
  173. lines = RemoveLine(lines, "Fax ");
  174. lines = RemoveLine(lines, "ADDRESS");
  175. lines = RemoveLine(lines, "DO NOT SHIP TO");
  176. lines = RemoveLine(lines, "STAMFORD, CT 06902");
  177. lines = RemoveLine(lines, "SEE BELOW");
  178. lines = RemoveLine(lines, "THE FOLLOWING MUST APPEAR");
  179. lines = RemoveLine(lines, "SHIPPING PAPERS");
  180. lines = RemovePhoneAndFax(lines);
  181. order.CustomerOrder = lines[0];
  182. DateTime orderDate = DateTime.Parse(lines[1].Trim());
  183. order.Date = DateTime.Parse(lines[1]);
  184. if (orderDate.Day > 15)
  185. {
  186. orderDate = orderDate.AddMonths(1);
  187. int lastDay = DateTime.DaysInMonth(orderDate.Year, orderDate.Month);
  188. orderDate = new DateTime(orderDate.Year, orderDate.Month, lastDay);
  189. if (orderDate.DayOfWeek == DayOfWeek.Sunday)
  190. orderDate = new DateTime(orderDate.Year, orderDate.Month, orderDate.Day - 2);
  191. if (orderDate.DayOfWeek == DayOfWeek.Saturday)
  192. orderDate = new DateTime(orderDate.Year, orderDate.Month, orderDate.Day - 1);
  193. }
  194. else
  195. {
  196. int lastDay = DateTime.DaysInMonth(orderDate.Year, orderDate.Month);
  197. orderDate = new DateTime(orderDate.Year, orderDate.Month, lastDay);
  198. if (orderDate.DayOfWeek == DayOfWeek.Sunday)
  199. orderDate = new DateTime(orderDate.Year, orderDate.Month, orderDate.Day - 2);
  200. if (orderDate.DayOfWeek == DayOfWeek.Saturday)
  201. orderDate = new DateTime(orderDate.Year, orderDate.Month, orderDate.Day - 1);
  202. }
  203. order.CustomerID = "000000001433";
  204. order.RequestedOn = orderDate;
  205. FindThePhoneNumber(ref lines, order);
  206. order.ShipToAddressOverride = true;
  207. order = CityStateZip(ref lines, order);
  208. order.Note = _note;
  209. order = GetDetails(order, ref lines);
  210. return order;
  211. }
  212. public static List<string> PDFLinesToText(string filePath)
  213. {
  214. List<string> list = new List<string>();
  215. using (PdfReader reader = new PdfReader(filePath))
  216. {
  217. StringBuilder text = new StringBuilder();
  218. ITextExtractionStrategy Strategy = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
  219. for (int i = 1; i <= reader.NumberOfPages; i++)
  220. {
  221. string page = "";
  222. page = PdfTextExtractor.GetTextFromPage(reader, i, Strategy);
  223. string[] lines = page.Split('\n');
  224. list.AddRange(lines);
  225. }
  226. }
  227. return list;
  228. }
  229. }
  230. public class SBTextRenderer : IRenderListener
  231. {
  232. private StringBuilder _builder;
  233. public SBTextRenderer(StringBuilder builder)
  234. {
  235. _builder = builder;
  236. }
  237. #region IRenderListener Members
  238. public void BeginTextBlock()
  239. {
  240. Console.Write("Begin Text Block");
  241. }
  242. public void EndTextBlock()
  243. {
  244. Console.Write("End Text Block");
  245. }
  246. public void RenderImage(ImageRenderInfo renderInfo)
  247. {
  248. }
  249. public void RenderText(TextRenderInfo renderInfo)
  250. {
  251. _builder.Append(renderInfo.GetText());
  252. }
  253. #endregion
  254. }
  255. }