public void FixSPM(int sparepartId)
        {
            DateTime serverTime = DateTime.Now;

            Sparepart sparepart = _sparepartRepository.GetById(sparepartId);

            if (sparepart != null)
            {
                // update sparepart to last stock
                SparepartStockCard lastStock = _sparepartStockCardRepository.GetMany(sc => sc.SparepartId == sparepartId).LastOrDefault();

                if (lastStock != null)
                {
                    sparepart.StockQty = lastStock.QtyLast.AsInteger();
                    _sparepartRepository.Update(sparepart);
                }

                //update spm by stockcarddetail
                List <SparepartManualTransaction> spmList = _sparepartManualTransactionRepository.GetMany(spm => spm.SparepartId == sparepartId).ToList();

                foreach (SparepartManualTransaction spm in spmList)
                {
                    SparepartStockCardDetail lastStockDetail = _sparepartStockCardDetailRepository.GetMany(scd => scd.SparepartManualTransactionId == spm.Id).LastOrDefault();

                    if (lastStockDetail != null)
                    {
                        spm.QtyRemaining = lastStockDetail.QtyLast.AsInteger();
                        _sparepartManualTransactionRepository.Update(spm);
                    }
                }
            }

            _unitOfWork.SaveChanges();
        }
Beispiel #2
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;
                }
            }
        }
Beispiel #3
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;
                }
            }
        }
Beispiel #4
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();
        }
Beispiel #5
0
        public void UpdateSparepartManualTransaction(SparepartManualTransactionViewModel sparepartManualTransaction, int userId)
        {
            DateTime serverTime = DateTime.Now;

            sparepartManualTransaction.ModifyDate   = serverTime;
            sparepartManualTransaction.ModifyUserId = userId;
            sparepartManualTransaction.QtyRemaining = sparepartManualTransaction.Qty;
            Reference updateTypeNew = _referenceRepository.GetById(sparepartManualTransaction.UpdateTypeId);
            SparepartManualTransaction manualTransactionOld = _sparepartManualTransactionRepository.GetById(sparepartManualTransaction.Id);
            Sparepart sparepartUpdated = _sparepartRepository.GetById(sparepartManualTransaction.SparepartId);

            if (manualTransactionOld != null && updateTypeNew != null && sparepartUpdated != null)
            {
                Reference updateTypeOld = _referenceRepository.GetById(manualTransactionOld.UpdateTypeId);

                Reference          transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_SPAREPARTMANUAL).FirstOrDefault();
                SparepartStockCard stockCard = new SparepartStockCard();
                stockCard.CreateUserId     = userId;
                stockCard.PurchaseDate     = serverTime;
                stockCard.ReferenceTableId = transactionReferenceTable.Id;
                stockCard.PrimaryKeyValue  = manualTransactionOld.Id;
                stockCard.SparepartId      = sparepartManualTransaction.SparepartId;

                SparepartStockCardDetail stockCardDetail = new SparepartStockCardDetail();
                stockCardDetail.SparepartManualTransactionId = manualTransactionOld.Id;

                SparepartStockCard lastStockCard = _sparepartStockCardRepository.RetrieveLastCard(sparepartManualTransaction.SparepartId);
                double             lastStock     = 0;
                double             lastPrice     = 0;
                if (lastStockCard != null)
                {
                    lastStock = lastStockCard.QtyLast;
                    lastPrice = lastStockCard.PricePerItem;
                }

                stockCard.QtyFirst            = lastStock;
                stockCard.QtyFirstPrice       = (stockCard.QtyFirst * lastPrice).AsDouble();
                stockCardDetail.QtyFirst      = stockCard.QtyFirst;
                stockCardDetail.QtyFirstPrice = stockCard.QtyFirstPrice;

                if (updateTypeOld.Code == (DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_PLUS))
                {
                    stockCard.Description = "Revisi pembatalan penambahan stok awal";
                    stockCard.QtyOut      = manualTransactionOld.Qty;
                    stockCard.QtyOutPrice = stockCard.QtyOut * manualTransactionOld.Price.AsDouble();

                    stockCardDetail.QtyOut      = manualTransactionOld.Qty;
                    stockCardDetail.QtyOutPrice = stockCardDetail.QtyOut * manualTransactionOld.Price.AsDouble();

                    sparepartUpdated.StockQty -= manualTransactionOld.Qty;
                }
                else if (updateTypeOld.Code == DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_MINUS)
                {
                    stockCard.Description = "Revisi pembatalan pengurangan stok awal";
                    stockCard.QtyIn       = manualTransactionOld.Qty;
                    stockCard.QtyInPrice  = stockCard.QtyIn * manualTransactionOld.Price.AsDouble();

                    stockCardDetail.QtyIn      = manualTransactionOld.Qty;
                    stockCardDetail.QtyInPrice = stockCardDetail.QtyIn * manualTransactionOld.Price.AsDouble();

                    sparepartUpdated.StockQty += manualTransactionOld.Qty;
                }

                stockCard.QtyLast      = stockCard.QtyFirst + (stockCard.QtyIn - stockCard.QtyOut);
                stockCard.QtyLastPrice = stockCard.QtyLast * lastPrice;

                stockCardDetail.QtyLast      = stockCard.QtyLast;
                stockCardDetail.QtyLastPrice = stockCard.QtyLastPrice;

                _sparepartStockCardRepository.AttachNavigation(stockCard.CreateUser);
                _sparepartStockCardRepository.AttachNavigation(stockCard.Sparepart);
                _sparepartStockCardRepository.AttachNavigation(stockCard.ReferenceTable);
                stockCard = _sparepartStockCardRepository.Add(stockCard);

                stockCardDetail.ParentStockCard = stockCard;
                _sparepartStockCardDetailRepository.AttachNavigation(stockCardDetail.ParentStockCard);
                _sparepartStockCardDetailRepository.Add(stockCardDetail);

                _unitOfWork.SaveChanges();

                SparepartStockCard revStockCard = new SparepartStockCard();
                revStockCard.CreateUserId     = userId;
                revStockCard.PurchaseDate     = serverTime;
                revStockCard.ReferenceTableId = transactionReferenceTable.Id;
                revStockCard.PrimaryKeyValue  = manualTransactionOld.Id;
                revStockCard.SparepartId      = sparepartManualTransaction.SparepartId;

                SparepartStockCardDetail revStockCardDetail = new SparepartStockCardDetail();
                revStockCardDetail.SparepartManualTransactionId = sparepartManualTransaction.Id;

                lastStockCard = _sparepartStockCardRepository.RetrieveLastCard(sparepartManualTransaction.SparepartId);
                lastStock     = 0;
                lastPrice     = 0;
                if (lastStockCard != null)
                {
                    lastStock = lastStockCard.QtyLast;
                    lastPrice = lastStockCard.PricePerItem;
                }

                revStockCard.QtyFirst      = lastStock;
                revStockCard.QtyFirstPrice = revStockCard.QtyFirst * lastPrice;

                revStockCardDetail.QtyFirst      = revStockCard.QtyFirst;
                revStockCardDetail.QtyFirstPrice = revStockCard.QtyFirstPrice;

                if (updateTypeNew.Code == DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_PLUS)
                {
                    revStockCard.Description = "Revisi penambahan stok awal";
                    revStockCard.QtyIn       = sparepartManualTransaction.Qty;
                    revStockCard.QtyInPrice  = revStockCard.QtyIn * lastPrice;

                    revStockCardDetail.QtyIn      = revStockCard.QtyIn;
                    revStockCardDetail.QtyInPrice = revStockCard.QtyInPrice;

                    sparepartUpdated.StockQty += sparepartManualTransaction.Qty;
                }
                else if (updateTypeNew.Code == DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_MINUS)
                {
                    revStockCard.Description = "Revisi pengurangan stok awal";
                    revStockCard.QtyOut      = sparepartManualTransaction.Qty;
                    revStockCard.QtyOutPrice = revStockCard.QtyOut * lastPrice;

                    revStockCardDetail.QtyOut      = revStockCard.QtyOut;
                    revStockCardDetail.QtyOutPrice = revStockCard.QtyOutPrice;

                    sparepartUpdated.StockQty -= sparepartManualTransaction.Qty;
                }

                revStockCard.QtyLast      = revStockCard.QtyFirst + (revStockCard.QtyIn - revStockCard.QtyOut);
                revStockCard.QtyLastPrice = revStockCard.QtyLast * lastPrice;

                revStockCardDetail.QtyLast      = revStockCard.QtyLast;
                revStockCardDetail.QtyLastPrice = revStockCard.QtyLastPrice;

                _sparepartStockCardRepository.AttachNavigation(revStockCard.CreateUser);
                _sparepartStockCardRepository.AttachNavigation(revStockCard.Sparepart);
                _sparepartStockCardRepository.AttachNavigation(revStockCard.ReferenceTable);
                revStockCard = _sparepartStockCardRepository.Add(revStockCard);

                revStockCardDetail.ParentStockCard = revStockCard;
                _sparepartStockCardDetailRepository.AttachNavigation(revStockCardDetail.ParentStockCard);
                _sparepartStockCardDetailRepository.Add(revStockCardDetail);

                SparepartManualTransaction entity = new SparepartManualTransaction();
                Map(sparepartManualTransaction, entity);
                _sparepartManualTransactionRepository.Update(entity);
                _sparepartRepository.Update(sparepartUpdated);

                _unitOfWork.SaveChanges();
            }
        }
        public void DeleteSalesReturn(int salesReturnID, int userID)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

                    Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_SALESRETURN).FirstOrDefault();

                    SalesReturn salesReturn = _salesReturnRepository.GetById(salesReturnID);
                    salesReturn.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                    salesReturn.ModifyDate   = serverTime;
                    salesReturn.ModifyUserId = userID;

                    _salesReturnRepository.AttachNavigation(salesReturn.CreateUser);
                    _salesReturnRepository.AttachNavigation(salesReturn.ModifyUser);
                    _salesReturnRepository.AttachNavigation(salesReturn.Invoice);
                    _salesReturnRepository.Update(salesReturn);
                    _unitOfWork.SaveChanges();

                    List <SalesReturnDetail> listDetail = _salesReturnDetailRepository.GetMany(x => x.SalesReturnId == salesReturnID).ToList();

                    List <ReturnViewModel> listReturn = listDetail
                                                        .GroupBy(l => l.InvoiceDetail.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId)
                                                        .Select(cl => new ReturnViewModel
                    {
                        SparepartId = cl.First().InvoiceDetail.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId,
                        ReturQty    = cl.Count(),
                    }).ToList();

                    List <PurchasingDetail>           listPurchasingDetail     = new List <PurchasingDetail>();
                    List <SparepartManualTransaction> listSparepartManualTrans = new List <SparepartManualTransaction>();
                    foreach (var itemDetail in listDetail)
                    {
                        itemDetail.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                        itemDetail.ModifyDate   = serverTime;
                        itemDetail.ModifyUserId = userID;

                        _salesReturnDetailRepository.AttachNavigation(itemDetail.CreateUser);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.ModifyUser);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.SalesReturn);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.InvoiceDetail);
                        _salesReturnDetailRepository.Update(itemDetail);

                        if (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail != null)
                        {
                            listPurchasingDetail.Add(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail);
                        }
                        if (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction != null)
                        {
                            listSparepartManualTrans.Add(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction);
                        }
                    }

                    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  = salesReturn.Id;
                        stockCard.ReferenceTableId = transactionReferenceTable.Id;
                        stockCard.SparepartId      = sparepart.Id;
                        stockCard.Description      = "Pembatalan Retur Penjualan";
                        stockCard.QtyOut           = itemReturn.ReturQty;
                        stockCard.QtyOutPrice      = Convert.ToDouble(listPurchasingDetail.Sum(x => x.Price) + listSparepartManualTrans.Sum(x => x.Price));
                        SparepartStockCard lastStockCard  = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id);
                        double             lastStock      = 0;
                        double             lastStockPrice = 0;
                        if (lastStockCard != null)
                        {
                            lastStock      = lastStockCard.QtyLast;
                            lastStockPrice = lastStockCard.QtyLastPrice;
                        }
                        stockCard.QtyFirst      = lastStock;
                        stockCard.QtyFirstPrice = lastStockPrice;
                        stockCard.QtyLast       = lastStock - stockCard.QtyOut;
                        stockCard.QtyLastPrice  = lastStockPrice - stockCard.QtyOutPrice;
                        _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                        stockCard = _sparepartStokCardRepository.Add(stockCard);

                        _unitOfWork.SaveChanges();

                        if (listPurchasingDetail.Count > 0)
                        {
                            List <PurchasingDetailViewModel> listPurchasing = listPurchasingDetail
                                                                              .GroupBy(l => l.PurchasingId)
                                                                              .Select(cl => new PurchasingDetailViewModel
                            {
                                PurchasingId = cl.Key,
                                Qty          = cl.Count(),
                                Price        = cl.First().Price
                            }).ToList();

                            foreach (var itemPurchasing in listPurchasing)
                            {
                                SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                                stockCardDtail.ParentStockCard = stockCard;
                                stockCardDtail.PricePerItem    = Convert.ToDouble(itemPurchasing.Price);
                                stockCardDtail.QtyOut          = itemPurchasing.Qty;
                                stockCardDtail.QtyOutPrice     = Convert.ToDouble(itemPurchasing.Qty * itemPurchasing.Price);
                                SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, itemPurchasing.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  = itemPurchasing.PurchasingId;

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

                                PurchasingDetail pDetail = _purchasingDetailRepository.GetById(itemPurchasing.Id);
                                pDetail.ModifyDate   = serverTime;
                                pDetail.ModifyUserId = userID;
                                pDetail.QtyRemaining = pDetail.QtyRemaining - itemPurchasing.Qty;

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

                        if (listSparepartManualTrans.Count > 0)
                        {
                            List <SparepartManualTransactionViewModel> listSpManual = listSparepartManualTrans
                                                                                      .GroupBy(l => l.Id)
                                                                                      .Select(cl => new SparepartManualTransactionViewModel
                            {
                                Id    = cl.Key,
                                Qty   = cl.Count(),
                                Price = cl.First().Price
                            }).ToList();
                            foreach (var itemSpTrans in listSpManual)
                            {
                                SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                                stockCardDtail.ParentStockCard = stockCard;
                                stockCardDtail.PricePerItem    = Convert.ToDouble(itemSpTrans.Price);
                                stockCardDtail.QtyOut          = 1;
                                stockCardDtail.QtyOutPrice     = Convert.ToDouble(1 * itemSpTrans.Price);
                                SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sparepart.Id, itemSpTrans.Id);
                                double lastStockDetail      = 0;
                                double lastStockDetailPrice = 0;
                                if (lastStockCardDetail != null)
                                {
                                    lastStockDetail      = lastStockCardDetail.QtyLast;
                                    lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                                }
                                stockCardDtail.QtyFirst      = lastStockDetail;
                                stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                                stockCardDtail.QtyLast       = lastStockDetail - stockCardDtail.QtyOut;
                                stockCardDtail.QtyLastPrice  = lastStockDetailPrice - stockCardDtail.QtyOutPrice;
                                stockCardDtail.SparepartManualTransactionId = itemSpTrans.Id;

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

                                SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(itemSpTrans.Id);
                                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();

                    Transaction transaction = _transactionRepository.GetMany(x => x.PrimaryKeyValue == salesReturnID && 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);

                    Invoice invoice = _invoiceRepository.GetById(salesReturn.InvoiceId);
                    invoice.Status       = (int)DbConstant.InvoiceStatus.Printed;
                    invoice.ModifyDate   = serverTime;
                    invoice.ModifyUserId = userID;
                    if (invoice.TotalPrice != invoice.TotalHasPaid && (invoice.TotalPrice - invoice.TotalHasPaid) >= (decimal)transaction.TotalTransaction)
                    {
                        invoice.TotalHasPaid -= (decimal)transaction.TotalTransaction;
                    }

                    if (invoice.TotalPrice == invoice.TotalHasPaid)
                    {
                        invoice.PaymentStatus = (int)DbConstant.PaymentStatus.Settled;
                    }
                    else
                    {
                        invoice.PaymentStatus = (int)DbConstant.PaymentStatus.NotSettled;
                    }

                    _invoiceRepository.AttachNavigation(invoice.CreateUser);
                    _invoiceRepository.AttachNavigation(invoice.ModifyUser);
                    _invoiceRepository.AttachNavigation(invoice.PaymentMethod);
                    _invoiceRepository.AttachNavigation(invoice.SPK);

                    _unitOfWork.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
        public void DeleteSalesReturn(int salesReturnID, int userID)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

                    Reference   transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_SALESRETURN).FirstOrDefault();
                    SalesReturn salesReturn = _salesReturnRepository.GetById(salesReturnID);
                    salesReturn.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                    salesReturn.ModifyDate   = serverTime;
                    salesReturn.ModifyUserId = userID;

                    _salesReturnRepository.AttachNavigation(salesReturn.CreateUser);
                    _salesReturnRepository.AttachNavigation(salesReturn.ModifyUser);
                    _salesReturnRepository.AttachNavigation(salesReturn.Invoice);
                    _salesReturnRepository.Update(salesReturn);
                    _unitOfWork.SaveChanges();

                    List <SalesReturnDetail> listDetail = _salesReturnDetailRepository.GetMany(x => x.SalesReturnId == salesReturnID).ToList();
                    foreach (var itemDetail in listDetail)
                    {
                        itemDetail.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                        itemDetail.ModifyDate   = serverTime;
                        itemDetail.ModifyUserId = userID;

                        _salesReturnDetailRepository.AttachNavigation(itemDetail.CreateUser);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.ModifyUser);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.SalesReturn);
                        _salesReturnDetailRepository.AttachNavigation(itemDetail.InvoiceDetail);
                        _salesReturnDetailRepository.Update(itemDetail);

                        Sparepart sparepart = _sparepartRepository.GetById(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId);
                        sparepart.StockQty -= 1;

                        _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  = salesReturn.Id;
                        stockCard.ReferenceTableId = transactionReferenceTable.Id;
                        stockCard.SparepartId      = sparepart.Id;
                        stockCard.Description      = "Pembatalan Retur Penjualan";
                        stockCard.QtyOut           = 1;
                        stockCard.QtyOutPrice      = Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail != null ? itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price : itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction != null ? itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction.Price : 0);
                        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 - 1;
                        stockCard.QtyLastPrice  = lastStockPrice - Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail != null ? itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price : itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction != null ? itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction.Price : 0);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                        stockCard = _sparepartStokCardRepository.Add(stockCard);

                        _unitOfWork.SaveChanges();

                        if (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail != null)
                        {
                            SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                            stockCardDtail.ParentStockCard = stockCard;
                            stockCardDtail.PricePerItem    = Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price);
                            stockCardDtail.QtyOut          = 1;
                            stockCardDtail.QtyOutPrice     = Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price);
                            SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, itemDetail.InvoiceDetail.SPKDetailSparepartDetail.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  = itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.PurchasingId;

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

                            PurchasingDetail pDetail = _purchasingDetailRepository.GetById(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.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 (itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction != null)
                        {
                            SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                            stockCardDtail.ParentStockCard = stockCard;
                            stockCardDtail.PricePerItem    = Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price);
                            stockCardDtail.QtyOut          = 1;
                            stockCardDtail.QtyOutPrice     = Convert.ToDouble(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.PurchasingDetail.Price);
                            SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByTransactionManualId(sparepart.Id, itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction.Id);
                            double lastStockDetail      = 0;
                            double lastStockDetailPrice = 0;
                            if (lastStockCardDetail != null)
                            {
                                lastStockDetail      = lastStockCardDetail.QtyLast;
                                lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                            }
                            stockCardDtail.QtyFirst      = lastStockDetail;
                            stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                            stockCardDtail.QtyLast       = lastStockDetail - stockCardDtail.QtyOut;
                            stockCardDtail.QtyLastPrice  = lastStockDetailPrice - stockCardDtail.QtyOutPrice;
                            stockCardDtail.SparepartManualTransactionId = itemDetail.InvoiceDetail.SPKDetailSparepartDetail.SparepartManualTransaction.Id;

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

                            SparepartManualTransaction spManual = _sparepartManualTransactionRepository.GetById(itemDetail.InvoiceDetail.SPKDetailSparepartDetail.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();

                    Transaction transaction = _transactionRepository.GetMany(x => x.PrimaryKeyValue == salesReturnID && x.ReferenceTableId == transactionReferenceTable.Id).FirstOrDefault();
                    transaction.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                    transaction.ModifyDate   = serverTime;
                    transaction.ModifyUserId = userID;

                    _transactionRepository.AttachNavigation(transaction.CreateUser);
                    _transactionRepository.AttachNavigation(transaction.ModifyUser);
                    _transactionRepository.AttachNavigation(transaction.PaymentMethod);
                    _transactionRepository.AttachNavigation(transaction.ReferenceTable);
                    _transactionRepository.Update(transaction);

                    _unitOfWork.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }