internal static void AssignValues(SaleProductItemInfo source,
                                   YuShang.ERP.Entities.Sale.SaleProductItem target)
 {
     target.Comments       = source.Comments;
     target.ShipmentStatus = source.ShipmentStatus;
     //target.Currency = source.Currency;
     target.ProductItemId  = source.ProductItemId;
     target.Quantity       = source.Quantity;
     target.SaleContractId = source.SaleContractId.GetValueOrDefault();
     target.StockItemId    = source.StockItemId;
     target.UnitPrice      = source.UnitPrice;
     target.Weight         = source.Weight;
 }
        public ProductItemViewModel GetIndexListProductItemsForShipment(IOwinContext owinContext,
                                                                        ExtendedIdentityDbContext db, ContractListCondition condition)
        {
            if (db == null || owinContext == null)
            {
                throw new ArgumentNullException("DbContext或IOwinContext对象为空。",
                                                new Exception("DbContext或IOwinContext对象为空。"));
            }

            if (condition == null)
            {
                UtilityFramework.LogHelper.Warn("获取待发货列表不可以不带条件参数。");
                condition = new ContractListCondition()
                {
                    UserName    = owinContext.Authentication.User.Identity.Name,
                    Page        = 1,
                    Rows        = 10,
                    IsEnable    = true,
                    ListInclude = ContractListInclude.SaleContractOnly,
                };
            }

            int userEntityPriv = PrivilegeManager.Instance.GetEntityPrivilegeLevel(condition.UserName);
            DbQuery <SaleProductItem> dbQuery = db.SaleProductItems;

            if (condition != null && condition.OrderType.HasValue == false)//期货 and 现货
            {
                dbQuery = dbQuery.Include("SaleContract.StockItem.ProductItem.Product");
                dbQuery = dbQuery.Include("SaleContract.ProductItem.Product");
            }
            else if (condition != null && condition.OrderType.GetValueOrDefault() == 0)
            {//期货 only
                dbQuery = dbQuery.Include("SaleContract.ProductItem.Product");
            }
            else if (condition != null && condition.OrderType.GetValueOrDefault() == 1)
            {//现货 only
                dbQuery = dbQuery.Include("SaleContract.StockItem.ProductItem.Product");
            }
            else
            {
                dbQuery = dbQuery.Include("SaleContract");
            }

            var productItemQueryable = dbQuery.Include(
                "SaleContract").Where(pEntity => (
                                          (pEntity.SaleContract.EntityPrivLevRequired < userEntityPriv ||
                                           (pEntity.SaleContract.EntityPrivLevRequired == userEntityPriv &&
                                            pEntity.SaleContract.OperatorSysUser == condition.UserName)) && //根据SaleContract进行数据权限控制
                                          (pEntity.ShipmentStatus == SaleShipmentStatus.NonShipment)))
                                       .Select(SaleItem => new
            {
                SaleItem,
                SaleItem.SaleContract,
                SaleItem.SaleContract.SaleContractKey,
                SaleItem.SaleContract.EntityPrivLevRequired,
            });

            AggregationsViewModel aggr = new AggregationsViewModel();

            if (ContractListInclude.WithAggregations ==
                (condition.ListInclude & ContractListInclude.WithAggregations))
            {
                aggr.IsEnable = true;
                aggr.Count    = productItemQueryable.Count();
            }

            if (condition.OrderField == ContractOrderField.CONTRACT_KEY_ASC)
            {
                productItemQueryable = productItemQueryable.OrderBy(m => m.SaleContractKey);
            }
            else if (condition.OrderField == ContractOrderField.CONTRACT_KEY_DESC)
            {
                productItemQueryable = productItemQueryable
                                       .OrderByDescending(m => m.SaleContractKey);
            }
            else if (condition.OrderField == ContractOrderField.CTIME_DESC)
            {
                productItemQueryable = productItemQueryable
                                       .OrderByDescending(m => m.SaleContract.SaleCreateTime);
            }
            else
            {
                productItemQueryable = productItemQueryable
                                       .OrderBy(m => m.SaleContract.SaleCreateTime);
            }

            if (condition.Page.HasValue && condition.Rows.HasValue)
            {
                productItemQueryable = productItemQueryable.Skip(
                    (condition.Page.Value - 1) * condition.Rows.Value)
                                       .Take(condition.Rows.Value);
            }

            var list = productItemQueryable.ToList();
            List <SaleProductItemInfo> result = new List <SaleProductItemInfo>();

            foreach (var item in list)
            {
                SaleProductItemInfo info = new SaleProductItemInfo(item.SaleItem);

                result.Add(info);
            }

            return(new ProductItemViewModel()
            {
                IsEnable = true,
                Aggregations = aggr,
                SaleProductItems = result,
            });
        }
        public StockOutViewModel GetIndexListStockItemForSold(IOwinContext owinContext,
                                                              ExtendedIdentityDbContext db, ContractListCondition condition)
        {
            var saleItemQuery = db.SaleProductItems.Include("SaleContract").Include("StockItem")
                                .Include("OrderProductItem").Where(si => si.StockItem != null &&
                                                                   si.StockItem.StockStatus == StockStatus.InStock)
                                .Select(SaleItem => new
            {
                SaleItem,
                SaleItem.SaleContract.SaleContractKey,
                SaleItem.SaleContract.SaleCreateTime,
                SaleItem.StockItem
            });

            StockOutViewModel resultViewModel = new StockOutViewModel();

            AggregationsViewModel aggregation = new AggregationsViewModel();

            if (condition != null && condition.IsEnable.GetValueOrDefault())
            {
                if (condition.OrderType.HasValue)
                {
                    saleItemQuery = saleItemQuery.Where(m => condition.OrderType.Value
                                                        == m.SaleItem.SaleContract.OrderType);
                }

                if (ContractListInclude.WithAggregations ==
                    (condition.ListInclude & ContractListInclude.WithAggregations))
                {
                    aggregation.IsEnable         = true;
                    aggregation.Count            = saleItemQuery.Count();
                    resultViewModel.Aggregations = aggregation;
                }

                if (condition.OrderField == ContractOrderField.CONTRACT_KEY_ASC)
                {
                    saleItemQuery = saleItemQuery.OrderBy(m => m.SaleContractKey);
                }
                else if (condition.OrderField == ContractOrderField.CONTRACT_KEY_DESC)
                {
                    saleItemQuery = saleItemQuery.OrderByDescending(m => m.SaleContractKey);
                }
                else if (condition.OrderField == ContractOrderField.CTIME_DESC)
                {
                    saleItemQuery = saleItemQuery.OrderByDescending(
                        m => m.SaleCreateTime);
                }
                else
                {
                    saleItemQuery = saleItemQuery.OrderBy(
                        m => m.SaleCreateTime);
                }

                if (condition.Page.HasValue && condition.Rows.HasValue)
                {
                    saleItemQuery = saleItemQuery.Skip(
                        (condition.Page.Value - 1) * condition.Rows.Value)
                                    .Take(condition.Rows.Value);
                }

                resultViewModel.IsEnable = true;

                List <SaleProductItemInfo> saleItemInfos = new List <SaleProductItemInfo>();
                List <StockItemInfo>       stockInfos    = new List <StockItemInfo>();
                resultViewModel.StockViewModel = new StockViewModel()
                {
                    IsEnable = true
                };



                var list = saleItemQuery.ToList();
                foreach (var one in list)
                {
                    SaleProductItemInfo info = new SaleProductItemInfo(one.SaleItem);
                    saleItemInfos.Add(info);
                    StockItemInfo stInfo = new StockItemInfo(one.StockItem);
                    stockInfos.Add(stInfo);
                }

                resultViewModel.SaleProductItems          = saleItemInfos;
                resultViewModel.StockViewModel.StockItems = stockInfos;
            }

            return(resultViewModel);
        }