public void AddStockInBack(DepartmentStockIn data)
        {
            long deptId = data.DepartmentStockInPK.DepartmentId;
            string deptStr = "000";
            if (deptId > 9999)
            {
                deptStr = deptId.ToString();
            }
            else
            {
                deptStr = string.Format("{0:000}", data.DepartmentStockInPK.DepartmentId);
            }
            string dateStr = data.StockInDate.ToString("yyMMdd");
            var criteria = new ObjectCriteria();
            string extraZero = "00000";
            if(deptId > 9999)
            {
                extraZero = "000";
            }
            criteria.AddGreaterCriteria("DepartmentStockInPK.StockInId", dateStr + deptStr + extraZero);
            var maxId = DepartmentStockInDAO.SelectSpecificType(criteria,
                                                                Projections.Max("DepartmentStockInPK.StockInId"));
            string startNum = "00001";
            if(deptId > 9999)
            {
                startNum = "001";
            }
            var stockInId = maxId == null
                                ? dateStr + deptStr + startNum
                                : string.Format("{0:00000000000000}", (Int64.Parse(maxId.ToString()) + 1));

            var stockInPk = new DepartmentStockInPK {DepartmentId = deptId, StockInId = stockInId + ""};

            data.DepartmentStockInPK = stockInPk;
            data.CreateDate = DateTime.Now;
            data.UpdateDate = DateTime.Now;
            data.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
            data.CreateId = ClientInfo.getInstance().LoggedUser.Name;
            DepartmentStockInDAO.Add(data);

            // we will get the stock to get the data
            IList productMasterIds = new ArrayList();
            IList productIds = new ArrayList();
            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                productIds.Add(stockInDetail.Product.ProductId);
            }

            criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddSearchInCriteria("DepartmentStockPK.ProductId", productIds);
            criteria.AddOrder("Product.ProductId", true);
            IList stockList = DepartmentStockDAO.FindAll(criteria);

            IList updateStockList = new ArrayList();
            IList stockInDetailList = new ArrayList();
            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                long quantity = stockInDetail.Quantity;
                DepartmentStockPK stockPk = new DepartmentStockPK
                                                {
                                                    DepartmentId = deptId,
                                                    ProductId = stockInDetail.Product.ProductId
                                                };
                DepartmentStock departmentStock = DepartmentStockDAO.FindById(stockPk);

                if (departmentStock.DepartmentStockPK.ProductId.Equals(stockInDetail.Product.ProductId))
                {
                        departmentStock.GoodQuantity += stockInDetail.Quantity;
                        departmentStock.Quantity += stockInDetail.Quantity;
                        departmentStock.UpdateDate = DateTime.Now;
                        departmentStock.UpdateId = ClientInfo.getInstance().LoggedUser.Name;

                        DepartmentStockDAO.Update(departmentStock);

                        var pk = new DepartmentStockInDetailPK
                                     {
                                         DepartmentId = data.DepartmentStockInPK.DepartmentId,
                                         ProductId = departmentStock.Product.ProductId,
                                         StockInId = stockInId
                                     };
                        stockInDetail.DepartmentStockInDetailPK = pk;
                        DepartmentStockInDetailDAO.Add(stockInDetail);
                }
            }
        }
        public void Update(DepartmentStockIn data)
        {
            data.DepartmentId = CurrentDepartment.Get().DepartmentId;
            data.UpdateDate = DateTime.Now;
            data.UpdateId = ClientInfo.getInstance().LoggedUser.Name;

            int delFlg = 0;
            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                // add product
                Product product = stockInDetail.Product;
                if (string.IsNullOrEmpty(product.ProductId))
                {
                    // TODO product.ProductId = productId++;
                    product.CreateDate = DateTime.Now;
                    product.UpdateDate = DateTime.Now;
                    product.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    product.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    ProductDAO.Add(product);

                    // add dept stock in
                    var detailPK = new DepartmentStockInDetailPK { DepartmentId = data.DepartmentId, ProductId = product.ProductId, StockInId = data.DepartmentStockInPK.StockInId};
                    stockInDetail.DepartmentStockInDetailPK = detailPK;
                    stockInDetail.CreateDate = DateTime.Now;
                    stockInDetail.UpdateDate = DateTime.Now;
                    stockInDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    stockInDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    DepartmentStockInDetailDAO.Add(stockInDetail);

                    // dept stock
                    var stockPk = new DepartmentStockPK { DepartmentId = data.DepartmentId, ProductId = product.ProductId };
                    var departmentStock = new DepartmentStock
                    {
                        DepartmentStockPK = stockPk,
                        CreateDate = DateTime.Now,
                        UpdateDate = DateTime.Now,
                        Product = product,
                        /*Quantity = product.Quantity,
                        GoodQuantity =product.Quantity,*/
                        Quantity = stockInDetail.Quantity,
                        GoodQuantity =stockInDetail.Quantity,
                        OnStorePrice = stockInDetail.OnStorePrice
                    };
                    departmentStock.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    departmentStock.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    DepartmentStockDAO.Add(departmentStock);

                    var pricePk = new DepartmentPricePK { DepartmentId = data.DepartmentId, ProductMasterId = product.ProductMaster.ProductMasterId };

                    var price = DepartmentPriceDAO.FindById(pricePk);
                    if (price == null)
                    {
                        price = new DepartmentPrice { DepartmentPricePK = pricePk, Price = stockInDetail.OnStorePrice, UpdateDate = DateTime.Now, CreateDate = DateTime.Now };
                        price.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        price.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                        DepartmentPriceDAO.Add(price);
                    }
                }
                else
                {
                    var temProduct = ProductDAO.FindById(product.ProductId);
                    if (stockInDetail.DelFlg == 0)
                    {
                        temProduct.Quantity = product.Quantity;
                        temProduct.Price = product.Price;
                    }
                    else
                    {
                        temProduct.DelFlg = 1;
                        delFlg++;
                    }

                    temProduct.UpdateDate = DateTime.Now;
                    product.UpdateId = ClientInfo.getInstance().LoggedUser.Name;

                    ProductDAO.Update(temProduct);

                    // update dept stock in
                    var detailPK = new DepartmentStockInDetailPK { DepartmentId = data.DepartmentId, ProductId = product.ProductId, StockInId = data.DepartmentStockInPK.StockInId};
                    stockInDetail.DepartmentStockInDetailPK = detailPK;
                    stockInDetail.UpdateDate = DateTime.Now;
                    stockInDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    DepartmentStockInDetailDAO.Update(stockInDetail);

                    // update stock
                    var stockPk = new DepartmentStockPK { DepartmentId = data.DepartmentId, ProductId = product.ProductId};
                    var departmentStock = DepartmentStockDAO.FindById(stockPk);
                    departmentStock.UpdateDate = DateTime.Now;
                    if (stockInDetail.DelFlg == 0)
                    {
                        /*departmentStock.Quantity = departmentStock.Quantity -
                                                   (stockInDetail.OldQuantity - stockInDetail.Quantity);*/
                        departmentStock.GoodQuantity = departmentStock.GoodQuantity -
                                                   (stockInDetail.OldQuantity - stockInDetail.Quantity);
                        departmentStock.Quantity = departmentStock.GoodQuantity + departmentStock.ErrorQuantity +
                                                   departmentStock.LostQuantity + departmentStock.DamageQuantity +
                                                   departmentStock.UnconfirmQuantity;
                    }
                    else
                    {
                        departmentStock.DelFlg = 1;
                    }
                    departmentStock.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                    departmentStock.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                    DepartmentStockDAO.Update(departmentStock);

                    var pricePk = new DepartmentPricePK { DepartmentId = data.DepartmentId, ProductMasterId = product.ProductMaster.ProductMasterId };

                    var price = DepartmentPriceDAO.FindById(pricePk);
                    if (price == null)
                    {
                        price = new DepartmentPrice { DepartmentPricePK = pricePk, Price = stockInDetail.OnStorePrice, UpdateDate = DateTime.Now, CreateDate = DateTime.Now };
                        price.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        price.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                        DepartmentPriceDAO.Add(price);
                    }
                }
            }

            if (delFlg == data.DepartmentStockInDetails.Count)
            {
                data.DelFlg = 1;
            }
            DepartmentStockInDAO.Update(data);
        }
        public void AddReStock(DepartmentStockIn data)
        {
            string deptStr = string.Format("{0:000}", data.DepartmentId);
            string dateStr = data.StockInDate.ToString("yyMMdd");
            var criteria = new ObjectCriteria();
            criteria.AddGreaterCriteria("DepartmentStockInPK.StockInId", dateStr + deptStr + "00000");
            var maxId = DepartmentStockInDAO.SelectSpecificType(criteria, Projections.Max("DepartmentStockInPK.StockInId"));
            var stockInId = maxId == null ? dateStr + deptStr + "00001" : string.Format("{0:00000000000000}", (Int64.Parse(maxId.ToString()) + 1));

            var stockInPk = new DepartmentStockInPK { DepartmentId = data.DepartmentId, StockInId = stockInId + "" };

            data.CreateDate = DateTime.Now;
            data.UpdateDate = DateTime.Now;
            data.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
            data.CreateId = ClientInfo.getInstance().LoggedUser.Name;
            data.StockInType = (long)1;
            data.DepartmentStockInPK = stockInPk;
            DepartmentStockInDAO.Add(data);

            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                // add dept stock in
                var detailPK = new DepartmentStockInDetailPK
                { ProductId = stockInDetail.Product.ProductId,
                  StockInId = stockInId,
                  DepartmentId = data.DepartmentId};

                stockInDetail.DepartmentStockInDetailPK = detailPK;
                stockInDetail.CreateDate = DateTime.Now;
                stockInDetail.UpdateDate = DateTime.Now;
                stockInDetail.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                stockInDetail.CreateId = ClientInfo.getInstance().LoggedUser.Name;
                stockInDetail.ProductMaster = stockInDetail.Product.ProductMaster;

                //                    stockInDetail.CurrentStockQuantity = (sum == null) ? 0 : Int64.Parse(sum.ToString());
                DepartmentStockInDetailDAO.Add(stockInDetail);
                ObjectCriteria stockCriteria = new ObjectCriteria();
                stockCriteria.AddEqCriteria("DepartmentStockPK.ProductId", stockInDetail.Product.ProductId);
                stockCriteria.AddEqCriteria("DepartmentStockPK.DepartmentId", data.DepartmentId);
                IList stockList = DepartmentStockDAO.FindAll(stockCriteria);

                // decrease error and increase good
                if (stockList != null)
                {
                    DepartmentStock stock = (DepartmentStock)stockList[0];
                    stock.ErrorQuantity -= stockInDetail.Quantity;
                    stock.GoodQuantity += stockInDetail.Quantity;
                    stock.Quantity = stock.ErrorQuantity + stock.GoodQuantity + stock.DamageQuantity +
                                     stock.UnconfirmQuantity + stock.LostQuantity;
                    DepartmentStockDAO.Update(stock);
                }
            }
        }
        public DepartmentStockIn Add(DepartmentStockIn data)
        {
            string deptStr = "";
            if(ClientSetting.IsSubStock())
            {
                deptStr = string.Format("{0:00000}", data.DepartmentStockInPK.DepartmentId);
            }
            else
            {
                deptStr = string.Format("{0:000}", data.DepartmentStockInPK.DepartmentId);
            }

            string dateStr = data.StockInDate.ToString("yyMMdd");
            var criteria = new ObjectCriteria();
            if(ClientSetting.IsSubStock())
            {
                criteria.AddGreaterCriteria("DepartmentStockInPK.StockInId", dateStr + deptStr + "000");
            }
            else
            {
                criteria.AddGreaterCriteria("DepartmentStockInPK.StockInId", dateStr + deptStr + "00000");
            }

            var maxId = DepartmentStockInDAO.SelectSpecificType(criteria, Projections.Max("DepartmentStockInPK.StockInId"));
            string stockInId = "";
            if(ClientSetting.IsSubStock())
            {
                stockInId = maxId == null ? dateStr + deptStr + "001" :   string.Format("{0:00000000000000}", (Int64.Parse(maxId.ToString()) + 1));
            }
            else
            {
                stockInId = maxId == null ? dateStr + deptStr + "00001" : string.Format("{0:00000000000000}", (Int64.Parse(maxId.ToString()) + 1));
            }

            var stockInPk = new DepartmentStockInPK {DepartmentId = data.DepartmentId, StockInId = stockInId + ""};

            data.DepartmentStockInPK = stockInPk;
            data.CreateDate = DateTime.Now;
            data.UpdateDate = DateTime.Now;
            data.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
            data.CreateId = ClientInfo.getInstance().LoggedUser.Name;
            DepartmentStockInDAO.Add(data);

            // we will get the stock to get the data
            IList productMasterIds = new ArrayList();
            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                productMasterIds.Add(stockInDetail.Product.ProductMaster.ProductMasterId);
            }

            criteria = new ObjectCriteria();
            criteria.AddEqCriteria("DelFlg", CommonConstants.DEL_FLG_NO);
            criteria.AddGreaterCriteria("Quantity", (long)0);
            criteria.AddSearchInCriteria("ProductMaster.ProductMasterId", productMasterIds);
            criteria.AddOrder("ProductMaster.ProductMasterId", true);
            criteria.AddOrder("Product.ProductId", true);
            IList stockList = DepartmentStockDAO.FindAll(criteria);

            IList updateStockList = new ArrayList();
            IList stockInDetailList = new ArrayList();
            foreach (DepartmentStockInDetail stockInDetail in data.DepartmentStockInDetails)
            {
                long quantity = stockInDetail.Quantity;
                foreach (DepartmentStock stock in stockList)
                {
                    long stockInQty = 0;
                   if (stock.Product.ProductId.Equals(stockInDetail.ProductId) && stock.Quantity >= 0)
                    {
                        /*if (quantity >= stock.Quantity)
                        {
                            stockInQty = stock.Quantity;
                            quantity -= stock.Quantity;
                            stock.Quantity = 0;
                        }
                        else
                        {
                            stockInQty = quantity;
                            stock.Quantity -= quantity;
                            quantity = 0;
                        }*/
                        if (quantity >= stock.GoodQuantity)
                        {
                            stockInQty = stock.GoodQuantity;
                            quantity -= stock.GoodQuantity;
                            stock.GoodQuantity = 0;
                        }
                        else
                        {
                            stockInQty = quantity;
                            stock.GoodQuantity -= quantity;
                            quantity = 0;
                        }
                        stock.Quantity = stock.GoodQuantity + stock.ErrorQuantity + stock.DamageQuantity +
                                         stock.LostQuantity + stock.UnconfirmQuantity;
                        stock.UpdateDate = DateTime.Now;
                        stock.UpdateId = ClientInfo.getInstance().LoggedUser.Name;
                        updateStockList.Add(stock);
                        var pk = new DepartmentStockInDetailPK
                                   {
                                       DepartmentId = data.DepartmentId,
                                       ProductId = stock.Product.ProductId,
                                       StockInId = stockInId
                                   };
                        var detail = new DepartmentStockInDetail
                        {
                            DepartmentStockInDetailPK = pk,
                            Quantity = stockInQty,
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now,
                            UpdateId = ClientInfo.getInstance().LoggedUser.Name,
                            CreateId = ClientInfo.getInstance().LoggedUser.Name,
                            ProductMaster = stock.ProductMaster
                        };
                        var deptStock = new DepartmentStock
                                            {
                                                DepartmentStockPK = new DepartmentStockPK
                                                                        {
                                                                            DepartmentId = data.DepartmentId,
                                                                            ProductId = stock.Product.ProductId
                                                                        },
                                                CreateDate = DateTime.Now,
                                                UpdateDate = DateTime.Now,
                                                UpdateId = ClientInfo.getInstance().LoggedUser.Name,
                                                CreateId = ClientInfo.getInstance().LoggedUser.Name,
                                                ProductMaster = stock.ProductMaster
                                            };
                        stockInDetailList.Add(detail);
                        if (quantity == 0)
                        {
                            break;
                        }
                    }
                }
                if (quantity > 0)
                {
                    data.DepartmentStockInPK.StockInId = null;
                    throw new BusinessException("Số lượng xuất kho lớn hơn số lượng trong kho");
                }
            }

            foreach (DepartmentStockInDetail detail in stockInDetailList)
            {
                DepartmentStockInDetailDAO.Add(detail);
            }

            foreach (Stock stock in updateStockList)
            {
                StockDAO.Update(stock);
            }
            return data;
        }