public void Create(CreateStorePurchaseOrder model)
        {
            var entity = new StorePurchaseOrder();

            entity = model.MapTo <StorePurchaseOrder>();
            entity.AddItems(model.ConvertJsonToItem());
            var reason       = "创建采购单";
            var billIdentity = BillIdentity.StorePurchaseOrder;

            if (entity.OrderType == OrderType.Refund)
            {
                reason       = "创建采购退单";
                billIdentity = BillIdentity.StorePurchaseRefundOrder;
            }

            var entitys = _service.SplitOrderItem(entity);

            foreach (var order in entitys)
            {
                entity.Code = _sequenceService.GenerateNewCode(billIdentity);
                entity.SetItems(order.Items.ToList());
                _db.Insert(entity);
                var history = new ProcessHistory(model.CreatedBy, model.CreatedByName, (int)entity.Status, entity.Id, billIdentity.ToString(), reason);
                _db.Command.AddExecute(history.CreateSql(entity.GetType().Name, entity.Code), history);
                _db.SaveChange();
            }
        }
Exemple #2
0
        public List <StorePurchaseOrder> SplitOrderItem(StorePurchaseOrder model)
        {
            var splitNumber = 10;
            List <StorePurchaseOrderItem> items   = new List <StorePurchaseOrderItem>();
            List <StorePurchaseOrder>     entitys = new List <StorePurchaseOrder>();

            foreach (var item in model.Items)
            {
                if (items.Count == splitNumber)
                {
                    StorePurchaseOrder entity = new StorePurchaseOrder();
                    entity.SetItems(items);
                    entitys.Add(entity);
                    items = new List <StorePurchaseOrderItem>(); // 重新分配一个
                }
                var product = _db.Table.Find <Product>(item.ProductId);
                if (model.OrderType == ValueObject.OrderType.Refund)
                {
                    //检查库存
                    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)
            {
                StorePurchaseOrder entity = new StorePurchaseOrder();
                entity.SetItems(items);
                entitys.Add(entity);
            }
            return(entitys);
        }