Esempio n. 1
0
        private void ImportAccountTitle()
        {
            try
            {
                ClearStatusMessage();

                ImportSetting importSetting = null;
                // 取込設定取得
                var task = Util.GetMasterImportSettingAsync(Login, ImportFileType.AccountTitle);
                ProgressDialog.Start(ParentForm, task, false, SessionKey);
                importSetting = task.Result;

                var definition = new AccountTitleFileDefinition(new DataExpression(ApplicationControl));

                definition.AccountTitleCodeField.ValidateAdditional = (val, param) =>
                {
                    var reports = new List <WorkingReport>();
                    if (((ImportMethod)param) != ImportMethod.Replace)
                    {
                        return(reports);
                    }

                    MasterDatasResult categoryResult      = null;
                    MasterDatasResult customerResult      = null;
                    MasterDatasResult debitBillingResult  = null;
                    MasterDatasResult creditBillingResult = null;
                    ServiceProxyFactory.LifeTime(factory =>
                    {
                        var accountTitle    = factory.Create <AccountTitleMasterClient>();
                        var codes           = val.Values.Where(x => !string.IsNullOrEmpty(x.Code)).Select(x => x.Code).Distinct().ToArray();
                        categoryResult      = accountTitle.GetImportItemsForCategory(SessionKey, CompanyId, codes);
                        customerResult      = accountTitle.GetImportItemsForCustomerDiscount(SessionKey, CompanyId, codes);
                        debitBillingResult  = accountTitle.GetImportItemsForDebitBilling(SessionKey, CompanyId, codes);
                        creditBillingResult = accountTitle.GetImportItemsForCreditBilling(SessionKey, CompanyId, codes);
                    });

                    foreach (MasterData ca in categoryResult.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code)))
                    {
                        reports.Add(new WorkingReport
                        {
                            LineNo    = null,
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"区分マスターに存在する{ca.Code}:{ca.Name}が存在しないため、インポートできません。",
                        });
                    }

                    foreach (MasterData ca in customerResult.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code)))
                    {
                        reports.Add(new WorkingReport
                        {
                            LineNo    = null,
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"得意先マスター歩引設定に存在する{ca.Code}:{ca.Name}が存在しないため、インポートできません。",
                        });
                    }

                    foreach (MasterData ca in debitBillingResult.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code)))
                    {
                        reports.Add(new WorkingReport
                        {
                            LineNo    = null,
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"請求データに存在する{ca.Code}:{ca.Name}が存在しないため、インポートできません。",
                        });
                    }

                    foreach (MasterData ca in creditBillingResult.MasterDatas.Where(p => !val.Any(a => a.Value.Code == p.Code)))
                    {
                        reports.Add(new WorkingReport
                        {
                            LineNo    = null,
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"請求データに存在する{ca.Code}:{ca.Name}が存在しないため、インポートできません。",
                        });
                    }
                    return(reports);
                };

                var importer = definition.CreateImporter(m => m.Code);
                importer.UserId      = Login.UserId;
                importer.UserCode    = Login.UserCode;
                importer.CompanyId   = CompanyId;
                importer.CompanyCode = Login.CompanyCode;
                importer.LoadAsync   = async() => await LoadGridAsync();

                importer.RegisterAsync = async unitOfWork => await RegisterForImportAsync(unitOfWork);

                System.Action clear = () =>
                {
                    AccountTitleList.Clear();
                    var loadTask = LoadGridAsync();
                    ProgressDialog.Start(ParentForm, loadTask, false, SessionKey);
                    AccountTitleList.AddRange(loadTask.Result);
                    grdAccountTitleMaster.DataSource = new BindingSource(AccountTitleList, null);
                };
                var importResult = DoImport(importer, importSetting, clear);
                if (!importResult)
                {
                    return;
                }
                txtCode.Clear();
                txtName.Clear();
                txtContraAccountCode.Clear();
                txtContraAccountName.Clear();
                txtContraAccountSubCode.Clear();
                AccountTitleId     = 0;
                txtCode.Enabled    = true;
                this.ActiveControl = txtCode;
                BaseContext.SetFunction03Enabled(false);
                Modified = false;
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
                ShowWarningDialog(MsgErrImportErrorWithoutLog);
            }
        }
Esempio n. 2
0
        private void ExportAccountTitle()
        {
            try
            {
                ClearStatusMessage();
                List <AccountTitle> list = null;
                var listTask             = ServiceProxyFactory.LifeTime(async factory =>
                {
                    var accountTitleMaster     = factory.Create <AccountTitleMasterClient>();
                    AccountTitlesResult result = await accountTitleMaster.GetItemsAsync(
                        SessionKey, new AccountTitleSearch {
                        CompanyId = CompanyId
                    });

                    if (result.ProcessResult.Result)
                    {
                        list = result.AccountTitles;
                    }
                });

                var pathTask = Util.GetGeneralSettingServerPathAsync(Login);
                ProgressDialog.Start(ParentForm, Task.WhenAll(listTask, pathTask), false, SessionKey);
                var serverPath = pathTask.Result;

                if (!list.Any())
                {
                    ShowWarningDialog(MsgWngNoExportData);
                    return;
                }

                if (!Directory.Exists(serverPath))
                {
                    serverPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                }

                var filePath = string.Empty;
                var fileName = $"科目マスター{DateTime.Today:yyyyMMdd}.csv";
                if (!ShowSaveExportFileDialog(serverPath, fileName, out filePath))
                {
                    return;
                }

                var definition = new AccountTitleFileDefinition(new DataExpression(ApplicationControl));
                var exporter   = definition.CreateExporter();
                exporter.UserId      = Login.UserId;
                exporter.UserCode    = Login.UserCode;
                exporter.CompanyId   = CompanyId;
                exporter.CompanyCode = Login.CompanyCode;

                ProgressDialog.Start(ParentForm, (cancel, progress) =>
                {
                    return(exporter.ExportAsync(filePath, list, cancel, progress));
                }, true, SessionKey);

                if (exporter.Exception != null)
                {
                    NLogHandler.WriteErrorLog(this, exporter.Exception, SessionKey);
                    ShowWarningDialog(MsgErrExportError);
                    return;
                }

                DispStatusMessage(MsgInfFinishExport);
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
                DispStatusMessage(MsgErrExportError);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// インポート処理
        /// </summary>
        /// <param name="source"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <ImportResult> ImportAsync(MasterImportSource source, CancellationToken token = default(CancellationToken))
        {
            var mode     = (ImportMethod)source.ImportMethod;
            var encoding = Encoding.GetEncoding(source.EncodingCodePage);
            var csv      = encoding.GetString(source.Data);

            var companyTask = companyProcessor.GetAsync(new CompanySearch {
                Id = source.CompanyId,
            }, token);
            var loginUserTask = loginUserProcessor.GetAsync(new LoginUserSearch {
                Ids = new[] { source.LoginUserId },
            }, token);
            var appConTask = applicationControlProcessor.GetAsync(source.CompanyId, token);

            await Task.WhenAll(companyTask, loginUserTask, appConTask);

            var company            = companyTask.Result.First();
            var loginUser          = loginUserTask.Result.First();
            var applicationControl = appConTask.Result;
            var definition         = new AccountTitleFileDefinition(new DataExpression(applicationControl));

            var parser = new CsvParser {
                Encoding      = encoding,
                StreamCreator = new PlainTextMemoryStreamCreator(),
            };

            var importer = definition.CreateImporter(m => m.Code, parser);

            importer.UserId      = source.LoginUserId;
            importer.UserCode    = loginUser.Code;
            importer.CompanyId   = source.CompanyId;
            importer.CompanyCode = company.Code;
            if (mode == ImportMethod.Replace)
            {
                importer.AdditionalWorker = async worker => {
                    var codes                 = worker.Models.Values.Select(x => x.Code).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray();
                    var categoryItemTask      = accountTitleProcessor.GetImportItemsCategoryAsync(source.CompanyId, codes, token);
                    var customerItemTask      = accountTitleProcessor.GetImportItemsCustomerDiscountAsync(source.CompanyId, codes, token);
                    var debitBillingItemTask  = accountTitleProcessor.GetImportItemsDebitBillingAsync(source.CompanyId, codes, token);
                    var creditBillingItemTask = accountTitleProcessor.GetImportItemsCreditBillingAsync(source.CompanyId, codes, token);

                    await Task.WhenAll(categoryItemTask, customerItemTask, debitBillingItemTask, creditBillingItemTask);


                    (worker.RowDef as AccountTitleFileDefinition).AccountTitleCodeField.ValidateAdditional = (val, param) => {
                        var reports = new List <WorkingReport>();
                        reports.AddRange(categoryItemTask.Result.Select(x => new WorkingReport
                        {
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"区分マスターに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。",
                        }).ToArray());

                        reports.AddRange(customerItemTask.Result.Select(x => new WorkingReport
                        {
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"得意先マスター歩引設定に存在する{x.Code}:{x.Name}が存在しないため、インポートできません。",
                        }).ToArray());

                        reports.AddRange(debitBillingItemTask.Result.Select(x => new WorkingReport
                        {
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"請求データに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。",
                        }).ToArray());

                        reports.AddRange(creditBillingItemTask.Result.Select(x => new WorkingReport
                        {
                            FieldNo   = definition.AccountTitleCodeField.FieldIndex,
                            FieldName = definition.AccountTitleCodeField.FieldName,
                            Message   = $"請求データに存在する{x.Code}:{x.Name}が存在しないため、インポートできません。",
                        }).ToArray());

                        return(reports);
                    };
                }
            }
            ;

            importer.LoadAsync = () => accountTitleProcessor.GetAsync(new AccountTitleSearch {
                CompanyId = source.CompanyId,
            }, token);
            importer.RegisterAsync = x => accountTitleProcessor.ImportAsync(x.New, x.Dirty, x.Removed, token);

            var result = await importer.ImportAsync(csv, mode, token, null);

            result.Logs = importer.GetErrorLogs();

            return(result);
        }
    }