72 lines
2.7 KiB
PHP
72 lines
2.7 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'];
|
|
|
|
// When the invoice is saved, update the invoice items with the
|
|
// selected account.
|
|
if (isset($_POST['accountSelector'])) {
|
|
foreach ($_POST['accountSelector'] as $invoiceItemId => $account) {
|
|
Capsule::table('mod_accounting')
|
|
->where('invoice', $invoiceId)
|
|
->where('item', $invoiceItemId)
|
|
->update(['account' => $account]);
|
|
}
|
|
}
|
|
|
|
// Get the invoice items from the database.
|
|
$invoiceItems = Capsule::table('mod_accounting')
|
|
->where('invoice', $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>';
|
|
|
|
// Select the account that was previously selected for the invoice
|
|
// item.
|
|
echo '<script type="text/javascript">
|
|
$(document).ready(function() {
|
|
$("select[name=\'accountSelector[' . $invoiceItem->id . ']\']").val("' . $invoiceItem->account . '");
|
|
});
|
|
</script>';
|
|
}
|
|
} catch (Exception $e) {
|
|
// Log the error
|
|
logActivity('Accounting Error: ' . $e->getMessage());
|
|
}
|
|
});
|