xref: /webtrees/app/Services/CalendarService.php (revision a69f5655040bc7cc0734a54d98f1eda262ddd292)
1f0a11419SGreg Roach<?php
2f0a11419SGreg Roach/**
3f0a11419SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify
6f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by
7f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8f0a11419SGreg Roach * (at your option) any later version.
9f0a11419SGreg Roach * This program is distributed in the hope that it will be useful,
10f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12f0a11419SGreg Roach * GNU General Public License for more details.
13f0a11419SGreg Roach * You should have received a copy of the GNU General Public License
14f0a11419SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15f0a11419SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
18f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services;
19f0a11419SGreg Roach
204a9ac934SGreg Roachuse Fisharebest\ExtCalendar\PersianCalendar;
21f0a11419SGreg Roachuse Fisharebest\Webtrees\Date;
2225d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
28f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
29f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact;
30f0a11419SGreg Roachuse Fisharebest\Webtrees\Family;
31f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
32f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual;
33f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree;
349e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
359e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder;
36*a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
379e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause;
388af3e5c1SGreg Roachuse Illuminate\Support\Collection;
390a614d28SGreg Roachuse function preg_match_all;
400a614d28SGreg Roachuse function range;
41f0a11419SGreg Roach
42f0a11419SGreg Roach/**
43f0a11419SGreg Roach * Calculate anniversaries, etc.
44f0a11419SGreg Roach */
45f0a11419SGreg Roachclass CalendarService
46f0a11419SGreg Roach{
470a614d28SGreg Roach    // If no facts specified, get all except these
480a614d28SGreg Roach    protected const SKIP_FACTS = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR', '_TODO'];
490a614d28SGreg Roach
50f0a11419SGreg Roach    /**
5170d8159dSGreg Roach     * List all the months in a given year.
5270d8159dSGreg Roach     *
5370d8159dSGreg Roach     * @param string $calendar
5470d8159dSGreg Roach     * @param int    $year
5570d8159dSGreg Roach     *
5670d8159dSGreg Roach     * @return string[]
5770d8159dSGreg Roach     */
5870d8159dSGreg Roach    public function calendarMonthsInYear(string $calendar, int $year): array
5970d8159dSGreg Roach    {
6070d8159dSGreg Roach        $date          = new Date($calendar . ' ' . $year);
6170d8159dSGreg Roach        $calendar_date = $date->minimumDate();
6270d8159dSGreg Roach        $month_numbers = range(1, $calendar_date->monthsInYear());
6370d8159dSGreg Roach        $month_names   = [];
6470d8159dSGreg Roach
6570d8159dSGreg Roach        foreach ($month_numbers as $month_number) {
664a83f5d7SGreg Roach            $calendar_date->day   = 1;
674a83f5d7SGreg Roach            $calendar_date->month = $month_number;
6870d8159dSGreg Roach            $calendar_date->setJdFromYmd();
6970d8159dSGreg Roach
7070d8159dSGreg Roach            if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
7170d8159dSGreg Roach                // No month 6 in Jewish non-leap years.
7270d8159dSGreg Roach                continue;
7370d8159dSGreg Roach            }
7470d8159dSGreg Roach
7570d8159dSGreg Roach            if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
7670d8159dSGreg Roach                // Month 7 is ADR in Jewish non-leap years (and ADS in others).
7770d8159dSGreg Roach                $mon = 'ADR';
7870d8159dSGreg Roach            } else {
7970d8159dSGreg Roach                $mon = $calendar_date->format('%O');
8070d8159dSGreg Roach            }
8170d8159dSGreg Roach
8270d8159dSGreg Roach            $month_names[$mon] = $calendar_date->format('%F');
8370d8159dSGreg Roach        }
8470d8159dSGreg Roach
8570d8159dSGreg Roach        return $month_names;
8670d8159dSGreg Roach    }
8770d8159dSGreg Roach
8870d8159dSGreg Roach    /**
89f0a11419SGreg Roach     * Get a list of events which occured during a given date range.
90f0a11419SGreg Roach     *
91f0a11419SGreg Roach     * @param int    $jd1   the start range of julian day
92f0a11419SGreg Roach     * @param int    $jd2   the end range of julian day
930a614d28SGreg Roach     * @param string $facts restrict the search to just these facts or leave blank for all
94f0a11419SGreg Roach     * @param Tree   $tree  the tree to search
95f0a11419SGreg Roach     *
96f0a11419SGreg Roach     * @return Fact[]
97f0a11419SGreg Roach     */
980a614d28SGreg Roach    public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array
99f0a11419SGreg Roach    {
100f0a11419SGreg Roach        // Events that start or end during the period
10125d4b688SGreg Roach        $query = DB::table('dates')
1029e87c1a2SGreg Roach            ->where('d_file', '=', $tree->id())
1030b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($jd1, $jd2): void {
1040b5fd0a6SGreg Roach                $query->where(static function (Builder $query) use ($jd1, $jd2): void {
1059e87c1a2SGreg Roach                    $query
1069e87c1a2SGreg Roach                        ->where('d_julianday1', '>=', $jd1)
1079e87c1a2SGreg Roach                        ->where('d_julianday1', '<=', $jd2);
1080b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query) use ($jd1, $jd2): void {
1099e87c1a2SGreg Roach                    $query
1109e87c1a2SGreg Roach                        ->where('d_julianday2', '>=', $jd1)
1119e87c1a2SGreg Roach                        ->where('d_julianday2', '<=', $jd2);
1129e87c1a2SGreg Roach                });
1139e87c1a2SGreg Roach            });
114f0a11419SGreg Roach
115f0a11419SGreg Roach        // Restrict to certain types of fact
1160a614d28SGreg Roach        if ($facts === '') {
1170a614d28SGreg Roach            $query->whereNotIn('d_fact', self::SKIP_FACTS);
118f0a11419SGreg Roach        } else {
1190a614d28SGreg Roach            preg_match_all('/([_A-Z]+)/', $facts, $matches);
1200a614d28SGreg Roach
1210a614d28SGreg Roach            $query->whereIn('d_fact', $matches[1]);
122f0a11419SGreg Roach        }
123f0a11419SGreg Roach
12425d4b688SGreg Roach        $ind_query = (clone $query)
1250b5fd0a6SGreg Roach            ->join('individuals', static function (JoinClause $join): void {
12625d4b688SGreg Roach                $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
12725d4b688SGreg Roach            })
12825d4b688SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
129f0a11419SGreg Roach
13025d4b688SGreg Roach        $fam_query = (clone $query)
1310b5fd0a6SGreg Roach            ->join('families', static function (JoinClause $join): void {
13225d4b688SGreg Roach                $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
13325d4b688SGreg Roach            })
13425d4b688SGreg Roach            ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
13525d4b688SGreg Roach
13625d4b688SGreg Roach        // Now fetch these events
13725d4b688SGreg Roach        $found_facts = [];
13825d4b688SGreg Roach
13925d4b688SGreg Roach        foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) {
14025d4b688SGreg Roach            foreach ($query->get() as $row) {
141f0a11419SGreg Roach                if ($type === 'INDI') {
142f0a11419SGreg Roach                    $record = Individual::getInstance($row->xref, $tree, $row->gedcom);
143f0a11419SGreg Roach                } else {
144f0a11419SGreg Roach                    $record = Family::getInstance($row->xref, $tree, $row->gedcom);
145f0a11419SGreg Roach                }
14625d4b688SGreg Roach
147f0a11419SGreg Roach                $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
14825d4b688SGreg Roach
14930158ae7SGreg Roach                foreach ($record->facts() as $fact) {
150f0a11419SGreg Roach                    // For date ranges, we need a match on either the start/end.
15125d4b688SGreg Roach                    if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) {
152f0a11419SGreg Roach                        $fact->anniv   = 0;
153f0a11419SGreg Roach                        $found_facts[] = $fact;
154f0a11419SGreg Roach                    }
155f0a11419SGreg Roach                }
156f0a11419SGreg Roach            }
157f0a11419SGreg Roach        }
158f0a11419SGreg Roach
159f0a11419SGreg Roach        return $found_facts;
160f0a11419SGreg Roach    }
161f0a11419SGreg Roach
162f0a11419SGreg Roach    /**
163f0a11419SGreg Roach     * Get the list of current and upcoming events, sorted by anniversary date
164f0a11419SGreg Roach     *
165f0a11419SGreg Roach     * @param int    $jd1
166f0a11419SGreg Roach     * @param int    $jd2
167f0a11419SGreg Roach     * @param string $events
168c7ff4153SGreg Roach     * @param bool   $only_living
169f0a11419SGreg Roach     * @param string $sort_by
170f0a11419SGreg Roach     * @param Tree   $tree
171f0a11419SGreg Roach     *
172f0a11419SGreg Roach     * @return Fact[]
173f0a11419SGreg Roach     */
174f0a11419SGreg Roach    public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array
175f0a11419SGreg Roach    {
176f0a11419SGreg Roach        $found_facts = [];
177f0a11419SGreg Roach        $facts       = [];
178f0a11419SGreg Roach
179f0a11419SGreg Roach        foreach (range($jd1, $jd2) as $jd) {
180f0a11419SGreg Roach            $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree));
181f0a11419SGreg Roach        }
182f0a11419SGreg Roach
183f0a11419SGreg Roach        foreach ($found_facts as $fact) {
184e7766c08SGreg Roach            $record = $fact->record();
185f0a11419SGreg Roach            // only living people ?
186f0a11419SGreg Roach            if ($only_living) {
187f0a11419SGreg Roach                if ($record instanceof Individual && $record->isDead()) {
188f0a11419SGreg Roach                    continue;
189f0a11419SGreg Roach                }
190f0a11419SGreg Roach                if ($record instanceof Family) {
19139ca88baSGreg Roach                    $husb = $record->husband();
1928f038c36SRico Sonntag                    if ($husb === null || $husb->isDead()) {
193f0a11419SGreg Roach                        continue;
194f0a11419SGreg Roach                    }
19539ca88baSGreg Roach                    $wife = $record->wife();
1968f038c36SRico Sonntag                    if ($wife === null || $wife->isDead()) {
197f0a11419SGreg Roach                        continue;
198f0a11419SGreg Roach                    }
199f0a11419SGreg Roach                }
200f0a11419SGreg Roach            }
201f0a11419SGreg Roach            $facts[] = $fact;
202f0a11419SGreg Roach        }
203f0a11419SGreg Roach
204f0a11419SGreg Roach        switch ($sort_by) {
205f0a11419SGreg Roach            case 'anniv':
2068af3e5c1SGreg Roach                $facts = Fact::sortFacts(Collection::make($facts))->all();
207f0a11419SGreg Roach                break;
208580a4d11SGreg Roach
209f0a11419SGreg Roach            case 'alpha':
2100b5fd0a6SGreg Roach                uasort($facts, static function (Fact $x, Fact $y): int {
211c156e8f5SGreg Roach                    return GedcomRecord::nameComparator()($x->record(), $y->record());
212f0a11419SGreg Roach                });
213f0a11419SGreg Roach                break;
214f0a11419SGreg Roach        }
215f0a11419SGreg Roach
216f0a11419SGreg Roach        return $facts;
217f0a11419SGreg Roach    }
218f0a11419SGreg Roach
219f0a11419SGreg Roach    /**
220f0a11419SGreg Roach     * Get a list of events whose anniversary occured on a given julian day.
221f0a11419SGreg Roach     * Used on the on-this-day/upcoming blocks and the day/month calendar views.
222f0a11419SGreg Roach     *
223f0a11419SGreg Roach     * @param int    $jd    the julian day
224f0a11419SGreg Roach     * @param string $facts restrict the search to just these facts or leave blank for all
225f0a11419SGreg Roach     * @param Tree   $tree  the tree to search
226f0a11419SGreg Roach     *
227f0a11419SGreg Roach     * @return Fact[]
228f0a11419SGreg Roach     */
22925d4b688SGreg Roach    public function getAnniversaryEvents($jd, string $facts, Tree $tree): array
230f0a11419SGreg Roach    {
231f0a11419SGreg Roach        $found_facts = [];
232f0a11419SGreg Roach
233f0a11419SGreg Roach        $anniversaries = [
234f0a11419SGreg Roach            new GregorianDate($jd),
235f0a11419SGreg Roach            new JulianDate($jd),
236f0a11419SGreg Roach            new FrenchDate($jd),
237f0a11419SGreg Roach            new JewishDate($jd),
238f0a11419SGreg Roach            new HijriDate($jd),
239f0a11419SGreg Roach        ];
240f0a11419SGreg Roach
2414a9ac934SGreg Roach        // @TODO - there is a bug in the Persian Calendar that gives zero months for invalid dates
2424a9ac934SGreg Roach        if ($jd > (new PersianCalendar())->jdStart()) {
2434a9ac934SGreg Roach            $anniversaries[] = new JalaliDate($jd);
2444a9ac934SGreg Roach        }
2454a9ac934SGreg Roach
246f0a11419SGreg Roach        foreach ($anniversaries as $anniv) {
24725d4b688SGreg Roach            // Build a query to match anniversaries in the appropriate calendar.
24825d4b688SGreg Roach            $query = DB::table('dates')
24925d4b688SGreg Roach                ->distinct()
25025d4b688SGreg Roach                ->where('d_file', '=', $tree->id())
25125d4b688SGreg Roach                ->where('d_type', '=', $anniv->format('%@'));
252f0a11419SGreg Roach
253f0a11419SGreg Roach            // SIMPLE CASES:
254f0a11419SGreg Roach            // a) Non-hebrew anniversaries
255f0a11419SGreg Roach            // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL
256e364afe4SGreg Roach            if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) {
25725d4b688SGreg Roach                $this->defaultAnniversaries($query, $anniv);
258f0a11419SGreg Roach            } else {
259f0a11419SGreg Roach                // SPECIAL CASES:
2604a83f5d7SGreg Roach                switch ($anniv->month) {
261f0a11419SGreg Roach                    case 2:
26225d4b688SGreg Roach                        $this->cheshvanAnniversaries($query, $anniv);
263f0a11419SGreg Roach                        break;
264f0a11419SGreg Roach                    case 3:
26525d4b688SGreg Roach                        $this->kislevAnniversaries($query, $anniv);
266f0a11419SGreg Roach                        break;
267f0a11419SGreg Roach                    case 4:
26825d4b688SGreg Roach                        $this->tevetAnniversaries($query, $anniv);
269f0a11419SGreg Roach                        break;
27025d4b688SGreg Roach                    case 7:
27125d4b688SGreg Roach                        $this->adarIIAnniversaries($query, $anniv);
272f0a11419SGreg Roach                        break;
27325d4b688SGreg Roach                    case 8:
27425d4b688SGreg Roach                        $this->nisanAnniversaries($query, $anniv);
275f0a11419SGreg Roach                        break;
276f0a11419SGreg Roach                }
277f0a11419SGreg Roach            }
278f0a11419SGreg Roach            // Only events in the past (includes dates without a year)
27925d4b688SGreg Roach            $query->where('d_year', '<=', $anniv->year());
280f0a11419SGreg Roach
2810a614d28SGreg Roach            if ($facts === '') {
2820a614d28SGreg Roach                // If no facts specified, get all except these
2830a614d28SGreg Roach                $query->whereNotIn('d_fact', self::SKIP_FACTS);
2840a614d28SGreg Roach            } else {
2850a614d28SGreg Roach                // Restrict to certain types of fact
286f0a11419SGreg Roach                preg_match_all('/([_A-Z]+)/', $facts, $matches);
28725d4b688SGreg Roach
28825d4b688SGreg Roach                $query->whereIn('d_fact', $matches[1]);
289f0a11419SGreg Roach            }
290f0a11419SGreg Roach
29125d4b688SGreg Roach            $query
29294969abbSRico Sonntag                ->orderBy('d_day')
29394969abbSRico Sonntag                ->orderByDesc('d_year');
29425d4b688SGreg Roach
29525d4b688SGreg Roach            $ind_query = (clone $query)
2960b5fd0a6SGreg Roach                ->join('individuals', static function (JoinClause $join): void {
29725d4b688SGreg Roach                    $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
29825d4b688SGreg Roach                })
29925d4b688SGreg Roach                ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
30025d4b688SGreg Roach
30125d4b688SGreg Roach            $fam_query = (clone $query)
3020b5fd0a6SGreg Roach                ->join('families', static function (JoinClause $join): void {
30325d4b688SGreg Roach                    $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
30425d4b688SGreg Roach                })
30525d4b688SGreg Roach                ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
306f0a11419SGreg Roach
307f0a11419SGreg Roach            // Now fetch these anniversaries
30825d4b688SGreg Roach            foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) {
30925d4b688SGreg Roach                foreach ($query->get() as $row) {
310f0a11419SGreg Roach                    if ($type === 'INDI') {
311f0a11419SGreg Roach                        $record = Individual::getInstance($row->xref, $tree, $row->gedcom);
312f0a11419SGreg Roach                    } else {
313f0a11419SGreg Roach                        $record = Family::getInstance($row->xref, $tree, $row->gedcom);
314f0a11419SGreg Roach                    }
31525d4b688SGreg Roach
316f0a11419SGreg Roach                    $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
31725d4b688SGreg Roach
31830158ae7SGreg Roach                    foreach ($record->facts() as $fact) {
3192decada7SGreg Roach                        if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) {
3204a83f5d7SGreg Roach                            $fact->anniv   = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year;
321f0a11419SGreg Roach                            $fact->jd      = $jd;
322f0a11419SGreg Roach                            $found_facts[] = $fact;
323f0a11419SGreg Roach                        }
324f0a11419SGreg Roach                    }
325f0a11419SGreg Roach                }
326f0a11419SGreg Roach            }
327f0a11419SGreg Roach        }
328f0a11419SGreg Roach
329f0a11419SGreg Roach        return $found_facts;
330f0a11419SGreg Roach    }
33125d4b688SGreg Roach
33225d4b688SGreg Roach    /**
33325d4b688SGreg Roach     * By default, missing days have anniversaries on the first of the month,
33425d4b688SGreg Roach     * and invalid days have anniversaries on the last day of the month.
33525d4b688SGreg Roach     *
33625d4b688SGreg Roach     * @param Builder              $query
33725d4b688SGreg Roach     * @param AbstractCalendarDate $anniv
33825d4b688SGreg Roach     */
33925d4b688SGreg Roach    private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void
34025d4b688SGreg Roach    {
34125d4b688SGreg Roach        if ($anniv->day() === 1) {
34225d4b688SGreg Roach            $query->where('d_day', '<=', 1);
34325d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
34425d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
34525d4b688SGreg Roach        } else {
34625d4b688SGreg Roach            $query->where('d_day', '=', $anniv->day());
34725d4b688SGreg Roach        }
34825d4b688SGreg Roach
34925d4b688SGreg Roach        $query->where('d_mon', '=', $anniv->month());
35025d4b688SGreg Roach    }
35125d4b688SGreg Roach
35225d4b688SGreg Roach    /**
35325d4b688SGreg Roach     * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH).
35425d4b688SGreg Roach     *
35525d4b688SGreg Roach     * @param Builder    $query
35625d4b688SGreg Roach     * @param JewishDate $anniv
35725d4b688SGreg Roach     */
35825d4b688SGreg Roach    private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void
35925d4b688SGreg Roach    {
36025d4b688SGreg Roach        if ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
36125d4b688SGreg Roach            $query
36225d4b688SGreg Roach                ->where('d_mon', '=', 2)
36325d4b688SGreg Roach                ->where('d_day', '>=', 29)
36425d4b688SGreg Roach                ->where('d_day', '<>', 30);
36525d4b688SGreg Roach        } else {
36625d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
36725d4b688SGreg Roach        }
36825d4b688SGreg Roach    }
36925d4b688SGreg Roach
37025d4b688SGreg Roach    /**
37125d4b688SGreg Roach     * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH).
37225d4b688SGreg Roach     * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL).
37325d4b688SGreg Roach     *
37425d4b688SGreg Roach     * @param Builder    $query
37525d4b688SGreg Roach     * @param JewishDate $anniv
37625d4b688SGreg Roach     */
37725d4b688SGreg Roach    private function kislevAnniversaries(Builder $query, JewishDate $anniv): void
37825d4b688SGreg Roach    {
37925d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']);
38025d4b688SGreg Roach
38125d4b688SGreg Roach        if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) {
3820b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
3830b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
38425d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 3);
3850b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
38625d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 2);
38725d4b688SGreg Roach                });
38825d4b688SGreg Roach            });
38925d4b688SGreg Roach        } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
39025d4b688SGreg Roach            $query
39125d4b688SGreg Roach                ->where('d_mon', '=', 3)
39225d4b688SGreg Roach                ->where('d_day', '>=', 29)
39325d4b688SGreg Roach                ->where('d_day', '<>', 30);
39425d4b688SGreg Roach        } else {
39525d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
39625d4b688SGreg Roach        }
39725d4b688SGreg Roach    }
39825d4b688SGreg Roach
39925d4b688SGreg Roach    /**
40025d4b688SGreg Roach     * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL).
40125d4b688SGreg Roach     *
40225d4b688SGreg Roach     * @param Builder    $query
40325d4b688SGreg Roach     * @param JewishDate $anniv
40425d4b688SGreg Roach     */
40525d4b688SGreg Roach    private function tevetAnniversaries(Builder $query, JewishDate $anniv): void
40625d4b688SGreg Roach    {
40725d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']);
40825d4b688SGreg Roach
40925d4b688SGreg Roach        if ($anniv->day === 1 && $tmp->daysInMonth() === 29) {
4100b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4110b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
41225d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 4);
4130b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
41425d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 3);
41525d4b688SGreg Roach                });
41625d4b688SGreg Roach            });
41725d4b688SGreg Roach        } else {
41825d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
41925d4b688SGreg Roach        }
42025d4b688SGreg Roach    }
42125d4b688SGreg Roach
42225d4b688SGreg Roach    /**
42325d4b688SGreg Roach     * ADS includes non-leap ADR.
42425d4b688SGreg Roach     *
42525d4b688SGreg Roach     * @param Builder    $query
42625d4b688SGreg Roach     * @param JewishDate $anniv
42725d4b688SGreg Roach     */
42825d4b688SGreg Roach    private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void
42925d4b688SGreg Roach    {
43025d4b688SGreg Roach        if ($anniv->day() === 1) {
43125d4b688SGreg Roach            $query->where('d_day', '<=', 1);
43225d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
43325d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
43425d4b688SGreg Roach        } else {
43525d4b688SGreg Roach            $query->where('d_day', '<=', 1);
43625d4b688SGreg Roach        }
43725d4b688SGreg Roach
4380b5fd0a6SGreg Roach        $query->where(static function (Builder $query): void {
43925d4b688SGreg Roach            $query
44025d4b688SGreg Roach                ->where('d_mon', '=', 7)
4410b5fd0a6SGreg Roach                ->orWhere(static function (Builder $query): void {
44225d4b688SGreg Roach                    $query
44325d4b688SGreg Roach                        ->where('d_mon', '=', 6)
444*a69f5655SGreg Roach                        ->where(new Expression('(7 * d_year + 1 % 19)'), '>=', 7);
44525d4b688SGreg Roach                });
44625d4b688SGreg Roach        });
44725d4b688SGreg Roach    }
44825d4b688SGreg Roach
44925d4b688SGreg Roach    /**
45025d4b688SGreg Roach     * 1 NSN includes 30 ADR, if this year is non-leap.
45125d4b688SGreg Roach     *
45225d4b688SGreg Roach     * @param Builder    $query
45325d4b688SGreg Roach     * @param JewishDate $anniv
45425d4b688SGreg Roach     */
45525d4b688SGreg Roach    private function nisanAnniversaries(Builder $query, JewishDate $anniv): void
45625d4b688SGreg Roach    {
45725d4b688SGreg Roach        if ($anniv->day === 1 && !$anniv->isLeapYear()) {
4580b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4590b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
46025d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 8);
4610b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
46225d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 6);
46325d4b688SGreg Roach                });
46425d4b688SGreg Roach            });
46525d4b688SGreg Roach        } else {
46625d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
46725d4b688SGreg Roach        }
46825d4b688SGreg Roach    }
469f0a11419SGreg Roach}
470