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;
                }
            }
        }
Exemple #2
0
        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 PurchaseReturn GetDetailsById(int id)
        {
            var data = _PurchaseReturnRepository.GetById(id);

            return(data);
        }