void goodsSaleReturnView_LoadGoodsEvent(object sender, GoodsSaleEventArgs e)
 {
     PurchaseOrderDetail detail = e.SelectedPurchaseOrderDetail;
     /*ProductMaster prodMaster = ProductMasterLogic.FindById(e.SelectedPurchaseOrderDetail.ProductMaster.ProductMasterId);
     if (prodMaster == null)
     {
         return;
     }*/
     //Product product = ProductLogic.FindById(e.SelectedPurchaseOrderDetail.Product.ProductId);
     ObjectCriteria objectCriteria = new ObjectCriteria();
     objectCriteria.AddEqCriteria("DepartmentStockPK.ProductId", e.SelectedPurchaseOrderDetail.Product.ProductId);
     objectCriteria.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId);
     objectCriteria.AddGreaterCriteria("GoodQuantity", (long)0);
     IList result = DepartmentStockLogic.FindAll(objectCriteria);
     if (result == null)
     {
         throw new BusinessException("Mặt hàng này không tồn tại trong kho !");
     }
     DepartmentStock stock = (DepartmentStock)result[0];
     Product product = stock.Product;
     detail.Product = product;
     detail.ProductMaster = product.ProductMaster;
     DepartmentPrice price = DepartmentPriceLogic.FindById(new DepartmentPricePK { DepartmentId = 0, ProductMasterId = detail.ProductMaster.ProductMasterId });
     if (price == null)
     {
         return;
     }
     detail.Price = price.Price;
     e.SelectedPurchaseOrderDetail = detail;
 }
        private void productMasterSearchDepartmentView_SearchProductEvent(object sender, ProductMasterSearchDepartmentEventArgs e)
        {
            ProductMaster searchProductMaster = e.SelectedProductMaster;
            ObjectCriteria searchByProductMasterCriteria = new ObjectCriteria();
            searchByProductMasterCriteria.AddEqCriteria("Department", CurrentDepartment.Get());
            searchByProductMasterCriteria.AddEqCriteria("ProductMaster", searchProductMaster);
            searchByProductMasterCriteria.AddOrder("CreateDate", false);
            searchByProductMasterCriteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            IList productsStockIn = DepartmentStockInDetailLogic.FindAllProductMaster(searchProductMaster);
            IList productsInStock = new ArrayList();
            IList productIds = new ArrayList();
            foreach (DepartmentStockInDetail detail in productsStockIn)
            {
                productIds.Add(detail.Product.ProductId);
            }
            ObjectCriteria stockCrit = new ObjectCriteria();
            stockCrit.AddSearchInCriteria("DepartmentStockPK.ProductId", productIds);
            if(e.AvailableInStock)
            {
                stockCrit.AddGreaterCriteria("GoodQuantity", (long) 0);
            }
            IList stockList = DepartmentStockLogic.FindAll(stockCrit);
            if (stockList != null && stockList.Count > 0)
            {
                foreach (DepartmentStock departmentStock in stockList)
                {
                    departmentStock.Product = ProductLogic.FindById(departmentStock.DepartmentStockPK.ProductId);
                    productsInStock.Add(departmentStock);
                }

            }
            e.ProductsInDepartment = productsInStock;
        }
        void goodsSaleView_LoadGoodsEvent(object sender, GoodsSaleEventArgs e)
        {
            PurchaseOrderDetail detail = e.SelectedPurchaseOrderDetail;
            bool NotAvailableInStock = e.NotAvailableInStock;
            /*ProductMaster prodMaster = ProductMasterLogic.FindById(e.SelectedPurchaseOrderDetail.ProductMaster.ProductMasterId);
            if (prodMaster == null)
            {
                return;
            }*/
            //Product product = ProductLogic.FindProduct(e.SelectedPurchaseOrderDetail.Product.ProductId,CurrentDepartment.Get().DepartmentId);
            // TEMP FIXING
            //NotAvailableInStock = false;

            ObjectCriteria objectCriteria = new ObjectCriteria();
            objectCriteria.AddEqCriteria("DepartmentStockPK.ProductId", e.SelectedPurchaseOrderDetail.Product.ProductId);
            objectCriteria.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId);
            if(!NotAvailableInStock)
            {
                objectCriteria.AddGreaterCriteria("GoodQuantity", (long) 0);
            }
            IList result = DepartmentStockLogic.FindAll(objectCriteria) ;

            if(!NotAvailableInStock)
            {
                if ((result == null || result.Count == 0))
                {
                    throw new BusinessException("Mặt hàng này không tồn tại hoặc đã hết !");
                }
            }

            if ((result == null || result.Count == 0))
            {
                throw new BusinessException("Mặt hàng này không tồn tại !");
            }

            Product product = null;
            bool NeedSwitchPrdId = false;
            if (result.Count == 1)
            {
                DepartmentStock stock = (DepartmentStock) result[0];
                product = stock.Product;
                if(stock.GoodQuantity <= 0)
                {
                    NeedSwitchPrdId = true;
                }
            }
            // START ---- don ma

            /*if (product != null && NeedSwitchPrdId)
            {
                ObjectCriteria prdCrit = new ObjectCriteria();
                prdCrit.AddEqCriteria("ProductMaster.ProductName", product.ProductMaster.ProductName);
                IList productIdsList = ProductLogic.FindAll(prdCrit);
                if (productIdsList != null && productIdsList.Count > 0)
                {
                    IList productIds = new ArrayList();
                    foreach (Product prd in productIdsList)
                    {
                        productIds.Add(prd.ProductId);
                    }
                    prdCrit = new ObjectCriteria();
                    prdCrit.AddSearchInCriteria("DepartmentStockPK.ProductId", productIds);
                    prdCrit.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId);
                    IList allStocks = DepartmentStockLogic.FindAll(prdCrit);
                    if(allStocks != null && allStocks.Count > 0)
                    {
                        SortByGoodQuantity(allStocks);

                    }
                }
            }*/
            // END ---- don ma
            /*else
            {
                if(CommonConstants.UNDEFINED_BARCODE.Equals(e.SelectedPurchaseOrderDetail.Product.ProductId))
                {
                    Product undefProduct = ProductLogic.FindById(e.SelectedPurchaseOrderDetail.Product.ProductId);
                    product = undefProduct;
                }
            }*/

            detail.Product = product;
            detail.ProductMaster = product.ProductMaster;
            DepartmentPrice price = DepartmentPriceLogic.FindById(new DepartmentPricePK { DepartmentId = 0,ProductMasterId = detail.ProductMaster.ProductMasterId} );
            if (price == null || price.Price == 0 )
            {
                throw new BusinessException("Giá của mặt hàng " + product.ProductMaster.ProductName + " không có hoặc là 0. Xin kiểm tra lại.");
            }
            detail.Price = price.Price;
            e.SelectedPurchaseOrderDetail = detail;
        }