<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_><+ve>");
adjCodes.Add("Q", "<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_><-ve>");
adjCodes.Add("U", "<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_><-ve>");
adjCodes.Add("X", "<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_><-ve>");
try
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT * FROM tblImportFbaInventoryAdjustmentReport WHERE IsProcessed=0 " +
"ORDER BY [adjusted-date], ImportFbaInventoryAdjustmentReportID DESC;"
, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
int record = 0;
int index01 = reader.GetOrdinal("ImportFbaInventoryAdjustmentReportID");
int index02 = reader.GetOrdinal("adjusted-date");
int index03 = reader.GetOrdinal("sku");
int index04 = reader.GetOrdinal("quantity");
int index05 = reader.GetOrdinal("transaction-item-id");
int index06 = reader.GetOrdinal("reason");
int index07 = reader.GetOrdinal("disposition");
int index08 = reader.GetOrdinal("fnsku");
int index09 = reader.GetOrdinal("fulfillment-center-id");
while (reader.Read())
{
int importTableId = reader.GetInt32(index01);
record = record + 1;
bool addTransaction = true;
Console.Write("\rProcessing record: " + record);
string matchString = "";
string reasonCode = reader.GetString(index06);
string disposition = reader.GetString(index07);
if (adjCodes.ContainsKey(reasonCode))
{
matchString = adjCodes[reasonCode] + "<" + disposition + ">";
}
else if (reasonCode.Length > 0)
{
matchString = "<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_><" + reasonCode + "><" + disposition + ">";
}
else
{
MiscFunction.EventLogInsert(
"Aborted TransposeFbaAdustmentReport(), no reason code was found in import table/file. Check imput data.",
1);
return;
}
int transactionTypeId = Stock.StockJournal.StockTransactionTypeIdSelect(sqlConnectionString, matchString);
/* GetStockTransactionTypeId return meanings
* >0 use the as the TypeId when inserting transaction
* 0 By default, new unreviewed type will return 0 to skip transpose.
* if enabled, new unreviewed type will return id, data will be transposed to table, reconciliation will pickup new items
* -1 Type import/transpose is disabled, IsProcessed=TRUE StockTransactionID=NULL */
if (transactionTypeId == 0)
{
transactionTypeId = Stock.StockJournal.StockTransactionTypeIdInsert(sqlConnectionString, stockJournalTypeId, matchString);
}
// not used any more, stock reconciliation will pickup new types
//if (transactionTypeId == 0)
//{
// string errorMessage =
// "New transaction-type created/detected while running TransposeFbaAdustmentReport. Stock transaction table incomplete, update transaction-type table and run import/transpose again before stock reconciliation. WHERE ImportFbaInventoryAdjustmentReportID=" + importTableId;
// //MiscFunction.EventLogInsert(errorMessage, 1);
// throw new Exception(errorMessage);
//}
DateTime transactionDate = reader.GetDateTime(index02);
transactionDate = DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
string sku = reader.GetString(index03);
string fnsku = reader.GetString(index08);
string fulfillmentCenterId = reader.GetString(index09);
int quantity = reader.GetInt32(index04);
if (quantity < 0) { quantity = quantity * -1; }
string reference = reader.GetString(index05);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true);
if (skuId < 1) { transposeSkip = transposeSkip + 1; continue; }
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection transConn = new SqlConnection(sqlConnectionString))
{
transConn.Open();
int transactionId = 0;
if (quantity == 0)
{
// this will set record IsProcessed=True and StockTransactionID=Null
transactionId = 0;
}
/* Special Case - Found Inventory
*
* No transaction table reconciliation issues if sku is lost before it is found. However, transactions
* will be un-reconcilable if sku is found before it is lost. In this case, hold 'found' transaction(s)
* in import table. When a corresponding sku lost is posted, transfer 'found' transaction over
* to transaction table and manually reconcile.
*
* Additional issues with points above (and how code is set up), misplaced and found do not always balance
* (i.e. F - M <> 0). An sku lost in a inbound shipment can be found, but will not get registered as misplaced.
*/
// always add 'misplaced' inventory to transaction table
else if (false) //(reasonCode == "F")
{
int misplacedTotal = 0;
int foundProcessedTotal = 0;
// retrive total misplaced (all)
using (SqlCommand cmd2 = new SqlCommand(@"
SELECT Sum(quantity) As SumOfQuantity
FROM tblImportFbaInventoryAdjustmentReport
WHERE reason='M' AND sku=@sku;
", transConn))
{
cmd2.Parameters.AddWithValue("sku", sku);
//cmd2.Parameters.AddWithValue("disposition", disposition);
object result = cmd2.ExecuteScalar();
if (!(result == DBNull.Value))
{
misplacedTotal = (int)result * -1;
}
}
// retrive total found (processed)
using (SqlCommand cmd2 = new SqlCommand(@"
SELECT Sum(quantity) As SumOfQuantity
FROM tblImportFbaInventoryAdjustmentReport
WHERE reason='F' AND sku=@sku AND IsProcessed=1;
", transConn))
{
cmd2.Parameters.AddWithValue("sku", sku);
//cmd2.Parameters.AddWithValue("disposition", disposition);
object result = cmd2.ExecuteScalar();
if (!(result == DBNull.Value))
{
foundProcessedTotal = (int)result;
}
}
int misplacedAvaliable = misplacedTotal - foundProcessedTotal;
// leave in import table, unprocessed
if (misplacedAvaliable < 1)
{
continue;
}
// item can be copied striaght into trans table and marked as processed
else if (misplacedAvaliable == quantity)
{
// nothing to do
}
// split record and add correct quantity to transaction table
else if (misplacedAvaliable < quantity)
{
// add new cloned record with unmatched quantity
using (SqlCommand cmd2 = new SqlCommand(@"
INSERT INTO tblImportFbaInventoryAdjustmentReport (
[adjusted-date], [transaction-item-id], fnsku, sku, [fulfillment-center-id], quantity, reason, disposition, IsProcessed )
VALUES (
@adjustedDate, @transactionItemId, @fnsku, @sku, @fulfillmentCenterId, @quantity, @reason, @disposition, 0 )
", transConn))
{
cmd2.Parameters.AddWithValue("adjustedDate", transactionDate.ToUniversalTime());
cmd2.Parameters.AddWithValue("transactionItemId", transactionTypeId);
cmd2.Parameters.AddWithValue("fnsku", fnsku);
cmd2.Parameters.AddWithValue("sku", sku);
cmd2.Parameters.AddWithValue("fulfillmentCenterId", fulfillmentCenterId);
cmd2.Parameters.AddWithValue("quantity", (quantity - misplacedAvaliable));
cmd2.Parameters.AddWithValue("reason", reasonCode);
cmd2.Parameters.AddWithValue("disposition", disposition);
cmd2.ExecuteNonQuery();
}
quantity = misplacedAvaliable;
// update quantity of soon to be processed record (added to trans table)
using (SqlCommand cmd2 = new SqlCommand(@"
UPDATE tblImportFbaInventoryAdjustmentReport
SET quantity=@quantity
WHERE ImportFbaInventoryAdjustmentReportID=@importTableId
", transConn))
{
cmd2.Parameters.AddWithValue("quantity", quantity);
cmd2.Parameters.AddWithValue("importTableId", importTableId);
cmd2.ExecuteNonQuery();
}
}
else if (misplacedAvaliable > quantity)
{
// nothing to do
}
// should never get here
else
{
throw new Exception("Shit the bed, 'unreachable' code has been reached...");
}
//// very old code
//using (SqlCommand cmd2 = new SqlCommand(@"
//SELECT StockTransactionID, Quantity FROM tblStockTransaction
//WHERE StockTransactionTypeID=@stockTransactionTypeId AND SkuID=@skuId AND Quantity>0 AND IsProcessed=0
//ORDER BY TransactionDate
//", transConn))
//{
// // retrive transaction type ids
// int oppTransactionTypeId = 0;
// if (reasonCode == "F")
// {
// oppTransactionTypeId = GeneralQueries.GetStockTransactionTypeId(sqlConnectionString, adjCodes["M"] + "<" + disposition + ">");
// }
// else
// {
// oppTransactionTypeId = GeneralQueries.GetStockTransactionTypeId(sqlConnectionString, adjCodes["F"] + "<" + disposition + ">");
// }
// if (oppTransactionTypeId < 1)
// {
// continue;
// }
// cmd2.Parameters.AddWithValue("stockTransactionTypeId", oppTransactionTypeId);
// cmd2.Parameters.AddWithValue("skuId", skuId);
// //cmd2.Parameters.AddWithValue("quantity", quantity);
// using (SqlDataReader reader2 = cmd2.ExecuteReader())
// {
// if (reader2.HasRows)
// {
// while (reader2.Read() && quantity > 0)
// {
// transactionId = reader2.GetInt32(0);
// int quantityAvaliable = reader2.GetInt32(1);
// // case: only transaction quantity requires update i.e. there is qty remaining
// if (quantityAvaliable > quantity)
// {
// quantityAvaliable = quantityAvaliable - quantity;
// using (SqlCommand cmd3 = new SqlCommand(@"
// UPDATE tblStockTransaction
// SET Quantity=@quantity
// WHERE StockTransactionID=@transactionId
// ", transConn))
// {
// cmd3.Parameters.AddWithValue("transactionId", transactionId);
// cmd3.Parameters.AddWithValue("quantity", quantityAvaliable);
// cmd3.ExecuteNonQuery();
// quantity = 0;
// }
// }
// // case: delete transaction as quantity availiable is <= quantity
// else
// {
// using (SqlCommand cmd3 = new SqlCommand(@"
// UPDATE tblImportFbaInventoryAdjustmentReport
// SET StockTransactionID=NULL
// WHERE StockTransactionID=@transactionId
// ", transConn))
// {
// cmd3.Parameters.AddWithValue("transactionId", transactionId);
// cmd3.ExecuteNonQuery();
// }
// using (SqlCommand cmd3 = new SqlCommand(@"
// DELETE FROM tblStockTransaction
// WHERE StockTransactionID=@transactionId
// ", transConn))
// {
// cmd3.Parameters.AddWithValue("transactionId", transactionId);
// cmd3.ExecuteNonQuery();
// }
// transactionId = 0;
// quantity = quantity - quantityAvaliable;
// }
// }
// if (quantity == 0)
// {
// addTransaction = false;
// }
// }
// else
// {
// //do nothing, leave entry uneffected
// addTransaction = true;
// }
// }
//}
}
// usual case
if (transactionTypeId > 0 && addTransaction == true)
{
transactionId = Stock.StockReconciliation.StockTransactionInsert(
sqlConnectionString, transactionDate, transactionTypeId, skuId, quantity, 0, reference, "", false, 0
);
}
using (SqlCommand updateCmd = new SqlCommand(
"UPDATE tblImportFbaInventoryAdjustmentReport " +
"SET StockTransactionID=@transactionId, IsProcessed=1 " +
"WHERE ImportFbaInventoryAdjustmentReportID=@importTableId;",
transConn))
{
if (transactionId == 0) { updateCmd.Parameters.AddWithValue("@transactionId", DBNull.Value); }
else { updateCmd.Parameters.AddWithValue("@transactionId", transactionId); }
updateCmd.Parameters.AddWithValue("@importTableId", importTableId);
updateCmd.ExecuteNonQuery();
}
scope.Complete();
transposeCount = transposeCount + 1;
}
}
}
Console.Write("\r");
}
}
}
}
MiscFunction.EventLogInsert("TransposeFbaAdustmentReport() complete, " + transposeCount + " total records transposed, " + transposeSkip + " records skipped.");
if (transposeSkip > 0) { MiscFunction.EventLogInsert(transposeSkip + " number records skipped during TransposeFbaAdustmentReport() operation.", 2); }
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Exception catch, aborting TransposeFbaAdustmentReport(), see detailed info. "
+ transposeCount + " total records completed, " + transposeSkip + " records skipped.", 1, ex.ToString());
}
return;
}
public void WIP_ProcessFbaReimbursementData(string sqlConnectionString)
{
/*
* Not to be used for stock reconciliation! A single stock item can have multiple reimburesements aginst it.
* Only use to move lost inventory to Amazon ownership (even then, with some caveats!)
*
* generally any lost or damaged stock goes to lost and found (and is hence written off)
* once amazon have reimbursed (confitmation via this report) the stock is then move to amazon owvership
* and also the 'Cost of goods' amounts moved to the appropreate account id
*/
MiscFunction.EventLogInsert("Starting TransposeFbaRemovalOrderReport()");
int transposeCount = 0;
int transposeSkip = 0;
int stockJournalTypeId = 11;
try
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT * FROM tblImportFbaReimbursementReport WHERE IsProcessed=0 " +
"ORDER BY [approval-date];"
, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
int record = 0;
int index01 = reader.GetOrdinal("ImportFbaReimbursementReportID");
int index02 = reader.GetOrdinal("approval-date");
int index03 = reader.GetOrdinal("reimbursement-id");
int index04 = reader.GetOrdinal("reason");
int index05 = reader.GetOrdinal("sku");
int index06 = reader.GetOrdinal("quantity-reimbursed-total");
int index07 = reader.GetOrdinal("quantity-reimbursed-Inventory");
while (reader.Read())
{
record = record + 1;
Console.Write("\rProcessing record: " + record);
int invRecived = reader.GetInt32(index07);
string matchStringPos = "";
int transactionTypeIdPos = 0;
// get transtypeIds for qty transaferred to amazon ownership
string matchStringNeg = "<_GET_FBA_REIMBURSEMENTS_DATA_><-ve><" + reader.GetString(index04) + ">";
int transactionTypeIdNeg = Stock.StockJournal.StockTransactionTypeIdSelect(sqlConnectionString, matchStringNeg);
if (invRecived > 0)
{
matchStringPos = "<_GET_FBA_REIMBURSEMENTS_DATA_><+ve><" + reader.GetString(index04) + ">";
transactionTypeIdPos = Stock.StockJournal.StockTransactionTypeIdSelect(sqlConnectionString, matchStringNeg);
}
/* GetStockTransactionTypeId return meanings
* >0 use the as the TypeId when inserting transaction
* 0 Skip transpose, type is new or has not been reviewed yet
* -1 Type import/transpose is disabled, IsProcessed=TRUE StockTransactionID=NULL */
if (transactionTypeIdNeg == 0 || (transactionTypeIdPos == 0 && invRecived > 0))
{
transposeSkip = transposeSkip + 1;
int temp = Stock.StockJournal.StockTransactionTypeIdInsert(sqlConnectionString, stockJournalTypeId, matchStringNeg);
if (transactionTypeIdPos == 0 && invRecived > 0)
{
temp = Stock.StockJournal.StockTransactionTypeIdInsert(sqlConnectionString, stockJournalTypeId, matchStringPos);
}
continue;
}
int importTableId = reader.GetInt32(index01);
DateTime transactionDate = DateTime.SpecifyKind(reader.GetDateTime(index02), DateTimeKind.Utc);
string sku = reader.GetString(index05);
string reference = reader.GetString(index03);
int quantityTotal = reader.GetInt32(index06);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, reader.GetString(index05), true);
if (skuId < 1)
{
transposeSkip = transposeSkip + 1;
continue;
}
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection transConn = new SqlConnection(sqlConnectionString))
{
transConn.Open();
int transactionId = 0;
if (transactionTypeIdNeg > 0)
{
transactionId = Stock.StockReconciliation.StockTransactionInsert(
sqlConnectionString, transactionDate, transactionTypeIdNeg, skuId, quantityTotal, 0, reference);
}
if (transactionTypeIdPos > 0)
{
int temp = Stock.StockReconciliation.StockTransactionInsert(
sqlConnectionString, transactionDate, transactionTypeIdPos, skuId, invRecived, 0, reference);
}
using (SqlCommand updateCmd = new SqlCommand(@"
UPDATE
tblImportFbaReimbursementReport
SET
StockTransactionID=@transactionId,
IsProcessed=1
WHERE
ImportFbaReimbursementReportID=@importTableId;
", transConn))
{
if (transactionId == 0) { updateCmd.Parameters.AddWithValue("@transactionId", DBNull.Value); }
else { updateCmd.Parameters.AddWithValue("@transactionId", transactionId); }
updateCmd.Parameters.AddWithValue("@importTableId", importTableId);
updateCmd.ExecuteNonQuery();
}
scope.Complete();
transposeCount = transposeCount + 1;
}
}
}
Console.Write("\r");
}
}
}
}
MiscFunction.EventLogInsert("ProcessFbaReimbursementData() complete, " + transposeCount + " total records transposed, " + transposeSkip + " records skipped.");
if (transposeSkip > 0) { MiscFunction.EventLogInsert(transposeSkip + " number records skipped during TransposeFbaRemovalOrderReport() operation.", 2); }
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Exception catch, aborting ProcessFbaReimbursementData(), see detailed info. "
+ transposeCount + " total records completed, " + transposeSkip + " records skipped.", 1, ex.ToString());
}
}
public void ProcessReportFbaRemovalOrder(string sqlConnectionString)
{
MiscFunction.EventLogInsert("Starting TransposeFbaRemovalOrderReport()");
int transposeCount = 0;
int transposeSkip = 0;
int stockJournalTypeId = 10;
try
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT * FROM tblImportFbaRemovalOrderReport WHERE IsProcessed=0 " +
"ORDER BY [request-date];"
, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
int record = 0;
int index01 = reader.GetOrdinal("ImportFbaRemovalOrderReportID");
int index02 = reader.GetOrdinal("request-date");
int index03 = reader.GetOrdinal("sku");
//int index04 = reader.GetOrdinal("requested-quantity");
int index05 = reader.GetOrdinal("order-id");
int index06 = reader.GetOrdinal("order-type");
int index07 = reader.GetOrdinal("order-status");
int index08 = reader.GetOrdinal("disposition");
int index09 = reader.GetOrdinal("disposed-quantity");
int index10 = reader.GetOrdinal("shipped-quantity");
while (reader.Read())
{
record = record + 1;
Console.Write("\rProcessing record: " + record);
// skip if order is not "Completed" status
if (!(reader.GetString(index07) == "Completed")) { continue; }
string matchString = "<_GET_FBA_FULFILLMENT_REMOVAL_ORDER_DETAIL_DATA_><"
+ reader.GetString(index06) + "><" + reader.GetString(index08) + ">";
int transactionTypeId = Stock.StockJournal.StockTransactionTypeIdSelect(sqlConnectionString, matchString);
/* GetStockTransactionTypeId return meanings
* >0 use the as the TypeId when inserting transaction
* 0 Skip transpose, type is new or has not been reviewed yet
* -1 Type import/transpose is disabled, IsProcessed=TRUE StockTransactionID=NULL */
if (transactionTypeId == 0)
{
int temp = Stock.StockJournal.StockTransactionTypeIdInsert(sqlConnectionString, stockJournalTypeId, matchString);
transposeSkip = transposeSkip + 1;
continue;
}
int importTableId = reader.GetInt32(index01);
DateTime transactionDate = reader.GetDateTime(index02);
transactionDate = DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
string sku = reader.GetString(index03);
string reference = reader.GetString(index05);
int quantity = 0;
int skuId = 0;
if (!reader.IsDBNull(index09))
{ quantity = reader.GetInt32(index09); }
if (!reader.IsDBNull(index10))
{ quantity = reader.GetInt32(index10); }
if (quantity == 0)
{ transactionTypeId = -1; }
else
{ skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true); }
if (skuId < 1)
{ transposeSkip = transposeSkip + 1; continue; }
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection transConn = new SqlConnection(sqlConnectionString))
{
transConn.Open();
int transactionId = 0;
if (transactionTypeId > 0)
{ transactionId = Stock.StockReconciliation.StockTransactionInsert(sqlConnectionString, transactionDate, transactionTypeId, skuId, quantity, 0, reference, "", false, 0); }
using (SqlCommand updateCmd = new SqlCommand(
"UPDATE tblImportFbaRemovalOrderReport " +
"SET StockTransactionID=@transactionId, IsProcessed=1 " +
"WHERE ImportFbaRemovalOrderReportID=@importTableId;",
transConn))
{
if (transactionId == 0) { updateCmd.Parameters.AddWithValue("@transactionId", DBNull.Value); }
else { updateCmd.Parameters.AddWithValue("@transactionId", transactionId); }
updateCmd.Parameters.AddWithValue("@importTableId", importTableId);
updateCmd.ExecuteNonQuery();
}
scope.Complete();
transposeCount = transposeCount + 1;
}
}
}
Console.Write("\r");
}
}
}
}
MiscFunction.EventLogInsert("TransposeFbaRemovalOrderReport() complete, " + transposeCount + " total records transposed, " + transposeSkip + " records skipped.");
if (transposeSkip > 0) { MiscFunction.EventLogInsert(transposeSkip + " number records skipped during TransposeFbaRemovalOrderReport() operation.", 2); }
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Exception catch, aborting TransposeFbaRemovalOrderReport(), see detailed info. "
+ transposeCount + " total records completed, " + transposeSkip + " records skipped.", 1, ex.ToString());
}
}
public static int StockTransactionInsert(string sqlConnectionString, DateTime transactionUtcDate, int transactionTypeId, int skuId, int quantity,
int foreignKey = 0, string reference = "", string detail = "", bool isProcessed = false, int stockJournalId = 0)
{
transactionUtcDate = DateTime.SpecifyKind(transactionUtcDate, DateTimeKind.Utc);
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"INSERT INTO tblStockTransaction ( TransactionDate, StockTransactionTypeID, ForeignKey, SkuID, Quantity, Reference, Detail, IsProcessed, StockJournalID ) " +
"OUTPUT INSERTED.StockTransactionID " +
"VALUES (@transactionDate, @transactionTypeId, @foreignKey, @skuId, @Quantity, @reference, @detail, @isProcessed, @StockJournalId );",
sqlConn))
{
cmd.Parameters.AddWithValue("@transactionDate", transactionUtcDate.ToUniversalTime());
cmd.Parameters.AddWithValue("@transactionTypeId", transactionTypeId);
cmd.Parameters.AddWithValue("@skuId", skuId);
cmd.Parameters.AddWithValue("@Quantity", quantity);
if (foreignKey == 0) { cmd.Parameters.AddWithValue("@foreignKey", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@foreignKey", foreignKey); }
if (reference == "") { cmd.Parameters.AddWithValue("@reference", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@reference", reference); }
if (detail == "") { cmd.Parameters.AddWithValue("@detail", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@detail", detail); }
if (isProcessed == false) { cmd.Parameters.AddWithValue("@isProcessed", 0); }
else { cmd.Parameters.AddWithValue("@isProcessed", 1); }
if (stockJournalId == 0) { cmd.Parameters.AddWithValue("@StockJournalId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@StockJournalId", stockJournalId); }
int transactionId = Convert.ToInt32(cmd.ExecuteScalar());
return transactionId;
}
}
}
// iterates through the stock transaction table and inserts stock journal entries, where applicable
// N.B. This function does not make allowances for status' that can create stock (i.e. if a status does not have stock available, this function will halt processing rows)
public ReconcileStockTransactionsResult ReconcileStockTransactions(string sqlConnectionString, bool updateTransactions)
{
// create the return object
ReconcileStockTransactionsResult returnResult = new ReconcileStockTransactionsResult();
string currentMethodName = nameof(ReconcileStockTransactions);
//bool returnComplete = false;
//string returnMessage = "";
//string returnDetails = "";
// ensure import table have been processed into transaction table without exception
if (updateTransactions == true)
{
try
{
var preCheck = new StockReconciliation();
preCheck.ProcessFbaStockImportData(sqlConnectionString);
}
catch (Exception ex)
{
returnResult.ProgressMessage = "Precheck failed: " + ex.Message;
return returnResult;
}
}
MiscFunction.EventLogInsert("Starting ReconcileStockTransactions()");
int record = 0;
int recordSkip = 0;
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
string sqlPartString = @"
FROM tblStockTransaction INNER JOIN
tblStockTransactionType ON tblStockTransaction.StockTransactionTypeID = tblStockTransactionType.StockTransactionTypeID
WHERE tblStockTransaction.IsProcessed = 0 AND tblStockTransaction.SkuID IS NOT NULL AND tblStockTransaction.Quantity IS NOT NULL
AND ( tblStockTransactionType.StockJournalEntryEnabled = 1 OR tblStockTransactionType.IsNewReviewRequired = 1 ) ";
// get row count of record set
using (SqlCommand cmd = new SqlCommand("SELECT COUNT (tblStockTransaction.StockTransactionID) " + sqlPartString, conn))
{
returnResult.ItemsRemaining = (int)cmd.ExecuteScalar();
returnResult.ItemsCompleted = 0;
}
// order by stocktransId desc = Amazon reports are listed in datetime ASC order, some reports have date only, however I have reason to believe these are in time order
// also. adding this means they at least get processed in the correct order (maybe)!
using (SqlCommand cmd = new SqlCommand(@"
SELECT tblStockTransaction.StockTransactionID, tblStockTransaction.TransactionDate, tblStockTransaction.StockTransactionTypeID, tblStockTransaction.ForeignKey,
tblStockTransaction.Reference, tblStockTransaction.Detail, tblStockTransaction.SkuID, tblStockTransaction.Quantity,
tblStockTransactionType.StockJournalEntryEnabled, tblStockTransactionType.DebitStockStatusID, tblStockTransactionType.CreditStockStatusID,
tblStockTransactionType.StatusBalanceCheckRequired, tblStockTransactionType.CreditStatusFirstInFirstOut, tblStockTransactionType.IsNewReviewRequired,
tblStockTransactionType.FilterStockOnDateTime, tblStockTransactionType.MatchString, StockJournalTypeID " +
sqlPartString + @"
ORDER BY tblStockTransaction.TransactionDate ASC, tblStockTransaction.StockTransactionID DESC;
", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
int index01 = reader.GetOrdinal("StockTransactionID");
int index02 = reader.GetOrdinal("TransactionDate");
int index03 = reader.GetOrdinal("StockTransactionTypeID");
int index04 = reader.GetOrdinal("SkuID");
int index05 = reader.GetOrdinal("Quantity");
int index06 = reader.GetOrdinal("StockJournalEntryEnabled");
int index07 = reader.GetOrdinal("ForeignKey");
int index08 = reader.GetOrdinal("Reference");
int index09 = reader.GetOrdinal("Detail");
int index10 = reader.GetOrdinal("CreditStatusFirstInFirstOut");
int index11 = reader.GetOrdinal("DebitStockStatusID");
int index12 = reader.GetOrdinal("CreditStockStatusID");
int index13 = reader.GetOrdinal("IsNewReviewRequired");
int index14 = reader.GetOrdinal("FilterStockOnDateTime");
int index15 = reader.GetOrdinal("MatchString");
int index16 = reader.GetOrdinal("StockJournalTypeID");
while (reader.Read())
{
record = record + 1;
Console.Write("\rProcessing record: {0} ({1} skipped)", (record + recordSkip), recordSkip);
// read values into variables
int stockTransactionId = reader.GetInt32(index01);
DateTime transactionDate = reader.GetDateTime(index02);
transactionDate = DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
int transactionTypeID = reader.GetInt32(index03);
int skuId = reader.GetInt32(index04);
int quantity = reader.GetInt32(index05);
bool entryEnabled = reader.GetBoolean(index06);
int foreignKey = 0;
if (reader[index07] != DBNull.Value)
{ foreignKey = reader.GetInt32(index07); }
string reference = "";
if (reader[index08] != DBNull.Value)
{ reference = reader.GetString(index08); }
string detail = "";
if (reader[index09] != DBNull.Value)
{ detail = reader.GetString(index09); }
bool orderAsc = reader.GetBoolean(index10);
// check/set debit/credit staus to 0 for special cases
int debitStatus = 0;
if (!reader.IsDBNull(index11))
{ debitStatus = reader.GetInt32(index11); }
int creditStatus = 0;
if (!reader.IsDBNull(index12))
{ creditStatus = reader.GetInt32(index12); }
bool filterStockOnDateTime = reader.GetBoolean(index14);
string matchString = "";
if (!reader.IsDBNull(index15))
{
matchString = reader.GetString(index15);
}
int journalTypeId = reader.GetInt32(index16);
// setup return values
returnResult.StockTransactionId = stockTransactionId;
returnResult.StockTransactionTypeId = transactionTypeID;
returnResult.LastItemDateTime = transactionDate;
// stop if a new transactiontype is encountered
if (reader.GetBoolean(index13) == true)
{
returnResult.ProgressMessage = "New 'Transaction-Type' encountered";
//Console.Write("\r");
//MiscFunction.EventLogInsert(errMessage, 1);
goto Finish;
}
// set debit/credit status' for special cases (i.e. NULL or 0 debit/credit ids in stockTransactionType table)
if (entryEnabled == true && (debitStatus == 0 || creditStatus == 0))
{
// FBA Shipment Receipt +ve
if (matchString == "<_GET_FBA_FULFILLMENT_INVENTORY_RECEIPTS_DATA_><+ve>"
|| matchString == "<_GET_FBA_FULFILLMENT_INVENTORY_RECEIPTS_DATA_><-ve>")
{
using (SqlCommand cmd2 = new SqlCommand("" +
"SELECT ShipmentStockStatusID FROM tblAmazonShipment WHERE ShipmentId=@reference", conn))
{
cmd2.Parameters.Add(new SqlParameter("reference", reference));
object temp = cmd2.ExecuteScalar();
if (temp != DBNull.Value)
{
// +ve shipment receipt
if (creditStatus == 0 && debitStatus > 0)
{ creditStatus = Convert.ToInt32(temp); }
// -ve shipment receipt
else if (debitStatus == 0 && creditStatus > 0)
{ debitStatus = Convert.ToInt32(temp); }
// something went wrong, raise error
else
{
returnResult.ProgressMessage = "Unable to retrive FBA shipment location/status from tblAmazonShipment for Amazon shipment '" + reference + "'.";
recordSkip = recordSkip + 1;
//Console.Write("\r");
//MiscFunction.EventLogInsert("Debit or credit id value missing for shipment receipt WHERE StockTransactionTypeId="
// + transactionTypeID, 1, "", default(DateTime), true, conn);
goto Finish;
}
}
}
}
// something went wrong, raise error
else
{
returnResult.ProgressMessage = "Coding required. Unhandled special case Transaction-Type encountered (Transaction-Type debit or credit is set to 0).";
recordSkip = recordSkip + 1;
goto Finish;
}
}
// make the changes
if (entryEnabled == false)
{
using (SqlCommand cmdIsProcessed = new SqlCommand(@"
UPDATE tblStockTransaction
SET IsProcessed=1
WHERE StockTransactionID=@stockTransactionId
", conn))
{
cmdIsProcessed.Parameters.AddWithValue("@stockTransactionId", stockTransactionId);
cmdIsProcessed.ExecuteNonQuery();
}
}
else
{
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection transConn = new SqlConnection(sqlConnectionString))
{
transConn.Open();
List> list = new List>();
if (filterStockOnDateTime == true)
{
list = Stock.StockJournal.StockReallocateBySkuId(sqlConnectionString, journalTypeId, skuId, quantity, debitStatus, creditStatus, orderAsc,
transactionDate.ToUniversalTime(), false);
}
else
{
list = Stock.StockJournal.StockReallocateBySkuId(sqlConnectionString, journalTypeId, skuId, quantity, debitStatus, creditStatus, orderAsc,
DateTime.UtcNow, false);
}
// insufficient balance available
if (list == null)
{
// in special case (found inventory), continue
if (matchString.Contains("<_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_>"))
{
continue;
}
else
{
returnResult.ProgressMessage = "Insurficent status/location balance to relocate stock";
recordSkip = recordSkip + 1;
//Console.Write("\r");
//MiscFunction.EventLogInsert("StockReallocateBySkuId() returned null (i.e. no avaiable stock to relocate), WHERE StockTransactionId="
// + stockTransactionId, 2, "", default(DateTime), true, conn);
goto Finish;
}
}
// fail safe
if ((list.Sum(c => c.Item2) > quantity))
{
throw new Exception(
currentMethodName + ": StockReallocateBySkuId() returned greater quantity than passed to function"
+ stockTransactionId);
//recordSkip = recordSkip + 1;
//Console.Write("\r");
//MiscFunction.EventLogInsert("StockReallocateBySkuId() returned retuned a greater quantity than passed to function, WHERE StockTransactionId="
// + stockTransactionId, 1, "", default(DateTime), true, conn);
//goto Finish;
}
using (SqlCommand cmdUpdate = new SqlCommand(@"
UPDATE tblStockTransaction
SET Quantity=@quantity, StockJournalID=@StockJournalId, IsProcessed=1
WHERE StockTransactionID=@stockTransactionId
", transConn))
{
cmdUpdate.Parameters.AddWithValue("@StockJournalId", list[0].Item1);
cmdUpdate.Parameters.AddWithValue("@quantity", list[0].Item2);
cmdUpdate.Parameters.AddWithValue("@stockTransactionId", stockTransactionId);
cmdUpdate.ExecuteNonQuery();
}
quantity = quantity - list[0].Item2;
// only nessecary when there is more than one StockJournal entry - stockTransction row will be copied and quantities split accordingly
if (list.Count > 1)
{
for (int i = 1; i < list.Count; i++)
{
int tempInt = Stock.StockReconciliation.StockTransactionInsert(sqlConnectionString,
transactionDate.ToUniversalTime(), transactionTypeID, skuId, list[i].Item2, foreignKey, reference, detail, true, list[i].Item1);
quantity = quantity - list[i].Item2;
//fail safe
if (quantity < 0)
{
throw new Exception(
currentMethodName + ": StockReallocateBySkuId() returned greater quantity than passed to function"
+ stockTransactionId);
//MiscFunction.EventLogInsert("StockReallocateBySkuId() function assigned/returned more quantity than was passed to function",
// 1, "", default(DateTime), true, conn);
//goto Finish;
}
}
}
// any remaining quantity not reallocated is added back to transaction table with no corresponding stock journal entry
if (quantity > 0)
{
int tempInt = Stock.StockReconciliation.StockTransactionInsert(sqlConnectionString,
transactionDate.ToUniversalTime(), transactionTypeID, skuId, quantity, foreignKey, reference, detail, false, 0);
}
scope.Complete();
//}
}
}
}
returnResult.ItemsCompleted = returnResult.ItemsCompleted + 1;
returnResult.ItemsRemaining = returnResult.ItemsRemaining - 1;
}
}
// no records returned
else
{
returnResult.ReconciliationComplete = true;
returnResult.ProgressMessage = "No new transactions to process";
goto Finish;
}
}
}
Console.Write("\r");
}
returnResult.ReconciliationComplete = true;
returnResult.ProgressMessage = "Stock transactions fully reconciled!";
Finish:
MiscFunction.EventLogInsert("ProcessStockTransactions() compete. " + (record - recordSkip) + " total records processed, " + recordSkip + " rows uncompllete due to insurficent stock.");
MiscFunction.EventLogInsert("ProcessStockTransactions(), " + recordSkip + " rows skipped due to insurficent stock.", 2);
return returnResult;
}
}
}
namespace Sku
{
public class SkuQuery
{
public static int SkuLookupId(string sqlConnectionString, string sku, bool enableLegacy = false)
{
// if enableLegacy = true, function will attempt to lookup value by sku count
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"
SELECT skuSkuID FROM tblSku WHERE skuSkuNumber=@sku;
", conn))
{
cmd.Parameters.AddWithValue("@sku", sku);
object obj = cmd.ExecuteScalar();
if (!(obj == null))
{
return Convert.ToInt32(obj);
}
}
// if that didn't work, lookup buy sku count
if (sku.Length == 6 & enableLegacy == true)
{
int skucount;
bool okay = int.TryParse(sku, out skucount);
if (okay)
{
using (SqlCommand cmd = new SqlCommand(
"SELECT skuSkuID FROM tblSku WHERE skuSkuCount=@skuCount;"
, conn))
{
cmd.Parameters.AddWithValue("@skuCount", skucount);
object obj = cmd.ExecuteScalar();
if (!(obj == null))
{
return Convert.ToInt32(obj);
}
else
{
return -1;
}
}
}
}
}
return -1;
}
// used for retriving SKU that matched parameters, creates new if required, returns 0 if not found
public static int WIP_SkuGetSet(string sqlConnectionString, int productId, int conditionId, int accountTaxCodeId, bool noMatchInsertNew)
{
using (TransactionScope scope = new TransactionScope())
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
// look for existing entry
using (SqlCommand cmd = new SqlCommand(@"
SELECT
tblSku.skuSkuID
FROM
tblSku
WHERE
(((tblSku.skuProductID)=@productId) AND ((tblSku.skuSkuConditionID)=@conditionId) AND ((tblSku.AccountTaxCodeID)=@accountTaxCodeId));
", conn))
{
cmd.Parameters.AddWithValue("@productId", productId);
cmd.Parameters.AddWithValue("@conditionId", conditionId);
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
object obj = cmd.ExecuteScalar();
if (obj != null)
{
return (int)obj;
}
}
// value check insert bool
if (noMatchInsertNew == false)
{
return 0;
}
else
{
// get this far, check tax code id is a valid for SKU
using (SqlCommand cmd = new SqlCommand(@"
SELECT tblAccountTaxCode.InvoiceSales
FROM tblAccountTaxCode
WHERE (((tblAccountTaxCode.AccountTaxCodeID)=@accountTaxCodeId));
", conn))
{
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
object obj = cmd.ExecuteScalar();
if (obj == null)
{
throw new Exception("AccountTaxCodeID=" + accountTaxCodeId + " doesn't exist!");
}
else
{
bool result = (bool)obj;
if (result == false)
{
throw new Exception("AccountTaxCodeID=" + accountTaxCodeId + " is not a valid type for an SKU.");
}
}
}
// get info to create sku number
int skuCount;
int skuSuffix;
using (SqlCommand cmd = new SqlCommand("SELECT NEXT VALUE FOR SkuCountSequence;", conn))
{
skuCount = (int)cmd.ExecuteScalar();
}
using (SqlCommand cmd = new SqlCommand(@"
SELECT tblSkuCondition.scnSkuNumberSuffix
FROM tblSkuCondition
WHERE (((tblSkuCondition.scnSkuConditionID)=@conditionId));
", conn))
{
cmd.Parameters.AddWithValue("@conditionId", conditionId);
try
{
skuSuffix = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception("Error retriving SKU number suffix for SkuConditionID=" + conditionId + "." +
System.Environment.NewLine + "Error Message: " + ex.Message);
}
}
string skuNumber = skuCount.ToString("D6") + "-" + skuSuffix.ToString("D2");
// insert new sku
int skuId;
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblSku
(skuSkuNumber, skuProductID, skuSkuConditionID, AccountTaxCodeID)
OUTPUT INSERTED.skuSkuID
VALUES
(@skuNumber, @productId, @conditionId, @accountTaxCodeId)
", conn))
{
cmd.Parameters.AddWithValue("@skuNumber", skuNumber);
cmd.Parameters.AddWithValue("@productId", productId);
cmd.Parameters.AddWithValue("@conditionId", conditionId);
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
skuId = (int)cmd.ExecuteScalar();
}
scope.Complete();
return skuId;
}
}
}
}
}
namespace Inventory
{
public class InventoryPricing
{
public static void AmazonMinMaxTemp(string sqlConnectionString)
{
MiscFunction.ConsoleUpdate("Operation started.");
// first command line git commit and push
DateTime crTimeStamp = DateTime.SpecifyKind( DateTime.UtcNow, DateTimeKind.Utc);
int count = 0;
int exceptionSku = 0;
string stringSql = @"
SELECT
b.SkuID
,b.SkuTotalQuantity
,b.SkuTotalCost
,b.SkuAvgUnitCost
,tblSku.skuProductID
,tblSku.skuSkuConditionID
,tblSku.AccountTaxCodeID
,tblProductCategory.ProfitMinPercent
,tblProductCategory.ProfitMinAmount
,(tblAmazonFeeEstimate.ReferralFee / tblAmazonFeeEstimate.PriceToEstimateFeeListingPrice) As AmazonMargin
,(tblAmazonFeeEstimate.VariableClosingFee + tblAmazonFeeEstimate.PerItemFee + tblAmazonFeeEstimate.FBAFee + tblAmazonFeeEstimate.OtherFee_Exception) AS AmazonFees
,tblAccountTaxCode.TaxRateMultiplierNet
,tblAccountTaxCode.TaxRateNameShort
,tblSkuCondition.IsFixedPrice
,tblSkuCondition.CompetitivePriceMultiplierNew
,tblSkuCondition.CompetitivePriceMultiplierMatch
,tblSkuCondition.scnSkuNumberSuffix
FROM
(((((
SELECT
a.SkuID,
Sum(a.SumOfQuantity) AS SkuTotalQuantity,
Sum(a.QuanityTimesUnitCost) AS SkuTotalCost,
Sum(a.QuanityTimesUnitCost)/Sum(a.SumOfQuantity) AS SkuAvgUnitCost
FROM
(
SELECT
tblStock.SkuID,
Sum(tblStockJournalPost.Quantity) AS SumOfQuantity,
tblStockJournal.StockID, tblAccountStockCost.AmountUnit,
Sum([tblStockJournalPost].[Quantity])*[tblAccountStockCost].[AmountUnit] AS QuanityTimesUnitCost
FROM
(((tblStockJournalPost
INNER JOIN tblStockStatus
ON tblStockJournalPost.StockStatusID = tblStockStatus.StockStatusID)
INNER JOIN tblStockJournal ON tblStockJournalPost.StockJournalID = tblStockJournal.StockJournalID)
INNER JOIN tblAccountStockCost ON tblStockJournal.StockID = tblAccountStockCost.StockID)
INNER JOIN tblStock ON tblAccountStockCost.StockID = tblStock.StockID
WHERE
tblStockStatus.StockStatusTypeID=3
OR tblStockStatus.StockStatusTypeID=4
GROUP BY
tblStockJournal.StockID,
tblAccountStockCost.AmountUnit,
tblStock.SkuID
HAVING
Sum(tblStockJournalPost.Quantity)>0
) a
GROUP BY
a.SkuID
) b
INNER JOIN tblSku ON b.SkuID = tblSku.skuSkuID)
INNER JOIN tblProduct ON tblSku.skuProductID = tblProduct.prdProductID)
LEFT JOIN tblAmazonFeeEstimate ON tblProduct.prdProductID = tblAmazonFeeEstimate.ProductIdentifier )
INNER JOIN tblProductCategory ON tblProduct.ProductCategoryID = tblProductCategory.ProductCategoryID)
INNER JOIN tblAccountTaxCode ON tblSku.AccountTaxCodeID = tblAccountTaxCode.AccountTaxCodeID
INNER JOIN tblSkuCondition ON tblSku.skuSkuConditionID = tblSkuCondition.scnSkuConditionID
";
try
{
using (var conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
// loop through table
using (var cmd01 = new SqlCommand(stringSql, conn))
{
using (var reader01 = cmd01.ExecuteReader())
{
if (reader01.HasRows)
{
}
else
{
throw new Exception("Querying the database returned no records.");
}
// get ordinlals
int ordinalSkuID = reader01.GetOrdinal("SkuID");
int ordinalSkuTotalQuantity = reader01.GetOrdinal("SkuTotalQuantity");
int ordinalSkuTotalCost = reader01.GetOrdinal("SkuTotalCost");
int ordinalSkuAvgUnitCost = reader01.GetOrdinal("SkuAvgUnitCost");
int ordinalProductId = reader01.GetOrdinal("skuProductID");
int ordinalSkuConditionId = reader01.GetOrdinal("skuSkuConditionID");
int ordinalAccountTaxCodeID = reader01.GetOrdinal("AccountTaxCodeID");
int ordinalProfitMinPercent = reader01.GetOrdinal("ProfitMinPercent");
int ordinalProfitMinAmount = reader01.GetOrdinal("ProfitMinAmount");
int ordinalAmazonMargin = reader01.GetOrdinal("AmazonMargin");
int ordinalAmazonFees = reader01.GetOrdinal("AmazonFees");
int ordinalTaxRateMultiplierNet = reader01.GetOrdinal("TaxRateMultiplierNet");
int ordinalTaxRateNameShort = reader01.GetOrdinal("TaxRateNameShort");
int ordinalIsFixedPrice = reader01.GetOrdinal("IsFixedPrice");
int ordinalCompetitivePriceMultiplierNew = reader01.GetOrdinal("CompetitivePriceMultiplierNew");
int ordinalCompetitivePriceMultiplierMatch = reader01.GetOrdinal("CompetitivePriceMultiplierMatch");
int ordinalSkuNumberSuffix = reader01.GetOrdinal("scnSkuNumberSuffix");
while (reader01.Read())
{
// other required variables
int skuId = reader01.GetInt32(ordinalSkuID);
exceptionSku = skuId;
int skuProductId = reader01.GetInt32(ordinalProductId);
int skuConditionId = reader01.GetInt32(ordinalSkuConditionId);
int skuTaxCodeId = reader01.GetInt32(ordinalAccountTaxCodeID);
decimal skuProfitMargin = reader01.GetDecimal(ordinalProfitMinPercent) / 100;
decimal skuAmazonMargin = decimal.Round(reader01.GetDecimal(ordinalAmazonMargin), 3);
decimal skuVatMargin = reader01.GetDecimal(ordinalTaxRateMultiplierNet);
string skuTaxRateName = reader01.GetString(ordinalTaxRateNameShort);
bool skuIsFixedPrice = reader01.GetBoolean(ordinalIsFixedPrice);
decimal skuCompetitivePriceMultiplierNew = reader01.GetDecimal(ordinalCompetitivePriceMultiplierNew);
decimal skuCompetitivePriceMultiplierMatch = reader01.GetDecimal(ordinalCompetitivePriceMultiplierMatch);
decimal skuTotalCost = reader01.GetDecimal(ordinalSkuTotalCost);
decimal skuPriceMinProfit = reader01.GetDecimal(ordinalProfitMinAmount);
decimal skuOrderChannelFee = reader01.GetDecimal(ordinalAmazonFees);
decimal crUnitAvgCost = reader01.GetDecimal(ordinalSkuAvgUnitCost);
int skuNumberSuffix = reader01.GetInt32(ordinalSkuNumberSuffix);
// STAGE 2
// SKU current stock details (i.e. quantity, cost per unit, inventory age, etc.)
// Stage 4
// Set MIN Price
decimal crUnitAvgCostActual = crUnitAvgCost;
decimal crPriceMinAmountAuto;
decimal crPriceMinAmountFinal;
decimal crProfitMinAmount;
// used loss, sells just to cover fees
if (skuNumberSuffix >= 11 && skuNumberSuffix <= 14)
{
if (crUnitAvgCost == 0)
{
skuVatMargin = 0m;
}
crUnitAvgCost = 0m;
skuProfitMargin = 0m;
crProfitMinAmount = 0m;
skuPriceMinProfit = 0m;
}
if (skuTaxRateName == "MS" || skuTaxRateName == "GA")
{
crPriceMinAmountAuto = (5m * crUnitAvgCost + 6m * skuOrderChannelFee) / (-6m * skuProfitMargin - 6m * skuAmazonMargin + 5m);
//crPriceMinCalculatedTax = (crPriceMinAmountAuto - crUnitAvgCost) * (1 / 6);
crProfitMinAmount = crPriceMinAmountAuto * skuProfitMargin;
}
else
{
crPriceMinAmountAuto = (crUnitAvgCost + skuOrderChannelFee) / (1 - (skuProfitMargin + skuAmazonMargin + skuVatMargin));
//crPriceMinCalculatedTax = crPriceMinAmountAuto * skuVatMargin;
crProfitMinAmount = crPriceMinAmountAuto * skuProfitMargin;
}
// if profit margin is less than min required, redo
if (crProfitMinAmount < skuPriceMinProfit)
{
if (skuTaxRateName == "MS" || skuTaxRateName == "GA")
{
crPriceMinAmountAuto = (6m * skuPriceMinProfit + 5m * crUnitAvgCost + 6m * skuOrderChannelFee) / (-6m * skuAmazonMargin + 5m);
//crPriceMinCalculatedTax = (crPriceMinAmountAuto - crUnitAvgCost) * (1 / 6);
crProfitMinAmount = crPriceMinAmountAuto * skuProfitMargin;
}
else
{
crPriceMinAmountAuto = (crUnitAvgCost + skuOrderChannelFee + skuPriceMinProfit) / (1 - (skuAmazonMargin + skuVatMargin));
//crPriceMinCalculatedTax = crPriceMinAmountAuto * skuVatMargin;
crProfitMinAmount = crPriceMinAmountAuto * skuProfitMargin;
}
}
crPriceMinAmountFinal = crPriceMinAmountAuto;
// STAGE 6
// Set MAX Price
decimal crPriceMaxAmountFinal;
// CASE: Reset MAX-Price base & multiplier values (new record or switching back to auto)
// get competative price and apply multiplier
var request = new bnhtradeDatabaseClient.Product.ProductQuery();
var compPrice = request.ProductCompetitivePriceGet(sqlConnectionString, skuProductId, 10);
if (compPrice.price == null || compPrice.priceDate == null)
{
using (var cmd02 = new SqlCommand(@"
SELECT tblProduct.prdMaxPrice
FROM tblSku INNER JOIN tblProduct ON tblSku.skuProductID = tblProduct.prdProductID
WHERE (((tblSku.skuSkuID)=@skuId));
", conn))
{
cmd02.Parameters.AddWithValue("@skuId", skuId);
crPriceMaxAmountFinal = (decimal)cmd02.ExecuteNonQuery();
}
}
else
{
crPriceMaxAmountFinal = (int)(compPrice.price * skuCompetitivePriceMultiplierNew);
}
// for fixed price items
if (skuIsFixedPrice == true)
{
using (var cmd02 = new SqlCommand(@"
SELECT tblSku.skuPriceMin, tblSku.skuPriceMax
FROM tblSku
WHERE (((tblSku.skuSkuID)=@skuId));
", conn))
{
cmd02.Parameters.AddWithValue("@skuId", skuId);
using (var reader02 = cmd02.ExecuteReader())
{
decimal? max = null;
decimal? min = null;
reader02.Read();
if (!reader02.IsDBNull(0)) { min = reader02.GetDecimal(0); }
if (!reader02.IsDBNull(1)) { max = reader02.GetDecimal(1); }
if (max == min && max != null)
{
crPriceMinAmountFinal = (decimal)min;
crPriceMaxAmountFinal = (decimal)max;
}
}
}
}
// STAGE 7
// Checks before update
// max < min
if (crPriceMaxAmountFinal < crPriceMinAmountFinal)
{
crPriceMaxAmountFinal = crPriceMinAmountFinal;
}
// this should be last check
// check for zero values (where there should not be any) -- this could be a life saver i.e. selling item for £0.00
if (crPriceMinAmountFinal * crPriceMaxAmountFinal == 0)
{
throw new Exception("Min and/or Max value has comouted to 0 for SkuId=" + skuId);
}
// STAGE 8
// Update sku table min/max values
// round decimals for db comarison
crUnitAvgCost = decimal.Round(crUnitAvgCost, 2);
crUnitAvgCostActual = decimal.Round(crUnitAvgCostActual, 2);
crPriceMinAmountAuto = decimal.Round(crPriceMinAmountAuto, 2);
crPriceMinAmountFinal = decimal.Round(crPriceMinAmountFinal, 2);
crPriceMaxAmountFinal = decimal.Round(crPriceMaxAmountFinal, 2);
// update sku table
using (var cmd03 = new SqlCommand(@"
UPDATE
tblSku
SET
tblSku.skuPriceMin = @priceMinAmountFinal
, tblSku.skuPriceMax = @priceMaxAmountFinal
,tblSku.skuMinMaxExpire = Null
,tblSku.skuSkuAvgCost = @unitAvgCost
,tblSku.skuSkuAvgCostDate = @unitAvgCostDate
,tblSku.skuPriceCompetitive = @compPrice
, tblSku.skuPriceCompetitiveDate = @compPriceDate
WHERE (((tblSku.skuSkuID)=@skuId));
", conn))
{
cmd03.Parameters.AddWithValue("@priceMinAmountFinal", crPriceMinAmountFinal);
cmd03.Parameters.AddWithValue("@priceMaxAmountFinal", crPriceMaxAmountFinal);
cmd03.Parameters.AddWithValue("@unitAvgCost", crUnitAvgCostActual);
cmd03.Parameters.AddWithValue("@unitAvgCostDate", crTimeStamp.ToUniversalTime());
if (compPrice.price == null)
{
cmd03.Parameters.AddWithValue("@compPrice", DBNull.Value);
cmd03.Parameters.AddWithValue("@compPriceDate", DBNull.Value);
}
else
{
cmd03.Parameters.AddWithValue("@compPrice", compPrice.price);
cmd03.Parameters.AddWithValue("@compPriceDate", DateTime.SpecifyKind((DateTime)compPrice.priceDate, DateTimeKind.Utc).ToUniversalTime());
}
cmd03.Parameters.AddWithValue("@skuId", skuId);
int updated = cmd03.ExecuteNonQuery();
if (!(updated > 0))
{
throw new Exception("record not updated, for some reasonq");
}
}
count = count + 1;
} // drop out of query while loop here
}
}
}
MiscFunction.ConsoleUpdate("Complete. " + count + "SKUs updated.");
}
catch (Exception ex)
{
MiscFunction.ConsoleUpdate("Operation incomplete, stopped at SkuId=" + exceptionSku +". " + count + "SKUs updated.");
MiscFunction.ConsoleUpdate("Exception: " + ex.Message);
throw ex;
}
}
}
}
namespace Order
{
public class OrderQuery
{
public static int GetSaleChannelIdByName(string sqlConnectionString, string orderChannel, bool addChannel = false)
{
if (orderChannel == "Amazon.co.uk") { return 2; }
else if (orderChannel == "Amazon.de") { return 3; }
else if (orderChannel == "Amazon.fr") { return 4; }
else if (orderChannel == "Amazon.it") { return 5; }
else if (orderChannel == "Amazon.es") { return 6; }
else if (orderChannel.Length > 0)
{
int orderChannelId;
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand selectCmd = new SqlCommand(@"
SELECT OrderChannelID FROM tblOrderChannel WHERE OrderChannel=@orderChannel
", sqlConn))
{
selectCmd.Parameters.AddWithValue("@orderChannel", orderChannel);
object obj = selectCmd.ExecuteScalar();
if ((addChannel = true) & (obj == null))
{
using (SqlCommand insertCmd = new SqlCommand(@"
INSERT INTO tblOrderChannel ( OrderChannel )
OUTPUT INSERTED.OrderChannelID
VALUES ( @orderChannel )
", sqlConn))
{
insertCmd.Parameters.AddWithValue("@orderChannel", orderChannel);
object orderIdObject = selectCmd.ExecuteScalar();
orderChannelId = Convert.ToInt32(orderIdObject);
return orderChannelId;
}
}
else
{
orderChannelId = Convert.ToInt32(obj);
return orderChannelId;
}
}
}
}
else { return -1; }
}
public static int GetOrderIdBySaleChannelRef(string sqlConnectionString, int saleChannelId, string saleChannelOrderRef, Boolean autoAdd = false)
{
if (saleChannelId < 1)
{
throw new Exception("Order Channel Id must be greater than zero");
}
if (saleChannelOrderRef.Length == 0)
{
throw new Exception("Incorrect Order Reference passed to method");
}
try
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT OrderID FROM tblOrder WHERE OrderChannelID=@orderChannelId AND OrderChannelRef=@orderChannelRef
", sqlConn))
{
sqlConn.Open();
sqlCommand.Parameters.AddWithValue("@orderChannelId", saleChannelId);
sqlCommand.Parameters.AddWithValue("@orderChannelRef", saleChannelOrderRef);
int orderId = new int();
object obj = sqlCommand.ExecuteScalar();
if (!(obj == null))
{
orderId = Convert.ToInt32(obj);
return orderId;
}
else if (autoAdd == true)
{
using (SqlCommand sqlInsert = new SqlCommand(@"
INSERT INTO tblOrder ( OrderChannelID, OrderChannelRef )
OUTPUT INSERTED.OrderID
VALUES ( @orderChannelId, @orderChannelRef )
", sqlConn))
{
sqlInsert.Parameters.AddWithValue("@orderChannelId", saleChannelId);
sqlInsert.Parameters.AddWithValue("@orderChannelRef", saleChannelOrderRef);
orderId = (int)sqlInsert.ExecuteScalar();
return orderId;
}
}
else
{
return -1;
}
}
}
}
catch (Exception)
{
throw;
}
}
public static int GetOrderItemIdBySaleChannelRef(string sqlConnectionString, int orderId, string saleChannelOrderItemRef, Boolean autoAdd = false)
{
if (orderId < 1)
{
throw new Exception("Order Channel Id must be greater than zero");
}
if (saleChannelOrderItemRef.Length == 0)
{
throw new Exception("Incorrect Order Reference passed to method");
}
try
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT OrderItemID FROM tblOrderItem WHERE OrderID=@orderId AND OrderChannelItemRef=@orderChannelItemRef
", sqlConn))
{
sqlCommand.Parameters.AddWithValue("@orderId", orderId);
sqlCommand.Parameters.AddWithValue("@orderChannelItemRef", saleChannelOrderItemRef);
int orderItemId = new int();
object obj = sqlCommand.ExecuteScalar();
if (!(obj == null))
{
orderItemId = Convert.ToInt32(obj);
return orderItemId;
}
else if (autoAdd == true)
{
using (SqlCommand sqlInsert = new SqlCommand(@"
INSERT INTO tblOrderItem ( OrderID, OrderChannelItemRef )
OUTPUT INSERTED.OrderID
VALUES ( @orderId, @orderChannelItemRef )
", sqlConn))
{
sqlInsert.Parameters.AddWithValue("@orderId", orderId);
sqlInsert.Parameters.AddWithValue("@orderChannelItemRef", saleChannelOrderItemRef);
orderItemId = (int)sqlInsert.ExecuteScalar();
return orderItemId;
}
}
else
{
return -1;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public static int GetSaleChannelIdByAmazonOrderId(string sqlConnectionString, string amazonOrderId)
{
// private, as it gets sale channel from Fba order shipment table, which may only hold data for limited period of time
try
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
// retrive sale channel id
string saleChannelName;
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT [sales-channel] FROM tblImportFbaSaleShipment WHERE [amazon-order-id]=@amazonOrderId
", sqlConn))
{
sqlCommand.Parameters.AddWithValue("@amazonOrderId", amazonOrderId);
object obj = sqlCommand.ExecuteScalar();
if (!(obj == null))
{
saleChannelName = Convert.ToString(obj);
}
else
{
throw new Exception("Could not retrive Amazon Order channel from imported order table, is the import table up to date?");
}
}
return Order.OrderQuery.GetSaleChannelIdByName(sqlConnectionString, saleChannelName, true);
}
}
catch (Exception)
{
throw;
}
}
public static List GetFbaOrderStockIdBySku(string sqlConnectionString, int orderId, int skuId)
{
// return list of stockIds that are attached to an order sku
int stockStatusId;
try
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
// retrive info for FbaOrders - transactionTypeId and it's debit statusId
int transactionTypeId = Stock.StockJournal.StockTransactionTypeIdSelect(sqlConnectionString, "<_GET_AMAZON_FULFILLED_SHIPMENTS_DATA_>");
if (transactionTypeId < 1)
{
throw new Exception("Could not retirve stockTransactionId for amazon shipped orders");
}
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT DebitStockStatusID FROM tblStockTransactionType WHERE StockTransactionTypeID=@transactionTypeId
", sqlConn))
{
sqlCommand.Parameters.AddWithValue("@transactionTypeId", transactionTypeId);
object obj = sqlCommand.ExecuteScalar();
if (obj == null || obj == DBNull.Value)
{
throw new Exception("Could not retrive debit status for amazon shipped orders");
}
else
{
stockStatusId = Convert.ToInt32(obj);
}
}
// retrive orderItemId(s)
List stockIdList = new List();
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT StockJournalID FROM tblStockTransaction
WHERE StockTransactionTypeID=@transactionTypeId AND ForeignKey=@orderId AND SkuID=@skuId
", sqlConn))
{
sqlCommand.Parameters.AddWithValue("@transactionTypeId", transactionTypeId);
sqlCommand.Parameters.AddWithValue("@orderId", orderId);
sqlCommand.Parameters.AddWithValue("@skuId", skuId);
using (SqlDataReader reader = sqlCommand.ExecuteReader())
{
if (reader.HasRows)
{
int index01 = reader.GetOrdinal("StockJournalID");
while (reader.Read())
{
stockIdList.Add(Stock.StockJournal.GetStockIdByStockJournalId(sqlConnectionString, reader.GetInt32(index01)));
}
}
else
{
return null;
}
}
}
// any further checking of available qquantities should be done by the calling function (to reduce to amount of stautus balance sql calls/checks)
return stockIdList;
}
}
catch (Exception)
{
throw;
}
}
}
}
namespace AmazonMWS
{
public class AmazonMwsCredential
{
public string merchantId { get { return "A3RUYNKLWWM5KW"; } }
public string marketPlaceId { get { return "A1F83G8C2ARO7P"; } } // Amazon.co.uk
public string accessKeyId { get { return "AKIAJU45WSYVINEN45UA"; } }
public string secretAccessKey { get { return "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"; } }
public string applicationName { get { return "bnhtrade Database Client"; } }
public string applicationVersion { get { return "0.1"; } }
public string serviceURL { get { return "https://mws.amazonservices.co.uk"; } }
}
public class AmazonMwsService
{
public MarketplaceWebService.MarketplaceWebService GetAmazonMwsService()
{
AmazonMwsCredential cred = new AmazonMwsCredential();
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(
cred.accessKeyId,
cred.secretAccessKey,
cred.applicationName,
cred.applicationVersion,
config);
config.ServiceURL = cred.serviceURL;
return service;
}
}
public class AmazonMwsProduct
{
public MarketplaceWebServiceProducts.MarketplaceWebServiceProducts GetAmazonMwsServiceProduct()
{
AmazonMwsCredential cred = new AmazonMwsCredential();
MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
config.ServiceURL = cred.serviceURL;
MarketplaceWebServiceProducts.MarketplaceWebServiceProducts service = new MarketplaceWebServiceProductsClient(
cred.applicationName,
cred.applicationVersion,
cred.accessKeyId,
cred.secretAccessKey,
config);
return service;
}
///
///
///
///
public List WIP_GetProductEstimateFee( FeesEstimateRequestList requestList)
{
AmazonMwsCredential cred = new AmazonMwsCredential();
AmazonMwsProduct mwsProduct = new AmazonMwsProduct();
MarketplaceWebServiceProducts.MarketplaceWebServiceProducts service = mwsProduct.GetAmazonMwsServiceProduct();
// Create a request.
GetMyFeesEstimateRequest request = new GetMyFeesEstimateRequest();
request.SellerId = cred.merchantId;
request.MWSAuthToken = "example";
//FeesEstimateRequestList requestList = new FeesEstimateRequestList();
//foreach (var item in itemList)
//{
// string idType;
// if (item.IdValueIsAsin) { idType = "ASIN"; }
// else { idType = "SellerSku"; }
// requestList.FeesEstimateRequest.Add(new FeesEstimateRequest
// {
// MarketplaceId = item.marketPlaceId,
// IdType = idType,
// IdValue = item.IdValue,
// PriceToEstimateFees = new PriceToEstimateFees
// {
// ListingPrice = new MoneyType { Amount = item.listingPrice, CurrencyCode = item.currencyCode },
// //Shipping = new MoneyType { Amount = 3.5M, CurrencyCode = "GBP" },
// //Points = new Points { PointsNumber = 0 }
// },
// Identifier = "request_" + Guid.NewGuid().ToString(),
// IsAmazonFulfilled = true
// });
//}
request.FeesEstimateRequestList = requestList;
// define the list
GetMyFeesEstimateResult result = new GetMyFeesEstimateResult();
//List resultList = new List();
try
{
int count = 0;
do
{
GetMyFeesEstimateResponse response = service.GetMyFeesEstimate(request);
if (response.IsSetGetMyFeesEstimateResult())
{
result = response.GetMyFeesEstimateResult;
FeesEstimateResultList resultList = result.FeesEstimateResultList;
List fees = resultList.FeesEstimateResult;
return fees;
}
else
{
Thread.Sleep(500);
count++;
}
} while (count < 60);
throw new Exception("Response timeout");
}
catch
{
throw;
}
}
}
}
public class AmazonReport
{
string merchantId = "A3RUYNKLWWM5KW";
string marketplaceId = "A1F83G8C2ARO7P"; // Amazon.co.uk
string accessKeyId = "AKIAJU45WSYVINEN45UA";
string secretAccessKey = "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";
string applicationName = "AmazonApiApp";
string applicationVersion = "0.1";
string serviceURL = "https://mws.amazonservices.co.uk";
public MarketplaceWebService.MarketplaceWebService GetMwsService()
{
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(
accessKeyId,
secretAccessKey,
applicationName,
applicationVersion,
config);
config.ServiceURL = serviceURL;
return service;
}
public List GetMwsReportList(GetReportListRequest requestList)
{
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
//define the list
GetReportListResult getReportListResult = new GetReportListResult();
List reportInfoList = new List();
do
{
try
{
GetReportListResponse responseList = service.GetReportList(requestList);
if (responseList.IsSetGetReportListResult())
{
getReportListResult = responseList.GetReportListResult;
reportInfoList = getReportListResult.ReportInfo;
}
MiscFunction.EventLogInsert("MWS 'GetReportListRequest' Successful, retrived list of " + reportInfoList.Count + " reports.");
return reportInfoList;
}
catch (MarketplaceWebServiceException ex) when (ex.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(73);
}
catch (MarketplaceWebServiceException ex)
{
MiscFunction.EventLogInsert(
"MWS 'GetReportListRequest' failed with exception: " + ex.Message,
1,
"Caught Exception: " + ex.Message +
"\r\nResponse Status Code: " + ex.StatusCode +
"\r\nError Code: " + ex.ErrorCode +
"\r\nError Type: " + ex.ErrorType +
"\r\nRequest ID: " + ex.RequestId +
"\r\nResponseHeaderMetadata: " + ex.ResponseHeaderMetadata +
"\r\nXML:\r\n" + ex.XML
);
return null;
}
} while (true);
}
public string GetMwsReportById(string reportId)
{
// retrives report by reportId, saves to disk, and returns filepath
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
string filePath = MiscFunction.GetTempFilePath(reportId + ".txt");
GetReportRequest reportRequest = new GetReportRequest();
reportRequest.Merchant = merchantId;
//reportRequest.WithReportId(getReportListResult.ReportInfo[0].ReportId);
reportRequest.ReportId = reportId;
//reportRequest.WithReportId(reportID);
GetReportResponse reportResponse = new GetReportResponse();
do
{
try
{
using (reportRequest.Report = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
{
reportResponse = service.GetReport(reportRequest);
MiscFunction.EventLogInsert("Succesfully downloaded Report '" + reportId + "'");
}
return filePath;
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(80);
}
catch (MarketplaceWebServiceException e)
{
MiscFunction.EventLogInsert(
"MWS request for ReportId '" + reportId + "' failed. See extended details for MWS exception.",
1,
"Request failed with MWS exception: " + e.Message +
"\r\nCaught Exception: " + e.Message +
"\r\nResponse Status Code: " + e.StatusCode +
"\r\nError Code: " + e.ErrorCode +
"\r\nError Type: " + e.ErrorType +
"\r\nRequest ID: " + e.RequestId +
"\r\nResponseHeaderMetadata: " + e.ResponseHeaderMetadata +
"\r\nXML:\r\n" + e.XML
);
break;
}
catch (Exception e)
{
MiscFunction.EventLogInsert(
"MWS request for ReportId '" + reportId + "' failed. See extended details for exception thrown.",
1,
e.ToString()
);
}
} while (true);
return "";
}
public string GetMwsReportByRequest(RequestReportRequest requestReport)
{
try
{
string targetRptType = requestReport.ReportType.ToString();
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
string status;
RequestReportResponse requestResponse = new RequestReportResponse();
while (true)
{
try
{
requestResponse = service.RequestReport(requestReport);
MiscFunction.ConsoleCountDown("Requested, status check in {0} seconds...", 30);
break;
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(60);
}
}
while (true)
{
try
{
status = requestResponse.RequestReportResult.ReportRequestInfo.ReportProcessingStatus;
break;
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(60);
}
}
GetReportRequestListRequest requestList = new GetReportRequestListRequest();
requestList.Merchant = merchantId;
List myListzz = new List();
GetReportRequestListResponse requestListResponse = new GetReportRequestListResponse();
while (true)
{
try
{
requestListResponse = service.GetReportRequestList(requestList);
break;
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(60);
}
}
GetReportRequestListResult requestListResult = new GetReportRequestListResult();
while (true)
{
try
{
requestListResult = requestListResponse.GetReportRequestListResult;
break;
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(60);
}
}
//wait until status = done
myListzz = requestListResult.ReportRequestInfo;
status = myListzz[0].ReportProcessingStatus.ToString();
// Misc.EventLogInsert("Report status: " + status);
while (
status != "_CANCELLED_" &&
status != "_DONE_" &&
status != "_DONE_NO_DATA_"
)
{
try
{
MiscFunction.ConsoleCountDown("Request status '" + status + "' next check {0} seconds...", 30);
requestListResponse = service.GetReportRequestList(requestList);
requestListResult = requestListResponse.GetReportRequestListResult;
myListzz = requestListResult.ReportRequestInfo;
string newStatus = myListzz[0].ReportProcessingStatus.ToString();
if (newStatus != status)
{
status = newStatus;
//Misc.EventLogInsert("Request status: " + status);
}
}
catch (MarketplaceWebServiceException e) when (e.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(60);
}
}
if (status == "_CANCELLED_")
{
MiscFunction.EventLogInsert("Request result: " + status);
return "_CANCELLED_";
}
else if (status == "_DONE_NO_DATA_")
{
MiscFunction.EventLogInsert("Request result: " + status);
return "_DONE_NO_DATA_";
}
else
{
MiscFunction.EventLogInsert("Request result: " + status);
}
GetReportListRequest listRequest = new GetReportListRequest();
listRequest.Merchant = merchantId;
listRequest.ReportRequestIdList = new IdList();
listRequest.ReportRequestIdList.Id.Add(requestResponse.RequestReportResult.ReportRequestInfo.ReportRequestId);
GetReportListResponse listResponse = service.GetReportList(listRequest);
GetReportListResult getReportListResult = listResponse.GetReportListResult;
string reportId = getReportListResult.ReportInfo[0].ReportId.ToString();
return GetMwsReportById(reportId);
}
catch (MarketplaceWebServiceException e)
{
MiscFunction.EventLogInsert(
"Request failed with MWS exception: " + e.Message,
1,
"Caught Exception: " + e.Message +
"\r\nResponse Status Code: " + e.StatusCode +
"\r\nError Code: " + e.ErrorCode +
"\r\nError Type: " + e.ErrorType +
"\r\nRequest ID: " + e.RequestId +
"\r\nResponseHeaderMetadata: " + e.ResponseHeaderMetadata +
"\r\nXML:\r\n" + e.XML
);
return "";
}
}
public List GetMwsReportByPeriod(string mwsReportEnum, DateTime startTime, DateTime endTime, int maxReportCount = 12, int reportMaxPeriod = 30)
{
// method downloads reports and returns a list of filePaths
// startTime =>
// endTime =<
List reportIdList = new List();
DateTime now = DateTime.UtcNow;
// time checks
if (startTime.ToUniversalTime() > now.ToUniversalTime())
{
MiscFunction.EventLogInsert(
"The most current date supplied from the database is a head of the current time. stoping...",
1,
MiscFunction.TraceMessage()
);
reportIdList.Clear();
return reportIdList;
}
else if (endTime.ToUniversalTime() > now.ToUniversalTime())
{
MiscFunction.EventLogInsert(
"Get report end time is ahead of current time, re-setting to current time",
2
);
}
int reportCount = 0;
bool stopFlag = false;
endTime = startTime.AddDays(reportMaxPeriod);
do
{
reportCount = reportCount + 1;
now = DateTime.UtcNow.AddMinutes(0);
if (endTime.ToUniversalTime() > now.ToUniversalTime())
{
endTime = now;
stopFlag = true;
}
// build the request
RequestReportRequest requestReport = new RequestReportRequest();
requestReport.Merchant = merchantId;
requestReport.ReportType = mwsReportEnum;
requestReport.StartDate = startTime.ToUniversalTime();
requestReport.EndDate = endTime.ToUniversalTime();
MiscFunction.EventLogInsert("Requesting '" + mwsReportEnum + "' '" + startTime.ToUniversalTime() + "' to '" + endTime.ToUniversalTime() + "'");
string filePath = GetMwsReportByRequest(requestReport);
if (filePath == "_DONE_NO_DATA_")
{
// do nothing, carry on with next iteration
}
else if (filePath == "_CANCELLED_")
{
/*
/ this is probably MWS higher level throttling when requesting duplicate report
/ for near real time reports waiting 30 minutes
/ for daily reports wait 4 hours
*/
return reportIdList;
}
else if (filePath.Length > 0)
{
reportIdList.Add(filePath);
}
else
{
MiscFunction.EventLogInsert(
"Something went wrong downloading a full list of reports over the defined period. Report downloaded up until this point (if any) will be processed.",
2
);
return reportIdList;
}
// add 30 days onto start date and loop
startTime = endTime.AddSeconds(1);
endTime = endTime.AddDays(reportMaxPeriod);
if (startTime.ToUniversalTime() > now.ToUniversalTime())
{
stopFlag = true;
}
if (reportCount == maxReportCount)
{
stopFlag = true;
}
} while (stopFlag == false);
return reportIdList;
}
public bool SetMwsReportAcknowledgement(string reportId, bool acknowledged = true)
{
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
do
{
try
{
UpdateReportAcknowledgementsRequest request = new UpdateReportAcknowledgementsRequest();
request.Merchant = merchantId;
request.Acknowledged = acknowledged;
var reportIdList = new IdList();
reportIdList.Id.Add(reportId);
request.ReportIdList = reportIdList;
UpdateReportAcknowledgementsResponse response = service.UpdateReportAcknowledgements(request);
if (response.IsSetUpdateReportAcknowledgementsResult())
{
UpdateReportAcknowledgementsResult updateReportAcknowledgementsResult = response.UpdateReportAcknowledgementsResult;
List reportAckList = updateReportAcknowledgementsResult.ReportInfo;
MiscFunction.EventLogInsert("Report '" + reportAckList[0].ReportId + "' Acknowledgement set to '" + reportAckList[0].Acknowledged + "'");
}
return true;
}
catch (MarketplaceWebServiceException ex) when (ex.ErrorCode == "RequestThrottled")
{
MiscFunction.MwsThrottleWait(80);
}
catch (MarketplaceWebServiceException ex)
{
MiscFunction.EventLogInsert(
"MWS 'UpdateReportAcknowledgementsRequest' failed with exception: " + ex.Message,
1,
"Caught Exception: " + ex.Message +
"\r\nResponse Status Code: " + ex.StatusCode +
"\r\nError Code: " + ex.ErrorCode +
"\r\nError Type: " + ex.ErrorType +
"\r\nRequest ID: " + ex.RequestId +
"\r\nResponseHeaderMetadata: " + ex.ResponseHeaderMetadata +
"\r\nXML:\r\n" + ex.XML
);
Console.WriteLine("Caught Exception: " + ex.Message);
Console.WriteLine("Response Status Code: " + ex.StatusCode);
Console.WriteLine("Error Code: " + ex.ErrorCode);
Console.WriteLine("Error Type: " + ex.ErrorType);
Console.WriteLine("Request ID: " + ex.RequestId);
Console.WriteLine("XML: " + ex.XML);
Console.WriteLine("ResponseHeaderMetadata: " + ex.ResponseHeaderMetadata);
Console.ReadKey();
break;
}
} while (true);
return false;
}
public bool ImportReportSettlementData(string sqlConnectionString, string filePath)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
int settlementReportId = 0;
string settlementRef = "";
bool marketPlaceUpdated = false;
decimal settlementAmount = 0m;
int lineNumber = 2;
int lineSkip = 0;
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int indexSettlementId = Array.IndexOf(headers, "settlement-id");
int indexSettlementStartDate = Array.IndexOf(headers, "settlement-start-date");
int indexSettlementEndDate = Array.IndexOf(headers, "settlement-end-date");
int indexDepositDate = Array.IndexOf(headers, "deposit-date");
int indexTotalAmount = Array.IndexOf(headers, "total-amount");
int indexCurrency = Array.IndexOf(headers, "currency");
int indexTransactionType = Array.IndexOf(headers, "transaction-type");
int indexOrderId = Array.IndexOf(headers, "order-id");
int indexMerchantOrderId = Array.IndexOf(headers, "merchant-order-id");
int indexAdjustmentId = Array.IndexOf(headers, "adjustment-id");
int indexShipmentId = Array.IndexOf(headers, "shipment-id");
int indexMarketplaceName = Array.IndexOf(headers, "marketplace-name");
int indexAmountType = Array.IndexOf(headers, "amount-type");
int indexAmountDescription = Array.IndexOf(headers, "amount-description");
int indexAmount = Array.IndexOf(headers, "amount");
int indexFulfillmentId = Array.IndexOf(headers, "fulfillment-id");
// int indexPostedDate = Array.IndexOf(headers, "posted-date");
int indexPostedDateTime = Array.IndexOf(headers, "posted-date-time");
int indexOrderItemCode = Array.IndexOf(headers, "order-item-code");
int indexMerchantOrderItemId = Array.IndexOf(headers, "merchant-order-item-id");
int indexMerchantAdjustmentItemId = Array.IndexOf(headers, "merchant-adjustment-item-id");
int indexSku = Array.IndexOf(headers, "sku");
int indexQuantityPurchased = Array.IndexOf(headers, "quantity-purchased");
int indexPromotionId = Array.IndexOf(headers, "promotion-id");
string currency = "";
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineSkip = lineSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else if (lineNumber == 2)
{
// check if settlement has already been imported
using (SqlCommand sqlCommand = new SqlCommand(
"SELECT COUNT(*) FROM tblImportAmazonSettlementReport WHERE [settlement-id]=@settlementId;"
, sqlConn))
{
sqlCommand.Parameters.AddWithValue("@settlementId", long.Parse(items[indexSettlementId]));
int recordCount = (int)sqlCommand.ExecuteScalar();
if (recordCount > 0)
{
MiscFunction.ConsoleUpdate("Settlement report already imported, skipping...");
return true;
}
}
//set currencyId
//currencyId = GeneralQueries.GetCurrencyId(items[5]);
//set currency
currency = items[indexCurrency];
settlementAmount = decimal.Parse(items[indexTotalAmount].Replace(",", "."));
// insert
using (SqlCommand sqlCommand = new SqlCommand(
"INSERT INTO tblImportAmazonSettlementReport ( " +
"[settlement-id], [settlement-start-date], [settlement-end-date], [deposit-date], [total-amount], [currency] ) " +
"OUTPUT INSERTED.ImportAmazonSettlementReportID " +
"VALUES ( @settlementId, @settlementStartDate, @settlementEndDate, @depositDate, @settlementotalAmounttId, @currency );"
, sqlConn))
{
// add parameters
if (indexSettlementId == -1 || items[indexSettlementId].Length == 0) { sqlCommand.Parameters.AddWithValue("@settlementId", DBNull.Value); }
else
{
settlementRef = items[indexSettlementId];
sqlCommand.Parameters.AddWithValue("@settlementId", settlementRef);
}
if (indexSettlementStartDate == -1 || items[indexSettlementStartDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@settlementStartDate", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@settlementStartDate", MiscFunction.ParseMwsReportDateTime(items[indexSettlementStartDate])); }
if (indexSettlementEndDate == -1 || items[indexSettlementEndDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@settlementEndDate", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@settlementEndDate", MiscFunction.ParseMwsReportDateTime(items[indexSettlementEndDate])); }
if (indexDepositDate == -1 || items[indexDepositDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@depositDate", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@depositDate", MiscFunction.ParseMwsReportDateTime(items[indexDepositDate])); }
if (indexTotalAmount == -1 || items[indexTotalAmount].Length == 0) { sqlCommand.Parameters.AddWithValue("@totalAmount", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@settlementotalAmounttId", settlementAmount); }
sqlCommand.Parameters.AddWithValue("@currency", currency);
//if (currencyId == -1) { sqlCommand.Parameters.AddWithValue("@currencyId", DBNull.Value); }
//else { sqlCommand.Parameters.AddWithValue("@currencyId", currencyId); }
//execute and retrive id
settlementReportId = (int)sqlCommand.ExecuteScalar();
}
}
else
{
//update market place name in main table, if required
if (marketPlaceUpdated == false && settlementReportId > 0 && items[indexMarketplaceName].Length > 1)
{
using (SqlCommand sqlCommand = new SqlCommand(@"
UPDATE tblImportAmazonSettlementReport
SET [marketplace-name]=@MarketplaceName
WHERE ImportAmazonSettlementReportID=@ImportAmazonSettlementReportID
", sqlConn))
{
sqlCommand.Parameters.AddWithValue("@MarketplaceName", items[indexMarketplaceName]);
sqlCommand.Parameters.AddWithValue("@ImportAmazonSettlementReportID", settlementReportId);
sqlCommand.ExecuteNonQuery();
marketPlaceUpdated = true;
}
}
//insert report items
using (SqlCommand sqlCommand = new SqlCommand(
"INSERT INTO tblImportAmazonSettlementReportLine ( " +
"ImportAmazonSettlementReportID, [transaction-type], [order-id], [merchant-order-id], [adjustment-id], [shipment-id], [marketplace-name], " +
"[amount-type], [amount-description], [currency], [amount], [fulfillment-id], [posted-date-time], [order-item-code], " +
"[merchant-order-item-id], [merchant-adjustment-item-id], [sku], [quantity-purchased], [promotion-id] ) " +
"VALUES ( " +
"@ImportAmazonSettlementReportID, @TransactionType, @orderRef, @merchantOrderRef, @AdjustmentRef, @ShipmentRef, @MarketplaceName, " +
"@AmountType, @AmountDescription, @currency, @Amount, @FulfillmentRef, @PostedDateTimeUTC, @OrderItemCode, " +
"@MerchantOrderItemRef, @MerchantAdjustmentItemRef, @SkuNumber, @QuantityPurchased, @PromotionRef );"
, sqlConn))
{
// add parameters
sqlCommand.Parameters.AddWithValue("@ImportAmazonSettlementReportID", settlementReportId);
sqlCommand.Parameters.AddWithValue("@currency", currency);
if (indexTransactionType == -1 || items[indexTransactionType].Length == 0) { sqlCommand.Parameters.AddWithValue("@TransactionType", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@TransactionType", items[indexTransactionType]); }
if (indexOrderId == -1 || items[indexOrderId].Length == 0) { sqlCommand.Parameters.AddWithValue("@orderRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@orderRef", items[indexOrderId]); }
if (indexMerchantOrderId == -1 || items[indexMerchantOrderId].Length == 0) { sqlCommand.Parameters.AddWithValue("@merchantOrderRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@merchantOrderRef", items[indexMerchantOrderId]); }
if (indexAdjustmentId == -1 || items[indexAdjustmentId].Length == 0) { sqlCommand.Parameters.AddWithValue("@AdjustmentRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@AdjustmentRef", items[indexAdjustmentId]); }
if (indexShipmentId == -1 || items[indexShipmentId].Length == 0) { sqlCommand.Parameters.AddWithValue("@ShipmentRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@ShipmentRef", items[indexShipmentId]); }
if (indexMarketplaceName == -1 || items[indexMarketplaceName].Length == 0) { sqlCommand.Parameters.AddWithValue("@MarketplaceName", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@MarketplaceName", items[indexMarketplaceName]); }
if (indexAmountType == -1 || items[indexAmountType].Length == 0) { sqlCommand.Parameters.AddWithValue("@AmountType", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@AmountType", items[indexAmountType]); }
if (indexAmountDescription == -1 || items[indexAmountDescription].Length == 0) { sqlCommand.Parameters.AddWithValue("@AmountDescription", DBNull.Value); }
else
{
string amountDescription = items[indexAmountDescription];
if (amountDescription.Length > 100) { amountDescription = amountDescription.Substring(0, 100); }
sqlCommand.Parameters.AddWithValue("@AmountDescription", amountDescription);
}
if (indexAmount == -1 || items[indexAmount].Length == 0) { sqlCommand.Parameters.AddWithValue("@Amount", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@Amount", decimal.Parse(items[indexAmount].Replace(",", "."))); }
if (indexFulfillmentId == -1 || items[indexFulfillmentId].Length == 0) { sqlCommand.Parameters.AddWithValue("@FulfillmentRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@FulfillmentRef", items[indexFulfillmentId]); }
if (indexPostedDateTime == -1 || items[indexPostedDateTime].Length == 0) { sqlCommand.Parameters.AddWithValue("@PostedDateTimeUTC", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@PostedDateTimeUTC", MiscFunction.ParseMwsReportDateTime(items[indexPostedDateTime])); }
if (indexOrderItemCode == -1 || items[indexOrderItemCode].Length == 0) { sqlCommand.Parameters.AddWithValue("@OrderItemCode", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@OrderItemCode", long.Parse(items[indexOrderItemCode])); }
if (indexMerchantOrderItemId == -1 || items[indexMerchantOrderItemId].Length == 0) { sqlCommand.Parameters.AddWithValue("@MerchantOrderItemRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@MerchantOrderItemRef", long.Parse(items[indexMerchantOrderItemId])); }
if (indexMerchantAdjustmentItemId == -1 || items[indexMerchantAdjustmentItemId].Length == 0) { sqlCommand.Parameters.AddWithValue("@MerchantAdjustmentItemRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@MerchantAdjustmentItemRef", items[indexMerchantAdjustmentItemId]); }
if (indexSku == -1 || items[indexSku].Length == 0) { sqlCommand.Parameters.AddWithValue("@SkuNumber", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@SkuNumber", items[indexSku]); }
if (indexQuantityPurchased == -1 || items[indexQuantityPurchased].Length == 0) { sqlCommand.Parameters.AddWithValue("@QuantityPurchased", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@QuantityPurchased", int.Parse(items[indexQuantityPurchased])); }
if (indexPromotionId == -1 || items[indexPromotionId].Length == 0) { sqlCommand.Parameters.AddWithValue("@PromotionRef", DBNull.Value); }
else { sqlCommand.Parameters.AddWithValue("@PromotionRef", items[indexPromotionId]); }
sqlCommand.ExecuteNonQuery();
}
}
lineNumber = lineNumber + 1;
}
}
//final check - settlement amount matches sum of inserted settlement lines
using (SqlCommand sqlCommand = new SqlCommand(@"
SELECT Sum(tblImportAmazonSettlementReportLine.amount) AS SumOfAmount
FROM tblImportAmazonSettlementReportLine
WHERE ImportAmazonSettlementReportID=@ImportAmazonSettlementReportID;
", sqlConn))
{
decimal sumOfAmount = -1.12345m;
sqlCommand.Parameters.AddWithValue("@ImportAmazonSettlementReportID", settlementReportId);
sumOfAmount = (decimal)sqlCommand.ExecuteScalar();
if (sumOfAmount != settlementAmount)
{
MiscFunction.EventLogInsert("Error importing settlement id'" + settlementRef + "'. Sum of inserted settlement lines (" + sumOfAmount +
") does not match settlement amount (" + settlementAmount + ").", 1);
return false;
}
}
scope.Complete();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (2 + lineSkip)) + " total settlement items inserted");
if (lineSkip > 0)
{
MiscFunction.EventLogInsert(lineSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
return false;
}
return true;
}
public bool ImportReportFbaInventoryReceipt(string sqlConnectionString, string filePath, DateTime startDate)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineOutsideScope = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
// create notification if amazon add extra headers
if (columnCount > 7)
{
MiscFunction.EventLogInsert(
"Amazon have added a new column to their 'Fba Inventory Receipt' report, you may want to check this out.",
2);
}
int indexOfReceivedDate = Array.IndexOf(headers, "received-date");
int indexOfFnsku = Array.IndexOf(headers, "fnsku");
int indexOfSku = Array.IndexOf(headers, "sku");
int indexOfProductName = Array.IndexOf(headers, "product-name");
int indexOfQuantity = Array.IndexOf(headers, "quantity");
int indexOfFbaShipmentId = Array.IndexOf(headers, "fba-shipment-id");
int indexOfFulfillmentCenterId = Array.IndexOf(headers, "fulfillment-center-id");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
DateTime receivedDate = DateTime.Parse(
items[indexOfReceivedDate],
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
//ensure line has recieved date <= startdate
// due to mws bug, downloaded report can contain records outside of the requested scope
if(receivedDate < startDate)
{
lineOutsideScope = lineOutsideScope + 1;
continue;
}
string fnsku = items[indexOfFnsku];
string sku = items[indexOfSku];
int quantity = int.Parse(items[indexOfQuantity]);
string fbaShipemntId = items[indexOfFbaShipmentId];
string fulfillmentCenterId = items[indexOfFulfillmentCenterId];
//insert report items
using (SqlCommand insertCmd = new SqlCommand(
"INSERT INTO tblImportFbaInventoryReceiptReport ( " +
"[received-date], [fnsku], [sku], [quantity], [fba-shipment-id], [fulfillment-center-id] ) " +
"VALUES ( " +
"@receivedDate, @fnsku, @sku, @quantity, @FbaShipmentId, @FulfillmentCenterId );"
, sqlConn, trans))
{
// add parameters
insertCmd.Parameters.AddWithValue("@receivedDate", receivedDate.ToUniversalTime());
insertCmd.Parameters.AddWithValue("@fnsku", fnsku);
insertCmd.Parameters.AddWithValue("@sku", sku);
insertCmd.Parameters.AddWithValue("@quantity", quantity);
insertCmd.Parameters.AddWithValue("@fbaShipmentId", fbaShipemntId);
insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId);
insertCmd.ExecuteNonQuery();
}
////check for duplicate line in db
//using (SqlCommand cmd = new SqlCommand(
// "SELECT ImportFbaInventoryReceiptReportID FROM tblImportFbaInventoryReceiptReport " +
// "WHERE [received-date]=@receivedDate AND [fnsku]=@fnsku AND [fba-shipment-id]=@fbaShipmentId AND [fulfillment-center-id]=@fulfillmentCenterId;"
// , sqlConn, trans))
//{
// cmd.Parameters.AddWithValue("@receivedDate", receivedDate);
// cmd.Parameters.AddWithValue("@fnsku", fnsku);
// cmd.Parameters.AddWithValue("@fbaShipmentId", fbaShipemntId);
// cmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId);
// using (SqlDataReader sqlReader = cmd.ExecuteReader())
// {
// if (sqlReader.HasRows == true)
// {
// lineDuplicateSkip = lineDuplicateSkip + 1;
// }
// else
// {
// }
// }
//}
}
}
// only commit if records all complete with no errors -- ommiting duplcates relies on all records from one day being committed together
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (1 + lineErrorSkip + lineOutsideScope)) + " total report items inserted into db, " + lineOutsideScope + " item(s) outside of requested time scope where skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running FbaInventoryReceiptReportImport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public bool ImportReportFbaSaleShipment(string sqlConnectionString, string filePath)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineDuplicateSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "amazon-order-id");
int index02 = Array.IndexOf(headers, "merchant-order-id");
int index03 = Array.IndexOf(headers, "shipment-id");
int index04 = Array.IndexOf(headers, "shipment-item-id");
int index05 = Array.IndexOf(headers, "amazon-order-item-id");
int index06 = Array.IndexOf(headers, "merchant-order-item-id");
int index07 = Array.IndexOf(headers, "purchase-date");
int index08 = Array.IndexOf(headers, "payments-date");
int index09 = Array.IndexOf(headers, "shipment-date");
int index10 = Array.IndexOf(headers, "reporting-date");
int index11 = Array.IndexOf(headers, "buyer-email");
int index12 = Array.IndexOf(headers, "buyer-name");
int index13 = Array.IndexOf(headers, "sku");
int index14 = Array.IndexOf(headers, "quantity-shipped");
int index15 = Array.IndexOf(headers, "currency");
int index16 = Array.IndexOf(headers, "item-price");
int index17 = Array.IndexOf(headers, "item-tax");
int index18 = Array.IndexOf(headers, "shipping-price");
int index19 = Array.IndexOf(headers, "shipping-tax");
int index20 = Array.IndexOf(headers, "gift-wrap-price");
int index21 = Array.IndexOf(headers, "gift-wrap-tax");
int index22 = Array.IndexOf(headers, "recipient-name");
int index23 = Array.IndexOf(headers, "ship-address-1");
int index24 = Array.IndexOf(headers, "ship-address-2");
int index25 = Array.IndexOf(headers, "ship-address-3");
int index26 = Array.IndexOf(headers, "ship-city");
int index27 = Array.IndexOf(headers, "ship-state");
int index28 = Array.IndexOf(headers, "ship-postal-code");
int index29 = Array.IndexOf(headers, "ship-country");
int index30 = Array.IndexOf(headers, "ship-phone-number");
int index31 = Array.IndexOf(headers, "bill-address-1");
int index32 = Array.IndexOf(headers, "bill-address-2");
int index33 = Array.IndexOf(headers, "bill-address-3");
int index34 = Array.IndexOf(headers, "bill-city");
int index35 = Array.IndexOf(headers, "bill-state");
int index36 = Array.IndexOf(headers, "bill-postal-code");
int index37 = Array.IndexOf(headers, "bill-country");
int index38 = Array.IndexOf(headers, "item-promotion-discount");
int index39 = Array.IndexOf(headers, "ship-promotion-discount");
int index40 = Array.IndexOf(headers, "fulfillment-center-id");
int index41 = Array.IndexOf(headers, "fulfillment-channel");
int index42 = Array.IndexOf(headers, "sales-channel");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string amazonOrderId = items[index01];
string merchantOrderid = items[index02];
string shipmentId = items[index03];
string shipmentItemId = items[index04];
string amazonOrderItemId = items[index05];
string merchantOrderItemId = items[index06];
DateTime purchaseDate = DateTime.Parse(items[index07]);
DateTime paymentsDate = DateTime.Parse(items[index08]);
DateTime shipmentDate = DateTime.Parse(items[index09]);
DateTime reportingDate = DateTime.Parse(items[index10]);
string buyerEmail = items[index11];
string buyerName = items[index12];
string sku = items[index13];
int quantityShipped = Int32.Parse(items[index14]);
string currency = items[index15];
decimal itemPrice = decimal.Parse(items[index16]);
decimal itemTax = decimal.Parse(items[index17]);
decimal shippingPrice = decimal.Parse(items[index18]);
decimal shippingTax = decimal.Parse(items[index19]);
decimal giftWrapPrice = decimal.Parse(items[index20]);
decimal giftWrapTax = decimal.Parse(items[index21]);
string recipientName = items[index22];
string shipAddress1 = items[index23];
string shipAddress2 = items[index24];
string shipAddress3 = items[index25];
string shipCity = items[index26];
string shipState = items[index27];
string shipPostalCode = items[index28];
string shipCountry = items[index29];
string shipPhoneNumber = items[index30];
string billAddress1 = items[index31];
string billAddress2 = items[index32];
string billAddress3 = items[index33];
string billCity = items[index34];
string billState = items[index35];
string billPostalCode = items[index36];
string billCountry = items[index37];
string itemPromotionDiscount = items[index38];
string shipPromotionDiscount = items[index39];
string fulfillmentCenterId = items[index40];
string fulfillmentChannel = items[index41];
string salesChannel = items[index42];
//check for duplicate line in db
using (SqlCommand cmd = new SqlCommand(
"SELECT ImportFbaSaleShipmentID FROM tblImportFbaSaleShipment " +
"WHERE [shipment-item-id]=@shipmentItemId;"
, sqlConn, trans))
{
cmd.Parameters.AddWithValue("@shipmentItemId", shipmentItemId);
using (SqlDataReader sqlReader = cmd.ExecuteReader())
{
if (sqlReader.Read())
{
lineDuplicateSkip = lineDuplicateSkip + 1;
}
else
{
//insert report items
//start transaction
using (SqlCommand insertCmd = new SqlCommand(
"INSERT INTO tblImportFbaSaleShipment ( " +
"[amazon-order-id], [merchant-order-id], [shipment-id], [shipment-item-id], [amazon-order-item-id], " +
"[merchant-order-item-id], [purchase-date], [payments-date], [shipment-date], [reporting-date], " +
"[buyer-email], [buyer-name], sku, [quantity-shipped], currency, " +
"[item-price], [item-tax], [shipping-price], [shipping-tax], [gift-wrap-price], " +
"[gift-wrap-tax], [recipient-name], [ship-address-1], [ship-address-2], [ship-address-3], " +
"[ship-city], [ship-state], [ship-postal-code], [ship-country], [ship-phone-number], " +
"[bill-address-1], [bill-address-2], [bill-address-3], [bill-city], [bill-state], " +
"[bill-postal-code], [bill-country], [item-promotion-discount], [ship-promotion-discount], [fulfillment-center-id], " +
"[fulfillment-channel], [sales-channel] ) " +
"VALUES ( " +
"@amazonOrderId, @merchantOrderid, @shipmentId, @shipmentItemId, @amazonOrderItemId, " +
"@merchantOrderItemId, @purchaseDate, @paymentsDate, @shipmentDate, @reportingDate, " +
"@buyerEmail, @buyerName, @sku, @quantityShipped, @currency, " +
"@itemPrice, @itemTax, @shippingPrice, @shippingTax, @giftWrapPrice, " +
"@giftWrapTax, @recipientName, @shipAddress1, @shipAddress2, @shipAddress3, " +
"@shipCity, @shipState, @shipPostalCode, @shipCountry, @shipPhoneNumber, " +
"@billAddress1, @billAddress2, @billAddress3, @billCity, @billState, " +
"@billPostalCode, @billCountry, @itemPromotionDiscount, @shipPromotionDiscount, @fulfillmentCenterId, " +
"@fulfillmentChannel, @salesChannel );"
, sqlConn, trans))
{
// add parameters
if (amazonOrderId == "") { insertCmd.Parameters.AddWithValue("@amazonOrderId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@amazonOrderId", amazonOrderId); }
if (merchantOrderid == "") { insertCmd.Parameters.AddWithValue("@merchantOrderid", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@merchantOrderid", merchantOrderid); }
if (shipmentId == "") { insertCmd.Parameters.AddWithValue("@shipmentId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipmentId", shipmentId); }
if (shipmentItemId == "") { insertCmd.Parameters.AddWithValue("@shipmentItemId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipmentItemId", shipmentItemId); }
if (amazonOrderItemId == "") { insertCmd.Parameters.AddWithValue("@amazonOrderItemId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@amazonOrderItemId", amazonOrderItemId); }
if (merchantOrderItemId == "") { insertCmd.Parameters.AddWithValue("@merchantOrderItemId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@merchantOrderItemId", merchantOrderItemId); }
if (purchaseDate == DateTime.MinValue) { insertCmd.Parameters.AddWithValue("@purchaseDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@purchaseDate", purchaseDate.ToUniversalTime()); }
if (paymentsDate == DateTime.MinValue) { insertCmd.Parameters.AddWithValue("@paymentsDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@paymentsDate", paymentsDate.ToUniversalTime()); }
if (shipmentDate == DateTime.MinValue) { insertCmd.Parameters.AddWithValue("@shipmentDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipmentDate", shipmentDate.ToUniversalTime()); }
if (reportingDate == DateTime.MinValue) { insertCmd.Parameters.AddWithValue("@reportingDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@reportingDate", reportingDate.ToUniversalTime()); }
if (buyerEmail == "") { insertCmd.Parameters.AddWithValue("@buyerEmail", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@buyerEmail", buyerEmail); }
if (buyerName == "") { insertCmd.Parameters.AddWithValue("@buyerName", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@buyerName", buyerName); }
if (sku == "") { insertCmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@sku", sku); }
insertCmd.Parameters.AddWithValue("@quantityShipped", quantityShipped);
if (currency == "") { insertCmd.Parameters.AddWithValue("@currency", DBNull.Value); }
insertCmd.Parameters.AddWithValue("@currency", currency);
insertCmd.Parameters.AddWithValue("@itemPrice", itemPrice);
insertCmd.Parameters.AddWithValue("@itemTax", itemTax);
insertCmd.Parameters.AddWithValue("@shippingPrice", shippingPrice);
insertCmd.Parameters.AddWithValue("@shippingTax", shippingTax);
insertCmd.Parameters.AddWithValue("@giftWrapPrice", giftWrapPrice);
insertCmd.Parameters.AddWithValue("@giftWrapTax", giftWrapTax);
if (recipientName == "") { insertCmd.Parameters.AddWithValue("@recipientName", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@recipientName", recipientName); }
if (shipAddress1 == "") { insertCmd.Parameters.AddWithValue("@shipAddress1", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipAddress1", shipAddress1); }
if (shipAddress2 == "") { insertCmd.Parameters.AddWithValue("@shipAddress2", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipAddress2", shipAddress2); }
if (shipAddress3 == "") { insertCmd.Parameters.AddWithValue("@shipAddress3", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipAddress3", shipAddress3); }
if (shipCity == "") { insertCmd.Parameters.AddWithValue("@shipCity", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipCity", shipCity); }
if (shipState == "") { insertCmd.Parameters.AddWithValue("@shipState", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipState", shipState); }
if (shipPostalCode == "") { insertCmd.Parameters.AddWithValue("@shipPostalCode", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipPostalCode", shipPostalCode); }
if (shipCountry == "") { insertCmd.Parameters.AddWithValue("@shipCountry", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipCountry", shipCountry); }
if (shipPhoneNumber == "") { insertCmd.Parameters.AddWithValue("@shipPhoneNumber", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipPhoneNumber", shipPhoneNumber); }
if (billAddress1 == "") { insertCmd.Parameters.AddWithValue("@billAddress1", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billAddress1", billAddress1); }
if (billAddress2 == "") { insertCmd.Parameters.AddWithValue("@billAddress2", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billAddress2", billAddress2); }
if (billAddress3 == "") { insertCmd.Parameters.AddWithValue("@billAddress3", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billAddress3", billAddress3); }
if (billCity == "") { insertCmd.Parameters.AddWithValue("@billCity", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billCity", billCity); }
if (billState == "") { insertCmd.Parameters.AddWithValue("@billState", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billState", billState); }
if (billPostalCode == "") { insertCmd.Parameters.AddWithValue("@billPostalCode", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billPostalCode", billPostalCode); }
if (billCountry == "") { insertCmd.Parameters.AddWithValue("@billCountry", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@billCountry", billCountry); }
if (itemPromotionDiscount == "") { insertCmd.Parameters.AddWithValue("@itemPromotionDiscount", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@itemPromotionDiscount", itemPromotionDiscount); }
if (shipPromotionDiscount == "") { insertCmd.Parameters.AddWithValue("@shipPromotionDiscount", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@shipPromotionDiscount", shipPromotionDiscount); }
if (fulfillmentCenterId == "") { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId); }
if (fulfillmentChannel == "") { insertCmd.Parameters.AddWithValue("@fulfillmentChannel", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fulfillmentChannel", fulfillmentChannel); }
if (salesChannel == "") { insertCmd.Parameters.AddWithValue("@salesChannel", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@salesChannel", salesChannel); }
insertCmd.ExecuteNonQuery();
}
}
}
}
}
}
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (1 + lineErrorSkip + lineDuplicateSkip)) + " total new items inserted, " + lineDuplicateSkip + " duplicates were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running FbaInventoryReceiptReportImport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public bool ImportReportFbaReturns(string sqlConnectionString, string filePath)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineDuplicateSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "return-date");
int index02 = Array.IndexOf(headers, "order-id");
int index03 = Array.IndexOf(headers, "sku");
int index04 = Array.IndexOf(headers, "asin");
int index05 = Array.IndexOf(headers, "fnsku");
int index06 = Array.IndexOf(headers, "quantity");
int index07 = Array.IndexOf(headers, "fulfillment-center-id");
int index08 = Array.IndexOf(headers, "detailed-disposition");
int index09 = Array.IndexOf(headers, "reason");
int index10 = Array.IndexOf(headers, "status");
int index11 = Array.IndexOf(headers, "license-plate-number");
int index12 = Array.IndexOf(headers, "customer-comments");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string returnDate = items[index01];
string orderId = items[index02];
string sku = items[index03];
string asin = items[index04];
string fnsku = items[index05];
string quantity = items[index06];
string fulfillmentCenterId = items[index07];
string detailedDisposition = items[index08];
string reason = items[index09];
string status = items[index10];
string licensePlateNumber = items[index11];
string customerComments = items[index12];
// check number of times line conbination appears in file
int fileCount = 0;
int dbCount = 0;
using (var dupReader = new StreamReader(filePath))
{
dupReader.ReadLine(); // read header row
string dupFileRow;
while ((dupFileRow = dupReader.ReadLine()) != null)
{
string[] dupItems = dupFileRow.Split('\t');
if (dupItems.Length != columnCount)
{
// skip
}
else
{
if (items[index01] == dupItems[index01] &&
items[index02] == dupItems[index02] &&
items[index05] == dupItems[index05] &&
items[index11] == dupItems[index11])
{
// count will always find at least one (itself)
fileCount = fileCount + 1;
}
}
}
}
//check for duplicate line in db
using (SqlCommand cmd = new SqlCommand(
"SELECT COUNT(ImportFbaCustomerReturnID) FROM tblImportFbaCustomerReturn " +
"WHERE [return-date]=@returnDate AND [order-id]=@orderId AND [fnsku]=@fnsku " +
"AND ([license-plate-number]=@licensePlateNumber OR [license-plate-number] IS NULL) ;"
, sqlConn, trans))
{
if (returnDate == "") { cmd.Parameters.AddWithValue("@returnDate", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@returnDate", DateTime.Parse(returnDate).ToUniversalTime()); }
if (orderId == "") { cmd.Parameters.AddWithValue("@orderId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@orderId", orderId); }
if (fnsku == "") { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (licensePlateNumber == "") { cmd.Parameters.AddWithValue("@licensePlateNumber", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@licensePlateNumber", licensePlateNumber); }
dbCount = (int)cmd.ExecuteScalar();
}
if (fileCount <= dbCount)
{
//skip
lineDuplicateSkip = lineDuplicateSkip + 1;
}
else
{
//insert report items
using (SqlCommand insertCmd = new SqlCommand(
"INSERT INTO tblImportFbaCustomerReturn ( " +
"[return-date], [order-id], sku, asin, fnsku, " +
"quantity, [fulfillment-center-id], [detailed-disposition], reason, status, " +
"[license-plate-number], [customer-comments] ) " +
"VALUES ( " +
"@returnDate, @orderId, @sku, @asin, @fnsku, " +
"@quantity, @fulfillmentCenterId, @detailedDisposition, @reason, @status, " +
"@licensePlateNumber, @customerComments );"
, sqlConn, trans))
{
// add parameters
if (returnDate == "") { insertCmd.Parameters.AddWithValue("@returnDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@returnDate", DateTime.Parse(returnDate).ToUniversalTime()); }
if (orderId == "") { insertCmd.Parameters.AddWithValue("@orderId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@orderId", orderId); }
if (sku == "") { insertCmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@sku", sku); }
if (asin == "") { insertCmd.Parameters.AddWithValue("@asin", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@asin", asin); }
if (fnsku == "") { insertCmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (quantity == "") { insertCmd.Parameters.AddWithValue("@quantity", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@quantity", int.Parse(quantity)); }
if (fulfillmentCenterId == "") { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId); }
if (detailedDisposition == "") { insertCmd.Parameters.AddWithValue("@detailedDisposition", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@detailedDisposition", detailedDisposition); }
if (reason == "") { insertCmd.Parameters.AddWithValue("@reason", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@reason", reason); }
if (status == "") { insertCmd.Parameters.AddWithValue("@status", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@status", status); }
if (licensePlateNumber == "") { insertCmd.Parameters.AddWithValue("@licensePlateNumber", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@licensePlateNumber", licensePlateNumber); }
if (customerComments == "") { insertCmd.Parameters.AddWithValue("@customerComments", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@customerComments", customerComments); }
insertCmd.ExecuteNonQuery();
}
}
}
}
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (1 + lineErrorSkip + lineDuplicateSkip)) + " total new items inserted, " + lineDuplicateSkip + " duplicates were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running FbaInventoryReceiptReportImport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public bool ImportReportFbaAdjustment(string sqlConnectionString, string filePath)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineDuplicateSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "adjusted-date");
int index02 = Array.IndexOf(headers, "transaction-item-id");
int index03 = Array.IndexOf(headers, "fnsku");
int index04 = Array.IndexOf(headers, "sku");
int index05 = Array.IndexOf(headers, "product-name");
int index06 = Array.IndexOf(headers, "fulfillment-center-id");
int index07 = Array.IndexOf(headers, "quantity");
int index08 = Array.IndexOf(headers, "reason");
int index09 = Array.IndexOf(headers, "disposition");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string adjustmentDate = items[index01];
string transactionItemId = items[index02];
string fnsku = items[index03];
string sku = items[index04];
string fulfillmentCenterId = items[index06];
string quantity = items[index07];
string reason = items[index08];
string disposition = items[index09];
// check number of times line combination appears in file
// don't think is a nesseary step, the transactionItemId is a unique identifier, I'm 99% sure
int fileCount = 0;
int dbCount = 0;
using (var dupReader = new StreamReader(filePath))
{
dupReader.ReadLine(); // read header row
string dupFileRow;
while ((dupFileRow = dupReader.ReadLine()) != null)
{
string[] dupItems = dupFileRow.Split('\t');
if (dupItems.Length != columnCount)
{
// skip
}
else
{
if (items[index01] == dupItems[index01] &&
items[index02] == dupItems[index02] &&
items[index03] == dupItems[index03] &&
items[index06] == dupItems[index06] &&
items[index08] == dupItems[index08] &&
items[index09] == dupItems[index09]
)
{
// count will always find at least one (itself)
fileCount = fileCount + 1;
}
}
}
}
//check for duplicate line in db
//using (SqlCommand cmd = new SqlCommand(
// "SELECT COUNT(ImportFbaInventoryAdjustmentReportID) FROM tblImportFbaInventoryAdjustmentReport " +
// "WHERE [adjusted-date]=@adjustmentDate AND [transaction-item-id]=@transactionItemId AND [fnsku]=@fnsku " +
// " AND [fulfillment-center-id]=@fulfillmentCenterId AND [reason]=@reason AND [disposition]=@disposition;"
// , sqlConn, trans))
//{
// if (adjustmentDate == "") { cmd.Parameters.AddWithValue("@adjustmentDate", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@adjustmentDate", DateTime.Parse(adjustmentDate).ToUniversalTime()); }
// if (transactionItemId == "") { cmd.Parameters.AddWithValue("@transactionItemId", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@transactionItemId", transactionItemId); }
// if (fnsku == "") { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
// if (fulfillmentCenterId == "") { cmd.Parameters.AddWithValue("@fulfillmentCenterId", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId); }
// if (reason == "") { cmd.Parameters.AddWithValue("@reason", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@reason", reason); }
// if (disposition == "") { cmd.Parameters.AddWithValue("@disposition", DBNull.Value); }
// else { cmd.Parameters.AddWithValue("@disposition", disposition); }
// dbCount = (int)cmd.ExecuteScalar();
//}
using (SqlCommand cmd = new SqlCommand(
"SELECT COUNT(ImportFbaInventoryAdjustmentReportID) FROM tblImportFbaInventoryAdjustmentReport " +
"WHERE [transaction-item-id]=@transactionItemId;"
, sqlConn, trans))
{
cmd.Parameters.AddWithValue("@transactionItemId", transactionItemId);
dbCount = (int)cmd.ExecuteScalar();
}
if (fileCount <= dbCount)
{
//skip
lineDuplicateSkip = lineDuplicateSkip + 1;
}
else
{
//insert report items
using (SqlCommand insertCmd = new SqlCommand(
"INSERT INTO tblImportFbaInventoryAdjustmentReport ( " +
"[adjusted-date], [transaction-item-id], fnsku, sku, " +
"[fulfillment-center-id], quantity, reason, disposition ) " +
"VALUES ( " +
"@adjustmentDate, @transactionItemId, @fnsku, @sku, " +
"@fulfillmentCenterId, @quantity, @reason, @disposition );"
, sqlConn, trans))
{
// add parameters
if (adjustmentDate == "") { insertCmd.Parameters.AddWithValue("@adjustmentDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@adjustmentDate", DateTime.Parse(adjustmentDate).ToUniversalTime()); }
if (transactionItemId == "") { insertCmd.Parameters.AddWithValue("@transactionItemId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@transactionItemId", transactionItemId); }
if (fnsku == "") { insertCmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (sku == "") { insertCmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@sku", sku); }
if (fulfillmentCenterId == "") { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fulfillmentCenterId", fulfillmentCenterId); }
if (quantity == "") { insertCmd.Parameters.AddWithValue("@quantity", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@quantity", int.Parse(quantity)); }
if (reason == "") { insertCmd.Parameters.AddWithValue("@reason", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@reason", reason); }
if (disposition == "") { insertCmd.Parameters.AddWithValue("@disposition", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@disposition", disposition); }
insertCmd.ExecuteNonQuery();
}
}
}
}
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (1 + lineErrorSkip + lineDuplicateSkip)) + " total new items inserted, " + lineDuplicateSkip + " duplicates were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running ImportFbaAdustmentReport method, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public bool ImportReportFbaReimbursement(string sqlConnectionString, string filePath)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineDuplicateSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "approval-date");
int index02 = Array.IndexOf(headers, "reimbursement-id");
int index03 = Array.IndexOf(headers, "case-id");
int index04 = Array.IndexOf(headers, "amazon-order-id");
int index05 = Array.IndexOf(headers, "reason");
int index06 = Array.IndexOf(headers, "sku");
int index07 = Array.IndexOf(headers, "fnsku");
int index08 = Array.IndexOf(headers, "asin");
int index09 = Array.IndexOf(headers, "product-name");
int index10 = Array.IndexOf(headers, "condition");
int index11 = Array.IndexOf(headers, "currency-unit");
int index12 = Array.IndexOf(headers, "amount-per-unit");
int index13 = Array.IndexOf(headers, "amount-total");
int index14 = Array.IndexOf(headers, "quantity-reimbursed-cash");
int index15 = Array.IndexOf(headers, "quantity-reimbursed-inventory");
int index16 = Array.IndexOf(headers, "quantity-reimbursed-total");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string approvalDate = items[index01];
string reimbursementid = items[index02];
string caseid = items[index03];
string amazonOrderId = items[index04];
string reason = items[index05];
string sku = items[index06];
string fnsku = items[index07];
string asin = items[index08];
string productName = items[index09];
string condition = items[index10];
string currencyUnit = items[index11];
string amountPerUnit = items[index12];
string amountTotal = items[index13];
int quantityReimbursedCash = 0;
if (items[index14] != "")
{ quantityReimbursedCash = int.Parse(items[index14]); }
int quantityReimbursedInventory = 0;
if (items[index15] != "")
{ quantityReimbursedInventory = int.Parse(items[index15]); }
int quantityReimbursedTotal = 0;
if (items[index16] != "")
{ quantityReimbursedTotal = int.Parse(items[index16]); }
// totals checks
if (quantityReimbursedTotal == 0)
{
throw new Exception("Total Reimbursed total cannot be zero.");
}
if (quantityReimbursedCash + quantityReimbursedInventory != quantityReimbursedTotal)
{
throw new Exception("Reimbursed totals do not tally.");
}
// check number of times line conbination appears in file
int fileCount = 0;
int dbCount = 0;
using (var dupReader = new StreamReader(filePath))
{
dupReader.ReadLine(); // read header row
string dupFileRow;
while ((dupFileRow = dupReader.ReadLine()) != null)
{
string[] dupItems = dupFileRow.Split('\t');
if (dupItems.Length != columnCount)
{
// skip
}
else
{
if (items[index01] == dupItems[index01] &&
items[index02] == dupItems[index02] &&
items[index03] == dupItems[index03] &&
items[index04] == dupItems[index04] &&
items[index05] == dupItems[index05] &&
items[index07] == dupItems[index07] &&
items[index13] == dupItems[index13]
)
{
// count will always find at least one (itself)
fileCount = fileCount + 1;
}
}
}
}
//check for duplicate line in db
using (SqlCommand cmd = new SqlCommand(@"
SELECT
COUNT(ImportFbaReimbursementReportID) AS number
FROM
tblImportFbaReimbursementReport
WHERE
[reimbursement-id]=@reimbursementid
AND ([case-id]=@caseid OR [case-id] IS NULL)
AND ([amazon-order-id]=@amazonOrderId OR [amazon-order-id] IS NULL)
AND reason=@reason AND fnsku=@fnsku
AND [amount-total]=@amountTotal;
", sqlConn, trans))
{
if (reimbursementid == "") { cmd.Parameters.AddWithValue("@reimbursementid", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@reimbursementid", reimbursementid); }
if (caseid == "") { cmd.Parameters.AddWithValue("@caseid", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@caseid", caseid); }
if (amazonOrderId == "") { cmd.Parameters.AddWithValue("@amazonOrderId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@amazonOrderId", amazonOrderId); }
if (reason == "") { cmd.Parameters.AddWithValue("@reason", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@reason", reason); }
if (fnsku == "") { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (amountTotal == "") { cmd.Parameters.AddWithValue("@amountTotal", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@amountTotal", decimal.Parse(amountTotal)); }
dbCount = (int)cmd.ExecuteScalar();
}
if (fileCount <= dbCount)
{
//skip
lineDuplicateSkip = lineDuplicateSkip + 1;
}
else
{
//insert report items
using (SqlCommand insertCmd = new SqlCommand(
"INSERT INTO tblImportFbaReimbursementReport ( " +
"[approval-date], [reimbursement-id], [case-id], [amazon-order-id], reason, " +
"sku, fnsku, asin, condition, [currency-unit], " +
"[amount-per-unit], [amount-total], [quantity-reimbursed-cash], [quantity-reimbursed-inventory], [quantity-reimbursed-total] )" +
"VALUES ( " +
"@approvalDate, @reimbursementid, @caseid, @amazonOrderId, @reason, " +
"@sku, @fnsku, @asin, @condition, @currencyUnit, " +
"@amountPerUnit, @amountTotal, @quantityReimbursedCash, @quantityReimbursedInventory, @quantityReimbursedTotal );"
, sqlConn, trans))
{
// add parameters
if (approvalDate == "") { insertCmd.Parameters.AddWithValue("@approvalDate", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@approvalDate", DateTime.Parse(approvalDate).ToUniversalTime()); }
if (reimbursementid == "") { insertCmd.Parameters.AddWithValue("@reimbursementid", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@reimbursementid", reimbursementid); }
if (caseid == "") { insertCmd.Parameters.AddWithValue("@caseid", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@caseid", caseid); }
if (amazonOrderId == "") { insertCmd.Parameters.AddWithValue("@amazonOrderId", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@amazonOrderId", amazonOrderId); }
if (reason == "") { insertCmd.Parameters.AddWithValue("@reason", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@reason", reason); }
if (sku == "") { insertCmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@sku", sku); }
if (fnsku == "") { insertCmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (asin == "") { insertCmd.Parameters.AddWithValue("@asin", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@asin", asin); }
if (condition == "") { insertCmd.Parameters.AddWithValue("@condition", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@condition", condition); }
if (currencyUnit == "") { insertCmd.Parameters.AddWithValue("@currencyUnit", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@currencyUnit", currencyUnit); }
if (amountPerUnit == "") { insertCmd.Parameters.AddWithValue("@amountPerUnit", DBNull.Value); }
else { insertCmd.Parameters.AddWithValue("@amountPerUnit", decimal.Parse(amountPerUnit)); }
if (amountTotal == "") { insertCmd.Parameters.AddWithValue("@amountTotal", DBNull.Value); }
else {insertCmd.Parameters.AddWithValue("@amountTotal", decimal.Parse(amountTotal)); }
insertCmd.Parameters.AddWithValue("@quantityReimbursedCash", quantityReimbursedCash);
insertCmd.Parameters.AddWithValue("@quantityReimbursedInventory", quantityReimbursedInventory);
insertCmd.Parameters.AddWithValue("@quantityReimbursedTotal", quantityReimbursedTotal);
insertCmd.ExecuteNonQuery();
}
}
}
}
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert((lineNumber - (1 + lineErrorSkip + lineDuplicateSkip)) + " total new items inserted, " + lineDuplicateSkip + " duplicates were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running ImportFbaReimbursementReport method, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public bool ImportReportFbaRemovalOrder(string sqlConnectionString, string filePath)
{
SqlConnection sqlConn;
SqlTransaction trans;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (trans = sqlConn.BeginTransaction())
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineUpdate = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "request-date");
int index02 = Array.IndexOf(headers, "order-id");
int index03 = Array.IndexOf(headers, "order-type");
int index04 = Array.IndexOf(headers, "service-speed");
int index05 = Array.IndexOf(headers, "order-status");
int index06 = Array.IndexOf(headers, "last-updated-date");
int index07 = Array.IndexOf(headers, "sku");
int index08 = Array.IndexOf(headers, "fnsku");
int index09 = Array.IndexOf(headers, "disposition");
int index10 = Array.IndexOf(headers, "requested-quantity");
int index11 = Array.IndexOf(headers, "cancelled-quantity");
int index12 = Array.IndexOf(headers, "disposed-quantity");
int index13 = Array.IndexOf(headers, "shipped-quantity");
int index14 = Array.IndexOf(headers, "in-process-quantity");
int index15 = Array.IndexOf(headers, "removal-fee");
int index16 = Array.IndexOf(headers, "currency");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string requestDate = items[index01];
string orderId = items[index02];
string orderType = items[index03];
string serviceSpeed = items[index04];
string orderStatus = items[index05];
string lastUpdatedDate = items[index06];
string sku = items[index07];
string fnsku = items[index08];
string disposition = items[index09];
string requestedQuantity = items[index10];
string cancelledQuantity = items[index11];
string disposedQuantity = items[index12];
string shippedQuantity = items[index13];
string inProcessQuantity = items[index14];
string removalFee = items[index15];
string currency = items[index16];
if (orderId == "") { continue; }
int importTableId = 0;
//check for existing orderId
using (SqlCommand cmd = new SqlCommand(
"SELECT ImportFbaRemovalOrderReportID FROM tblImportFbaRemovalOrderReport WHERE [order-id]=@orderId AND fnsku=@fnsku AND disposition=@disposition;"
, sqlConn, trans))
{
cmd.Parameters.AddWithValue("@orderId", orderId);
cmd.Parameters.AddWithValue("@fnsku", fnsku);
cmd.Parameters.AddWithValue("@disposition", disposition);
importTableId = Convert.ToInt32(cmd.ExecuteScalar());
}
string sqlQuery;
if (importTableId > 0)
{
// update
lineUpdate = lineUpdate + 1;
sqlQuery =
"UPDATE tblImportFbaRemovalOrderReport SET " +
"[request-date]=@requestDate, [order-id]=@orderId, [order-type]=@orderType, [service-speed]=@serviceSpeed, " +
"[order-status]=@orderStatus, [last-updated-date]=@lastUpdatedDate, sku=@sku, fnsku=@fnsku, " +
"disposition=@disposition, [requested-quantity]=@requestedQuantity, [cancelled-quantity]=@cancelledQuantity, [disposed-quantity]=@disposedQuantity, " +
"[shipped-quantity]=@shippedQuantity, [in-process-quantity]=@inProcessQuantity, [removal-fee]=@removalFee, currency=@currency " +
"WHERE ImportFbaRemovalOrderReportID=@importTableId;";
}
else
{
// insert
sqlQuery =
"INSERT INTO tblImportFbaRemovalOrderReport ( " +
"[request-date], [order-id], [order-type], [service-speed], [order-status], [last-updated-date], sku, fnsku, " +
"disposition, [requested-quantity], [cancelled-quantity], [disposed-quantity], [shipped-quantity], [in-process-quantity], [removal-fee], currency ) " +
"VALUES ( " +
"@requestDate, @orderId, @orderType, @serviceSpeed, @orderStatus, @lastUpdatedDate, @sku, @fnsku, " +
"@disposition, @requestedQuantity, @cancelledQuantity, @disposedQuantity, @shippedQuantity, @inProcessQuantity, @removalFee, @currency );";
}
// make the update/insert
using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn, trans))
{
// add parameters
cmd.Parameters.AddWithValue("@importTableId", importTableId);
if (requestDate == "") { cmd.Parameters.AddWithValue("@requestDate", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@requestDate", DateTime.Parse(requestDate).ToUniversalTime()); }
if (orderId == "") { cmd.Parameters.AddWithValue("@orderId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@orderId", orderId); }
if (orderType == "") { cmd.Parameters.AddWithValue("@orderType", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@orderType", orderType); }
if (serviceSpeed == "") { cmd.Parameters.AddWithValue("@serviceSpeed", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@serviceSpeed", serviceSpeed); }
if (orderStatus == "") { cmd.Parameters.AddWithValue("@orderStatus", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@orderStatus", orderStatus); }
if (lastUpdatedDate == "") { cmd.Parameters.AddWithValue("@lastUpdatedDate", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@lastUpdatedDate", DateTime.Parse(lastUpdatedDate).ToUniversalTime()); }
if (sku == "") { cmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@sku", sku); }
if (fnsku == "") { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (disposition == "") { cmd.Parameters.AddWithValue("@disposition", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@disposition", disposition); }
if (requestedQuantity == "") { cmd.Parameters.AddWithValue("@requestedQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@requestedQuantity", int.Parse(requestedQuantity)); }
if (cancelledQuantity == "") { cmd.Parameters.AddWithValue("@cancelledQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@cancelledQuantity", int.Parse(cancelledQuantity)); }
if (disposedQuantity == "") { cmd.Parameters.AddWithValue("@disposedQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@disposedQuantity", int.Parse(disposedQuantity)); }
if (shippedQuantity == "") { cmd.Parameters.AddWithValue("@shippedQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@shippedQuantity", int.Parse(shippedQuantity)); }
if (inProcessQuantity == "") { cmd.Parameters.AddWithValue("@inProcessQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@inProcessQuantity", int.Parse(inProcessQuantity)); }
if (removalFee == "") { cmd.Parameters.AddWithValue("@removalFee", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@removalFee", decimal.Parse(removalFee)); }
if (currency == "") { cmd.Parameters.AddWithValue("@currency", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@currency", currency); }
cmd.ExecuteNonQuery();
}
}
}
trans.Commit();
Console.Write("\r");
MiscFunction.EventLogInsert("ImportFbaRemovalOrderReport() " + (lineNumber - (1 + lineErrorSkip + lineUpdate)) + " records created, " + lineUpdate + " records updated.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running ImportFbaRemovalOrderReport method, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return false;
}
return true;
}
public void UpdateFbaInventoryData(string sqlConnectionString)
{
MiscFunction.EventLogInsert("Staring ImportFbaInventoryData()...");
string mwsReportEnum = "_GET_FBA_MYI_ALL_INVENTORY_DATA_";
string filePath = "";
// Uncomment to override getreport and load directly from file
//filePath = @"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\8539341218017503.txt";
// build the request
if (filePath == "")
{
RequestReportRequest requestReport = new RequestReportRequest();
requestReport.Merchant = merchantId;
requestReport.ReportType = mwsReportEnum;
MiscFunction.EventLogInsert("Requesting '" + mwsReportEnum + "''");
filePath = GetMwsReportByRequest(requestReport);
if (filePath == "_DONE_NO_DATA_")
{
// do nothing, carry on with next iteration
}
else if (filePath == "_CANCELLED_")
{
/*
/ this is probably MWS higher level throttling when requesting duplicate report
/ for near real time reports waiting 30 minutes
/ for daily reports wait 4 hours
*/
}
else if (filePath.Length == 0)
{
MiscFunction.EventLogInsert(
"Something went wrong retriving report from MWS. Operation cancelled.",
2
);
return;
}
}
// import into database
SqlConnection sqlConn;
try
{
using (TransactionScope scope = new TransactionScope())
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
//clear table data
using (SqlCommand cmd = new SqlCommand(@"
DELETE FROM tblImportFbaManageInventory;
", sqlConn))
{
cmd.ExecuteNonQuery();
}
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineNoStockSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "sku");
int index02 = Array.IndexOf(headers, "fnsku");
int index03 = Array.IndexOf(headers, "asin");
int index04 = Array.IndexOf(headers, "product-name");
int index05 = Array.IndexOf(headers, "condition");
int index06 = Array.IndexOf(headers, "your-price");
int index07 = Array.IndexOf(headers, "mfn-listing-exists");
int index08 = Array.IndexOf(headers, "mfn-fulfillable-quantity");
int index09 = Array.IndexOf(headers, "afn-listing-exists");
int index10 = Array.IndexOf(headers, "afn-warehouse-quantity");
int index11 = Array.IndexOf(headers, "afn-fulfillable-quantity");
int index12 = Array.IndexOf(headers, "afn-unsellable-quantity");
int index13 = Array.IndexOf(headers, "afn-reserved-quantity");
int index14 = Array.IndexOf(headers, "afn-total-quantity");
int index15 = Array.IndexOf(headers, "per-unit-volume");
int index16 = Array.IndexOf(headers, "afn-inbound-working-quantity");
int index17 = Array.IndexOf(headers, "afn-inbound-shipped-quantity");
int index18 = Array.IndexOf(headers, "afn-inbound-receiving-quantity");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
// only import sku with stock
// afnTotalQuantity includes fba stock and inbound shipments
string afnTotalQuantity = items[index14];
//if (int.Parse(afnTotalQuantity) == 0)
//{
// lineNoStockSkip = lineNoStockSkip + 1;
// continue;
//}
//read values
string sku = items[index01];
string fnsku = items[index02];
string asin = items[index03];
string productName = items[index04];
string condition = items[index05];
string yourPrice = items[index06];
string mfnListingExists = items[index07];
string mfnFulfillableQuantity = items[index08];
string afnListingExists = items[index09];
string afnWarehouseQuantity = items[index10];
string afnFulfillableQuantity = items[index11];
string afnUnsellableQuantity = items[index12];
string afnReservedQuantity = items[index13];
string perUnitVolume = items[index15];
string afnInboundWorkingQuantity = items[index16];
string afnInboundShippedQuantity = items[index17];
string afnInboundReceivingQuantity = items[index18];
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblImportFbaManageInventory(
sku, fnsku, asin, [product-name], condition, [your-price], [mfn-listing-exists], [mfn-fulfillable-quantity],
[afn-listing-exists], [afn-warehouse-quantity], [afn-fulfillable-quantity], [afn-unsellable-quantity],
[afn-reserved-quantity], [afn-total-quantity], [per-unit-volume], [afn-inbound-working-quantity],
[afn-inbound-shipped-quantity], [afn-inbound-receiving-quantity] )
VALUES (
@sku, @fnsku, @asin, @productName, @condition, @yourPrice, @mfnListingExists, @mfnFulfillableQuantity,
@afnListingExists, @afnWarehouseQuantity, @afnFulfillableQuantity, @afnUnsellableQuantity,
@afnReservedQuantity, @afnTotalQuantity, @perUnitVolume, @afnInboundWorkingQuantity,
@afnInboundShippedQuantity, @afnInboundReceivingQuantity );
", sqlConn))
{
// add parameters
cmd.Parameters.AddWithValue("@sku", sku);
if (fnsku.Length == 0) { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (asin.Length == 0) { cmd.Parameters.AddWithValue("@asin", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@asin", asin); }
if (productName.Length == 0) { cmd.Parameters.AddWithValue("@productName", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@productName", productName); }
if (condition.Length == 0) { cmd.Parameters.AddWithValue("@condition", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@condition", condition); }
if (yourPrice.Length == 0) { cmd.Parameters.AddWithValue("@yourPrice", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@yourPrice", decimal.Parse(yourPrice)); }
if (mfnListingExists.Length == 0) { cmd.Parameters.AddWithValue("@mfnListingExists", DBNull.Value); }
else if (mfnListingExists == "Yes") { cmd.Parameters.AddWithValue("@mfnListingExists", true); }
else { cmd.Parameters.AddWithValue("@mfnListingExists", false); }
if (mfnFulfillableQuantity.Length == 0) { cmd.Parameters.AddWithValue("@mfnFulfillableQuantity", 0); }
else { cmd.Parameters.AddWithValue("@mfnFulfillableQuantity", int.Parse(mfnFulfillableQuantity)); }
if (afnListingExists.Length == 0) { cmd.Parameters.AddWithValue("@afnListingExists", DBNull.Value); }
else if (afnListingExists == "Yes") { cmd.Parameters.AddWithValue("@afnListingExists", true); }
else { cmd.Parameters.AddWithValue("@afnListingExists", false); }
if (afnWarehouseQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnWarehouseQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnWarehouseQuantity", int.Parse(afnWarehouseQuantity)); }
if (afnFulfillableQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnFulfillableQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnFulfillableQuantity", int.Parse(afnFulfillableQuantity)); }
if (afnUnsellableQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnUnsellableQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnUnsellableQuantity", int.Parse(afnUnsellableQuantity)); }
if (afnReservedQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnReservedQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnReservedQuantity", int.Parse(afnReservedQuantity)); }
if (afnTotalQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnTotalQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnTotalQuantity", int.Parse(afnTotalQuantity)); }
if (perUnitVolume.Length == 0) { cmd.Parameters.AddWithValue("@perUnitVolume", 0); }
else { cmd.Parameters.AddWithValue("@perUnitVolume", decimal.Parse(perUnitVolume)); }
if (afnInboundWorkingQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnInboundWorkingQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnInboundWorkingQuantity", int.Parse(afnInboundWorkingQuantity)); }
if (afnInboundShippedQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnInboundShippedQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnInboundShippedQuantity", int.Parse(afnInboundShippedQuantity)); }
if (afnInboundReceivingQuantity.Length == 0) { cmd.Parameters.AddWithValue("@afnInboundReceivingQuantity", 0); }
else { cmd.Parameters.AddWithValue("@afnInboundReceivingQuantity", int.Parse(afnInboundReceivingQuantity)); }
// execute the query
cmd.ExecuteNonQuery();
}
}
}
Console.Write("\r");
MiscFunction.EventLogInsert(
"Operation complete. " + lineNumber + " items processes. " + (lineNumber - lineErrorSkip - lineNoStockSkip) + " total new items inserted, "
+ lineNoStockSkip + " 'No Stock' records were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
scope.Complete();
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Something went wrong during import, check details for more.", 1, ex.ToString());
Console.WriteLine(ex.ToString());
}
}
public void UpdateFbaInventoryAgeData(string sqlConnectionString)
{
MiscFunction.EventLogInsert("Staring ImportFbaInventoryAgeReport()...");
string mwsReportEnum = "_GET_FBA_INVENTORY_AGED_DATA_";
string filePath = "";
// Uncomment to override getreport and load directly from file
//filePath = @"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\8539341218017503.txt";
// build the request
if (filePath == "")
{
RequestReportRequest requestReport = new RequestReportRequest();
requestReport.Merchant = merchantId;
requestReport.ReportType = mwsReportEnum;
MiscFunction.EventLogInsert("Requesting '" + mwsReportEnum + "''");
filePath = GetMwsReportByRequest(requestReport);
if (filePath == "_DONE_NO_DATA_")
{
// do nothing, carry on with next iteration
}
else if (filePath == "_CANCELLED_")
{
/*
/ this is probably MWS higher level throttling when requesting duplicate report
/ for near real time reports waiting 30 minutes
/ for daily reports wait 4 hours
*/
}
else if (filePath.Length == 0)
{
MiscFunction.EventLogInsert(
"Something went wrong retriving report from MWS. Operation cancelled.",
2
);
return;
}
}
// import into database
SqlConnection sqlConn;
try
{
using (TransactionScope scope = new TransactionScope())
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
//clear table data
using (SqlCommand cmd = new SqlCommand(@"
DELETE FROM tblImportFbaInventoryAgeReport;
", sqlConn))
{
cmd.ExecuteNonQuery();
}
using (var reader = new StreamReader(filePath))
{
//read file one line at a time and insert data into table if required
int lineNumber = 1;
int lineErrorSkip = 0;
int lineNoStockSkip = 0;
// read header and retrive information
string headerRow = reader.ReadLine();
string[] headers = headerRow.Split('\t');
int columnCount = headers.Length;
int index01 = Array.IndexOf(headers, "snapshot-date");
int index02 = Array.IndexOf(headers, "marketplace");
int index03 = Array.IndexOf(headers, "sku");
int index04 = Array.IndexOf(headers, "fnsku");
int index05 = Array.IndexOf(headers, "asin");
int index06 = Array.IndexOf(headers, "product-name");
int index07 = Array.IndexOf(headers, "condition");
int index08 = Array.IndexOf(headers, "avaliable-quantity(sellable)");
int index09 = Array.IndexOf(headers, "qty-with-removals-in-progress");
int index10 = Array.IndexOf(headers, "inv-age-0-to-90-days");
int index11 = Array.IndexOf(headers, "inv-age-91-to-180-days");
int index12 = Array.IndexOf(headers, "inv-age-181-to-270-days");
int index13 = Array.IndexOf(headers, "inv-age-271-to-365-days");
int index14 = Array.IndexOf(headers, "inv-age-365-plus-days");
int index15 = Array.IndexOf(headers, "currency");
int index16 = Array.IndexOf(headers, "qty-to-be-charged-ltsf-6-mo");
int index17 = Array.IndexOf(headers, "projected-ltsf-6-mo");
int index18 = Array.IndexOf(headers, "qty-to-be-charged-ltsf-12-mo");
int index19 = Array.IndexOf(headers, "projected-ltsf-12-mo");
int index20 = Array.IndexOf(headers, "units-shipped-last-7-days");
int index21 = Array.IndexOf(headers, "units-shipped-last-30-days");
int index22 = Array.IndexOf(headers, "units-shipped-last-60-days");
int index23 = Array.IndexOf(headers, "units-shipped-last-90-days");
int index24 = Array.IndexOf(headers, "alert");
int index25 = Array.IndexOf(headers, "your-price");
int index26 = Array.IndexOf(headers, "sales_price");
int index27 = Array.IndexOf(headers, "lowest_price_new");
int index28 = Array.IndexOf(headers, "lowest_price_used");
int index29 = Array.IndexOf(headers, "Recommended action");
int index30 = Array.IndexOf(headers, "Healthy Inventory Level");
int index31 = Array.IndexOf(headers, "Recommended sales price");
int index32 = Array.IndexOf(headers, "Recommended sale duration (days)");
int index33 = Array.IndexOf(headers, "Recommended Removal Quantity");
int index34 = Array.IndexOf(headers, "Estimated cost savings of removal");
string fileRow;
while ((fileRow = reader.ReadLine()) != null)
{
lineNumber = lineNumber + 1;
Console.Write("\rParsing record: " + lineNumber);
//split line into array
string[] items = fileRow.Split('\t');
if (items.Length != columnCount)
{
// skip line
lineErrorSkip = lineErrorSkip + 1;
MiscFunction.EventLogInsert(
"Line #" + lineNumber + " skipped due to no enough element in row.",
2,
filePath
);
}
else
{
//read values
string snapshotDate = items[index01];
string marketplace = items[index02];
string sku = items[index03];
string fnsku = items[index04];
string asin = items[index05];
string productName = items[index06];
string condition = items[index07];
string avaliableQuantity = items[index08];
string qtyWithRemovalsInProgress = items[index09];
string invAge0To90Days = items[index10];
string invAge91To180Days = items[index11];
string invAge181To270Days = items[index12];
string invAge271To365Days = items[index13];
string invAge365PlusDays = items[index14];
string currency = items[index15];
string qtyToBeChargedLtsf6Mo = items[index16];
string projectedLtsf6Mo = Regex.Replace(items[index17], "[^.0-9]", ""); // strip currency code prefix
string qtyToBeChargedLtsf12Mo = items[index18];
string projectedLtsf12Mo = Regex.Replace(items[index19], "[^.0-9]", ""); // strip currency code prefix
string unitsShippedLast7Days = items[index20];
string unitsShippedLast30Days = items[index21];
string unitsShippedLast60Days = items[index22];
string unitsShippedLast90Days = items[index23];
string alert = items[index24];
string yourPrice = Regex.Replace(items[index25], "[^.0-9]", ""); // strip currency code prefix
string salesPrice = Regex.Replace(items[index26], "[^.0-9]", ""); // strip currency code prefix
string lowestPriceNew = Regex.Replace(items[index27], "[^.0-9]", ""); // strip currency code prefix
string lowestPriceUsed = Regex.Replace(items[index28], "[^.0-9]", ""); // strip currency code prefix
string recommendedAction = items[index29];
string healthyInventoryLevel = items[index30];
string recommendedSalesPrice = Regex.Replace(items[index31], "[^.0-9]", ""); // strip currency code prefix
string recommendedSaleDuration = items[index32];
string recommendedRemovalQuantity = items[index33];
string estimatedCostSavingsOfRemoval = items[index34];
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblImportFbaInventoryAgeReport(
[snapshot-date], marketplace, sku, fnsku, asin, [product-name], condition, [avaliable-quantity(sellable)],
[qty-with-removals-in-progress], [inv-age-0-to-90-days], [inv-age-91-to-180-days], [inv-age-181-to-270-days],
[inv-age-271-to-365-days], [inv-age-365-plus-days], currency, [qty-to-be-charged-ltsf-6-mo], [projected-ltsf-6-mo],
[qty-to-be-charged-ltsf-12-mo], [projected-ltsf-12-mo], [units-shipped-last-7-days], [units-shipped-last-30-days],
[units-shipped-last-60-days], [units-shipped-last-90-days], alert, [your-price], sales_price, lowest_price_new,
lowest_price_used, [Recommended action], [Healthy Inventory Level], [Recommended sales price],
[Recommended sale duration (days)], [Recommended Removal Quantity], [Estimated cost savings of removal] )
VALUES (
@snapshotDate, @marketplace, @sku, @fnsku, @asin, @productName, @condition, @avaliableQuantity,
@qtyWithRemovalsInProgress, @invAge0To90Days, @invAge91To180Days, @invAge181To270Days, @invAge271To365Days, @invAge365PlusDays, @currency, @qtyToBeChargedLtsf6Mo,
@projectedLtsf6Mo, @qtyToBeChargedLtsf12Mo, @projectedLtsf12Mo, @unitsShippedLast7Days, @unitsShippedLast30Days, @unitsShippedLast60Days, @unitsShippedLast90Days, @alert,
@yourPrice, @salesPrice, @lowestPriceNew, @lowestPriceUsed, @recommendedAction, @healthyInventoryLevel, @recommendedSalesPrice, @recommendedSaleDuration,
@recommendedRemovalQuantity, @estimatedCostSavingsOfRemoval );
", sqlConn))
{
// add parameters
if (snapshotDate.Length == 0) { cmd.Parameters.AddWithValue("@snapshotDate", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@snapshotDate", DateTime.Parse(snapshotDate).ToUniversalTime()); }
if (marketplace.Length == 0) { cmd.Parameters.AddWithValue("@marketplace", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@marketplace", marketplace); }
if (sku.Length == 0) { cmd.Parameters.AddWithValue("@sku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@sku", sku); }
if (fnsku.Length == 0) { cmd.Parameters.AddWithValue("@fnsku", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@fnsku", fnsku); }
if (asin.Length == 0) { cmd.Parameters.AddWithValue("@asin", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@asin", asin); }
if (productName.Length == 0) { cmd.Parameters.AddWithValue("@productName", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@productName", productName); }
if (condition.Length == 0) { cmd.Parameters.AddWithValue("@condition", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@condition", condition); }
if (avaliableQuantity.Length == 0) { cmd.Parameters.AddWithValue("@avaliableQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@avaliableQuantity", int.Parse(avaliableQuantity)); }
if (qtyWithRemovalsInProgress.Length == 0) { cmd.Parameters.AddWithValue("@qtyWithRemovalsInProgress", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@qtyWithRemovalsInProgress", int.Parse(qtyWithRemovalsInProgress)); }
if (invAge0To90Days.Length == 0) { cmd.Parameters.AddWithValue("@invAge0To90Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@invAge0To90Days", int.Parse(invAge0To90Days)); }
if (invAge91To180Days.Length == 0) { cmd.Parameters.AddWithValue("@invAge91To180Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@invAge91To180Days", int.Parse(invAge91To180Days)); }
if (invAge181To270Days.Length == 0) { cmd.Parameters.AddWithValue("@invAge181To270Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@invAge181To270Days", int.Parse(invAge181To270Days)); }
if (invAge271To365Days.Length == 0) { cmd.Parameters.AddWithValue("@invAge271To365Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@invAge271To365Days", int.Parse(invAge271To365Days)); }
if (invAge365PlusDays.Length == 0) { cmd.Parameters.AddWithValue("@invAge365PlusDays", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@invAge365PlusDays", int.Parse(invAge365PlusDays)); }
if (currency.Length == 0) { cmd.Parameters.AddWithValue("@currency", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@currency", currency); }
if (qtyToBeChargedLtsf6Mo.Length == 0) { cmd.Parameters.AddWithValue("@qtyToBeChargedLtsf6Mo", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@qtyToBeChargedLtsf6Mo", int.Parse(qtyToBeChargedLtsf6Mo)); }
if (projectedLtsf6Mo.Length == 0) { cmd.Parameters.AddWithValue("@projectedLtsf6Mo", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@projectedLtsf6Mo", decimal.Parse(projectedLtsf6Mo)); }
if (qtyToBeChargedLtsf12Mo.Length == 0) { cmd.Parameters.AddWithValue("@qtyToBeChargedLtsf12Mo", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@qtyToBeChargedLtsf12Mo", int.Parse(qtyToBeChargedLtsf12Mo)); }
if (projectedLtsf12Mo.Length == 0) { cmd.Parameters.AddWithValue("@projectedLtsf12Mo", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@projectedLtsf12Mo", decimal.Parse(projectedLtsf12Mo)); }
if (unitsShippedLast7Days.Length == 0) { cmd.Parameters.AddWithValue("@unitsShippedLast7Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@unitsShippedLast7Days", int.Parse(unitsShippedLast7Days)); }
if (unitsShippedLast30Days.Length == 0) { cmd.Parameters.AddWithValue("@unitsShippedLast30Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@unitsShippedLast30Days", int.Parse(unitsShippedLast30Days)); }
if (unitsShippedLast60Days.Length == 0) { cmd.Parameters.AddWithValue("@unitsShippedLast60Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@unitsShippedLast60Days", int.Parse(unitsShippedLast60Days)); }
if (unitsShippedLast90Days.Length == 0) { cmd.Parameters.AddWithValue("@unitsShippedLast90Days", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@unitsShippedLast90Days", int.Parse(unitsShippedLast90Days)); }
if (alert.Length == 0) { cmd.Parameters.AddWithValue("@alert", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@alert", alert); }
if (yourPrice.Length == 0) { cmd.Parameters.AddWithValue("@yourPrice", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@yourPrice", decimal.Parse(yourPrice)); }
if (salesPrice.Length == 0) { cmd.Parameters.AddWithValue("@salesPrice", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@salesPrice", decimal.Parse(salesPrice)); }
if (lowestPriceNew.Length == 0) { cmd.Parameters.AddWithValue("@lowestPriceNew", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@lowestPriceNew", decimal.Parse(lowestPriceNew)); }
if (lowestPriceUsed.Length == 0) { cmd.Parameters.AddWithValue("@lowestPriceUsed", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@lowestPriceUsed", decimal.Parse(lowestPriceUsed)); }
if (recommendedAction.Length == 0) { cmd.Parameters.AddWithValue("@recommendedAction", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@recommendedAction", recommendedAction); }
if (healthyInventoryLevel.Length == 0) { cmd.Parameters.AddWithValue("@healthyInventoryLevel", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@healthyInventoryLevel", int.Parse(healthyInventoryLevel)); }
if (recommendedSalesPrice.Length == 0) { cmd.Parameters.AddWithValue("@recommendedSalesPrice", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@recommendedSalesPrice", decimal.Parse(recommendedSalesPrice)); }
if (recommendedSaleDuration.Length == 0) { cmd.Parameters.AddWithValue("@recommendedSaleDuration", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@recommendedSaleDuration", int.Parse(recommendedSaleDuration)); }
if (recommendedRemovalQuantity.Length == 0) { cmd.Parameters.AddWithValue("@recommendedRemovalQuantity", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@recommendedRemovalQuantity", int.Parse(recommendedRemovalQuantity)); }
if (estimatedCostSavingsOfRemoval.Length == 0) { cmd.Parameters.AddWithValue("@estimatedCostSavingsOfRemoval", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@estimatedCostSavingsOfRemoval", decimal.Parse(estimatedCostSavingsOfRemoval)); }
// execute the query
cmd.ExecuteNonQuery();
}
}
}
Console.Write("\r");
MiscFunction.EventLogInsert(
"Operation complete. " + lineNumber + " items processes. " + (lineNumber - lineErrorSkip - lineNoStockSkip) + " total new items inserted, "
+ lineNoStockSkip + " 'No Stock' records were skipped.");
if (lineErrorSkip > 0)
{
MiscFunction.EventLogInsert(lineErrorSkip + " total line(s) where skipped due to insufficent number of cells on row", 1);
}
}
}
scope.Complete();
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Something went wrong during import, check details for more.", 1, ex.ToString());
Console.WriteLine(ex.ToString());
}
}
public void UpdateAmazonSettlementData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2_";
MiscFunction.EventLogInsert("Requesting 'unacknowledged' '" + mwsReportEnum + "' MWS reports");
/*
/ get the list of avaiable reports
*/
TypeList reportList = new TypeList();
reportList.Type.Add(mwsReportEnum);
GetReportListRequest requestList = new GetReportListRequest();
requestList.Merchant = merchantId;
requestList.MaxCount = 100;
requestList.ReportTypeList = reportList;
bool? returnAcknowledged = false;
if (returnAcknowledged == true) { requestList.Acknowledged = true; }
else if (returnAcknowledged == false) { requestList.Acknowledged = false; }
/*
/ request, recive request, and then iterate through the list
*/
List reportInfoList = GetMwsReportList(requestList);
int reportsImported = 0;
foreach (ReportInfo reportInfo in reportInfoList)
{
//don't know what this next line does, might need it
//if (reportInfo.IsSetReportId() & reportInfo.IsSetAcknowledged())
string reportId = reportInfo.ReportId;
bool acknowledged = reportInfo.Acknowledged;
string filePath = GetMwsReportById(reportInfo.ReportId);
/*
* Pass file path to function to parse into database
*/
bool import = ImportReportSettlementData(sqlConnectionString, filePath);
// if opertion succeded, set the acknowledged property
if (import & (returnAcknowledged == false | returnAcknowledged == null))
{
SetMwsReportAcknowledgement(reportId, true);
}
if (import == true)
{
reportsImported = reportsImported + 1;
}
}
if (reportsImported > 0)
{
MiscFunction.EventLogInsert("No new '" + mwsReportEnum + "' reports available for download.");
//return true;
}
else
{
MiscFunction.EventLogInsert("Total of " + reportsImported + " new settlement reports imported");
//return false;
}
}
public void UpdateFbaInventoryReceiptData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_FBA_FULFILLMENT_INVENTORY_RECEIPTS_DATA_";
/*
* Not able to uniquley define each row in flat file and check for duplicate in DB
* therefore, do not download report date range that has already been imported, as records will be duplicated
*
* Due to a MWS bug, MWS can/will include some records that are before the specified date range. As mws report is updated once daily,
* it is safe to omitt lines < startDate when parsing/inserting lines into db (as these will have been previously imported)
*/
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
DateTime currentTime = DateTime.UtcNow.AddMinutes(-5);
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([received-date]) AS MaxDate FROM tblImportFbaInventoryReceiptReport;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2014-09-01T00:00:00Z");
// no need to specific timezone, etc, as "Z" already specifis UTC
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
startTime = startTime.AddSeconds(1);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running FbaInventoryReceiptReportImport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping UpdateFbaInventoryReceiptData method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaInventoryReceipt(sqlConnectionString, reportPath, startTime);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
public void UpdateFbaSaleShipmentData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_AMAZON_FULFILLED_SHIPMENTS_DATA_";
//// Uncomment to override getreport and load directly from file
//bool ackImportTemp = FbaSaleShipmentReportImport(@"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\7019109828017378.txt");
//return;
/*
* flat file lines/rows are unique
* FROM MWS GUIDANCE
* https://docs.developer.amazonservices.com/en_UK/fba_guide/FBAGuide_TipsShipmentsReport.html
* Overlap report by 3 days. To disambiguate duplicate shipment reports, use the ShipmentItemId (not the OrderId or the OrderItemId)
*/
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
DateTime latestTime = DateTime.UtcNow.AddMinutes(-5);
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([shipment-date]) AS MaxDate FROM tblImportFbaSaleShipment;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2014-09-01T00:00:00Z");
// no need to specific timezone, etc, as "Z" already specifis UTC
startTime = DateTime.Parse("2016-02-01T00:00:00Z");
// fba sale shipments for previous 18 months only
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
// Amazon states in MWS guidance that shipments are added in near real time, however, in most cases,
// there will be a delay of approximately one to three hours. In some rare cases there could be a delay of up to 24 hours.
startTime = startTime.AddDays(-3);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running FbaInventoryReceiptReportImport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping GetFbaReturnsReport method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaSaleShipment(sqlConnectionString, reportPath);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
public void UpdateFbaReturnData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA_";
//_GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA_
//// Uncomment to override getreport and load directly from file
//bool ackImportTemp = ImportFbaReturnsReport(@"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\7027223463017379.txt");
//return;
/*
* flat file lines/rows maybe unique
* Overlap reports by 3 days.
* To disambiguate duplicate row, I will use combination of date, orderid, fnsku, quantity, LPN#, disposition, and reason.
*/
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([return-date]) AS MaxDate FROM tblImportFbaCustomerReturn;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2015-08-25T00:00:00Z"); //this before first return
// no need to specific timezone, etc, as "Z" already specifis UTC
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
// Amazon states in MWS guidance that content updated daily.
startTime = startTime.AddDays(-14);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running GetFbaReturnsReport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping GetFbaReturnsReport method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaReturns(sqlConnectionString, reportPath);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
public void UpdateFbaAdustmentData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_FBA_FULFILLMENT_INVENTORY_ADJUSTMENTS_DATA_";
//// Uncomment to override getreport and load directly from file
//bool ackImportTemp = ImportFbaAdustmentReport(@"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\7034366409017380.txt");
//return;
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([adjusted-date]) AS MaxDate FROM tblImportFbaInventoryAdjustmentReport;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2014-09-01T00:00:00Z");
// no need to specific timezone, etc, as "Z" already specifis UTC
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
// Amazon states in MWS guidance that content updated daily.
startTime = startTime.AddDays(-3);
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running GetFbaAdustmentData, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping GetFbaAdustmentData method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaAdjustment(sqlConnectionString, reportPath);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
public void UpdateFbaReimbursementData(string sqlConnectionString)
{
string mwsReportEnum = "_GET_FBA_REIMBURSEMENTS_DATA_";
// Uncomment to override getreport and load directly from file
//bool ackImportTemp = ImportFbaReimbursementReport(@"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\7037074827017380.txt");
//return;
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([approval-date]) AS MaxDate FROM tblImportFbaReimbursementReport;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2014-09-01T00:00:00Z");
// no need to specific timezone, etc, as "Z" already specifis UTC
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
// Amazon states in MWS guidance that content updated daily.
startTime = startTime.AddDays(-3);
//startTime = DateTime.Parse("2015-05-01T00:00:00Z");
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running GetFbaReimbursementData, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping GetFbaReimbursementData method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaReimbursement(sqlConnectionString, reportPath);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
public void UpdateFbaRemovalOrderReport(string sqlConnectionString)
{
string mwsReportEnum = "_GET_FBA_FULFILLMENT_REMOVAL_ORDER_DETAIL_DATA_";
// Uncomment to override getreport and load directly from file
//bool ackImportTemp = ImportFbaReimbursementReport(@"C:\Users\Bobbie\AppData\Local\Temp\_e2A Client\File\7037074827017380.txt");
//return;
// get the most recent date from db table
DateTime startTime;
DateTime endTime = DateTime.UtcNow;
SqlConnection sqlConn;
try
{
using (sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand(
"SELECT Max([request-date]) AS MaxDate FROM tblImportFbaRemovalOrderReport;"
, sqlConn))
{
if (cmd.ExecuteScalar() == DBNull.Value)
{
// use first month started selling on Amazon
startTime = DateTime.Parse("2015-09-15T00:00:00Z");
// no need to specific timezone, etc, as "Z" already specifis UTC
}
else
{
startTime = ((DateTime)cmd.ExecuteScalar());
startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc);
startTime = startTime.AddDays(-30); // yes, that's right -30 days
//startTime = DateTime.Parse("2015-05-01T00:00:00Z");
}
}
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("Error running GetFbaRemovalOrderReport, no records were commited",
1,
ex.ToString() + "\r\nTraceMessage:\r\n" + MiscFunction.TraceMessage()
);
return;
}
// get list of reports
List reportPathList = GetMwsReportByPeriod(mwsReportEnum, startTime, endTime, 24);
if (reportPathList.Count == 0)
{
// no report downloadeded
MiscFunction.EventLogInsert("No reports downloaded, stopping GetFbaReimbursementData method...");
return;
}
// loop throught list of filepaths
foreach (string reportPath in reportPathList)
{
bool ackImport = ImportReportFbaRemovalOrder(sqlConnectionString, reportPath);
if (ackImport == false)
{
MiscFunction.EventLogInsert("Problem importing report '" + mwsReportEnum + "'. stopping further report imports...",
1,
MiscFunction.TraceMessage()
);
return;
}
}
}
}
public class MiscFunction
{
public static void EventLogInsert
(string detailShort = "", int eventType = 3, string detailLong = "", DateTime eventDateTime = default(DateTime), bool consolePrint = true)
{
// login credentials only allow insert on log table
string userId = "Log_bnhtrade";
string password = "52ya9dky55cniyynwro5e48mV9";
string sqlConnectionString =
"Data Source=SQL-Server;Initial Catalog=e2A;Persist Security Info=TRUE;User ID=" + userId +
";Password=" + password + ";MultipleActiveResultSets=TRUE";
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress))
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
if (detailShort.Length == 0)
{
throw new ArgumentException("The string parameter detailShort is empty, this is a required paramenter.");
}
if (eventDateTime == default(DateTime))
{
eventDateTime = DateTime.UtcNow;
}
try
{
using (SqlCommand cmd = new SqlCommand(
"INSERT INTO tblLogEvent ( EventDateTime, LogEventTypeID, LogEventSourceID, Detail, DetailLong )" +
"VALUES ( @eventDateTime, @eventType, 1, @detailShort, @detailLong );"
, sqlConn))
{
cmd.Parameters.AddWithValue("@eventDateTime", eventDateTime);
cmd.Parameters.AddWithValue("@eventType", eventType);
cmd.Parameters.AddWithValue("@detailShort", detailShort);
cmd.Parameters.AddWithValue("@detailLong", detailLong);
cmd.ExecuteNonQuery();
}
ConsoleUpdate(detailShort);
}
catch (Exception ex)
{
ConsoleUpdate("WTF!!!! Error with error logging, jobs foooked!");
throw ex;
}
scope.Complete();
}
}
}
public static string TraceMessage(
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
string traceMessage =
"Trace:" +
"\r\n\tmember name: " + memberName +
"\r\n\tsource file path: " + sourceFilePath +
"\r\n\tsource line number: " + sourceLineNumber;
return traceMessage;
}
public static void MwsThrottleWait(int waitSeconds)
{
MiscFunction.EventLogInsert("MWS request throttled, retrying in " + waitSeconds + " seconds...");
string consoleMessage = "{0} seconds remaining...";
ConsoleCountDown(consoleMessage, waitSeconds);
}
public static void ConsoleCountDown(string consoleMessage, int waitSeconds)
{
do
{
Console.Write("\r[--------] " + consoleMessage, String.Format("{0:00}", waitSeconds));
System.Threading.Thread.Sleep(1000);
waitSeconds = waitSeconds - 1;
} while (waitSeconds > 0);
Console.Write("\r");
Console.Write(new String(' ', Console.BufferWidth -1));
Console.Write("\r");
}
public static void ConsoleUpdate(string consoleText, Boolean newLine = true)
{
if (newLine)
{
Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
}
else
{
Console.WriteLine("\r[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
}
}
public static string GetTempFilePath(string fileName)
{
string directoryPath = Path.GetTempPath()
+ "_" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
+ "\\File\\";
System.IO.Directory.CreateDirectory(directoryPath);
string fullPath = directoryPath + fileName;
return fullPath;
}
public static DateTime ParseMwsReportDateTime(string reportDateTime)
{
string isoDateTime =
reportDateTime.Substring(6, 4) + "-" +
reportDateTime.Substring(3, 2) + "-" +
reportDateTime.Substring(0, 2) + "T" +
reportDateTime.Substring(11, 2) + ":" +
reportDateTime.Substring(14, 2) + ":" +
reportDateTime.Substring(17, 2) + "Z";
return DateTime.Parse(isoDateTime);
}
}
public class TempFunction
{
public void UpdateSkuCost(string sqlConnectionString)
{
MiscFunction.EventLogInsert("Starting temp function UpdateSkuCost()");
int count = 0;
try
{
using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
{
sqlConn.Open();
using (SqlCommand cmd01 = sqlConn.CreateCommand())
{
//query for list of all sku's with stock
cmd01.CommandText = @"
SELECT sku, [mfn-fulfillable-quantity], [afn-total-quantity]
FROM tblImportFbaManageInventory
WHERE ( [mfn-fulfillable-quantity] > 0 ) OR ( [afn-total-quantity] > 0 )
";
using (SqlDataReader reader01 = cmd01.ExecuteReader())
{
// retrive index of columns for faster operation
int indexOfColumn1 = reader01.GetOrdinal("sku");
int indexOfColumn2 = reader01.GetOrdinal("mfn-fulfillable-quantity");
int indexOfColumn3 = reader01.GetOrdinal("afn-total-quantity");
while (reader01.Read())
{
count = count + 1;
Console.Write("\rProcessing record #" + count);
//assign values
string skuNumber = reader01.GetString(indexOfColumn1);
int mfnTotal;
if (reader01.IsDBNull(indexOfColumn2)) { mfnTotal = 0; }
else { mfnTotal = reader01.GetInt32(indexOfColumn2); }
int afnTotal;
if (reader01.IsDBNull(indexOfColumn3)) { afnTotal = 0; }
else { afnTotal = reader01.GetInt32(indexOfColumn3); }
int total = mfnTotal + afnTotal;
//query for average unit cost
using (SqlCommand cmd02 = new SqlCommand(@"
SELECT AVG(Q.UnitCost) AS AvgCost
FROM(
SELECT TOP (@total) UnitQuantity, n, UnitCost
FROM(tblNumbers INNER JOIN tblStock ON n <= UnitQuantity) INNER JOIN tblSku ON SkuID = skuSkuID
WHERE(skuSkuNumber = @skuNumber)
ORDER BY StockID DESC
) Q
", sqlConn))
{
cmd02.Parameters.AddWithValue("@total", total);
cmd02.Parameters.AddWithValue("@skuNumber", skuNumber);
decimal AvgCost = 0;
object obj = cmd02.ExecuteScalar();
if (obj == null || obj == DBNull.Value)
{
AvgCost = 0;
}
else
{
AvgCost = Convert.ToDecimal(obj);
}
AvgCost = Math.Round(AvgCost, 2);
//Console.WriteLine(skuNumber + " " + AvgCost);
//update sku table
using (SqlCommand cmd03 = sqlConn.CreateCommand())
{
cmd03.Parameters.AddWithValue("@skuNumber", skuNumber);
cmd03.Parameters.AddWithValue("@AvgCost", AvgCost);
cmd03.Parameters.AddWithValue("@timeStamp", DateTime.UtcNow);
Console.Write(" £" + AvgCost );
cmd03.CommandText =
"UPDATE tblSku " +
"SET skuSkuAvgCost = @AvgCost, skuSkuAvgCostUpdate = @timeStamp " +
"WHERE skuSkuNumber = @skuNumber;";
cmd03.ExecuteNonQuery();
}
}
}
}
}
Console.Write("\r");
MiscFunction.EventLogInsert("UpdateSkuCost() operation complete. " + count + " total SKU average cost(s) updated");
}
}
catch (Exception ex)
{
MiscFunction.EventLogInsert("UpdateSkuCost() operation exceltion. See 'details' for further information.", 1, ex.ToString());
Console.WriteLine(ex.ToString());
}
}
}
}