Exemple #1
0
        public int GetCurrentStockByProductAndBatchCode(long?productCode, string batchNo)
        {
            int currentStock = 0;
            int totalStock   = 0;

            if (string.IsNullOrEmpty(batchNo))
            {
                List <Purchase>        _directpurchases      = _purchaseRepository.GetDirectPurchases().ToList();
                int?[]                 _directpurchasesIDs   = _directpurchases.Select(x => x.Id).ToArray();
                int?[]                 _purchaseFromSupplier = _purchaseRepository.GetPurchases().Where(x => x.Status != 2).Select(x => x.Id).ToArray(); // temprary code(int)CommonEnum.PurchaseStatus.WaitingForApproval
                List <OpeningStock>    _openingStock         = _openingStockRepository.GetOpeningStockByProductCode().Where(x => x.ProductCode == productCode).ToList();
                List <Wastage>         _wastage         = _productRepository.GetWastage().Where(x => x.ItemCode == productCode).ToList();
                List <Stock>           _stock           = _purchaseRepository.GetStocks().Where(x => (x.ProductCode == productCode && x.PurchaseId != null && _directpurchasesIDs.Contains(x.PurchaseId)) || ((x.ProductCode == productCode && _purchaseFromSupplier.Contains(x.PurchaseOrderId)))).ToList();
                List <StockAdjustment> _stockAdjustment = _stockAdjustmentRepository.GetStockAdjustments().Where(x => x.ProductCode == productCode).ToList();
                currentStock = CommonFunctions.CalcCurrentStock(_stock == null ? 0 : _stock.Sum(x => x.Quantity), _openingStock == null ? 0 : _openingStock.Sum(x => x.Quantity), _wastage == null ? 0 : _wastage.Sum(x => x.Quantity), _stockAdjustment == null ? 0 : _stockAdjustment.Sum(x => x.Quantity));
            }
            else
            {
                List <OpeningStock>    _openingStock    = _openingStockRepository.GetOpeningStockByProductCode().Where(x => x.BatchNo == batchNo && x.ProductCode == productCode).ToList();
                List <Stock>           _stock           = _purchaseRepository.GetStocks().Where(x => x.BatchNo == batchNo && x.ProductCode == productCode).ToList();
                List <StockAdjustment> _stockAdjustment = _stockAdjustmentRepository.GetStockAdjustments().Where(x => x.BatchNo == batchNo && x.ProductCode == productCode).ToList();
                //Batch not included
                List <Wastage> _wastage = _productRepository.GetWastage().Where(x => x.ItemCode == productCode).ToList();
                currentStock = CommonFunctions.CalcCurrentStock(_stock == null ? 0 : _stock.Sum(x => x.Quantity), _openingStock == null ? 0 : _openingStock.Sum(x => x.Quantity), _wastage == null ? 0 : _stock.Sum(x => x.Quantity), _stockAdjustment == null ? 0 : _stockAdjustment.Sum(x => x.Quantity));
            }
            List <PurchaseReturn> stocksRetrun = _purchaseRepository.GetPurchaseReturns().Where(x => (x.ProductCode == productCode)).ToList();

            totalStock = currentStock - (stocksRetrun == null ? 0 : stocksRetrun.Sum(x => x.Quantity));
            return(totalStock);
        }