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

153 lines
4.5 KiB
PHP

<?php
/**
* WHMCS Account Selector Addon Module
*
* An addon module for WHMCS that allows you to select the account to be used
* for a given invoice item in accounting.
*
* @copyright Copyright (c) WHMCS Limited 2017, 2023 Kumi Systems e.U.
* @license MIT License
*/
use WHMCS\Database\Capsule;
use WHMCS\Module\Addon\AddonModule\Admin\AdminDispatcher;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
/**
* Define addon module configuration parameters.
*
* Includes a number of required system fields including name, description,
* author, language and version.
*
* Also allows you to define any configuration parameters that should be
* presented to the user when activating and configuring the module. These
* values are then made available in all module function calls.
*
* Examples of each and their possible configuration parameters are provided in
* the fields parameter below.
*
* @return array
*/
function accounting_config()
{
return [
// Display name for your module
'name' => 'Account Selector',
// Description displayed within the admin interface
'description' => 'This module allows you to select the account to be used for a given invoice item in accounting.',
// Module author name
'author' => 'Kumi Systems e.U.',
// Default language
'language' => 'english',
// Version number
'version' => '1.0',
'fields' => [
'Available Accounts' => [
'FriendlyName' => 'Available Accounts',
'Type' => 'textarea',
'Rows' => '20',
'Cols' => '100',
'Default' => '1234 - Example Account',
'Description' => 'List of available accounts, one per line. Format: Account Number - Account Name',
],
]
];
}
/**
* Called upon activation of the module for the first time.
* Used to create database tables required by the module.
*
* @see https://developers.whmcs.com/advanced/db-interaction/
*
* @return array Optional success/failure message
*/
function accounting_activate()
{
try {
Capsule::schema()
->create(
'mod_accounting',
function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->integer('invoice');
$table->integer('item');
$table->text('account');
}
);
return [
'status' => 'success',
'description' => 'Successfully created mod_accounting table. Now, please set up your accounts in the module configuration.',
];
} catch (\Exception $e) {
return [
'status' => "error",
'description' => 'Unable to create mod_accounting: ' . $e->getMessage(),
];
}
}
/**
* Deactivate.
*
* Called upon deactivation of the module.
* Used to drop database tables created by the module.
*
* @return array Optional success/failure message
*/
function accounting_deactivate()
{
try {
Capsule::schema()
->dropIfExists('mod_accounting');
return [
'status' => 'success',
'description' => 'Dropped mod_accounting table.',
];
} catch (\Exception $e) {
return [
// Supported values here include: success, error or info
"status" => "error",
"description" => "Unable to drop mod_accounting: {$e->getMessage()}",
];
}
}
/**
* Admin Area Output.
*
* Called when the addon module is accessed via the admin area.
* Should return HTML output for display to the admin user.
*
* This function is optional.
*
* @see AddonModule\Admin\Controller::index()
*
* @return string
*/
function accounting_output($vars)
{
// Get common module parameters
$modulelink = $vars['modulelink'];
$version = $vars['version'];
$_lang = $vars['_lang']; // an array of the currently loaded language variables
// Get module configuration parameters
$configTextareaField = $vars['Available Accounts'];
// Dispatch and handle request here. What follows is a demonstration of one
// possible way of handling this using a very basic dispatcher implementation.
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
$dispatcher = new AdminDispatcher();
$response = $dispatcher->dispatch($action, $vars);
echo $response;
}