hmaddy Posted May 12, 2023 Share Posted May 12, 2023 this code is not working <?php use WHMCS\Database\Capsule; add_hook('DailyCronJob', 1, function($vars) { // Perform hook code here... ##Database Data Collection #foreach (Capsule::table('tblhosting')->where("domainstatus", "=", "Active") ->where("packageid", "=", 264) ->orwhere("packageid", "=", 265) ->get() as $hosting) { foreach (Capsule::table('tblhosting')->where("domainstatus", "=", "Active") ->whereIn("packageid", [264,265]) ->get() as $hosting) { $auditdate=date_create(date('Y-m-d', strtotime($hosting->nextinvoicedate. ' - 30 days'))); $auditordate=$hosting->nextinvoicedate; $Cdate=date("Y/m/d"); $CurDate=date_create(date("Y/m/d")); $ndiff=date_diff($CurDate,$auditdate); $datediff=$ndiff->format("%R%a"); $myuserid=$hosting->userid; $userdomain=$hosting->domain; ##Condition Checking. if ( $datediff == "+0" ) { ##Open Ticket $adminUsername = 'emily'; // Optional for WHMCS 7.2 and later $command = 'OpenTicket'; $postData = array( 'deptid' => '6', 'subject' => 'Monthly Server Audit : ' . PHP_EOL $userdomain, 'message' => 'Dear Sir,' . PHP_EOL . PHP_EOL . ' Audit the server and attach the result. ' ' . PHP_EOL . PHP_EOL . ucfirst($adminUsername) . PHP_EOL . 'System Admin', 'clientid' => $myuserid 'priority' => 'Medium', 'markdown' => true); $results = localAPI($command, $postData, $adminUsername); return $results; } ##hook function close }); ticket not opening. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 13, 2023 Share Posted May 13, 2023 Going through the code quick, I spotted this: 'subject' => 'Monthly Server Audit : ' . PHP_EOL $userdomain, You need to do 'subject' => 'Monthly Server Audit : ' . PHP_EOL . $userdomain, 0 Quote Link to comment Share on other sites More sharing options...
hmaddy Posted May 13, 2023 Author Share Posted May 13, 2023 ok thanks. i need to run it on daily cron. But how can i test it whether its working or not or need to wait for next day cron 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 13, 2023 Share Posted May 13, 2023 Change "DailyCronJob" to "AfterCron" and run the cron job manually to see if it executes. 0 Quote Link to comment Share on other sites More sharing options...
hmaddy Posted May 13, 2023 Author Share Posted May 13, 2023 changed to add_hook('AfterCronJob', 1, function($vars) { the code is not working. 95% code is working. that ticket is not creates. thats the issue. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted May 13, 2023 Share Posted May 13, 2023 Do print_r($results) to show the result from creating the ticket. You need to watch the cron output. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 13, 2023 Share Posted May 13, 2023 (edited) Don't waste time testing cron-based hook points using the actual cron. Save time as follows: While I'm still coding, I comment AfterCronJob trigger and temporarily replace it with AdminAreaPage. At the very top of the function I put a condition that immediatlly exits the script if the query string is missing simulate_cron parameter. In other words I can browse WHMCS backend as normal. Whenever I want to simulate the cron I simply need to add ?simulate_cron=blabla in the query string on any page I am (eg. yourwhmcs.example.com/clients.php?simulate_cron=1). This way you can test your script without waiting for real cron job. Moreover if there's an error you can see that on screen. When you're ready to go live simply remove AdminAreaPage trigger and remove comment for AfterCronJob. Alternatively you can use an approach like this one basically a button you can press to run the cron job on click. Edited May 13, 2023 by Kian 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.