RoussKS / financial-year

A PHP library to handle different financial year date requirements
MIT License
8 stars 0 forks source link

If I select type "calendar" and use this function "getBusinessWeekIdIdByDate" to Exception generate #24

Closed ImAnandPanchal closed 1 year ago

ImAnandPanchal commented 1 year ago
public function getFinancialYearWeek($date) : int
    {
        $date = Carbon::parse($date);

        // Set the start of the tax year (6th April)
        $taxYearStart = Carbon::createFromDate($date->year, 4, 6);

        // Adjust the year if the input date falls before the tax year start
        if ($date->lessThan($taxYearStart)) {
            $taxYearStart->subYear();
        }

        $fy = new \RoussKS\FinancialYear\DateTimeAdapter('calendar', $taxYearStart->toDateString(), true); // false for 52 weeks, true for 53 weeks

        try {
            $week = $fy->getBusinessWeekIdIdByDate($date->toDateString()); // 5
        } catch (\Exception $e) {
            info("Exception" . $e->getMessage());
            return 0;
        }

        return $week;
    } 

Exception Message = "A business week could not be found for the specified date."

RoussKS commented 1 year ago

@ImAnandPanchal apologies for the delayed response.

The method is not expected to be used with calendar type year, as the number of weeks is not set/predefined. So this is not a valid use case.

However, there should be some validation logic to check if business type is set as with other business specific methods.

Thanks for raising.

ImAnandPanchal commented 1 year ago

@RoussKS Thanks for reply. If I use business type with above same code than fyEndDate will be generated wrong. For example, $taxYearStart = '2022-04-06' If I get getFyEndDate().. it will return '2023-04-04'.

RoussKS commented 1 year ago

@RoussKS Thanks for reply. If I use business type with above same code than fyEndDate will be generated wrong. For example, $taxYearStart = '2022-04-06' If I get getFyEndDate().. it will return '2023-04-04'.

@ImAnandPanchal this is expected behaviour because you use the calendar type named business. This has either 52 (default) or 53 weeks. So when business type is loaded with 52 weeks, the year financial year will have 364 days.

If you expected to have 2023-04-05, it means you need the calendar financial year type and not the business one.

ps. For issues other than the current one, can you please create a new Issue instead of replying on the same thread?

RoussKS commented 1 year ago

Closed by #26 Released with version 1.2.1