public List <PurchaseReturnViewModel> SearchPurchaseReturnList(DateTime?dateFrom, DateTime?dateTo, int supplierID)
        {
            List <PurchaseReturn> result = null;

            if (dateFrom.HasValue && dateTo.HasValue)
            {
                dateFrom = dateFrom.Value.Date;
                dateTo   = dateTo.Value.Date.AddDays(1).AddSeconds(-1);
                result   = _purchaseReturnRepository.GetMany(c => c.Date >= dateFrom && c.Date <= dateTo && c.Status == (int)DbConstant.DefaultDataStatus.Active).OrderBy(c => c.Date).ToList();
            }
            else
            {
                result = _purchaseReturnRepository.GetMany(c => c.Status == (int)DbConstant.DefaultDataStatus.Active).OrderBy(c => c.Date).ToList();
            }

            if (supplierID != 0)
            {
                result = result.Where(p => p.Purchasing.SupplierId == supplierID).ToList();
            }

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

            Map(result, mappedResult);

            foreach (var itemMapped in mappedResult)
            {
                itemMapped.TotalPriceReturn = CalculateTotalReturn(itemMapped.Id);
            }

            return(mappedResult);
        }
Esempio n. 2
0
        public List <RecapPurchasingItemViewModel> RetrieveRecap(DateTime dateFrom, DateTime dateTo,
                                                                 int supplierId)
        {
            List <Purchasing> purchaseResult = _purchasingRepository.GetMany(i =>
                                                                             DbFunctions.TruncateTime(i.CreateDate) >= DbFunctions.TruncateTime(dateFrom) &&
                                                                             DbFunctions.TruncateTime(i.CreateDate) <= DbFunctions.TruncateTime(dateTo) &&
                                                                             i.Status != (int)DbConstant.DefaultDataStatus.Deleted &&
                                                                             i.PaymentStatus != (int)DbConstant.PaymentStatus.Settled &&
                                                                             i.SupplierId == supplierId).ToList();

            purchaseResult = purchaseResult.OrderBy(i => i.CreateDate).ToList();
            List <PurchasingViewModel> mappedPurchaseResult = new List <PurchasingViewModel>();

            Map(purchaseResult, mappedPurchaseResult);

            List <PurchaseReturn> purchaseReturnResult = _purchaseReturnRepository.GetMany(i =>
                                                                                           DbFunctions.TruncateTime(i.CreateDate) >= DbFunctions.TruncateTime(dateFrom) &&
                                                                                           DbFunctions.TruncateTime(i.CreateDate) <= DbFunctions.TruncateTime(dateTo) &&
                                                                                           i.Status != (int)DbConstant.DefaultDataStatus.Deleted &&
                                                                                           i.Purchasing.PaymentStatus != (int)DbConstant.PaymentStatus.Settled &&
                                                                                           i.Purchasing.SupplierId == supplierId).ToList();

            purchaseReturnResult = purchaseReturnResult.OrderBy(i => i.CreateDate).ToList();
            List <PurchaseReturnViewModel> mappedPurchaseReturnResult = new List <PurchaseReturnViewModel>();

            Map(purchaseReturnResult, mappedPurchaseReturnResult);

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

            foreach (var itemPurchase in mappedPurchaseResult)
            {
                mappedResult.Add(new RecapPurchasingItemViewModel
                {
                    Code       = itemPurchase.Code,
                    Date       = itemPurchase.Date,
                    IsReturn   = false,
                    Supplier   = itemPurchase.Supplier,
                    TotalPrice = itemPurchase.TotalPrice
                });
            }

            foreach (var itemPurchaseReturn in mappedPurchaseReturnResult)
            {
                mappedResult.Add(new RecapPurchasingItemViewModel
                {
                    Code       = itemPurchaseReturn.Code,
                    Date       = itemPurchaseReturn.Date,
                    IsReturn   = true,
                    Supplier   = itemPurchaseReturn.Purchasing.Supplier,
                    TotalPrice = -1 * itemPurchaseReturn.TotalPriceReturn
                });
            }
            return(mappedResult);
        }
Esempio n. 3
0
        public List <PurchaseReturnViewModel> SearchPurchaseReturnList(DateTime?dateFrom, DateTime?dateTo, int purchasingID)
        {
            List <PurchaseReturn> result = null;

            if (dateFrom.HasValue && dateTo.HasValue)
            {
                dateFrom = dateFrom.Value.Date;
                dateTo   = dateTo.Value.Date.AddDays(1).AddSeconds(-1);
                result   = _purchaseReturnRepository.GetMany(c => c.Date >= dateFrom && c.CreateDate <= dateTo && c.Status == (int)DbConstant.DefaultDataStatus.Active).OrderByDescending(c => c.Date).ToList();
            }
            else
            {
                result = _purchaseReturnRepository.GetMany(c => c.Status == (int)DbConstant.DefaultDataStatus.Active).OrderByDescending(c => c.Date).ToList();
            }

            if (purchasingID > 0)
            {
                result = result.Where(x => x.PurchasingId == purchasingID).OrderByDescending(c => c.Date).ToList();
            }

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

            return(Map(result, mappedResult));
        }
Esempio n. 4
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 PurchaseReturn GetLastReturnBillOfSupplier(string inwardno)
        {
            var data = _PurchaseReturnRepository.GetMany(m => m.InwardNo == inwardno).LastOrDefault();

            return(data);
        }