//This is used for make separate copy of object (without reference) public static WriteOffItemsModel Clone(WriteOffItemsModel obj) { WriteOffItemsModel new_obj = new WriteOffItemsModel(); foreach (PropertyInfo pi in obj.GetType().GetProperties()) { if (pi.CanRead && pi.CanWrite && pi.PropertyType.IsSerializable) { pi.SetValue(new_obj, pi.GetValue(obj, null), null); } } return(new_obj); }
//This function is Transaction and do followig things //1) Save WriteOff Items entry in WriteOff Table 2) WriteOff Items Entry in Stock_Transaction table //3) Update Stock Table Quantity public static Boolean WriteOffItemsTransaction(List <WriteOffItemsModel> writeOffItemsFromClient, InventoryDbContext inventoryDbContext) { //Transaction Begin //We first Need to make Stock, Stock_Transaction Object with WriteOff data (which has client data) using (var dbContextTransaction = inventoryDbContext.Database.BeginTransaction()) { try { //This is updated list of stock records after write off items List <StockModel> stockListForUpdate = new List <StockModel>(); //This is transaction list of write off items List <StockTransactionModel> stockTxnListForInsert = new List <StockTransactionModel>(); //This is WriteOff List for insert into writeOff table List <WriteOffItemsModel> writeOffListForInsert = new List <WriteOffItemsModel>(); var createdOn = DateTime.Now; for (int i = 0; i < writeOffItemsFromClient.Count; i++) { List <StockModel> currStockList = new List <StockModel>(); currStockList = GetStockItemsByItemIdBatchNO(writeOffItemsFromClient[i].ItemId, writeOffItemsFromClient[i].BatchNO, inventoryDbContext); if (currStockList.Count > 0) { foreach (var currStkItm in currStockList) { if (writeOffItemsFromClient[i].WriteOffQuantity > 0) { //When stockItem availableQuantity is > WriteOffQuantity if (currStkItm.AvailableQuantity > writeOffItemsFromClient[i].WriteOffQuantity) { WriteOffItemsModel woItemsClone = new WriteOffItemsModel(); //Clone WriteList item woItemsClone = Clone(writeOffItemsFromClient[i]); currStkItm.AvailableQuantity = currStkItm.AvailableQuantity - woItemsClone.WriteOffQuantity.Value; //Push Updated StockItem into StockList for Update Stock stockListForUpdate.Add(currStkItm); woItemsClone.StockId = currStkItm.StockId; woItemsClone.GoodsReceiptItemId = currStkItm.GoodsReceiptItemId; woItemsClone.WriteOffQuantity = woItemsClone.WriteOffQuantity.Value; woItemsClone.Remark = woItemsClone.Remark.ToString(); woItemsClone.CreatedOn = createdOn; //Push Updated WriteOff Item Into WriteOffItemList for Save writeOffListForInsert.Add(woItemsClone); //updated Current WriteOff Item Quantity as 0 writeOffItemsFromClient[i].WriteOffQuantity = 0; } else if (currStkItm.AvailableQuantity < writeOffItemsFromClient[i].WriteOffQuantity) { //when curStkItm.AvailableQuantity< woitm.WriteOffQuantity WriteOffItemsModel woItemsClone = new WriteOffItemsModel(); woItemsClone = Clone(writeOffItemsFromClient[i]); woItemsClone.StockId = currStkItm.StockId; woItemsClone.WriteOffQuantity = currStkItm.AvailableQuantity; //double and decimal can't multiply so, need explicitly typecasting woItemsClone.TotalAmount = (decimal)currStkItm.AvailableQuantity * woItemsClone.ItemRate.Value; woItemsClone.GoodsReceiptItemId = currStkItm.GoodsReceiptItemId; //Push Updated WriteOff Item Into WriteOffItemList for Save writeOffListForInsert.Add(woItemsClone); currStkItm.AvailableQuantity = 0; //Push Updated StockItem into StockList for Update Stock stockListForUpdate.Add(currStkItm); writeOffItemsFromClient[i].WriteOffQuantity = writeOffItemsFromClient[i].WriteOffQuantity - currStkItm.AvailableQuantity; } } } } } //Save WriteOffItems in database AddWriteOffItems(inventoryDbContext, writeOffListForInsert); //Make Fill data into Stock_transaction object for save into INV_TXN_StockTransaction table foreach (var woItem in writeOffListForInsert) { StockTransactionModel stkTxnItem = new StockTransactionModel(); //StockTxnId,StockId,Quantity,InOut, ReferenceNo CreatedBy ,CreatedOn,TransactionType stkTxnItem.StockId = woItem.StockId; stkTxnItem.Quantity = (int)woItem.WriteOffQuantity; stkTxnItem.InOut = "out"; stkTxnItem.ReferenceNo = woItem.WriteOffId; stkTxnItem.CreatedBy = woItem.CreatedBy; stkTxnItem.TransactionType = "writeoff"; //Push current StkTxnItem into StkTxnItemList for Save to database stockTxnListForInsert.Add(stkTxnItem); } //Save Stock Transaction record AddStockTransaction(inventoryDbContext, stockTxnListForInsert); //Update Stock records UpdateStock(inventoryDbContext, stockListForUpdate); //Commit Transaction dbContextTransaction.Commit(); return(true); } catch (Exception ex) { //Rollback all transaction if exception occured i.e. WriteOff Insertion, Stock_Transaction Insertion, Stock Updation dbContextTransaction.Rollback(); throw ex; } } }