Пример #1
0
        public void StockIn(OutInOrder 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)
            {
                if (item.Quantity == 0)
                {
                    continue;
                }
                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.Quantity;
                inventory.SaleQuantity += item.Quantity;
                inventory.AvgCostPrice  = CalculatedAveragePrice(inventory.AvgCostPrice, inventoryQuantity, item.CostPrice, item.Quantity); // 修改库存均价
                if (inventory.LastCostPrice == 0)
                {
                    inventory.LastCostPrice = item.CostPrice > 0?item.CostPrice:item.LastCostPrice;
                }

                //记录库存流水
                var history = new StoreInventoryHistory(item.ProductId, entity.StoreId, inventoryQuantity, item.Quantity,
                                                        item.CostPrice, batchNo, entity.Id, entity.Code, BillIdentity.OtherInOrder, entity.UpdatedBy, entity.SupplierId);
                inventoryHistorys.Add(history);
                //记录库存批次
                var batchQuantity = CalculatedBatchQuantity(inventoryQuantity, item.Quantity);
                var batch         = new StoreInventoryBatch(item.ProductId, entity.StoreId, entity.SupplierId, batchQuantity,
                                                            item.LastCostPrice, item.CostPrice, batchNo, null, 0, entity.UpdatedBy);
                inventoryBatchs.Add(batch);
            }

            _db.Update(entity.Items.ToArray());
            _db.Update(inventorys.ToArray());
            _db.Insert(inventoryHistorys.ToArray());
            _db.Insert(inventoryBatchs.ToArray());
        }
Пример #2
0
        public List <OutInOrder> SplitOrderItem(OutInOrder model, OutInOrderType orderType)
        {
            var splitNumber               = 10;
            List <OutInOrderItem> items   = new List <OutInOrderItem>();
            List <OutInOrder>     entitys = new List <OutInOrder>();

            foreach (var item in model.Items)
            {
                if (items.Count == splitNumber)
                {
                    OutInOrder entity = new OutInOrder();
                    entity.AddRange(items, orderType);
                    entitys.Add(entity);
                    items = new List <OutInOrderItem>(); // 重新分配一个
                }
                var product = _db.Table.Find <Product>(item.ProductId);
                if (orderType.OutInInventory == ValueObject.OutInInventoryType.Out)
                {
                    //检查库存
                    var inventoryModel = _db.Table.Find <StoreInventory>(n => n.ProductId == item.ProductId && n.StoreId == model.StoreId);
                    if (item.Quantity > inventoryModel.Quantity)
                    {
                        throw new FriendlyException(string.Format("商品{0}退货数量{1} > 库存数{2}", product.Code, item.Quantity, inventoryModel.Quantity));
                    }
                }
                else
                {
                    if (item.Quantity <= 0)
                    {
                        throw new FriendlyException(string.Format("{0}:数量不能小于等于0", product.Code));
                    }
                }
                var itemProduct = items.FirstOrDefault(n => n.ProductId == item.ProductId);
                if (itemProduct == null)
                {
                    items.Add(item);
                }
                else
                {
                    itemProduct.Quantity += item.Quantity;
                }
            }
            if (items.Count > 0 && items.Count <= splitNumber)
            {
                OutInOrder entity = new OutInOrder();
                entity.AddRange(items, orderType);
                entitys.Add(entity);
            }
            return(entitys);
        }
Пример #3
0
        public void Create(OutInOrderModel model)
        {
            OutInOrder entity = model.MapTo <OutInOrder>();

            entity.CreatedBy     = model.EditBy;
            entity.CreatedByName = model.EditByName;
            entity.UpdatedBy     = model.EditBy;
            entity.UpdatedByName = model.EditByName;
            // entity.Code = _sequenceService.GenerateNewCode(BillIdentity.OtherInOrder);
            // 明细
            var items = JsonConvert.DeserializeObject <List <OutInOrderItem> >(model.ItemsJson);

            entity.SetItems(items);
            var orderType = _db.Table.Find <OutInOrderType>(entity.OutInOrderTypeId);

            if (orderType == null)
            {
                throw new FriendlyException("业务类别为空");
            }

            var reason       = "创建其他入库单";
            var billIdentity = BillIdentity.OtherInOrder;

            if (orderType.OutInInventory == OutInInventoryType.Out)
            {
                reason       = "创建其他出库单";
                billIdentity = BillIdentity.OtherOutOrder;
            }

            var entitys = _service.SplitOrderItem(entity, orderType);

            foreach (var order in entitys)
            {
                entity.Code = _sequenceService.GenerateNewCode(billIdentity);
                entity.SetItems(order.Items.ToList());
                _db.Insert(entity);
                var history = new ProcessHistory(entity.CreatedBy, entity.CreatedByName, (int)entity.Status, entity.Id, billIdentity.ToString(), reason);
                _db.Command.AddExecute(history.CreateSql(entity.GetType().Name, entity.Code), history);
                _db.SaveChange();

                if (model.SaveAndSubmit)
                {
                    var modelEntity = _db.Table.Find <OutInOrder>(n => n.Code == entity.Code);
                    model.Id = modelEntity.Id;
                    Submit(model.Id, model.EditBy, model.EditByName);
                }
            }
        }
Пример #4
0
        public IEnumerable <StoreInventory> CheckNotExistsProduct(OutInOrder entity)
        {
            if (entity == null)
            {
                throw new FriendlyException("单据不存在");
            }
            if (entity.Items.Count() == 0)
            {
                throw new FriendlyException("单据明细为空");
            }
            //查询门店库存中不存在商品
            string sql   = @"select i.ProductId ,o.StoreId  from OutInOrderitem i inner join OutInOrder o on i.OutInOrderId = o.Id 
left join  storeinventory s  on i.ProductId = s.ProductId and s.StoreId=o.StoreId 
where s.Id is null  and i.`OutInOrderId`=@OutInOrderId";
            var    items = _db.Table.FindAll <StoreInventory>(sql, new { StoreId = entity.StoreId, OutInOrderId = entity.Id });

            return(items);
        }
Пример #5
0
        public void StockOut(OutInOrder entity)
        {
            if (entity == null)
            {
                throw new FriendlyException("单据不存在");
            }
            if (entity.Items.Count() == 0)
            {
                throw new FriendlyException("单据明细为空");
            }
            var entityItems           = entity.Items;
            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 }).ToList();
            var inventoryBatchs       = _db.Table.FindAll <StoreInventoryBatch>("select * from storeinventorybatch where  storeId=@StoreId and productId in @ProductIds and Quantity>0", new { StoreId = entity.StoreId, ProductIds = productIdArray }).ToList();
            var inventoryHistorys     = new List <StoreInventoryHistory>();
            var inventoryBatchUpdates = new List <StoreInventoryBatch>(); //批次更新

            foreach (var item in entity.Items)
            {
                var inventory = inventorys.FirstOrDefault(n => n.ProductId == item.ProductId);
                if (inventory == null)
                {
                    throw new FriendlyException(string.Format("商品{0}库存记录不存在", item.ProductId));
                }
                // 先检查总库存是否够扣减
                if (inventory.Quantity < item.Quantity)
                {
                    var product = _db.Table.Find <Product>(inventory.ProductId);
                    throw new FriendlyException(string.Format("{0}库存不足!", product.Code));
                }
                // 扣减总库存
                var inventoryQuantity = inventory.Quantity;
                inventory.Quantity     -= item.Quantity;
                inventory.SaleQuantity -= item.Quantity;

                //按照先进先出扣减批次库存
                var productBatchs = SortBatchByFIFO(inventoryBatchs, item.ProductId, 0);
                var leftQuantity  = item.Quantity; // 需要扣减的总数量
                foreach (var batchItem in productBatchs)
                {
                    if (batchItem.Quantity >= leftQuantity)
                    {
                        batchItem.Quantity -= leftQuantity;
                        inventoryBatchUpdates.Add(batchItem);
                        //记录修改历史
                        inventoryHistorys.Add(new StoreInventoryHistory(inventory.ProductId, entity.StoreId, inventoryQuantity, -leftQuantity,
                                                                        batchItem.Price, batchItem.BatchNo, entity.Id, entity.Code, BillIdentity.OtherOutOrder, entity.UpdatedBy, batchItem.SupplierId));
                        break;
                    }
                    else
                    {
                        inventoryHistorys.Add(new StoreInventoryHistory(inventory.ProductId, entity.StoreId, inventoryQuantity, -batchItem.Quantity,
                                                                        batchItem.Price, batchItem.BatchNo, entity.Id, entity.Code, BillIdentity.OtherOutOrder, entity.UpdatedBy, batchItem.SupplierId));
                        // 剩余扣减数
                        inventoryQuantity  = inventoryQuantity - batchItem.Quantity; // 第1+N次扣减后总库存
                        leftQuantity       = leftQuantity - batchItem.Quantity;
                        batchItem.Quantity = 0;                                      //扣完
                        inventoryBatchUpdates.Add(batchItem);
                    }
                }
            }

            _db.Update(inventorys.ToArray());
            if (inventoryBatchUpdates.Count > 0)
            {
                _db.Update(inventoryBatchUpdates.ToArray());
            }
            _db.Insert(inventoryHistorys.ToArray());
        }