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();
        }
        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();
        }
示例#3
0
        public void DeleteVehicle(VehicleViewModel vehicle, int userId)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

                    vehicle.ModifyDate   = serverTime;
                    vehicle.ModifyUserId = userId;
                    vehicle.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                    Vehicle entity = _vehicleRepository.GetById(vehicle.Id);
                    Map(vehicle, entity);

                    _vehicleRepository.AttachNavigation(entity.Brand);
                    _vehicleRepository.AttachNavigation(entity.Type);
                    _vehicleRepository.AttachNavigation(entity.VehicleGroup);
                    _vehicleRepository.AttachNavigation(entity.Customer);
                    _vehicleRepository.AttachNavigation(entity.CreateUser);
                    _vehicleRepository.AttachNavigation(entity.ModifyUser);
                    _vehicleRepository.Update(entity);
                    _unitOfWork.SaveChanges();

                    foreach (var item in _vehicleWheelRepository.GetMany(vw => vw.VehicleId == vehicle.Id && vw.Status == (int)DbConstant.DefaultDataStatus.Active))
                    {
                        item.ModifyDate   = serverTime;
                        item.ModifyUserId = userId;
                        item.Status       = (int)DbConstant.DefaultDataStatus.Deleted;

                        _vehicleWheelRepository.AttachNavigation(item.Vehicle);
                        _vehicleWheelRepository.AttachNavigation(item.WheelDetail);
                        _vehicleWheelRepository.AttachNavigation(item.CreateUser);
                        _vehicleWheelRepository.AttachNavigation(item.ModifyUser);
                        _vehicleWheelRepository.Update(item);
                        _unitOfWork.SaveChanges();

                        SpecialSparepartDetail wdEntity = _specialSparepartDetailRepository.GetById(item.WheelDetailId);
                        wdEntity.ModifyDate   = serverTime;
                        wdEntity.ModifyUserId = userId;
                        wdEntity.Status       = (int)DbConstant.WheelDetailStatus.Ready;

                        _specialSparepartDetailRepository.AttachNavigation(wdEntity.Sparepart);
                        _specialSparepartDetailRepository.AttachNavigation(wdEntity.PurchasingDetail);
                        _specialSparepartDetailRepository.AttachNavigation(wdEntity.SparepartManualTransaction);
                        _specialSparepartDetailRepository.AttachNavigation(wdEntity.CreateUser);
                        _specialSparepartDetailRepository.AttachNavigation(wdEntity.ModifyUser);
                        _specialSparepartDetailRepository.Update(wdEntity);
                        _unitOfWork.SaveChanges();

                        Sparepart spEntity = _sparepartRepository.GetById(wdEntity.SparepartId);
                        spEntity.ModifyDate   = serverTime;
                        spEntity.ModifyUserId = userId;
                        spEntity.StockQty     = spEntity.StockQty + 1;

                        _sparepartRepository.AttachNavigation(spEntity.UnitReference);
                        _sparepartRepository.AttachNavigation(spEntity.CategoryReference);
                        _sparepartRepository.AttachNavigation(spEntity.CreateUser);
                        _sparepartRepository.AttachNavigation(spEntity.ModifyUser);
                        _sparepartRepository.Update(spEntity);
                        _unitOfWork.SaveChanges();

                        SparepartStockCard stockCard = new SparepartStockCard();
                        Reference          transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_VEHICLE).FirstOrDefault();

                        stockCard.CreateUserId     = userId;
                        stockCard.PurchaseDate     = serverTime;
                        stockCard.PrimaryKeyValue  = vehicle.Id;
                        stockCard.ReferenceTableId = transactionReferenceTable.Id;
                        stockCard.SparepartId      = spEntity.Id;
                        stockCard.Description      = "Delete Vehicle";
                        stockCard.QtyIn            = 1;
                        stockCard.QtyInPrice       = Convert.ToDouble(wdEntity.PurchasingDetail != null ? wdEntity.PurchasingDetail.Price : wdEntity.SparepartManualTransaction != null ? wdEntity.SparepartManualTransaction.Price : 0);

                        SparepartStockCard lastStockCard  = _sparepartStokCardRepository.RetrieveLastCard(spEntity.Id);
                        double             lastStock      = 0;
                        double             lastStockPrice = 0;
                        if (lastStockCard != null)
                        {
                            lastStock      = lastStockCard.QtyLast;
                            lastStockPrice = lastStockCard.QtyLastPrice;
                        }
                        stockCard.QtyFirst      = lastStock;
                        stockCard.QtyFirstPrice = lastStockPrice;
                        stockCard.QtyLast       = lastStock + stockCard.QtyIn;
                        stockCard.QtyLastPrice  = lastStockPrice + stockCard.QtyInPrice;
                        _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                        stockCard = _sparepartStokCardRepository.Add(stockCard);
                        _unitOfWork.SaveChanges();

                        if (wdEntity.PurchasingDetail != null)
                        {
                            SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                            stockCardDtail.ParentStockCard = stockCard;
                            stockCardDtail.PricePerItem    = Convert.ToDouble(wdEntity.PurchasingDetail.Price);
                            stockCardDtail.QtyIn           = 1;
                            stockCardDtail.QtyInPrice      = Convert.ToDouble(wdEntity.PurchasingDetail.Price);
                            SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(wdEntity.Sparepart.Id, wdEntity.PurchasingDetail.PurchasingId);
                            double lastStockDetail      = 0;
                            double lastStockDetailPrice = 0;
                            if (lastStockCardDetail != null)
                            {
                                lastStockDetail      = lastStockCardDetail.QtyLast;
                                lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                            }
                            stockCardDtail.QtyFirst      = lastStockDetail;
                            stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                            stockCardDtail.QtyLast       = lastStockDetail + stockCardDtail.QtyIn;
                            stockCardDtail.QtyLastPrice  = lastStockDetailPrice + stockCardDtail.QtyInPrice;
                            stockCardDtail.PurchasingId  = wdEntity.PurchasingDetail.PurchasingId;

                            _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                            _sparepartStokCardDetailRepository.Add(stockCardDtail);
                            _unitOfWork.SaveChanges();

                            PurchasingDetail pDetail = _purchasingDetailRepository.GetById(wdEntity.PurchasingDetailId);
                            pDetail.ModifyDate   = serverTime;
                            pDetail.ModifyUserId = userId;
                            pDetail.QtyRemaining = pDetail.QtyRemaining + 1;

                            _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing);
                            _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart);
                            _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser);
                            _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser);
                            _purchasingDetailRepository.Update(pDetail);
                            _unitOfWork.SaveChanges();
                        }

                        if (wdEntity.SparepartManualTransaction != null)
                        {
                            SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                            stockCardDtail.ParentStockCard = stockCard;
                            stockCardDtail.PricePerItem    = Convert.ToDouble(wdEntity.SparepartManualTransaction.Price);
                            stockCardDtail.QtyIn           = 1;
                            stockCardDtail.QtyInPrice      = Convert.ToDouble(1 * wdEntity.SparepartManualTransaction.Price);
                            SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(wdEntity.Sparepart.Id, wdEntity.SparepartManualTransactionId.Value);
                            double lastStockDetail      = 0;
                            double lastStockDetailPrice = 0;
                            if (lastStockCardDetail != null)
                            {
                                lastStockDetail      = lastStockCardDetail.QtyLast;
                                lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                            }
                            stockCardDtail.QtyFirst      = lastStockDetail;
                            stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                            stockCardDtail.QtyLast       = lastStockDetail + stockCardDtail.QtyIn;
                            stockCardDtail.QtyLastPrice  = lastStockDetailPrice + stockCardDtail.QtyInPrice;
                            stockCardDtail.SparepartManualTransactionId = wdEntity.SparepartManualTransactionId;

                            _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                            _sparepartStokCardDetailRepository.Add(stockCardDtail);
                            _unitOfWork.SaveChanges();

                            SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(wdEntity.SparepartManualTransactionId);
                            spManual.ModifyDate   = serverTime;
                            spManual.ModifyUserId = userId;
                            spManual.QtyRemaining = spManual.QtyRemaining + 1;

                            _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType);
                            _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart);
                            _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser);
                            _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser);
                            _sparepartManualTransactionRepository.Update(spManual);
                            _unitOfWork.SaveChanges();
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
示例#4
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;
                }
            }
        }
示例#6
0
        public void RemoveSpecialSparepart(int specialSparepartDetailId, int userId)
        {
            DateTime serverTime = DateTime.Now;

            SpecialSparepartDetail sspdEntity = _specialSparepartDetailRepository.GetById(specialSparepartDetailId);

            sspdEntity.ModifyDate   = serverTime;
            sspdEntity.ModifyUserId = userId;
            sspdEntity.Status       = (int)DbConstant.WheelDetailStatus.Deleted;

            _specialSparepartDetailRepository.AttachNavigation(sspdEntity.Sparepart);
            _specialSparepartDetailRepository.AttachNavigation(sspdEntity.CreateUser);
            _specialSparepartDetailRepository.AttachNavigation(sspdEntity.ModifyUser);
            _specialSparepartDetailRepository.Update(sspdEntity);
            _unitOfWork.SaveChanges();

            _unitOfWork.SaveChanges();

            Sparepart spEntity = _sparepartRepository.GetById(sspdEntity.SparepartId);

            spEntity.ModifyDate   = serverTime;
            spEntity.ModifyUserId = userId;

            if (spEntity.StockQty > 0)
            {
                spEntity.StockQty = spEntity.StockQty - 1;
            }

            _sparepartRepository.AttachNavigation(spEntity.UnitReference);
            _sparepartRepository.AttachNavigation(spEntity.CategoryReference);
            _sparepartRepository.AttachNavigation(spEntity.CreateUser);
            _sparepartRepository.AttachNavigation(spEntity.ModifyUser);
            _sparepartRepository.Update(spEntity);
            _unitOfWork.SaveChanges();

            SparepartStockCard stockCard = new SparepartStockCard();
            Reference          transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_MANUAL).FirstOrDefault();

            stockCard.CreateUserId     = userId;
            stockCard.PurchaseDate     = serverTime;
            stockCard.PrimaryKeyValue  = sspdEntity.Id;
            stockCard.ReferenceTableId = transactionReferenceTable.Id;
            stockCard.SparepartId      = spEntity.Id;
            stockCard.Description      = "Remove Special Sparepart Detail";
            stockCard.QtyOut           = 1;
            stockCard.QtyOutPrice      = Convert.ToDouble(sspdEntity.PurchasingDetail != null ? sspdEntity.PurchasingDetail.Price : sspdEntity.SparepartManualTransaction != null ? sspdEntity.SparepartManualTransaction.Price : 0);

            SparepartStockCard lastStockCard  = _sparepartStockCardRepository.RetrieveLastCard(spEntity.Id);
            double             lastStock      = 0;
            double             lastStockPrice = 0;

            if (lastStockCard != null)
            {
                lastStock      = lastStockCard.QtyLast;
                lastStockPrice = lastStockCard.QtyLastPrice;
            }

            stockCard.QtyFirst      = lastStock;
            stockCard.QtyFirstPrice = lastStockPrice;
            stockCard.QtyLast       = lastStock - stockCard.QtyOut;
            stockCard.QtyLastPrice  = lastStockPrice - stockCard.QtyOutPrice;
            _sparepartStockCardRepository.AttachNavigation(stockCard.CreateUser);
            _sparepartStockCardRepository.AttachNavigation(stockCard.Sparepart);
            _sparepartStockCardRepository.AttachNavigation(stockCard.ReferenceTable);
            stockCard = _sparepartStockCardRepository.Add(stockCard);
            _unitOfWork.SaveChanges();

            if (sspdEntity.PurchasingDetail != null)
            {
                SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                stockCardDtail.ParentStockCard = stockCard;
                stockCardDtail.PricePerItem    = Convert.ToDouble(sspdEntity.PurchasingDetail.Price);
                stockCardDtail.QtyOut          = 1;
                stockCardDtail.QtyOutPrice     = Convert.ToDouble(sspdEntity.PurchasingDetail.Price);
                SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sspdEntity.Sparepart.Id, sspdEntity.PurchasingDetail.PurchasingId);
                double lastStockDetail      = 0;
                double lastStockDetailPrice = 0;
                if (lastStockCardDetail != null)
                {
                    lastStockDetail      = lastStockCardDetail.QtyLast;
                    lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                }
                stockCardDtail.QtyFirst      = lastStockDetail;
                stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                stockCardDtail.QtyLast       = lastStockDetail - stockCardDtail.QtyOut;
                stockCardDtail.QtyLastPrice  = lastStockDetailPrice - stockCardDtail.QtyOutPrice;
                stockCardDtail.PurchasingId  = sspdEntity.PurchasingDetail.PurchasingId;

                _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                _sparepartStockCardDetailRepository.Add(stockCardDtail);
                _unitOfWork.SaveChanges();

                PurchasingDetail pDetail = _purchasingDetailRepository.GetById(sspdEntity.PurchasingDetailId);
                pDetail.ModifyDate   = serverTime;
                pDetail.ModifyUserId = userId;
                pDetail.QtyRemaining = pDetail.QtyRemaining - 1;

                _purchasingDetailRepository.AttachNavigation(pDetail.Purchasing);
                _purchasingDetailRepository.AttachNavigation(pDetail.Sparepart);
                _purchasingDetailRepository.AttachNavigation(pDetail.CreateUser);
                _purchasingDetailRepository.AttachNavigation(pDetail.ModifyUser);
                _purchasingDetailRepository.Update(pDetail);
                _unitOfWork.SaveChanges();
            }

            if (sspdEntity.SparepartManualTransaction != null)
            {
                SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                stockCardDtail.ParentStockCard = stockCard;
                stockCardDtail.PricePerItem    = Convert.ToDouble(sspdEntity.SparepartManualTransaction.Price);
                stockCardDtail.QtyOut          = 1;
                stockCardDtail.QtyOutPrice     = Convert.ToDouble(1 * sspdEntity.SparepartManualTransaction.Price);
                SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sspdEntity.Sparepart.Id, sspdEntity.SparepartManualTransactionId.Value);
                double lastStockDetail      = 0;
                double lastStockDetailPrice = 0;
                if (lastStockCardDetail != null)
                {
                    lastStockDetail      = lastStockCardDetail.QtyLast;
                    lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                }
                stockCardDtail.QtyFirst      = lastStockDetail;
                stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                stockCardDtail.QtyLast       = lastStockDetail - stockCardDtail.QtyOut;
                stockCardDtail.QtyLastPrice  = lastStockDetailPrice - stockCardDtail.QtyOutPrice;
                stockCardDtail.SparepartManualTransactionId = sspdEntity.SparepartManualTransactionId;

                _sparepartStockCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                _sparepartStockCardDetailRepository.Add(stockCardDtail);
                _unitOfWork.SaveChanges();

                SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(sspdEntity.SparepartManualTransactionId);
                spManual.ModifyDate   = serverTime;
                spManual.ModifyUserId = userId;
                spManual.QtyRemaining = spManual.QtyRemaining - 1;

                _sparepartManualTransactionRepository.AttachNavigation(spManual.UpdateType);
                _sparepartManualTransactionRepository.AttachNavigation(spManual.Sparepart);
                _sparepartManualTransactionRepository.AttachNavigation(spManual.CreateUser);
                _sparepartManualTransactionRepository.AttachNavigation(spManual.ModifyUser);
                _sparepartManualTransactionRepository.Update(spManual);
                _unitOfWork.SaveChanges();
            }

            _unitOfWork.SaveChanges();
        }
示例#7
0
        public void RevertVehicleWheel(int vehicleWheelId, int userId)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

                    VehicleWheel vwEntity = _vehicleWheelRepository.GetById(vehicleWheelId);

                    _vehicleWheelRepository.AttachNavigation(vwEntity.Vehicle);
                    _vehicleWheelRepository.AttachNavigation(vwEntity.WheelDetail);
                    _vehicleWheelRepository.AttachNavigation(vwEntity.CreateUser);
                    _vehicleWheelRepository.AttachNavigation(vwEntity.ModifyUser);
                    _vehicleWheelRepository.Delete(vwEntity);
                    _unitOfWork.SaveChanges();

                    SpecialSparepartDetail wdEntity = _specialSparepartDetailRepository.GetById(vwEntity.WheelDetailId);
                    wdEntity.ModifyDate   = serverTime;
                    wdEntity.ModifyUserId = userId;
                    wdEntity.Status       = (int)DbConstant.WheelDetailStatus.Ready;

                    _specialSparepartDetailRepository.AttachNavigation(wdEntity.Sparepart);
                    _specialSparepartDetailRepository.AttachNavigation(wdEntity.CreateUser);
                    _specialSparepartDetailRepository.AttachNavigation(wdEntity.ModifyUser);
                    _specialSparepartDetailRepository.Update(wdEntity);
                    _unitOfWork.SaveChanges();

                    Sparepart spEntity = _sparepartRepository.GetById(wdEntity.SparepartId);
                    spEntity.ModifyDate   = serverTime;
                    spEntity.ModifyUserId = userId;
                    spEntity.StockQty     = spEntity.StockQty + 1;

                    _sparepartRepository.AttachNavigation(spEntity.UnitReference);
                    _sparepartRepository.AttachNavigation(spEntity.CategoryReference);
                    _sparepartRepository.AttachNavigation(spEntity.CreateUser);
                    _sparepartRepository.AttachNavigation(spEntity.ModifyUser);
                    _sparepartRepository.Update(spEntity);
                    _unitOfWork.SaveChanges();

                    SparepartStockCard stockCard = new SparepartStockCard();
                    Reference          transactionReferenceTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_VEHICLE).FirstOrDefault();

                    stockCard.CreateUserId     = userId;
                    stockCard.PurchaseDate     = serverTime;
                    stockCard.PrimaryKeyValue  = wdEntity.Id;
                    stockCard.ReferenceTableId = transactionReferenceTable.Id;
                    stockCard.SparepartId      = spEntity.Id;
                    stockCard.Description      = "Lepas Ban Kendaraan";
                    stockCard.QtyIn            = 1;
                    stockCard.QtyInPrice       = Convert.ToDouble(wdEntity.PurchasingDetail != null ? wdEntity.PurchasingDetail.Price : wdEntity.SparepartManualTransaction != null ? wdEntity.SparepartManualTransaction.Price : 0);

                    SparepartStockCard lastStockCard  = _sparepartStokCardRepository.RetrieveLastCard(spEntity.Id);
                    double             lastStock      = 0;
                    double             lastStockPrice = 0;
                    if (lastStockCard != null)
                    {
                        lastStock      = lastStockCard.QtyLast;
                        lastStockPrice = lastStockCard.QtyLastPrice;
                    }
                    stockCard.QtyFirst      = lastStock;
                    stockCard.QtyFirstPrice = lastStockPrice;
                    stockCard.QtyLast       = lastStock + stockCard.QtyIn;
                    stockCard.QtyLastPrice  = lastStockPrice + stockCard.QtyInPrice;
                    _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                    _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                    _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                    stockCard = _sparepartStokCardRepository.Add(stockCard);
                    _unitOfWork.SaveChanges();

                    if (wdEntity.PurchasingDetail != null)
                    {
                        SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                        stockCardDtail.ParentStockCard = stockCard;
                        stockCardDtail.PricePerItem    = Convert.ToDouble(wdEntity.PurchasingDetail.Price);
                        stockCardDtail.QtyIn           = 1;
                        stockCardDtail.QtyInPrice      = Convert.ToDouble(wdEntity.PurchasingDetail.Price);
                        SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(wdEntity.Sparepart.Id, wdEntity.PurchasingDetail.PurchasingId);
                        double lastStockDetail      = 0;
                        double lastStockDetailPrice = 0;
                        if (lastStockCardDetail != null)
                        {
                            lastStockDetail      = lastStockCardDetail.QtyLast;
                            lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                        }
                        stockCardDtail.QtyFirst      = lastStockDetail;
                        stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                        stockCardDtail.QtyLast       = lastStockDetail + stockCardDtail.QtyIn;
                        stockCardDtail.QtyLastPrice  = lastStockDetailPrice + stockCardDtail.QtyInPrice;
                        stockCardDtail.PurchasingId  = wdEntity.PurchasingDetail.PurchasingId;

                        _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                        _sparepartStokCardDetailRepository.Add(stockCardDtail);
                        _unitOfWork.SaveChanges();
                    }

                    if (wdEntity.SparepartManualTransaction != null)
                    {
                        SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                        stockCardDtail.ParentStockCard = stockCard;
                        stockCardDtail.PricePerItem    = Convert.ToDouble(wdEntity.SparepartManualTransaction.Price);
                        stockCardDtail.QtyIn           = 1;
                        stockCardDtail.QtyInPrice      = Convert.ToDouble(1 * wdEntity.SparepartManualTransaction.Price);
                        SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(wdEntity.Sparepart.Id, wdEntity.SparepartManualTransactionId.Value);
                        double lastStockDetail      = 0;
                        double lastStockDetailPrice = 0;
                        if (lastStockCardDetail != null)
                        {
                            lastStockDetail      = lastStockCardDetail.QtyLast;
                            lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                        }
                        stockCardDtail.QtyFirst      = lastStockDetail;
                        stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                        stockCardDtail.QtyLast       = lastStockDetail + stockCardDtail.QtyIn;
                        stockCardDtail.QtyLastPrice  = lastStockDetailPrice + stockCardDtail.QtyInPrice;
                        stockCardDtail.SparepartManualTransactionId = wdEntity.SparepartManualTransactionId;

                        _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                        _sparepartStokCardDetailRepository.Add(stockCardDtail);
                        _unitOfWork.SaveChanges();
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
示例#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();
        }