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();
        }
Ejemplo n.º 2
0
        public void UpdateSparepart(SparepartViewModel sparepart, int userId)
        {
            DateTime serverTime = DateTime.Now;

            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();
        }
        private List <InvoiceSparepartViewModel> GetInvoiceSparepartList(int invoiceID)
        {
            List <InvoiceSparepartViewModel> result            = new List <InvoiceSparepartViewModel>();
            List <InvoiceDetail>             listInvoiceDetail = _invoiceDetailRepository.GetMany(x => x.InvoiceId == invoiceID).ToList();

            int[] sparepartIDs = listInvoiceDetail.Select(x => x.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId).Distinct().ToArray();

            foreach (var sparepartID in sparepartIDs)
            {
                Sparepart            sparepart = _sparepartRepository.GetById(sparepartID);
                List <InvoiceDetail> listInvoiceDetailFilter = listInvoiceDetail.Where(x => x.SPKDetailSparepartDetail.SPKDetailSparepart.SparepartId == sparepartID).ToList();
                result.Add(new InvoiceSparepartViewModel
                {
                    SparepartName    = sparepart.Name,
                    Qty              = listInvoiceDetailFilter.Count(),
                    NominalFee       = listInvoiceDetailFilter.Sum(x => x.FeePctg > 0 ? (100 / (100 + x.FeePctg)) * x.SubTotalPrice : 0),
                    SubTotalPrice    = listInvoiceDetailFilter.Sum(x => x.SubTotalPrice),
                    SparepartCode    = sparepart.Code,
                    UnitCategoryName = sparepart.UnitReference.Name,
                });
            }
            return(result);
        }
Ejemplo n.º 4
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;
                }
            }
        }
Ejemplo n.º 5
0
        public void DeletePurchaseReturn(int purchaseReturnID, int userID)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

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

                    PurchaseReturn purchaseReturn = _purchaseReturnRepository.GetById(purchaseReturnID);
                    purchaseReturn.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                    purchaseReturn.ModifyDate   = serverTime;
                    purchaseReturn.ModifyUserId = userID;

                    _purchaseReturnRepository.AttachNavigation(purchaseReturn.CreateUser);
                    _purchaseReturnRepository.AttachNavigation(purchaseReturn.ModifyUser);
                    _purchaseReturnRepository.AttachNavigation(purchaseReturn.Purchasing);
                    _purchaseReturnRepository.Update(purchaseReturn);
                    _unitOfWork.SaveChanges();

                    List <PurchaseReturnDetail> listDetail = _purchaseReturnDetailRepository.GetMany(x => x.PurchaseReturnId == purchaseReturnID).ToList();
                    foreach (var itemDetail in listDetail)
                    {
                        itemDetail.Status       = (int)DbConstant.DefaultDataStatus.Deleted;
                        itemDetail.ModifyDate   = serverTime;
                        itemDetail.ModifyUserId = userID;

                        _purchaseReturnDetailRepository.AttachNavigation(itemDetail.CreateUser);
                        _purchaseReturnRepository.AttachNavigation(itemDetail.ModifyUser);
                        _purchaseReturnRepository.AttachNavigation(itemDetail.PurchaseReturn);
                        _purchaseReturnRepository.AttachNavigation(itemDetail.PurchasingDetail);
                        _purchaseReturnRepository.AttachNavigation(itemDetail.SparepartManualTransaction);
                        _purchaseReturnDetailRepository.Update(itemDetail);

                        Sparepart sparepart = _sparepartRepository.GetById(itemDetail.PurchasingDetail.SparepartId);
                        sparepart.StockQty += 1;


                        PurchasingDetail purchasingDetail = _purchasingDetailRepository.GetById(itemDetail.PurchasingDetailId);
                        purchasingDetail.QtyRemaining += itemDetail.Qty;
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser);
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser);
                        _purchasingDetailRepository.Update(purchasingDetail);

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

                        SparepartStockCard stockCard = new SparepartStockCard();
                        stockCard.CreateUserId     = userID;
                        stockCard.PurchaseDate     = serverTime;
                        stockCard.PrimaryKeyValue  = purchaseReturn.Id;
                        stockCard.ReferenceTableId = transactionReferenceTable.Id;
                        stockCard.SparepartId      = sparepart.Id;
                        stockCard.Description      = "Pembatalan Retur Pembelian";
                        stockCard.QtyIn            = 1;
                        stockCard.QtyInPrice       = Convert.ToDouble(itemDetail.PurchasingDetail.Price);
                        SparepartStockCard lastStockCard  = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id);
                        double             lastStock      = 0;
                        double             lastStockPrice = 0;
                        if (lastStockCard != null)
                        {
                            lastStock      = lastStockCard.QtyLast;
                            lastStockPrice = lastStockCard.QtyLastPrice;
                        }
                        stockCard.QtyFirst      = lastStock;
                        stockCard.QtyFirstPrice = lastStockPrice;
                        stockCard.QtyLast       = lastStock + stockCard.QtyIn;
                        stockCard.QtyLastPrice  = lastStockPrice + stockCard.QtyInPrice;
                        _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                        stockCard = _sparepartStokCardRepository.Add(stockCard);
                        _unitOfWork.SaveChanges();

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

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

                    _unitOfWork.SaveChanges();

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

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

                    _unitOfWork.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
        public List <GroupSparepartStockCardViewModel> RetrieveStockCards(DateTime fromDate, DateTime toDate, int sparepartId)
        {
            List <GroupSparepartStockCard> result = new List <GroupSparepartStockCard>();
            DateTime lastDay = toDate.AddDays(1).AddSeconds(-1);
            //result = _sparepartStockCardRepository.RetrieveCurrentStock(fromDate, toDate, sparepartId);

            List <SparepartStockCard> list = _sparepartStockCardRepository.GetMany(sp => sp.PurchaseDate >= fromDate && sp.PurchaseDate <= lastDay &&
                                                                                   (sparepartId > 0 ? sp.SparepartId == sparepartId : true)).ToList();

            if (list != null && list.Count > 0)
            {
                var spp = from sp in list
                          group sp by new
                {
                    sp.Sparepart,
                    sp.SparepartId,
                } into gsp
                    select new GroupSparepartStockCard
                {
                    LastPurchaseDate   = gsp.Max(g => g.PurchaseDate),
                    Sparepart          = gsp.Key.Sparepart,
                    SparepartId        = gsp.Key.SparepartId,
                    PricePerItem       = gsp.LastOrDefault().PricePerItem,
                    TotalQtyFirst      = gsp.FirstOrDefault().QtyFirst,
                    TotalQtyFirstPrice = gsp.FirstOrDefault().QtyFirstPrice,
                    TotalQtyIn         = gsp.Sum(g => g.QtyIn),
                    TotalQtyInPrice    = gsp.Sum(g => g.QtyInPrice),
                    TotalQtyOut        = gsp.Sum(g => g.QtyOut),
                    TotalQtyOutPrice   = gsp.Sum(g => g.QtyOutPrice),
                    TotalQtyLast       = gsp.LastOrDefault().QtyLast,
                    TotalQtyLastPrice  = gsp.LastOrDefault().QtyLastPrice
                };

                result = spp.ToList();
            }

            List <GroupSparepartStockCard> reportResult = result;

            //check if there are sparepartID not in range of filter, just fill with totalqtyfirst from the day close to start date filter
            if (sparepartId == 0)
            {
                var listSparepart = _sparepartRepository.GetAll();
                if (reportResult == null || reportResult.Count() == 0)
                {
                    reportResult = new List <GroupSparepartStockCard>();
                    foreach (var itemSparepart in listSparepart)
                    {
                        SparepartStockCard firstInitData = _sparepartStockCardRepository.GetMany(x => x.SparepartId == itemSparepart.Id && x.PurchaseDate < fromDate).LastOrDefault();
                        if (firstInitData != null)
                        {
                            GroupSparepartStockCard newItem = new GroupSparepartStockCard();
                            newItem.TotalQtyFirst      = firstInitData.QtyLast;
                            newItem.TotalQtyFirstPrice = firstInitData.QtyLastPrice;
                            newItem.LastPurchaseDate   = firstInitData.PurchaseDate;
                            newItem.Sparepart          = firstInitData.Sparepart;
                            newItem.SparepartId        = firstInitData.SparepartId;
                            newItem.PricePerItem       = firstInitData.PricePerItem;
                            newItem.TotalQtyIn         = 0;
                            newItem.TotalQtyInPrice    = 0;
                            newItem.TotalQtyOut        = 0;
                            newItem.TotalQtyOutPrice   = 0;
                            newItem.TotalQtyLast       = firstInitData.QtyLast;
                            newItem.TotalQtyLastPrice  = firstInitData.QtyLastPrice;
                            reportResult.Add(newItem);
                        }
                    }
                }
                else if (reportResult.Count != listSparepart.Count())
                {
                    foreach (var itemSparepart in listSparepart)
                    {
                        if (reportResult.Where(x => x.SparepartId == itemSparepart.Id).Count() == 0)
                        {
                            SparepartStockCard firstInitData = _sparepartStockCardRepository.GetMany(x => x.SparepartId == itemSparepart.Id && x.PurchaseDate < fromDate).LastOrDefault();
                            if (firstInitData != null)
                            {
                                GroupSparepartStockCard newItem = new GroupSparepartStockCard();
                                newItem.TotalQtyFirst      = firstInitData.QtyLast;
                                newItem.TotalQtyFirstPrice = firstInitData.QtyLastPrice;
                                newItem.LastPurchaseDate   = firstInitData.PurchaseDate;
                                newItem.Sparepart          = firstInitData.Sparepart;
                                newItem.SparepartId        = firstInitData.SparepartId;
                                newItem.PricePerItem       = firstInitData.PricePerItem;
                                newItem.TotalQtyIn         = 0;
                                newItem.TotalQtyInPrice    = 0;
                                newItem.TotalQtyOut        = 0;
                                newItem.TotalQtyOutPrice   = 0;
                                newItem.TotalQtyLast       = firstInitData.QtyLast;
                                newItem.TotalQtyLastPrice  = firstInitData.QtyLastPrice;
                                reportResult.Add(newItem);
                            }
                        }
                    }
                }
            }
            else
            {
                var itemSparepart = _sparepartRepository.GetById(sparepartId);
                if (reportResult == null || reportResult.Count() == 0)
                {
                    reportResult = new List <GroupSparepartStockCard>();
                    SparepartStockCard firstInitData = _sparepartStockCardRepository.GetMany(x => x.SparepartId == sparepartId && x.PurchaseDate < fromDate).LastOrDefault();
                    if (firstInitData != null)
                    {
                        GroupSparepartStockCard newItem = new GroupSparepartStockCard();
                        newItem.TotalQtyFirst      = firstInitData.QtyLast;
                        newItem.TotalQtyFirstPrice = firstInitData.QtyLastPrice;
                        newItem.LastPurchaseDate   = firstInitData.PurchaseDate;
                        newItem.Sparepart          = firstInitData.Sparepart;
                        newItem.SparepartId        = firstInitData.SparepartId;
                        newItem.PricePerItem       = firstInitData.PricePerItem;
                        newItem.TotalQtyIn         = 0;
                        newItem.TotalQtyInPrice    = 0;
                        newItem.TotalQtyOut        = 0;
                        newItem.TotalQtyOutPrice   = 0;
                        newItem.TotalQtyLast       = firstInitData.QtyLast;
                        newItem.TotalQtyLastPrice  = firstInitData.QtyLastPrice;
                        reportResult.Add(newItem);
                    }
                }
            }


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

            mappedResult = Map(reportResult, mappedResult);
            return(mappedResult.OrderBy(x => x.Sparepart.Code).ToList());
        }
        public void Approve(PurchasingViewModel purchasing, int userID)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;

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

                    List <PurchasingDetail> listPurchasingDetail = _purchasingDetailRepository
                                                                   .GetMany(c => c.PurchasingId == purchasing.Id).ToList();
                    foreach (var purchasingDetail in listPurchasingDetail)
                    {
                        Sparepart sparepartDB = _sparepartRepository.GetById(purchasingDetail.SparepartId);

                        for (int i = 1; i <= purchasingDetail.Qty; i++)
                        {
                            if (!string.IsNullOrEmpty(purchasingDetail.SerialNumber))
                            {
                                SpecialSparepartDetail wd = new SpecialSparepartDetail();
                                wd.PurchasingDetail = purchasingDetail;
                                wd.SerialNumber     = purchasingDetail.SerialNumber;
                                wd.CreateUserId     = userID;
                                wd.CreateDate       = serverTime;
                                wd.ModifyUserId     = userID;
                                wd.ModifyDate       = serverTime;
                                wd.SparepartId      = sparepartDB.Id;
                                wd.Status           = (int)DbConstant.WheelDetailStatus.Ready;

                                _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.CreateUser);
                                _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.ModifyUser);
                                _specialSparepartDetailRepository.AttachNavigation(purchasingDetail);
                                _specialSparepartDetailRepository.AttachNavigation(purchasingDetail.Sparepart);;
                                _specialSparepartDetailRepository.Add(wd);
                            }
                        }

                        purchasingDetail.Status       = (int)DbConstant.PurchasingStatus.Active;
                        purchasingDetail.QtyRemaining = purchasingDetail.Qty;
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.CreateUser);
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.ModifyUser);
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.Purchasing);
                        _purchasingDetailRepository.AttachNavigation(purchasingDetail.Sparepart);
                        _purchasingDetailRepository.Update(purchasingDetail);

                        Sparepart sparepart = _sparepartRepository.GetById(purchasingDetail.SparepartId);
                        sparepart.StockQty += purchasingDetail.Qty;

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

                        SparepartStockCard stockCard = new SparepartStockCard();
                        stockCard.CreateUserId     = userID;
                        stockCard.PurchaseDate     = serverTime;
                        stockCard.PrimaryKeyValue  = purchasing.Id;
                        stockCard.ReferenceTableId = transactionReferenceTable.Id;
                        stockCard.SparepartId      = sparepart.Id;
                        stockCard.Description      = "Purchasing";
                        stockCard.QtyIn            = purchasingDetail.Qty;
                        stockCard.QtyInPrice       = Convert.ToDouble(purchasingDetail.Price * purchasingDetail.Qty);
                        SparepartStockCard lastStockCard  = _sparepartStokCardRepository.RetrieveLastCard(sparepart.Id);
                        double             lastStock      = 0;
                        double             lastStockPrice = 0;
                        if (lastStockCard != null)
                        {
                            lastStock      = lastStockCard.QtyLast;
                            lastStockPrice = lastStockCard.QtyLastPrice;
                        }
                        stockCard.QtyFirst      = lastStock;
                        stockCard.QtyFirstPrice = lastStockPrice;
                        stockCard.QtyLast       = lastStock + stockCard.QtyIn;
                        stockCard.QtyLastPrice  = lastStockPrice + stockCard.QtyInPrice;
                        _sparepartStokCardRepository.AttachNavigation(stockCard.CreateUser);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.Sparepart);
                        _sparepartStokCardRepository.AttachNavigation(stockCard.ReferenceTable);
                        stockCard = _sparepartStokCardRepository.Add(stockCard);
                        _unitOfWork.SaveChanges();


                        SparepartStockCardDetail stockCardDtail = new SparepartStockCardDetail();
                        stockCardDtail.ParentStockCard = stockCard;
                        stockCardDtail.PricePerItem    = Convert.ToDouble(purchasingDetail.Price);
                        stockCardDtail.QtyIn           = purchasingDetail.Qty;
                        stockCardDtail.QtyInPrice      = Convert.ToDouble(purchasingDetail.Price * purchasingDetail.Qty);
                        SparepartStockCardDetail lastStockCardDetail = _sparepartStokCardDetailRepository.RetrieveLastCardDetailByPurchasingId(sparepart.Id, purchasing.Id);
                        double lastStockDetail      = 0;
                        double lastStockDetailPrice = 0;
                        if (lastStockCardDetail != null)
                        {
                            lastStockDetail      = lastStockCardDetail.QtyLast;
                            lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                        }
                        stockCardDtail.QtyFirst      = lastStockDetail;
                        stockCardDtail.QtyFirstPrice = lastStockDetailPrice;
                        stockCardDtail.QtyLast       = lastStockDetail + stockCardDtail.QtyIn;
                        stockCardDtail.QtyLastPrice  = lastStockDetailPrice + stockCardDtail.QtyInPrice;
                        stockCardDtail.PurchasingId  = purchasing.Id;
                        _sparepartStokCardDetailRepository.AttachNavigation(stockCardDtail.ParentStockCard);
                        _sparepartStokCardDetailRepository.Add(stockCardDtail);
                        _unitOfWork.SaveChanges();
                    }

                    Reference refSelected = _referenceRepository.GetById(purchasing.PaymentMethodId);
                    purchasing.Status = (int)DbConstant.PurchasingStatus.Active;
                    if (refSelected != null &&
                        (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI ||
                         refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1 ||
                         refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2 ||
                         refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_KAS)
                        )
                    {
                        purchasing.TotalHasPaid = purchasing.TotalPrice;
                    }

                    if (purchasing.TotalHasPaid != purchasing.TotalPrice)
                    {
                        purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.NotSettled;
                    }
                    else
                    {
                        purchasing.PaymentStatus = (int)DbConstant.PaymentStatus.Settled;
                    }
                    Purchasing entity = _purchasingRepository.GetById(purchasing.Id);
                    //Map(purchasing, entity);
                    entity.PaymentStatus   = purchasing.PaymentStatus;
                    entity.Status          = purchasing.Status;
                    entity.TotalHasPaid    = purchasing.TotalHasPaid;
                    entity.TotalPrice      = purchasing.TotalPrice;
                    entity.PaymentMethodId = purchasing.PaymentMethodId;
                    _purchasingRepository.AttachNavigation(entity.CreateUser);
                    _purchasingRepository.AttachNavigation(entity.ModifyUser);
                    _purchasingRepository.AttachNavigation(entity.PaymentMethod);
                    _purchasingRepository.AttachNavigation(entity.Supplier);
                    _purchasingRepository.Update(entity);
                    _unitOfWork.SaveChanges();

                    Transaction transaction = new Transaction();
                    transaction.TransactionDate  = purchasing.Date;
                    transaction.TotalPayment     = Convert.ToDouble(purchasing.TotalHasPaid);
                    transaction.TotalTransaction = Convert.ToDouble(purchasing.TotalPrice);
                    transaction.ReferenceTableId = transactionReferenceTable.Id;
                    transaction.PrimaryKeyValue  = purchasing.Id;
                    transaction.CreateDate       = serverTime;
                    transaction.CreateUserId     = userID;
                    transaction.ModifyUserId     = userID;
                    transaction.ModifyDate       = serverTime;
                    transaction.Status           = (int)DbConstant.DefaultDataStatus.Active;
                    transaction.Description      = "Pembelian sparepart";
                    transaction.PaymentMethodId  = purchasing.PaymentMethodId;

                    _transactionRepository.AttachNavigation(transaction.CreateUser);
                    _transactionRepository.AttachNavigation(transaction.ModifyUser);
                    _transactionRepository.AttachNavigation(transaction.PaymentMethod);
                    _transactionRepository.AttachNavigation(transaction.ReferenceTable);
                    Transaction transactionInserted = _transactionRepository.Add(transaction);
                    _unitOfWork.SaveChanges();

                    switch (refSelected.Code)
                    {
                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI:
                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1:
                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2:
                    {
                        // Bank Kredit --> Karena berkurang
                        TransactionDetail detailBank = new TransactionDetail();
                        detailBank.Credit = purchasing.TotalHasPaid;
                        if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_EKONOMI)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id;
                        }
                        else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA1)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id;
                        }
                        else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_BANK_BCA2)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id;
                        }
                        detailBank.ParentId = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailBank.Journal);
                        _transactionDetailRepository.AttachNavigation(detailBank.Parent);
                        _transactionDetailRepository.Add(detailBank);
                        break;
                    }

                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_KAS:
                        // Kas Kredit --> Karena berkurang
                        TransactionDetail detailKas = new TransactionDetail();
                        detailKas.Credit    = purchasing.TotalHasPaid;
                        detailKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id;
                        detailKas.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailKas.Journal);
                        _transactionDetailRepository.AttachNavigation(detailKas.Parent);
                        _transactionDetailRepository.Add(detailKas);
                        break;

                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_KAS:
                        // Kas Kredit --> Karena berkurang
                        TransactionDetail detailKasKarenaUangMuka = new TransactionDetail();
                        detailKasKarenaUangMuka.Credit    = purchasing.TotalHasPaid;
                        detailKasKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id;
                        detailKasKarenaUangMuka.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailKasKarenaUangMuka.Journal);
                        _transactionDetailRepository.AttachNavigation(detailKasKarenaUangMuka.Parent);
                        _transactionDetailRepository.Add(detailKasKarenaUangMuka);

                        // Uang Muka Debit --> Karena bertambah
                        TransactionDetail detailUangMukaBertambahKarenaKas = new TransactionDetail();
                        detailUangMukaBertambahKarenaKas.Debit     = purchasing.TotalHasPaid;
                        detailUangMukaBertambahKarenaKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id;
                        detailUangMukaBertambahKarenaKas.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaKas.Journal);
                        _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaKas.Parent);
                        _transactionDetailRepository.Add(detailUangMukaBertambahKarenaKas);

                        // Uang Muka Kredit --> Karena berkurang
                        TransactionDetail detailUangMukaBerkurangKarenaKas = new TransactionDetail();
                        detailUangMukaBerkurangKarenaKas.Credit    = purchasing.TotalHasPaid;
                        detailUangMukaBerkurangKarenaKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id;
                        detailUangMukaBerkurangKarenaKas.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaKas.Journal);
                        _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaKas.Parent);
                        _transactionDetailRepository.Add(detailUangMukaBerkurangKarenaKas);
                        break;

                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI:
                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1:
                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2:
                    {
                        // Bank Kredit --> Karena berkurang
                        TransactionDetail detailBankKarenaUangMuka = new TransactionDetail();
                        detailBankKarenaUangMuka.Credit = purchasing.TotalHasPaid;
                        if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI)
                        {
                            detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id;
                        }
                        else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1)
                        {
                            detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id;
                        }
                        else if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2)
                        {
                            detailBankKarenaUangMuka.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id;
                        }
                        detailBankKarenaUangMuka.ParentId = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailBankKarenaUangMuka.Journal);
                        _transactionDetailRepository.AttachNavigation(detailBankKarenaUangMuka.Parent);
                        _transactionDetailRepository.Add(detailBankKarenaUangMuka);

                        // Uang Muka Debit --> Karena bertambah
                        TransactionDetail detailUangMukaBertambahKarenaBank = new TransactionDetail();
                        detailUangMukaBertambahKarenaBank.Debit     = purchasing.TotalHasPaid;
                        detailUangMukaBertambahKarenaBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id;
                        detailUangMukaBertambahKarenaBank.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaBank.Journal);
                        _transactionDetailRepository.AttachNavigation(detailUangMukaBertambahKarenaBank.Parent);
                        _transactionDetailRepository.Add(detailUangMukaBertambahKarenaBank);

                        // Uang Muka Kredit --> Karena berkurang
                        TransactionDetail detailUangMukaBerkurangKarenaBank = new TransactionDetail();
                        detailUangMukaBerkurangKarenaBank.Credit    = purchasing.TotalHasPaid;
                        detailUangMukaBerkurangKarenaBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.05.01.01").FirstOrDefault().Id;
                        detailUangMukaBerkurangKarenaBank.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaBank.Journal);
                        _transactionDetailRepository.AttachNavigation(detailUangMukaBerkurangKarenaBank.Parent);
                        _transactionDetailRepository.Add(detailUangMukaBerkurangKarenaBank);
                        break;
                    }

                    case DbConstant.REF_PURCHASE_PAYMENTMETHOD_UTANG:
                        TransactionDetail utang = new TransactionDetail();
                        utang.Credit    = purchasing.TotalPrice - purchasing.TotalHasPaid;
                        utang.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id;
                        utang.ParentId  = transactionInserted.Id;

                        _transactionDetailRepository.AttachNavigation(utang.Journal);
                        _transactionDetailRepository.AttachNavigation(utang.Parent);
                        _transactionDetailRepository.Add(utang);
                        break;
                    }

                    if (refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_EKONOMI ||
                        refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA1 ||
                        refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_BANK_BCA2 ||
                        refSelected.Code == DbConstant.REF_PURCHASE_PAYMENTMETHOD_UANGMUKA_KAS)
                    {
                        if (purchasing.TotalPrice > purchasing.TotalHasPaid)
                        {
                            // Utang Kredit --> Karena bertambah
                            TransactionDetail utang = new TransactionDetail();
                            utang.Credit    = purchasing.TotalPrice - purchasing.TotalHasPaid;
                            utang.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id;
                            utang.ParentId  = transactionInserted.Id;

                            _transactionDetailRepository.AttachNavigation(utang.Journal);
                            _transactionDetailRepository.AttachNavigation(utang.Parent);
                            _transactionDetailRepository.Add(utang);
                        }
                    }

                    // Sparepart Debit --> Karena bertambah
                    TransactionDetail detailSparepart = new TransactionDetail();
                    detailSparepart.Debit     = purchasing.TotalPrice;
                    detailSparepart.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.04.01").FirstOrDefault().Id;
                    detailSparepart.ParentId  = transactionInserted.Id;

                    _transactionDetailRepository.AttachNavigation(detailSparepart.Journal);
                    _transactionDetailRepository.AttachNavigation(detailSparepart.Parent);
                    _transactionDetailRepository.Add(detailSparepart);

                    _unitOfWork.SaveChanges();

                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
        public void 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;
                }
            }
        }
Ejemplo n.º 9
0
        public List <GroupSparepartStockCardViewModel> RetrieveStockCards(DateTime fromDate, DateTime toDate, int sparepartId)
        {
            List <GroupSparepartStockCard> result = new List <GroupSparepartStockCard>();
            DateTime lastDay = toDate.AddDays(1).AddSeconds(-1);
            //result = _sparepartStockCardDetailRepository.RetrieveFIFOCurrentSparepart(fromDate, toDate, sparepartId);

            List <SparepartStockCardDetail> list = _sparepartStockCardDetailRepository.GetMany(sp => sp.ParentStockCard.PurchaseDate >= fromDate && sp.ParentStockCard.PurchaseDate <= lastDay &&
                                                                                               (sparepartId > 0 ? sp.ParentStockCard.SparepartId == sparepartId : true)).ToList();

            if (list != null)
            {
                var spp = from sp in list
                          group sp by new
                {
                    sp.ParentStockCard.Sparepart,
                    sp.ParentStockCard.SparepartId,
                    sp.Purchasing,
                    sp.PurchasingId,
                    sp.SparepartManualTransaction,
                    sp.SparepartManualTransactionId
                } into gsp
                    select new GroupSparepartStockCard
                {
                    LastPurchaseDate             = gsp.FirstOrDefault().Purchasing != null?gsp.FirstOrDefault().Purchasing.Date : gsp.FirstOrDefault().SparepartManualTransaction.CreateDate,
                    Sparepart                    = gsp.Key.Sparepart,
                    SparepartId                  = gsp.Key.SparepartId,
                    Purchasing                   = gsp.Key.Purchasing,
                    PurchasingId                 = gsp.Key.PurchasingId,
                    SparepartManualTransaction   = gsp.Key.SparepartManualTransaction,
                    SparepartManualTransactionId = gsp.Key.SparepartManualTransactionId,
                    PricePerItem                 = gsp.LastOrDefault().PricePerItem,
                    TotalQtyFirst                = gsp.FirstOrDefault().QtyFirst,
                    TotalQtyFirstPrice           = gsp.FirstOrDefault().QtyFirstPrice,
                    TotalQtyIn                   = gsp.Sum(g => g.QtyIn),
                    TotalQtyInPrice              = gsp.Sum(g => g.QtyInPrice),
                    TotalQtyOut                  = gsp.Sum(g => g.QtyOut),
                    TotalQtyOutPrice             = gsp.Sum(g => g.QtyOutPrice),
                    TotalQtyLast                 = gsp.LastOrDefault().QtyLast,
                    TotalQtyLastPrice            = gsp.LastOrDefault().QtyLastPrice
                };

                result = spp.ToList();
            }

            List <GroupSparepartStockCard> reportResult = result;

            //check if there are sparepartID not in range of filter, just fill with totalqtyfirst from the day close to start date filter
            if (sparepartId != 0)
            {
                var itemSparepart       = _sparepartRepository.GetById(sparepartId);
                var listPurchasing      = _purchasingDetailRepository.GetMany(x => x.SparepartId == sparepartId).Select(x => x.Purchasing);
                var listSparepartManual = _sparepartManualTransactionRepository.GetMany(x => x.SparepartId == sparepartId);
                if (reportResult == null || reportResult.Count() == 0)
                {
                    reportResult = new List <GroupSparepartStockCard>();
                }

                foreach (var itemPurchasing in listPurchasing)
                {
                    if (reportResult.Where(x => x.PurchasingId == itemPurchasing.Id).Count() == 0)
                    {
                        SparepartStockCardDetail firstInitData = _sparepartStockCardDetailRepository.GetMany(x => x.PurchasingId == itemPurchasing.Id && x.ParentStockCard.PurchaseDate < fromDate).LastOrDefault();
                        if (firstInitData != null)
                        {
                            GroupSparepartStockCard newItem = new GroupSparepartStockCard();
                            newItem.TotalQtyFirst      = firstInitData.QtyLast;
                            newItem.TotalQtyFirstPrice = firstInitData.QtyLastPrice;
                            newItem.LastPurchaseDate   = firstInitData.Purchasing.CreateDate;
                            newItem.Sparepart          = firstInitData.ParentStockCard.Sparepart;
                            newItem.SparepartId        = firstInitData.ParentStockCard.SparepartId;
                            newItem.Purchasing         = firstInitData.Purchasing;
                            newItem.PurchasingId       = firstInitData.PurchasingId;
                            newItem.PricePerItem       = firstInitData.PricePerItem;
                            newItem.TotalQtyIn         = 0;
                            newItem.TotalQtyInPrice    = 0;
                            newItem.TotalQtyOut        = 0;
                            newItem.TotalQtyOutPrice   = 0;
                            newItem.TotalQtyLast       = firstInitData.QtyLast;
                            newItem.TotalQtyLastPrice  = firstInitData.QtyLastPrice;
                            reportResult.Add(newItem);
                        }
                    }
                }
                foreach (var itemSpManual in listSparepartManual)
                {
                    if (reportResult.Where(x => x.SparepartManualTransactionId == itemSpManual.Id).Count() == 0)
                    {
                        SparepartStockCardDetail firstInitData = _sparepartStockCardDetailRepository.GetMany(x => x.SparepartManualTransactionId == itemSpManual.Id && x.ParentStockCard.PurchaseDate < fromDate).LastOrDefault();
                        if (firstInitData != null)
                        {
                            GroupSparepartStockCard newItem = new GroupSparepartStockCard();
                            newItem.TotalQtyFirst                = firstInitData.QtyLast;
                            newItem.TotalQtyFirstPrice           = firstInitData.QtyLastPrice;
                            newItem.LastPurchaseDate             = firstInitData.SparepartManualTransaction.CreateDate;
                            newItem.Sparepart                    = firstInitData.ParentStockCard.Sparepart;
                            newItem.SparepartId                  = firstInitData.ParentStockCard.SparepartId;
                            newItem.SparepartManualTransaction   = firstInitData.SparepartManualTransaction;
                            newItem.SparepartManualTransactionId = firstInitData.SparepartManualTransactionId;
                            newItem.PricePerItem                 = firstInitData.PricePerItem;
                            newItem.TotalQtyIn                   = 0;
                            newItem.TotalQtyInPrice              = 0;
                            newItem.TotalQtyOut                  = 0;
                            newItem.TotalQtyOutPrice             = 0;
                            newItem.TotalQtyLast                 = firstInitData.QtyLast;
                            newItem.TotalQtyLastPrice            = firstInitData.QtyLastPrice;
                            reportResult.Add(newItem);
                        }
                    }
                }
            }
            List <GroupSparepartStockCardViewModel> mappedResult = new List <GroupSparepartStockCardViewModel>();

            mappedResult = Map(reportResult, mappedResult);
            return(mappedResult.OrderBy(x => x.Sparepart.Code).ToList());
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
0
        public void InsertSparepartManualTransaction(SparepartManualTransactionViewModel sparepartManualTransaction, decimal totalPrice, int userId)
        {
            using (var trans = _unitOfWork.BeginTransaction())
            {
                try
                {
                    DateTime serverTime = DateTime.Now;
                    sparepartManualTransaction.CreateDate      = serverTime;
                    sparepartManualTransaction.CreateUserId    = userId;
                    sparepartManualTransaction.ModifyDate      = serverTime;
                    sparepartManualTransaction.ModifyUserId    = userId;
                    sparepartManualTransaction.TransactionDate = serverTime;
                    sparepartManualTransaction.QtyRemaining    = sparepartManualTransaction.Qty;
                    Reference updateType       = _referenceRepository.GetById(sparepartManualTransaction.UpdateTypeId);
                    Sparepart sparepartUpdated = _sparepartRepository.GetById(sparepartManualTransaction.SparepartId);
                    if (updateType != null && sparepartUpdated != null)
                    {
                        SparepartManualTransaction entity = new SparepartManualTransaction();
                        Map(sparepartManualTransaction, entity);

                        _sparepartManualTransactionRepository.AttachNavigation(entity.CreateUser);
                        _sparepartManualTransactionRepository.AttachNavigation(entity.ModifyUser);
                        _sparepartManualTransactionRepository.AttachNavigation(entity.Sparepart);
                        _sparepartManualTransactionRepository.AttachNavigation(entity.UpdateType);
                        SparepartManualTransaction manualTransaction = _sparepartManualTransactionRepository.Add(entity);
                        _unitOfWork.SaveChanges();

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

                        if (updateType.Code == DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_PLUS)
                        {
                            stockCard.Description = "Penambahan stok awal";

                            sparepartUpdated.StockQty += sparepartManualTransaction.Qty;
                            stockCard.QtyIn            = sparepartManualTransaction.Qty;
                            stockCard.QtyInPrice       = (sparepartManualTransaction.Qty * sparepartManualTransaction.Price).AsDouble();

                            for (int i = 1; i <= sparepartManualTransaction.Qty; i++)
                            {
                                if (!string.IsNullOrEmpty(sparepartManualTransaction.SerialNumber))
                                {
                                    SpecialSparepartDetail wDetail = new SpecialSparepartDetail();
                                    wDetail.Sparepart = manualTransaction.Sparepart;
                                    wDetail.SparepartManualTransaction = manualTransaction;
                                    wDetail.SerialNumber = sparepartManualTransaction.SerialNumber;
                                    wDetail.CreateDate   = serverTime;
                                    wDetail.CreateUserId = userId;
                                    wDetail.ModifyUserId = userId;
                                    wDetail.ModifyDate   = serverTime;
                                    wDetail.Status       = (int)DbConstant.WheelDetailStatus.Ready;

                                    _specialSparepartDetailRepository.AttachNavigation(manualTransaction.CreateUser);
                                    _specialSparepartDetailRepository.AttachNavigation(manualTransaction.ModifyUser);
                                    _specialSparepartDetailRepository.AttachNavigation(manualTransaction.Sparepart);
                                    _specialSparepartDetailRepository.AttachNavigation(manualTransaction);
                                    _specialSparepartDetailRepository.Add(wDetail);

                                    _unitOfWork.SaveChanges();
                                }
                            }
                        }
                        else if (updateType.Code == DbConstant.REF_SPAREPART_TRANSACTION_MANUAL_TYPE_MINUS)
                        {
                            stockCard.Description      = "Pengurangan stok awal";
                            sparepartUpdated.StockQty -= sparepartManualTransaction.Qty;
                            stockCard.QtyOut           = sparepartManualTransaction.Qty;
                            stockCard.QtyOutPrice      = (sparepartManualTransaction.Qty * sparepartManualTransaction.Price).AsDouble();
                        }

                        SparepartStockCard lastStockCard  = _sparepartStockCardRepository.RetrieveLastCard(manualTransaction.SparepartId);
                        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.QtyOut);
                        stockCard.QtyLastPrice  = lastStockPrice + (stockCard.QtyInPrice - stockCard.QtyOutPrice);

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

                        SparepartStockCardDetail stockCardDetail = new SparepartStockCardDetail();
                        stockCardDetail.ParentStockCard = stockCard;
                        stockCardDetail.PricePerItem    = Convert.ToDouble(manualTransaction.Price);
                        stockCardDetail.QtyIn           = manualTransaction.Qty;
                        stockCardDetail.QtyInPrice      = Convert.ToDouble(manualTransaction.Price * manualTransaction.Qty);
                        stockCardDetail.QtyOut          = 0;
                        stockCardDetail.QtyOutPrice     = 0;
                        SparepartStockCardDetail lastStockCardDetail = _sparepartStockCardDetailRepository.RetrieveLastCardDetailByPurchasingId(manualTransaction.SparepartId, manualTransaction.Id);
                        double lastStockDetail      = 0;
                        double lastStockDetailPrice = 0;
                        if (lastStockCardDetail != null)
                        {
                            lastStockDetail      = lastStockCardDetail.QtyLast;
                            lastStockDetailPrice = lastStockCardDetail.QtyLastPrice;
                        }
                        stockCardDetail.QtyFirst      = lastStockDetail;
                        stockCardDetail.QtyFirstPrice = lastStockDetailPrice;
                        stockCardDetail.QtyLast       = manualTransaction.Qty;
                        stockCardDetail.QtyLastPrice  = Convert.ToDouble(manualTransaction.Price * manualTransaction.Qty);
                        stockCardDetail.SparepartManualTransactionId = manualTransaction.Id;
                        _sparepartStockCardDetailRepository.AttachNavigation(stockCardDetail.ParentStockCard);
                        _sparepartStockCardDetailRepository.Add(stockCardDetail);

                        _sparepartRepository.AttachNavigation(sparepartUpdated.CreateUser);
                        _sparepartRepository.AttachNavigation(sparepartUpdated.ModifyUser);
                        _sparepartRepository.AttachNavigation(sparepartUpdated.CategoryReference);
                        _sparepartRepository.AttachNavigation(sparepartUpdated.UnitReference);
                        _sparepartRepository.Update(sparepartUpdated);
                        _unitOfWork.SaveChanges();
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw;
                }
            }
        }
Ejemplo n.º 12
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;
                }
            }
        }
Ejemplo n.º 13
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();
        }
        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;
                }
            }
        }