String query = "INSERT INTO Pazaryeri_Odemeler (ty_id,transactionDate,barcode,transactionType,receiptId,description,debt,credit,paymentPeriod,commissionRate,orderNumber,paymentOrderId,vadeTarihi,paymentDate,sellerId,Gonderi_Ucreti,Platform_Hizmet,orderBenzersiz) " +
"VALUES (@ty_id,@transactionDate,@barcode,@transactionType,@receiptId,@description,@debt,@credit,@paymentPeriod,@commissionRate,@orderNumber,@paymentOrderId,@vadeTarihi,@paymentDate,@sellerId,@Gonderi_Ucreti,@Platform_Hizmet,@orderBenzersiz)";
SqlCommand command = new SqlCommand(query, db_baglanti);
foreach (var item in itemsFromAPI) // itemsFromAPI Trendyol API'den alınan siparişler
{
// Her bir sipariş numarası için ürünlerin işlenmesi
int index = 1;
foreach (var itemGroup in item.GroupBy(i => i.orderNumber))
{
foreach (var subItem in itemGroup)
{
command.Parameters.Clear(); // :))
command.Parameters.AddWithValue("@ty_id", subItem.id);
command.Parameters.AddWithValue("@transactionDate", new DateTime(1970, 1, 1).AddTicks(long.Parse(subItem.transactionDate.ToString().TrimEnd()) * 10000));
command.Parameters.AddWithValue("@barcode", subItem.barcode);
command.Parameters.AddWithValue("@transactionType", subItem.transactionType);
command.Parameters.AddWithValue("@receiptId", subItem.receiptId == null ? "0" : subItem.receiptId);
command.Parameters.AddWithValue("@description", subItem.description);
command.Parameters.AddWithValue("@debt", subItem.debt);
command.Parameters.AddWithValue("@credit", subItem.credit);
command.Parameters.AddWithValue("@paymentPeriod", subItem.paymentPeriod);
command.Parameters.AddWithValue("@commissionRate", subItem.commissionRate);
command.Parameters.AddWithValue("@orderNumber", subItem.orderNumber);
command.Parameters.AddWithValue("@paymentOrderId", subItem.paymentOrderId == null ? "0" : subItem.paymentOrderId);
command.Parameters.AddWithValue("@vadeTarihi", new DateTime(1970, 1, 1).AddTicks(long.Parse(subItem.paymentDate.ToString().TrimEnd()) * 10000));
command.Parameters.AddWithValue("@paymentDate", (int)odemeGunu.DayOfWeek == 0 ? odemeGunu.AddDays(1) : (int)odemeGunu.DayOfWeek == 1 ? odemeGunu.AddDays(3) : (int)odemeGunu.DayOfWeek == 2 ? odemeGunu.AddDays(2) : (int)odemeGunu.DayOfWeek == 3 ? odemeGunu.AddDays(1) : (int)odemeGunu.DayOfWeek == 4 ? odemeGunu.AddDays(4) : (int)odemeGunu.DayOfWeek == 5 ? odemeGunu.AddDays(3) : (int)odemeGunu.DayOfWeek == 6 ? odemeGunu.AddDays(2) : odemeGunu.AddDays(Math.Min(pazDif, perDif)));
command.Parameters.AddWithValue("@sellerId", subItem.sellerId);
command.Parameters.AddWithValue("@Gonderi_Ucreti", kargoucret);
command.Parameters.AddWithValue("@Platform_Hizmet", 4.19);
command.Parameters.AddWithValue("@orderBenzersiz", $"{subItem.orderNumber}-{index}");
command.ExecuteNonQuery();
index++;
}
}
}