Advertisement
bluebunny72

Duty Drawback

Sep 21st, 2023 (edited)
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.18 KB | None | 0 0
  1. Stopwatch sw = new Stopwatch();
  2. sw.Start();
  3.  
  4. DateTime startDate = new DateTime(2023, 4, 1);
  5. DateTime endDate = new DateTime(2023, 7, 1); //not inclusive
  6.  
  7. string? RM_Lot = null;
  8.  
  9. List<ExcelLine> elList;
  10.  
  11. using SyteLineHelper h = new();
  12.  
  13. //GROUP A
  14. List<LotList> lotLists = h.GetLotList(startDate, endDate, RM_Lot, config);
  15.  
  16. foreach (LotList ll in lotLists.OrderBy(x => x.RM_TransDate).ThenBy(x => x.RM_Lot))
  17. {
  18.     elList = new();
  19.  
  20.     Console.WriteLine("***********BEGIN LOT************");
  21.     Console.WriteLine(string.Concat("RM Lot: ", ll.RM_Lot, ", RM Item: ", ll.RM_Item));
  22.  
  23.     row += 1;
  24.     int rm_lot_row = row;
  25.  
  26.     ws.Cell(row, "A").Value = ll.RM_Lot;
  27.     ws.Cell(row, "B").Value = ll.RM_TransDate.ToString("yyyy-MM-dd");
  28.     ws.Cell(row, "C").Value = ll.RM_Item;
  29.     ws.Cell(row, "D").Value = ll.RM_ItemDesc;
  30.     ws.Cell(row, "E").Value = ll.RM_CommodityCode;
  31.    
  32.     string tracker = ll.RM_Lot;
  33.     int level = 0;
  34.  
  35.     int oldRow = row;
  36.     Guid id = Guid.NewGuid();
  37.     Recurse(ll.RM_Lot, ll.RM_Lot, tracker, level, id);
  38.     row = oldRow;
  39.  
  40.     WriteExcelLinesPass1(ll.RM_USD_FXRate);
  41.     WriteExcelLinesPass2();
  42.     WriteExcelLinesPass3();
  43.  
  44.     Console.WriteLine("***********END LOT************");
  45.     Console.WriteLine();
  46.  
  47. }
  48.  
  49. //ws.Columns().AdjustToContents();
  50.  
  51. // Freeze rows and columns in one shot
  52. ws.SheetView.Freeze(1, 1);
  53. ws.SheetView.ZoomScale = 85;
  54.  
  55. var filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.xlsx");
  56.  
  57. sw.Stop();
  58. TimeSpan timeTaken = sw.Elapsed;
  59. string foo = "Time taken: " + timeTaken.ToString(@"hh\:mm\:ss\.fff");
  60. Console.WriteLine(foo);
  61.  
  62. workbook.SaveAs(filePath);
  63.  
  64. ProcessStartInfo psInfo = new ProcessStartInfo
  65. {
  66.     FileName = filePath,
  67.     UseShellExecute = true
  68. };
  69. Process.Start(psInfo);
  70. Console.ReadLine();
  71.  
  72. void ScrapSection(string lot, int row, IConfiguration config)
  73. {
  74.     int scrap_row = row;
  75.     List<ScrapList> scrapLists = h.GetScrapList(startDate, endDate, lot, config);
  76.     foreach (ScrapList scrap in scrapLists)
  77.     {
  78.         ws.Cell(scrap_row, "AP").Value = scrap.Scrap_Qty; //Qty Scrapped/Written Off
  79.         ws.Cell(scrap_row, "AQ").Value = scrap.Scrap_Amt; //Amt Scrapped/Written Off
  80.         ws.Cell(scrap_row, "AR").Value = scrap.Item; //FG Qtm Item
  81.         ws.Cell(scrap_row, "AS").Value = scrap.Lot; //FG Qty Lot
  82.         ws.Cell(scrap_row, "AT").Value = scrap.Ref_Item; //Qty Ref Item
  83.  
  84.         scrap_row +=1;
  85.     }
  86. }
  87.  
  88. string Recurse(string RM_Lot, string lot, string tracker, int level, Guid parentId)
  89. {
  90.     List<MatlTranList> mtList = h.GetMatlTranByLotList(startDate, endDate, lot, config);
  91.     foreach (MatlTranList mt in mtList.OrderBy(x => x.CategorySort))
  92.     {
  93.         Console.WriteLine(String.Concat("Lot: ", RM_Lot, " Recurse Level: ", level));
  94.  
  95.         ExcelLine el = new();
  96.  
  97.         //row +=1;
  98.         el.Id = Guid.NewGuid();
  99.         el.ParentId = parentId;
  100.         el.Row = 0;
  101.         el.RMLot = RM_Lot;
  102.         el.Level = level;
  103.  
  104.         switch (mt.Category)
  105.         {
  106.             case "Ship":
  107.                 el.CategoryType = Enums.CategoryType.Ship;
  108.                 break;
  109.             case "Production":
  110.                 el.CategoryType = Enums.CategoryType.Production;
  111.                 break;
  112.             case "External Receipts":
  113.                 el.CategoryType = Enums.CategoryType.ExternalReceipts;
  114.                 break;
  115.             case "Internal Receipts":
  116.                 el.CategoryType = Enums.CategoryType.InternalReceipts;
  117.                 break;
  118.             case "Scrap":
  119.                 el.CategoryType = Enums.CategoryType.Scrap;
  120.                 break;
  121.             default:
  122.                 el.CategoryType = Enums.CategoryType.Other;
  123.                 break;
  124.         }
  125.  
  126.         el.CategoryDesc = mt.Category;
  127.         el.CategorySort = mt.CategorySort;
  128.         el.Lot = mt.Lot;
  129.         el.RefNum = mt.RefNum;
  130.         el.RefLineSuf = mt.RefLineSuf;
  131.         el.PackNumList = mt.PackNumList;
  132.         el.Tracker = tracker;
  133.         el.Item = mt.Item;
  134.         el.UM = mt.UM;
  135.         el.RMQty = 0;
  136.         el.RMAmt = 0;
  137.         el.QtyFactor = 0;
  138.         el.QtyFactor = 0;
  139.         el.QtyConsumed = mt.QtyConsumed;
  140.         el.AmtConsumed = mt.AmtConsumed;
  141.  
  142.         if (mt.Category==null)
  143.         {
  144.             throw new ApplicationException("No Category!");
  145.         }
  146.         else if (el.CategoryType == Enums.CategoryType.Production)
  147.         {
  148.  
  149.             string refNum = "000" + mt.RefLineSuf;
  150.             string refLot = string.Concat(mt.RefNum, "-", refNum.Right(4));
  151.  
  152.             Recurse(RM_Lot, refLot, tracker + "|" + refLot, level + 1, el.Id);
  153.  
  154.         }
  155.  
  156.         elList.Add(el);
  157.     }
  158.  
  159.     return tracker;
  160. }
  161.  
  162. void WriteExcelLinesPass1(decimal fxRate)
  163. {
  164.     foreach (ExcelLine el in elList
  165.         .OrderBy(x => x.CategorySort)
  166.         .ThenBy(x => x.CategoryType)
  167.         .ThenBy(x => x.Level)
  168.         .ThenBy(x => x.Tracker))
  169.     {
  170.         Console.WriteLine(String.Concat("Pass: 1 Row: ", row, " Level: ", el.Level));
  171.  
  172.         el.Row = row;
  173.  
  174.         ws.Cell(row, "A").Value = el.RMLot;
  175.         ws.Cell(row, "G").Value = el.Level;
  176.         ws.Cell(row, "H").Value = el.CategoryDesc;
  177.         ws.Cell(row, "I").Value = el.Lot;
  178.         ws.Cell(row, "J").Value = el.RefNum;
  179.         ws.Cell(row, "K").Value = el.RefLineSuf;
  180.         ws.Cell(row, "L").Value = el.Tracker;
  181.         ws.Cell(row, "M").Value = el.Item;
  182.         ws.Cell(row, "N").Value = el.UM;
  183.         ws.Cell(row, COL_RMQty).FormulaA1 = string.Concat(COL_QtyConsumed, row);
  184.         ws.Cell(row, COL_RMAmt).FormulaA1 = string.Concat(COL_AmtConsumed, row);
  185.         ws.Cell(row, COL_QtyFactor).Value = 1; //Qty Factor
  186.         ws.Cell(row, COL_AmtFactor).Value = 1; //Amt Factor
  187.         ws.Cell(row, COL_QtyConsumed).Value = el.QtyConsumed;
  188.         ws.Cell(row, COL_AmtConsumed).Value = el.AmtConsumed;
  189.         ws.Cell(row, COL_USDFXRate).Value = fxRate;
  190.         ws.Cell(row, COL_PackNumList).Value = el.PackNumList;              
  191.  
  192.         if (el.CategoryType == Enums.CategoryType.Ship)
  193.         {
  194.             FGShippedList fgs = h.GetFGShippedListByCustomerOrderLine(startDate, endDate, el.RefNum, el.RefLineSuf, el.Lot, config).FirstOrDefault();
  195.  
  196.             ws.Cell(row, "A").Value = el.RMLot; //A
  197.  
  198.             ws.Cell(row, "V").Value = fgs.Customer; //Customer
  199.             ws.Cell(row, "W").Value = fgs.ShipTo; //CustomerShipTo
  200.             ws.Cell(row, "X").Value = fgs.Country; //Country
  201.             ws.Cell(row, "Y").Value = fgs.Is_EU ? "Y" : "N"; //EU
  202.             ws.Cell(row, "Z").Value = fgs.FG_CommCode; //FG_CommCode
  203.             ws.Cell(row, "AA").Value = fgs.Is_ExportCommCode ? "Y" : "N"; //ValidExportCommCode
  204.             ws.Cell(row, "AB").Value = fgs.Shipper; //Shipper
  205.             ws.Cell(row, "AC").Value = fgs.Ship_Date.ToString("yyyy-MM-dd"); //Shipper
  206.             ws.Cell(row, "AC").Value = fgs.Ship_Date.ToString("yyyy-MM-dd"); //Shipper    
  207.             ws.Cell(row, COL_ShippedLotQty).Value = fgs.ShippedLotQty; // 42.12345;
  208.             ws.Cell(row, COL_COLineUnitPrice).Value = fgs.COLineUnitPrice; //42.1234;
  209.             ws.Cell(row, COL_COCurrCode).Value = fgs.COCurrCode; //"USD";
  210.             ws.Cell(row, COL_COFXRate).Value = fgs.COFXRate;
  211.         }
  212.         row += 1;
  213.     }
  214. }
  215.  
  216. void WriteExcelLinesPass2()
  217. {
  218.     foreach (ExcelLine el in elList
  219.         .Where(x => x.CategoryType != Enums.CategoryType.InternalReceipts)
  220.         .OrderBy(x => x.Row))
  221.     {
  222.         Console.WriteLine(String.Concat("Pass: 2 Row: ", el.Row, " Level: ", el.Level));
  223.  
  224.         //For every tran type other than Finish,
  225.         //use Finish tran type qty and amount factor to determine RM qty and amount
  226.         int? finishRow = GetFinishRowNumber(el.ParentId); // ???
  227.  
  228.         if (finishRow != null)
  229.         {
  230.             ws.Cell(el.Row, COL_RMQty).FormulaA1 = String.Concat(COL_QtyConsumed, el.Row, "/", "$", COL_QtyFactor, "$", finishRow);
  231.             ws.Cell(el.Row, COL_RMAmt).FormulaA1 = String.Concat(COL_AmtConsumed, el.Row, "/", "$", COL_AmtFactor, "$", finishRow);
  232.         }
  233.         else
  234.         {
  235.             ws.Cell(el.Row, COL_RMQty).FormulaA1 = String.Concat(COL_QtyConsumed, el.Row);
  236.             ws.Cell(el.Row, COL_RMAmt).FormulaA1 = String.Concat(COL_AmtConsumed, el.Row);
  237.         }
  238.     }
  239. }
  240.  
  241. void WriteExcelLinesPass3()
  242. {
  243.     foreach (ExcelLine el in elList
  244.         .Where(x => x.CategoryType == Enums.CategoryType.InternalReceipts)
  245.         .OrderBy(x => x.Row))
  246.     {
  247.         Console.WriteLine(String.Concat("Pass: 3 Row: ", el.Row, " Level: ", el.Level));
  248.  
  249.         //For every "Finish" Tran Type, read RM qty and RM amount based on the consumption of predecessor lot consumption factor.
  250.         //Then determine the Qty and Amount Factor
  251.  
  252.         GetPredessorLotConsumption2(el.ParentId, el.RefNum, el.RefLineSuf);
  253.  
  254.         ws.Cell(el.Row, COL_QtyFactor).FormulaA1 = String.Concat(COL_QtyConsumed, el.Row, "/", COL_RMQty, el.Row);
  255.         ws.Cell(el.Row, COL_AmtFactor).FormulaA1 = String.Concat(COL_AmtConsumed, el.Row, "/", COL_RMAmt, el.Row);
  256.  
  257.     }
  258. }
  259.  
  260. int? GetFinishRowNumber(Guid parentId)
  261. {
  262.     int? row = null;
  263.  
  264.     ExcelLine el = elList.Where(x => x.ParentId == parentId
  265.                                         && x.CategoryType == Enums.CategoryType.InternalReceipts).FirstOrDefault();
  266.  
  267.     if (el != null)
  268.     {
  269.         row = el.Row;
  270.     }
  271.     return row;
  272. }
  273.  
  274. void GetPredessorLotConsumption2(Guid parentId, string refNumber, int RefLineSuf)
  275. {
  276.     int? finishRow = GetFinishRowNumber(parentId);
  277.     string s1 = string.Empty;
  278.     string s2 = string.Empty;
  279.  
  280.     foreach (ExcelLine el in elList.Where(x => x.Id == parentId &&
  281.                                      x.RefNum == refNumber &&
  282.                                      x.RefLineSuf == RefLineSuf &&
  283.                                      x.CategoryType == Enums.CategoryType.Production))
  284.     {
  285.         s1 += string.Concat(COL_RMQty, el.Row, ",");
  286.         s2 += string.Concat(COL_RMAmt, el.Row, ",");
  287.     }
  288.  
  289.     if (finishRow != null)
  290.     {
  291.         ws.Cell(finishRow.Value, COL_RMQty).FormulaA1 = string.Concat("-1*SUM(", s1.TrimEnd(','), ")");
  292.         ws.Cell(finishRow.Value, COL_RMAmt).FormulaA1 = string.Concat("-1*SUM(", s2.TrimEnd(','), ")");
  293.     }
  294. }
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement