public void InsertPurchaseReturnFunc(int purchasingID, List <ReturnViewModel> listReturn, int userID) { DateTime serverTime = DateTime.Now; Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_PURCHASERETURN).FirstOrDefault(); PurchaseReturn purchaseReturn = new PurchaseReturn(); purchaseReturn.CreateDate = serverTime; purchaseReturn.CreateUserId = userID; purchaseReturn.ModifyDate = serverTime; purchaseReturn.ModifyUserId = userID; purchaseReturn.PurchasingId = purchasingID; purchaseReturn.Date = serverTime; purchaseReturn.Status = (int)DbConstant.DefaultDataStatus.Active; string code = "PRR" + "-" + serverTime.Month.ToString() + serverTime.Day.ToString() + "-"; //get total purchasing return created today List <PurchaseReturn> todayPRR = _purchaseReturnRepository.GetMany(s => s.Code.ToString().Contains(code) && s.CreateDate.Year == serverTime.Year).ToList(); code = code + (todayPRR.Count + 1); purchaseReturn.Code = code; _purchaseReturnRepository.AttachNavigation(purchaseReturn.CreateUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.ModifyUser); _purchaseReturnRepository.AttachNavigation(purchaseReturn.Purchasing); purchaseReturn = _purchaseReturnRepository.Add(purchaseReturn); _unitOfWork.SaveChanges(); List <PurchaseReturnDetail> listReturnDetail = new List <PurchaseReturnDetail>(); decimal totalTransaction = 0; foreach (var itemReturn in listReturn) { PurchasingDetail purchasingDetail = _purchasingDetailRepository.GetMany(x => x.Id == itemReturn.PurchasingDetailId).FirstOrDefault(); listReturnDetail.Add(new PurchaseReturnDetail { CreateDate = serverTime, CreateUserId = userID, ModifyDate = serverTime, ModifyUserId = userID, PurchaseReturnId = purchaseReturn.Id, PurchasingDetailId = purchasingDetail.Id, Qty = itemReturn.ReturQty, Status = (int)DbConstant.DefaultDataStatus.Active }); itemReturn.PricePerItem = purchasingDetail.Price; if (itemReturn.ReturQty > 0) { totalTransaction += itemReturn.ReturQty * purchasingDetail.Price; purchasingDetail.QtyRemaining -= itemReturn.ReturQty; SpecialSparepartDetail spDetail = _specialSparepartDetailRepository.GetMany(x => x.PurchasingDetailId == purchasingDetail.Id).FirstOrDefault(); if (spDetail != null) { spDetail.Status = (int)DbConstant.WheelDetailStatus.Deleted; _specialSparepartDetailRepository.AttachNavigation(spDetail.CreateUser); _specialSparepartDetailRepository.AttachNavigation(spDetail.ModifyUser); _specialSparepartDetailRepository.AttachNavigation(spDetail.PurchasingDetail); _specialSparepartDetailRepository.AttachNavigation(spDetail.Sparepart); _specialSparepartDetailRepository.AttachNavigation(spDetail.SparepartManualTransaction); _specialSparepartDetailRepository.Update(spDetail); } } _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Sparepart); _purchasingDetailRepository.Update(purchasingDetail); _unitOfWork.SaveChanges(); } List <ReturnViewModel> listReturnGroup = listReturn .GroupBy(l => l.SparepartId) .Select(cl => new ReturnViewModel { SparepartId = cl.First().SparepartId, ReturQty = cl.Sum(c => c.ReturQty), SubTotalFee = cl.Sum(c => c.PricePerItem * c.ReturQty), PricePerItem = cl.First().PricePerItem, }).ToList(); foreach (var itemReturnGroup in listReturnGroup) { Sparepart sparepart = _sparepartRepository.GetById(itemReturnGroup.SparepartId); sparepart.ModifyDate = serverTime; sparepart.ModifyUserId = userID; sparepart.StockQty -= itemReturnGroup.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 = "Retur Pembelian"; stockCard.QtyOut = itemReturnGroup.ReturQty; stockCard.QtyOutPrice = Convert.ToDouble(itemReturnGroup.SubTotalFee); 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(); SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail(); stockCardDtail.ParentStockCard = stockCard; stockCardDtail.PricePerItem = Convert.ToDouble(itemReturnGroup.PricePerItem); stockCardDtail.QtyOut = itemReturnGroup.ReturQty; stockCardDtail.QtyOutPrice = Convert.ToDouble(itemReturnGroup.SubTotalFee); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, 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 = purchasingID; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } Purchasing purchasing = _purchasingRepository.GetById(purchaseReturn.PurchasingId); purchasing.Status = (int)DbConstant.PurchasingStatus.HasReturn; if (purchasing.TotalPrice != purchasing.TotalHasPaid && (purchasing.TotalPrice - purchasing.TotalHasPaid) >= totalTransaction) { purchasing.TotalHasPaid += 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(); foreach (var itemReturnDetail in listReturnDetail) { _purchaseReturnDetailRepository.AttachNavigation(itemReturnDetail.CreateUser); _purchaseReturnRepository.AttachNavigation(itemReturnDetail.ModifyUser); _purchaseReturnRepository.AttachNavigation(itemReturnDetail.PurchaseReturn); _purchaseReturnRepository.AttachNavigation(itemReturnDetail.PurchasingDetail); _purchaseReturnDetailRepository.Add(itemReturnDetail); } _unitOfWork.SaveChanges(); Transaction transaction = new Transaction(); transaction.CreateDate = serverTime; transaction.CreateUserId = userID; transaction.ModifyDate = serverTime; transaction.ModifyUserId = userID; transaction.PrimaryKeyValue = purchaseReturn.Id; transaction.ReferenceTableId = transactionReferenceTable.Id; transaction.TotalPayment = totalTransaction.AsDouble(); transaction.TotalTransaction = totalTransaction.AsDouble(); transaction.Status = (int)DbConstant.DefaultDataStatus.Active; transaction.Description = "Retur Pembelian"; transaction.TransactionDate = serverTime; _transactionRepository.AttachNavigation(transaction.CreateUser); _transactionRepository.AttachNavigation(transaction.ModifyUser); _transactionRepository.AttachNavigation(transaction.PaymentMethod); _transactionRepository.AttachNavigation(transaction.ReferenceTable); transaction = _transactionRepository.Add(transaction); _unitOfWork.SaveChanges(); TransactionDetail transDebit = new TransactionDetail(); transDebit.Debit = totalTransaction; transDebit.ParentId = transaction.Id; transDebit.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id; _transactionDetailRepository.AttachNavigation(transDebit.Journal); _transactionDetailRepository.AttachNavigation(transDebit.Parent); _transactionDetailRepository.Add(transDebit); TransactionDetail transCredit = new TransactionDetail(); transCredit.Credit = totalTransaction; transCredit.ParentId = transaction.Id; transCredit.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.04.01").FirstOrDefault().Id; _transactionDetailRepository.AttachNavigation(transCredit.Journal); _transactionDetailRepository.AttachNavigation(transCredit.Parent); _transactionDetailRepository.Add(transCredit); _unitOfWork.SaveChanges(); }
public IActionResult AddPurchaseReturn([FromBody] PurchaseReturn model) { model.ReturnNumber = GenRN(); return_repo.Add(model); return(new OkObjectResult(new { PurchaseReturnID = model.PurchaseReturnId })); }
public void Create(PurchaseReturn PurchaseReturn) { _PurchaseReturnRepository.Add(PurchaseReturn); _UnitOfWork.Commit(); }