148 lines
5.1 KiB
PHP
148 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\IntegrationHub\Http\Controllers;
|
|
|
|
use App\Http\MenuIcons;
|
|
use App\Utils\ModuleUtil;
|
|
use Illuminate\Routing\Controller;
|
|
use Menu;
|
|
use Modules\IntegrationHub\Services\ExecutiveCockpitExportService;
|
|
use Modules\IntegrationHub\Services\WebhookDispatcherService;
|
|
|
|
class DataController extends Controller
|
|
{
|
|
public function superadmin_package()
|
|
{
|
|
return [
|
|
[
|
|
'name' => 'integrationhub_module',
|
|
'label' => __('integrationhub::lang.module_name'),
|
|
'default' => false,
|
|
],
|
|
];
|
|
}
|
|
|
|
public function user_permissions()
|
|
{
|
|
return [
|
|
['value' => 'integrationhub.access', 'label' => __('integrationhub::lang.access'), 'default' => false],
|
|
['value' => 'integrationhub.manage_webhooks', 'label' => __('integrationhub::lang.manage_webhooks'), 'default' => false],
|
|
['value' => 'integrationhub.manage_tokens', 'label' => __('integrationhub::lang.manage_tokens'), 'default' => false],
|
|
['value' => 'integrationhub.export', 'label' => __('integrationhub::lang.export_data'), 'default' => false],
|
|
];
|
|
}
|
|
|
|
public function modifyAdminMenu()
|
|
{
|
|
if (! \Module::has('IntegrationHub') || ! \Module::isEnabled('IntegrationHub')) {
|
|
return;
|
|
}
|
|
|
|
$module_util = new ModuleUtil();
|
|
$business_id = session()->get('user.business_id');
|
|
|
|
if (! $module_util->isModuleInstalled('IntegrationHub')
|
|
|| ! $module_util->hasThePermissionInSubscription($business_id, 'integrationhub_module')) {
|
|
return;
|
|
}
|
|
|
|
if (! auth()->user()->can('integrationhub.access')) {
|
|
return;
|
|
}
|
|
|
|
Menu::modify('admin-sidebar-menu', function ($menu) {
|
|
$menu->dropdown(
|
|
__('integrationhub::lang.module_name'),
|
|
function ($sub) {
|
|
$user = auth()->user();
|
|
|
|
$sub->url(
|
|
action([DashboardController::class, 'index']),
|
|
__('integrationhub::lang.dashboard'),
|
|
['active' => request()->segment(2) == '' || request()->segment(2) == 'dashboard']
|
|
);
|
|
|
|
if ($user->can('integrationhub.manage_webhooks')) {
|
|
$sub->url(
|
|
action([WebhookController::class, 'index']),
|
|
__('integrationhub::lang.webhooks'),
|
|
['active' => request()->segment(2) == 'webhooks']
|
|
);
|
|
}
|
|
|
|
if ($user->can('integrationhub.manage_tokens')) {
|
|
$sub->url(
|
|
action([ODataController::class, 'indexTokens']),
|
|
__('integrationhub::lang.odata_tokens'),
|
|
['active' => request()->segment(2) == 'odata-tokens']
|
|
);
|
|
}
|
|
|
|
if ($user->can('integrationhub.export')) {
|
|
$sub->url(
|
|
action([ExportController::class, 'index']),
|
|
__('integrationhub::lang.exports'),
|
|
['active' => request()->segment(2) == 'exports']
|
|
);
|
|
}
|
|
},
|
|
[
|
|
'icon' => MenuIcons::svg('plug'),
|
|
'active' => request()->segment(1) == 'integrationhub',
|
|
'data-module' => 'IntegrationHub',
|
|
'data-menu-group' => 'integrations',
|
|
]
|
|
)->order(88);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook for Executive Cockpit export from ManagementTools or other modules.
|
|
*/
|
|
public function onExecutiveCockpitExport($data)
|
|
{
|
|
if (! \Module::has('IntegrationHub') || ! \Module::isEnabled('IntegrationHub')) {
|
|
return null;
|
|
}
|
|
|
|
$businessId = (int) ($data['business_id'] ?? session()->get('user.business_id'));
|
|
$format = $data['format'] ?? 'json';
|
|
|
|
return app(ExecutiveCockpitExportService::class)->export($businessId, $format);
|
|
}
|
|
|
|
/**
|
|
* Dispatch webhook when a sale is finalized (called via ModuleUtil hook).
|
|
*/
|
|
public function after_sales($data)
|
|
{
|
|
if (! \Module::has('IntegrationHub') || ! \Module::isEnabled('IntegrationHub')) {
|
|
return;
|
|
}
|
|
|
|
$transaction = $data['transaction'] ?? null;
|
|
if (! $transaction || ($transaction->status ?? '') !== 'final') {
|
|
return;
|
|
}
|
|
|
|
app(WebhookDispatcherService::class)->dispatch(
|
|
(int) $transaction->business_id,
|
|
'sale.finalized',
|
|
[
|
|
'transaction_id' => $transaction->id,
|
|
'invoice_no' => $transaction->invoice_no ?? null,
|
|
'final_total' => (float) ($transaction->final_total ?? 0),
|
|
'transaction_date' => $transaction->transaction_date ?? null,
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @alias after_sales
|
|
*/
|
|
public function after_sale_finalized($data)
|
|
{
|
|
return $this->after_sales($data);
|
|
}
|
|
}
|