void departmentStockOutReportView_ConfirmStockOutEvent(object sender, ReportStockOutEventArgs e)
        {
            IList list = e.ConfirmDepartmentStockOutList;

            StockOutMapper mapper = new StockOutMapper();
            StockOutDetailMapper detailMapper = new StockOutDetailMapper();
            DeptRetProdStockInMapper drpsiMapper = new DeptRetProdStockInMapper();
            DeptRetProdStockInDetailMapper drpsiDetMapper = new DeptRetProdStockInDetailMapper();
            foreach (DepartmentStockOut departmentStockOut in list)
            {

                StockOut stockOut = mapper.Convert(departmentStockOut);
                StockIn stockIn = drpsiMapper.Convert(departmentStockOut);
                stockIn.StockInDate = DateTime.Now;
                stockIn.StockInType = 3; // stock in for stock out to manufacturers
                stockIn.StockInDetails = new ArrayList();
                stockOut.NotUpdateMainStock = true;
                stockOut.StockOutDate = DateTime.Now;

                IList detlist = new ArrayList();
                foreach (DepartmentStockOutDetail detail in departmentStockOut.DepartmentStockOutDetails)
                {
                    StockInDetail detailStockIn = drpsiDetMapper.Convert(detail);
                    stockIn.StockInDetails.Add(detailStockIn);
                    StockOutDetail detailStockOut = detailMapper.Convert(detail);
                    detlist.Add(detailStockOut);
                }
                stockOut.StockOutDetails = detlist;
                StockInLogic.AddForStockOutToProducer(stockIn);
                StockOutLogic.Add(stockOut);
                departmentStockOut.ConfirmFlg = 2;
                departmentStockOut.StockOutDate = DateTime.Now;
                DepartmentStockOutLogic.Update(departmentStockOut);
            }
            MessageBox.Show(" Lưu thành công !");
            e.HasErrors = false;
        }
        public void SyncToMain(SyncFromDepartmentToMain sync)
        {
            // insert department stock temp
            if(sync.DepartmentStockTempList!=null && sync.DepartmentStockTempList.Count > 0)
            {
                foreach (DepartmentStockTemp departmentStockTemp in sync.DepartmentStockTempList)
                {
                    bool failed = false;
                    if (departmentStockTemp != null)
                    {
                        DepartmentStockTemp curDeptTemp = DepartmentStockTempDAO.FindById(departmentStockTemp.DepartmentStockTempPK);
                        if (curDeptTemp == null)
                        {
                            DepartmentStockTempDAO.Add(departmentStockTemp);
                        }
                        else
                        {
                            if(departmentStockTemp.Fixed == 1 && departmentStockTemp.DelFlg == 1)
                            {
                                curDeptTemp.Fixed = 1;
                                curDeptTemp.DelFlg = 1;
                                DepartmentStockTempDAO.Update(curDeptTemp);
                            }
                        }
                    }
                }
            }
            // insert timeline
            if (sync.DepartmentTimelineList != null && sync.DepartmentTimelineList.Count > 0)
            {
                foreach (DepartmentTimeline timeline in sync.DepartmentTimelineList)
                {
                    bool failed = false;
                    if (timeline != null)
                    {
                        DepartmentTimeline curTimeline = DepartmentTimelineDAO.FindById(timeline.DepartmentTimelinePK);
                        if (curTimeline == null)
                        {
                            DepartmentTimelineDAO.Add(timeline);
                        }
                        else
                        {
                            // error
                            //failed = true;
                        }
                    }
                }
            }

            // insert PO, PO Detail
            if (sync.PurchaseOrderList != null && sync.PurchaseOrderList.Count > 0)
            {
                foreach (PurchaseOrder po in sync.PurchaseOrderList)
                {
                    PurchaseOrder poTemp = PurchaseOrderDAO.FindById(po.PurchaseOrderPK);
                    if (poTemp == null)
                    {
                        PurchaseOrderDAO.Add(po);
                        foreach (PurchaseOrderDetail detail in po.PurchaseOrderDetails)
                        {
                            PurchaseOrderDetailDAO.Add(detail);
                        }
                    }
                    else
                    {
                        // Error
                        //failed = true;
                    }
                }
            }

            // insert dept stock out
            if (sync.DepartmentStockOutList != null && sync.DepartmentStockOutList.Count > 0)
            {
                IList productIds = new ArrayList();
                foreach (DepartmentStockOut po in sync.DepartmentStockOutList)
                {
                    if (po.DefectStatus.DefectStatusId == StockDefectStatus.SEND_BACK_TO_MAIN)
                    {
                        foreach (DepartmentStockOutDetail detail in po.DepartmentStockOutDetails)
                        {
                            if (NotInList(productIds, detail.Product.ProductId))
                            {
                                productIds.Add(detail.Product.ProductId);
                            }
                        }
                    }
                }
                IList stockList = new ArrayList();
                ObjectCriteria criteria = new ObjectCriteria();
                criteria.AddSearchInCriteria("Product.ProductId", productIds);
                stockList = StockDAO.FindAll(criteria);

                StockOutMapper mapper = new StockOutMapper();
                StockOutDetailMapper detailMapper = new StockOutDetailMapper();
                DeptRetProdStockInMapper drpsiMapper = new DeptRetProdStockInMapper();
                DeptRetProdStockInDetailMapper drpsiDetMapper = new DeptRetProdStockInDetailMapper();

                // save to main.
                string dateStr = DateTime.Now.ToString("yyMMdd");
                var SICriteria = new ObjectCriteria();
                var maxId = StockInDAO.SelectSpecificType(SICriteria, Projections.Max("StockInId"));
                var stockInId = maxId == null ? dateStr + "00001" : string.Format("{0:00000000000}", (Int64.Parse(maxId.ToString()) + 1));

                foreach (DepartmentStockOut po in sync.DepartmentStockOutList)
                {

                    /*if (po.DefectStatus.DefectStatusId == StockDefectStatus.SEND_BACK_TO_MAIN)
                    {
                        foreach (DepartmentStockOutDetail detail in po.DepartmentStockOutDetails)
                        {
                            productIds.Add(detail.Product.ProductId);
                        }
                    }*/
                    DepartmentStockOut poTemp = DepartmentStockOutDAO.FindById(po.DepartmentStockOutPK);
                    if (poTemp == null)
                    {
                        // ++ Add code for add an empty stock in : 20090906

                        po.StockOutDate = DateTime.Now;
                        DepartmentStockOutDAO.Add(po);

                        StockIn stockIn = drpsiMapper.Convert(po);
                        stockIn.StockInDate = DateTime.Now;
                        if (po.DefectStatus.DefectStatusId == StockDefectStatus.SEND_BACK_TO_MAIN)
                        {
                            stockIn.StockInType = 3; // stock in for stock out to manufacturers
                        }
                        stockIn.StockInDetails = new ArrayList();

                        foreach (DepartmentStockOutDetail detail in po.DepartmentStockOutDetails)
                        {
                            DepartmentStockOutDetailDAO.Add(detail);

                            // update stock if it's a dept stock out return to main ( defectstatus = 6 )
                            if (po.DefectStatus.DefectStatusId == StockDefectStatus.SEND_BACK_TO_MAIN
                                && stockList!=null
                                && stockList.Count > 0)
                            {
                                Stock stock = GetStockOfProduct(detail.Product,stockList);
                                if(stock!=null)
                                {
                                    stock.Quantity += detail.GoodQuantity + detail.ErrorQuantity;
                                    stock.GoodQuantity += detail.GoodQuantity;
                                    stock.ErrorQuantity += detail.ErrorQuantity;
                                    StockDAO.Update(stock);
                                }

                                StockInDetail detailStockIn = drpsiDetMapper.Convert(detail);
                                stockIn.StockInDetails.Add(detailStockIn);
                            }
                        }
                        if(stockIn.StockInDetails != null && stockIn.StockInDetails.Count > 0)
                        {

                            // save to main.
                            stockIn.StockInId = stockInId;
                            StockInDAO.Add(stockIn);

                            foreach (StockInDetail stockInDetail in stockIn.StockInDetails)
                            {
                                // add dept stock in
                                var detailPK = new StockInDetailPK { ProductId = stockInDetail.Product.ProductId, StockInId = stockInId };
                                stockInDetail.StockInDetailPK = detailPK;
                                StockInDetailDAO.Add(stockInDetail);
                            }
                            // create new stockInId
                            stockInId = string.Format("{0:00000000000}", (Int64.Parse(stockInId.ToString()) + 1));
                        }
                        // -- Add code for add an empty stock in : 20090906
                    }
                    else
                    {
                        // Error
                        //failed = true;
                    }
                }
            }

            // insert Dept stock in
            if (sync.DepartmentStockInList != null && sync.DepartmentStockInList.Count > 0)
            {
                foreach (DepartmentStockIn po in sync.DepartmentStockInList)
                {
                    DepartmentStockIn poTemp = DepartmentStockInDAO.FindById(po.DepartmentStockInPK);
                    if (poTemp == null)
                    {
                        DepartmentStockInDAO.Add(po);
                        foreach (DepartmentStockInDetail detail in po.DepartmentStockInDetails)
                        {
                            DepartmentStockInDetailDAO.Add(detail);
                        }
                    }
                    else
                    {
                        // Error
                    }
                }
            }

            // insert return po
            if (sync.ReturnPoList != null && sync.ReturnPoList.Count > 0)
            {
                foreach (ReturnPo po in sync.ReturnPoList)
                {
                    ReturnPo poTemp = ReturnPoDAO.FindById(po.ReturnPoPK);
                    if (poTemp == null)
                    {
                        ReturnPoDAO.Add(po);
                    }
                    else
                    {
                        // Error
                    }
                }
            }

            // insert stock history
            if (sync.DepartmentStockHistoryList != null && sync.DepartmentStockHistoryList.Count > 0)
            {
                foreach (DepartmentStockHistory po in sync.DepartmentStockHistoryList)
                {
                    DepartmentStockHistory poTemp = DepartmentStockHistoryDAO.FindById(po.DepartmentStockHistoryPK);
                    if (poTemp == null)
                    {
                        DepartmentStockHistoryDAO.Add(po);
                    }
                    else
                    {
                        // Error
                    }
                }
            }

            // insert or update dept stock
            if (sync.DepartmentStockList != null && sync.DepartmentStockList.Count > 0)
            {
                /*if (!failed)
                {*/
                    foreach (DepartmentStock po in sync.DepartmentStockList)
                    {

                        try
                        {
                            DepartmentStock poTemp = DepartmentStockDAO.FindById(po.DepartmentStockPK);
                            if (poTemp == null)
                            {
                                DepartmentStockDAO.Add(po);
                            }
                            else
                            {
                                poTemp.Quantity = po.Quantity;
                                poTemp.GoodQuantity = po.GoodQuantity;
                                poTemp.LostQuantity = po.LostQuantity;
                                poTemp.ErrorQuantity = po.ErrorQuantity;
                                poTemp.DamageQuantity = po.DamageQuantity;
                                poTemp.OldDamageQuantity = po.OldDamageQuantity;
                                poTemp.OldErrorQuantity = po.OldErrorQuantity;
                                poTemp.OldGoodQuantity = po.OldGoodQuantity;
                                poTemp.OldLostQuantity = po.OldLostQuantity;
                                poTemp.OldUnconfirmQuantity = po.OldUnconfirmQuantity;
                                poTemp.UnconfirmQuantity = po.UnconfirmQuantity;
                                poTemp.UpdateDate = DateTime.Now;
                                DepartmentStockDAO.Update(poTemp);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                /*}*/
            }

            // sync dept cost
            if (sync.DepartmentCostList != null && sync.DepartmentCostList.Count > 0)
            {
                foreach (DepartmentCost departmentCost in sync.DepartmentCostList)
                {
                    DepartmentCost existDeptCost = DepartmentCostDAO.FindById(departmentCost.DepartmentCostPK);
                    if (existDeptCost == null)
                    {
                        DepartmentCostDAO.Add(departmentCost);
                    }
                }
            }

            // sync money
            if(sync.EmployeeMoneyList != null && sync.EmployeeMoneyList.Count > 0)
            {
                foreach (EmployeeMoney employeeMoney in sync.EmployeeMoneyList)
                {
                    EmployeeMoney existEmpMoney = EmployeeMoneyDAO.FindById(employeeMoney.EmployeeMoneyPK);
                    if (existEmpMoney == null)
                    {
                        EmployeeMoneyDAO.Add(employeeMoney);
                    }
                }
            }
        }
        void departmentStockOutReportView_DenyStockOutEvent(object sender, ReportStockOutEventArgs e)
        {
            IList list = e.DenyDepartmentStockOutList;
            StockOutMapper mapper = new StockOutMapper();
            StockOutDetailMapper detailMapper = new StockOutDetailMapper();
            DeptRetProdStockInMapper drpsiMapper = new DeptRetProdStockInMapper();
            DeptRetProdStockInDetailMapper drpsiDetMapper = new DeptRetProdStockInDetailMapper();

            foreach (DepartmentStockOut departmentStockOut in list)
            {
                // ++ Add code for add an empty stock in : 20090906

                StockIn stockIn = drpsiMapper.Convert(departmentStockOut);
                stockIn.StockInDate = DateTime.Now;
                stockIn.StockInType = 0; // stock in for stock out to manufacturers
                stockIn.StockInDetails = new ArrayList();
                stockIn.NotUpdateMainStock = true;
                foreach (DepartmentStockOutDetail detail in departmentStockOut.DepartmentStockOutDetails)
                {
                    ObjectCriteria criteria = new ObjectCriteria();
                    criteria.AddEqCriteria("Product.ProductId", detail.Product.ProductId);
                    IList productList = StockLogic.FindAll(criteria);
                    if (productList != null)
                    {
                        Stock currStock = (Stock)productList[0];
                        currStock.GoodQuantity += detail.GoodQuantity;
                        currStock.Quantity += detail.GoodQuantity;
                        currStock.ErrorQuantity += detail.ErrorQuantity;
                        currStock.Quantity += detail.ErrorQuantity;
                        currStock.LostQuantity += detail.LostQuantity;
                        currStock.LostQuantity += detail.LostQuantity;
                        currStock.DamageQuantity += detail.DamageQuantity;
                        currStock.DamageQuantity += detail.DamageQuantity;
                        StockLogic.Update(currStock);

                        StockInDetail detailStockIn = drpsiDetMapper.Convert(detail);
                        stockIn.StockInDetails.Add(detailStockIn);

                    }
                    else // error
                    {
                        MessageBox.Show(" Không có mã vạch trong kho, đề nghị kiểm tra lại");
                        e.HasErrors = true;
                        return;
                    }

                }

                departmentStockOut.ConfirmFlg = 2;
                DepartmentStockOutLogic.Update(departmentStockOut);

                if(stockIn.StockInDetails.Count > 0 )
                {
                    StockInLogic.AddForStockOutToProducer(stockIn);
                }
                // -- Add code for add an empty stock in : 20090906
            }
            MessageBox.Show(" Lưu thành công !");
            e.HasErrors = false;
        }