public JsonResult Shipment(SaleProductItemInfo model)
        {
            SaleProductItem item = this.GetSaleProductItem(model);

            if (item == null)
            {
                ModelState.AddModelError(string.Empty, "找不到对应的货品项。");
                return(Json(this.GetModelStateErrors(ModelState)));
                //return View(this.GetModelStateErrors(ModelState));
            }

            if (item != null)
            {
                item.Comments       = model.Comments;
                item.ShipmentStatus = model.ShipmentStatus;
            }

            string errorMsg = AppBusinessManager.Instance.AddOrUpdateSaleProductItem(
                HttpContext.GetOwinContext(), db, item);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                ModelState.AddModelError(string.Empty, errorMsg);
            }

            return(Json(this.GetModelStateErrors(ModelState)));
            //FIXED 销售发货业务逻辑
            //return View(this.GetModelStateErrors(ModelState));
        }
        private string AddSaleContractFirst(ExtendedIdentityDbContext dbContext,
                                            ContractInfo model, string userName, SaleContract contract)
        {
            ContractInfo.AssignValues(model, contract);
            contract.SaleCreateTime        = DateTime.Now;
            contract.OperatorSysUser       = userName;
            contract.EntityPrivLevRequired = PrivilegeManager.Instance.GetEntityPrivilegeLevel(userName);
            StringBuilder builder = new StringBuilder();

            foreach (var item in model.SaleProductItems)
            {
                if (item != null)
                {
                    SaleProductItem saleItem = dbContext.SaleProductItems.Create();
                    SaleProductItemInfo.AssignValues(item, saleItem);
                    saleItem.SaleContract = contract;

                    string errMsg = this.HandleStockItemMove(dbContext, contract, saleItem, userName);
                    if (!string.IsNullOrWhiteSpace(errMsg))
                    {
                        builder.AppendLine(errMsg);
                    }

                    dbContext.SaleProductItems.Add(saleItem);
                }
            }

            dbContext.SaleContracts.Add(contract);

            string temp = builder.ToString();

            if (!string.IsNullOrWhiteSpace(temp))
            {
                return(temp.Trim());
            }

            return(string.Empty);
        }
        private SaleProductItem GetSaleProductItem(SaleProductItemInfo model)
        {
            SaleProductItem productItem = null;

            if (model != null && model.SaleContractId.HasValue && model.ProductItemId.HasValue)
            {
                productItem = db.SaleProductItems.FirstOrDefault(m =>
                                                                 m.SaleContractId == model.SaleContractId.GetValueOrDefault() &&
                                                                 (m.ProductItemId == model.ProductItemId.Value));
            }
            else if (model != null && model.SaleProductItemId.HasValue)
            {
                productItem = db.SaleProductItems.Find(
                    model.SaleProductItemId.GetValueOrDefault());
            }
            else if (model != null && model.StockItemId.HasValue)
            {
                productItem = db.SaleProductItems.FirstOrDefault(m =>
                                                                 m.StockItemId == model.StockItemId.Value);
            }

            //if (productItem == null)
            //{
            //    productItem = db.SaleProductItems.Create();
            //    productItem.ProductItemId = model.ProductItemId;
            //    productItem.Quantity = model.Quantity;
            //    productItem.SaleContractId = model.SaleContractId.GetValueOrDefault();
            //    productItem.StockItemId = model.StockItemId;
            //    productItem.UnitPrice = model.UnitPrice;
            //    productItem.Weight = model.Weight;
            //    productItem.Currency = model.Currency;
            //    productItem.Comments = model.Comments;
            //    productItem.ShipmentStatus = model.ShipmentStatus;
            //}

            return(productItem);
        }
        private string UpdateSaleContractFirst(ExtendedIdentityDbContext dbContext,
                                               ContractInfo model, string userName, SaleContract contract)
        {
            ContractInfo.AssignValues(model, contract);
            //contract.SaleCreateTime = DateTime.Now;
            //contract.OperatorSysUser = userName;
            StringBuilder builder = new StringBuilder();

            //需要删除的:contract里面有,但是model里面没有的,这一点应该先做
            List <SaleProductItem> tobeDeleted = new List <SaleProductItem>();

            System.Diagnostics.Debug.Assert(model.OrderType == contract.OrderType);
            if (model.OrderType == 0)
            {
                var lookup0 = model.SaleProductItems.ToLookup <SaleProductItemInfo, int>(
                    sp => sp.ProductItemId.HasValue ? sp.ProductItemId.Value : 0);
                var result0 = from one in contract.SaleProducts
                              where one.ProductItemId.HasValue && !lookup0.Contains(one.ProductItemId.Value)
                              select one;
                if (result0 != null && result0.Count() > 0)
                {
                    dbContext.SaleProductItems.RemoveRange(result0);
                    tobeDeleted.AddRange(result0);
                }
            }
            else if (model.OrderType == 1)
            {
                var lookup1 = model.SaleProductItems.ToLookup <SaleProductItemInfo, int>(
                    sp => sp.ProductItemId.HasValue ? sp.ProductItemId.Value : 0);
                var result1 = from one in contract.SaleProducts
                              where one.StockItem != null && !lookup1.Contains(one.StockItem.ProductItemId)
                              select one;
                if (result1 != null && result1.Count() > 0)
                {
                    dbContext.SaleProductItems.RemoveRange(result1);
                    tobeDeleted.AddRange(result1);
                }
            }

            var toDeleteIds = tobeDeleted.ToLookup <SaleProductItem, int>(
                sp => sp.SaleProductItemId);

            foreach (var item in model.SaleProductItems)
            {
                if (item != null)
                {
                    SaleProductItem saleItem = null;
                    if (model.OrderType == 0)
                    {
                        saleItem = contract.SaleProducts.FirstOrDefault(
                            s => (s.ProductItemId.HasValue && item.ProductItemId.HasValue &&
                                  s.ProductItemId.Value == item.ProductItemId.Value));
                    }
                    else if (model.OrderType == 1)
                    {
                        saleItem = contract.SaleProducts.FirstOrDefault(
                            s => (item.ProductItemId.HasValue && s.StockItem != null &&
                                  s.StockItem.ProductItemId == item.ProductItemId.Value));
                    }

                    if (saleItem == null)
                    {
                        saleItem = dbContext.SaleProductItems.Create();
                        contract.SaleProducts.Add(saleItem);
                    }
                    SaleProductItemInfo.AssignValues(item, saleItem);
                    saleItem.SaleContract = contract;

                    string errMsg = string.Empty;
                    if (contract.OrderType == 1)
                    {//现货
                        errMsg = this.HandleStockItemMove(dbContext, contract, saleItem, userName);
                    }
                    if (!string.IsNullOrWhiteSpace(errMsg))
                    {
                        builder.AppendLine(errMsg);
                    }
                    //dbContext.SaleProductItems.Add(saleItem);
                }
            }

            //var tp = from it in contract.SaleProducts
            //         where model.SaleProductItems.Any(s =>
            //         (s.SaleProductItemId != it.ProductItemId && contract.OrderType == 0)
            //         || (s.SaleProductItemId != it.SaleProductItemId && contract.OrderType == 1))
            //         // m => m.ProductItemId == it.SaleProductItemId)
            //         select it;

            //if (tp != null && tp.Count() > 0)
            //{
            //    foreach (var i in tp)
            //    {
            //        contract.SaleProducts.Remove(i);
            //    }
            //    dbContext.SaleProductItems.RemoveRange(tp);
            //}
            //dbContext.SaleContracts.Add(contract);

            if (tobeDeleted != null && tobeDeleted.Count() > 0)
            {
                for (int k = contract.SaleProducts.Count - 1; k >= 0; k--)
                {
                    var tempk = contract.SaleProducts.ElementAt(k);
                    if (toDeleteIds.Contains(tempk.ProductItemId.GetValueOrDefault()))
                    {
                        contract.SaleProducts.Remove(tempk);
                    }
                }
            }

            string temp = builder.ToString();

            if (!string.IsNullOrWhiteSpace(temp))
            {
                return(temp.Trim());
            }

            return(string.Empty);
        }