public DataResult GetMaterialBill([FromQuery] int categoryId, int nameId, int specificationId, int siteId, int qId, bool menu)
        {
            var result = new DataResult();
            var data   = MaterialBillHelper.GetDetails(categoryId, nameId, specificationId, siteId, qId);

            if (qId != 0 && !data.Any())
            {
                return(Result.GenError <DataResult>(Error.MaterialBillNotExist));
            }

            if (menu)
            {
                result.datas.AddRange(data.Select(x => new
                {
                    x.Id,
                    x.Code,
                    x.Unit,
                    x.Price,
                    x.Stock,
                    x.Remark,
                    x.CategoryId,
                    x.Category,
                    x.NameId,
                    x.Name,
                    x.SpecificationId,
                    x.Specification,
                    x.SiteId,
                    x.Site
                }));
            }
            else
            {
                result.datas.AddRange(data);
            }
            return(result);
        }
예제 #2
0
        public object IncreaseMaterialPurchaseItem([FromBody] IEnumerable <MaterialPurchaseItem> purchaseItems)
        {
            if (purchaseItems == null)
            {
                return(Result.GenError <Result>(Error.ParamError));
            }

            purchaseItems = purchaseItems.Where(x => x.Count > 0);
            if (!purchaseItems.Any())
            {
                return(Result.GenError <Result>(Error.MaterialPurchaseItemNotExist));
            }

            if (purchaseItems.Any(x => x.Id == 0))
            {
                return(Result.GenError <Result>(Error.MaterialPurchaseItemNotExist));
            }

            if (purchaseItems.Count() != purchaseItems.GroupBy(x => x.Id).Count())
            {
                return(Result.GenError <Result>(Error.MaterialPurchaseItemDuplicate));
            }

            var purchaseItemIds  = purchaseItems.Select(x => x.Id).Distinct();
            var oldPurchaseItems = MaterialPurchaseItemHelper.Instance.GetByIds <MaterialPurchaseItem>(purchaseItemIds);

            if (oldPurchaseItems.Count() != purchaseItemIds.Count())
            {
                return(Result.GenError <Result>(Error.MaterialPurchaseItemNotExist));
            }
            if (oldPurchaseItems.Any(x => x.ErpId == 0))
            {
                return(Result.GenError <Result>(Error.MaterialPurchaseItemNotBuy));
            }

            var purchaseIds    = oldPurchaseItems.Select(x => x.PurchaseId).Distinct();
            var purchases      = MaterialPurchaseHelper.Instance.GetByIds <MaterialPurchase>(purchaseIds).ToDictionary(x => x.Id);
            var createUserId   = Request.GetIdentityInformation();
            var markedDateTime = DateTime.Now;
            var result         = new DataResult();

            #region 新编号
            var unknow = "无";
            foreach (var purchaseItem in oldPurchaseItems)
            {
                if ((purchaseItem.Class + purchaseItem.Category).IsNullOrEmpty())
                {
                    purchaseItem.Category = unknow;
                }
                if (purchaseItem.Name.IsNullOrEmpty())
                {
                    return(Result.GenError <Result>(Error.MaterialNameNotEmpty));
                }
                if (purchaseItem.Specification.IsNullOrEmpty())
                {
                    purchaseItem.Specification = unknow;
                }
                if (purchaseItem.Supplier.IsNullOrEmpty())
                {
                    purchaseItem.Supplier = unknow;
                }
            }

            var erpBills = oldPurchaseItems.Select(x =>
            {
                var purchase = purchases.ContainsKey(x.PurchaseId) ? purchases[x.PurchaseId] : null;
                return(new OpMaterial
                {
                    Id = x.Id,
                    ////借用,透传物料单id
                    BillId = x.BillId,
                    Code = x.Code,
                    Category = x.Category.IsNullOrEmpty() ? x.Class : x.Category,
                    Name = x.Name,
                    Supplier = x.Supplier,
                    Specification = x.Specification,
                    Site = "",
                    Price = x.Price,
                    Number = purchaseItems.FirstOrDefault(y => x.Id == y.Id)?.Count ?? 0,
                    Unit = x.Unit,
                    Remark = x.Remark,
                    File = x.File,
                    FileUrl = x.FileUrl,
                    RelatedPerson = purchase?.Name ?? "",
                    Purpose = $"Erp采购-{x.Order ?? ""}-{purchase?.ErpId.ToString() ?? ""}",
                });
            }).ToList();

            if (erpBills.Any(x => x.Code.IsNullOrEmpty()))
            {
                return(Result.GenError <Result>(Error.MaterialBillNotEmpty));
            }
            var codes    = erpBills.Select(x => x.Code).Distinct();
            var allBills = new Dictionary <int, Material>();
            allBills.AddRange(MaterialHelper.GetDetails(codes).ToDictionary(x => x.BillId));
            foreach (var bill in erpBills)
            {
                //var b = allBills.Values.FirstOrDefault(x => x.Code == bill.Code
                //                                    && x.Category == bill.Category
                //                                    && x.Name == bill.Name
                //                                    && x.Specification == bill.Specification);
                var b = allBills.Values.FirstOrDefault(x => x.Category == bill.Category &&
                                                       x.Name == bill.Name &&
                                                       x.Specification == bill.Specification);
                if (b == null)
                {
                    if (allBills.Values.Any(x => x.Code == bill.Code))
                    {
                        var bb     = allBills.Values.FirstOrDefault(x => x.Code == bill.Code);
                        var errmsg = $"货品编号异常,{bill.Code}";
                        if (bb.Category != bill.Category)
                        {
                            errmsg += $",类别:(100${bill.Category}$:222,${bb.Category}$)";
                        }
                        if (bb.Name != bill.Name)
                        {
                            errmsg += $",名称:(100,${bill.Name}$:222,${bb.Name}$)";
                        }
                        if (bb.Specification != bill.Specification)
                        {
                            errmsg += $",规格:(100,${bill.Specification}$:222,${bb.Specification}$)";
                        }
                        return(new
                        {
                            errno = 1,
                            errmsg,
                        });
                        //return Result.GenError<Result>(Error.MaterialCodeError);
                    }
                    bill.BillId = 0;
                    continue;
                }

                if (b.Code != bill.Code)
                {
                    result.datas.Add($"data:{b.BillId}, {b.Code}, erp: {bill.Code}");
                }
                if (b.BillId == 0)
                {
                    return(Result.GenError <Result>(Error.ParamError));
                }
                bill.BillId          = b.BillId;
                bill.Code            = b.Code;
                bill.CategoryId      = b.CategoryId;
                bill.NameId          = b.NameId;
                bill.SpecificationId = b.SpecificationId;
                bill.SiteId          = b.SiteId;
                bill.Site            = b.Site;
            }

            if (result.datas.Any())
            {
                result.errno = Error.MaterialCodeError;
                return(result);
            }
            #region 新货品
            var bills = erpBills.Where(x => x.CategoryId == 0 || x.NameId == 0 || x.SpecificationId == 0 || x.SupplierId == 0 || (x.SiteId == 0 && !x.Site.IsNullOrEmpty()) || x.BillId == 0);
            if (bills.Any())
            {
                #region 新位置
                var notExistSite = bills.Where(x => x.SiteId == 0 && !x.Site.IsNullOrEmpty());
                var newSites     = notExistSite.Select(x => x.Site).Distinct();
                if (newSites.Any())
                {
                    var data = MaterialSiteHelper.GetDetails(newSites).ToDictionary(x => x.Site);
                    if (data.Count() != newSites.Count())
                    {
                        MaterialSiteHelper.Instance.Add(newSites.Where(x => !data.ContainsKey(x)).Select(x => new MaterialSite
                        {
                            CreateUserId   = createUserId,
                            MarkedDateTime = markedDateTime,
                            Site           = x
                        }));
                        data = MaterialSiteHelper.GetDetails(newSites).ToDictionary(x => x.Site);
                    }
                    foreach (var bill in notExistSite)
                    {
                        var key = bill.Site;
                        if (data.ContainsKey(key))
                        {
                            bill.SiteId = data[key].Id;
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.MaterialSiteNotEmpty));
                        }
                    }
                }
                #endregion

                #region 新类别
                var notExistCategories = bills.Where(x => x.CategoryId == 0);
                var newCategories      = notExistCategories.GroupBy(x => x.Category).Select(y => y.Key);
                if (newCategories.Any())
                {
                    var data = MaterialCategoryHelper.GetDetails(newCategories).ToDictionary(x => x.Category);
                    if (data.Count() != newCategories.Count())
                    {
                        MaterialCategoryHelper.Instance.Add(newCategories.Where(x => !data.ContainsKey(x)).Select(x => new MaterialCategory
                        {
                            CreateUserId   = createUserId,
                            MarkedDateTime = markedDateTime,
                            Category       = x
                        }));
                        data = MaterialCategoryHelper.GetDetails(newCategories).ToDictionary(x => x.Category);
                    }
                    foreach (var bill in notExistCategories)
                    {
                        var key = bill.Category;
                        if (data.ContainsKey(key))
                        {
                            bill.CategoryId = data[key].Id;
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.MaterialCategoryNotEmpty));
                        }
                    }
                }
                #endregion

                #region 新名称
                var notExistNames = bills.Where(x => x.NameId == 0);
                var newNames      = notExistNames.Select(x => x.Name).Distinct();
                if (newNames.Any())
                {
                    var data = MaterialNameHelper.GetDetails(newNames).ToDictionary(x => x.Name);
                    if (data.Count() != newNames.Count())
                    {
                        MaterialNameHelper.Instance.Add(newNames.Where(x => !data.ContainsKey(x)).Select(x => new MaterialName
                        {
                            CreateUserId   = createUserId,
                            MarkedDateTime = markedDateTime,
                            Name           = x
                        }));
                        data = MaterialNameHelper.GetDetails(newNames).ToDictionary(x => x.Name);
                    }
                    foreach (var bill in notExistNames)
                    {
                        var key = bill.Name;
                        if (data.ContainsKey(key))
                        {
                            bill.NameId = data[key].Id;
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.MaterialNameNotEmpty));
                        }
                    }
                }

                #endregion

                #region 新规格
                var notExistSpecifications = bills.Where(x => x.SpecificationId == 0);
                var newSpecifications      = notExistSpecifications.Select(x => x.Specification).Distinct();
                if (newSpecifications.Any())
                {
                    var data = MaterialSpecificationHelper.GetDetails(newSpecifications).ToDictionary(x => x.Specification);
                    if (data.Count() != newSpecifications.Count())
                    {
                        MaterialSpecificationHelper.Instance.Add(newSpecifications.Where(x => !data.ContainsKey(x)).Select(x => new MaterialSpecification
                        {
                            CreateUserId   = createUserId,
                            MarkedDateTime = markedDateTime,
                            Specification  = x
                        }));
                        data = MaterialSpecificationHelper.GetDetails(newSpecifications).ToDictionary(x => x.Specification);
                    }
                    foreach (var bill in notExistSpecifications)
                    {
                        var key = bill.Specification;
                        if (data.ContainsKey(key))
                        {
                            bill.SpecificationId = data[key].Id;
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.MaterialSpecificationNotEmpty));
                        }
                    }
                }

                #endregion

                #region 供应商
                var notExistSuppliers = bills.Where(x => x.SupplierId == 0);
                var newSuppliers      = notExistSuppliers.Select(x => x.Supplier).Distinct();
                if (newSuppliers.Any())
                {
                    var data = MaterialSupplierHelper.GetDetails(newSuppliers).ToDictionary(x => x.Supplier);
                    if (data.Count() != newSuppliers.Count())
                    {
                        MaterialSupplierHelper.Instance.Add(newSuppliers.Where(x => !data.ContainsKey(x)).Select(x => new MaterialSupplier
                        {
                            CreateUserId   = createUserId,
                            MarkedDateTime = markedDateTime,
                            Supplier       = x
                        }));
                        data = MaterialSupplierHelper.GetDetails(newSuppliers).ToDictionary(x => x.Supplier);
                    }
                    foreach (var bill in notExistSuppliers)
                    {
                        var key = bill.Supplier;
                        if (data.ContainsKey(key))
                        {
                            bill.SupplierId = data[key].Id;
                        }
                        else
                        {
                            return(Result.GenError <Result>(Error.MaterialSupplierNotEmpty));
                        }
                    }
                }

                #endregion

                #region 新编号
                var billI_0s = bills.Where(x => x.BillId == 0);
                if (billI_0s.Any())
                {
                    var errCodes = bills.GroupBy(x => new { x.CategoryId, x.NameId, x.SpecificationId }).Where(x => x.Count() > 1);
                    if (errCodes.Any())
                    {
                        return(Result.GenError <Result>(Error.MaterialCodeError));
                    }
                    MaterialBillHelper.Instance.Add(billI_0s.Select(x => new MaterialBill
                    {
                        CreateUserId    = createUserId,
                        MarkedDateTime  = markedDateTime,
                        Code            = x.Code,
                        CategoryId      = x.CategoryId,
                        NameId          = x.NameId,
                        SpecificationId = x.SpecificationId,
                        Unit            = x.Unit,
                        Stock           = x.Stock,
                        Remark          = x.Remark ?? "",
                        Images          = x.Images ?? "[]",
                        File            = x.File ?? "",
                        FileUrl         = x.FileUrl ?? "",
                    }));

                    var code_0s = billI_0s.Select(x => x.Code).Distinct();
                    var bill_0s = MaterialBillHelper.GetDetails(code_0s).ToDictionary(x => x.Code);
                    foreach (var bill in billI_0s)
                    {
                        var key = bill.Code;
                        if (bill_0s.ContainsKey(key))
                        {
                            bill.BillId = bill_0s[key].Id;
                        }
                    }

                    allBills.AddRange(bill_0s.Values.ToDictionary(x => x.Id, x =>
                    {
                        var y    = ClassExtension.CopyTo <MaterialBillDetail, Material>(x);
                        y.BillId = x.Id;
                        y.Exist  = false;
                        return(y);
                    }));
                }
                #endregion
            }
            #endregion

            #endregion

            var originBatch = MaterialBatchHelper.GetBatch(createUserId, markedDateTime);
            var addBatches  = erpBills.GroupBy(x => new { x.BillId, x.SupplierId }).Select(x =>
            {
                var bill               = x.First();
                var price              = bill.Price;
                originBatch.BillId     = x.Key.BillId;
                originBatch.Price      = price;
                originBatch.SupplierId = x.Key.SupplierId;
                bill.Number            = x.Sum(a => a.Number);
                return(new MaterialBatch(originBatch, bill));
            });

            MaterialBatchHelper.Instance.Add(addBatches);

            var bIds       = addBatches.Select(x => x.BillId).Distinct();
            var sIds       = addBatches.Select(x => x.SupplierId).Distinct();
            var newBatches = MaterialBatchHelper.GetDetails(originBatch.BatchId, bIds, sIds)
                             .ToDictionary(x => new Tuple <int, int, int>(x.BatchId, x.BillId, x.SupplierId));

            #region 更新
            var existBill = allBills.Where(x => erpBills.Any(y => y.BillId == x.Key) && x.Value.Exist).ToDictionary(x => x.Key, x => x.Value);
            if (existBill.Any())
            {
                foreach (var(key, bill) in existBill)
                {
                    bill.OldNumber = bill.Number;
                    bill.Number   += erpBills.Where(x => x.BillId == bill.Id).Sum(y => y.Number);
                    bill.InTime    = markedDateTime;
                }
                MaterialHelper.Increase(existBill.Values);
            }
            #endregion

            #region 添加
            var addBill = erpBills.Where(x => !existBill.ContainsKey(x.BillId));
            if (addBill.Any())
            {
                var billIds = addBill.GroupBy(y => y.BillId);
                MaterialHelper.Instance.Add(billIds.Select(
                                                x => new
                {
                    BillId = x.Key,
                    InTime = markedDateTime,
                    Number = addBill.Where(z => z.BillId == x.Key).Sum(a => a.Number)
                }));
            }
            #endregion

            foreach (var erpBill in erpBills)
            {
                var oldMaterialPurchaseItem = oldPurchaseItems.FirstOrDefault(x => x.Id == erpBill.Id);
                if (oldMaterialPurchaseItem != null)
                {
                    oldMaterialPurchaseItem.BillId   = erpBill.BillId;
                    oldMaterialPurchaseItem.ThisCode = erpBill.Code;
                }
            }
            var logs = erpBills.Select(x =>
            {
                x.Type      = MaterialOp.入库;
                var batches = new List <MaterialBatch>();
                var batch   = newBatches[new Tuple <int, int, int>(originBatch.BatchId, x.BillId, x.SupplierId)];
                batches.Add(batch);
                var log = new MaterialLog(createUserId, markedDateTime, x.Type, x.Purpose ?? $"Erp采购", x.Number,
                                          allBills[x.BillId].Number, x.RelatedPerson, createUserId, allBills[x.BillId], batches);
                log.ItemId = x.Id;
                return(log);
            }).ToList();

            foreach (var item in oldPurchaseItems)
            {
                var purchaseItem = purchaseItems.FirstOrDefault(x => x.Id == item.Id);
                if (purchaseItem != null)
                {
                    if (item.Stock == 0)
                    {
                        item.IncreaseTime = markedDateTime;
                    }

                    item.MarkedDateTime = markedDateTime;
                    item.Stock         += purchaseItem.Count;
                    item.Count          = purchaseItem.Count;
                }
            }
            MaterialPurchaseItemHelper.Increase(oldPurchaseItems);
            if (allBills.Keys.Any())
            {
                HMaterialHelper.UpdateBillPrice(allBills.Keys);
            }
            Task.Run(() =>
            {
                if (logs.Any())
                {
                    MaterialLogHelper.Instance.Add(logs);
                }
            });
            //TimerHelper.MaterialRecovery(true);
            result.datas.AddRange(newBatches.Values.Select(x => new
            {
                x.Time,
                x.Id,
                x.BillId,
                x.Batch,
                x.Supplier,
                x.Price,
                allBills[x.BillId].Code,
                allBills[x.BillId].Name,
                allBills[x.BillId].Specification,
                allBills[x.BillId].Category,
                allBills[x.BillId].Site
            }));

            return(result);
        }