Skip to content

Commit

Permalink
Added cron job for processing pre order bookings
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapjansma committed Apr 3, 2024
1 parent 723601e commit 7918672
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Controller/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function massBooking(Request $request): Response
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$insertIds = [];
foreach($data['product_ids'] as $product_id) {
$product = Product::findOneBy('sku', $product_id['sku']);
if ($product) {
Expand Down Expand Up @@ -213,14 +214,12 @@ public function massBooking(Request $request): Response
}
$creditBookingLine->pid = $booking->id;
$creditBookingLine->save();
BookingHelper::updateBalanceStatusForBooking($booking->id);

$event = new ManualBookingEvent($booking);
System::getContainer()
->get('event_dispatcher')
->dispatch($event, Events::MANUAL_BOOKING_EVENT);
$insertIds[] = "(" . $booking->id . ")";
}
}
if (count($insertIds)) {
\Database::getInstance()->execute("INSERT INTO `tl_isotope_stock_booking_event` (`booking_id`) VALUES " . implode(", ", $insertIds));
}
$url = $this->generateUrl('contao_backend', ['do' => 'tl_isotope_stock_booking']);
return new RedirectResponse($url);
}
Expand Down
66 changes: 66 additions & 0 deletions src/Cron/CheckBookingEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright (C) 2024 Jaap Jansma ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Krabo\IsotopeStockBundle\Cron;

use Contao\CoreBundle\ServiceAnnotation\CronJob;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Database;
use Contao\System;
use Isotope\Model\Product;
use Krabo\IsotopeStockBundle\Event\Events;
use Krabo\IsotopeStockBundle\Event\ManualBookingEvent;
use Krabo\IsotopeStockBundle\Helper\BookingHelper;
use Krabo\IsotopeStockBundle\Model\BookingModel;

/**
* @CronJob("minutely")
*/
class CheckBookingEvents
{

/**
* @param \Contao\CoreBundle\Framework\ContaoFramework $contaoFramework
*/
public function __construct(ContaoFramework $contaoFramework)
{
$contaoFramework->initialize();
}

public function __invoke(): void
{
$config = System::getContainer()->getParameter('jvh.jvh_isotope_stock.config');
/** @var Database $db */
$db = System::importStatic('Database');
$objResult = $db->execute("SELECT * FROM `tl_isotope_stock_booking_event` LIMIT 0, 1");
$ids = [];
while($objResult->next()) {
$ids[] = $objResult->id;
$booking = BookingModel::findByPk($objResult->booking_id);
BookingHelper::updateBalanceStatusForBooking($booking->id);
$event = new ManualBookingEvent($booking);
System::getContainer()
->get('event_dispatcher')
->dispatch($event, Events::MANUAL_BOOKING_EVENT);
}
if (count($ids)) {
$sql = "DELETE FROM `tl_isotope_stock_booking_event` WHERE `id` IN (" . implode(", ", $ids) . ")";
$db->execute($sql);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
Krabo\IsotopeStockBundle\Controller\BookingController: ~
Krabo\IsotopeStockBundle\Controller\OverviewController: ~
Krabo\IsotopeStockBundle\Cron\UpdateTstamp: ~
Krabo\IsotopeStockBundle\Cron\CheckBookingEvents: ~
Krabo\IsotopeStockBundle\EventListener\KernelListener:
public: true
Krabo\IsotopeStockBundle\EventListener\ProductListener:
Expand Down
51 changes: 51 additions & 0 deletions src/Resources/contao/dca/tl_isotope_stock_booking_event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright (C) 2022 Jaap Jansma ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

$GLOBALS['TL_DCA']['tl_isotope_stock_booking_event'] = array
(
// Config
'config' => array
(
'dataContainer' => 'Table',
'switchToEdit' => true,
'sql' => array
(
'keys' => array
(
'id' => 'primary'
)
),
),

// Fields
'fields' => array
(
'id' => array
(
'sql' => "int(10) unsigned NOT NULL auto_increment"
),
'tstamp' => array
(
'sql' => "int(10) unsigned NOT NULL default 0"
),
'booking_id' => array
(
'sql' => "int(10) unsigned NOT NULL default 0"
),
)
);

0 comments on commit 7918672

Please sign in to comment.