void inventoryCheckingView_LoadGoodsByProductIdEvent(object sender, DepartmentStockCheckingEventArgs e)
 {
     ObjectCriteria objectCriteria = new ObjectCriteria();
     objectCriteria.AddEqCriteria("DepartmentStockPK.ProductId", e.InputBarcode);
     objectCriteria.AddEqCriteria("DepartmentStockPK.DepartmentId", CurrentDepartment.Get().DepartmentId);
     objectCriteria.AddEqCriteria("DelFlg", (long)0);
     System.Collections.IList stockList = DepartmentStockLogic.FindAll(objectCriteria);
     if (stockList != null && stockList.Count > 0)
     {
         e.ScannedStock = (DepartmentStock)stockList[0];
     }
     else
     {
         e.ScannedStock = null;
     }
 }
        void inventoryCheckingView_SaveInventoryCheckingEvent(object sender, DepartmentStockCheckingEventArgs e)
        {
            if (e.SaveStockList != null && e.SaveStockList.Count > 0)
            {

                foreach (DepartmentStock stock in e.SaveStockList)
                {
                    if(    stock.DamageQuantity == 0 && stock.OldDamageQuantity == 0
                        && stock.ErrorQuantity == 0 && stock.OldErrorQuantity == 0
                        && stock.LostQuantity == 0 && stock.OldLostQuantity == 0
                        && stock.UnconfirmQuantity == 0 && stock.OldUnconfirmQuantity == 0 )
                    {
                        continue;
                    }

                    stock.CreateDate = DateTime.Now;
                    stock.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    stock.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    stock.UpdateDate = DateTime.Now;
                    stock.DelFlg = 0;

                    DepartmentStockLogic.Update(stock);
                }

            }
        }
 void departmentStockCheckingView_LoadGoodsByProductIdEvent(object sender, DepartmentStockCheckingEventArgs e)
 {
 }
 void departmentStockCheckingView_SaveTempInventoryCheckingEvent(object sender, DepartmentStockCheckingEventArgs e)
 {
 }
        private void UpdateScanType(DepartmentStockView stock)
        {
            ScanType scannedType = GetFromScanList(scanTypesList, stock);
            if (scannedType != null)
            {
                IList unscanList = scannedType.UnscanProducts;
                string productName = stock.ProductMaster.ProductName + "_" + stock.ProductMaster.ProductColor.ColorName +
                                     "_" + stock.ProductMaster.ProductSize.SizeName;
                int scanIndex = GetIndexFromList(unscanList, productName);
                if (scanIndex >= 0)
                {
                    scannedType.ScannedProducts.Add(unscanList[scanIndex]);
                    unscanList.RemoveAt(scanIndex);
                }
            }
            else
            {
                // create new scantype and add to scanTypeList
                ScanType scanType = new ScanType();
                scanType.ScannedProducts = new ArrayList();
                scanType.UnscanProducts = new ArrayList();
                scanType.TypeName = stock.ProductMaster.ProductType.TypeName;
                DepartmentStockCheckingEventArgs eventArgs = new DepartmentStockCheckingEventArgs();
                eventArgs.ScannedType = scanType;
                EventUtility.fireEvent(LoadProductNamesInTypeEvent, this, eventArgs);
                scanTypesList.Add(eventArgs.ScannedType);

            }
            cboTypeList.Refresh();
            cboTypeList.Invalidate();
            cboTypeList_SelectedIndexChanged(null, null);
        }
 void departmentStockCheckingView_FillProductMasterToComboEvent(object sender, DepartmentStockCheckingEventArgs e)
 {
 }
        private void button1_Click(object sender, EventArgs e)
        {
            if (stockList.Count < 1)
            {
                MessageBox.Show("Không có gì để lưu");
                return;
            }
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.SaveStockViewList = ObjectConverter.ConvertToNonGenericList(stockList);
            if (!CheckDepartmentDataIntegrity())
            {
                DialogResult result =
                    MessageBox.Show(
                        "Kết quả kiểm kê không khớp với số liệu trong chương trình. 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(SaveInventoryCheckingEvent, this, checkingEventArgs);
            if (!checkingEventArgs.HasErrors)
            {
                MessageBox.Show("Lưu kết quả thành công");
                ClearForm();
                // clear all temp files
                ClearTempFiles();
            }
        }
        private void LoadGoodsByProductId(string id)
        {
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.InputBarcode = id;
            EventUtility.fireEvent(LoadGoodsByProductIdEvent, this, checkingEventArgs);
            DepartmentStockView stock = checkingEventArgs.ScannedStockView;
            if (stock == null)
            {
                if(!checkingEventArgs.UnconfirmTempBarcode)
                MessageBox.Show("Không tìm thấy mã vạch trong kho", "Lỗi");
                return;
            }

            txtProductType.Text = stock.ProductMaster.ProductType.TypeName;
            txtProductName.Text = stock.ProductMaster.ProductName;
            txtStockQuantity.Text = stock.Quantity.ToString("##,##0");
            txtDescription.Text = stock.ProductMaster.Description;
            pictureBox1.ImageLocation = stock.ProductMaster.ImagePath;
            if(!CheckUtility.IsNullOrEmpty(pictureBox1.ImageLocation))
            {
                if (File.Exists(pictureBox1.ImageLocation))
                {
                    pictureBox1.Load();
                }
            }

            int stockDefIndex = -1;
            if (dgvStocks.CurrentCell != null)
            {
                stockDefIndex = dgvStocks.CurrentCell.RowIndex;
            }
            if (HasInStockDefectList(stock, stockList, out stockDefIndex))
            {
                if (stockDefIndex > -1 && stockDefIndex < stockList.Count)
                {
                    stockList[stockDefIndex].GoodQuantity += 1;
                    dgvStocks.CurrentCell = dgvStocks[5, stockDefIndex];
                }

            }
            else // create new stock defect row
            {
                    stockList.AddNew();
                    DepartmentStockView defect = checkingEventArgs.ScannedStockView;
                    stockList[stockList.Count - 1] = defect;

                    stockList[stockList.Count - 1].GoodQuantity = 1;
                    stockList[stockList.Count - 1].OldErrorQuantity = stockList[stockList.Count - 1].ErrorQuantity = 0;
                    stockList[stockList.Count - 1].OldDamageQuantity = stockList[stockList.Count - 1].DamageQuantity = 0;
                    stockList[stockList.Count - 1].OldLostQuantity = stockList[stockList.Count - 1].LostQuantity = 0;
                    stockList[stockList.Count - 1].OldUnconfirmQuantity = stockList[stockList.Count - 1].UnconfirmQuantity = 0;

                    txtStockQuantity.Text = stockList[stockList.Count - 1].Quantity.ToString("##,##0");
                    dgvStocks.CurrentCell = dgvStocks[5, stockList.Count - 1];
            }

            // update scanned type and product
            //UpdateScanType(stock);
            ScanType scannedType = GetFromScanList(scanTypesList, stock);
            if(scannedType == null)
            {
                // create new scantype and add to scanTypeList
                ScanType scanType = new ScanType();
                scanType.ScannedProducts = new ArrayList();
                scanType.UnscanProducts = new ArrayList();
                scanType.TypeName = stock.ProductMaster.ProductType.TypeName;
                DepartmentStockCheckingEventArgs eventArgs = new DepartmentStockCheckingEventArgs();
                eventArgs.ScannedType = scanType;
                EventUtility.fireEvent(LoadProductNamesInTypeEvent, this, eventArgs);
                scanTypesList.Add(eventArgs.ScannedType);
                scannedType = eventArgs.ScannedType;
            }

                IList unscanList = scannedType.UnscanProducts;
                string productName = stock.ProductMaster.ProductName + "_" + stock.ProductMaster.ProductColor.ColorName +
                                     "_" + stock.ProductMaster.ProductSize.SizeName;
                int scanIndex = GetIndexFromList(unscanList, productName);
                if (scanIndex >= 0)
                {
                    scannedType.ScannedProducts.Add(unscanList[scanIndex]);
                    unscanList.RemoveAt(scanIndex);
                }

            cboTypeList.Refresh();
            cboTypeList.Invalidate();
            cboTypeList_SelectedIndexChanged(null, null);

            bdsStockDefect.EndEdit();
            dgvStocks.Refresh();
            dgvStocks.Invalidate();
            txtBarcode.Text = "";
            txtBarcode.Focus();
        }
        void departmentStockCheckingView_LoadTempInventoryCheckingEvent(object sender, DepartmentStockCheckingEventArgs e)
        {
            // search in temp stock
            ObjectCriteria criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DepartmentStockTempPK.DepartmentId", CurrentDepartment.Get().DepartmentId);
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddEqCriteria("Fixed", CommonConstants.DEL_FLG_NO);
            criteria.AddEqCriteria("TempSave", 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 stockTempSave in list)
                {
                    int viewIndex = -1;
                    DepartmentStock stockTemp = new DepartmentStockMapper().Convert(stockTempSave);
                    if (HasInList(stockTemp, deptStockTempList, out viewIndex))
                    {
                        DepartmentStockView view = (DepartmentStockView)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.DepartmentStocks.Add(stockTemp);
                    }
                    else
                    {
                        DepartmentStockView view = new DepartmentStockView();

                        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.ProductMaster = stockTemp.ProductMaster;
                        view.DepartmentStocks = new ArrayList();
                        view.DepartmentStocks.Add(stockTemp);
                        deptStockTempList.Add(view);
                    }
                }

            }

            e.ReturnStockViewList = deptStockTempList;
        }
        void departmentStockCheckingView_SaveTempInventoryCheckingEvent(object sender, DepartmentStockCheckingEventArgs e)
        {
            try
            {
                DepartmentStockTempLogic.TempSave(e.SaveStockViewList);
            }
            catch (Exception)
            {

                e.HasErrors = true;
            }
        }
        void departmentStockCheckingView_LoadProductNamesInTypeEvent(object sender, DepartmentStockCheckingEventArgs e)
        {
            SubObjectCriteria prdTypeCrit = new SubObjectCriteria("ProductType");
            prdTypeCrit.AddEqCriteria("TypeName",e.ScannedType.TypeName);

            ObjectCriteria criteria = new ObjectCriteria();
            criteria.AddSubCriteria("ProductType",prdTypeCrit);

            IList prdMasterList = ProductMasterLogic.FindAll(criteria);
            foreach (ProductMaster master in prdMasterList)
            {
                string productName = master.ProductName + "_" + master.ProductColor.ColorName + "_" +
                                     master.ProductSize.SizeName;
                if (!HasInList(e.ScannedType.UnscanProducts, productName))
                {
                    e.ScannedType.UnscanProducts.Add(productName);
                }
            }
        }
        private void LoadGoodsByProductId(string id)
        {
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.InputBarcode = id;
            EventUtility.fireEvent(LoadGoodsByProductIdEvent, this, checkingEventArgs);
            DepartmentStockView stock = checkingEventArgs.ScannedStockView;
            if (stock == null)
            {
                if(!checkingEventArgs.UnconfirmTempBarcode)
                MessageBox.Show("Không tìm thấy mã vạch trong kho", "Lỗi");
                return;
            }

            txtProductType.Text = stock.ProductMaster.ProductType.TypeName;
            txtProductName.Text = stock.ProductMaster.ProductName;
            txtStockQuantity.Text = stock.Quantity.ToString("##,##0");
            txtDescription.Text = stock.ProductMaster.Description;
            pictureBox1.ImageLocation = stock.ProductMaster.ImagePath;
            if(!CheckUtility.IsNullOrEmpty(pictureBox1.ImageLocation))
            {
                pictureBox1.Load();
            }

            int stockDefIndex = -1;
            if (dgvStocks.CurrentCell != null)
            {
                stockDefIndex = dgvStocks.CurrentCell.RowIndex;
            }
            if (HasInStockDefectList(stock, stockList, out stockDefIndex))
            {
                if (stockDefIndex > -1 && stockDefIndex < stockList.Count)
                {
                    stockList[stockDefIndex].GoodQuantity += 1;
                    dgvStocks.CurrentCell = dgvStocks[5, stockDefIndex];
                }

            }
            else // create new stock defect row
            {
                    stockList.AddNew();
                    DepartmentStockView defect = checkingEventArgs.ScannedStockView;
                    stockList[stockList.Count - 1] = defect;

                    stockList[stockList.Count - 1].GoodQuantity = 1;
                    stockList[stockList.Count - 1].OldErrorQuantity = stockList[stockList.Count - 1].ErrorQuantity = 0;
                    stockList[stockList.Count - 1].OldDamageQuantity = stockList[stockList.Count - 1].DamageQuantity = 0;
                    stockList[stockList.Count - 1].OldLostQuantity = stockList[stockList.Count - 1].LostQuantity = 0;
                    stockList[stockList.Count - 1].OldUnconfirmQuantity = stockList[stockList.Count - 1].UnconfirmQuantity = 0;

                    txtStockQuantity.Text = stockList[stockList.Count - 1].Quantity.ToString("##,##0");
                    dgvStocks.CurrentCell = dgvStocks[5, stockList.Count - 1];
            }
            bdsStockDefect.EndEdit();
            dgvStocks.Refresh();
            dgvStocks.Invalidate();
            txtBarcode.Text = "";
            txtBarcode.Focus();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (stockList.Count < 1)
            {
                MessageBox.Show("Không có gì để lưu");
                return;
            }
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.SaveStockViewList = ObjectConverter.ConvertToNonGenericList(stockList);

            EventUtility.fireEvent(SaveTempInventoryCheckingEvent, this, checkingEventArgs);
            if (!checkingEventArgs.HasErrors)
            {
                MessageBox.Show("Lưu kết quả tạm thành công");
            }
        }
        private void LoadGoodsByProductId(string id)
        {
            if(this.Status == ViewStatus.OPENDIALOG)
            {
                bool checkInList = false;
                if(RestrictDepartmentStocks!=null)
                {
                    foreach (DepartmentStock departmentStock in RestrictDepartmentStocks)
                    {
                        if(departmentStock.Product.ProductId.Equals(id))
                        {
                            checkInList = true;
                            break;
                        }
                    }
                }
                if(!checkInList)
                {
                    string productName = "";
                    if(RestrictProductMaster!=null)
                    {
                        productName = RestrictProductMaster.ProductName;
                    }
                    MessageBox.Show("Mã vạch không phải của mặt hàng " + productName + " cần kiểm tra", "Lỗi");
                    return;
                }
            }
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.InputBarcode = id;
            EventUtility.fireEvent(LoadGoodsByProductIdEvent, this, checkingEventArgs);
            DepartmentStock stock = checkingEventArgs.ScannedStock;
            if (stock == null)
            {
                MessageBox.Show("Không tìm thấy mã vạch trong kho", "Lỗi");
                return;
            }

            txtProductType.Text = stock.Product.ProductMaster.ProductType.TypeName;
            txtProductName.Text = stock.Product.ProductMaster.ProductName;
            txtStockQuantity.Text = stock.Quantity.ToString("##,##0");
            txtDescription.Text = stock.Product.ProductMaster.Description;
            pictureBox1.ImageLocation = stock.Product.ProductMaster.ImagePath;
            if(!CheckUtility.IsNullOrEmpty(pictureBox1.ImageLocation))
            {
                pictureBox1.Load();
            }

            int stockDefIndex = -1;
            if (dgvStock.CurrentCell != null)
            {
                stockDefIndex = dgvStock.CurrentCell.RowIndex;
            }
            if (HasInStockDefectList(stock, stockList, out stockDefIndex))
            {
                if (stockDefIndex > -1 && stockDefIndex < stockList.Count)
                {
                    stockList[stockDefIndex].GoodQuantity += 1;
                    dgvStock.CurrentCell = dgvStock[5, stockDefIndex];
                }

            }
            else // create new stock defect row
            {
                    stockList.AddNew();
                    DepartmentStock defect = checkingEventArgs.ScannedStock;
                    stockList[stockList.Count - 1] = defect;

                    stockList[stockList.Count - 1].GoodQuantity = 1;
                    stockList[stockList.Count - 1].OldErrorQuantity = stockList[stockList.Count - 1].ErrorQuantity = 0;
                    stockList[stockList.Count - 1].OldDamageQuantity = stockList[stockList.Count - 1].DamageQuantity = 0;
                    stockList[stockList.Count - 1].OldLostQuantity = stockList[stockList.Count - 1].LostQuantity = 0;
                    stockList[stockList.Count - 1].OldUnconfirmQuantity = stockList[stockList.Count - 1].UnconfirmQuantity = 0;

                    txtStockQuantity.Text = stockList[stockList.Count - 1].Quantity.ToString("##,##0");
                    dgvStock.CurrentCell = dgvStock[5, stockList.Count - 1];
            }
            bdsStockDefect.EndEdit();
            dgvStock.Refresh();
            dgvStock.Invalidate();
            txtBarcode.Text = "";
            txtBarcode.Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (stockList.Count < 1)
            {
                MessageBox.Show("Không có gì để lưu");
            }
            DepartmentStockCheckingEventArgs checkingEventArgs = new DepartmentStockCheckingEventArgs();
            checkingEventArgs.SaveStockList = ObjectConverter.ConvertToNonGenericList(stockList);

            if(Status == ViewStatus.OPENDIALOG )
            {
                if(!CheckDepartmentDataIntegrity())
                {
                    DialogResult result =MessageBox.Show(
                        "Số liệu trong chương trình không khớp với số liệu kiểm kê thực tế. Bạn vẫn muốn chấp nhận kết quả kiểm kê này ?",
                        "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                    if(result == DialogResult.No)
                    {
                        return;
                    }
                }
                this.RestrictDepartmentStocks = checkingEventArgs.SaveStockList;
                if(this.RestrictDepartmentStocksChecked!=null)
                {
                    RestrictDepartmentStocksChecked(this, null);
                }
                Close();
                return;
            }
            if (!CheckDepartmentDataIntegrity())
                return;
            EventUtility.fireEvent(SaveInventoryCheckingEvent, this, checkingEventArgs);
            MessageBox.Show("Lưu kết quả thành công");
            ClearForm();
        }