| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2024 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* VikBooking payment schedules controller. |
| 16 |
* |
| 17 |
* @since 1.16.10 (J) - 1.6.10 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingControllerPayschedules extends JControllerAdmin |
| 20 |
{ |
| 21 |
/** |
| 22 |
* AJAX endpoint to save a new payment schedule. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function save() |
| 27 |
{ |
| 28 |
$app = JFactory::getApplication(); |
| 29 |
|
| 30 |
if (!JSession::checkToken()) { |
| 31 |
VBOHttpDocument::getInstance()->close(403, JText::translate('JINVALID_TOKEN')); |
| 32 |
} |
| 33 |
|
| 34 |
$bid = $app->input->getInt('bid'); |
| 35 |
$amount = $app->input->getFloat('amount'); |
| 36 |
$dt = $app->input->getString('dt'); |
| 37 |
$time = $app->input->getString('time', '00:00'); |
| 38 |
|
| 39 |
if (!$bid || !$amount || !$dt || !$time) { |
| 40 |
VBOHttpDocument::getInstance()->close(400, JText::translate('VBO_PLEASE_FILL_FIELDS')); |
| 41 |
} |
| 42 |
|
| 43 |
$booking = VikBooking::getBookingInfoFromID($bid); |
| 44 |
if (!$booking) { |
| 45 |
VBOHttpDocument::getInstance()->close(404, JText::translate('VBO_NO_RECORDS_FOUND')); |
| 46 |
} |
| 47 |
|
| 48 |
// current date object |
| 49 |
$now_dt = JFactory::getdate('now'); |
| 50 |
|
| 51 |
// access the time with hours and minutes |
| 52 |
$time_parts = explode(':', $time); |
| 53 |
$time_hours = (int) $time_parts[0]; |
| 54 |
$time_minutes = (int) ($time_parts[1] ?? 0); |
| 55 |
|
| 56 |
// build date timestamp and string |
| 57 |
$dt_ts = VikBooking::getDateTimestamp($dt, $time_hours, $time_minutes, 0); |
| 58 |
$dt_military = date('Y-m-d H:i:s', $dt_ts); |
| 59 |
$for_dt_obj = JFactory::getDate($dt_military); |
| 60 |
|
| 61 |
if ($now_dt > $for_dt_obj) { |
| 62 |
// payment date cannot be in the past |
| 63 |
VBOHttpDocument::getInstance()->close(400, 'Payment collection date and time must be in the future (current date and time is ' . $now_dt->format('Y-m-d H:i:s') . ')'); |
| 64 |
} |
| 65 |
|
| 66 |
$user = JFactory::getUser(); |
| 67 |
$created_by = $user->name; |
| 68 |
|
| 69 |
// prepare record to be saved |
| 70 |
$pay_schedule = new stdClass; |
| 71 |
$pay_schedule->idorder = $booking['id']; |
| 72 |
$pay_schedule->fordt = $for_dt_obj->toSql(); |
| 73 |
$pay_schedule->amount = $amount; |
| 74 |
$pay_schedule->status = 0; |
| 75 |
$pay_schedule->created_on = $now_dt->toSql(); |
| 76 |
$pay_schedule->created_by = $created_by; |
| 77 |
|
| 78 |
// access the model |
| 79 |
$model = VBOModelPayschedules::getInstance(); |
| 80 |
|
| 81 |
if (!$model->save($pay_schedule)) { |
| 82 |
VBOHttpDocument::getInstance()->close(500, 'Could not store the payment schedule record'); |
| 83 |
} |
| 84 |
|
| 85 |
// reload all active schedules for this booking |
| 86 |
$active_payschedules = $model->getItems([ |
| 87 |
'idorder' => [ |
| 88 |
'value' => $booking['id'], |
| 89 |
], |
| 90 |
]); |
| 91 |
|
| 92 |
// output the JSON encoded list of active payment schedules for this booking |
| 93 |
VBOHttpDocument::getInstance()->json($active_payschedules); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* AJAX endpoint to delete an existing payment schedule. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function delete() |
| 102 |
{ |
| 103 |
$app = JFactory::getApplication(); |
| 104 |
$dbo = JFactory::getDbo(); |
| 105 |
|
| 106 |
if (!JSession::checkToken()) { |
| 107 |
VBOHttpDocument::getInstance()->close(403, JText::translate('JINVALID_TOKEN')); |
| 108 |
} |
| 109 |
|
| 110 |
$bid = $app->input->getInt('bid'); |
| 111 |
$schedule_id = $app->input->getInt('schedule_id'); |
| 112 |
|
| 113 |
if (!$bid || !$schedule_id) { |
| 114 |
VBOHttpDocument::getInstance()->close(400, 'Missing data to delete the record'); |
| 115 |
} |
| 116 |
|
| 117 |
// delete the requested record |
| 118 |
$dbo->setQuery( |
| 119 |
$dbo->getQuery(true) |
| 120 |
->delete($dbo->qn('#__vikbooking_payschedules')) |
| 121 |
->where($dbo->qn('id') . ' = ' . $schedule_id) |
| 122 |
->where($dbo->qn('idorder') . ' = ' . $bid) |
| 123 |
); |
| 124 |
|
| 125 |
$dbo->execute(); |
| 126 |
|
| 127 |
// access the model |
| 128 |
$model = VBOModelPayschedules::getInstance(); |
| 129 |
|
| 130 |
// reload all schedules for this booking |
| 131 |
$active_payschedules = $model->getItems([ |
| 132 |
'idorder' => [ |
| 133 |
'value' => $bid, |
| 134 |
], |
| 135 |
]); |
| 136 |
|
| 137 |
// output the JSON encoded list of active payment schedules for this booking |
| 138 |
VBOHttpDocument::getInstance()->json($active_payschedules); |
| 139 |
} |
| 140 |
} |
| 141 |
|