Example #1
0
        /// <summary>
        /// 采购单确认入库(接口调用)
        /// </summary>
        /// <param name="poSysNo"></param>
        public static PurchaseOrderInfo WaitingInstockPO(PurchaseOrderInfo poInfo, List <KeyValuePair <string, int> > productList)
        {
            if (null == poInfo || !poInfo.SysNo.HasValue)
            {
                throw new BusinessException("找不到相关的采购单信息!");
            }
            if (poInfo.PurchaseOrderBasicInfo.PurchaseOrderStatus != PurchaseOrderStatus.Reporting)
            {
                throw new BusinessException("该采购单状态不是'申报中'状态,不能进行入库确认操作!");
            }
            if (productList == null || productList.Count <= 0)
            {
                throw new BusinessException("该采购单商品明细传入为空!");
            }
            //poInfo.PurchaseOrderBasicInfo.ETATimeInfo.ETATime = localEntity.PurchaseOrderBasicInfo.ETATimeInfo.ETATime;
            //poInfo.PurchaseOrderBasicInfo.ETATimeInfo.HalfDay = localEntity.PurchaseOrderBasicInfo.ETATimeInfo.HalfDay;
            TransactionOptions options = new TransactionOptions();

            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            options.Timeout        = System.Transactions.TransactionManager.DefaultTimeout;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                #region 更新PO单Item的PurchaseQty:
                if (null != productList && productList.Count > 0)
                {
                    foreach (var item in productList)
                    {
                        var poItem = poInfo.POItems.SingleOrDefault(x => x.ProductID.Trim().ToLower() == item.Key.Trim().ToLower());
                        if (null != poItem)
                        {
                            if (poItem.PrePurchaseQty < item.Value)
                            {
                                throw new BusinessException(string.Format("采购单编号:{0},商品ID为{1}的实际采购数量(ItemNum)大于计划采购数量!", poInfo.SysNo.Value, item.Key));
                            }

                            poItem.PurchaseQty = item.Value;
                            ProductPurchaseDA.UpdatePOItemPurchaseQty(poItem.ItemSysNo.Value, item.Value);
                        }
                        else
                        {
                            throw new BusinessException(string.Format("采购单编号:{0},找不到商品ID为{1}的采购单商品信息!", poInfo.SysNo.Value, item.Key));
                        }
                    }
                }
                #endregion

                #region 更新采购单状态:
                poInfo.PurchaseOrderBasicInfo.PurchaseOrderStatus = PurchaseOrderStatus.WaitingInStock;
                poInfo.PurchaseOrderBasicInfo.MemoInfo.RefuseMemo = string.Empty;

                ProductPurchaseDA.WaitingInStockPO(poInfo);
                #endregion

                #region 更新POItem信息:
                List <KeyValuePair <int, int> > kv = new List <KeyValuePair <int, int> >();
                foreach (PurchaseOrderItemInfo item in poInfo.POItems)
                {
                    kv.Add(new KeyValuePair <int, int>(item.ProductSysNo.Value, item.PurchaseQty.Value));

                    //总仓有效库存 上月销售总量
                    PurchaseOrderItemInfo tempPoItem = ProductPurchaseDA.LoadExtendPOItem(item.ProductSysNo.Value);
                    item.M1                  = tempPoItem.M1;
                    item.AvailableQty        = tempPoItem.AvailableQty;
                    item.UnitCostWithoutTax  = item.UnitCostWithoutTax ?? 0;
                    item.CurrentUnitCost     = tempPoItem.CurrentUnitCost;
                    item.CurrentPrice        = tempPoItem.CurrentPrice;
                    item.LastInTime          = tempPoItem.LastInTime;
                    item.LastAdjustPriceDate = tempPoItem.LastAdjustPriceDate;
                    item.LastOrderPrice      = tempPoItem.LastOrderPrice;
                    ProductPurchaseDA.UpdatePOItem(item);
                }
                #endregion

                #region 设置采购在途数量,(代销PO该业务逻辑不变)

                ProductPurchaseInstockAdjustInventoryInfo inventoryAdjustInfo = new ProductPurchaseInstockAdjustInventoryInfo()
                {
                    ReferenceSysNo   = poInfo.SysNo.Value,
                    SourceActionName = "Audit",
                    AdjustItemList   = new List <Entity.Inventory.InventoryAdjustItemInfo>()
                };
                kv.ForEach(x =>
                {
                    inventoryAdjustInfo.AdjustItemList.Add(new InventoryAdjustItemInfo()
                    {
                        ProductSysNo   = x.Key,
                        StockSysNo     = poInfo.PurchaseOrderBasicInfo.StockInfo.SysNo.Value,
                        AdjustQuantity = x.Value
                    });
                });

                foreach (InventoryAdjustItemInfo adjustItem in inventoryAdjustInfo.AdjustItemList)
                {
                    //this.CurrentAdjustItemInfo = adjustItem;
                    //this.AdjustQuantity = adjustItem.AdjustQuantity;
                    //ProcessAdjustItemInfo();
                    CostLockType     costLockAction = CostLockType.NoUse;
                    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
                    };
                    //获取负po的成本库存
                    List <ProductCostIn> productCostInList = InventoryDA.GetProductCostIn(adjustItem.ProductSysNo, Convert.ToInt32(inventoryAdjustInfo.ReferenceSysNo), adjustItem.StockSysNo);


                    List <ProductCostIn> adjustProductCostInList = new List <ProductCostIn>();
                    int CanUseQuantity = 0;
                    //区分正负PO单
                    if (adjustItem.AdjustQuantity < 0)
                    {
                        //负PO单审核, AdjustQty<0, 减少可用库存, 增加已分配库存
                        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;
                        }

                        //标识要锁定成本库存
                        costLockAction = CostLockType.Lock;
                        int temp = Math.Abs(adjustItem.AdjustQuantity);
                        //锁定库存
                        foreach (var item in productCostInList)
                        {
                            CanUseQuantity = item.LeftQuantity - item.LockQuantity;
                            //可用数量大于要锁定数量,直接累加加到锁定数量
                            if (CanUseQuantity >= temp)
                            {
                                item.LockQuantity += temp;
                                adjustProductCostInList.Add(item);
                                break;
                            }
                            else if (CanUseQuantity > 0) //可用数量不足且大于0,
                            {
                                //调整数量减少相应值,进行一次锁定分配
                                temp = temp - CanUseQuantity;
                                //将可用加到锁定数量上
                                item.LockQuantity += CanUseQuantity;
                                adjustProductCostInList.Add(item);
                            }
                        }
                    }
                    else
                    {
                        //正PO单审核, AdjustQty>0, 增加采购库存
                        stockInventoryAdjustInfo.PurchaseQty = adjustItem.AdjustQuantity;
                        totalInventoryAdjustInfo.PurchaseQty = 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);
                    //如果需要调整锁定库存
                    if (costLockAction != CostLockType.NoUse && adjustProductCostInList != null)
                    {
                        InventoryDA.LockProductCostInList(adjustProductCostInList);
                    }
                }
                #endregion

                #region 如果是负采购单,调整批次库存:
                if (poInfo.PurchaseOrderBasicInfo.PurchaseOrderType == PurchaseOrderType.Negative)
                {
                    SetInventoryInfo(poInfo, "");
                }

                #endregion

                scope.Complete();
            }
            return(poInfo);
        }