xref: /webtrees/app/Services/CalendarService.php (revision a091ac74647eab281b25090b737835eeea14ae10)
1f0a11419SGreg Roach<?php
23976b470SGreg Roach
3f0a11419SGreg Roach/**
4f0a11419SGreg Roach * webtrees: online genealogy
5*a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team
6f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify
7f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by
8f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f0a11419SGreg Roach * (at your option) any later version.
10f0a11419SGreg Roach * This program is distributed in the hope that it will be useful,
11f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f0a11419SGreg Roach * GNU General Public License for more details.
14f0a11419SGreg Roach * You should have received a copy of the GNU General Public License
15f0a11419SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16f0a11419SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services;
21f0a11419SGreg Roach
224a9ac934SGreg Roachuse Fisharebest\ExtCalendar\PersianCalendar;
23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date;
2425d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
28f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
29f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
30f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
31f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact;
32*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
33f0a11419SGreg Roachuse Fisharebest\Webtrees\Family;
34f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
35f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual;
36f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree;
379e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
389e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder;
39a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
409e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause;
418af3e5c1SGreg Roachuse Illuminate\Support\Collection;
423976b470SGreg Roach
430195b0ddSGreg Roachuse function array_merge;
440195b0ddSGreg Roachuse function in_array;
450a614d28SGreg Roachuse function preg_match_all;
460a614d28SGreg Roachuse function range;
47f0a11419SGreg Roach
48f0a11419SGreg Roach/**
49f0a11419SGreg Roach * Calculate anniversaries, etc.
50f0a11419SGreg Roach */
51f0a11419SGreg Roachclass CalendarService
52f0a11419SGreg Roach{
530a614d28SGreg Roach    // If no facts specified, get all except these
540a614d28SGreg Roach    protected const SKIP_FACTS = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR', '_TODO'];
550a614d28SGreg Roach
56f0a11419SGreg Roach    /**
5770d8159dSGreg Roach     * List all the months in a given year.
5870d8159dSGreg Roach     *
5970d8159dSGreg Roach     * @param string $calendar
6070d8159dSGreg Roach     * @param int    $year
6170d8159dSGreg Roach     *
6270d8159dSGreg Roach     * @return string[]
6370d8159dSGreg Roach     */
6470d8159dSGreg Roach    public function calendarMonthsInYear(string $calendar, int $year): array
6570d8159dSGreg Roach    {
6670d8159dSGreg Roach        $date          = new Date($calendar . ' ' . $year);
6770d8159dSGreg Roach        $calendar_date = $date->minimumDate();
6870d8159dSGreg Roach        $month_numbers = range(1, $calendar_date->monthsInYear());
6970d8159dSGreg Roach        $month_names   = [];
7070d8159dSGreg Roach
7170d8159dSGreg Roach        foreach ($month_numbers as $month_number) {
724a83f5d7SGreg Roach            $calendar_date->day   = 1;
734a83f5d7SGreg Roach            $calendar_date->month = $month_number;
7470d8159dSGreg Roach            $calendar_date->setJdFromYmd();
7570d8159dSGreg Roach
7670d8159dSGreg Roach            if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
7770d8159dSGreg Roach                // No month 6 in Jewish non-leap years.
7870d8159dSGreg Roach                continue;
7970d8159dSGreg Roach            }
8070d8159dSGreg Roach
8170d8159dSGreg Roach            if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
8270d8159dSGreg Roach                // Month 7 is ADR in Jewish non-leap years (and ADS in others).
8370d8159dSGreg Roach                $mon = 'ADR';
8470d8159dSGreg Roach            } else {
8570d8159dSGreg Roach                $mon = $calendar_date->format('%O');
8670d8159dSGreg Roach            }
8770d8159dSGreg Roach
8870d8159dSGreg Roach            $month_names[$mon] = $calendar_date->format('%F');
8970d8159dSGreg Roach        }
9070d8159dSGreg Roach
9170d8159dSGreg Roach        return $month_names;
9270d8159dSGreg Roach    }
9370d8159dSGreg Roach
9470d8159dSGreg Roach    /**
95f0a11419SGreg Roach     * Get a list of events which occured during a given date range.
96f0a11419SGreg Roach     *
97f0a11419SGreg Roach     * @param int    $jd1   the start range of julian day
98f0a11419SGreg Roach     * @param int    $jd2   the end range of julian day
990a614d28SGreg Roach     * @param string $facts restrict the search to just these facts or leave blank for all
100f0a11419SGreg Roach     * @param Tree   $tree  the tree to search
101f0a11419SGreg Roach     *
102f0a11419SGreg Roach     * @return Fact[]
103f0a11419SGreg Roach     */
1040a614d28SGreg Roach    public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array
105f0a11419SGreg Roach    {
106f0a11419SGreg Roach        // Events that start or end during the period
10725d4b688SGreg Roach        $query = DB::table('dates')
1089e87c1a2SGreg Roach            ->where('d_file', '=', $tree->id())
1090b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($jd1, $jd2): void {
1100b5fd0a6SGreg Roach                $query->where(static function (Builder $query) use ($jd1, $jd2): void {
1119e87c1a2SGreg Roach                    $query
1129e87c1a2SGreg Roach                        ->where('d_julianday1', '>=', $jd1)
1139e87c1a2SGreg Roach                        ->where('d_julianday1', '<=', $jd2);
1140b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query) use ($jd1, $jd2): void {
1159e87c1a2SGreg Roach                    $query
1169e87c1a2SGreg Roach                        ->where('d_julianday2', '>=', $jd1)
1179e87c1a2SGreg Roach                        ->where('d_julianday2', '<=', $jd2);
1189e87c1a2SGreg Roach                });
1199e87c1a2SGreg Roach            });
120f0a11419SGreg Roach
121f0a11419SGreg Roach        // Restrict to certain types of fact
1220a614d28SGreg Roach        if ($facts === '') {
1230a614d28SGreg Roach            $query->whereNotIn('d_fact', self::SKIP_FACTS);
124f0a11419SGreg Roach        } else {
1250a614d28SGreg Roach            preg_match_all('/([_A-Z]+)/', $facts, $matches);
1260a614d28SGreg Roach
1270a614d28SGreg Roach            $query->whereIn('d_fact', $matches[1]);
128f0a11419SGreg Roach        }
129f0a11419SGreg Roach
13025d4b688SGreg Roach        $ind_query = (clone $query)
1310b5fd0a6SGreg Roach            ->join('individuals', static function (JoinClause $join): void {
13225d4b688SGreg Roach                $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
13325d4b688SGreg Roach            })
13425d4b688SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
135f0a11419SGreg Roach
13625d4b688SGreg Roach        $fam_query = (clone $query)
1370b5fd0a6SGreg Roach            ->join('families', static function (JoinClause $join): void {
13825d4b688SGreg Roach                $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
13925d4b688SGreg Roach            })
14025d4b688SGreg Roach            ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
14125d4b688SGreg Roach
14225d4b688SGreg Roach        // Now fetch these events
14325d4b688SGreg Roach        $found_facts = [];
14425d4b688SGreg Roach
14561b2e6eeSGreg Roach        foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $record_query) {
14661b2e6eeSGreg Roach            foreach ($record_query->get() as $row) {
147f0a11419SGreg Roach                if ($type === 'INDI') {
148*a091ac74SGreg Roach                    $record = Factory::individual()->make($row->xref, $tree, $row->gedcom);
149f0a11419SGreg Roach                } else {
150*a091ac74SGreg Roach                    $record = Factory::family()->make($row->xref, $tree, $row->gedcom);
151f0a11419SGreg Roach                }
15225d4b688SGreg Roach
153f0a11419SGreg Roach                $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
15425d4b688SGreg Roach
15530158ae7SGreg Roach                foreach ($record->facts() as $fact) {
156f0a11419SGreg Roach                    // For date ranges, we need a match on either the start/end.
15725d4b688SGreg Roach                    if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) {
158f0a11419SGreg Roach                        $fact->anniv   = 0;
159f0a11419SGreg Roach                        $found_facts[] = $fact;
160f0a11419SGreg Roach                    }
161f0a11419SGreg Roach                }
162f0a11419SGreg Roach            }
163f0a11419SGreg Roach        }
164f0a11419SGreg Roach
165f0a11419SGreg Roach        return $found_facts;
166f0a11419SGreg Roach    }
167f0a11419SGreg Roach
168f0a11419SGreg Roach    /**
169f0a11419SGreg Roach     * Get the list of current and upcoming events, sorted by anniversary date
170f0a11419SGreg Roach     *
171f0a11419SGreg Roach     * @param int    $jd1
172f0a11419SGreg Roach     * @param int    $jd2
173f0a11419SGreg Roach     * @param string $events
174c7ff4153SGreg Roach     * @param bool   $only_living
175f0a11419SGreg Roach     * @param string $sort_by
176f0a11419SGreg Roach     * @param Tree   $tree
177f0a11419SGreg Roach     *
178cd51dbdfSGreg Roach     * @return Collection<Fact>
179f0a11419SGreg Roach     */
180cd51dbdfSGreg Roach    public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): Collection
181f0a11419SGreg Roach    {
182f0a11419SGreg Roach        $found_facts = [];
183cd51dbdfSGreg Roach        $facts       = new Collection();
184f0a11419SGreg Roach
185f0a11419SGreg Roach        foreach (range($jd1, $jd2) as $jd) {
186f0a11419SGreg Roach            $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree));
187f0a11419SGreg Roach        }
188f0a11419SGreg Roach
189f0a11419SGreg Roach        foreach ($found_facts as $fact) {
190e7766c08SGreg Roach            $record = $fact->record();
191f0a11419SGreg Roach            // only living people ?
192f0a11419SGreg Roach            if ($only_living) {
193f0a11419SGreg Roach                if ($record instanceof Individual && $record->isDead()) {
194f0a11419SGreg Roach                    continue;
195f0a11419SGreg Roach                }
196f0a11419SGreg Roach                if ($record instanceof Family) {
19739ca88baSGreg Roach                    $husb = $record->husband();
1988f038c36SRico Sonntag                    if ($husb === null || $husb->isDead()) {
199f0a11419SGreg Roach                        continue;
200f0a11419SGreg Roach                    }
20139ca88baSGreg Roach                    $wife = $record->wife();
2028f038c36SRico Sonntag                    if ($wife === null || $wife->isDead()) {
203f0a11419SGreg Roach                        continue;
204f0a11419SGreg Roach                    }
205f0a11419SGreg Roach                }
206f0a11419SGreg Roach            }
207cd51dbdfSGreg Roach            $facts->push($fact);
208f0a11419SGreg Roach        }
209f0a11419SGreg Roach
210f0a11419SGreg Roach        switch ($sort_by) {
211f0a11419SGreg Roach            case 'anniv':
212cd51dbdfSGreg Roach                $facts = $facts->sort(static function (Fact $x, Fact $y): int {
213cd51dbdfSGreg Roach                    return $x->jd <=> $y->jd;
214cd51dbdfSGreg Roach                });
215f0a11419SGreg Roach                break;
216580a4d11SGreg Roach
217f0a11419SGreg Roach            case 'alpha':
218cd51dbdfSGreg Roach                $facts = $facts->sort(static function (Fact $x, Fact $y): int {
219c156e8f5SGreg Roach                    return GedcomRecord::nameComparator()($x->record(), $y->record());
220f0a11419SGreg Roach                });
221f0a11419SGreg Roach                break;
222f0a11419SGreg Roach        }
223f0a11419SGreg Roach
224e24053e5SGreg Roach        return $facts->values();
225f0a11419SGreg Roach    }
226f0a11419SGreg Roach
227f0a11419SGreg Roach    /**
228f0a11419SGreg Roach     * Get a list of events whose anniversary occured on a given julian day.
229f0a11419SGreg Roach     * Used on the on-this-day/upcoming blocks and the day/month calendar views.
230f0a11419SGreg Roach     *
231f0a11419SGreg Roach     * @param int    $jd    the julian day
232f0a11419SGreg Roach     * @param string $facts restrict the search to just these facts or leave blank for all
233f0a11419SGreg Roach     * @param Tree   $tree  the tree to search
234f0a11419SGreg Roach     *
235f0a11419SGreg Roach     * @return Fact[]
236f0a11419SGreg Roach     */
23725d4b688SGreg Roach    public function getAnniversaryEvents($jd, string $facts, Tree $tree): array
238f0a11419SGreg Roach    {
239f0a11419SGreg Roach        $found_facts = [];
240f0a11419SGreg Roach
241f0a11419SGreg Roach        $anniversaries = [
242f0a11419SGreg Roach            new GregorianDate($jd),
243f0a11419SGreg Roach            new JulianDate($jd),
244f0a11419SGreg Roach            new FrenchDate($jd),
245f0a11419SGreg Roach            new JewishDate($jd),
246f0a11419SGreg Roach            new HijriDate($jd),
247f0a11419SGreg Roach        ];
248f0a11419SGreg Roach
2491bd3bc77SGreg Roach        // There is a bug in the Persian Calendar that gives zero months for invalid dates
2504a9ac934SGreg Roach        if ($jd > (new PersianCalendar())->jdStart()) {
2514a9ac934SGreg Roach            $anniversaries[] = new JalaliDate($jd);
2524a9ac934SGreg Roach        }
2534a9ac934SGreg Roach
254f0a11419SGreg Roach        foreach ($anniversaries as $anniv) {
25525d4b688SGreg Roach            // Build a query to match anniversaries in the appropriate calendar.
25625d4b688SGreg Roach            $query = DB::table('dates')
25725d4b688SGreg Roach                ->distinct()
25825d4b688SGreg Roach                ->where('d_file', '=', $tree->id())
25925d4b688SGreg Roach                ->where('d_type', '=', $anniv->format('%@'));
260f0a11419SGreg Roach
261f0a11419SGreg Roach            // SIMPLE CASES:
262f0a11419SGreg Roach            // a) Non-hebrew anniversaries
263f0a11419SGreg Roach            // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL
264e364afe4SGreg Roach            if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) {
26525d4b688SGreg Roach                $this->defaultAnniversaries($query, $anniv);
266f0a11419SGreg Roach            } else {
267f0a11419SGreg Roach                // SPECIAL CASES:
2684a83f5d7SGreg Roach                switch ($anniv->month) {
269f0a11419SGreg Roach                    case 2:
27025d4b688SGreg Roach                        $this->cheshvanAnniversaries($query, $anniv);
271f0a11419SGreg Roach                        break;
272f0a11419SGreg Roach                    case 3:
27325d4b688SGreg Roach                        $this->kislevAnniversaries($query, $anniv);
274f0a11419SGreg Roach                        break;
275f0a11419SGreg Roach                    case 4:
27625d4b688SGreg Roach                        $this->tevetAnniversaries($query, $anniv);
277f0a11419SGreg Roach                        break;
27825d4b688SGreg Roach                    case 7:
27925d4b688SGreg Roach                        $this->adarIIAnniversaries($query, $anniv);
280f0a11419SGreg Roach                        break;
28125d4b688SGreg Roach                    case 8:
28225d4b688SGreg Roach                        $this->nisanAnniversaries($query, $anniv);
283f0a11419SGreg Roach                        break;
284f0a11419SGreg Roach                }
285f0a11419SGreg Roach            }
286f0a11419SGreg Roach            // Only events in the past (includes dates without a year)
28725d4b688SGreg Roach            $query->where('d_year', '<=', $anniv->year());
288f0a11419SGreg Roach
2890a614d28SGreg Roach            if ($facts === '') {
2900a614d28SGreg Roach                // If no facts specified, get all except these
2910a614d28SGreg Roach                $query->whereNotIn('d_fact', self::SKIP_FACTS);
2920a614d28SGreg Roach            } else {
2930a614d28SGreg Roach                // Restrict to certain types of fact
294f0a11419SGreg Roach                preg_match_all('/([_A-Z]+)/', $facts, $matches);
29525d4b688SGreg Roach
29625d4b688SGreg Roach                $query->whereIn('d_fact', $matches[1]);
297f0a11419SGreg Roach            }
298f0a11419SGreg Roach
29925d4b688SGreg Roach            $query
30094969abbSRico Sonntag                ->orderBy('d_day')
3010195b0ddSGreg Roach                ->orderBy('d_year', 'DESC');
30225d4b688SGreg Roach
30325d4b688SGreg Roach            $ind_query = (clone $query)
3040b5fd0a6SGreg Roach                ->join('individuals', static function (JoinClause $join): void {
30525d4b688SGreg Roach                    $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
30625d4b688SGreg Roach                })
30725d4b688SGreg Roach                ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
30825d4b688SGreg Roach
30925d4b688SGreg Roach            $fam_query = (clone $query)
3100b5fd0a6SGreg Roach                ->join('families', static function (JoinClause $join): void {
31125d4b688SGreg Roach                    $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
31225d4b688SGreg Roach                })
31325d4b688SGreg Roach                ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
314f0a11419SGreg Roach
315f0a11419SGreg Roach            // Now fetch these anniversaries
31661b2e6eeSGreg Roach            foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $record_query) {
31761b2e6eeSGreg Roach                foreach ($record_query->get() as $row) {
318f0a11419SGreg Roach                    if ($type === 'INDI') {
319*a091ac74SGreg Roach                        $record = Factory::individual()->make($row->xref, $tree, $row->gedcom);
320f0a11419SGreg Roach                    } else {
321*a091ac74SGreg Roach                        $record = Factory::family()->make($row->xref, $tree, $row->gedcom);
322f0a11419SGreg Roach                    }
32325d4b688SGreg Roach
324f0a11419SGreg Roach                    $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
32525d4b688SGreg Roach
326543ab5f6SGreg Roach                    // The record may have multiple facts of this type.
327543ab5f6SGreg Roach                    // Find the ones that match the date.
328543ab5f6SGreg Roach                    foreach ($record->facts([$row->d_fact]) as $fact) {
329543ab5f6SGreg Roach                        $min_date = $fact->date()->minimumDate();
330543ab5f6SGreg Roach                        $max_date = $fact->date()->maximumDate();
331543ab5f6SGreg Roach
332543ab5f6SGreg Roach                        if ($min_date->minimumJulianDay() === $anniv_date->minimumJulianDay() && $min_date::ESCAPE === $row->d_type || $max_date->maximumJulianDay() === $anniv_date->maximumJulianDay() && $max_date::ESCAPE === $row->d_type) {
3334a83f5d7SGreg Roach                            $fact->anniv   = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year;
334f0a11419SGreg Roach                            $fact->jd      = $jd;
335f0a11419SGreg Roach                            $found_facts[] = $fact;
336f0a11419SGreg Roach                        }
337f0a11419SGreg Roach                    }
338f0a11419SGreg Roach                }
339f0a11419SGreg Roach            }
340f0a11419SGreg Roach        }
341f0a11419SGreg Roach
342f0a11419SGreg Roach        return $found_facts;
343f0a11419SGreg Roach    }
34425d4b688SGreg Roach
34525d4b688SGreg Roach    /**
34625d4b688SGreg Roach     * By default, missing days have anniversaries on the first of the month,
34725d4b688SGreg Roach     * and invalid days have anniversaries on the last day of the month.
34825d4b688SGreg Roach     *
34925d4b688SGreg Roach     * @param Builder              $query
35025d4b688SGreg Roach     * @param AbstractCalendarDate $anniv
35125d4b688SGreg Roach     */
35225d4b688SGreg Roach    private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void
35325d4b688SGreg Roach    {
35425d4b688SGreg Roach        if ($anniv->day() === 1) {
35525d4b688SGreg Roach            $query->where('d_day', '<=', 1);
35625d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
35725d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
35825d4b688SGreg Roach        } else {
35925d4b688SGreg Roach            $query->where('d_day', '=', $anniv->day());
36025d4b688SGreg Roach        }
36125d4b688SGreg Roach
36225d4b688SGreg Roach        $query->where('d_mon', '=', $anniv->month());
36325d4b688SGreg Roach    }
36425d4b688SGreg Roach
36525d4b688SGreg Roach    /**
36625d4b688SGreg Roach     * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH).
36725d4b688SGreg Roach     *
36825d4b688SGreg Roach     * @param Builder    $query
36925d4b688SGreg Roach     * @param JewishDate $anniv
37025d4b688SGreg Roach     */
37125d4b688SGreg Roach    private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void
37225d4b688SGreg Roach    {
37325d4b688SGreg Roach        if ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
37425d4b688SGreg Roach            $query
37525d4b688SGreg Roach                ->where('d_mon', '=', 2)
37625d4b688SGreg Roach                ->where('d_day', '>=', 29)
37725d4b688SGreg Roach                ->where('d_day', '<>', 30);
37825d4b688SGreg Roach        } else {
37925d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
38025d4b688SGreg Roach        }
38125d4b688SGreg Roach    }
38225d4b688SGreg Roach
38325d4b688SGreg Roach    /**
38425d4b688SGreg Roach     * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH).
38525d4b688SGreg Roach     * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL).
38625d4b688SGreg Roach     *
38725d4b688SGreg Roach     * @param Builder    $query
38825d4b688SGreg Roach     * @param JewishDate $anniv
38925d4b688SGreg Roach     */
39025d4b688SGreg Roach    private function kislevAnniversaries(Builder $query, JewishDate $anniv): void
39125d4b688SGreg Roach    {
39225d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']);
39325d4b688SGreg Roach
39425d4b688SGreg Roach        if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) {
3950b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
3960b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
39725d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 3);
3980b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
39925d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 2);
40025d4b688SGreg Roach                });
40125d4b688SGreg Roach            });
40225d4b688SGreg Roach        } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
40325d4b688SGreg Roach            $query
40425d4b688SGreg Roach                ->where('d_mon', '=', 3)
40525d4b688SGreg Roach                ->where('d_day', '>=', 29)
40625d4b688SGreg Roach                ->where('d_day', '<>', 30);
40725d4b688SGreg Roach        } else {
40825d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
40925d4b688SGreg Roach        }
41025d4b688SGreg Roach    }
41125d4b688SGreg Roach
41225d4b688SGreg Roach    /**
41325d4b688SGreg Roach     * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL).
41425d4b688SGreg Roach     *
41525d4b688SGreg Roach     * @param Builder    $query
41625d4b688SGreg Roach     * @param JewishDate $anniv
41725d4b688SGreg Roach     */
41825d4b688SGreg Roach    private function tevetAnniversaries(Builder $query, JewishDate $anniv): void
41925d4b688SGreg Roach    {
42025d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']);
42125d4b688SGreg Roach
42225d4b688SGreg Roach        if ($anniv->day === 1 && $tmp->daysInMonth() === 29) {
4230b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4240b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
42525d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 4);
4260b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
42725d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 3);
42825d4b688SGreg Roach                });
42925d4b688SGreg Roach            });
43025d4b688SGreg Roach        } else {
43125d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
43225d4b688SGreg Roach        }
43325d4b688SGreg Roach    }
43425d4b688SGreg Roach
43525d4b688SGreg Roach    /**
43625d4b688SGreg Roach     * ADS includes non-leap ADR.
43725d4b688SGreg Roach     *
43825d4b688SGreg Roach     * @param Builder    $query
43925d4b688SGreg Roach     * @param JewishDate $anniv
44025d4b688SGreg Roach     */
44125d4b688SGreg Roach    private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void
44225d4b688SGreg Roach    {
44325d4b688SGreg Roach        if ($anniv->day() === 1) {
44425d4b688SGreg Roach            $query->where('d_day', '<=', 1);
44525d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
44625d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
44725d4b688SGreg Roach        } else {
44825d4b688SGreg Roach            $query->where('d_day', '<=', 1);
44925d4b688SGreg Roach        }
45025d4b688SGreg Roach
4510b5fd0a6SGreg Roach        $query->where(static function (Builder $query): void {
45225d4b688SGreg Roach            $query
45325d4b688SGreg Roach                ->where('d_mon', '=', 7)
4540b5fd0a6SGreg Roach                ->orWhere(static function (Builder $query): void {
45525d4b688SGreg Roach                    $query
45625d4b688SGreg Roach                        ->where('d_mon', '=', 6)
457a69f5655SGreg Roach                        ->where(new Expression('(7 * d_year + 1 % 19)'), '>=', 7);
45825d4b688SGreg Roach                });
45925d4b688SGreg Roach        });
46025d4b688SGreg Roach    }
46125d4b688SGreg Roach
46225d4b688SGreg Roach    /**
46325d4b688SGreg Roach     * 1 NSN includes 30 ADR, if this year is non-leap.
46425d4b688SGreg Roach     *
46525d4b688SGreg Roach     * @param Builder    $query
46625d4b688SGreg Roach     * @param JewishDate $anniv
46725d4b688SGreg Roach     */
46825d4b688SGreg Roach    private function nisanAnniversaries(Builder $query, JewishDate $anniv): void
46925d4b688SGreg Roach    {
47025d4b688SGreg Roach        if ($anniv->day === 1 && !$anniv->isLeapYear()) {
4710b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4720b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
47325d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 8);
4740b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
47525d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 6);
47625d4b688SGreg Roach                });
47725d4b688SGreg Roach            });
47825d4b688SGreg Roach        } else {
47925d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
48025d4b688SGreg Roach        }
48125d4b688SGreg Roach    }
482f0a11419SGreg Roach}
483