diff --git a/BroseCumulativeReport/App.cs b/BroseCumulativeReport/App.cs index 12599e5..52f74e5 100644 --- a/BroseCumulativeReport/App.cs +++ b/BroseCumulativeReport/App.cs @@ -26,7 +26,13 @@ public class App( IncludedConfiguration includedConfig = configuration.GetSection("Included").Get() ?? new IncludedConfiguration(); + + DateTime closestSendingDate = GetClosestSendingDate(DateTime.Now); + // scheduleOrdersByPoNumber = scheduleOrdersByPoNumber + // .Where(x => !string.IsNullOrWhiteSpace(x.Key) && x.Key == "5500278577") + // .ToDictionary(x => x.Key, x => x.Value); + foreach (KeyValuePair scheduleOrderByPoNumber in scheduleOrdersByPoNumber) { if (scheduleOrderByPoNumber.Value == null) continue; @@ -85,7 +91,8 @@ public class App( out List? materialTransactions); IDictionary> itemsToSendByDate = GetItemsToSendByDate(scheduleOrder, - scheduleOrder.ScheduleOrderDetails.First(), scheduleOrderDetailDetails, materialTransactions); + scheduleOrder.ScheduleOrderDetails.First(), scheduleOrderDetailDetails, materialTransactions, + closestSendingDate); orderNumbersByWz.TryGetValue(wzByPoNumber.Value.WzNumber, out string? orderNumber); @@ -200,23 +207,24 @@ public class App( private IDictionary> GetItemsToSendByDate(ScheduleOrderDto scheduleOrder, ScheduleOrderDetailDto scheduleOrderDetail, IList scheduleOrderDetailDetails, - List? materialTransactions) + List? materialTransactions, DateTime closestSendingDate) { IDictionary> itemsToSendByDate = new Dictionary>(); + Item materialTransactionItem = new Item(scheduleOrder.Recipient.RecipientDesc, + scheduleOrderDetail.Sc_productCode, scheduleOrderDetail.Sh_productCode, + materialTransactions?.Sum(x => (int?)x.Qty ?? 0) ?? 0, "MAL_TRAN"); + + itemsToSendByDate.Add(closestSendingDate, new List { materialTransactionItem }); + foreach (ScheduleOrderDetailDetailDto scheduleOrderDetailDetail in scheduleOrderDetailDetails) { if (scheduleOrderDetailDetail.Qty <= 0) continue; - DateTime nextSendDate = GetNextSendDate(scheduleOrderDetailDetail); + DateTime nextSendDate = GetNextSendDate(scheduleOrderDetailDetail, closestSendingDate); + int quantity = scheduleOrderDetailDetail.Qty; - if (scheduleOrderDetailDetail.QtyType == "83") - { - nextSendDate = nextSendDate.AddDays(7); - quantity += materialTransactions?.Sum(x => (int?)x.Qty ?? 0) ?? 0; - } - Item item = new Item(scheduleOrder.Recipient.RecipientDesc, scheduleOrderDetail.Sc_productCode, scheduleOrderDetail.Sh_productCode, quantity, scheduleOrderDetailDetail.QtyType); @@ -233,7 +241,7 @@ public class App( return itemsToSendByDate; } - private DateTime GetNextSendDate(ScheduleOrderDetailDetailDto scheduleOrderDetailDetail) + private DateTime GetNextSendDate(ScheduleOrderDetailDetailDto scheduleOrderDetailDetail, DateTime closestSendingDate) { DateTime ediDate = scheduleOrderDetailDetail.DateFrom; @@ -244,6 +252,17 @@ public class App( if (sendingDate.DayOfWeek < DayOfWeek.Thursday) sendingDate = sendingDate.AddDays(-7); - return sendingDate; + return sendingDate <= closestSendingDate ? closestSendingDate : sendingDate; + } + + public static DateTime GetClosestSendingDate(DateTime date) + { + int daysUntilThursday = ((int)DayOfWeek.Thursday - (int)date.DayOfWeek + 7) % 7; + if (daysUntilThursday == 0) + { + daysUntilThursday = 7; // Jeśli dzisiaj jest czwartek, bierzemy następny + } + + return date.AddDays(daysUntilThursday); } } \ No newline at end of file diff --git a/BroseCumulativeReport/ExcelGenerator.cs b/BroseCumulativeReport/ExcelGenerator.cs index e0a27a2..bbd4144 100644 --- a/BroseCumulativeReport/ExcelGenerator.cs +++ b/BroseCumulativeReport/ExcelGenerator.cs @@ -111,7 +111,9 @@ public class ExcelGenerator(IConfiguration configuration) if (index < 0) continue; letter = GetColumnLetter(index); - worksheet.Range[$"{letter}{currentRow}"].Text = sendDateWithQuantity.Quantity.ToString(); + worksheet.Range[$"{letter}{currentRow}"].Text = sendDateWithQuantity.Quantity <= 0 + ? null + : sendDateWithQuantity.Quantity.ToString(); worksheet.Range[$"{letter}{currentRow}"].HorizontalAlignment = ExcelHAlign.HAlignRight; worksheet.Range[$"{letter}{currentRow}"].NumberFormat = "0"; }