mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-21 15:27:15 +00:00
Export amazon settlement report fix
This commit is contained in:
323
src/bnhtrade.Core/Data/Database/Import/ReadAmazonSettlement.cs
Normal file
323
src/bnhtrade.Core/Data/Database/Import/ReadAmazonSettlement.cs
Normal file
@@ -0,0 +1,323 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Import
|
||||
{
|
||||
public class ReadAmazonSettlement : Connection
|
||||
{
|
||||
private Dictionary<string, int> dicTablePkBySettlementId = new Dictionary<string, int>();
|
||||
private int? returnTop = null;
|
||||
private List<string> settlementIdList;
|
||||
|
||||
private bool FilterOutIsProcessed { get; set; }
|
||||
|
||||
public bool DescendingOrder { get; set; }
|
||||
|
||||
public int ReturnTop
|
||||
{
|
||||
get { return (int)returnTop; }
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{ returnTop = value; }
|
||||
else
|
||||
{ returnTop = null; }
|
||||
}
|
||||
}
|
||||
|
||||
public bool ReturnTopIsSet
|
||||
{
|
||||
get { return returnTop != null; }
|
||||
}
|
||||
|
||||
private List<string> SettlementIdList
|
||||
{
|
||||
get { return settlementIdList; }
|
||||
set
|
||||
{
|
||||
if (value.Any())
|
||||
{ settlementIdList = value; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool SettlementIdListIsSet
|
||||
{
|
||||
get { return SettlementIdList != null; }
|
||||
}
|
||||
|
||||
public ReadAmazonSettlement(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
Innit();
|
||||
}
|
||||
|
||||
private void Innit()
|
||||
{
|
||||
DescendingOrder = false;
|
||||
FilterOutIsProcessed = false;
|
||||
ReturnTop = 0;
|
||||
settlementIdList = null;
|
||||
}
|
||||
|
||||
public List<Model.Import.AmazonSettlement> AllUnprocessed()
|
||||
{
|
||||
Innit();
|
||||
FilterOutIsProcessed = true;
|
||||
return ExecuteDbQuery();
|
||||
}
|
||||
|
||||
public Model.Import.AmazonSettlement BySettlementId(string settlementId)
|
||||
{
|
||||
Innit();
|
||||
|
||||
// create settlement list
|
||||
var idList = new List<string>();
|
||||
idList.Add(settlementId);
|
||||
var settlementList = BySettlementId(idList);
|
||||
|
||||
// return answer
|
||||
if (settlementList == null || !settlementList.Any())
|
||||
{ return null; }
|
||||
else
|
||||
{ return settlementList.First(); }
|
||||
}
|
||||
|
||||
public List<Model.Import.AmazonSettlement> BySettlementId(List<string> settlementIdList)
|
||||
{
|
||||
Innit();
|
||||
|
||||
if (settlementIdList == null || !settlementIdList.Any())
|
||||
{ return null; }
|
||||
|
||||
SettlementIdList = settlementIdList;
|
||||
|
||||
return ExecuteDbQuery();
|
||||
}
|
||||
|
||||
private List<Model.Import.AmazonSettlement> ExecuteDbQuery()
|
||||
{
|
||||
// get header info
|
||||
var settlementList = ReadHeaderList();
|
||||
if (settlementList == null || !settlementList.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// add lines to header
|
||||
foreach (var item in settlementList)
|
||||
{
|
||||
if (!dicTablePkBySettlementId.ContainsKey(item.SettlementId))
|
||||
{
|
||||
throw new Exception("This shouldnt' happen!");
|
||||
}
|
||||
int tablePk = dicTablePkBySettlementId[item.SettlementId];
|
||||
|
||||
item.SettlementLineList = ReadLineList(tablePk);
|
||||
}
|
||||
|
||||
return settlementList;
|
||||
}
|
||||
|
||||
private List<Model.Import.AmazonSettlement> ReadHeaderList()
|
||||
{
|
||||
// build the sql string
|
||||
string sqlString = "SELECT ";
|
||||
|
||||
if (ReturnTopIsSet)
|
||||
{
|
||||
sqlString = sqlString + "TOP " + ReturnTop + " ";
|
||||
}
|
||||
|
||||
sqlString = sqlString + @"
|
||||
ImportAmazonSettlementReportID
|
||||
,[marketplace-name]
|
||||
,[settlement-id]
|
||||
,[settlement-start-date]
|
||||
,[settlement-end-date]
|
||||
,[deposit-date]
|
||||
,[total-amount]
|
||||
,currency
|
||||
,IsProcessed
|
||||
FROM tblImportAmazonSettlementReport
|
||||
WHERE 1 = 1";
|
||||
|
||||
if (FilterOutIsProcessed)
|
||||
{
|
||||
sqlString = sqlString + @"
|
||||
AND IsProcessed = 0";
|
||||
}
|
||||
|
||||
// build dictionary of parameter and values
|
||||
var dicSettlementIdByParameterString = new Dictionary<string, string>();
|
||||
if (SettlementIdListIsSet)
|
||||
{
|
||||
int count = 0;
|
||||
foreach (string item in SettlementIdList)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(item))
|
||||
{
|
||||
count = count + 1;
|
||||
string parameterString = "@settlementId" + count;
|
||||
dicSettlementIdByParameterString.Add(parameterString, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dicSettlementIdByParameterString.Any())
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var item in dicSettlementIdByParameterString)
|
||||
{
|
||||
count = count + 1;
|
||||
if (count == 1)
|
||||
{
|
||||
sqlString = sqlString + @"
|
||||
AND ( [settlement-id] = " + item.Key;
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlString = sqlString + @"
|
||||
OR [settlement-id] = " + item.Key;
|
||||
}
|
||||
}
|
||||
sqlString = sqlString + " )";
|
||||
}
|
||||
|
||||
sqlString = sqlString + @"
|
||||
ORDER BY [settlement-start-date] ";
|
||||
if (DescendingOrder) { sqlString = sqlString + " DESC"; }
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
if (dicSettlementIdByParameterString.Any())
|
||||
{
|
||||
foreach (var item in dicSettlementIdByParameterString)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (!reader.HasRows)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var headerList = new List<Model.Import.AmazonSettlement>();
|
||||
while (reader.Read())
|
||||
{
|
||||
var header = new Model.Import.AmazonSettlement();
|
||||
|
||||
int tablePk = reader.GetInt32(0);
|
||||
if (!reader.IsDBNull(1)) { header.MarketPlaceName = reader.GetString(1); }
|
||||
header.SettlementId = reader.GetString(2);
|
||||
header.StartDate = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
|
||||
header.EndDate = DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc);
|
||||
header.DepositDate = DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc);
|
||||
header.TotalAmount = reader.GetDecimal(6);
|
||||
header.CurrencyCode = reader.GetString(7);
|
||||
header.IsProcessed = reader.GetBoolean(8);
|
||||
|
||||
// update dictionary
|
||||
if (!dicTablePkBySettlementId.ContainsKey(header.SettlementId))
|
||||
{
|
||||
dicTablePkBySettlementId.Add(header.SettlementId, tablePk);
|
||||
}
|
||||
|
||||
// add header to list
|
||||
headerList.Add(header);
|
||||
}
|
||||
|
||||
return headerList;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Model.Import.AmazonSettlement.SettlementLine> ReadLineList(int settlementPk)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT ImportAmazonSettlementReportLineID
|
||||
,[transaction-type]
|
||||
,[order-id]
|
||||
,[merchant-order-id]
|
||||
,[adjustment-id]
|
||||
,[shipment-id]
|
||||
,[marketplace-name]
|
||||
,[amount-type]
|
||||
,[amount-description]
|
||||
,amount
|
||||
,currency
|
||||
,[fulfillment-id]
|
||||
,[posted-date-time]
|
||||
,[order-item-code]
|
||||
,[merchant-order-item-id]
|
||||
,[merchant-adjustment-item-id]
|
||||
,sku
|
||||
,[quantity-purchased]
|
||||
,[promotion-id]
|
||||
,IsProcessed
|
||||
,ExportAccountInvoiceLineID
|
||||
FROM tblImportAmazonSettlementReportLine
|
||||
WHERE ImportAmazonSettlementReportID = @settlementPk
|
||||
ORDER BY [posted-date-time]
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@settlementPk", settlementPk);
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (!reader.HasRows)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var lineList = new List<Model.Import.AmazonSettlement.SettlementLine>();
|
||||
while (reader.Read())
|
||||
{
|
||||
var line = new Model.Import.AmazonSettlement.SettlementLine();
|
||||
|
||||
int tablePk = reader.GetInt32(0);
|
||||
line.TransactionType = reader.GetString(1);
|
||||
if (!reader.IsDBNull(2)) { line.OrderId = reader.GetString(2); }
|
||||
if (!reader.IsDBNull(3)) { line.MerchantOrderId = reader.GetString(3); }
|
||||
if (!reader.IsDBNull(4)) { line.AdjustmentId = reader.GetString(4); }
|
||||
if (!reader.IsDBNull(5)) { line.ShipmentId = reader.GetString(5); }
|
||||
if (!reader.IsDBNull(6)) { line.MarketPlaceName = reader.GetString(6); }
|
||||
line.AmountType = reader.GetString(7);
|
||||
line.AmountDescription = reader.GetString(8);
|
||||
line.Amount = reader.GetDecimal(9);
|
||||
line.CurrenyCode = reader.GetString(10);
|
||||
if (!reader.IsDBNull(11)) { line.FulfillmentId = reader.GetString(11); }
|
||||
line.PostDateTime = DateTime.SpecifyKind(reader.GetDateTime(12), DateTimeKind.Utc);
|
||||
if (!reader.IsDBNull(13)) { line.OrderItemCode = reader.GetString(13); }
|
||||
if (!reader.IsDBNull(14)) { line.MerchantOrderItemId = reader.GetString(14); }
|
||||
if (!reader.IsDBNull(15)) { line.MerchantAdjustmentItemId = reader.GetString(15); }
|
||||
if (!reader.IsDBNull(16)) { line.Sku = reader.GetString(16); }
|
||||
if (!reader.IsDBNull(17)) { line.QuantityPurchased = reader.GetInt32(17); }
|
||||
if (!reader.IsDBNull(18)) { line.PromotionId = reader.GetString(18); }
|
||||
line.IsProcessed = reader.GetBoolean(19);
|
||||
if (!reader.IsDBNull(20)) { int exportAccountInvoiceLineId = reader.GetInt32(20); }
|
||||
|
||||
lineList.Add(line);
|
||||
}
|
||||
return lineList;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Import
|
||||
{
|
||||
public class UpdateAmazonSettlement : Connection
|
||||
{
|
||||
public UpdateAmazonSettlement(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void SetIsProcessedTrue(List<string> settlementIdList)
|
||||
{
|
||||
if (settlementIdList == null || !settlementIdList.Any())
|
||||
{
|
||||
throw new Exception("Settlement ID list is empty.");
|
||||
}
|
||||
|
||||
string sqlString = @"
|
||||
UPDATE tblImportAmazonSettlementReport
|
||||
SET IsProcessed = 1
|
||||
WHERE (1=0)";
|
||||
|
||||
for (int i = 0; i < settlementIdList.Count(); i++)
|
||||
{
|
||||
sqlString += @"
|
||||
OR ([settlement-id] = @settlementId" + i + ")";
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
for (int i = 0; i < settlementIdList.Count(); i++)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@settlementId" + i, settlementIdList[i]);
|
||||
}
|
||||
|
||||
if (cmd.ExecuteNonQuery() == 0)
|
||||
{
|
||||
throw new Exception("Something went wrong updating settlement status.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user