public SyncFromDepartmentToMain GetSyncData(bool IsSubmitPeriod,DateTime lastSyncTime)
        {
            // Save the end of period
            // select 5 day nearest

            var sync = new SyncFromDepartmentToMain();

            // find unconfirm department stock
            ObjectCriteria deptTempCrit = new ObjectCriteria();
            //deptTempCrit.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);

            IList deptTempList = DepartmentStockTempDAO.FindAll(null);
            sync.DepartmentStockTempList = deptTempList;

            // find timeline
            IList departmentTimelineList = DepartmentTimelineDAO.FindAll(null);

            // find po
            IList poList = PurchaseOrderDAO.FindAll(null);

            // find po detail
            foreach (PurchaseOrder po in poList)
            {
                var criteria = new ObjectCriteria();
                criteria.AddEqCriteria("PurchaseOrderDetailPK.PurchaseOrderId", po.PurchaseOrderPK.PurchaseOrderId);
                po.PurchaseOrderDetails = PurchaseOrderDetailDAO.FindAll(criteria);
            }

            // find stock history
            ObjectCriteria incrementalCrit = new ObjectCriteria();
            incrementalCrit.AddGreaterOrEqualsCriteria("CreateDate", lastSyncTime);
            IList stockHistoryList = DepartmentStockHistoryDAO.FindAll(incrementalCrit);

            // find dept stock out
            IList deptStockOutList = DepartmentStockOutDAO.FindAll(incrementalCrit);

            // find dept stock out detail
            foreach (DepartmentStockOut so in deptStockOutList)
            {
                var criteria = new ObjectCriteria();
                criteria.AddEqCriteria("DepartmentStockOut.DepartmentStockOutPK.StockOutId", so.DepartmentStockOutPK.StockOutId);
                so.DepartmentStockOutDetails = DepartmentStockOutDetailDAO.FindAll(criteria);
            }

            // find dept stock
            IList deptStock = DepartmentStockDAO.FindAll(null);

            // find return PO
            IList returnPoList = ReturnPoDAO.FindAll(null);

            // dept restock in
            /*var crit = new ObjectCriteria();
            crit.AddEqCriteria("StockInType", (long) 1);*/
            //IList deptReStockInList = DepartmentStockInDAO.FindAll(crit);
            IList deptStockInList = DepartmentStockInDAO.FindAll(incrementalCrit);
            foreach (DepartmentStockIn si in deptStockInList)
            {
                var criteria = new ObjectCriteria();
                criteria.AddEqCriteria("DepartmentStockIn.DepartmentStockInPK.StockInId", si.DepartmentStockInPK.StockInId);
                si.DepartmentStockInDetails = DepartmentStockInDetailDAO.FindAll(criteria);
            }

            sync.DepartmentStockOutList = deptStockOutList;
            sync.DepartmentStockHistoryList = stockHistoryList;
            sync.DepartmentStockList = deptStock;
            sync.PurchaseOrderList = poList;
            sync.ReturnPoList = returnPoList;
            sync.DepartmentStockInList = deptStockInList;
            sync.DepartmentTimelineList = departmentTimelineList;

            // get money info
            sync.EmployeeMoneyList = EmployeeMoneyDAO.FindAll(null);
            sync.DepartmentCostList = DepartmentCostDAO.FindAll(null);
            // end period confirmation
            //DepartmentTimeline timeline = new DepartmentTimeline();

            return sync;
        }
        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);
                    }
                }
            }
        }
        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.DepartemntStockOutList != null && sync.DepartemntStockOutList.Count > 0)
            {
                foreach (DepartmentStockOut po in sync.DepartemntStockOutList)
                {
                    IList productIds = new ArrayList();
                    IList stockList = null;
                    if (po.DefectStatus.DefectStatusId == StockDefectStatus.SEND_BACK_TO_MAIN)
                    {
                        foreach (DepartmentStockOutDetail detail in po.DepartmentStockOutDetails)
                        {
                            productIds.Add(detail.Product.ProductId);
                        }
                        ObjectCriteria criteria = new ObjectCriteria();
                        criteria.AddSearchInCriteria("Product.ProductId", productIds);
                        stockList = StockDAO.FindAll(criteria);
                    }
                    DepartmentStockOut poTemp = DepartmentStockOutDAO.FindById(po.DepartmentStockOutPK);
                    if (poTemp == null)
                    {
                        po.StockOutDate = DateTime.Now;
                        DepartmentStockOutDAO.Add(po);

                        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);
                                }
                            }
                        }
                    }
                    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)
                    {
                        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);
                        }
                    }
                /*}*/
            }
        }