Beispiel #1
0
        public async Task <ReceiptInputsResult> SaveAsync(ReceiptSaveItem item, CancellationToken token = default(CancellationToken))
        {
            var items  = item.Receipts;
            var result = new List <ReceiptInput>();

            using (var scope = transactionScopeBuilder.Create())
            {
                var first           = items.First();
                var registered      = first.Id != 0L;
                var updateAll       = first.InputType == 2; // InputType = 2(入力)
                var saveKanaHistory = first.LearnKanaHistory;

                if (registered)
                {
                    result = new List <ReceiptInput>();
                    var input = updateAll
                        ? await updateReceiptQueryProcessor.UpdateReceiptInputAsync(first, token)
                        : await updateReceiptQueryProcessor.UpdateCustomerIdAsync(first, token);

                    if (input == null)
                    {
                        return(new ReceiptInputsResult
                        {
                            ProcessResult = new ProcessResult
                            {
                                ErrorCode = Rac.VOne.Common.ErrorCode.OtherUserAlreadyUpdated,
                                ErrorMessage = "データの取得後に、データの更新が発生しています",
                            },
                        });
                    }

                    result.Add(input);

                    if ((!updateAll) && saveKanaHistory && input.CustomerId.HasValue)
                    {
                        var kana = new KanaHistoryCustomer {
                            CompanyId        = input.CompanyId,
                            PayerName        = input.PayerName,
                            SourceBankName   = input.SourceBankName ?? string.Empty,
                            SourceBranchName = input.SourceBranchName ?? string.Empty,
                            CustomerId       = input.CustomerId.Value,
                            HitCount         = 1,
                            CreateBy         = input.CreateBy,
                            UpdateBy         = input.UpdateBy,
                        };

                        var exist = await kanaHistoryCustomerQueryProcessor.ExistAsync(kana, token);

                        if (!exist)
                        {
                            await addKanaHistoryCustomerQueryProcessor.SaveAsync(kana, token);
                        }
                    }
                }
                else
                {
                    result = (await addReceiptQueryProcessor.SaveReceiptInputAsync(items, token)).ToList();
                }

                for (int i = 0; i < result.Count; i++)
                {
                    var receiptId = result[i].Id;
                    var memo      = items[i].Memo;

                    if (!string.IsNullOrWhiteSpace(memo))
                    {
                        await addReceiptMemoQueryProcessor.SaveAsync(receiptId, memo, token);

                        result[i].Memo = memo;
                    }
                }

                if (item.ClientKey != null && item.ParentCustomerId.HasValue)
                {
                    for (int i = 0; i < result.Count; i++)
                    {
                        var r = result[i];
                        await matchingQueryProcessor.SaveWorkReceiptTargetAsync(item.ClientKey, r.Id,
                                                                                r.CompanyId, r.CurrencyId, r.PayerName, "", "", r.PayerCode, r.SourceBankName, r.SourceBranchName, r.CollationKey, r.CustomerId, token);

                        await matchingQueryProcessor.SaveWorkCollationAsync(item.ClientKey, r.Id, item.ParentCustomerId.Value, 0, r.CustomerId ?? 0,
                                                                            r.PayerName, r.PayerCode, "", "", r.SourceBankName, r.SourceBranchName, r.CollationKey, r.ReceiptAmount, token);
                    }
                }

                scope.Complete();
            }
            return(new ReceiptInputsResult
            {
                ProcessResult = new ProcessResult {
                    Result = true
                },
                ReceiptInputs = result,
            });
        }
Beispiel #2
0
 public async Task <ReceiptInputsResult> Save(ReceiptSaveItem items, CancellationToken token)
 => await receiptProcessor.SaveAsync(items, token);