public bool StoreTransfer(tbl_TransferOrder order, List <tbl_TransferOrderDetail> orderDetails, ref string msg)
        {
            using (var db = _connectionData.OpenDbConnection())
            {
                using (var ts = db.OpenTransaction())
                {
                    try
                    {
                        order.Id = Convert.ToInt32(db.Insert(order, true));
                        foreach (var item in orderDetails)
                        {
                            item.TransferID = order.Id;
                        }
                        db.InsertAll(orderDetails);
                        //Cập nhật tồn kho:
                        //Group theo StoreId
                        var srcStoreId = orderDetails.FirstOrDefault().FromStoreId;
                        var storeId    = orderDetails.FirstOrDefault().StoreID;
                        //  foreach (var storeId in storeIds)

                        var orderByStore = orderDetails.Where(p => p.StoreID == storeId);
                        //Group theo sản phẩm
                        var productGroup = orderByStore.GroupBy(p => p.itemid, (key, g) => new
                        {
                            ProductId = key,
                            Total     = g.Sum(p => p.quantity)
                        });
                        foreach (var item in productGroup)
                        {
                            //trừ tồn kho Kho gốc
                            var srcStoreProduct = db.Select <tbl_StoreProduct>(
                                p => p.productid == item.ProductId && p.storeid == srcStoreId).FirstOrDefault();

                            var product =
                                db.Select <tbl_Product>(
                                    p => p.SysHotelID == CommService.GetHotelId() && p.Id == item.ProductId)
                                .FirstOrDefault();

                            if (srcStoreProduct == null || srcStoreProduct.quantity < item.Total)
                            {
                                //  var product =
                                msg = "Tạo phiếu lỗi. Sản phẩm " + product.Name + " không đủ số lượng trong kho nguồn";
                                ts.Rollback();
                                return(false);
                            }
                            srcStoreProduct.quantity = srcStoreProduct.quantity - item.Total;
                            db.Update(srcStoreProduct);
                            var existItem =
                                db.Select <tbl_StoreProduct>(
                                    p => p.productid == item.ProductId && p.storeid == storeId).FirstOrDefault();
                            if (existItem == null)
                            {
                                existItem = new tbl_StoreProduct
                                {
                                    quantity    = item.Total,
                                    SysHotelID  = CommService.GetHotelId(),
                                    datecreated = DateTime.Now,
                                    productid   = item.ProductId,
                                    unitid      = product.UnitID,
                                    storeid     = storeId
                                };
                                var id = db.Insert(existItem, true);
                                //Thêm bản ghi Log
                                var log = new tbl_StoreProductLog
                                {
                                    typeImportExport = 3,
                                    SysHotelID       = CommService.GetHotelId(),
                                    datecreated      = DateTime.Now,
                                    storeid          = storeId,
                                    productid        = item.ProductId,
                                    storeproductid   = Convert.ToInt32(id),
                                    unitid           = product.UnitID,
                                    quantity         = item.Total,
                                    fromstoreid      = srcStoreId
                                };
                                db.Insert(log);
                            }
                            else
                            {
                                var log = new tbl_StoreProductLog
                                {
                                    typeImportExport = 3,
                                    SysHotelID       = CommService.GetHotelId(),
                                    datecreated      = DateTime.Now,
                                    storeid          = storeId,
                                    productid        = item.ProductId,
                                    storeproductid   = existItem.Id,
                                    unitid           = product.UnitID,
                                    quantity         = item.Total,
                                    fromstoreid      = srcStoreId
                                };
                                db.Insert(log);
                                existItem.quantity = existItem.quantity + item.Total;
                                db.Update(existItem);
                            }
                        }


                        ts.Commit();
                        return(true);
                    }
                    catch (Exception)
                    {
                        ts.Rollback();
                        return(false);
                    }
                }
            }
        }
        public bool StoreTransfer(tbl_TransferOrder order, List <tbl_TransferOrderDetail> orderDetails)
        {
            using (var db = _connectionData.OpenDbConnection())
            {
                using (var ts = db.OpenTransaction())
                {
                    try
                    {
                        order.Id = Convert.ToInt32(db.Insert(order, true));
                        foreach (var item in orderDetails)
                        {
                            item.TransferID = order.Id;
                        }
                        var srcStoreId = orderDetails.FirstOrDefault().FromStoreId;
                        foreach (var item in orderDetails)
                        {
                            var product =
                                db.Select <tbl_Product>(
                                    p => p.SysHotelID == CommService.GetHotelId() && p.Id == item.itemid)
                                .FirstOrDefault();
                            var srcStoreProduct = db.Select <tbl_StoreProduct>(
                                p => p.productid == item.itemid && p.storeid == srcStoreId).FirstOrDefault();
                            if (srcStoreProduct == null || srcStoreProduct.quantity == 0)
                            {
                                continue;
                            }

                            item.quantity = srcStoreProduct.quantity > item.quantity
                                ? item.quantity
                                : srcStoreProduct.quantity;

                            db.Insert(item);
                            srcStoreProduct.quantity = srcStoreProduct.quantity - item.quantity;
                            db.Update(srcStoreProduct);
                            var existItem =
                                db.Select <tbl_StoreProduct>(
                                    p => p.productid == item.itemid && p.storeid == item.StoreID).FirstOrDefault();
                            if (existItem == null)
                            {
                                existItem = new tbl_StoreProduct
                                {
                                    quantity    = item.quantity,
                                    SysHotelID  = CommService.GetHotelId(),
                                    datecreated = DateTime.Now,
                                    productid   = item.itemid,
                                    unitid      = product.UnitID,
                                    storeid     = item.StoreID
                                };
                                var id = db.Insert(existItem, true);
                                //Thêm bản ghi Log
                                var log = new tbl_StoreProductLog
                                {
                                    typeImportExport = 3,
                                    SysHotelID       = CommService.GetHotelId(),
                                    datecreated      = DateTime.Now,
                                    storeid          = item.StoreID,
                                    productid        = item.itemid,
                                    storeproductid   = Convert.ToInt32(id),
                                    unitid           = product.UnitID,
                                    quantity         = item.quantity,
                                    fromstoreid      = srcStoreId
                                };
                                db.Insert(log);
                            }
                            else
                            {
                                var log = new tbl_StoreProductLog
                                {
                                    typeImportExport = 3,
                                    SysHotelID       = CommService.GetHotelId(),
                                    datecreated      = DateTime.Now,
                                    storeid          = item.StoreID,
                                    productid        = item.itemid,
                                    storeproductid   = existItem.Id,
                                    unitid           = product.UnitID,
                                    quantity         = item.quantity,
                                    fromstoreid      = srcStoreId
                                };
                                db.Insert(log);
                                existItem.quantity = existItem.quantity + item.quantity;
                                db.Update(existItem);
                            }
                        }
                        ts.Commit();
                        return(true);
                    }
                    catch (Exception)
                    {
                        ts.Rollback();
                        return(false);
                    }
                }
            }
        }
        public bool StoreInput(tbl_PurchaseOrder order, List <tbl_PurchaseOrderDetail> orderDetails)
        {
            using (var db = _connectionData.OpenDbConnection())
            {
                // using (var ts = new TransactionScope())
                var tran = db.OpenTransaction();
                {
                    try
                    {
                        order.Id = Convert.ToInt32(db.Insert(order, true));
                        foreach (var item in orderDetails)
                        {
                            item.PurchaseID = order.Id;
                        }
                        db.InsertAll(orderDetails);
                        //Cập nhật tồn kho:
                        //Group theo StoreId
                        var storeIds = orderDetails.Select(p => p.StoreID).Distinct().ToList();
                        foreach (var storeId in storeIds)
                        {
                            var orderByStore = orderDetails.Where(p => p.StoreID == storeId);
                            //Group theo sản phẩm
                            var productGroup = orderByStore.GroupBy(p => p.itemid, (key, g) => new
                            {
                                ProductId = key,
                                Total     = g.Sum(p => p.quantity)
                            });
                            foreach (var item in productGroup)
                            {
                                var product =
                                    db.Select <tbl_Product>(
                                        p => p.SysHotelID == CommService.GetHotelId() && p.Id == item.ProductId)
                                    .FirstOrDefault();

                                var existItem =
                                    db.Select <tbl_StoreProduct>(
                                        p => p.productid == item.ProductId && p.storeid == storeId).FirstOrDefault();
                                if (existItem == null)
                                {
                                    existItem = new tbl_StoreProduct
                                    {
                                        quantity    = item.Total,
                                        SysHotelID  = CommService.GetHotelId(),
                                        datecreated = DateTime.Now,
                                        productid   = item.ProductId,
                                        unitid      = product.UnitID,
                                        storeid     = storeId
                                    };
                                    var id = db.Insert(existItem, true);
                                    //Thêm bản ghi Log
                                    var log = new tbl_StoreProductLog
                                    {
                                        typeImportExport = 1,
                                        SysHotelID       = CommService.GetHotelId(),
                                        datecreated      = DateTime.Now,
                                        storeid          = storeId,
                                        productid        = item.ProductId,
                                        storeproductid   = Convert.ToInt32(id),
                                        unitid           = product.UnitID,
                                        quantity         = item.Total,
                                    };
                                    db.Insert(log);
                                }
                                else
                                {
                                    var log = new tbl_StoreProductLog
                                    {
                                        typeImportExport = 1,
                                        SysHotelID       = CommService.GetHotelId(),
                                        datecreated      = DateTime.Now,
                                        storeid          = storeId,
                                        productid        = item.ProductId,
                                        storeproductid   = existItem.Id,
                                        unitid           = product.UnitID,
                                        quantity         = item.Total,
                                    };
                                    db.Insert(log);
                                    existItem.quantity = existItem.quantity + item.Total;
                                    db.Update(existItem);
                                }
                            }
                        }
                        tran.Commit();
                        //   ts.Complete();
                        return(true);
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return(false);
                    }
                }
            }
        }