xref: /webtrees/app/Services/CalendarService.php (revision a51456341d2020736a75d90c5bf22e4fc5e947c2)
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
20f0a11419SGreg Roachuse Fisharebest\Webtrees\Date;
2125d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
22f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
24f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
28f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact;
29f0a11419SGreg Roachuse Fisharebest\Webtrees\Family;
30f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
31f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual;
32f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree;
339e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
349e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder;
359e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause;
368af3e5c1SGreg Roachuse Illuminate\Support\Collection;
37f0a11419SGreg Roach
38f0a11419SGreg Roach/**
39f0a11419SGreg Roach * Calculate anniversaries, etc.
40f0a11419SGreg Roach */
41f0a11419SGreg Roachclass CalendarService
42f0a11419SGreg Roach{
43f0a11419SGreg Roach    /**
4470d8159dSGreg Roach     * List all the months in a given year.
4570d8159dSGreg Roach     *
4670d8159dSGreg Roach     * @param string $calendar
4770d8159dSGreg Roach     * @param int    $year
4870d8159dSGreg Roach     *
4970d8159dSGreg Roach     * @return string[]
5070d8159dSGreg Roach     */
5170d8159dSGreg Roach    public function calendarMonthsInYear(string $calendar, int $year): array
5270d8159dSGreg Roach    {
5370d8159dSGreg Roach        $date          = new Date($calendar . ' ' . $year);
5470d8159dSGreg Roach        $calendar_date = $date->minimumDate();
5570d8159dSGreg Roach        $month_numbers = range(1, $calendar_date->monthsInYear());
5670d8159dSGreg Roach        $month_names   = [];
5770d8159dSGreg Roach
5870d8159dSGreg Roach        foreach ($month_numbers as $month_number) {
594a83f5d7SGreg Roach            $calendar_date->day   = 1;
604a83f5d7SGreg Roach            $calendar_date->month = $month_number;
6170d8159dSGreg Roach            $calendar_date->setJdFromYmd();
6270d8159dSGreg Roach
6370d8159dSGreg Roach            if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
6470d8159dSGreg Roach                // No month 6 in Jewish non-leap years.
6570d8159dSGreg Roach                continue;
6670d8159dSGreg Roach            }
6770d8159dSGreg Roach
6870d8159dSGreg Roach            if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
6970d8159dSGreg Roach                // Month 7 is ADR in Jewish non-leap years (and ADS in others).
7070d8159dSGreg Roach                $mon = 'ADR';
7170d8159dSGreg Roach            } else {
7270d8159dSGreg Roach                $mon = $calendar_date->format('%O');
7370d8159dSGreg Roach            }
7470d8159dSGreg Roach
7570d8159dSGreg Roach            $month_names[$mon] = $calendar_date->format('%F');
7670d8159dSGreg Roach        }
7770d8159dSGreg Roach
7870d8159dSGreg Roach        return $month_names;
7970d8159dSGreg Roach    }
8070d8159dSGreg Roach
8170d8159dSGreg Roach    /**
82f0a11419SGreg Roach     * Get a list of events which occured during a given date range.
83f0a11419SGreg Roach     *
84f0a11419SGreg Roach     * @param int      $jd1   the start range of julian day
85f0a11419SGreg Roach     * @param int      $jd2   the end range of julian day
869e87c1a2SGreg Roach     * @param string[] $facts restrict the search to just these facts or leave blank for all
87f0a11419SGreg Roach     * @param Tree     $tree  the tree to search
88f0a11419SGreg Roach     *
89f0a11419SGreg Roach     * @return Fact[]
90f0a11419SGreg Roach     */
919e87c1a2SGreg Roach    public function getCalendarEvents(int $jd1, int $jd2, array $facts, Tree $tree): array
92f0a11419SGreg Roach    {
93f0a11419SGreg Roach        // If no facts specified, get all except these
949e87c1a2SGreg Roach        $skipfacts = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR'];
95f0a11419SGreg Roach
96f0a11419SGreg Roach        // Events that start or end during the period
9725d4b688SGreg Roach        $query = DB::table('dates')
989e87c1a2SGreg Roach            ->where('d_file', '=', $tree->id())
9925d7fe95SGreg Roach            ->where(function (Builder $query) use ($jd1, $jd2): void {
10025d7fe95SGreg Roach                $query->where(function (Builder $query) use ($jd1, $jd2): void {
1019e87c1a2SGreg Roach                    $query
1029e87c1a2SGreg Roach                        ->where('d_julianday1', '>=', $jd1)
1039e87c1a2SGreg Roach                        ->where('d_julianday1', '<=', $jd2);
10425d7fe95SGreg Roach                })->orWhere(function (Builder $query) use ($jd1, $jd2): void {
1059e87c1a2SGreg Roach                    $query
1069e87c1a2SGreg Roach                        ->where('d_julianday2', '>=', $jd1)
1079e87c1a2SGreg Roach                        ->where('d_julianday2', '<=', $jd2);
1089e87c1a2SGreg Roach                });
1099e87c1a2SGreg Roach            });
110f0a11419SGreg Roach
111f0a11419SGreg Roach        // Restrict to certain types of fact
112f0a11419SGreg Roach        if (empty($facts)) {
1139e87c1a2SGreg Roach            $query->whereNotIn('d_fact', $skipfacts);
114f0a11419SGreg Roach        } else {
1159e87c1a2SGreg Roach            $query->whereIn('d_fact', $facts);
116f0a11419SGreg Roach        }
117f0a11419SGreg Roach
11825d4b688SGreg Roach        $ind_query = (clone $query)
11925d4b688SGreg Roach            ->join('individuals', function (JoinClause $join): void {
12025d4b688SGreg Roach                $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
12125d4b688SGreg Roach            })
12225d4b688SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
123f0a11419SGreg Roach
12425d4b688SGreg Roach        $fam_query = (clone $query)
12525d4b688SGreg Roach            ->join('families', function (JoinClause $join): void {
12625d4b688SGreg Roach                $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
12725d4b688SGreg Roach            })
12825d4b688SGreg Roach            ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
12925d4b688SGreg Roach
13025d4b688SGreg Roach        // Now fetch these events
13125d4b688SGreg Roach        $found_facts = [];
13225d4b688SGreg Roach
13325d4b688SGreg Roach        foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) {
13425d4b688SGreg Roach            foreach ($query->get() as $row) {
135f0a11419SGreg Roach                if ($type === 'INDI') {
136f0a11419SGreg Roach                    $record = Individual::getInstance($row->xref, $tree, $row->gedcom);
137f0a11419SGreg Roach                } else {
138f0a11419SGreg Roach                    $record = Family::getInstance($row->xref, $tree, $row->gedcom);
139f0a11419SGreg Roach                }
14025d4b688SGreg Roach
141f0a11419SGreg Roach                $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
14225d4b688SGreg Roach
14330158ae7SGreg Roach                foreach ($record->facts() as $fact) {
144f0a11419SGreg Roach                    // For date ranges, we need a match on either the start/end.
14525d4b688SGreg Roach                    if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) {
146f0a11419SGreg Roach                        $fact->anniv   = 0;
147f0a11419SGreg Roach                        $found_facts[] = $fact;
148f0a11419SGreg Roach                    }
149f0a11419SGreg Roach                }
150f0a11419SGreg Roach            }
151f0a11419SGreg Roach        }
152f0a11419SGreg Roach
153f0a11419SGreg Roach        return $found_facts;
154f0a11419SGreg Roach    }
155f0a11419SGreg Roach
156f0a11419SGreg Roach    /**
157f0a11419SGreg Roach     * Get the list of current and upcoming events, sorted by anniversary date
158f0a11419SGreg Roach     *
159f0a11419SGreg Roach     * @param int    $jd1
160f0a11419SGreg Roach     * @param int    $jd2
161f0a11419SGreg Roach     * @param string $events
162c7ff4153SGreg Roach     * @param bool   $only_living
163f0a11419SGreg Roach     * @param string $sort_by
164f0a11419SGreg Roach     * @param Tree   $tree
165f0a11419SGreg Roach     *
166f0a11419SGreg Roach     * @return Fact[]
167f0a11419SGreg Roach     */
168f0a11419SGreg Roach    public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array
169f0a11419SGreg Roach    {
170f0a11419SGreg Roach        $found_facts = [];
171f0a11419SGreg Roach        $facts       = [];
172f0a11419SGreg Roach
173f0a11419SGreg Roach        foreach (range($jd1, $jd2) as $jd) {
174f0a11419SGreg Roach            $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree));
175f0a11419SGreg Roach        }
176f0a11419SGreg Roach
177f0a11419SGreg Roach        foreach ($found_facts as $fact) {
178e7766c08SGreg Roach            $record = $fact->record();
179f0a11419SGreg Roach            // only living people ?
180f0a11419SGreg Roach            if ($only_living) {
181f0a11419SGreg Roach                if ($record instanceof Individual && $record->isDead()) {
182f0a11419SGreg Roach                    continue;
183f0a11419SGreg Roach                }
184f0a11419SGreg Roach                if ($record instanceof Family) {
18539ca88baSGreg Roach                    $husb = $record->husband();
1868f038c36SRico Sonntag                    if ($husb === null || $husb->isDead()) {
187f0a11419SGreg Roach                        continue;
188f0a11419SGreg Roach                    }
18939ca88baSGreg Roach                    $wife = $record->wife();
1908f038c36SRico Sonntag                    if ($wife === null || $wife->isDead()) {
191f0a11419SGreg Roach                        continue;
192f0a11419SGreg Roach                    }
193f0a11419SGreg Roach                }
194f0a11419SGreg Roach            }
195f0a11419SGreg Roach            $facts[] = $fact;
196f0a11419SGreg Roach        }
197f0a11419SGreg Roach
198f0a11419SGreg Roach        switch ($sort_by) {
199f0a11419SGreg Roach            case 'anniv':
2008af3e5c1SGreg Roach                $facts = Fact::sortFacts(Collection::make($facts))->all();
201f0a11419SGreg Roach                break;
202580a4d11SGreg Roach
203f0a11419SGreg Roach            case 'alpha':
204a6312006SGreg Roach                uasort($facts, function (Fact $x, Fact $y): int {
205c156e8f5SGreg Roach                    return GedcomRecord::nameComparator()($x->record(), $y->record());
206f0a11419SGreg Roach                });
207f0a11419SGreg Roach                break;
208f0a11419SGreg Roach        }
209f0a11419SGreg Roach
210f0a11419SGreg Roach        return $facts;
211f0a11419SGreg Roach    }
212f0a11419SGreg Roach
213f0a11419SGreg Roach    /**
214f0a11419SGreg Roach     * Get a list of events whose anniversary occured on a given julian day.
215f0a11419SGreg Roach     * Used on the on-this-day/upcoming blocks and the day/month calendar views.
216f0a11419SGreg Roach     *
217f0a11419SGreg Roach     * @param int    $jd    the julian day
218f0a11419SGreg Roach     * @param string $facts restrict the search to just these facts or leave blank for all
219f0a11419SGreg Roach     * @param Tree   $tree  the tree to search
220f0a11419SGreg Roach     *
221f0a11419SGreg Roach     * @return Fact[]
222f0a11419SGreg Roach     */
22325d4b688SGreg Roach    public function getAnniversaryEvents($jd, string $facts, Tree $tree): array
224f0a11419SGreg Roach    {
225f0a11419SGreg Roach        $found_facts = [];
226f0a11419SGreg Roach
227f0a11419SGreg Roach        $anniversaries = [
228f0a11419SGreg Roach            new GregorianDate($jd),
229f0a11419SGreg Roach            new JulianDate($jd),
230f0a11419SGreg Roach            new FrenchDate($jd),
231f0a11419SGreg Roach            new JewishDate($jd),
232f0a11419SGreg Roach            new HijriDate($jd),
233f0a11419SGreg Roach            new JalaliDate($jd),
234f0a11419SGreg Roach        ];
235f0a11419SGreg Roach
236f0a11419SGreg Roach        foreach ($anniversaries as $anniv) {
23725d4b688SGreg Roach            // Build a query to match anniversaries in the appropriate calendar.
23825d4b688SGreg Roach            $query = DB::table('dates')
23925d4b688SGreg Roach                ->distinct()
24025d4b688SGreg Roach                ->where('d_file', '=', $tree->id())
24125d4b688SGreg Roach                ->where('d_type', '=', $anniv->format('%@'));
242f0a11419SGreg Roach
243f0a11419SGreg Roach            // SIMPLE CASES:
244f0a11419SGreg Roach            // a) Non-hebrew anniversaries
245f0a11419SGreg Roach            // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL
246e364afe4SGreg Roach            if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) {
24725d4b688SGreg Roach                $this->defaultAnniversaries($query, $anniv);
248f0a11419SGreg Roach            } else {
249f0a11419SGreg Roach                // SPECIAL CASES:
2504a83f5d7SGreg Roach                switch ($anniv->month) {
251f0a11419SGreg Roach                    case 2:
25225d4b688SGreg Roach                        $this->cheshvanAnniversaries($query, $anniv);
253f0a11419SGreg Roach                        break;
254f0a11419SGreg Roach                    case 3:
25525d4b688SGreg Roach                        $this->kislevAnniversaries($query, $anniv);
256f0a11419SGreg Roach                        break;
257f0a11419SGreg Roach                    case 4:
25825d4b688SGreg Roach                        $this->tevetAnniversaries($query, $anniv);
259f0a11419SGreg Roach                        break;
26025d4b688SGreg Roach                    case 7:
26125d4b688SGreg Roach                        $this->adarIIAnniversaries($query, $anniv);
262f0a11419SGreg Roach                        break;
26325d4b688SGreg Roach                    case 8:
26425d4b688SGreg Roach                        $this->nisanAnniversaries($query, $anniv);
265f0a11419SGreg Roach                        break;
266f0a11419SGreg Roach                }
267f0a11419SGreg Roach            }
268f0a11419SGreg Roach            // Only events in the past (includes dates without a year)
26925d4b688SGreg Roach            $query->where('d_year', '<=', $anniv->year());
270f0a11419SGreg Roach
271f0a11419SGreg Roach            preg_match_all('/([_A-Z]+)/', $facts, $matches);
27225d4b688SGreg Roach
27325d4b688SGreg Roach            if (!empty($matches[1])) {
27425d4b688SGreg Roach                // Restrict to certain types of fact
27525d4b688SGreg Roach                $query->whereIn('d_fact', $matches[1]);
276f0a11419SGreg Roach            } else {
277f0a11419SGreg Roach                // If no facts specified, get all except these
27825d4b688SGreg Roach                $query->whereNotIn('d_fact', ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', '_TODO']);
279f0a11419SGreg Roach            }
280f0a11419SGreg Roach
28125d4b688SGreg Roach            $query
28294969abbSRico Sonntag                ->orderBy('d_day')
28394969abbSRico Sonntag                ->orderByDesc('d_year');
28425d4b688SGreg Roach
28525d4b688SGreg Roach            $ind_query = (clone $query)
28625d4b688SGreg Roach                ->join('individuals', function (JoinClause $join): void {
28725d4b688SGreg Roach                    $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
28825d4b688SGreg Roach                })
28925d4b688SGreg Roach                ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
29025d4b688SGreg Roach
29125d4b688SGreg Roach            $fam_query = (clone $query)
29225d4b688SGreg Roach                ->join('families', function (JoinClause $join): void {
29325d4b688SGreg Roach                    $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
29425d4b688SGreg Roach                })
29525d4b688SGreg Roach                ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
296f0a11419SGreg Roach
297f0a11419SGreg Roach            // Now fetch these anniversaries
29825d4b688SGreg Roach            foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) {
29925d4b688SGreg Roach                foreach ($query->get() as $row) {
300f0a11419SGreg Roach                    if ($type === 'INDI') {
301f0a11419SGreg Roach                        $record = Individual::getInstance($row->xref, $tree, $row->gedcom);
302f0a11419SGreg Roach                    } else {
303f0a11419SGreg Roach                        $record = Family::getInstance($row->xref, $tree, $row->gedcom);
304f0a11419SGreg Roach                    }
30525d4b688SGreg Roach
306f0a11419SGreg Roach                    $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
30725d4b688SGreg Roach
30830158ae7SGreg Roach                    foreach ($record->facts() as $fact) {
3092decada7SGreg Roach                        if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) {
3104a83f5d7SGreg Roach                            $fact->anniv   = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year;
311f0a11419SGreg Roach                            $fact->jd      = $jd;
312f0a11419SGreg Roach                            $found_facts[] = $fact;
313f0a11419SGreg Roach                        }
314f0a11419SGreg Roach                    }
315f0a11419SGreg Roach                }
316f0a11419SGreg Roach            }
317f0a11419SGreg Roach        }
318f0a11419SGreg Roach
319f0a11419SGreg Roach        return $found_facts;
320f0a11419SGreg Roach    }
32125d4b688SGreg Roach
32225d4b688SGreg Roach    /**
32325d4b688SGreg Roach     * By default, missing days have anniversaries on the first of the month,
32425d4b688SGreg Roach     * and invalid days have anniversaries on the last day of the month.
32525d4b688SGreg Roach     *
32625d4b688SGreg Roach     * @param Builder              $query
32725d4b688SGreg Roach     * @param AbstractCalendarDate $anniv
32825d4b688SGreg Roach     */
32925d4b688SGreg Roach    private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void
33025d4b688SGreg Roach    {
33125d4b688SGreg Roach        if ($anniv->day() === 1) {
33225d4b688SGreg Roach            $query->where('d_day', '<=', 1);
33325d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
33425d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
33525d4b688SGreg Roach        } else {
33625d4b688SGreg Roach            $query->where('d_day', '=', $anniv->day());
33725d4b688SGreg Roach        }
33825d4b688SGreg Roach
33925d4b688SGreg Roach        $query->where('d_mon', '=', $anniv->month());
34025d4b688SGreg Roach    }
34125d4b688SGreg Roach
34225d4b688SGreg Roach    /**
34325d4b688SGreg Roach     * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH).
34425d4b688SGreg Roach     *
34525d4b688SGreg Roach     * @param Builder    $query
34625d4b688SGreg Roach     * @param JewishDate $anniv
34725d4b688SGreg Roach     */
34825d4b688SGreg Roach    private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void
34925d4b688SGreg Roach    {
35025d4b688SGreg Roach        if ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
35125d4b688SGreg Roach            $query
35225d4b688SGreg Roach                ->where('d_mon', '=', 2)
35325d4b688SGreg Roach                ->where('d_day', '>=', 29)
35425d4b688SGreg Roach                ->where('d_day', '<>', 30);
35525d4b688SGreg Roach        } else {
35625d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
35725d4b688SGreg Roach        }
35825d4b688SGreg Roach    }
35925d4b688SGreg Roach
36025d4b688SGreg Roach    /**
36125d4b688SGreg Roach     * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH).
36225d4b688SGreg Roach     * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL).
36325d4b688SGreg Roach     *
36425d4b688SGreg Roach     * @param Builder    $query
36525d4b688SGreg Roach     * @param JewishDate $anniv
36625d4b688SGreg Roach     */
36725d4b688SGreg Roach    private function kislevAnniversaries(Builder $query, JewishDate $anniv): void
36825d4b688SGreg Roach    {
36925d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']);
37025d4b688SGreg Roach
37125d4b688SGreg Roach        if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) {
37225d4b688SGreg Roach            $query->where(function (Builder $query): void {
37325d4b688SGreg Roach                $query->where(function (Builder $query): void {
37425d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 3);
37525d4b688SGreg Roach                })->orWhere(function (Builder $query): void {
37625d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 2);
37725d4b688SGreg Roach                });
37825d4b688SGreg Roach            });
37925d4b688SGreg Roach        } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
38025d4b688SGreg Roach            $query
38125d4b688SGreg Roach                ->where('d_mon', '=', 3)
38225d4b688SGreg Roach                ->where('d_day', '>=', 29)
38325d4b688SGreg Roach                ->where('d_day', '<>', 30);
38425d4b688SGreg Roach        } else {
38525d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
38625d4b688SGreg Roach        }
38725d4b688SGreg Roach    }
38825d4b688SGreg Roach
38925d4b688SGreg Roach    /**
39025d4b688SGreg Roach     * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL).
39125d4b688SGreg Roach     *
39225d4b688SGreg Roach     * @param Builder    $query
39325d4b688SGreg Roach     * @param JewishDate $anniv
39425d4b688SGreg Roach     */
39525d4b688SGreg Roach    private function tevetAnniversaries(Builder $query, JewishDate $anniv): void
39625d4b688SGreg Roach    {
39725d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']);
39825d4b688SGreg Roach
39925d4b688SGreg Roach        if ($anniv->day === 1 && $tmp->daysInMonth() === 29) {
40025d4b688SGreg Roach            $query->where(function (Builder $query): void {
40125d4b688SGreg Roach                $query->where(function (Builder $query): void {
40225d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 4);
40325d4b688SGreg Roach                })->orWhere(function (Builder $query): void {
40425d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 3);
40525d4b688SGreg Roach                });
40625d4b688SGreg Roach            });
40725d4b688SGreg Roach        } else {
40825d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
40925d4b688SGreg Roach        }
41025d4b688SGreg Roach    }
41125d4b688SGreg Roach
41225d4b688SGreg Roach    /**
41325d4b688SGreg Roach     * ADS includes non-leap ADR.
41425d4b688SGreg Roach     *
41525d4b688SGreg Roach     * @param Builder    $query
41625d4b688SGreg Roach     * @param JewishDate $anniv
41725d4b688SGreg Roach     */
41825d4b688SGreg Roach    private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void
41925d4b688SGreg Roach    {
42025d4b688SGreg Roach        if ($anniv->day() === 1) {
42125d4b688SGreg Roach            $query->where('d_day', '<=', 1);
42225d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
42325d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
42425d4b688SGreg Roach        } else {
42525d4b688SGreg Roach            $query->where('d_day', '<=', 1);
42625d4b688SGreg Roach        }
42725d4b688SGreg Roach
42825d4b688SGreg Roach        $query->where(function (Builder $query): void {
42925d4b688SGreg Roach            $query
43025d4b688SGreg Roach                ->where('d_mon', '=', 7)
43125d4b688SGreg Roach                ->orWhere(function (Builder $query): void {
43225d4b688SGreg Roach                    $query
43325d4b688SGreg Roach                        ->where('d_mon', '=', 6)
434*a5145634SGreg Roach                        ->where(DB::raw('(7 * d_year + 1 % 19)'), '>=', 7);
43525d4b688SGreg Roach                });
43625d4b688SGreg Roach        });
43725d4b688SGreg Roach    }
43825d4b688SGreg Roach
43925d4b688SGreg Roach    /**
44025d4b688SGreg Roach     * 1 NSN includes 30 ADR, if this year is non-leap.
44125d4b688SGreg Roach     *
44225d4b688SGreg Roach     * @param Builder    $query
44325d4b688SGreg Roach     * @param JewishDate $anniv
44425d4b688SGreg Roach     */
44525d4b688SGreg Roach    private function nisanAnniversaries(Builder $query, JewishDate $anniv): void
44625d4b688SGreg Roach    {
44725d4b688SGreg Roach        if ($anniv->day === 1 && !$anniv->isLeapYear()) {
44825d4b688SGreg Roach            $query->where(function (Builder $query): void {
44925d4b688SGreg Roach                $query->where(function (Builder $query): void {
45025d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 8);
45125d4b688SGreg Roach                })->orWhere(function (Builder $query): void {
45225d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 6);
45325d4b688SGreg Roach                });
45425d4b688SGreg Roach            });
45525d4b688SGreg Roach        } else {
45625d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
45725d4b688SGreg Roach        }
45825d4b688SGreg Roach    }
459f0a11419SGreg Roach}
460