Assets from purchase invoice lines, straight-line depreciation/amortization with salvage value, disposals, and [Asset Mgmt] sources in Batch GL Entries. Green third workspace with an app access guard.
71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_app_access('asset');
|
|
require_once '../../../assets/utils/classes/UsageGuard.php';
|
|
require_once '../../../assets/utils/classes_ac/PostingWindowGuard.php';
|
|
require_once '../../../assets/utils/classes_ac/GlManager.php';
|
|
require_once '../../../assets/utils/classes_as/AssetManager.php';
|
|
|
|
$action = (string)($data['action'] ?? '');
|
|
$id = (int)($data['id'] ?? 0);
|
|
|
|
$messages = [
|
|
'create' => 'Asset saved as a draft.',
|
|
'update' => 'Asset saved.',
|
|
'activate' => 'Asset activated. Accounting posts its capitalization from Batch GL Entries.',
|
|
'delete' => 'Draft asset deleted.',
|
|
'void' => 'Asset voided.',
|
|
'dispose' => 'Disposal recorded. Accounting posts it from Batch GL Entries.',
|
|
'undo_disposal' => 'Disposal undone.',
|
|
];
|
|
|
|
if (!isset($messages[$action])) {
|
|
$answer['message'] = 'Invalid action.';
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
// Staff may prepare assets; everything that changes the books is owner/admin.
|
|
require_role($user_role, in_array($action, ['create', 'update'], true) ? ['owner', 'admin', 'staff'] : ['owner', 'admin']);
|
|
|
|
if ($action !== 'create' && $id <= 0) {
|
|
$answer['message'] = 'Missing asset id.';
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
try {
|
|
$result_id = dbTransaction($pdo2, function ($pdo) use ($action, $id, $data, $company_id, $user_id, $pdo1) {
|
|
$assets = new AssetManager($pdo, $company_id);
|
|
$gl = new GlManager($pdo, $company_id, new PostingWindowGuard($pdo1, $company_id));
|
|
|
|
switch ($action) {
|
|
case 'create':
|
|
case 'update': return $assets->save($data, $user_id);
|
|
case 'activate': $assets->activate($id); break;
|
|
case 'delete': $assets->delete($id); break;
|
|
case 'void': $assets->void($id, $gl); break;
|
|
case 'dispose': $assets->dispose($id, $data); break;
|
|
case 'undo_disposal': $assets->undoDisposal($id, $gl); break;
|
|
}
|
|
return $id;
|
|
});
|
|
|
|
if ($action === 'create') {
|
|
(new UsageGuard($pdo1, $company_id, $packages))->increment();
|
|
}
|
|
|
|
$answer['success'] = 1;
|
|
$answer['output'] = ['id' => $result_id];
|
|
$answer['message'] = $messages[$action];
|
|
} catch (PDOException $e) {
|
|
$answer['message'] = 'Database error, please try again.';
|
|
http_response_code(500);
|
|
} catch (Exception $e) {
|
|
$answer['message'] = $e->getMessage();
|
|
http_response_code(400);
|
|
}
|
|
|
|
exit(json_encode($answer));
|