public void Recalculate(Purchasing purchasing) { List <PurchasingDetail> listPurchasingDetail = _purchasingDetailRepository.GetMany(c => c.PurchasingId == purchasing.Id).ToList(); decimal totalPrice = 0; foreach (var itemPD in listPurchasingDetail) { totalPrice += itemPD.Qty * itemPD.Price; } purchasing.TotalPrice = totalPrice; _purchasingRepository.AttachNavigation(purchasing.CreateUser); _purchasingRepository.AttachNavigation(purchasing.ModifyUser); _purchasingRepository.AttachNavigation(purchasing.PaymentMethod); _purchasingRepository.AttachNavigation(purchasing.Supplier); _purchasingRepository.Update(purchasing); _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 Approve(PurchasingViewModel purchasing, int userID) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_PURCHASING).FirstOrDefault(); List <PurchasingDetail> listPurchasingDetail = _purchasingDetailRepository .GetMany(c => c.PurchasingId == purchasing.Id).ToList(); foreach (var purchasingDetail in listPurchasingDetail) { Sparepart sparepartDB = _sparepartRepository.GetById(purchasingDetail.SparepartId); for (int i = 1; i <= purchasingDetail.Qty; i++) { if (!string.IsNullOrEmpty(purchasingDetail.SerialNumber)) { SpecialSparepartDetail wd = new SpecialSparepartDetail(); wd.PurchasingDetail = purchasingDetail; wd.SerialNumber = purchasingDetail.SerialNumber; wd.CreateUserId = userID; wd.CreateDate = serverTime; wd.ModifyUserId = userID; wd.ModifyDate = serverTime; wd.SparepartId = sparepartDB.Id; wd.Status = (int)DbConstant.WheelDetailStatus.Ready; _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.CreateUser); _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.ModifyUser); _specialSparepartDetailRepository.AttachNavigation(purchasingDetail); _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.Sparepart);; _specialSparepartDetailRepository.Add(wd); } } purchasingDetail.Status = (int)DbConstant.PurchasingStatus.Active; purchasingDetail.QtyRemaining = purchasingDetail.Qty; _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Purchasing); _purchasingDetailRepository.AttachNavigation(purchasingDetail.Sparepart); _purchasingDetailRepository.Update(purchasingDetail); Sparepart sparepart = _sparepartRepository.GetById(purchasingDetail.SparepartId); sparepart.StockQty += purchasingDetail.Qty; _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 = purchasing.Id; stockCard.ReferenceTableId = transactionReferenceTable.Id; stockCard.SparepartId = sparepart.Id; stockCard.Description = "Purchasing"; stockCard.QtyIn = purchasingDetail.Qty; stockCard.QtyInPrice = Convert.ToDouble(purchasingDetail.Price * purchasingDetail.Qty); 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(purchasingDetail.Price); stockCardDtail.QtyIn = purchasingDetail.Qty; stockCardDtail.QtyInPrice = Convert.ToDouble(purchasingDetail.Price * purchasingDetail.Qty); SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, purchasing.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.QtyIn; stockCardDtail.QtyLastPrice = lastStockDetailPrice + stockCardDtail.QtyInPrice; stockCardDtail.PurchasingId = purchasing.Id; _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard); _sparepartStokCardDetailRepository.Add(stockCardDtail); _unitOfWork.SaveChanges(); } Reference refSelected = _referenceRepository.GetById(purchasing.PaymentMethodId); purchasing.Status = (int)DbConstant.PurchasingStatus.Active; if (refSelected != null && (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1 || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2 || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_KAS) ) { purchasing.TotalHasPaid = purchasing.TotalPrice; } if (purchasing.TotalHasPaid != purchasing.TotalPrice) { purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.NotSettled; } else { purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.Settled; } Purchasing entity = _purchasingRepository.GetById(purchasing.Id); //Map(purchasing, entity); entity.PaymentStatus = purchasing.PaymentStatus; entity.Status = purchasing.Status; entity.TotalHasPaid = purchasing.TotalHasPaid; entity.TotalPrice = purchasing.TotalPrice; entity.PaymentMethodId = purchasing.PaymentMethodId; _purchasingRepository.AttachNavigation(entity.CreateUser); _purchasingRepository.AttachNavigation(entity.ModifyUser); _purchasingRepository.AttachNavigation(entity.PaymentMethod); _purchasingRepository.AttachNavigation(entity.Supplier); _purchasingRepository.Update(entity); _unitOfWork.SaveChanges(); Transaction transaction = new Transaction(); transaction.TransactionDate = purchasing.Date; transaction.TotalPayment = Convert.ToDouble(purchasing.TotalHasPaid); transaction.TotalTransaction = Convert.ToDouble(purchasing.TotalPrice); transaction.ReferenceTableId = transactionReferenceTable.Id; transaction.PrimaryKeyValue = purchasing.Id; transaction.CreateDate = serverTime; transaction.CreateUserId = userID; transaction.ModifyUserId = userID; transaction.ModifyDate = serverTime; transaction.Status = (int)DbConstant.DefaultDataStatus.Active; transaction.Description = "Pembelian sparepart"; transaction.PaymentMethodId = purchasing.PaymentMethodId; _transactionRepository.AttachNavigation(transaction.CreateUser); _transactionRepository.AttachNavigation(transaction.ModifyUser); _transactionRepository.AttachNavigation(transaction.PaymentMethod); _transactionRepository.AttachNavigation(transaction.ReferenceTable); Transaction transactionInserted = _transactionRepository.Add(transaction); _unitOfWork.SaveChanges(); switch (refSelected.Code) { case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI: case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1: case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2: { // Bank Kredit --> Karena berkurang TransactionDetail detailBank = new TransactionDetail(); detailBank.Credit = purchasing.TotalHasPaid; if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI) { detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id; } else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1) { detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id; } else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2) { detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id; } detailBank.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailBank.Journal); _transactionDetailRepository.AttachNavigation(detailBank.Parent); _transactionDetailRepository.Add(detailBank); break; } case DbConstant.REF_PURCHASE_PAYMENTMETHOD_KAS: // Kas Kredit --> Karena berkurang TransactionDetail detailKas = new TransactionDetail(); detailKas.Credit = purchasing.TotalHasPaid; detailKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id; detailKas.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailKas.Journal); _transactionDetailRepository.AttachNavigation(detailKas.Parent); _transactionDetailRepository.Add(detailKas); break; case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_KAS: // Kas Kredit --> Karena berkurang TransactionDetail detailKasKarenaUangMuka = new TransactionDetail(); detailKasKarenaUangMuka.Credit = purchasing.TotalHasPaid; detailKasKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id; detailKasKarenaUangMuka.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailKasKarenaUangMuka.Journal); _transactionDetailRepository.AttachNavigation(detailKasKarenaUangMuka.Parent); _transactionDetailRepository.Add(detailKasKarenaUangMuka); // Uang Muka Debit --> Karena bertambah TransactionDetail detailUangMukaBertambahKarenaKas = new TransactionDetail(); detailUangMukaBertambahKarenaKas.Debit = purchasing.TotalHasPaid; detailUangMukaBertambahKarenaKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id; detailUangMukaBertambahKarenaKas.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaKas.Journal); _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaKas.Parent); _transactionDetailRepository.Add(detailUangMukaBertambahKarenaKas); // Uang Muka Kredit --> Karena berkurang TransactionDetail detailUangMukaBerkurangKarenaKas = new TransactionDetail(); detailUangMukaBerkurangKarenaKas.Credit = purchasing.TotalHasPaid; detailUangMukaBerkurangKarenaKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id; detailUangMukaBerkurangKarenaKas.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaKas.Journal); _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaKas.Parent); _transactionDetailRepository.Add(detailUangMukaBerkurangKarenaKas); break; case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI: case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1: case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2: { // Bank Kredit --> Karena berkurang TransactionDetail detailBankKarenaUangMuka = new TransactionDetail(); detailBankKarenaUangMuka.Credit = purchasing.TotalHasPaid; if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI) { detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id; } else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1) { detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id; } else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2) { detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id; } detailBankKarenaUangMuka.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailBankKarenaUangMuka.Journal); _transactionDetailRepository.AttachNavigation(detailBankKarenaUangMuka.Parent); _transactionDetailRepository.Add(detailBankKarenaUangMuka); // Uang Muka Debit --> Karena bertambah TransactionDetail detailUangMukaBertambahKarenaBank = new TransactionDetail(); detailUangMukaBertambahKarenaBank.Debit = purchasing.TotalHasPaid; detailUangMukaBertambahKarenaBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id; detailUangMukaBertambahKarenaBank.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaBank.Journal); _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaBank.Parent); _transactionDetailRepository.Add(detailUangMukaBertambahKarenaBank); // Uang Muka Kredit --> Karena berkurang TransactionDetail detailUangMukaBerkurangKarenaBank = new TransactionDetail(); detailUangMukaBerkurangKarenaBank.Credit = purchasing.TotalHasPaid; detailUangMukaBerkurangKarenaBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id; detailUangMukaBerkurangKarenaBank.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaBank.Journal); _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaBank.Parent); _transactionDetailRepository.Add(detailUangMukaBerkurangKarenaBank); break; } case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UTANG: TransactionDetail utang = new TransactionDetail(); utang.Credit = purchasing.TotalPrice - purchasing.TotalHasPaid; utang.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id; utang.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(utang.Journal); _transactionDetailRepository.AttachNavigation(utang.Parent); _transactionDetailRepository.Add(utang); break; } if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1 || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2 || refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_KAS) { if (purchasing.TotalPrice > purchasing.TotalHasPaid) { // Utang Kredit --> Karena bertambah TransactionDetail utang = new TransactionDetail(); utang.Credit = purchasing.TotalPrice - purchasing.TotalHasPaid; utang.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id; utang.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(utang.Journal); _transactionDetailRepository.AttachNavigation(utang.Parent); _transactionDetailRepository.Add(utang); } } // Sparepart Debit --> Karena bertambah TransactionDetail detailSparepart = new TransactionDetail(); detailSparepart.Debit = purchasing.TotalPrice; detailSparepart.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.04.01").FirstOrDefault().Id; detailSparepart.ParentId = transactionInserted.Id; _transactionDetailRepository.AttachNavigation(detailSparepart.Journal); _transactionDetailRepository.AttachNavigation(detailSparepart.Parent); _transactionDetailRepository.Add(detailSparepart); _unitOfWork.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }
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(); }