private void btnPMChoosing_Click(object sender, EventArgs e)
        {
            ProductMasterChoosingForm pmChoosingForm = new ProductMasterChoosingForm();
            pmChoosingForm.ShowDialog();

            if (pmChoosingForm.SelectedProductMasterList == null) return;
            var eventArgs = new MainStockOutEventArgs();
            eventArgs.DefectStatusId = ((StockDefectStatus)cbbStockOutType.SelectedItem).DefectStatusId;
            eventArgs.SelectedProductMasterList = pmChoosingForm.SelectedProductMasterList;
            EventUtility.fireEvent(LoadStockStatusEvent, this, eventArgs);
            if (eventArgs.FoundStockOutDetailList != null && eventArgs.FoundStockOutDetailList.Count > 0)
            {
                foreach (StockOutDetail detail in eventArgs.FoundStockOutDetailList)
                {
                    bool found = false;
                    foreach (StockOutDetail detail1 in stockOutDetailList)
                    {
                        if (detail.Product.ProductId.Equals(detail1.Product.ProductId))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                    StockDefectStatus defectStatus = (StockDefectStatus) cbbStockOutType.SelectedItem;
                    if (defectStatus.DefectStatusId == 4)
                    {
                        // if xuattam, so we check error quantity & good quantity ( for shoes )
                        if (detail.GoodQuantity == 0 && detail.ErrorQuantity == 0) // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }

                    if (defectStatus.DefectStatusId == 5)
                    {
                        // if travenhasanxuat, so we check good && error quantity
                        if (detail.GoodQuantity == 0 /*&& detail.ErrorQuantity == 0*/)
                            // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }

                    if (defectStatus.DefectStatusId == 7)
                    {
                        // if xuatdicuahangkhac, so we check good quantity
                        if (detail.GoodQuantity == 0) // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }
                    detail.DefectStatus = defectStatus;
                    stockOutDetailList.Add(detail);
                    stockOutDetailList.EndNew(stockOutDetailList.Count - 1);
                    LockField(stockOutDetailList.Count - 1, detail);
                }
                foreach (Stock def in eventArgs.StockList)
                {
                    bool found = false;
                    foreach (Stock detail in stockList)
                    {
                        if (def.Product.ProductId.Equals(detail.Product.ProductId))
                        {

                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        stockList.Add(def);
                    }
                }
            }
            CalculateTotalStorePrice();
        }
        private void btnChoose_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection collection = dgvStockIn.SelectedRows;
            if (collection.Count > 0)
            {
                IList stockInIds = new ArrayList();
                foreach (DataGridViewRow selectedRowCollection in collection)
                {
                    string stockInId = selectedRowCollection.Cells[0].Value.ToString();
                    stockInIds.Add(stockInId);
                }

                MainStockOutEventArgs ea = new MainStockOutEventArgs();
                ea.SelectedStockInIds = stockInIds;

                EventUtility.fireAsyncEvent(FindByStockInIdEvent, this, ea, new AsyncCallback(EndEvent));
                this.Enabled = false;
                //EventUtility.fireEvent(FindByStockInIdEvent,this,ea);
            }
        }
 private void btnInput_Click(object sender, EventArgs e)
 {
     if (cbbStockOutType.SelectedIndex < 0)
     {
         MessageBox.Show("Hãy chọn Loại để xuất kho");
         return;
     }
     if (string.IsNullOrEmpty(txtBarcode.Text))
     {
         if (cboProductMasters.SelectedIndex < 0)
         {
             MessageBox.Show("Hãy chọn 1 sản phẩm để xuất kho");
             return;
         }
         if (lstColor.SelectedIndices == null || lstColor.SelectedIndices.Count <= 0)
         {
             MessageBox.Show("Hãy chọn màu sản phẩm để xuất kho");
             return;
         }
         if (lstSize.SelectedIndices == null || lstSize.SelectedIndices.Count <= 0)
         {
             MessageBox.Show("Hãy chọn kích cỡ sản phẩm để xuất kho");
             return;
         }
         if (cbbStockOutType.SelectedIndex < 0)
         {
             MessageBox.Show("Hãy chọn Loại để xuất kho");
             return;
         }
         //            long outValue = 0;
         //            if (!NumberUtility.CheckLongNullIsZero(txtPriceIn.Text, out outValue)
         //                || outValue < 0
         //                || !NumberUtility.CheckLongNullIsZero(txtPriceOut.Text, out outValue)
         //                || outValue < 0)
         //            {
         //                MessageBox.Show("Giá phải là số dương");
         //                return;
         //            }
         cbbStockOutType.Enabled = false;
         PopulateGridByProductMaster(lstColor.SelectedItems, lstSize.SelectedItems);
         CalculateTotalStorePrice();
     }
     else
     {
         var eventArgs = new MainStockOutEventArgs();
         eventArgs.ProductId = txtBarcode.Text;
         EventUtility.fireEvent(FindBarcodeEvent, this, eventArgs);
         if (eventArgs.EventResult == null)
         {
             MessageBox.Show("Không tìm thấy mã vạch này");
             return;
         }
         bool found = false;
         foreach (StockOutDetail detail in stockOutDetailList)
         {
             if (eventArgs.SelectedStockOutDetail.Product.ProductId.Equals(detail.Product.ProductId))
             {
                 found = true;
                 break;
             }
         }
         if (found)
         {
             MessageBox.Show("Mã vạch đã được nhập");
             return;
         }
         if (eventArgs.Stock != null)
         {
             found = false;
             foreach (Stock detail in stockList)
             {
                 if (eventArgs.Stock.Product.ProductId.Equals(detail.Product.ProductId))
                 {
                     found = true;
                     break;
                 }
             }
             if (!found)
             {
                 stockList.Add(eventArgs.Stock);
             }
         }
         stockOutDetailList.Add(eventArgs.SelectedStockOutDetail);
         stockOutDetailList.EndNew(stockOutDetailList.Count - 1);
         cbbStockOutType.Enabled = false;
         LockField(stockOutDetailList.Count - 1, eventArgs.SelectedStockOutDetail);
     }
 }
        private void txtReadBarcode_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Filter = "Text Files|*.txt";
            DialogResult result = fileDialog.ShowDialog();
            if(result == DialogResult.OK)
            {

                Dictionary<string,int> list = new Dictionary<string, int>();
                string path = fileDialog.FileName;
                StreamReader fileReader = new StreamReader(File.OpenRead(path));

                while(!fileReader.EndOfStream)
                {
                    string line = fileReader.ReadLine();
                    string[] parseLines = line.Split(',');

                    try
                    {
                        if (parseLines.Length == 2)
                        {
                            if(list.ContainsKey(parseLines[0].Trim()))
                            {
                                list[parseLines[0].Trim()] += Int32.Parse(parseLines[1].Trim());
                            }
                            else
                            {
                                list.Add(parseLines[0].Trim(), Int32.Parse(parseLines[1].Trim()));
                            }

                        }
                        else
                        {
                            if (list.ContainsKey(parseLines[0].Trim()))
                            {
                                list[parseLines[0].Trim()] += 1;
                            }
                            else
                            {
                                list.Add(parseLines[0].Trim(), 1);
                            }

                        }
                    }
                    catch (Exception)
                    {
                        if(_errorForm == null)
                        {
                            _errorForm = new ErrorForm();
                            _errorForm.Caption = "Lỗi";
                            _errorForm.ErrorString = "Các mã vạch bị lỗi khi nhập mã vạch từ file text";
                        }
                        _errorForm.ErrorDetails.Add(line);
                        continue;
                    }
                }
                foreach (KeyValuePair<string, int> barCodeLine in list)
                {
                    if (!string.IsNullOrEmpty(barCodeLine.Key) && barCodeLine.Key.Length == 12)
                    {
                        var eventArgs = new MainStockOutEventArgs();
                        eventArgs.ProductId = barCodeLine.Key;
                        eventArgs.DefectStatusId = ((StockDefectStatus)cbbStockOutType.SelectedItem).DefectStatusId;
                        EventUtility.fireEvent(FindBarcodeEvent, this, eventArgs);
                        if (eventArgs.EventResult == null)
                        {
                            if (_errorForm == null)
                            {
                                _errorForm = new ErrorForm();
                                _errorForm.Caption = "Lỗi";
                                _errorForm.ErrorString = "Các mã vạch bị lỗi khi nhập mã vạch từ file text";
                            }
                            _errorForm.ErrorDetails.Add(barCodeLine.Key +","+barCodeLine.Value);
                            continue;
                        }
                        bool found = false;
                        StockOutDetail foundStockOutDetail = null;
                        foreach (StockOutDetail detail in stockOutDetailList)
                        {
                            if (eventArgs.SelectedStockOutDetail.Product.ProductId.Equals(detail.Product.ProductId))
                            {
                                found = true;
                                foundStockOutDetail = detail;
                                break;
                            }
                        }
                        if (found)
                        {
                            //MessageBox.Show("Mã vạch đã được nhập");
                            foundStockOutDetail.GoodQuantity += barCodeLine.Value;
                            continue;
                        }
                        if (eventArgs.Stock != null)
                        {
                            found = false;
                            foreach (Stock detail in stockList)
                            {
                                if (eventArgs.Stock.Product.ProductId.Equals(detail.Product.ProductId))
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                stockList.Add(eventArgs.Stock);
                            }
                        }
                        // reset quantity to 1
                        eventArgs.SelectedStockOutDetail.GoodQuantity = barCodeLine.Value;
                        stockOutDetailList.Add(eventArgs.SelectedStockOutDetail);
                        stockOutDetailList.EndNew(stockOutDetailList.Count - 1);
                        cbbStockOutType.Enabled = false;
                        LockField(stockOutDetailList.Count - 1, eventArgs.SelectedStockOutDetail);
                    }

                }
                CalculateTotalStorePrice();
                if(_errorForm!= null)
                {
                    _errorForm.ShowDialog();
                    _errorForm = null;
                }
            }
        }
 void _mainStockOutController_CompletedFindByStockInEvent(object sender, MainStockOutEventArgs e)
 {
     IList list = e.SelectedStockOutDetails;
     if (list != null && list.Count > 0)
     {
         foreach (StockOutDetail needStockOutDetail in list)
         {
             /*if (!IsInList(stockOutDetailList, stockInDetail))
             {
                 stockInDetail.DefectStatus = (StockDefectStatus) cbbStockOutType.SelectedItem;
                 stockOutDetailList.Add(stockInDetail);
             }*/
             bool found = false;
             foreach (StockOutDetail detail in stockOutDetailList)
             {
                 if(detail.Product.ProductId.Equals(needStockOutDetail.Product.ProductId))
                 {
                     found = true;
                     detail.Quantity += needStockOutDetail.Quantity;
                     detail.GoodQuantity += needStockOutDetail.GoodQuantity;
                     break;
                 }
             }
             if(!found)
             {
                 needStockOutDetail.DefectStatus = (StockDefectStatus) cbbStockOutType.SelectedItem;
                 stockOutDetailList.Add(needStockOutDetail);
             }
         }
     }
     this.Enabled = true;
     bdsStockIn.ResetBindings(false);
     dgvDeptStockOut.Refresh();
     dgvStockIn.Invalidate();
     panelStockIns.Visible = false;
     cbbStockOutType.Enabled = false;
     CalculateTotalStorePrice();
 }
 private void FillProductToComboBox(object sender, string name)
 {
     var mainStockInEventArgs = new MainStockOutEventArgs();
     if (dgvDeptStockOut == null || dgvDeptStockOut.CurrentRow == null)
     {
         return;
     }
     int selectedIndex = dgvDeptStockOut.CurrentRow.Index;
     mainStockInEventArgs.SelectedIndex = selectedIndex;
     mainStockInEventArgs.SelectedStockOutDetail = stockOutDetailList[selectedIndex];
     mainStockInEventArgs.IsFillToComboBox = true;
     if (name.Equals("columnProductName"))
     {
         mainStockInEventArgs.ComboBoxDisplayMember = "ProductName";
     }
     if (name.Equals("columnProductId"))
     {
         mainStockInEventArgs.ComboBoxDisplayMember = "ProductMasterId";
     }
     EventUtility.fireEvent<MainStockOutEventArgs>(FillProductToComboEvent, sender, mainStockInEventArgs);
 }
        void mainStockOutView_FindByStockInIdEvent(object sender, MainStockOutEventArgs e)
        {
            if (e.SelectedStockInIds.Count > 0)
            {
                ObjectCriteria objectCriteria = new ObjectCriteria();
                objectCriteria.AddSearchInCriteria("StockInDetailPK.StockInId", e.SelectedStockInIds);
                IList list = StockInDetailLogic.FindAll(objectCriteria);
                IList stockOutList = new ArrayList();
                foreach (StockInDetail inDetail in list)
                {
                    StockOutDetail deptDetail = new StockOutDetail();
                    deptDetail.CreateDate = DateTime.Now;
                    deptDetail.UpdateDate = DateTime.Now;
                    deptDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    deptDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    //deptDetail. = new DepartmentStockInDetailPK();
                    deptDetail.Product = inDetail.Product;
                    deptDetail.ProductMaster = inDetail.Product.ProductMaster;
                    deptDetail.Quantity = inDetail.Quantity;
                    deptDetail.GoodQuantity = inDetail.Quantity;
                    stockOutList.Add(deptDetail);
                }
                GetRemainStockNumber(stockOutList);

                e.SelectedStockOutDetails = stockOutList;
            }
            EventUtility.fireEvent(CompletedFindByStockInEvent, this, e);
        }
 public void mainStockOutView_SaveStockOutEvent(object sender, MainStockOutEventArgs e)
 {
     if (e.StockOut.StockoutId == 0)
     {
         StockOutLogic.Add(e.StockOut);
         ClientUtility.Log(logger, e.StockOut.ToString(), "Xuất kho");
         e.EventResult = "Success";
     }
 }
        public void _departmentStockInView_FindBarcodeEvent(object sender, MainStockOutEventArgs e)
        {
            var criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddEqCriteria("Product.ProductId", e.ProductId);
            IList list = StockLogic.FindAll(criteria);
            if (list.Count == 0)
            {
                return;
            }
            Stock stock = list[0] as Stock;
            e.SelectedStockOutDetail = new StockOutDetail();
            e.SelectedStockOutDetail.Product = stock.Product;
            e.SelectedStockOutDetail.Quantity = stock.Quantity;
            e.SelectedStockOutDetail.ErrorQuantity = stock.ErrorQuantity;
            e.SelectedStockOutDetail.LostQuantity = stock.LostQuantity;
            e.SelectedStockOutDetail.DamageQuantity = stock.DamageQuantity;
            e.SelectedStockOutDetail.UnconfirmQuantity = stock.UnconfirmQuantity;
            e.SelectedStockOutDetail.Quantity = stock.Quantity;
            e.EventResult = "Success";

            e.Stock = stock;
        }
 public void mainStockOutView_LoadProductSizeEvent(object sender, MainStockOutEventArgs e)
 {
     if (e.SelectedStockOutDetail != null && e.SelectedStockOutDetail.Product != null && !string.IsNullOrEmpty(e.SelectedStockOutDetail.Product.ProductMaster.ProductName))
     {
         var subCriteria = new SubObjectCriteria("ProductMasters");
         subCriteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
         subCriteria.AddEqCriteria("ProductType", e.SelectedStockOutDetail.Product.ProductMaster.ProductType);
         subCriteria.AddEqCriteria("ProductName", e.SelectedStockOutDetail.Product.ProductMaster.ProductName);
         subCriteria.AddEqCriteria("ProductColor", e.SelectedStockOutDetail.Product.ProductMaster.ProductColor);
         subCriteria.AddEqCriteria("Country", e.SelectedStockOutDetail.Product.ProductMaster.Country);
         subCriteria.AddEqCriteria("Manufacturer", e.SelectedStockOutDetail.Product.ProductMaster.Manufacturer);
         subCriteria.AddEqCriteria("Distributor", e.SelectedStockOutDetail.Product.ProductMaster.Distributor);
         subCriteria.AddEqCriteria("Packager", e.SelectedStockOutDetail.Product.ProductMaster.Packager);
         var criteria = new ObjectCriteria();
         criteria.AddSubCriteria("ProductMasters", subCriteria);
         IList productSizes = ProductSizeLogic.FindAll(criteria);
         e.ProductSizeList = productSizes;
     }
 }
        public void mainStockOutView_LoadStockStatusEvent(object sender, MainStockOutEventArgs e)
        {
            IList productMasterIds = new ArrayList();
            foreach (ProductMaster master in e.SelectedProductMasterList)
            {
                productMasterIds.Add(master.ProductMasterId);
            }
            var criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddSearchInCriteria("ProductMaster.ProductMasterId", productMasterIds);
            IList list = StockLogic.FindAll(criteria);
            if (list.Count == 0)
            {
                return;
            }
            e.StockList = new ArrayList();
            e.FoundStockOutDetailList = new ArrayList();
            foreach (Stock stock in list)
            {
                StockOutDetail detail = new StockOutDetail();
                detail.Product = stock.Product;
                detail.GoodQuantity = stock.GoodQuantity;
                detail.ErrorQuantity = stock.ErrorQuantity;
                detail.LostQuantity = stock.LostQuantity;
                detail.DamageQuantity = stock.DamageQuantity;
                detail.UnconfirmQuantity = stock.UnconfirmQuantity;
                detail.Quantity = stock.Quantity;
                e.StockList.Add(stock);

                e.FoundStockOutDetailList.Add(detail);
            }
        }
 public void mainStockOutView_LoadGoodsByNameEvent(object sender, MainStockOutEventArgs e)
 {
     StockOutDetail detail = e.SelectedStockOutDetail;
     ObjectCriteria objectCriteria = new ObjectCriteria();
     objectCriteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
     objectCriteria.AddEqCriteria("ProductName", detail.Product.ProductMaster.ProductName);
     IList list = ProductMasterLogic.FindAll(objectCriteria);
     e.FoundProductMasterList = list;
     if (list == null || list.Count == 0)
     {
         return;
     }
     ProductMaster prodMaster = list[0] as ProductMaster;
     detail.Product.ProductMaster = prodMaster;
     e.SelectedStockOutDetail = detail;
     IList detailList = new ArrayList();
     detailList.Add(detail);
     //            GetRemainStockNumber(detailList);
 }
 public void mainStockOutView_LoadGoodsByNameColorSizeEvent(object sender, MainStockOutEventArgs e)
 {
     throw new NotImplementedException();
 }
        public void mainStockOutView_FillProductToComboEvent(object sender, MainStockOutEventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            string originalText = comboBox.Text;
            if (e.IsFillToComboBox)
            {
                ProductMaster searchPM = e.SelectedStockOutDetail.Product.ProductMaster;
                var criteria = new ObjectCriteria(true);
                criteria.AddEqCriteria("pm.DelFlg", CommonConstants.DEL_FLG_NO);
                criteria.AddEqCriteria("stock.DelFlg", CommonConstants.DEL_FLG_NO);
                criteria.AddLikeCriteria("pm.ProductName","%" +searchPM.ProductName + "%");
                criteria.MaxResult = 50;
                IList list = StockLogic.FindByQueryForStockIn(criteria);

                if (list == null || list.Count == 0)
                {
                    return;
                }
                IList result = new ArrayList();
                foreach (Stock stock in list)
                {
                    result.Add(stock.ProductMaster);
                }
                IList retlist = RemoveDuplicateName(result);

                result = new ArrayList();
                int count = 0;
                foreach (var p in retlist)
                {
                    if (count == 50)
                    {
                        break;
                    }
                    result.Add(p);
                    count++;
                }

                var productMasters = new BindingList<ProductMaster>();
                foreach (ProductMaster master in result)
                {
                    productMasters.Add(master);
                }

                var bindingSource = new BindingSource();
                bindingSource.DataSource = productMasters;
                comboBox.DataSource = bindingSource;
                comboBox.DisplayMember = "TypeAndName";
                comboBox.ValueMember = e.ComboBoxDisplayMember;
                comboBox.DropDownWidth = 300;
                comboBox.DropDownHeight = 200;
                // keep the original text
                comboBox.Text = originalText;
                //comboBox.SelectedIndex = -1;
                //comboBox.SelectionStart = comboBox.Text.Length;
                //comboBox.DroppedDown = true;
                comboBox.MaxDropDownItems = 10;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(
                "Bạn hãy kiểm tra kỹ trước khi lưu số liệu bởi vì sau khi lưu sẽ không thay đổi được nữa. Bạn có chắc chắn muốn lưu ?", "Xác nhận",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.No)
            {
                return;
            }
            // first remove all blank row
            int count = 0;
            int length = stockOutDetailList.Count;
            for (int i = 0; i < length - count; i++)
            {
                StockOutDetail detail = stockOutDetailList[i];
                if (string.IsNullOrEmpty(detail.Product.ProductMaster.ProductMasterId)
                    && string.IsNullOrEmpty(detail.Product.ProductMaster.ProductName))
                {
                    stockOutDetailList.RemoveAt(i - count);
                    count++;
                }
            }

            if (stockOutDetailList.Count == 0)
            {
                MessageBox.Show("Không có sản phẩm nào để nhập kho!!!!");
                return;
            }

            // validate quantity
            int line = 1;
            long StockDefectId = ((StockDefectStatus) cbbStockOutType.SelectedItem).DefectStatusId;
            foreach (StockOutDetail detail in stockOutDetailList)
            {
                foreach (Stock stock in stockList)
                {
                    if (detail.Product.ProductId.Equals(stock.Product.ProductId))
                    {
                        if(StockDefectId == 9)
                        {
                            if (detail.GoodQuantity < 0)
                            {
                                MessageBox.Show("Lỗi ở dòng " + line + " : Số lượng Tốt phải là số dương ");
                                dgvDeptStockOut.CurrentCell = dgvDeptStockOut[3, Math.Max(0,line-1)];
                                return;
                            }
                        }
                        else
                        {
                            if (detail.GoodQuantity < 0 || detail.GoodQuantity > stock.GoodQuantity)
                            {
                                MessageBox.Show("Lỗi ở dòng " + line + " : Số lượng Tốt phải là số dương nhỏ hơn hoặc bằng " + stock.GoodQuantity);
                                dgvDeptStockOut.CurrentCell = dgvDeptStockOut[3, Math.Max(0, line - 1)];
                                return;
                            }
                        }
                        if (detail.LostQuantity < 0 || detail.LostQuantity > stock.LostQuantity)
                        {
                            MessageBox.Show("Lỗi ở dòng " + line + " : Số lượng Mất phải là số dương nhỏ hơn hoặc bằng " + stock.LostQuantity);
                            return;
                        }
                        if (detail.DamageQuantity < 0 || detail.DamageQuantity > stock.DamageQuantity)
                        {
                            MessageBox.Show("Lỗi ở dòng " + line + " : Số lượng Lỗi phải là số dương nhỏ hơn hoặc bằng " + stock.DamageQuantity);
                            return;
                        }
                        if (detail.ErrorQuantity < 0 || detail.ErrorQuantity > stock.ErrorQuantity)
                        {
                            MessageBox.Show("Lỗi ở dòng " + line + " : Số lượng Hư phải là số dương nhỏ hơn hoặc bằng " + stock.ErrorQuantity);
                            return;
                        }
                    }
                }

                line++;
            }

            if (stockOut == null)
            {
                stockOut = new StockOut();
            }
            bool isNeedClearData = stockOut.StockoutId == 0;
            stockOut.StockOutDate = dtpImportDate.Value;
            stockOut.StockOutDetails = stockOutDetailList;
            stockOut.DefectStatus = (StockDefectStatus) cbbStockOutType.SelectedItem;
            if(stockOut.DefectStatus.DefectStatusId == 9)
            {
                stockOut.NotUpdateMainStock = true;
            }
            else
            {
                stockOut.NotUpdateMainStock = false;
            }
            //            stockOut.Description = txtDexcription.Text;
            var eventArgs = new MainStockOutEventArgs();
            eventArgs.StockOut = stockOut;
            EventUtility.fireEvent(SaveStockOutEvent, this, eventArgs);
            if (eventArgs.EventResult != null)
            {
                MessageBox.Show("Lưu thành công");

                if (isNeedClearData)
                {
                    stockOut = new StockOut();
                    stockOutDetailList.Clear();
            //                    txtDexcription.Text = "";
            //                    txtPriceIn.Text = "";
            //                    txtPriceOut.Text = "";
                    txtSumProduct.Text = "";
                    txtSumValue.Text = "";
                    ClearSelectionOnListBox(lstColor);
                    ClearSelectionOnListBox(lstSize);
                    //CreateNewStockInDetail();
                }
            }
            else
            {
                //MessageBox.Show("Có lỗi khi lưu");
            }
        }
        private void PopulateGridByProductMaster(IList colorList, IList sizeList)
        {
            IList selectedProductMasterList = new ArrayList();
            foreach (ProductColor color in colorList)
            {
                foreach (ProductSize size in sizeList)
                {
                    foreach (ProductMaster productMaster in productMasterList)
                    {
                        // do not allow duplicate
                        bool goOut = false;
                        foreach (ProductMaster detail in selectedProductMasterList)
                        {
                            if (productMaster.ProductMasterId.Equals(detail.ProductMasterId))
                            {
                                goOut = true;
                                break;
                            }
                        }
                        if (goOut)
                        {
                            continue;
                        }

                        if (productMaster.ProductColor != null
                            && productMaster.ProductColor.ColorId == color.ColorId
                            && productMaster.ProductSize != null
                            && productMaster.ProductSize.SizeId == size.SizeId)
                        {
                            selectedProductMasterList.Add(productMaster);
                        }
                    }
                }
            }

            var eventArgs = new MainStockOutEventArgs();
            eventArgs.DefectStatusId = ((StockDefectStatus)cbbStockOutType.SelectedItem).DefectStatusId;
            eventArgs.SelectedProductMasterList = selectedProductMasterList;
            EventUtility.fireEvent(LoadStockStatusEvent, this, eventArgs);
            if (eventArgs.FoundStockOutDetailList != null && eventArgs.FoundStockOutDetailList.Count > 0)
            {
                foreach (StockOutDetail detail in eventArgs.FoundStockOutDetailList)
                {
                    bool found = false;
                    foreach (StockOutDetail detail1 in stockOutDetailList)
                    {
                        if (detail.Product.ProductId.Equals(detail1.Product.ProductId))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
            //                        MessageBox.Show("Mã vạch đã được nhập");
            //                        return;
                        continue;
                    }
                    // DefectStatusId = 4, DefectStatusName = "Xuất tạm để sửa hàng" });
                    // DefectStatusId = 5, DefectStatusName = "Xuất trả về nhà sản xuất" });
                    // DefectStatusId = 7, DefectStatusName = "Xuất đi cửa hàng khác ngoài hệ thống" });
                    StockDefectStatus defectStatus = (StockDefectStatus)cbbStockOutType.SelectedItem;
                    if(defectStatus.DefectStatusId == 4 )
                    {
                        // if xuattam, so we check error quantity & good quantity ( for shoes )
                        if(detail.GoodQuantity == 0 && detail.ErrorQuantity == 0) // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }

                    if (defectStatus.DefectStatusId == 5)
                    {
                        // if travenhasanxuat, so we check good && error quantity
                        if (detail.GoodQuantity == 0 /*&& detail.ErrorQuantity == 0*/) // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }

                    if (defectStatus.DefectStatusId == 7)
                    {
                        // if xuatdicuahangkhac, so we check good quantity
                        if (detail.GoodQuantity == 0 ) // = 0 , so we don't need to show it
                        {
                            continue;
                        }
                    }
                    detail.DefectStatus = defectStatus;
                    stockOutDetailList.Add(detail);
                    stockOutDetailList.EndNew(stockOutDetailList.Count - 1);
                    LockField(stockOutDetailList.Count - 1, detail);
                }
                foreach(Stock def in eventArgs.StockList)
                {
                    bool found = false;
                    foreach (Stock detail in stockList)
                    {
                        if (def.Product.ProductId.Equals(detail.Product.ProductId))
                        {

                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        // DefectStatusId = 4, DefectStatusName = "Xuất tạm để sửa hàng" });
                        // DefectStatusId = 5, DefectStatusName = "Xuất trả về nhà sản xuất" });
                        // DefectStatusId = 7, DefectStatusName = "Xuất đi cửa hàng khác ngoài hệ thống" });

                        stockList.Add(def);
                    }
                }

            }
        }
        private void cboProductMasters_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProductMaster proMaster = cboProductMasters.SelectedItem as ProductMaster;
            if (proMaster == null)
            {
                return;
            }
            string productName = proMaster.ProductName;
            BindingSource bindingSource = (BindingSource)cboProductMasters.DataSource;

            if (string.IsNullOrEmpty(productName))
            {
                return;
            }

            var mainStockInEventArgs = new MainStockOutEventArgs();
            mainStockInEventArgs.SelectedStockOutDetail = new StockOutDetail();
            mainStockInEventArgs.SelectedStockOutDetail.Product = new Product { ProductMaster = new ProductMaster() };
            mainStockInEventArgs.SelectedStockOutDetail.Product.ProductMaster.ProductName = productName;
            EventUtility.fireEvent(LoadGoodsByNameEvent, this, mainStockInEventArgs);

            // clear the binding list
            colorBindingSource.Clear();
            sizeBindingSource.Clear();

            IList colorList = new ArrayList();
            IList sizeList = new ArrayList();
            foreach (ProductMaster productMaster in mainStockInEventArgs.FoundProductMasterList)
            {
                if (productMaster.ProductColor != null)
                {
                    bool found = false;
                    foreach (ProductColor color in colorList)
                    {
                        if (color.ColorId == productMaster.ProductColor.ColorId)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        colorList.Add(productMaster.ProductColor);
                    }
                }
                if (productMaster.ProductSize != null)
                {
                    bool found = false;
                    foreach (ProductSize size in sizeList)
                    {
                        if (size.SizeId == productMaster.ProductSize.SizeId)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        sizeList.Add(productMaster.ProductSize);
                    }
                }
            }
            colorBindingSource.DataSource = colorList;
            sizeBindingSource.DataSource = sizeList;
            productMasterList = mainStockInEventArgs.FoundProductMasterList;
        }
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtBarcode.Text) && txtBarcode.Text.Length == 12)
            {
                var eventArgs = new MainStockOutEventArgs();
                eventArgs.ProductId = txtBarcode.Text;
                eventArgs.DefectStatusId = ((StockDefectStatus)cbbStockOutType.SelectedItem).DefectStatusId;
                EventUtility.fireEvent(FindBarcodeEvent, this, eventArgs);
                if (eventArgs.EventResult == null)
                {
                    MessageBox.Show("Không tìm thấy mã vạch này");
                    return;
                }
                bool found = false;
                StockOutDetail foundStockOutDetail = null;
                foreach (StockOutDetail detail in stockOutDetailList)
                {
                    if (eventArgs.SelectedStockOutDetail.Product.ProductId.Equals(detail.Product.ProductId))
                    {
                        found = true;
                        foundStockOutDetail = detail;
                        break;
                    }
                }
                if (found)
                {
                    //MessageBox.Show("Mã vạch đã được nhập");
                    foundStockOutDetail.GoodQuantity += 1;
                }
                else
                {
                    // reset quantity to 1
                    eventArgs.SelectedStockOutDetail.GoodQuantity = 1;
                    stockOutDetailList.Add(eventArgs.SelectedStockOutDetail);
                    stockOutDetailList.EndNew(stockOutDetailList.Count - 1);
                }
                if (eventArgs.Stock != null)
                {
                    found = false;
                    foreach (Stock detail in stockList)
                    {
                        if (eventArgs.Stock.Product.ProductId.Equals(detail.Product.ProductId))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        stockList.Add(eventArgs.Stock);
                    }
                }
                bdsStockIn.ResetBindings(false);
                dgvDeptStockOut.Refresh();
                dgvDeptStockOut.Invalidate();
                cbbStockOutType.Enabled = false;
                txtBarcode.Text = "";
                LockField(stockOutDetailList.Count - 1, eventArgs.SelectedStockOutDetail);

            }
            CalculateTotalStorePrice();
        }
 private void LoadProductMasterToComboBox()
 {
     var mainStockInEventArgs = new MainStockOutEventArgs();
     /*if (dgvDeptStockOut == null || dgvDeptStockOut.CurrentRow == null)
     {
         return;
     }*/
     // selectectIndex is the firstrow
     //int selectedIndex = 0;
     //mainStockInEventArgs.SelectedIndex = selectedIndex;
     //mainStockInEventArgs.SelectedStockInDetail = stockOutDetailList[selectedIndex];
     mainStockInEventArgs.SelectedStockOutDetail = new StockOutDetail { Product = new Product { ProductMaster = new ProductMaster { ProductName = "" } } };
     mainStockInEventArgs.IsFillToComboBox = true;
     mainStockInEventArgs.ComboBoxDisplayMember = "ProductName";
     EventUtility.fireEvent<MainStockOutEventArgs>(FillProductToComboEvent, cboProductMasters, mainStockInEventArgs);
 }
 public void mainStockOutView_LoadGoodsByNameColorSizeEvent(object sender, MainStockOutEventArgs e)
 {
 }