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; 369e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause; 378af3e5c1SGreg Roachuse Illuminate\Support\Collection; 38*0a614d28SGreg Roachuse function preg_match_all; 39*0a614d28SGreg Roachuse function range; 40f0a11419SGreg Roach 41f0a11419SGreg Roach/** 42f0a11419SGreg Roach * Calculate anniversaries, etc. 43f0a11419SGreg Roach */ 44f0a11419SGreg Roachclass CalendarService 45f0a11419SGreg Roach{ 46*0a614d28SGreg Roach // If no facts specified, get all except these 47*0a614d28SGreg Roach protected const SKIP_FACTS = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR', '_TODO']; 48*0a614d28SGreg Roach 49f0a11419SGreg Roach /** 5070d8159dSGreg Roach * List all the months in a given year. 5170d8159dSGreg Roach * 5270d8159dSGreg Roach * @param string $calendar 5370d8159dSGreg Roach * @param int $year 5470d8159dSGreg Roach * 5570d8159dSGreg Roach * @return string[] 5670d8159dSGreg Roach */ 5770d8159dSGreg Roach public function calendarMonthsInYear(string $calendar, int $year): array 5870d8159dSGreg Roach { 5970d8159dSGreg Roach $date = new Date($calendar . ' ' . $year); 6070d8159dSGreg Roach $calendar_date = $date->minimumDate(); 6170d8159dSGreg Roach $month_numbers = range(1, $calendar_date->monthsInYear()); 6270d8159dSGreg Roach $month_names = []; 6370d8159dSGreg Roach 6470d8159dSGreg Roach foreach ($month_numbers as $month_number) { 654a83f5d7SGreg Roach $calendar_date->day = 1; 664a83f5d7SGreg Roach $calendar_date->month = $month_number; 6770d8159dSGreg Roach $calendar_date->setJdFromYmd(); 6870d8159dSGreg Roach 6970d8159dSGreg Roach if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 7070d8159dSGreg Roach // No month 6 in Jewish non-leap years. 7170d8159dSGreg Roach continue; 7270d8159dSGreg Roach } 7370d8159dSGreg Roach 7470d8159dSGreg Roach if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 7570d8159dSGreg Roach // Month 7 is ADR in Jewish non-leap years (and ADS in others). 7670d8159dSGreg Roach $mon = 'ADR'; 7770d8159dSGreg Roach } else { 7870d8159dSGreg Roach $mon = $calendar_date->format('%O'); 7970d8159dSGreg Roach } 8070d8159dSGreg Roach 8170d8159dSGreg Roach $month_names[$mon] = $calendar_date->format('%F'); 8270d8159dSGreg Roach } 8370d8159dSGreg Roach 8470d8159dSGreg Roach return $month_names; 8570d8159dSGreg Roach } 8670d8159dSGreg Roach 8770d8159dSGreg Roach /** 88f0a11419SGreg Roach * Get a list of events which occured during a given date range. 89f0a11419SGreg Roach * 90f0a11419SGreg Roach * @param int $jd1 the start range of julian day 91f0a11419SGreg Roach * @param int $jd2 the end range of julian day 92*0a614d28SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 93f0a11419SGreg Roach * @param Tree $tree the tree to search 94f0a11419SGreg Roach * 95f0a11419SGreg Roach * @return Fact[] 96f0a11419SGreg Roach */ 97*0a614d28SGreg Roach public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array 98f0a11419SGreg Roach { 99f0a11419SGreg Roach // Events that start or end during the period 10025d4b688SGreg Roach $query = DB::table('dates') 1019e87c1a2SGreg Roach ->where('d_file', '=', $tree->id()) 1020b5fd0a6SGreg Roach ->where(static function (Builder $query) use ($jd1, $jd2): void { 1030b5fd0a6SGreg Roach $query->where(static function (Builder $query) use ($jd1, $jd2): void { 1049e87c1a2SGreg Roach $query 1059e87c1a2SGreg Roach ->where('d_julianday1', '>=', $jd1) 1069e87c1a2SGreg Roach ->where('d_julianday1', '<=', $jd2); 1070b5fd0a6SGreg Roach })->orWhere(static function (Builder $query) use ($jd1, $jd2): void { 1089e87c1a2SGreg Roach $query 1099e87c1a2SGreg Roach ->where('d_julianday2', '>=', $jd1) 1109e87c1a2SGreg Roach ->where('d_julianday2', '<=', $jd2); 1119e87c1a2SGreg Roach }); 1129e87c1a2SGreg Roach }); 113f0a11419SGreg Roach 114f0a11419SGreg Roach // Restrict to certain types of fact 115*0a614d28SGreg Roach if ($facts === '') { 116*0a614d28SGreg Roach $query->whereNotIn('d_fact', self::SKIP_FACTS); 117f0a11419SGreg Roach } else { 118*0a614d28SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 119*0a614d28SGreg Roach 120*0a614d28SGreg Roach $query->whereIn('d_fact', $matches[1]); 121f0a11419SGreg Roach } 122f0a11419SGreg Roach 12325d4b688SGreg Roach $ind_query = (clone $query) 1240b5fd0a6SGreg Roach ->join('individuals', static function (JoinClause $join): void { 12525d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 12625d4b688SGreg Roach }) 12725d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 128f0a11419SGreg Roach 12925d4b688SGreg Roach $fam_query = (clone $query) 1300b5fd0a6SGreg Roach ->join('families', static function (JoinClause $join): void { 13125d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 13225d4b688SGreg Roach }) 13325d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']); 13425d4b688SGreg Roach 13525d4b688SGreg Roach // Now fetch these events 13625d4b688SGreg Roach $found_facts = []; 13725d4b688SGreg Roach 13825d4b688SGreg Roach foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) { 13925d4b688SGreg Roach foreach ($query->get() as $row) { 140f0a11419SGreg Roach if ($type === 'INDI') { 141f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 142f0a11419SGreg Roach } else { 143f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 144f0a11419SGreg Roach } 14525d4b688SGreg Roach 146f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 14725d4b688SGreg Roach 14830158ae7SGreg Roach foreach ($record->facts() as $fact) { 149f0a11419SGreg Roach // For date ranges, we need a match on either the start/end. 15025d4b688SGreg Roach if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 151f0a11419SGreg Roach $fact->anniv = 0; 152f0a11419SGreg Roach $found_facts[] = $fact; 153f0a11419SGreg Roach } 154f0a11419SGreg Roach } 155f0a11419SGreg Roach } 156f0a11419SGreg Roach } 157f0a11419SGreg Roach 158f0a11419SGreg Roach return $found_facts; 159f0a11419SGreg Roach } 160f0a11419SGreg Roach 161f0a11419SGreg Roach /** 162f0a11419SGreg Roach * Get the list of current and upcoming events, sorted by anniversary date 163f0a11419SGreg Roach * 164f0a11419SGreg Roach * @param int $jd1 165f0a11419SGreg Roach * @param int $jd2 166f0a11419SGreg Roach * @param string $events 167c7ff4153SGreg Roach * @param bool $only_living 168f0a11419SGreg Roach * @param string $sort_by 169f0a11419SGreg Roach * @param Tree $tree 170f0a11419SGreg Roach * 171f0a11419SGreg Roach * @return Fact[] 172f0a11419SGreg Roach */ 173f0a11419SGreg Roach public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array 174f0a11419SGreg Roach { 175f0a11419SGreg Roach $found_facts = []; 176f0a11419SGreg Roach $facts = []; 177f0a11419SGreg Roach 178f0a11419SGreg Roach foreach (range($jd1, $jd2) as $jd) { 179f0a11419SGreg Roach $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 180f0a11419SGreg Roach } 181f0a11419SGreg Roach 182f0a11419SGreg Roach foreach ($found_facts as $fact) { 183e7766c08SGreg Roach $record = $fact->record(); 184f0a11419SGreg Roach // only living people ? 185f0a11419SGreg Roach if ($only_living) { 186f0a11419SGreg Roach if ($record instanceof Individual && $record->isDead()) { 187f0a11419SGreg Roach continue; 188f0a11419SGreg Roach } 189f0a11419SGreg Roach if ($record instanceof Family) { 19039ca88baSGreg Roach $husb = $record->husband(); 1918f038c36SRico Sonntag if ($husb === null || $husb->isDead()) { 192f0a11419SGreg Roach continue; 193f0a11419SGreg Roach } 19439ca88baSGreg Roach $wife = $record->wife(); 1958f038c36SRico Sonntag if ($wife === null || $wife->isDead()) { 196f0a11419SGreg Roach continue; 197f0a11419SGreg Roach } 198f0a11419SGreg Roach } 199f0a11419SGreg Roach } 200f0a11419SGreg Roach $facts[] = $fact; 201f0a11419SGreg Roach } 202f0a11419SGreg Roach 203f0a11419SGreg Roach switch ($sort_by) { 204f0a11419SGreg Roach case 'anniv': 2058af3e5c1SGreg Roach $facts = Fact::sortFacts(Collection::make($facts))->all(); 206f0a11419SGreg Roach break; 207580a4d11SGreg Roach 208f0a11419SGreg Roach case 'alpha': 2090b5fd0a6SGreg Roach uasort($facts, static function (Fact $x, Fact $y): int { 210c156e8f5SGreg Roach return GedcomRecord::nameComparator()($x->record(), $y->record()); 211f0a11419SGreg Roach }); 212f0a11419SGreg Roach break; 213f0a11419SGreg Roach } 214f0a11419SGreg Roach 215f0a11419SGreg Roach return $facts; 216f0a11419SGreg Roach } 217f0a11419SGreg Roach 218f0a11419SGreg Roach /** 219f0a11419SGreg Roach * Get a list of events whose anniversary occured on a given julian day. 220f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 221f0a11419SGreg Roach * 222f0a11419SGreg Roach * @param int $jd the julian day 223f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 224f0a11419SGreg Roach * @param Tree $tree the tree to search 225f0a11419SGreg Roach * 226f0a11419SGreg Roach * @return Fact[] 227f0a11419SGreg Roach */ 22825d4b688SGreg Roach public function getAnniversaryEvents($jd, string $facts, Tree $tree): array 229f0a11419SGreg Roach { 230f0a11419SGreg Roach $found_facts = []; 231f0a11419SGreg Roach 232f0a11419SGreg Roach $anniversaries = [ 233f0a11419SGreg Roach new GregorianDate($jd), 234f0a11419SGreg Roach new JulianDate($jd), 235f0a11419SGreg Roach new FrenchDate($jd), 236f0a11419SGreg Roach new JewishDate($jd), 237f0a11419SGreg Roach new HijriDate($jd), 238f0a11419SGreg Roach ]; 239f0a11419SGreg Roach 2404a9ac934SGreg Roach // @TODO - there is a bug in the Persian Calendar that gives zero months for invalid dates 2414a9ac934SGreg Roach if ($jd > (new PersianCalendar())->jdStart()) { 2424a9ac934SGreg Roach $anniversaries[] = new JalaliDate($jd); 2434a9ac934SGreg Roach } 2444a9ac934SGreg Roach 245f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 24625d4b688SGreg Roach // Build a query to match anniversaries in the appropriate calendar. 24725d4b688SGreg Roach $query = DB::table('dates') 24825d4b688SGreg Roach ->distinct() 24925d4b688SGreg Roach ->where('d_file', '=', $tree->id()) 25025d4b688SGreg Roach ->where('d_type', '=', $anniv->format('%@')); 251f0a11419SGreg Roach 252f0a11419SGreg Roach // SIMPLE CASES: 253f0a11419SGreg Roach // a) Non-hebrew anniversaries 254f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 255e364afe4SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) { 25625d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 257f0a11419SGreg Roach } else { 258f0a11419SGreg Roach // SPECIAL CASES: 2594a83f5d7SGreg Roach switch ($anniv->month) { 260f0a11419SGreg Roach case 2: 26125d4b688SGreg Roach $this->cheshvanAnniversaries($query, $anniv); 262f0a11419SGreg Roach break; 263f0a11419SGreg Roach case 3: 26425d4b688SGreg Roach $this->kislevAnniversaries($query, $anniv); 265f0a11419SGreg Roach break; 266f0a11419SGreg Roach case 4: 26725d4b688SGreg Roach $this->tevetAnniversaries($query, $anniv); 268f0a11419SGreg Roach break; 26925d4b688SGreg Roach case 7: 27025d4b688SGreg Roach $this->adarIIAnniversaries($query, $anniv); 271f0a11419SGreg Roach break; 27225d4b688SGreg Roach case 8: 27325d4b688SGreg Roach $this->nisanAnniversaries($query, $anniv); 274f0a11419SGreg Roach break; 275f0a11419SGreg Roach } 276f0a11419SGreg Roach } 277f0a11419SGreg Roach // Only events in the past (includes dates without a year) 27825d4b688SGreg Roach $query->where('d_year', '<=', $anniv->year()); 279f0a11419SGreg Roach 280*0a614d28SGreg Roach if ($facts === '') { 281*0a614d28SGreg Roach // If no facts specified, get all except these 282*0a614d28SGreg Roach $query->whereNotIn('d_fact', self::SKIP_FACTS); 283*0a614d28SGreg Roach } else { 284*0a614d28SGreg Roach // Restrict to certain types of fact 285f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 28625d4b688SGreg Roach 28725d4b688SGreg Roach $query->whereIn('d_fact', $matches[1]); 288f0a11419SGreg Roach } 289f0a11419SGreg Roach 29025d4b688SGreg Roach $query 29194969abbSRico Sonntag ->orderBy('d_day') 29294969abbSRico Sonntag ->orderByDesc('d_year'); 29325d4b688SGreg Roach 29425d4b688SGreg Roach $ind_query = (clone $query) 2950b5fd0a6SGreg Roach ->join('individuals', static function (JoinClause $join): void { 29625d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 29725d4b688SGreg Roach }) 29825d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 29925d4b688SGreg Roach 30025d4b688SGreg Roach $fam_query = (clone $query) 3010b5fd0a6SGreg Roach ->join('families', static function (JoinClause $join): void { 30225d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 30325d4b688SGreg Roach }) 30425d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 305f0a11419SGreg Roach 306f0a11419SGreg Roach // Now fetch these anniversaries 30725d4b688SGreg Roach foreach (['INDI' => $ind_query, 'FAM' => $fam_query] as $type => $query) { 30825d4b688SGreg Roach foreach ($query->get() as $row) { 309f0a11419SGreg Roach if ($type === 'INDI') { 310f0a11419SGreg Roach $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 311f0a11419SGreg Roach } else { 312f0a11419SGreg Roach $record = Family::getInstance($row->xref, $tree, $row->gedcom); 313f0a11419SGreg Roach } 31425d4b688SGreg Roach 315f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 31625d4b688SGreg Roach 31730158ae7SGreg Roach foreach ($record->facts() as $fact) { 3182decada7SGreg Roach if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 3194a83f5d7SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 320f0a11419SGreg Roach $fact->jd = $jd; 321f0a11419SGreg Roach $found_facts[] = $fact; 322f0a11419SGreg Roach } 323f0a11419SGreg Roach } 324f0a11419SGreg Roach } 325f0a11419SGreg Roach } 326f0a11419SGreg Roach } 327f0a11419SGreg Roach 328f0a11419SGreg Roach return $found_facts; 329f0a11419SGreg Roach } 33025d4b688SGreg Roach 33125d4b688SGreg Roach /** 33225d4b688SGreg Roach * By default, missing days have anniversaries on the first of the month, 33325d4b688SGreg Roach * and invalid days have anniversaries on the last day of the month. 33425d4b688SGreg Roach * 33525d4b688SGreg Roach * @param Builder $query 33625d4b688SGreg Roach * @param AbstractCalendarDate $anniv 33725d4b688SGreg Roach */ 33825d4b688SGreg Roach private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void 33925d4b688SGreg Roach { 34025d4b688SGreg Roach if ($anniv->day() === 1) { 34125d4b688SGreg Roach $query->where('d_day', '<=', 1); 34225d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 34325d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 34425d4b688SGreg Roach } else { 34525d4b688SGreg Roach $query->where('d_day', '=', $anniv->day()); 34625d4b688SGreg Roach } 34725d4b688SGreg Roach 34825d4b688SGreg Roach $query->where('d_mon', '=', $anniv->month()); 34925d4b688SGreg Roach } 35025d4b688SGreg Roach 35125d4b688SGreg Roach /** 35225d4b688SGreg Roach * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH). 35325d4b688SGreg Roach * 35425d4b688SGreg Roach * @param Builder $query 35525d4b688SGreg Roach * @param JewishDate $anniv 35625d4b688SGreg Roach */ 35725d4b688SGreg Roach private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void 35825d4b688SGreg Roach { 35925d4b688SGreg Roach if ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 36025d4b688SGreg Roach $query 36125d4b688SGreg Roach ->where('d_mon', '=', 2) 36225d4b688SGreg Roach ->where('d_day', '>=', 29) 36325d4b688SGreg Roach ->where('d_day', '<>', 30); 36425d4b688SGreg Roach } else { 36525d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 36625d4b688SGreg Roach } 36725d4b688SGreg Roach } 36825d4b688SGreg Roach 36925d4b688SGreg Roach /** 37025d4b688SGreg Roach * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH). 37125d4b688SGreg Roach * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL). 37225d4b688SGreg Roach * 37325d4b688SGreg Roach * @param Builder $query 37425d4b688SGreg Roach * @param JewishDate $anniv 37525d4b688SGreg Roach */ 37625d4b688SGreg Roach private function kislevAnniversaries(Builder $query, JewishDate $anniv): void 37725d4b688SGreg Roach { 37825d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']); 37925d4b688SGreg Roach 38025d4b688SGreg Roach if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) { 3810b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 3820b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 38325d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 3); 3840b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 38525d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 2); 38625d4b688SGreg Roach }); 38725d4b688SGreg Roach }); 38825d4b688SGreg Roach } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 38925d4b688SGreg Roach $query 39025d4b688SGreg Roach ->where('d_mon', '=', 3) 39125d4b688SGreg Roach ->where('d_day', '>=', 29) 39225d4b688SGreg Roach ->where('d_day', '<>', 30); 39325d4b688SGreg Roach } else { 39425d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 39525d4b688SGreg Roach } 39625d4b688SGreg Roach } 39725d4b688SGreg Roach 39825d4b688SGreg Roach /** 39925d4b688SGreg Roach * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL). 40025d4b688SGreg Roach * 40125d4b688SGreg Roach * @param Builder $query 40225d4b688SGreg Roach * @param JewishDate $anniv 40325d4b688SGreg Roach */ 40425d4b688SGreg Roach private function tevetAnniversaries(Builder $query, JewishDate $anniv): void 40525d4b688SGreg Roach { 40625d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']); 40725d4b688SGreg Roach 40825d4b688SGreg Roach if ($anniv->day === 1 && $tmp->daysInMonth() === 29) { 4090b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4100b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 41125d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 4); 4120b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 41325d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 3); 41425d4b688SGreg Roach }); 41525d4b688SGreg Roach }); 41625d4b688SGreg Roach } else { 41725d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 41825d4b688SGreg Roach } 41925d4b688SGreg Roach } 42025d4b688SGreg Roach 42125d4b688SGreg Roach /** 42225d4b688SGreg Roach * ADS includes non-leap ADR. 42325d4b688SGreg Roach * 42425d4b688SGreg Roach * @param Builder $query 42525d4b688SGreg Roach * @param JewishDate $anniv 42625d4b688SGreg Roach */ 42725d4b688SGreg Roach private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void 42825d4b688SGreg Roach { 42925d4b688SGreg Roach if ($anniv->day() === 1) { 43025d4b688SGreg Roach $query->where('d_day', '<=', 1); 43125d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 43225d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 43325d4b688SGreg Roach } else { 43425d4b688SGreg Roach $query->where('d_day', '<=', 1); 43525d4b688SGreg Roach } 43625d4b688SGreg Roach 4370b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 43825d4b688SGreg Roach $query 43925d4b688SGreg Roach ->where('d_mon', '=', 7) 4400b5fd0a6SGreg Roach ->orWhere(static function (Builder $query): void { 44125d4b688SGreg Roach $query 44225d4b688SGreg Roach ->where('d_mon', '=', 6) 443a5145634SGreg Roach ->where(DB::raw('(7 * d_year + 1 % 19)'), '>=', 7); 44425d4b688SGreg Roach }); 44525d4b688SGreg Roach }); 44625d4b688SGreg Roach } 44725d4b688SGreg Roach 44825d4b688SGreg Roach /** 44925d4b688SGreg Roach * 1 NSN includes 30 ADR, if this year is non-leap. 45025d4b688SGreg Roach * 45125d4b688SGreg Roach * @param Builder $query 45225d4b688SGreg Roach * @param JewishDate $anniv 45325d4b688SGreg Roach */ 45425d4b688SGreg Roach private function nisanAnniversaries(Builder $query, JewishDate $anniv): void 45525d4b688SGreg Roach { 45625d4b688SGreg Roach if ($anniv->day === 1 && !$anniv->isLeapYear()) { 4570b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4580b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 45925d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 8); 4600b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 46125d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 6); 46225d4b688SGreg Roach }); 46325d4b688SGreg Roach }); 46425d4b688SGreg Roach } else { 46525d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 46625d4b688SGreg Roach } 46725d4b688SGreg Roach } 468f0a11419SGreg Roach} 469