whmcs-accounting/modules/addons/accounting/hooks.php
2023-03-22 07:22:00 +00:00

57 lines
2.1 KiB
PHP

<?php
/**
* WHMCS SDK Sample Addon Module Hooks File
*
* Hooks allow you to tie into events that occur within the WHMCS application.
*
* This allows you to execute your own code in addition to, or sometimes even
* instead of that which WHMCS executes by default.
*
* @see https://developers.whmcs.com/hooks/
*
* @copyright Copyright (c) WHMCS Limited 2017
* @license http://www.whmcs.com/license/ WHMCS Eula
*/
// A hook displaying a drop-down field for selection of an account to the left of each invoice item in the invoice edit page.
use WHMCS\Database\Capsule;
add_hook('InvoiceEdit', 1, function(array $params) {
try {
// Get the invoice ID from the parameters.
$invoiceId = $params['invoiceid'];
// Get the invoice items from the database.
$invoiceItems = Capsule::table('tblinvoiceitems')
->where('invoiceid', $invoiceId)
->get();
// Get the available accounts from the module configuration.
$availableAccounts = explode("\n", $params['Available Accounts']);
// Create the drop-down field for each invoice item.
foreach ($invoiceItems as $invoiceItem) {
$accountSelector = '<select name="accountSelector[' . $invoiceItem->id . ']">';
foreach ($availableAccounts as $availableAccount) {
$accountSelector .= '<option value="' . $availableAccount . '">' . $availableAccount . '</option>';
}
$accountSelector .= '</select>';
// Add the drop-down field to the invoice item.
echo '<script type="text/javascript">
$(document).ready(function() {
$("input[name=\'description[' . $invoiceItem->id . ']\']").before("' . $accountSelector . '");
});
</script>';
}
} catch (Exception $e) {
// Consider logging or reporting the error.
}
});
add_hook('ClientEdit', 1, function(array $params) {
try {
// Call the service's function, using the values provided by WHMCS in
// `$params`.
} catch (Exception $e) {
// Consider logging or reporting the error.
}
});