115 lines
5.0 KiB
PHP
115 lines
5.0 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Budgetcheck extends CI_Controller
|
|
{
|
|
private function boot()
|
|
{
|
|
if (!$this->input->is_cli_request()) show_404();
|
|
$this->load->database();
|
|
$access = $this->db->order_by('is_default', 'DESC')->get('user_companies')->row();
|
|
if (!$access) throw new RuntimeException('Tidak ada user-company untuk smoke test.');
|
|
$this->session->set_userdata(array(
|
|
'user_id' => $access->user_id,
|
|
'company_id' => $access->company_id,
|
|
'role' => 'Admin',
|
|
'logged_in' => true,
|
|
));
|
|
$this->load->library(array('CompanyContext', 'BudgetService'));
|
|
return $access;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->boot();
|
|
$required = array(
|
|
'budgets' => array('company_id', 'fiscal_year', 'status', 'control_mode'),
|
|
'budget_lines' => array('company_id', 'budget_id', 'period', 'account_id', 'amount'),
|
|
'purchase_order_lines' => array('budget_account_id'),
|
|
);
|
|
$missing = array();
|
|
foreach ($required as $table => $columns) {
|
|
foreach ($columns as $column) {
|
|
if (!$this->db->field_exists($column, $table)) $missing[] = $table.'.'.$column;
|
|
}
|
|
}
|
|
$duplicateActive = (int) $this->db->query(
|
|
"SELECT COUNT(*) total FROM (SELECT company_id, fiscal_year FROM budgets WHERE status='active' GROUP BY company_id, fiscal_year HAVING COUNT(*) > 1) x"
|
|
)->row()->total;
|
|
$duplicateLines = (int) $this->db->query(
|
|
"SELECT COUNT(*) total FROM (SELECT budget_id, period, account_id FROM budget_lines GROUP BY budget_id, period, account_id HAVING COUNT(*) > 1) x"
|
|
)->row()->total;
|
|
$rows = $this->budgetservice->report(array(
|
|
'from' => date('Y-01-01'),
|
|
'to' => date('Y-12-31'),
|
|
'account_id' => 0,
|
|
));
|
|
$ok = !$missing && $duplicateActive === 0 && $duplicateLines === 0;
|
|
echo json_encode(array(
|
|
'migration' => (int) $this->db->get('migrations')->row()->version,
|
|
'missing_schema' => $missing,
|
|
'money_20_000_50' => $this->budgetservice->money('20.000,50'),
|
|
'active_budget_duplicates' => $duplicateActive,
|
|
'budget_line_duplicates' => $duplicateLines,
|
|
'report_rows' => count($rows),
|
|
'status' => $ok ? 'OK' : 'REVIEW',
|
|
), JSON_PRETTY_PRINT).PHP_EOL;
|
|
}
|
|
|
|
public function workflow()
|
|
{
|
|
$access = $this->boot();
|
|
$user = (int) $access->user_id;
|
|
$budgetIds = array();
|
|
$doubleSubmitBlocked = false;
|
|
try {
|
|
$account = $this->db
|
|
->where(array(
|
|
'company_id' => $access->company_id,
|
|
'tipe' => 'expense',
|
|
'is_active' => 1,
|
|
'allow_posting' => 1,
|
|
))
|
|
->order_by('id')->get('accounts')->row();
|
|
if (!$account) throw new RuntimeException('Akun pengeluaran posting tidak tersedia.');
|
|
|
|
$id = $this->budgetservice->create(array(
|
|
'name' => 'QA Budget Temporary',
|
|
'fiscal_year' => 2199,
|
|
'control_mode' => 'warning',
|
|
), $user);
|
|
$budgetIds[] = $id;
|
|
$this->budgetservice->saveAnnualLines($id, array(array(
|
|
'account_id' => $account->id,
|
|
'month' => array(1 => '20.000,50', 2 => '10.000,25'),
|
|
)), $user);
|
|
$this->budgetservice->workflow($id, 'submit', $user);
|
|
try {
|
|
$this->budgetservice->workflow($id, 'submit', $user);
|
|
} catch (Throwable $expected) {
|
|
$doubleSubmitBlocked = true;
|
|
}
|
|
$status = $this->budgetservice->workflow($id, 'approve', $user);
|
|
$revision = $this->budgetservice->revise($id, 'Smoke test revisi', $user);
|
|
$budgetIds[] = $revision;
|
|
$revisionDraft = $this->budgetservice->find($revision)->status === 'draft';
|
|
$ok = $status === 'active' && $doubleSubmitBlocked && $revisionDraft;
|
|
echo json_encode(array(
|
|
'draft_submit_approve' => $status === 'active',
|
|
'double_submit_blocked' => $doubleSubmitBlocked,
|
|
'revision_draft' => $revisionDraft,
|
|
'account_only_budget' => true,
|
|
'status' => $ok ? 'OK' : 'REVIEW',
|
|
), JSON_PRETTY_PRINT).PHP_EOL;
|
|
} finally {
|
|
if ($budgetIds) {
|
|
$this->db->where_in('budget_id', $budgetIds)->delete('budget_lines');
|
|
$this->db->group_start()->where_in('budget_id', $budgetIds)->or_where_in('entity_id', $budgetIds)->group_end()
|
|
->where('entity_type', 'budget')->delete('budget_audit_logs');
|
|
$this->db->where_in('budget_id', $budgetIds)->delete('budget_revisions');
|
|
$this->db->where_in('id', $budgetIds)->delete('budgets');
|
|
}
|
|
}
|
|
}
|
|
}
|