ultimatepos/Modules/Tms/Http/Controllers/SettingsController.php

75 lines
3.4 KiB
PHP

<?php
namespace Modules\Tms\Http\Controllers;
use App\Business;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Tms\Http\Controllers\Concerns\AuthorizesTms;
use Modules\Tms\Utils\TmsUtil;
class SettingsController extends Controller
{
use AuthorizesTms;
public function __construct(protected TmsUtil $tmsUtil) {}
public function index()
{
$this->authorizeTmsAccess('tms.access');
$business_id = $this->tmsUtil->getBusinessId();
return view('tms::settings.index', [
'settings' => $this->tmsUtil->getTmsSettings($business_id),
'carriers' => $this->tmsUtil->getCarriersDropdown($business_id),
'accounting_enabled' => $this->tmsUtil->isAccountingEnabled($business_id),
'accounts' => $this->tmsUtil->getAccountingAccountsDropdown($business_id),
]);
}
public function update(Request $request)
{
$this->authorizeTmsAccess('tms.access');
$business_id = $this->tmsUtil->getBusinessId();
$existing = $this->tmsUtil->getTmsSettings($business_id);
$data = $request->validate([
'ref_prefix' => 'required|string|max:20',
'customs_ref_prefix' => 'nullable|string|max:20',
'default_carrier_id' => 'nullable|exists:tms_carriers,id',
'freight_expense_account_id' => 'nullable|integer',
'freight_payment_account_id' => 'nullable|integer',
'customs_duty_expense_account_id' => 'nullable|integer',
'customs_payment_account_id' => 'nullable|integer',
]);
$settings = array_merge($existing, [
'ref_prefix' => $data['ref_prefix'],
'customs_ref_prefix' => $data['customs_ref_prefix'] ?? ($existing['customs_ref_prefix'] ?? 'CUS'),
'default_carrier_id' => $data['default_carrier_id'] ?? null,
'auto_create_on_sale' => $request->boolean('auto_create_on_sale'),
'sync_transaction_shipping' => $request->boolean('sync_transaction_shipping'),
'auto_post_freight_journal' => $request->boolean('auto_post_freight_journal'),
'auto_post_customs_journal' => $request->boolean('auto_post_customs_journal'),
'freight_expense_account_id' => $data['freight_expense_account_id'] ?? null,
'freight_payment_account_id' => $data['freight_payment_account_id'] ?? null,
'customs_duty_expense_account_id' => $data['customs_duty_expense_account_id'] ?? null,
'customs_payment_account_id' => $data['customs_payment_account_id'] ?? null,
'sync_customs_shipment_status' => $request->boolean('sync_customs_shipment_status'),
'auto_link_customs_shipment' => $request->boolean('auto_link_customs_shipment'),
'auto_create_shipment_on_customs_import' => $request->boolean('auto_create_shipment_on_customs_import'),
'require_customs_cleared_before_transit' => $request->boolean('require_customs_cleared_before_transit'),
'exclude_uncleared_from_routes' => $request->boolean('exclude_uncleared_from_routes'),
'sync_mission_status' => $request->boolean('sync_mission_status'),
]);
Business::where('id', $business_id)->update([
'tms_settings' => json_encode($settings),
]);
return redirect()
->action([self::class, 'index'])
->with('status', __('tms::lang.settings_saved'));
}
}