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

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

            EventUtility.fireEvent(LoadAdhocStocksEvent,this,eventArgs);
            if(eventArgs.DeptStockAdhocList!= null && eventArgs.DeptStockAdhocList.Count > 0)
            {
                foreach (DepartmentStockTempView stockTempView in eventArgs.DeptStockAdhocList)
                {
                    stockList.Add(stockTempView);
                }
                bdsStockDefect.EndEdit();
                dgvStock.Refresh();
                dgvStock.Invalidate();
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            DepartmentStockAdhocProcessingEventArgs eventArgs = new DepartmentStockAdhocProcessingEventArgs();
            eventArgs.DeptStockProcessedList = new ArrayList();
            foreach (DepartmentStockTempView 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);
                }

            }
            SortStockTempByDeptId(eventArgs.DeptStockProcessedList);
            EventUtility.fireEvent(ProcessAdhocStocksEvent,this,eventArgs);
            if(!eventArgs.HasErrors)
            {
                MessageBox.Show("Lưu kết quả thành công !");
                ClearForm();
            }
        }
        void _departmentStockAdhocProcessingView_ProcessAdhocStocksEvent(object sender, DepartmentStockAdhocProcessingEventArgs 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.StockInDate = DateTime.Now;
                stockIn.StockInType = 2; // stock in for fixing department stock 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++)
                {
                    DepartmentStockTemp stockTemp = (DepartmentStockTemp) e.DeptStockProcessedList[i];
                    if(stockTemp.DepartmentStockTempPK.DepartmentId != departmentId)
                    {
                        departmentId = stockTemp.DepartmentStockTempPK.DepartmentId;

                        if (stockOut != null)
                        {
                            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;
                    DepartmentStockTempLogic.Update(stockTemp);
                    long realQty = stockTemp.GoodQuantity + stockTemp.ErrorQuantity + stockTemp.DamageQuantity +
                                   stockTemp.LostQuantity + stockTemp.UnconfirmQuantity;
                    if(stockTemp.Quantity < realQty)
                    {
                        long stockInQty = realQty - stockTemp.Quantity;
                        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 = stockInQty;
                        stockOutDetail.Product = stockTemp.Product;
                        stockOutDetail.StockOutId = stockOut.StockoutId;
                        stockOutDetail.StockOut = stockOut;
                        stockOutDetail.DefectStatus = new StockDefectStatus{DefectStatusId = 0};
                        stockOutDetail.Description = "Export goods";
                        stockOutDetail.ProductMaster = stockTemp.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.Quantity = stockInQty;
                        stockInDetail.Product = stockTemp.Product;
                        stockInDetail.ProductMaster = stockTemp.ProductMaster;
                        stockInDetail.StockInDetailPK = new StockInDetailPK
                                                            {
                                                                ProductId = stockTemp.Product.ProductId,
                                                                StockInId = stockIn.StockInId
                                                            };
                        stockIn.StockInDetails.Add(stockInDetail);

                    }

                    if (i == e.DeptStockProcessedList.Count - 1) // last item
                    {
                        StockOutLogic.AddFixedStockOut(stockOut);
                    }
                }
                StockInLogic.AddFixedStockIn(stockIn);

            }
            catch (Exception)
            {
                e.HasErrors = true;
                throw;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (stockList.Count < 1)
            {
                MessageBox.Show("Không có gì để lưu");
                return;
            }
            DepartmentStockAdhocProcessingEventArgs checkingEventArgs = new DepartmentStockAdhocProcessingEventArgs();
            checkingEventArgs.DeptStockProcessedList = ObjectConverter.ConvertToNonGenericList(stockList);
            if (!CheckDepartmentDataIntegrity())
            {
                DialogResult result =
                    MessageBox.Show(
                        "Kết quả xử lý không hoàn tất. Bạn vẫn muốn lưu kết quả ?", "Cảnh báo",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if(result == DialogResult.No)
                {
                    return;
                }

            }
            EventUtility.fireEvent(ProcessAdhocStocksEvent, this, checkingEventArgs);
            if (!checkingEventArgs.HasErrors)
            {
                MessageBox.Show("Lưu kết quả thành công");
                ClearForm();
            }
        }
        void _departmentStockAdhocProcessingView_LoadAdhocStocksEvent(object sender, DepartmentStockAdhocProcessingEventArgs e)
        {
            ObjectCriteria criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddEqCriteria("Fixed", CommonConstants.DEL_FLG_NO);
            criteria.AddOrder("ProductMaster.ProductMasterId", true);
            IList list = DepartmentStockTempLogic.FindAll(criteria);
            IList deptStockTempList = null;
            if (list != null && list.Count > 0)
            {
                deptStockTempList = new ArrayList();

                foreach (DepartmentStockTemp stockTemp in list)
                {
                    int viewIndex = -1;
                    if(HasInList(stockTemp,deptStockTempList,out viewIndex))
                    {
                        DepartmentStockTempView view = (DepartmentStockTempView) deptStockTempList[viewIndex];
                        view.Quantity += stockTemp.Quantity;

                        view.GoodQuantity += stockTemp.GoodQuantity;
                        view.ErrorQuantity += stockTemp.ErrorQuantity;
                        view.DamageQuantity += stockTemp.DamageQuantity;
                        view.LostQuantity += stockTemp.LostQuantity;
                        view.UnconfirmQuantity += stockTemp.UnconfirmQuantity;
                        view.RealQuantity += stockTemp.GoodQuantity + stockTemp.ErrorQuantity + stockTemp.DamageQuantity +
                                        stockTemp.LostQuantity + stockTemp.UnconfirmQuantity;
                        view.DepartmentStockTemps.Add(stockTemp);
                    }
                    else
                    {
                        DepartmentStockTempView view = new DepartmentStockTempView();
                        view.Quantity += stockTemp.Quantity;
                        view.GoodQuantity += stockTemp.GoodQuantity;
                        view.ErrorQuantity += stockTemp.ErrorQuantity;
                        view.DamageQuantity += stockTemp.DamageQuantity;
                        view.LostQuantity += stockTemp.LostQuantity;
                        view.UnconfirmQuantity += stockTemp.UnconfirmQuantity;
                        view.RealQuantity += stockTemp.GoodQuantity + stockTemp.ErrorQuantity + stockTemp.DamageQuantity +
                                        stockTemp.LostQuantity + stockTemp.UnconfirmQuantity;

                        view.ProductMaster = stockTemp.ProductMaster;
                        view.DepartmentStockTemps = new ArrayList();
                        view.DepartmentStockTemps.Add(stockTemp);
                        deptStockTempList.Add(view);
                    }
                }

            }

            e.DeptStockAdhocList = deptStockTempList;
        }