private static async Task <bool> ImportAccountTransferResultAsync(string sessionKey,
                                                                          IEnumerable <AccountTransferImportData> importDataList,
                                                                          int?dueDateOffset,
                                                                          int?collectCategoryId)
        {
            AccountTransferImportResult result = null;

            await ServiceProxyFactory.LifeTime(async factory =>
            {
                var client = factory.Create <BillingService.BillingServiceClient>();
                result     = await client.ImportAccountTransferResultAsync(sessionKey, importDataList.ToArray(), dueDateOffset, collectCategoryId);
            });

            if (result == null || result.ProcessResult.Result == false)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        /// <summary>取込処理</summary>
        public async Task <AccountTransferImportResult> ReadAsync(AccountTransferImportSource source, CancellationToken token = default(CancellationToken))
        {
            var encoding = Encoding.GetEncoding(source.EncodingCodePage);
            var csv      = encoding.GetString(source.Data);

            var companyTask  = companyGetByIdQueryProcessor.GetByIdsAsync(new[] { source.CompanyId }, token);
            var currencyTask = currencyQueryProcessor.GetAsync(new CurrencySearch {
                CompanyId = source.CompanyId, Codes = new[] { DefaultCurrencyCode },
            }, token);
            var agencyTask = paymentAgencyGetByIdsQueryProcessor.GetByIdsAsync(new[] { source.PaymentAgencyId }, token);

            await Task.WhenAll(companyTask, currencyTask, agencyTask);

            var company  = companyTask.Result.First();
            var currency = currencyTask.Result.First();
            var agency   = agencyTask.Result.First();

            var helper = new Helper {
                GetBillingsAsync  = async(companyId, paymentAgencyId, dueAt) => (await billingQueryProcessor.GetAccountTransferMatchingTargetListAsync(paymentAgencyId, dueAt, currency.Id)).ToList(),
                GetCustomersAsync = async(ids) => (await customerQueryProcessor.GetAsync(new CustomerSearch {
                    Ids = ids,
                }, token)).ToDictionary(x => x.Id),
            };
            var reader = helper.CreateReader((AccountTransferFileFormatId)agency.FileFormatId);

            reader.CompanyId         = company.Id;
            reader.AggregateBillings = company.TransferAggregate == 1;
            reader.PaymentAgencyId   = agency.Id;
            reader.TransferYear      = source.TransferYear;
            reader.Encoding          = encoding;
            reader.FileName          = source.FileName;
            reader.IsAsync           = true;
            reader.IsPlainText       = true;

            var sources = await reader.ReadAsync(csv);

            var data = new ImportData {
                CompanyId = source.CompanyId,
                FileName  = source.FileName,
                FileSize  = encoding.GetByteCount(csv),
                CreateBy  = source.LoginUserId,
            };

            data.Details = sources.Select(x => new ImportDataDetail {
                ObjectType = 0,
                RecordItem = serializer.PackSingleObject(x),
            }).ToArray();

            var dataSaved = await importDataProcessor.SaveAsync(data, token);

            var result = new AccountTransferImportResult {
                ImportData    = dataSaved,
                ProcessResult = new ProcessResult {
                    Result = true
                },
            };

            for (var i = 0; i < sources.Count; i++)
            {
                result.ReadCount++;
                if (sources[i].TransferResultCode == 0)
                {
                    result.ValidCount++;
                    result.ValidAmount += sources[i].TransferAmount;
                }
                else
                {
                    result.InvalidCount++;
                    result.InvalidAmount += sources[i].TransferAmount;
                }
            }
            result.InvalidSources = sources.Where(x => x.TransferResultCode != 0 || !(x.Billings?.Any() ?? false)).ToList();
            result.Logs           = sources.SelectMany(x => x.GetInvalidLogs()).ToList();

            return(result);
        }