示例#1
0
        public async Task <BillingInputResult> PublishInvoicesAsync(BillingInvoiceForPublish[] invoices,
                                                                    int loginUserId,
                                                                    CancellationToken token = default(CancellationToken))
        {
            using (var scope = transactionScopeBuilder.Create())
            {
                var result = new BillingInputResult {
                    BillingInputIds = new List <long>(),
                    ProcessResult   = new ProcessResult(),
                };

                var first = invoices.First();

                // refactor private variable to local variable
                invoiceNumberSetting = await invoiceNumberSettingProcessor.GetAsync(first.CompanyId, token);

                var useNumbering = (invoiceNumberSetting?.UseNumbering == 1);
                if (useNumbering)
                {
                    MaxNumber = Convert.ToInt64("999999999999999".Substring(0, invoiceNumberSetting.Length));
                }

                //同時実行制御
                //最終更新日時をチェック
                var tempIds     = invoices.Select(x => x.TemporaryBillingInputId).ToArray();
                var maxUpdateAt = await billingInvoiceQueryProcessor.GetMaxUpdateAtAsync(first.ClientKey, tempIds, token);

                var maxUpdateAtSource = invoices.Max(x => x.UpdateAt);
                if (!maxUpdateAt.HasValue || maxUpdateAtSource != maxUpdateAt)
                {
                    result.BillingInputIds         = null;
                    result.ProcessResult.ErrorCode = ErrorCode.OtherUserAlreadyUpdated;
                    return(result);
                }

                foreach (BillingInvoiceForPublish invoice in invoices)
                {
                    var doUpdateInvoiceCode      = false;
                    InvoiceNumberHistory history = null;
                    if (useNumbering &&
                        string.IsNullOrEmpty(invoice.InvoiceCode))
                    {
                        doUpdateInvoiceCode = true;
                        //自動採番の請求書番号を取得
                        var condition = GetKeysForNumberingHistory(invoice);

                        history = await GetHistoryAsync(condition, token);

                        invoice.InvoiceCode = GetNewInvoiceCode(invoice, condition, history);
                    }

                    if (invoice.BillingInputId == null || invoice.BillingInputId == 0)
                    {
                        //InputId新規生成
                        invoice.BillingInputId = billingInputProcessor.SaveAsync().Id;
                        //請求データ側のInputIdも更新
                        await billingInvoiceQueryProcessor.UpdateBillingForPublishNewInputId(invoice, doUpdateInvoiceCode, token);
                    }
                    else
                    {
                        await billingProcessor.UpdateBillingForPublishAsync(invoice, doUpdateInvoiceCode, token);
                    }

                    //発行済みフラグの更新
                    var billingInputSource = new BillingInputSource {
                        Id                      = invoice.BillingInputId.Value,
                        IsFirstPublish          = true,
                        UseInvoiceCodeNumbering = doUpdateInvoiceCode ? 1 : 0,
                        InvoiceTemplateId       = invoice.InvoiceTemplateId
                    };
                    var resultBillingInput = billingInputProcessor.UpdateForPublishAsync(billingInputSource);

                    if (invoice.BillingInputId.HasValue && resultBillingInput != null)
                    {
                        result.BillingInputIds.Add(invoice.BillingInputId.Value);
                    }

                    //自動採番履歴を更新
                    if (history != null)
                    {
                        history.UpdateBy = loginUserId;
                        await invoiceNumberHistoryProcessor.SaveAsync(history, token);
                    }
                }

                if (result.BillingInputIds.Count != invoices.Count())
                {
                    return(null);
                }

                scope.Complete();

                result.ProcessResult.Result = true;
                return(result);
            }
        }