Ejemplo n.º 1
0
        /// <summary>
        /// 采购入库
        /// </summary>
        /// <param name="entity"></param>
        public void StockInProducts(StorePurchaseOrder entity)
        {
            if (entity == null)
            {
                throw new FriendlyException("单据不存在");
            }
            if (entity.Items.Count() == 0)
            {
                throw new FriendlyException("单据明细为空");
            }
            //记录库存批次
            var productIdArray    = entity.Items.Select(n => n.ProductId).ToArray();
            var inventorys        = _db.Table.FindAll <StoreInventory>("select * from storeinventory where productId in @ProductIds and StoreId=@StoreId", new { ProductIds = productIdArray, StoreId = entity.StoreId });
            var inventoryBatchs   = new List <StoreInventoryBatch>();
            var inventoryHistorys = new List <StoreInventoryHistory>();
            var batchNo           = _sequenceService.GenerateBatchNo(entity.StoreId);

            foreach (var item in entity.Items)
            {
                // 采购单允许实收为0,这种商品需要跳过,不处理
                if (item.ActualQuantity == 0)
                {
                    continue;
                }
                item.BatchNo = batchNo; //更新单据明细批次
                var inventory = inventorys.FirstOrDefault(n => n.ProductId == item.ProductId);
                if (inventory == null)
                {
                    throw new FriendlyException(string.Format("商品{0}不存在", item.ProductId));
                }

                var inventoryQuantity = inventory.Quantity;
                inventory.Quantity     += item.ActualQuantity;
                inventory.SaleQuantity += item.ActualQuantity;
                inventory.AvgCostPrice  = CalculatedAveragePrice(inventory.AvgCostPrice, inventoryQuantity, item.Price, item.ActualQuantity); // 修改库存均价
                inventory.LastCostPrice = item.Price > 0 ? item.Price : inventory.LastCostPrice;


                //记录库存流水
                var history = new StoreInventoryHistory(item.ProductId, entity.StoreId, inventoryQuantity, item.ActualQuantity,
                                                        item.Price, item.BatchNo, entity.Id, entity.Code, BillIdentity.StorePurchaseOrder, entity.StoragedBy, entity.SupplierId);
                inventoryHistorys.Add(history);
                //记录库存批次
                var batchQuantity = CalculatedBatchQuantity(inventoryQuantity, item.ActualQuantity);
                var batch         = new StoreInventoryBatch(item.ProductId, entity.StoreId, entity.SupplierId, batchQuantity,
                                                            item.ContractPrice, item.Price, item.BatchNo, item.ProductionDate, item.ShelfLife, entity.StoragedBy);
                inventoryBatchs.Add(batch);
            }

            _db.Update(entity.Items.ToArray());
            _db.Update(inventorys.ToArray());
            _db.Insert(inventoryHistorys.ToArray());
            _db.Insert(inventoryBatchs.ToArray());
        }
Ejemplo n.º 2
0
        public List <StoreInventoryBatch> SaveBatch(StorePurchaseOrder entity)
        {
            var inventoryBatchs = new List <StoreInventoryBatch>();
            var batchNo         = _sequenceService.GenerateBatchNo(entity.StoreId);

            foreach (var item in entity.Items)
            {
                //批次
                item.BatchNo = batchNo;
                var batch = new StoreInventoryBatch(item.ProductId, entity.StoreId, entity.SupplierId, item.ActualQuantity,
                                                    item.ContractPrice, item.Price, item.BatchNo, item.ProductionDate, item.ShelfLife, entity.StoragedBy);
                inventoryBatchs.Add(batch);
            }
            return(inventoryBatchs);
        }