public void DeleteVehicle(VehicleViewModel vehicle, int userId) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; vehicle.ModifyDate = serverTime; vehicle.ModifyUserId = userId; vehicle.Status = (int)DbConstant.DefaultDataStatus.Deleted; Vehicle entity = _vehicleRepository.GetById(vehicle.Id); Map(vehicle, entity); _vehicleRepository.AttachNavigation(entity.Brand); _vehicleRepository.AttachNavigation(entity.Type); _vehicleRepository.AttachNavigation(entity.VehicleGroup); _vehicleRepository.AttachNavigation(entity.Customer); _vehicleRepository.AttachNavigation(entity.CreateUser); _vehicleRepository.AttachNavigation(entity.ModifyUser); _vehicleRepository.Update(entity); _unitOfWork.SaveChanges(); foreach (var item in _vehicleWheelRepository.GetMany(vw => vw.VehicleId == vehicle.Id && vw.Status == (int)DbConstant.DefaultDataStatus.Active)) { item.ModifyDate = serverTime; item.ModifyUserId = userId; item.Status = (int)DbConstant.DefaultDataStatus.Deleted; _vehicleWheelRepository.AttachNavigation(item.Vehicle); _vehicleWheelRepository.AttachNavigation(item.WheelDetail); _vehicleWheelRepository.AttachNavigation(item.CreateUser); _vehicleWheelRepository.AttachNavigation(item.ModifyUser); _vehicleWheelRepository.Update(item); _unitOfWork.SaveChanges(); SpecialSparepartDetail wdEntity = _specialSparepartDetailRepository.GetById(item.WheelDetailId); wdEntity.ModifyDate = serverTime; wdEntity.ModifyUserId = userId; wdEntity.Status = (int)DbConstant.WheelDetailStatus.Ready; _specialSparepartDetailRepository.AttachNavigation(wdEntity.Sparepart); _specialSparepartDetailRepository.AttachNavigation(wdEntity.PurchasingDetail); _specialSparepartDetailRepository.AttachNavigation(wdEntity.SparepartManualTransaction); _specialSparepartDetailRepository.AttachNavigation(wdEntity.CreateUser); _specialSparepartDetailRepository.AttachNavigation(wdEntity.ModifyUser); _specialSparepartDetailRepository.Update(wdEntity); _unitOfWork.SaveChanges(); Sparepart spEntity = _sparepartRepository.GetById(wdEntity.SparepartId); spEntity.ModifyDate = serverTime; spEntity.ModifyUserId = userId; spEntity.StockQty = spEntity.StockQty + 1; _sparepartRepository.AttachNavigation(spEntity.UnitReference); _sparepartRepository.AttachNavigation(spEntity.CategoryReference); _sparepartRepository.AttachNavigation(spEntity.CreateUser); _sparepartRepository.AttachNavigation(spEntity.ModifyUser); _sparepartRepository.Update(spEntity); _unitOfWork.SaveChanges(); SparepartStockCard stockCard = new SparepartStockCard(); Reference transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_VEHICLE).FirstOrDefault(); stockCard.CreateUserId = userId; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = vehicle.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = spEntity.Id; stockCard.Description = "Delete Vehicle"; stockCard.QtyIn = 1; stockCard.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail != null ? wdEntity.PurchasingDetail.Price : wdEntity.SparepartManualTransaction != null ? wdEntity.SparepartManualTransaction.Price : 0); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(spEntity.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock + stockCard.QtyIn; stockCard.QtyLastPrice = lastStockPrice + stockCard.QtyInPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (wdEntity.PurchasingDetail != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.PurchasingDetail.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(wdEntity.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(wdEntity.Sparepart.Id, wdEntity.PurchasingDetail.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = wdEntity.PurchasingDetail.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); PurchasingDetail pDetail = _purchasingDetailRepository.GetById(wdEntity.PurchasingDetailId); pDetail.ModifyDate = serverTime; pDetail.ModifyUserId = userId; pDetail.QtyRemaining = pDetail.QtyRemaining + 1; _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart); _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser); _purchasingDetailRepository.Update(pDetail); _unitOfWork.SaveChanges(); } if (wdEntity.SparepartManualTransaction != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(wdEntity.SparepartManualTransaction.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(1 * wdEntity.SparepartManualTransaction.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(wdEntity.Sparepart.Id, wdEntity.SparepartManualTransactionId.Value); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.SparepartManualTransactionId = wdEntity.SparepartManualTransactionId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(wdEntity.SparepartManualTransactionId); spManual.ModifyDate = serverTime; spManual.ModifyUserId = userId; spManual.QtyRemaining = spManual.QtyRemaining + 1; _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType); _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart); _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser); _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser); _sparepartManualTransactionRepository.Update(spManual); _unitOfWork.SaveChanges(); } } trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }
public void DeletePurchaseReturn(int purchaseReturnID, int userID) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_PURCHASERETURN).FirstOrDefault(); PurchaseReturn purchaseReturn = _purchaseReturnRepository.GetById(purchaseReturnID); purchaseReturn.Status = (int)DbConstant.DefaultDataStatus.Deleted; purchaseReturn.ModifyDate = serverTime; purchaseReturn.ModifyUserId = userID; _purchaseReturnRepository.AttachNavigation(purchaseReturn.CreateUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.ModifyUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.Purchasing); _purchaseReturnRepository.Update(purchaseReturn); _unitOfWork.SaveChanges(); List <PurchaseReturnDetail> listDetail = _purchaseReturnDetailRepository.GetMany(x => x.PurchaseReturnId == purchaseReturnID).ToList(); foreach (var itemDetail in listDetail) { itemDetail.Status = (int)DbConstant.DefaultDataStatus.Deleted; itemDetail.ModifyDate = serverTime; itemDetail.ModifyUserId = userID; _purchaseReturnDetailRepository.AttachNavigation(itemDetail.CreateUser); _purchaseReturnRepository.AttachNavigation(itemDetail.ModifyUser); _purchaseReturnRepository.AttachNavigation(itemDetail.PurchaseReturn); _purchaseReturnRepository.AttachNavigation(itemDetail.PurchasingDetail); _purchaseReturnRepository.AttachNavigation(itemDetail.SparepartManualTransaction); _purchaseReturnDetailRepository.Update(itemDetail); Sparepart sparepart = _sparepartRepository.GetById(itemDetail.PurchasingDetail.SparepartId); sparepart.StockQty += 1; PurchasingDetail purchasingDetail = _purchasingDetailRepository.GetById(itemDetail.PurchasingDetailId); purchasingDetail.QtyRemaining += itemDetail.Qty; _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser); _purchasingDetailRepository.Update(purchasingDetail); _sparepartRepository.AttachNavigation(sparepart.CreateUser); _sparepartRepository.AttachNavigation(sparepart.ModifyUser); _sparepartRepository.AttachNavigation(sparepart.CategoryReference); _sparepartRepository.AttachNavigation(sparepart.UnitReference); _sparepartRepository.Update(sparepart); SparepartStockCard stockCard = new SparepartStockCard(); stockCard.CreateUserId = userID; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = purchaseReturn.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = sparepart.Id; stockCard.Description = "Pembatalan Retur Pembelian"; stockCard.QtyIn = 1; stockCard.QtyInPrice = Convert.ToDouble(itemDetail.PurchasingDetail.Price); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock + stockCard.QtyIn; stockCard.QtyLastPrice = lastStockPrice + stockCard.QtyInPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(itemDetail.PurchasingDetail.Price); stockCardDtail.QtyIn = 1; stockCardDtail.QtyInPrice = Convert.ToDouble(itemDetail.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, purchaseReturn.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = purchaseReturn.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } _unitOfWork.SaveChanges(); Transaction transaction = _transactionRepository.GetMany(x => x.PrimaryKeyValue == purchaseReturnID && x.ReferenceTableId == transactionReferenceTable.Id).FirstOrDefault(); transaction.Status = (int)DbConstant.DefaultDataStatus.Deleted; transaction.ModifyDate = serverTime; transaction.ModifyUserId = userID; _transactionRepository.AttachNavigation(transaction.CreateUser); _transactionRepository.AttachNavigation(transaction.ModifyUser); _transactionRepository.AttachNavigation(transaction.PaymentMethod); _transactionRepository.AttachNavigation(transaction.ReferenceTable); _transactionRepository.Update(transaction); _unitOfWork.SaveChanges(); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } } }
public void RemoveSpecialSparepart(int specialSparepartDetailId, int userId) { DateTime serverTime = DateTime.Now; SpecialSparepartDetail sspdEntity = _specialSparepartDetailRepository.GetById(specialSparepartDetailId); sspdEntity.ModifyDate = serverTime; sspdEntity.ModifyUserId = userId; sspdEntity.Status = (int)DbConstant.WheelDetailStatus.Deleted; _specialSparepartDetailRepository.AttachNavigation(sspdEntity.Sparepart); _specialSparepartDetailRepository.AttachNavigation(sspdEntity.CreateUser); _specialSparepartDetailRepository.AttachNavigation(sspdEntity.ModifyUser); _specialSparepartDetailRepository.Update(sspdEntity); _unitOfWork.SaveChanges(); _unitOfWork.SaveChanges(); Sparepart spEntity = _sparepartRepository.GetById(sspdEntity.SparepartId); spEntity.ModifyDate = serverTime; spEntity.ModifyUserId = userId; if (spEntity.StockQty > 0) { spEntity.StockQty = spEntity.StockQty - 1; } _sparepartRepository.AttachNavigation(spEntity.UnitReference); _sparepartRepository.AttachNavigation(spEntity.CategoryReference); _sparepartRepository.AttachNavigation(spEntity.CreateUser); _sparepartRepository.AttachNavigation(spEntity.ModifyUser); _sparepartRepository.Update(spEntity); _unitOfWork.SaveChanges(); SparepartStockCard stockCard = new SparepartStockCard(); Reference transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_MANUAL).FirstOrDefault(); stockCard.CreateUserId = userId; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = sspdEntity.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = spEntity.Id; stockCard.Description = "Remove Special Sparepart Detail"; stockCard.QtyOut = 1; stockCard.QtyOutPrice = Convert.ToDouble(sspdEntity.PurchasingDetail != null ? sspdEntity.PurchasingDetail.Price : sspdEntity.SparepartManualTransaction != null ? sspdEntity.SparepartManualTransaction.Price : 0); SparepartStockCard lastStockCard = _sparepartStockCardRepository.RetrieveLastCard(spEntity.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock - stockCard.QtyOut; stockCard.QtyLastPrice = lastStockPrice - stockCard.QtyOutPrice; _sparepartStockCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStockCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStockCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStockCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (sspdEntity.PurchasingDetail != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(sspdEntity.PurchasingDetail.Price); stockCardDtail.QtyOut = 1; stockCardDtail.QtyOutPrice = Convert.ToDouble(sspdEntity.PurchasingDetail.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sspdEntity.Sparepart.Id, sspdEntity.PurchasingDetail.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.PurchasingId = sspdEntity.PurchasingDetail.PurchasingId; _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStockCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); PurchasingDetail pDetail = _purchasingDetailRepository.GetById(sspdEntity.PurchasingDetailId); pDetail.ModifyDate = serverTime; pDetail.ModifyUserId = userId; pDetail.QtyRemaining = pDetail.QtyRemaining - 1; _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart); _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser); _purchasingDetailRepository.Update(pDetail); _unitOfWork.SaveChanges(); } if (sspdEntity.SparepartManualTransaction != null) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(sspdEntity.SparepartManualTransaction.Price); stockCardDtail.QtyOut = 1; stockCardDtail.QtyOutPrice = Convert.ToDouble(1 * sspdEntity.SparepartManualTransaction.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sspdEntity.Sparepart.Id, sspdEntity.SparepartManualTransactionId.Value); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.SparepartManualTransactionId = sspdEntity.SparepartManualTransactionId; _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStockCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(sspdEntity.SparepartManualTransactionId); spManual.ModifyDate = serverTime; spManual.ModifyUserId = userId; spManual.QtyRemaining = spManual.QtyRemaining - 1; _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType); _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart); _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser); _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser); _sparepartManualTransactionRepository.Update(spManual); _unitOfWork.SaveChanges(); } _unitOfWork.SaveChanges(); }
public void DeletePurchaseReturn(int purchaseReturnID, int userID) { using (var trans = _unitOfWork.BeginTransaction()) { try { //delete old purchase return DateTime serverTime = DateTime.Now; Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_PURCHASERETURN).FirstOrDefault(); PurchaseReturn purchaseReturn = _purchaseReturnRepository.GetById(purchaseReturnID); purchaseReturn.Status = (int)DbConstant.DefaultDataStatus.Deleted; purchaseReturn.ModifyDate = serverTime; purchaseReturn.ModifyUserId = userID; _purchaseReturnRepository.AttachNavigation(purchaseReturn.CreateUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.ModifyUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.Purchasing); _purchaseReturnRepository.Update(purchaseReturn); _unitOfWork.SaveChanges(); List <PurchaseReturnDetail> listDetail = _purchaseReturnDetailRepository.GetMany(x => x.PurchaseReturnId == purchaseReturnID).ToList(); foreach (var item in listDetail) { if (item.Qty > 0) { PurchasingDetail purchasingDetail = _purchasingDetailRepository.GetById(item.PurchasingDetailId); purchasingDetail.QtyRemaining += item.Qty; _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Sparepart); _purchasingDetailRepository.Update(purchasingDetail); SpecialSparepartDetail spDetail = _specialSparepartDetailRepository.GetMany(x => x.PurchasingDetailId == purchasingDetail.Id).FirstOrDefault(); if (spDetail != null) { spDetail.Status = (int)DbConstant.WheelDetailStatus.Ready; _specialSparepartDetailRepository.AttachNavigation(spDetail.CreateUser); _specialSparepartDetailRepository.AttachNavigation(spDetail.ModifyUser); _specialSparepartDetailRepository.AttachNavigation(spDetail.PurchasingDetail); _specialSparepartDetailRepository.AttachNavigation(spDetail.Sparepart); _specialSparepartDetailRepository.AttachNavigation(spDetail.SparepartManualTransaction); _specialSparepartDetailRepository.Update(spDetail); } _unitOfWork.SaveChanges(); } } List <ReturnViewModel> listReturn = listDetail .GroupBy(l => l.PurchasingDetail.Sparepart) .Select(cl => new ReturnViewModel { SparepartId = cl.First().PurchasingDetail.SparepartId, ReturQty = cl.Sum(c => c.Qty), SubTotalFee = cl.Sum(c => c.PurchasingDetail.Price * c.Qty), PricePerItem = cl.First().PurchasingDetail.Price, }).ToList(); foreach (var itemDetail in listDetail) { itemDetail.Status = (int)DbConstant.DefaultDataStatus.Deleted; itemDetail.ModifyDate = serverTime; itemDetail.ModifyUserId = userID; _purchaseReturnDetailRepository.AttachNavigation(itemDetail.CreateUser); _purchaseReturnDetailRepository.AttachNavigation(itemDetail.ModifyUser); _purchaseReturnDetailRepository.AttachNavigation(itemDetail.PurchaseReturn); _purchaseReturnDetailRepository.AttachNavigation(itemDetail.PurchasingDetail); _purchaseReturnDetailRepository.Update(itemDetail); } foreach (var itemReturn in listReturn) { Sparepart sparepart = _sparepartRepository.GetById(itemReturn.SparepartId); sparepart.StockQty += itemReturn.ReturQty; _sparepartRepository.AttachNavigation(sparepart.CreateUser); _sparepartRepository.AttachNavigation(sparepart.ModifyUser); _sparepartRepository.AttachNavigation(sparepart.CategoryReference); _sparepartRepository.AttachNavigation(sparepart.UnitReference); _sparepartRepository.Update(sparepart); SparepartStockCard stockCard = new SparepartStockCard(); stockCard.CreateUserId = userID; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = purchaseReturn.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = sparepart.Id; stockCard.Description = "Pembatalan Retur Pembelian"; stockCard.QtyIn = itemReturn.ReturQty; stockCard.QtyInPrice = itemReturn.SubTotalFee.AsDouble(); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock + stockCard.QtyIn; stockCard.QtyLastPrice = lastStockPrice + stockCard.QtyInPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(itemReturn.PricePerItem); stockCardDtail.QtyIn = itemReturn.ReturQty; stockCardDtail.QtyInPrice = itemReturn.SubTotalFee.AsDouble(); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, purchaseReturn.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail.QtyIn != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail + stockCardDtail.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = purchaseReturn.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } _unitOfWork.SaveChanges(); Transaction transaction = _transactionRepository.GetMany(x => x.PrimaryKeyValue == purchaseReturnID && x.ReferenceTableId == transactionReferenceTable.Id).FirstOrDefault(); transaction.Status = (int)DbConstant.DefaultDataStatus.Deleted; transaction.ModifyDate = serverTime; transaction.ModifyUserId = userID; _transactionRepository.AttachNavigation(transaction.CreateUser); _transactionRepository.AttachNavigation(transaction.ModifyUser); _transactionRepository.AttachNavigation(transaction.PaymentMethod); _transactionRepository.AttachNavigation(transaction.ReferenceTable); _transactionRepository.Update(transaction); Purchasing purchasing = _purchasingRepository.GetById(purchaseReturn.PurchasingId); purchasing.Status = (int)DbConstant.PurchasingStatus.Active; if (purchasing.TotalPrice != purchasing.TotalHasPaid && (purchasing.TotalPrice - purchasing.TotalHasPaid) >= (decimal)transaction.TotalTransaction) { purchasing.TotalHasPaid -= (decimal)transaction.TotalTransaction; } if (purchasing.TotalPrice == purchasing.TotalHasPaid) { purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.Settled; } else { purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.NotSettled; } _purchasingRepository.AttachNavigation(purchasing.CreateUser); _purchasingRepository.AttachNavigation(purchasing.ModifyUser); _purchasingRepository.AttachNavigation(purchasing.PaymentMethod); _purchasingRepository.AttachNavigation(purchasing.Supplier); _unitOfWork.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }
public void DeleteSalesReturn(int salesReturnID, int userID) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_SALESRETURN).FirstOrDefault(); SalesReturn salesReturn = _salesReturnRepository.GetById(salesReturnID); salesReturn.Status = (int)DbConstant.DefaultDataStatus.Deleted; salesReturn.ModifyDate = serverTime; salesReturn.ModifyUserId = userID; _salesReturnRepository.AttachNavigation(salesReturn.CreateUser); _salesReturnRepository.AttachNavigation(salesReturn.ModifyUser); _salesReturnRepository.AttachNavigation(salesReturn.Invoice); _salesReturnRepository.Update(salesReturn); _unitOfWork.SaveChanges(); List <SalesReturnDetail> listDetail = _salesReturnDetailRepository.GetMany(x => x.SalesReturnId == salesReturnID).ToList(); List <ReturnViewModel> listReturn = listDetail .GroupBy(l => l.InvoiceDetail.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId) .Select(cl => new ReturnViewModel { SparepartId = cl.First().InvoiceDetail.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId, ReturQty = cl.Count(), }).ToList(); List <PurchasingDetail> listPurchasingDetail = new List <PurchasingDetail>(); List <SparepartManualTransaction> listSparepartManualTrans = new List <SparepartManualTransaction>(); foreach (var itemDetail in listDetail) { itemDetail.Status = (int)DbConstant.DefaultDataStatus.Deleted; itemDetail.ModifyDate = serverTime; itemDetail.ModifyUserId = userID; _salesReturnDetailRepository.AttachNavigation(itemDetail.CreateUser); _salesReturnDetailRepository.AttachNavigation(itemDetail.ModifyUser); _salesReturnDetailRepository.AttachNavigation(itemDetail.SalesReturn); _salesReturnDetailRepository.AttachNavigation(itemDetail.InvoiceDetail); _salesReturnDetailRepository.Update(itemDetail); if (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail != null) { listPurchasingDetail.Add(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail); } if (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction != null) { listSparepartManualTrans.Add(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction); } } foreach (var itemReturn in listReturn) { Sparepart sparepart = _sparepartRepository.GetById(itemReturn.SparepartId); sparepart.StockQty -= itemReturn.ReturQty; _sparepartRepository.AttachNavigation(sparepart.CreateUser); _sparepartRepository.AttachNavigation(sparepart.ModifyUser); _sparepartRepository.AttachNavigation(sparepart.CategoryReference); _sparepartRepository.AttachNavigation(sparepart.UnitReference); _sparepartRepository.Update(sparepart); SparepartStockCard stockCard = new SparepartStockCard(); stockCard.CreateUserId = userID; stockCard.PurchaseDate = serverTime; stockCard.PrimaryKeyValue = salesReturn.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = sparepart.Id; stockCard.Description = "Pembatalan Retur Penjualan"; stockCard.QtyOut = itemReturn.ReturQty; stockCard.QtyOutPrice = Convert.ToDouble(listPurchasingDetail.Sum(x => x.Price) + listSparepartManualTrans.Sum(x => x.Price)); SparepartStockCard lastStockCard = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id); double lastStock = 0; double lastStockPrice = 0; if (lastStockCard != null) { lastStock = lastStockCard.QtyLast; lastStockPrice = lastStockCard.QtyLastPrice; } stockCard.QtyFirst = lastStock; stockCard.QtyFirstPrice = lastStockPrice; stockCard.QtyLast = lastStock - stockCard.QtyOut; stockCard.QtyLastPrice = lastStockPrice - stockCard.QtyOutPrice; _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser); _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart); _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable); stockCard = _sparepartStokCardRepository.Add(stockCard); _unitOfWork.SaveChanges(); if (listPurchasingDetail.Count > 0) { List <PurchasingDetailViewModel> listPurchasing = listPurchasingDetail .GroupBy(l => l.PurchasingId) .Select(cl => new PurchasingDetailViewModel { PurchasingId = cl.Key, Qty = cl.Count(), Price = cl.First().Price }).ToList(); foreach (var itemPurchasing in listPurchasing) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(itemPurchasing.Price); stockCardDtail.QtyOut = itemPurchasing.Qty; stockCardDtail.QtyOutPrice = Convert.ToDouble(itemPurchasing.Qty * itemPurchasing.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, itemPurchasing.PurchasingId); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.PurchasingId = itemPurchasing.PurchasingId; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); PurchasingDetail pDetail = _purchasingDetailRepository.GetById(itemPurchasing.Id); pDetail.ModifyDate = serverTime; pDetail.ModifyUserId = userID; pDetail.QtyRemaining = pDetail.QtyRemaining - itemPurchasing.Qty; _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart); _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser); _purchasingDetailRepository.Update(pDetail); _unitOfWork.SaveChanges(); } } if (listSparepartManualTrans.Count > 0) { List <SparepartManualTransactionViewModel> listSpManual = listSparepartManualTrans .GroupBy(l => l.Id) .Select(cl => new SparepartManualTransactionViewModel { Id = cl.Key, Qty = cl.Count(), Price = cl.First().Price }).ToList(); foreach (var itemSpTrans in listSpManual) { SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(itemSpTrans.Price); stockCardDtail.QtyOut = 1; stockCardDtail.QtyOutPrice = Convert.ToDouble(1 * itemSpTrans.Price); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sparepart.Id, itemSpTrans.Id); double lastStockDetail = 0; double lastStockDetailPrice = 0; if (lastStockCardDetail != null) { lastStockDetail = lastStockCardDetail.QtyLast; lastStockDetailPrice = lastStockCardDetail.QtyLastPrice; } stockCardDtail.QtyFirst = lastStockDetail; stockCardDtail.QtyFirstPrice = lastStockDetailPrice; stockCardDtail.QtyLast = lastStockDetail - stockCardDtail.QtyOut; stockCardDtail.QtyLastPrice = lastStockDetailPrice - stockCardDtail.QtyOutPrice; stockCardDtail.SparepartManualTransactionId = itemSpTrans.Id; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(itemSpTrans.Id); spManual.ModifyDate = serverTime; spManual.ModifyUserId = userID; spManual.QtyRemaining = spManual.QtyRemaining - 1; _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType); _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart); _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser); _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser); _sparepartManualTransactionRepository.Update(spManual); _unitOfWork.SaveChanges(); } } } _unitOfWork.SaveChanges(); Transaction transaction = _transactionRepository.GetMany(x => x.PrimaryKeyValue == salesReturnID && x.ReferenceTableId == transactionReferenceTable.Id).FirstOrDefault(); transaction.Status = (int)DbConstant.DefaultDataStatus.Deleted; transaction.ModifyDate = serverTime; transaction.ModifyUserId = userID; _transactionRepository.AttachNavigation(transaction.CreateUser); _transactionRepository.AttachNavigation(transaction.ModifyUser); _transactionRepository.AttachNavigation(transaction.PaymentMethod); _transactionRepository.AttachNavigation(transaction.ReferenceTable); _transactionRepository.Update(transaction); Invoice invoice = _invoiceRepository.GetById(salesReturn.InvoiceId); invoice.Status = (int)DbConstant.InvoiceStatus.Printed; invoice.ModifyDate = serverTime; invoice.ModifyUserId = userID; if (invoice.TotalPrice != invoice.TotalHasPaid && (invoice.TotalPrice - invoice.TotalHasPaid) >= (decimal)transaction.TotalTransaction) { invoice.TotalHasPaid -= (decimal)transaction.TotalTransaction; } if (invoice.TotalPrice == invoice.TotalHasPaid) { invoice.PaymentStatus = (int)DbConstant.PaymentStatus.Settled; } else { invoice.PaymentStatus = (int)DbConstant.PaymentStatus.NotSettled; } _invoiceRepository.AttachNavigation(invoice.CreateUser); _invoiceRepository.AttachNavigation(invoice.ModifyUser); _invoiceRepository.AttachNavigation(invoice.PaymentMethod); _invoiceRepository.AttachNavigation(invoice.SPK); _unitOfWork.SaveChanges(); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } } }