Exemplo n.º 1
0
        public List <SpecialSparepartDetailViewModel> RetrieveReadyWheelDetails()
        {
            List <SpecialSparepartDetail> result = _specialSparepartDetailRepository.GetMany(wd => wd.Status == (int)DbConstant.WheelDetailStatus.Ready
                                                                                             //&& wd.SpecialSparepart.ReferenceCategory.Code == DbConstant.REF_SPECIAL_SPAREPART_TYPE_WHEEL
                                                                                             ).ToList();
            List <SpecialSparepartDetailViewModel> mappedResult = new List <SpecialSparepartDetailViewModel>();

            return(Map(result, mappedResult));
        }
        public void DeleteSparepart(SparepartViewModel sparepart, int userId)
        {
            DateTime serverTime = DateTime.Now;
            List <SpecialSparepartDetail> details = _specialSparepartDetailRepository.GetMany(spd => spd.SparepartId == sparepart.Id).ToList();

            foreach (var iDetails in details)
            {
                iDetails.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                iDetails.ModifyDate   = serverTime;
                iDetails.ModifyUserId = userId;
                _specialSparepartDetailRepository.AttachNavigation(iDetails.CreateUser);
                _specialSparepartDetailRepository.AttachNavigation(iDetails.ModifyUser);
                _specialSparepartDetailRepository.AttachNavigation(iDetails.PurchasingDetail);
                _specialSparepartDetailRepository.AttachNavigation(iDetails.Sparepart);
                _specialSparepartDetailRepository.AttachNavigation(iDetails.SparepartManualTransaction);
                _specialSparepartDetailRepository.Update(iDetails);
            }

            sparepart.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
            sparepart.ModifyDate   = serverTime;
            sparepart.ModifyUserId = userId;
            Sparepart entity = _sparepartRepository.GetById(sparepart.Id);

            Map(sparepart, entity);
            _sparepartRepository.AttachNavigation(entity.CreateUser);
            _sparepartRepository.AttachNavigation(entity.ModifyUser);
            _sparepartRepository.AttachNavigation(entity.CategoryReference);
            _sparepartRepository.AttachNavigation(entity.UnitReference);
            _sparepartRepository.Update(entity);

            _unitOfWork.SaveChanges();
        }
Exemplo n.º 3
0
        public List <SpecialSparepartDetailViewModel> SearchDetail(int sparepartId, string serialNumber, DbConstant.WheelDetailStatus status)
        {
            List <SpecialSparepartDetail> result;

            if (!string.IsNullOrWhiteSpace(serialNumber))
            {
                result = _specialSparepartDetailRepository.GetMany(whd =>
                                                                   whd.Status == (int)status &&
                                                                   whd.SparepartId == sparepartId &&
                                                                   whd.SerialNumber.ToLower().Contains(serialNumber.ToLower())).ToList();
            }
            else
            {
                result = _specialSparepartDetailRepository.GetMany(whd => whd.Status == (int)status &&
                                                                   whd.SparepartId == sparepartId).ToList();
            }

            List <SpecialSparepartDetailViewModel> mappedResult = new List <SpecialSparepartDetailViewModel>();

            return(Map(result, mappedResult));
        }
        public bool IsSerialNumberExist(string sn)
        {
            SpecialSparepartDetail ssd = _specialSparepartDetailRepository.GetMany(dtl => dtl.SerialNumber.ToLower() == sn.ToLower() && dtl.Status != (int)DbConstant.WheelDetailStatus.Deleted).FirstOrDefault();

            if (ssd != null)
            {
                if (ssd.Status == (int)DbConstant.SparepartDetailDataStatus.OutService)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }
        public void DeleteWheel(SpecialSparepartViewModel SpecialSparepart, int userId)
        {
            DateTime serverTime = DateTime.Now;
            List <SpecialSparepartDetail> details = _specialSparepartDetailRepository.GetMany(spd => spd.SpecialSparepartId == SpecialSparepart.Id).ToList();

            foreach (var iDetails in details)
            {
                iDetails.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                iDetails.ModifyDate   = serverTime;
                iDetails.ModifyUserId = userId;
                _specialSparepartDetailRepository.Update(iDetails);
            }

            SpecialSparepart.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
            SpecialSparepart.ModifyDate   = serverTime;
            SpecialSparepart.ModifyUserId = userId;
            SpecialSparepart entity = _specialSparepartRepository.GetById(SpecialSparepart.Id);

            Map(SpecialSparepart, entity);
            _specialSparepartRepository.Update(entity);

            _unitOfWork.SaveChanges();
        }
Exemplo n.º 6
0
        public void RepairInvoice()
        {
            DateTime start      = new DateTime(2018, 11, 1);
            DateTime serverTime = DateTime.Now;

            List <Invoice> invList = _invoiceRepository.GetMany(i => i.CreateDate >= start).ToList();

            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    foreach (Invoice inv in invList)
                    {
                        List <InvoiceDetail> invDetails = _invoiceDetailRepository.GetMany(ivd => ivd.InvoiceId == inv.Id).ToList();

                        List <SPKDetailSparepart> spkSps = _spkDetailSparepartRepository.GetMany(spkspd => spkspd.SPKId == inv.SPKId).ToList();
                        List <int> spkSpsId = spkSps.Select(x => x.Id).ToList();
                        List <SPKDetailSparepartDetail> spkSPDetails = _spkDetailSparepartDetailRepository.GetMany(spkspdt => spkSpsId.Contains(spkspdt.SPKDetailSparepartId)).ToList();

                        if (spkSPDetails.Count < spkSps.Count)
                        {
                            spkSps = spkSps.Where(spksp => !spkSPDetails.Any(spkspdt => spkspdt.SPKDetailSparepartId == spksp.Id)).ToList();

                            foreach (SPKDetailSparepart item in spkSps)
                            {
                                SpecialSparepartDetail          sspd = _specialSparepartDetailRepository.GetMany(e => e.SparepartId == item.SparepartId).FirstOrDefault();
                                List <SPKDetailSparepartDetail> newSPKdetailSparepartDetails = getRandomSPKDetails(item.SparepartId, item.TotalQuantity, sspd != null? sspd.Id : 0);

                                foreach (SPKDetailSparepartDetail spkDetailSparepartDetail in newSPKdetailSparepartDetails)
                                {
                                    SPKDetailSparepartDetail newSPKSparepartDetail = new SPKDetailSparepartDetail();

                                    newSPKSparepartDetail.CreateDate           = item.CreateDate;
                                    newSPKSparepartDetail.CreateUserId         = item.CreateUserId;
                                    newSPKSparepartDetail.ModifyDate           = serverTime;
                                    newSPKSparepartDetail.ModifyUserId         = item.ModifyUserId;
                                    newSPKSparepartDetail.Status               = (int)DbConstant.DefaultDataStatus.Active;
                                    newSPKSparepartDetail.SPKDetailSparepartId = item.Id;
                                    newSPKSparepartDetail.Qty = spkDetailSparepartDetail.Qty;

                                    if (sspd != null)
                                    {
                                        newSPKSparepartDetail.SpecialSparepartDetailId = sspd.Id;
                                        sspd.Status = (int)DbConstant.WheelDetailStatus.Installed;
                                        _specialSparepartDetailRepository.Update(sspd);
                                    }

                                    if (spkDetailSparepartDetail.PurchasingDetailId.HasValue)
                                    {
                                        newSPKSparepartDetail.PurchasingDetailId = spkDetailSparepartDetail.PurchasingDetailId;

                                        PurchasingDetail pdt = spkDetailSparepartDetail.PurchasingDetail;

                                        pdt.ModifyDate   = serverTime;
                                        pdt.ModifyUserId = item.CreateUserId;

                                        _purchasingDetailRepository.Update(pdt);
                                    }
                                    else if (spkDetailSparepartDetail.SparepartManualTransactionId.HasValue)
                                    {
                                        newSPKSparepartDetail.SparepartManualTransactionId = spkDetailSparepartDetail.SparepartManualTransactionId;

                                        SparepartManualTransaction spm = spkDetailSparepartDetail.SparepartManualTransaction;

                                        spm.ModifyDate   = serverTime;
                                        spm.ModifyUserId = item.CreateUserId;

                                        _sparepartManualTransactionRepository.Update(spm);
                                    }


                                    SPKDetailSparepartDetail insertedSPKSpDtl = _spkDetailSparepartDetailRepository.Add(newSPKSparepartDetail);

                                    InvoiceDetail invcDtl = new InvoiceDetail();

                                    invcDtl.InvoiceId = inv.Id;
                                    invcDtl.SPKDetailSparepartDetail = insertedSPKSpDtl;

                                    if (spkDetailSparepartDetail.PurchasingDetailId > 0)
                                    {
                                        invcDtl.SubTotalPrice = spkDetailSparepartDetail.PurchasingDetail.Price.AsDouble();
                                    }
                                    else
                                    {
                                        invcDtl.SubTotalPrice = spkDetailSparepartDetail.SparepartManualTransaction.Price.AsDouble();
                                    }

                                    invcDtl.Status  = (int)DbConstant.DefaultDataStatus.Active;
                                    invcDtl.FeePctg = 0;

                                    invcDtl.CreateDate   = serverTime;
                                    invcDtl.ModifyDate   = serverTime;
                                    invcDtl.ModifyUserId = item.CreateUserId;
                                    invcDtl.CreateUserId = item.CreateUserId;

                                    _invoiceDetailRepository.Add(invcDtl);

                                    _unitOfWork.SaveChanges();
                                }
                            }
                        }
                    }

                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
        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;
                }
            }
        }
Exemplo n.º 8
0
        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();
        }