Example #1
0
        /// <summary>
        /// 损益单出库 (确认成功):
        /// </summary>
        /// <param name="adjustRequestSysNo">原始损益单编号</param>
        /// <param name="productSysNo">损益商品编号</param>
        /// <param name="realAdjustQty">该商品的实际损益数量</param>
        /// <returns></returns>
        public static AdjustRequestInfo AdjustOutStock(int adjustRequestSysNo, int?productSysNo, int?realAdjustQty, int sellerSysNo)
        {
            var adjustRequestInfo = GetAdjustRequestInfoBySysNo(adjustRequestSysNo);


            #region Check操作 :
            if (null == adjustRequestInfo || adjustRequestInfo.SysNo <= 0)
            {
                throw new BusinessException(string.Format("找不到编号为{0}的损益单据信息!", adjustRequestSysNo));
            }

            if (productSysNo.HasValue && realAdjustQty.HasValue)
            {
                var productItemInfo = adjustRequestInfo.AdjustItemInfoList.FirstOrDefault(x => x.ProductSysNo == productSysNo);
                if (productItemInfo == null)
                {
                    throw new BusinessException(string.Format("编号为{0}的商品不存在于该损益单中!损益单编号 :{1}", productSysNo, adjustRequestSysNo));
                }
                if (realAdjustQty >= 0)
                {
                    if (realAdjustQty > productItemInfo.AdjustQuantity)
                    {
                        throw new BusinessException(string.Format("编号为{0}的商品实际损益的数量大于预损益的数量!损益单编号 :{1}", productSysNo, adjustRequestSysNo));
                    }
                }
                else
                {
                    if (realAdjustQty < productItemInfo.AdjustQuantity)
                    {
                        throw new BusinessException(string.Format("编号为{0}的商品实际损益的数量大于预损益的数量!损益单编号 :{1}", productSysNo, adjustRequestSysNo));
                    }
                }
            }

            var stockInfo = StockService.LoadStock(adjustRequestInfo.Stock.SysNo);
            if (null == stockInfo)
            {
                throw new BusinessException("损益单据关联的仓库编号无效!");
            }
            if (stockInfo.MerchantSysNo != sellerSysNo)
            {
                throw new BusinessException("此商家无权操作此单据!");
            }

            //增加损益单状态Check (已申报状态):
            if (adjustRequestInfo.RequestStatus != AdjustRequestStatus.Reported)
            {
                throw new BusinessException(string.Format("损益单编号 :{1},当前单据的状态不是'已申报'状态,不能进行损益单确认操作", productSysNo, adjustRequestSysNo));
            }

            #endregion

            bool isConsign = false;
            adjustRequestInfo.OutStockDate  = DateTime.Now;
            adjustRequestInfo.RequestStatus = Enums.AdjustRequestStatus.OutStock;
            isConsign = (adjustRequestInfo.ConsignFlag == RequestConsignFlag.Consign || adjustRequestInfo.ConsignFlag == RequestConsignFlag.GatherPay);
            var inventoryAdjustContract = new InventoryAdjustContractInfo
            {
                SourceBizFunctionName = InventoryAdjustSourceBizFunction.Inventory_AdjustRequest,
                SourceActionName      = InventoryAdjustSourceAction.OutStock,
                ReferenceSysNo        = adjustRequestInfo.SysNo.ToString(),
                AdjustItemList        = new List <InventoryAdjustItemInfo>()
            };

            using (TransactionScope scope = new TransactionScope())
            {
                if (adjustRequestInfo.AdjustItemInfoList != null && adjustRequestInfo.AdjustItemInfoList.Count > 0)
                {
                    adjustRequestInfo.AdjustItemInfoList.ForEach(adjustItem =>
                    {
                        //if (adjustItem.AdjustProduct.ProductPriceInfo == null)
                        //{
                        //    BizExceptionHelper.Throw("Common_CannotFindPriceInformation");
                        //    throw new BusinessException("损益数量只能为非0的整数!");
                        //}
                        if (adjustItem.AdjustQuantity == 0)
                        {
                            throw new BusinessException("损益数量只能为非0的整数!");
                        }

                        var cost = InventoryDA.GetItemCost(adjustItem.ProductSysNo.Value);
                        inventoryAdjustContract.AdjustItemList.Add(new InventoryAdjustItemInfo
                        {
                            AdjustQuantity = adjustItem.AdjustQuantity.Value,
                            ProductSysNo   = adjustItem.ProductSysNo.Value,
                            StockSysNo     = (int)adjustRequestInfo.Stock.SysNo,
                            AdjustUnitCost = cost,
                        });

                        //update flash item unit cost
                        if (adjustItem.AdjustCost != cost)
                        {
                            adjustItem.AdjustCost = cost;
                            InventoryDA.UpdateAdjustItemCost(adjustItem);
                        }
                    });
                }
                InventoryDA.UpdateAdjustRequestStatus(adjustRequestInfo);

                if (inventoryAdjustContract.AdjustItemList.Count > 0)
                {
                    //string adjustResult = ObjectFactory<InventoryAdjustContractProcessor>.Instance.ProcessAdjustContract(inventoryAdjustContract);
                    //if (!string.IsNullOrEmpty(adjustResult))
                    //{
                    //    throw new BizException("库存调整失败: " + adjustResult);
                    //}


                    #region 调整库存:

                    foreach (InventoryAdjustItemInfo adjustItem in inventoryAdjustContract.AdjustItemList)
                    {
                        ProductQueryInfo productInfo = ProductService.GetProductBySysNo(adjustItem.ProductSysNo);
                        if (productInfo == null || productInfo.SysNo <= 0)
                        {
                            throw new BusinessException(string.Format("欲调库存的商品不存在,商品编号:{0}", adjustItem.ProductSysNo));
                        }
                        InventoryDA.InitProductInventoryInfo(adjustItem.ProductSysNo, adjustItem.StockSysNo);
                        var inventoryType = InventoryDA.GetProductInventroyType(adjustItem.ProductSysNo);
                        ECommerce.Entity.Inventory.ProductInventoryInfo stockInventoryCurrentInfo = InventoryDA.GetProductInventoryInfoByStock(adjustItem.ProductSysNo, adjustItem.StockSysNo);
                        ECommerce.Entity.Inventory.ProductInventoryInfo totalInventoryCurrentInfo = InventoryDA.GetProductTotalInventoryInfo(adjustItem.ProductSysNo);

                        ECommerce.Entity.Inventory.ProductInventoryInfo stockInventoryAdjustInfo = new Entity.Inventory.ProductInventoryInfo()
                        {
                            ProductSysNo = adjustItem.ProductSysNo,
                            StockSysNo   = adjustItem.StockSysNo
                        };

                        ECommerce.Entity.Inventory.ProductInventoryInfo totalInventoryAdjustInfo = new ECommerce.Entity.Inventory.ProductInventoryInfo()
                        {
                            ProductSysNo = adjustItem.ProductSysNo
                        };


                        if (adjustItem.AdjustQuantity < 0)
                        {
                            //损单出库
                            if (adjustRequestInfo.ConsignFlag == RequestConsignFlag.Consign || adjustRequestInfo.ConsignFlag == RequestConsignFlag.GatherPay)
                            {
                                //代销商品, 恢复可用库存, 减少已分配库存/代销库存
                                stockInventoryAdjustInfo.AvailableQty = -adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.AvailableQty = -adjustItem.AdjustQuantity;

                                if (adjustItem.AdjustQuantity < 0)
                                {
                                    //AllocatedQty(-,->0),小于0则自动调为0。
                                    if (stockInventoryCurrentInfo.AllocatedQty + adjustItem.AdjustQuantity < 0)
                                    {
                                        stockInventoryAdjustInfo.AllocatedQty = -stockInventoryCurrentInfo.AllocatedQty;
                                    }
                                    else
                                    {
                                        stockInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    }

                                    if (totalInventoryCurrentInfo.AllocatedQty + adjustItem.AdjustQuantity < 0)
                                    {
                                        totalInventoryAdjustInfo.AllocatedQty = -totalInventoryCurrentInfo.AllocatedQty;
                                    }
                                    else
                                    {
                                        totalInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    }
                                }
                                else
                                {
                                    stockInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    totalInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                }

                                stockInventoryAdjustInfo.ConsignQty = adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.ConsignQty = adjustItem.AdjustQuantity;
                            }
                            else
                            {
                                //非代销商品, 减少财务库存/已分配库存
                                stockInventoryAdjustInfo.AccountQty = adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.AccountQty = adjustItem.AdjustQuantity;

                                if (adjustItem.AdjustQuantity < 0)
                                {
                                    //AllocatedQty(-,->0),小于0则自动调为0。
                                    if (stockInventoryCurrentInfo.AllocatedQty + adjustItem.AdjustQuantity < 0)
                                    {
                                        stockInventoryAdjustInfo.AllocatedQty = -stockInventoryCurrentInfo.AllocatedQty;
                                    }
                                    else
                                    {
                                        stockInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    }

                                    if (totalInventoryCurrentInfo.AllocatedQty + adjustItem.AdjustQuantity < 0)
                                    {
                                        totalInventoryAdjustInfo.AllocatedQty = -totalInventoryCurrentInfo.AllocatedQty;
                                    }
                                    else
                                    {
                                        totalInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    }
                                }
                                else
                                {
                                    stockInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                    totalInventoryAdjustInfo.AllocatedQty = adjustItem.AdjustQuantity;
                                }
                            }
                        }
                        else
                        {
                            //溢单出库
                            if (adjustRequestInfo.ConsignFlag == RequestConsignFlag.Consign || adjustRequestInfo.ConsignFlag == RequestConsignFlag.GatherPay)
                            {
                                //代销商品, 增加代销库存 (损/溢单都增加代销库存?)
                                stockInventoryAdjustInfo.ConsignQty = adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.ConsignQty = adjustItem.AdjustQuantity;
                            }
                            else
                            {
                                //非代销商品, 增加财务库存/可用库存
                                stockInventoryAdjustInfo.AccountQty   = adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.AccountQty   = adjustItem.AdjustQuantity;
                                stockInventoryAdjustInfo.AvailableQty = adjustItem.AdjustQuantity;
                                totalInventoryAdjustInfo.AvailableQty = adjustItem.AdjustQuantity;
                            }
                        }

                        //预检调整后的商品库存是否合法
                        Entity.Inventory.ProductInventoryInfo stockInventoryAdjustAfterAdjust = InventoryService.PreCalculateInventoryAfterAdjust(stockInventoryCurrentInfo, stockInventoryAdjustInfo);
                        Entity.Inventory.ProductInventoryInfo totalInventoryAdjustAfterAdjust = InventoryService.PreCalculateInventoryAfterAdjust(totalInventoryCurrentInfo, totalInventoryAdjustInfo);

                        bool isNeedCompareAvailableQtyAndAccountQty = true;
                        InventoryService.PreCheckGeneralRules(stockInventoryAdjustAfterAdjust, ref isNeedCompareAvailableQtyAndAccountQty);
                        InventoryService.PreCheckGeneralRules(totalInventoryAdjustAfterAdjust, ref isNeedCompareAvailableQtyAndAccountQty);

                        //调整商品库存:
                        InventoryDA.AdjustProductStockInventoryInfo(stockInventoryAdjustInfo);
                        InventoryDA.AdjustProductTotalInventoryInfo(totalInventoryAdjustInfo);
                    }
                    #endregion

                    #region 损益单为代销类型,出库时需要写入代销转财务日志
                    if (isConsign)
                    {
                        List <ConsignToAcctLogInfo> acctLogInfoList = new List <ConsignToAcctLogInfo>();
                        adjustRequestInfo.AdjustItemInfoList.ForEach(x =>
                        {
                            ConsignToAcctLogInfo acctLog = new ConsignToAcctLogInfo();
                            acctLog.ProductSysNo         = x.ProductSysNo;
                            acctLog.StockSysNo           = adjustRequestInfo.Stock.SysNo;
                            acctLog.VendorSysNo          = InventoryDA.GetProductBelongVendorSysNo(x.ProductSysNo.Value);
                            acctLog.ProductQuantity      = -x.AdjustQuantity;
                            acctLog.OutStockTime         = adjustRequestInfo.OutStockDate;
                            acctLog.CreateCost           = x.AdjustCost;
                            acctLog.OrderSysNo           = adjustRequestInfo.SysNo;
                            acctLog.CompanyCode          = adjustRequestInfo.CompanyCode;
                            acctLog.StoreCompanyCode     = adjustRequestInfo.CompanyCode;
                            acctLog.IsConsign            = (int)adjustRequestInfo.ConsignFlag;
                            acctLogInfoList.Add(acctLog);
                        });

                        if (acctLogInfoList.Count > 0)
                        {
                            foreach (var item in acctLogInfoList)
                            {
                                InventoryDA.CreatePOConsignToAccLogForInventory(item);
                            }
                        }
                    }
                    #endregion

                    #region
                    string InUser = "******";
                    List <InventoryBatchDetailsInfo> batchDetailsInfoEntitylist = InventoryDA.GetBatchDetailsInfoEntityListByNumber(adjustRequestInfo.SysNo.Value);
                    if (batchDetailsInfoEntitylist != null && batchDetailsInfoEntitylist.Count > 0)
                    {
                        #region 构建损益单 出库调整批次库存表的SSB消息 调整库存
                        List <ItemBatchInfo> itemBatchInfoList = new List <ItemBatchInfo>();
                        foreach (var item in batchDetailsInfoEntitylist)
                        {
                            ItemBatchInfo itemBatchInfo = new ItemBatchInfo();
                            itemBatchInfo.BatchNumber   = item.BatchNumber;
                            itemBatchInfo.ProductNumber = item.ProductSysNo.ToString();
                            Stock stock = new Stock();
                            AdjustRequestItemInfo aEntity = new AdjustRequestItemInfo();
                            aEntity = adjustRequestInfo.AdjustItemInfoList.Find(x => { return(x.ProductSysNo == item.ProductSysNo); });
                            if (aEntity != null && aEntity.AdjustQuantity > 0)
                            {
                                stock.Quantity     = item.Quantity.ToString(); //单据出库两个数量都要调整
                                stock.AllocatedQty = string.Empty;             //益单不调 占用库存 作废 取消作废 单据 只调整 占用库存
                            }
                            else
                            {
                                stock.Quantity     = item.Quantity.ToString(); //单据出库两个数量都要调整
                                stock.AllocatedQty = item.Quantity.ToString(); //损单 需要调整 占用库存 作废 取消作废 单据 只调整 占用库存
                            }
                            stock.WarehouseNumber = item.StockSysNo.ToString();
                            List <Stock> StockList = new List <Stock>();
                            StockList.Add(stock);

                            Stocks stocks = new Stocks();
                            stocks.Stock = StockList;

                            itemBatchInfo.Stocks = stocks;
                            itemBatchInfoList.Add(itemBatchInfo);
                        }
                        BatchXMLMessage batchXMLMessage = new BatchXMLMessage()
                        {
                            Header = new InventoryHeader
                            {
                                NameSpace          = "http://soa.ECommerce.com/InventoryProfile",
                                Action             = "OutStock",
                                Version            = "V10",
                                Type               = "Adjust",
                                CompanyCode        = "8601",
                                Tag                = "AdjustOutStock",
                                Language           = "zh-CN",
                                From               = "IPP",
                                GlobalBusinessType = "Listing",
                                StoreCompanyCode   = "8601",
                                TransactionCode    = adjustRequestInfo.SysNo.ToString()
                            },
                            Body = new InventoryBody
                            {
                                InUser        = InUser,
                                Number        = adjustRequestInfo.SysNo.ToString(),
                                ItemBatchInfo = itemBatchInfoList
                            }
                        };
                        string      paramXml = SerializationUtility.XmlSerialize(batchXMLMessage);
                        XmlDocument xmlD     = new XmlDocument();
                        xmlD.LoadXml(paramXml);
                        paramXml = "<" + xmlD.DocumentElement.Name + ">" + xmlD.DocumentElement.InnerXml + "</" + xmlD.DocumentElement.Name + ">";

                        InventoryDA.AdjustBatchNumberInventory(paramXml);//调整批次库存表 占用数量
                        #endregion
                    }
                    List <InventoryAdjustItemInfo> adjustCaseEntityList = new List <InventoryAdjustItemInfo>();
                    foreach (var item in inventoryAdjustContract.AdjustItemList)
                    {
                        if (!InventoryDA.CheckISBatchNumberProduct(item.ProductSysNo))
                        {
                            item.AdjustQuantity = item.AdjustQuantity;

                            adjustCaseEntityList.Add(item);
                        }
                    }
                    AdjustSendSSBToWMS(adjustRequestInfo.SysNo.Value, adjustRequestInfo.Stock.SysNo.ToString(), batchDetailsInfoEntitylist, adjustCaseEntityList);//损益单出库向仓库发送SSB消息

                    #endregion
                }

                scope.Complete();
            }

            return(adjustRequestInfo);
        }