Perubahan barcode dan inventory

This commit is contained in:
Wian Drs
2026-07-12 22:10:02 +07:00
parent 3ac6316da4
commit 3113d77242
21 changed files with 2989 additions and 1138 deletions
+320 -4
View File
@@ -9,9 +9,9 @@ class Cronjob extends CI_Controller {
$this->load->database();
// ONLY CLI ACCESS
if (!$this->input->is_cli_request()) {
exit('No direct access allowed');
}
// if (!$this->input->is_cli_request()) {
// exit('No direct access allowed');
// }
}
public function generate_insight()
@@ -353,4 +353,320 @@ class Cronjob extends CI_Controller {
}
}
}
// =====================================================
// CRONJOB AUTO PENYUSUTAN ASSET BULANAN
// =====================================================
public function depreciation_asset()
{
$period = date('Y-m');
$runDate = date('Y-m-d');
$akun_beban_penyusutan = 54;
$assets = $this->db
->select('id, nama_asset, nilai_perolehan, nilai_buku, nilai_residu, penyusutan_per_bulan, masa_manfaat, tanggal_perolehan, akumulasi_penyusutan, account_debit_id')
->from('assets')
->where('status', 'aktif')
->where('nilai_buku > nilai_residu', null, false)
->where('penyusutan_per_bulan >', 0)
->get()
->result();
$processed = 0;
$success = 0;
$skipped = 0;
$errors = 0;
$details = [];
$this->db->trans_begin();
try {
foreach ($assets as $a) {
$processed++;
$assetDetails = [
'asset_id' => (int)$a->id,
'asset_name' => $a->nama_asset,
'processed_months' => [],
'skipped_months' => []
];
$account_parent = $this->db
->select('id')
->where('parent_id', $a->account_debit_id)
->get('accounts')
->row();
if (!$account_parent || empty($account_parent->id)) {
$skipped++;
$assetDetails['status'] = 'skip';
$assetDetails['reason'] = 'account_parent_not_found';
$details[] = $assetDetails;
continue;
}
$akun_akumulasi_penyusutan = (int)$account_parent->id;
$mutasiPatokan = $this->db
->select('tanggal_dibuat')
->from('asset_mutations')
->where('asset_id', $a->id)
->where_in('tipe', ['perolehan', 'penambahan'])
->order_by('tanggal_dibuat', 'DESC')
->order_by('id', 'DESC')
->limit(1)
->get()
->row();
$tanggalPatokan = null;
if ($mutasiPatokan && !empty($mutasiPatokan->tanggal_dibuat)) {
$tanggalPatokan = $mutasiPatokan->tanggal_dibuat;
}
if (empty($tanggalPatokan)) {
$tanggalPatokan = $a->tanggal_perolehan;
}
if (empty($tanggalPatokan)) {
$skipped++;
$assetDetails['status'] = 'skip';
$assetDetails['reason'] = 'tanggal_patokan_tidak_ditemukan';
$details[] = $assetDetails;
continue;
}
$startMonth = date('Y-m', strtotime(date('Y-m-01', strtotime($tanggalPatokan)) . ' +1 month'));
$currentMonth = $period;
if ($startMonth > $currentMonth) {
$skipped++;
$assetDetails['status'] = 'skip';
$assetDetails['reason'] = 'start_next_month_after_acquisition';
$assetDetails['tanggal_patokan'] = $tanggalPatokan;
$details[] = $assetDetails;
continue;
}
$nilaiPerolehan = (float)$a->nilai_perolehan;
$nilaiResidu = (float)$a->nilai_residu;
$susutPerBulan = (float)$a->penyusutan_per_bulan;
$masaManfaat = (int)$a->masa_manfaat;
$akumulasiMutasi = $this->db
->select('COALESCE(SUM(nilai),0) total', false)
->from('asset_mutations')
->where('asset_id', $a->id)
->where('tipe', 'penyusutan')
->get()
->row();
$akumulasiBerjalan = (float)($akumulasiMutasi ? $akumulasiMutasi->total : 0);
$nilaiBukuBerjalan = $nilaiPerolehan - $akumulasiBerjalan;
if ($nilaiBukuBerjalan < $nilaiResidu) {
$nilaiBukuBerjalan = $nilaiResidu;
$akumulasiBerjalan = $nilaiPerolehan - $nilaiBukuBerjalan;
}
$iterMonth = $startMonth;
$assetHasSuccess = false;
while ($iterMonth <= $currentMonth) {
$monthsUsed = 0;
$start = new DateTime(date('Y-m-01', strtotime($tanggalPatokan)));
$end = new DateTime(date('Y-m-01', strtotime($iterMonth . '-01')));
$diff = $start->diff($end);
$monthsUsed = ($diff->y * 12) + $diff->m;
if ($monthsUsed < 0) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'tanggal_patokan_after_period'
];
$iterMonth = date('Y-m', strtotime($iterMonth . '-01 +1 month'));
continue;
}
if ($masaManfaat > 0 && $monthsUsed >= $masaManfaat) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'masa_manfaat_finished'
];
break;
}
$acqDay = (int)date('d', strtotime($tanggalPatokan));
$lastDayOfMonth = (int)date('t', strtotime($iterMonth . '-01'));
$dueDay = min($acqDay, $lastDayOfMonth);
$dueDateThisMonth = $iterMonth . '-' . str_pad($dueDay, 2, '0', STR_PAD_LEFT);
// Untuk bulan berjalan, tetap hormati due date.
// Untuk bulan yang sudah lewat, langsung boleh diproses (catch-up).
if ($iterMonth === $currentMonth && strtotime($runDate) < strtotime($dueDateThisMonth)) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'not_due_date_today',
'due_date' => $dueDateThisMonth,
'run_date' => $runDate
];
$iterMonth = date('Y-m', strtotime($iterMonth . '-01 +1 month'));
continue;
}
$startDate = $iterMonth . '-01';
$endDate = date('Y-m-d', strtotime($startDate . ' +1 month'));
$already = $this->db
->where('asset_id', $a->id)
->where('tipe', 'penyusutan')
->where('tanggal_dibuat >=', $startDate)
->where('tanggal_dibuat <', $endDate)
->count_all_results('asset_mutations');
if ($already > 0) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'already_depreciated_this_month'
];
$iterMonth = date('Y-m', strtotime($iterMonth . '-01 +1 month'));
continue;
}
if ($nilaiBukuBerjalan <= $nilaiResidu) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'nothing_to_depreciate'
];
break;
}
$maxSusut = max(0, $nilaiBukuBerjalan - $nilaiResidu);
$susut = min($susutPerBulan, $maxSusut);
if ($susut <= 0) {
$skipped++;
$assetDetails['skipped_months'][] = [
'period' => $iterMonth,
'reason' => 'nothing_to_depreciate'
];
break;
}
$akumulasiBefore = $akumulasiBerjalan;
$nilaiBukuBefore = $nilaiBukuBerjalan;
$akumulasiBerjalan += $susut;
$nilaiBukuBerjalan = $nilaiPerolehan - $akumulasiBerjalan;
if ($nilaiBukuBerjalan < $nilaiResidu) {
$nilaiBukuBerjalan = $nilaiResidu;
$akumulasiBerjalan = $nilaiPerolehan - $nilaiBukuBerjalan;
}
$this->db->insert('asset_mutations', [
'asset_id' => $a->id,
'tipe' => 'penyusutan',
'nilai' => $susut,
'keterangan' => 'Penyusutan otomatis periode ' . $iterMonth . ' (cronjob)',
'tanggal_dibuat' => $dueDateThisMonth
]);
$no_ref = 'DEP-' . date('YmdHis') . '-' . $a->id . '-' . str_replace('-', '', $iterMonth);
$this->db->insert('journals', [
'tanggal' => $dueDateThisMonth,
'no_ref' => $no_ref,
'keterangan' => 'Penyusutan Asset ' . $a->nama_asset . ' periode ' . $iterMonth,
'ref_type' => 'assets',
'ref_id' => $a->id,
'created_by' => 1
]);
$jid = $this->db->insert_id();
$this->db->insert('journal_details', [
'journal_id' => $jid,
'account_id' => $akun_beban_penyusutan,
'debit' => $susut,
'kredit' => 0
]);
$this->db->insert('journal_details', [
'journal_id' => $jid,
'account_id' => $akun_akumulasi_penyusutan,
'debit' => 0,
'kredit' => $susut
]);
$success++;
$assetHasSuccess = true;
$assetDetails['processed_months'][] = [
'period' => $iterMonth,
'depreciation' => (float)$susut,
'due_date' => $dueDateThisMonth,
'akumulasi_before' => (float)$akumulasiBefore,
'akumulasi_after' => (float)$akumulasiBerjalan,
'nilai_buku_before' => (float)$nilaiBukuBefore,
'nilai_buku_after' => (float)$nilaiBukuBerjalan,
'journal_no_ref' => $no_ref
];
$iterMonth = date('Y-m', strtotime($iterMonth . '-01 +1 month'));
}
if ($assetHasSuccess) {
$this->db->where('id', $a->id)->update('assets', [
'akumulasi_penyusutan' => $akumulasiBerjalan,
'nilai_buku' => $nilaiBukuBerjalan
]);
$assetDetails['status'] = 'success';
$assetDetails['akumulasi_final'] = (float)$akumulasiBerjalan;
$assetDetails['nilai_buku_final'] = (float)$nilaiBukuBerjalan;
} else {
$assetDetails['status'] = 'skip';
if (empty($assetDetails['reason'])) {
$assetDetails['reason'] = 'no_eligible_month_to_process';
}
}
$details[] = $assetDetails;
}
if ($this->db->trans_status() === FALSE) {
throw new Exception('Transaction failed');
}
$this->db->trans_commit();
echo json_encode([
'status' => true,
'period' => $period,
'processed' => $processed,
'success' => $success,
'skipped' => $skipped,
'errors' => $errors,
'message' => 'Depreciation cron executed',
'details' => $details
]);
} catch (Exception $e) {
$this->db->trans_rollback();
$errors++;
echo json_encode([
'status' => false,
'period' => $period,
'processed' => $processed,
'success' => $success,
'skipped' => $skipped,
'errors' => $errors,
'message' => $e->getMessage()
]);
}
}
}