Files
Thanakorn SandClaude Sonnet 4.6 b07882e3f4 code audit fixes: require_once, issue flow, role guards
- Upgraded all plain `require` to `require_once` across 172 api/engine
  and api/engine_report files to prevent class-redeclaration errors
- Added issue button, issue_invoice() with GL toastr, and delete_invoice()
  to expense/manage_purchase_invoice.php, bringing it in line with
  po/manage_purchase_invoice.php
- Added can_delete role guard (admin/owner only) to trash icons on
  revenue/invoice.php and expense/purchase_invoice.php, matching the
  existing pattern in finance/receipt.php and finance/payment.php

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 17:06:08 +07:00

57 lines
1.6 KiB
PHP

<?php
/**
* switch_branch.php — Switch the active company for the current session.
*
* action: 'read' → return list of companies the user belongs to
* action: 'update' → switch to the requested company_id
*/
session_start();
require_once '../../../assets/utils/db_auth.php';
require_once '../../../assets/utils/classes/UserManager.php';
$action = $data['action'] ?? '';
$um = new UserManager($pdo1, $company_id, $user_id);
if ($action === 'read') {
$answer['success'] = 1;
$answer['output'] = $um->getCompanyList();
$answer['current'] = (int)$_SESSION['login_company_id'];
exit(json_encode($answer));
}
if ($action === 'update') {
$target_company_id = (int)($data['company_id'] ?? 0);
if (!$target_company_id) {
$answer['message'] = 'Invalid company.';
http_response_code(400);
exit(json_encode($answer));
}
$target_map = $um->getCompanyAccess($target_company_id);
if (!$target_map) {
$answer['message'] = 'You do not have access to this company.';
http_response_code(403);
exit(json_encode($answer));
}
$_SESSION['login_company_id'] = $target_company_id;
$_SESSION['login_role'] = $target_map['role'] ?? 'viewer';
if (($_SESSION['login_license'] ?? 'user') === 'owner') {
$_SESSION['login_app_access'] = $target_map['user_app_access'] ?? 'wms';
} else {
$_SESSION['login_app_access'] = $target_map['app_access'] ?? 'wms';
}
$answer['success'] = 1;
$answer['message'] = 'Switched successfully.';
exit(json_encode($answer));
}
$answer['message'] = 'Invalid action.';
http_response_code(400);
exit(json_encode($answer));
?>