mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-05-18 19:48:23 +00:00
199 lines
8.9 KiB
C#
199 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Transactions;
|
|
|
|
namespace bnhtrade.Core.Data.Database.Stock
|
|
{
|
|
public class UpdateSkuTransaction : Connection
|
|
{
|
|
// Any update method below to the quanity, isprocessed or journalId, implements a consistancy check.
|
|
|
|
private string err = "Database UpdateSkuTransaction: ";
|
|
private ReadSkuTransaction dbRead = new ReadSkuTransaction();
|
|
|
|
public UpdateSkuTransaction()
|
|
{
|
|
}
|
|
|
|
public void Update(Model.Stock.SkuTransaction skuTransaction)
|
|
{
|
|
// Consistancy check: is the target marked as is processed?
|
|
dbRead.Init();
|
|
if (dbRead.GetIsProcessed(skuTransaction.SkuTransactionId))
|
|
throw new InvalidOperationException("Update restricted, sku transaction isProcessed=true");
|
|
|
|
using (var conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
UPDATE tblStockSkuTransaction
|
|
SET TransactionDate = @transactionDate
|
|
,StockSkuTransactionTypeID = (
|
|
SELECT StockSkuTransactionTypeID
|
|
FROM tblStockSkuTransactionType
|
|
WHERE TypeCode = @skuTransactionTypeCode
|
|
)
|
|
,ForeignKey = @foreignKey
|
|
,Reference = @reference
|
|
,Detail = @Detail
|
|
,SkuID = (
|
|
SELECT skuSkuID
|
|
FROM tblSku
|
|
WHERE skuSkuNumber = @skuNumber
|
|
)
|
|
,Quantity = @quantity
|
|
,IsProcessed = @isProcessed
|
|
,StockJournalID = @stockJournalID
|
|
WHERE StockSkuTransactionID = @transactionId
|
|
", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@transactionDate", skuTransaction.TransactionDate.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@skuTransactionTypeCode", skuTransaction.SkuTransactionTypeCode);
|
|
if (skuTransaction.ForeignKey == null) { cmd.Parameters.AddWithValue("@foreignKey", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@foreignKey", skuTransaction.ForeignKey); }
|
|
if (skuTransaction.Reference == null) { cmd.Parameters.AddWithValue("@reference", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@reference", skuTransaction.Reference); }
|
|
if (skuTransaction.Detail == null) { cmd.Parameters.AddWithValue("@detail", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@detail", skuTransaction.Detail); }
|
|
cmd.Parameters.AddWithValue("@skuNumber", skuTransaction.SkuNumber);
|
|
cmd.Parameters.AddWithValue("@quantity", skuTransaction.Quantity);
|
|
cmd.Parameters.AddWithValue("@isProcessed", skuTransaction.IsProcessed);
|
|
if (skuTransaction.StockJournalId == null) { cmd.Parameters.AddWithValue("@stockJournalID", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@stockJournalID", skuTransaction.StockJournalId); }
|
|
cmd.Parameters.AddWithValue("@transactionId", skuTransaction.SkuTransactionId);
|
|
|
|
int effected = cmd.ExecuteNonQuery();
|
|
|
|
if (effected < 1)
|
|
{
|
|
throw new Exception(err += "stockSkuTransaction IsProcessed update failed");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the isProcessed and StockJournalId field, with journal consistancy check.
|
|
/// </summary>
|
|
/// <param name="skuTransactionId">Sku Transaction Id</param>
|
|
/// <param name="isProcessed"></param>
|
|
/// <param name="stockJournalId"></param>
|
|
/// <exception cref="InvalidOperationException">Invalid journalId, isProcessed combination</exception>
|
|
/// <exception cref="Exception">DB update sql fail</exception>
|
|
public void UpdateIsProcessed(int skuTransactionId, bool isProcessed, int? stockJournalId, bool enableJournalDelete = false)
|
|
{
|
|
// IMPORTANT!!!!
|
|
// For consistancy reasons: need to make sure that if a journal entry exists, it is deteled before it is
|
|
// edited, or removed from the transaction table
|
|
// Best to implement this here, at only point where the isprocessed/journalId can be updated
|
|
|
|
|
|
// intial checks
|
|
if (isProcessed == false && stockJournalId != null)
|
|
throw new InvalidOperationException("If stock journal id is set, isProcessed must be true");
|
|
|
|
|
|
using (var scope = new TransactionScope())
|
|
{
|
|
try
|
|
{
|
|
// check for existing journal Id in transactin table
|
|
var existingJournalId = new Data.Database.Stock.ReadSkuTransaction().GetJournalId(skuTransactionId);
|
|
bool journalDeleteRequired = false;
|
|
if (existingJournalId != null)
|
|
{
|
|
if (stockJournalId == null || stockJournalId != existingJournalId)
|
|
{
|
|
journalDeleteRequired = true;
|
|
}
|
|
}
|
|
|
|
// make the journal delete, if required and enabled
|
|
if (journalDeleteRequired && enableJournalDelete)
|
|
{
|
|
new Data.Database.Stock.JournalCrud().StockJournalDelete((int)existingJournalId);
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("Removal of stock journal to update sku transaction table not allowed");
|
|
}
|
|
|
|
// update the transaction table
|
|
string sql = @"
|
|
UPDATE tblStockSkuTransaction
|
|
SET IsProcessed = @isProcessed
|
|
,StockJournalID = @stockJournalID
|
|
WHERE StockSkuTransactionID = @transactionId;";
|
|
|
|
using (var conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@isProcessed", isProcessed);
|
|
if (stockJournalId == null) { cmd.Parameters.AddWithValue("@stockJournalID", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@stockJournalID", (int)stockJournalId); }
|
|
cmd.Parameters.AddWithValue("@transactionId", skuTransactionId);
|
|
|
|
int effected = cmd.ExecuteNonQuery();
|
|
|
|
if (effected != 1)
|
|
{
|
|
throw new Exception(err += "Sku Transaction StockJournalID update failed");
|
|
}
|
|
}
|
|
}
|
|
scope.Complete();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
scope.Dispose();
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the quantity field, with journal consistancy check.
|
|
/// </summary>
|
|
/// <param name="skuTransactionId">stock sku transaction id</param>
|
|
/// <param name="quantity">quantity</param>
|
|
/// <exception cref="InvalidOperationException">Update restricted, sku transaction isProcessed=true</exception>
|
|
/// <exception cref="Exception">Database write failed</exception>
|
|
public void UpdateQuanitity(int skuTransactionId, int quantity)
|
|
{
|
|
// Consistancy check: is the target marked as is processed?
|
|
dbRead.Init();
|
|
if (dbRead.GetIsProcessed(skuTransactionId))
|
|
throw new InvalidOperationException("Update restricted, sku transaction isProcessed=true");
|
|
|
|
using (var conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(@"
|
|
UPDATE tblStockSkuTransaction
|
|
SET Quantity = @quantity
|
|
WHERE StockSkuTransactionID = @transactionId
|
|
", conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@quantity", quantity);
|
|
|
|
int effected = cmd.ExecuteNonQuery();
|
|
|
|
if (effected != 1)
|
|
{
|
|
throw new Exception(err += "stockSkuTransaction Quantity update failed");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|