void DepartmentStockAdhocProcessingViewLoadAdhocStocksEvent(object sender, DepartmentStockFixingEventArgs e)
        {
            ObjectCriteria criteria = new ObjectCriteria();
            criteria.AddLesserCriteria("GoodQuantity", (long)0);
            criteria.AddEqCriteria("DelFlg", (long)0);
            criteria.AddOrder("DepartmentStockPK.DepartmentId", true);
            IList list = DepartmentStockLogic.FindAll(criteria);
            if (list != null && list.Count > 0)
            {

                e.DeptStockAdhocList = list;
            }
        }
        private void DepartmentStockCheckingForm_Load(object sender, EventArgs e)
        {
            stockList = new DepartmentStockCollection(bdsStockDefect);
            bdsStockDefect.DataSource = stockList;
            bdsStockDefect.EndEdit();
            bdsStockDefect.ResetBindings(true);
            dgvStock.Refresh();
            dgvStock.Invalidate();

            stockList.Clear();
            DepartmentStockFixingEventArgs eventArgs = new DepartmentStockFixingEventArgs();

            EventUtility.fireEvent(LoadAdhocStocksEvent,this,eventArgs);
            if(eventArgs.DeptStockAdhocList!= null && eventArgs.DeptStockAdhocList.Count > 0)
            {
                foreach (DepartmentStock stockTempView in eventArgs.DeptStockAdhocList)
                {
                    stockList.Add(stockTempView);
                }
                bdsStockDefect.EndEdit();
                dgvStock.Refresh();
                dgvStock.Invalidate();
            }
        }
        void DepartmentStockAdhocProcessingViewProcessAdhocStocksEvent(object sender, DepartmentStockFixingEventArgs e)
        {
            try
            {

                long departmentId = -1;
                StockOut stockOut = null;
                StockIn stockIn = new StockIn();

                stockIn.CreateDate = DateTime.Now;
                stockIn.UpdateDate = DateTime.Now;
                stockIn.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                stockIn.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                stockIn.StockInType = 2; // fixing stockin for negative quantity.
                stockIn.StockInDate = DateTime.Now;
                stockIn.Description = " Import goods for fixing negative quantity";
                stockIn.StockInId = StockInLogic.FindMaxId();
                stockIn.StockInDetails = new ArrayList();

                long stockOutDetailMaxId = StockOutDetailLogic.FindMaxId() + 1;
                long stockOutMaxId = StockOutLogic.FindMaxId()+1;

                for (int i=0; i< e.DeptStockProcessedList.Count;i++)
                {
                    DepartmentStock stockTemp = (DepartmentStock)e.DeptStockProcessedList[i];
                    if(stockTemp.DepartmentStockPK.DepartmentId != departmentId)
                    {
                        departmentId = stockTemp.DepartmentStockPK.DepartmentId;

                        if (stockOut != null && stockOut.StockOutDetails.Count > 0 )
                        {
                            StockOutLogic.AddFixedStockOut(stockOut);
                        }
                        stockOut = new StockOut();
                        stockOut.CreateDate = DateTime.Now;
                        stockOut.UpdateDate = DateTime.Now;
                        stockOut.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockOut.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockOut.StockOutDate = DateTime.Now;
                        stockOut.StockOutDetails = new ArrayList();
                        stockOut.DepartmentId = departmentId;
                        stockOut.DefectStatus = new StockDefectStatus{ DefectStatusId = 0};

                        stockOut.StockoutId =  stockOutMaxId++;
                    }

                    /*stockTemp.Fixed = 1;*/
                    /*stockTemp.UpdateDate = DateTime.Now;
                    stockTemp.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    DepartmentStockLogic.Update(stockTemp);*/
                    long realQty = stockTemp.GoodQuantity + stockTemp.ErrorQuantity + stockTemp.DamageQuantity +
                                   stockTemp.LostQuantity + stockTemp.UnconfirmQuantity;
                    if(stockTemp.GoodQuantity < 0)
                    {
                        long needStockMoreQty = 0 - stockTemp.GoodQuantity;
                        StockOutDetail stockOutDetail = new StockOutDetail();
                        stockOutDetail.CreateDate = DateTime.Now;
                        stockOutDetail.UpdateDate = DateTime.Now;
                        stockOutDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockOutDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockOutDetail.Quantity = needStockMoreQty;
                        stockOutDetail.Product = stockTemp.Product;
                        stockOutDetail.StockOutId = stockOut.StockoutId;
                        stockOutDetail.StockOut = stockOut;
                        stockOutDetail.DefectStatus = new StockDefectStatus{DefectStatusId = 0};
                        stockOutDetail.Description = "Export goods for compensate negative quantity";
                        stockOutDetail.ProductMaster = stockTemp.Product.ProductMaster;
                        stockOutDetail.StockOutDetailId = stockOutDetailMaxId++;
                        stockOut.StockOutDetails.Add(stockOutDetail);

                        StockInDetail stockInDetail = new StockInDetail();
                        stockInDetail.CreateDate = DateTime.Now;
                        stockInDetail.UpdateDate = DateTime.Now;
                        stockInDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockInDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        stockInDetail.StockInType = 0;
                        stockInDetail.StockIn = stockIn;
                        stockInDetail.StockInType = 2; // fixing stock in for negative quantity
                        stockInDetail.Quantity = needStockMoreQty;
                        stockInDetail.Product = stockTemp.Product;
                        stockInDetail.ProductMaster = stockTemp.Product.ProductMaster;
                        stockInDetail.StockInDetailPK = new StockInDetailPK
                                                            {
                                                                ProductId = stockTemp.Product.ProductId,
                                                                StockInId = stockIn.StockInId
                                                            };
                        stockIn.StockInDetails.Add(stockInDetail);
                        if(i == e.DeptStockProcessedList.Count -1)
                        {
                            StockOutLogic.AddFixedStockOut(stockOut);
                        }
                        stockTemp.GoodQuantity += needStockMoreQty;
                        stockTemp.Quantity += needStockMoreQty;
                        DepartmentStockLogic.Update(stockTemp);
                    }

                }

                if (stockIn.StockInDetails.Count > 0)
                {
                    StockInLogic.AddFixedStockIn(stockIn);
                }

            }
            catch (Exception)
            {
                e.HasErrors = true;
                throw;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if(stockList.Count <= 0 )
            {
                MessageBox.Show("Không có gì để xử lý");
            }
            DepartmentStockFixingEventArgs eventArgs = new DepartmentStockFixingEventArgs();
            eventArgs.DeptStockProcessedList = new ArrayList();
            #region unused code
            /*foreach (DepartmentStock tempView in stockList)
            {
                long checkedGoodQty = tempView.GoodQuantity;
                long checkedErrorQty = tempView.ErrorQuantity;
                long checkedDamageQty = tempView.DamageQuantity;
                long checkedLostQty = tempView.LostQuantity;
                long checkedUnconfirmQty = tempView.UnconfirmQuantity;

                long goodQty = 0;
                long errorQty = 0;
                long damageQty = 0;
                long lostQty = 0;
                long unconfirmQty = 0;

                foreach(DepartmentStockTemp stockTemp in tempView.DepartmentStockTemps)
                {
                    goodQty += stockTemp.GoodQuantity;
                    errorQty += stockTemp.ErrorQuantity;
                    damageQty += stockTemp.DamageQuantity;
                    lostQty += stockTemp.LostQuantity;
                    unconfirmQty += stockTemp.UnconfirmQuantity;
                }

                bool needAdjust = false;

                if(    checkedGoodQty!=goodQty
                    || checkedErrorQty!= errorQty
                    || checkedLostQty != lostQty
                    || checkedUnconfirmQty != unconfirmQty
                    || checkedDamageQty != damageQty)
                {
                    needAdjust = true;
                }
                SortListByProductId(tempView.DepartmentStockTemps);
                IList departmentStocks = tempView.DepartmentStockTemps;
                if (needAdjust)
                {
                    AdjustGoodQuantity(tempView.DepartmentStockTemps,checkedGoodQty);
                    for(int i=departmentStocks.Count;i>= 0;i--)
                    {
                        DepartmentStockTemp stock = (DepartmentStockTemp) departmentStocks[i];
                        if (i > 0)
                        {
                            // fixing
                            AutoFixing(stock, ref checkedErrorQty, ref checkedDamageQty, ref checkedLostQty, ref checkedUnconfirmQty);
                        }
                        else // last fixing
                        {
                            // don't need to fix, just map the remain quantities to stock
                            stock.ErrorQuantity = checkedErrorQty;
                            stock.DamageQuantity = checkedDamageQty;
                            stock.LostQuantity = checkedLostQty;
                            stock.UnconfirmQuantity = checkedUnconfirmQty;
                        }

                    }
                }
                foreach (DepartmentStockTemp stockTemp in departmentStocks)
                {
                    eventArgs.DeptStockProcessedList.Add(stockTemp);
                }

            }*/
            #endregion

            SortStockTempByDeptId(eventArgs.DeptStockProcessedList);
            eventArgs.DeptStockProcessedList = ObjectConverter.ConvertToNonGenericList(stockList);
            EventUtility.fireEvent(ProcessAdhocStocksEvent,this,eventArgs);
            if(!eventArgs.HasErrors)
            {
                MessageBox.Show("Lưu kết quả thành công !");
                ClearForm();
            }
        }