Last active
October 20, 2021 12:22
-
-
Save jmosul/6a101bc45696e01adcfed50b4fcb8563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DesignMyNight\Collins\Services\PreorderManager; | |
if(!defined('ABSPATH')) { | |
require_once('/srv/core/current/site-config.php'); | |
require_once('/srv/core/current/includes/solr-php/Service.php'); | |
} | |
global $mongo, $mongoDB, $user, $request, $site; | |
ini_set('xdebug.var_display_max_depth', 9); | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
ini_set('memory_limit', -1); | |
error_reporting(E_STRICT); | |
$site = new Site(); | |
$request = new Request(); | |
UserUtils::cookieConstants(); | |
UserUtils::getCurrentUser(); | |
$venueGroup = '514ada610df690b6770000fd'; | |
$dryRun = true; | |
$bookingTypes = [ | |
'5d5ea62408561856dd3ad97e', | |
'5d5ea62408561856dd3ad980', | |
'61405a7bce8a2360df6efbfb', | |
'5fa17f679753250a897add88', | |
'5a6f16c901be4b19894b663b', | |
'5a6f265e01be4b4090694e53', | |
'5c4891f7293c784e6c6c0e84', | |
'5f3cf6eab9a17e489c701a82', | |
'5f3e30dc0793df27cc114d17', | |
'5f3e33f92a310d015e01b402', | |
'5f869c33c94f952f8770d764', | |
'5f869c33c94f952f8770d765', | |
'59a6a4883676be05532491d0', | |
'5b472c21cd31d35d4a4101a4', | |
'5b647dea93a9fa582231f184', | |
'558d6d6b6197f6b72a8b456b', | |
'5da5fcd41e70c9141934eadc', | |
'5f6c7b4512ac06390b1f695f', | |
'5f6c7b4512ac06390b1f6960', | |
'5c6eba9adf4f5625234b0513', | |
'5f7d7421cc930a7b574c9d07', | |
'5f647b99f954171f28064a5c', | |
'5f647b99f954171f28064a5d', | |
'614c4c5483ceca0bbb21e23f', | |
'5d480e60ffd4a16776472dde', | |
'5fc120b6d7b1da3e221746f5', | |
'5fda424308f59c1daf7faac2', | |
'5d972e290ad93818f217d629', | |
'5d972e290ad93818f217d628', | |
'5f3e353097d4a056031d888d', | |
]; | |
class StoneGatePreOrders { | |
/** @var array */ | |
private $bookingTypes; | |
/** @var bool */ | |
private $dryRun = false; | |
/** @var int */ | |
private $total; | |
/** @var VenueGroup */ | |
private $venueGroup; | |
/** @var array */ | |
private $venues = []; | |
public function __construct(VenueGroup $venueGroup, array $bookingTypes) { | |
$this->venueGroup = $venueGroup; | |
$this->bookingTypes = $bookingTypes; | |
} | |
/** | |
* @param bool $dryRun | |
* @throws Exception | |
*/ | |
public function handle(bool $dryRun = true) { | |
$this->dryRun = $dryRun; | |
$count = 1; | |
echo PHP_EOL; | |
echo $this->dryRun ? "DRY RUN MODE" : "LIVE MODE"; | |
echo PHP_EOL; | |
foreach($this->getBookings() as $bookingData) { | |
$booking = new Booking($bookingData); | |
echo sprintf('%04d', $count); | |
echo "/{$this->total} "; | |
echo "Booking {$booking->getId()} - "; | |
// remove _id so we can pretend this booking has not yet been created | |
unset($bookingData['_id']); | |
$fakeBooking = new Booking($bookingData); | |
// use fakeBookings to find the correct preorder settings that should have been added at creation | |
$preOrderSettings = $this->getPreOrderSettings($fakeBooking); | |
// copy preorder settings to actual booking | |
if($preOrderSettings) { | |
$booking->customer_preorder_settings = $preOrderSettings; | |
if(!$this->dryRun) { | |
echo " SAVED "; | |
$booking->save(); | |
} | |
echo "menus added"; | |
} | |
else { | |
echo "NA"; | |
} | |
echo PHP_EOL; | |
$count++; | |
} | |
echo PHP_EOL; | |
echo "COMPLETE"; | |
echo PHP_EOL; | |
} | |
/** | |
* uses a fake booking to find the preorder settings, mostly copied from CustomerBookingService.php:209 | |
* | |
* @param Booking $fakeBooking | |
* @return null | |
* @throws Exception | |
*/ | |
private function getPreOrderSettings(Booking $fakeBooking) { | |
$bookingTypeDetails = $fakeBooking->getTypeDetails(); | |
$venue = $this->getVenue($fakeBooking); | |
$isSendEmailBeforeBookingValid = isset($bookingTypeDetails['customer_preorder_settings']['send_email_before_booking']); | |
$preorderCloseDate = PreorderManager::resolvePreorderClosedDate($fakeBooking, $venue); | |
$isPreOrderOpen = DateUtils::isDateInFuture($preorderCloseDate); | |
// Open pre-orders | |
if ( | |
$isPreOrderOpen || | |
$isSendEmailBeforeBookingValid | |
){ | |
$preorderOptions = array( | |
'payment_required' => isset($bookingTypeDetails['customer_preorder_settings']['payment_required']) ? $bookingTypeDetails['customer_preorder_settings']['payment_required'] : 'full_amount' | |
); | |
if ($limitItems = $bookingTypeDetails['customer_preorder_settings']['limit_preorders'] ?? null) { | |
$preorderOptions['limit_preorders'] = $limitItems; | |
} | |
if (isset($bookingTypeDetails['customer_preorder_settings']['payment_percentage'])) { | |
$preorderOptions['payment_percentage'] = $bookingTypeDetails['customer_preorder_settings']['payment_percentage']; | |
} | |
$preorderOptions['preorder_close_date'] = $preorderCloseDate; | |
PreorderManager::openBookingPreorders($fakeBooking, $preorderOptions); | |
} | |
return !empty($fakeBooking->customer_preorder_settings) ? $fakeBooking->customer_preorder_settings : null; | |
} | |
/** | |
* @return Generator|array[] | |
*/ | |
private function getBookings():\Generator { | |
global $mongoDB; | |
$query = [ | |
'venue_group' => $this->venueGroup->getId('bson'), | |
'date' => [ | |
'$gte' => DateUtils::formatDate('2021-11-01', 'bson'), | |
'$lt' => DateUtils::formatDate('2022-02-01', 'bson'), | |
], | |
'created_date' => [ | |
'$lt' => DateUtils::formatDate('2021-10-10', 'bson'), | |
], | |
'preorders.0' => [ | |
'$exists' => false | |
], | |
'customer_preorder_settings' => [ | |
'$exists' => false | |
], | |
'type.id' => [ | |
'$in' => array_map(function(string $typeId) { | |
return new \MongoDB\BSON\ObjectId($typeId); | |
}, $this->bookingTypes), | |
], | |
'status' => [ | |
'$in' => [\Booking::STATUS_COMPLETE, \Booking::STATUS_IN_PROGRESS, \Booking::STATUS_NEW], | |
], | |
'lost' => [ | |
'$ne' => true | |
] | |
]; | |
$this->total = $mongoDB->bookings->count($query); | |
echo "Total Bookings Found: {$this->total}"; | |
echo PHP_EOL; | |
echo PHP_EOL; | |
$bookings = $mongoDB->bookings->find($query); | |
foreach($bookings as $booking) { | |
yield $booking; | |
} | |
} | |
/** | |
* Keeps cache of venues | |
*/ | |
private function getVenue(Booking $booking) { | |
$venueId = (string)$booking->venue_id; | |
if (!isset($this->venues[$venueId])) { | |
$this->venues[$venueId] = $booking->getVenue(); | |
} | |
$booking->venue_object = $this->venues[$venueId]; | |
return $booking->getVenue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment