Exemplo n.º 1
0
        public Task <BillingInput> UpdateForPublishAsync(BillingInputSource source, CancellationToken token = default(CancellationToken))
        {
            var query = new StringBuilder(@"
UPDATE BillingInput
SET PublishAt               = GETDATE()");

            if (source.IsFirstPublish)
            {
                query.Append(@"
  , PublishAt1st            = GETDATE()");
            }
            if (source.InvoiceTemplateId.HasValue)
            {
                query.AppendLine(@"
  , InvoiceTemplateId       = @InvoiceTemplateId");
            }
            if (source.UseInvoiceCodeNumbering.HasValue)
            {
                query.Append(@"
  , UseInvoiceCodeNumbering = @UseInvoiceCodeNumbering");
            }
            query.Append(@"
OUTPUT INSERTED.*
WHERE Id = @Id");
            return(dbHelper.ExecuteAsync <BillingInput>(query.ToString(), source, token));
        }
Exemplo n.º 2
0
        public async Task <CountResult> UpdatePublishAtAsync(long[] BillingInputIds,
                                                             CancellationToken token = default(CancellationToken))
        {
            using (var scope = transactionScopeBuilder.Create())
            {
                var result = new CountResult()
                {
                    ProcessResult = new ProcessResult(), Count = 0
                };

                await Task.Run(() => {
                    foreach (var id in BillingInputIds)
                    {
                        var billingInputSource = new BillingInputSource()
                        {
                            Id             = id,
                            IsFirstPublish = false
                        };
                        var billingInput = billingInputProcessor.UpdateForPublishAsync(billingInputSource);
                        if (billingInput != null)
                        {
                            result.Count++;
                        }
                    }
                    result.ProcessResult.Result = (result.Count == BillingInputIds.Count());
                });

                if (result.ProcessResult.Result)
                {
                    scope.Complete();
                }

                return(result);
            }
        }
Exemplo n.º 3
0
 public Task <BillingInput> UpdateForPublishAsync(BillingInputSource billingInputsource, CancellationToken token = default(CancellationToken))
 => addBillingInputQueryProcessor.UpdateForPublishAsync(billingInputsource, token);
Exemplo n.º 4
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);
            }
        }