public async Task <IActionResult> Post([FromBody] kc_store value)
 {
     value.CreatedAt = DateTime.Now;
     value.UpdatedAt = DateTime.Now;
     value.IsValid   = true;
     if (User.Identity is ClaimsIdentity identity)
     {
         value.CreatedBy = identity.Name ?? "test";
     }
     _kcStoreRpt.Add(value);
     _kcStoreRpt.Commit();
     return(new OkObjectResult(value));
 }
Exemple #2
0
        public async Task <IActionResult> Post([FromBody] StoreInDto value)
        {
            if (value.Storein != null && value.StoreinList != null)
            {
                if (_kcStoreinRpt.Exist(f => f.OrderNo == value.Storein.OrderNo))
                {
                    return(BadRequest($"入库单号{value.Storein.OrderNo}已经存在。"));
                }
                var storeIn = value.Storein;
                storeIn.CreatedAt = DateTime.Now;
                storeIn.UpdatedAt = DateTime.Now;
                storeIn.IsValid   = true;
                storeIn.Status    = "正常";
                if (User.Identity is ClaimsIdentity identity)
                {
                    storeIn.CreatedBy = identity.Name ?? "admin";
                }
                //storeIn.OrderNo = GetOrderNo();
                using (var tran = _context.Database.BeginTransaction())
                {
                    try
                    {
                        //入库单
                        _kcStoreinRpt.Add(storeIn);
                        _kcStoreinRpt.Commit();
                        foreach (var store in value.StoreinList)
                        {
                            if (_kcStoreinlistRpt.Exist(f => f.batchno == store.batchno && f.GoodsId == store.GoodsId))
                            {
                                throw new Exception($"批次号{store.batchno},产品ID{store.goodscode}已经存在");
                            }
                            //入库明细
                            store.orderno = storeIn.OrderNo;
                            _kcStoreinlistRpt.Add(store);
                            _kcStoreinlistRpt.Commit();

                            //更新库存(仓库,货位,产品)
                            var kucun = _kcStoreRpt.GetSingle(f =>
                                                              f.GoodsId == store.GoodsId && f.StoreId == storeIn.StoreId && f.BatchNo == store.batchno);
                            if (kucun == null)
                            {
                                var kcstore = new kc_store
                                {
                                    StoreId     = storeIn.StoreId,
                                    GoodsId     = store.GoodsId,
                                    Amount      = store.amount,
                                    Number      = store.number,
                                    CreatedAt   = DateTime.Now,
                                    UpdatedAt   = DateTime.Now,
                                    IsValid     = true,
                                    CreatedBy   = storeIn.CreatedBy,
                                    OrgId       = storeIn.OrgId,
                                    GoodsSite   = store.goodssite,
                                    GoodsTypeId = store.GoodsTypeId,
                                    BatchNo     = store.batchno
                                };
                                _kcStoreRpt.Add(kcstore);
                            }
                            else
                            {
                                kucun.Amount = kucun.Amount + store.amount;
                                kucun.Number = kucun.Number + store.number;
                            }
                            _kcStoreRpt.Commit();
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return(BadRequest(ex.Message));
                    }
                }
                return(new NoContentResult());
            }
            return(new BadRequestResult());
        }