使用C#将日期转换为会计期间

时间:2014-01-31 09:06:48

标签: dynamics-crm-2011 dynamics-crm dynamics-crm-2013

是否有标准或方便的方法来获取自定义工作流活动中的当前财务周期并使用它将日历日期字段转换为其财务日期?

财政期间是每月 手动从日期字段中删除月份以获得财务日期实际上不是一种选择。

我找到了这个集合,但它是空的:

XRMServices/2011/OrganizationData.svc/MonthlyFiscalCalendarSet

1 个答案:

答案 0 :(得分:0)

我发现了这个

<d:FiscalCalendarStart m:type="Edm.DateTime">
    2013-10-01T10:59:00Z
</d:FiscalCalendarStart>

/XRMServices/2011/OrganizationData.svc/OrganizationSet

此方法从日历日期返回会计期间(作为月份):

/* returns month, year */
private Tuple<int, int> GetFinancialDate(WorkflowContext context, DateTime? date)
{
    var start = context.Linq.OrganizationSet
        .FirstOrDefault().FiscalCalendarStart;
    if (date.HasValue && start.HasValue)
    {
        var localDate = date.Value.ToLocalTime();
        var month = localDate.Month - start.Value.ToLocalTime().Month + 1; // +1 because not 0 based
        var year = (localDate.Month == 1) ? localDate.Year : localDate.Year + 1; // if it is Jan then do not move the year forward - move forward for Feb onward
        if (month <= 0) // the date is in the previous financial year, 0 would be the last period of the previous fiscal year
        {
            --year; // move to the previous year
            month = month + 12; // month is negative or 0 before this, so is 12 or less after
        }
        return Tuple.Create(month, year);
    }
    return Tuple.Create(0, 0);
}