private bool HasInStockDefectList(DepartmentStockTempView stock, DepartmentStockTempViewCollection list, out int stockDefIndex)
 {
     int count = 0;
     foreach (DepartmentStockTempView stockDefect in list)
     {
         if (stockDefect.ProductMaster.ProductMasterId.Equals(stock.ProductMaster.ProductMasterId))
         {
             stockDefIndex = count;
             return true;
         }
         count += 1;
     }
     stockDefIndex = count;
     return false;
 }
        private void btnSaveSelectedStocks_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn muốn lưu kết quả ?", "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                MessageBoxDefaultButton.Button2);
                if (result == DialogResult.No)
                {
                    dgvStock.Focus();
                    return;
                }

                pnlSelectedStocks.Visible = false;
                if (selectedTempView != null)
                {
                    DepartmentStockTempCollection editedStockTemps = (DepartmentStockTempCollection)bdsSelectedStock.DataSource;
                    selectedTempView.DepartmentStockTemps.Clear();
                    selectedTempView.Quantity = 0;
                    selectedTempView.RealQuantity = 0;
                    selectedTempView.GoodQuantity = 0;
                    selectedTempView.ErrorQuantity = 0;
                    selectedTempView.DamageQuantity = 0;
                    selectedTempView.LostQuantity = 0;
                    selectedTempView.UnconfirmQuantity = 0;
                    foreach (DepartmentStockTemp stockTemp in editedStockTemps)
                    {
                        selectedTempView.GoodQuantity += stockTemp.GoodQuantity;
                        selectedTempView.ErrorQuantity += stockTemp.ErrorQuantity;
                        selectedTempView.LostQuantity += stockTemp.LostQuantity;
                        selectedTempView.DamageQuantity += stockTemp.DamageQuantity;
                        selectedTempView.UnconfirmQuantity += stockTemp.UnconfirmQuantity;
                        selectedTempView.Quantity += stockTemp.Quantity;
                        selectedTempView.RealQuantity += stockTemp.GoodQuantity + stockTemp.ErrorQuantity +
                                                    stockTemp.DamageQuantity + stockTemp.UnconfirmQuantity +
                                                    stockTemp.LostQuantity;
                        selectedTempView.DepartmentStockTemps.Add(stockTemp);
                    }

                    editedStockTemps = null;
                    selectedTempView = null;
                    bdsStockDefect.EndEdit();
                    dgvStock.Enabled = true;
                    dgvStock.Focus();
                    dgvStock.Refresh();
                    dgvStock.Invalidate();
                }
        }
        private void dgvStock_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dgvStock.CurrentCell == null)
            {
                return;
            }
            DataGridViewSelectedCellCollection cellCollection = dgvStock.SelectedCells;
            if(cellCollection.Count <= 0)
            {
                return;
            }
            dgvStock.Enabled = false;
            int startY = dgvStock.Location.Y + dgvStock.ColumnHeadersHeight + (cellCollection[0].OwningRow.Height*(cellCollection[0].RowIndex+1));
            int maxY = dgvStock.Size.Height + dgvStock.Location.Y;
            if(maxY < startY + pnlSelectedStocks.Size.Height)
            {
                startY = startY - cellCollection[0].OwningRow.Height - pnlSelectedStocks.Size.Height;
            }

            pnlSelectedStocks.Location = new Point(pnlSelectedStocks.Location.X, startY);
            pnlSelectedStocks.Visible = true;
            selectedTempView = stockList[cellCollection[0].RowIndex];
            DepartmentStockTempCollection selectedStockList = new DepartmentStockTempCollection(bdsSelectedStock);
            foreach (DepartmentStockTemp departmentStock in selectedTempView.DepartmentStockTemps)
            {
                departmentStock.OldGoodQuantity = departmentStock.GoodQuantity;
                departmentStock.OldErrorQuantity = departmentStock.ErrorQuantity;
                departmentStock.OldUnconfirmQuantity = departmentStock.UnconfirmQuantity;
                departmentStock.OldDamageQuantity = departmentStock.DamageQuantity;
                departmentStock.OldLostQuantity = departmentStock.LostQuantity;
            }
            bdsSelectedStock.EndEdit();
            dgvSelectedStock.Refresh();
            dgvSelectedStock.Invalidate();
            foreach (DepartmentStockTemp stockTemp in selectedTempView.DepartmentStockTemps)
            {
               selectedStockList.Add(stockTemp);
            }
            // save current checking value
        }
        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;
        }