Exemplo n.º 1
0
        public Task <InvoiceNumberHistory> SaveAsync(InvoiceNumberHistory InvoiceNumberHistory, CancellationToken token = default(CancellationToken))
        {
            #region merge query
            var query = @"
MERGE INTO InvoiceNumberHistory target
USING (
    SELECT @CompanyId         [CompanyId]
         , @NumberingYear     [NumberingYear]
         , @NumberingMonth    [NumberingMonth]
         , @FixedString       [FixedString]
) source
ON    (
        target.CompanyId      = source.CompanyId
    AND target.NumberingYear  = source.NumberingYear
    AND target.NumberingMonth = source.NumberingMonth
    AND target.FixedString    = source.FixedString
)
WHEN MATCHED THEN
    UPDATE SET 
         LastNumber    = @LastNumber
        ,UpdateBy      = @UpdateBy
        ,UpdateAt      = GETDATE()
WHEN NOT MATCHED THEN 
    INSERT (CompanyId, NumberingYear, NumberingMonth, FixedString, LastNumber, CreateBy, CreateAt, UpdateBy, UpdateAt) 
    VALUES (@CompanyId, @NumberingYear, @NumberingMonth, @FixedString, @LastNumber, @UpdateBy, GETDATE(), @UpdateBy, GETDATE()) 
OUTPUT inserted.*; ";
            #endregion

            return(dbHelper.ExecuteAsync <InvoiceNumberHistory>(query, InvoiceNumberHistory, token));
        }
Exemplo n.º 2
0
        /// <summary>??? </summary>
        /// <param name="billing"></param>
        /// <returns></returns>
        private InvoiceNumberHistory GetKeysForNumberingHistory(BillingInvoiceForPublish billing)
        {
            var history = new InvoiceNumberHistory {
                CompanyId = billing.CompanyId
            };

            if (invoiceNumberSetting.ResetType == (int)ResetType.Max)
            {
                if (invoiceNumberSetting.FormatType == (int)FormatType.NumberAndFixedString &&
                    invoiceNumberSetting.FixedStringType == (int)FixedStringType.isFixed)
                {
                    history.FixedString = invoiceNumberSetting.FixedString;
                    return(history);
                }
                else if (invoiceNumberSetting.FormatType == (int)FormatType.NumberAndFixedString &&
                         invoiceNumberSetting.FixedStringType != (int)FixedStringType.isFixed)
                {
                    history.FixedString = billing.InvoiceTemplateFixedString;
                    return(history);
                }
                else if (invoiceNumberSetting.FormatType == (int)FormatType.OnlyNumber)
                {
                    return(history);
                }
            }

            var targetDate = invoiceNumberSetting.DateType == (int)DateType.BilledAt
            ? billing.BilledAt
            : billing.ClosingAt;
            var year  = targetDate.Year;
            var month = targetDate.Month;

            if (invoiceNumberSetting.ResetType == (int)(ResetType.Year))
            {
                //var targetYear = targetDate.Year;
                //var targetMonth = targetDate.Month;

                var x = (invoiceNumberSetting.ResetMonth.Value - 1);
                if (month <= x)
                {
                    history.NumberingYear = (year - 1).ToString();
                }
                else
                {
                    history.NumberingYear = targetDate.ToString("yyyy");
                }
                return(history);
            }
            else
            {
                history.NumberingYear  = year.ToString("0000");
                history.NumberingMonth = month.ToString("00");
                return(history);
            }
        }
Exemplo n.º 3
0
        private async Task <InvoiceNumberHistory> GetHistoryAsync(InvoiceNumberHistory condition, CancellationToken token)
        {
            var history = await invoiceNumberHistoryProcessor.GetAsync(condition, token);

            if (history == null)
            {
                history            = condition;
                history.LastNumber = 0;
            }
            return(history);
        }
Exemplo n.º 4
0
        public Task <InvoiceNumberHistory> GetAsync(InvoiceNumberHistory InvoiceNumberHistory, CancellationToken token = default(CancellationToken))
        {
            #region query
            var query = @"
SELECT [CompanyId]
     , [NumberingYear]
     , [NumberingMonth]
     , [FixedString]
     , [LastNumber]
     , [CreateBy]
     , [CreateAt]
     , [UpdateBy]
     , [UpdateAt]
 FROM InvoiceNumberHistory
WHERE CompanyId      = @CompanyId
  AND NumberingYear  = @NumberingYear
  AND NumberingMonth = @NumberingMonth
  AND FixedString    = @FixedString
";
            #endregion

            return(dbHelper.ExecuteAsync <InvoiceNumberHistory>(query, InvoiceNumberHistory, token));
        }
Exemplo n.º 5
0
 public async Task <ActionResult <InvoiceNumberHistory> > SaveInvoiceNumberHistory(InvoiceNumberHistory history, CancellationToken token)
 => await invoiceNumberHistoryProcessor.SaveAsync(history, token);
 public async Task <InvoiceNumberHistory> GetAsync(InvoiceNumberHistory InvoiceNumberHistory, CancellationToken token = default(CancellationToken))
 => await addInvoiceNumberHistoryQueryProcessor.GetAsync(InvoiceNumberHistory, token);
Exemplo n.º 7
0
 public async Task <InvoiceNumberHistoryResult> SaveInvoiceNumberHistoryAsync(string SessionKey, InvoiceNumberHistory InvoiceNumberHistory)
 {
     return(await authorizationProcessor.DoAuthorizeAsync(SessionKey, async token =>
     {
         var result = await invoiceNumberHistoryProcessor.SaveAsync(InvoiceNumberHistory, token);
         return new InvoiceNumberHistoryResult
         {
             ProcessResult = new ProcessResult {
                 Result = true
             },
             InvoiceNumberHistory = result,
         };
     }, logger));
 }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
        private string GetNewInvoiceCode(BillingInvoiceForPublish billing, InvoiceNumberHistory condition, InvoiceNumberHistory history)
        {
            var invoiceCode = "";

            //最大まで到達したらリセット
            if (history.LastNumber >= MaxNumber)
            {
                history.LastNumber = 1;
            }
            else
            {
                history.LastNumber++;
            }

            //前ゼロ処理
            if (invoiceNumberSetting.ZeroPadding == 0)
            {
                invoiceCode += history.LastNumber.ToString();
            }
            else
            {
                invoiceCode += history.LastNumber.ToString().PadLeft(invoiceNumberSetting.Length, '0');
            }

            //連番のみ
            if (invoiceNumberSetting.FormatType == (int)FormatType.OnlyNumber)
            {
                return(invoiceCode);
            }

            var formatString = string.Empty;

            //日付を使用
            if (invoiceNumberSetting.FormatType == (int)FormatType.NumberAndDate)
            {
                var targetDate = invoiceNumberSetting.DateType == (int)DateType.BilledAt
                ? billing.BilledAt
                : billing.ClosingAt;
                if (invoiceNumberSetting.DateFormat == (int)DateFormat.yyyy)
                {
                    formatString = condition.NumberingYear;
                }
                else
                {
                    formatString = condition.NumberingYear + condition.NumberingMonth;
                }
            }
            //固定文字を使用
            else
            {
                //完全固定
                if (invoiceNumberSetting.FixedStringType == (int)FixedStringType.isFixed)
                {
                    formatString = invoiceNumberSetting.FixedString;
                }
                //文面パターンごとに固定
                else
                {
                    formatString = billing.InvoiceTemplateFixedString;
                }
            }

            invoiceCode = GetDisplayFormat(invoiceCode, formatString);
            return(invoiceCode);
        }