Update besar apk finance
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
class Migration_Create_configurable_tax_engine extends CI_Migration
|
||||
{
|
||||
public function up(){
|
||||
$tables=array(
|
||||
'tax_codes'=>"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,code VARCHAR(30) NOT NULL,name VARCHAR(120) NOT NULL,tax_type ENUM('sales','purchase','withholding','payroll') NOT NULL,calculation_method ENUM('exclusive','inclusive') NOT NULL DEFAULT 'exclusive',rate DECIMAL(9,6) NOT NULL DEFAULT 0,formula_expression VARCHAR(500) NULL,input_account_id INT NULL,output_account_id INT NULL,withholding_payable_account_id INT NULL,withholding_receivable_account_id INT NULL,effective_from DATE NOT NULL,effective_to DATE NULL,is_active TINYINT(1) NOT NULL DEFAULT 1,UNIQUE KEY uq_tax_code_effective(code,effective_from)",
|
||||
'tax_periods'=>"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,period CHAR(7) NOT NULL,tax_type ENUM('sales','purchase','withholding','payroll','all') NOT NULL DEFAULT 'all',status ENUM('open','reviewed','filed','closed') NOT NULL DEFAULT 'open',filing_reference VARCHAR(100) NULL,filed_at DATETIME NULL,filed_by INT NULL,closed_at DATETIME NULL,closed_by INT NULL,notes TEXT NULL,UNIQUE KEY uq_tax_period(period,tax_type)",
|
||||
'tax_transactions'=>"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,tax_date DATE NOT NULL,tax_period_id BIGINT UNSIGNED NULL,tax_code_id BIGINT UNSIGNED NOT NULL,direction ENUM('input','output','withheld','withholding') NOT NULL,source_type VARCHAR(50) NOT NULL,source_id BIGINT UNSIGNED NOT NULL,source_line_id BIGINT UNSIGNED NULL,counterparty_type VARCHAR(30) NULL,counterparty_id BIGINT UNSIGNED NULL,tax_invoice_no VARCHAR(100) NULL,tax_base DECIMAL(18,2) NOT NULL,tax_amount DECIMAL(18,2) NOT NULL,status ENUM('draft','posted','reversed') NOT NULL DEFAULT 'posted',journal_id INT NULL,idempotency_key VARCHAR(140) NOT NULL,created_at DATETIME NOT NULL,UNIQUE KEY uq_tax_transaction_key(idempotency_key),KEY idx_tax_register(tax_date,direction,tax_code_id)",
|
||||
'tax_exports'=>"id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,tax_period_id BIGINT UNSIGNED NOT NULL,export_type VARCHAR(50) NOT NULL,file_name VARCHAR(255) NOT NULL,row_count INT NOT NULL,file_hash CHAR(64) NOT NULL,created_by INT NULL,created_at DATETIME NOT NULL"
|
||||
);foreach($tables as$t=>$s)$this->db->query("CREATE TABLE IF NOT EXISTS `$t`($s) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
$out=$this->db->where('mapping_key','sales_tax_payable')->get('system_account_mappings')->row();$in=$this->db->where('mapping_key','purchase_input_tax')->get('system_account_mappings')->row();$this->db->query("INSERT IGNORE INTO tax_codes(code,name,tax_type,calculation_method,rate,input_account_id,output_account_id,effective_from,is_active)VALUES('PPN','PPN','sales','exclusive',11,?,?,?,1)",array($in?$in->account_id:null,$out?$out->account_id:null,date('Y-m-01')));
|
||||
$this->db->query("INSERT IGNORE INTO tax_transactions(tax_date,tax_code_id,direction,source_type,source_id,source_line_id,counterparty_type,counterparty_id,tax_invoice_no,tax_base,tax_amount,status,journal_id,idempotency_key,created_at) SELECT i.tanggal,t.id,'output','invoice',i.id,d.id,'customer',i.customer_id,NULL,(d.subtotal-d.tax_amount),d.tax_amount,'posted',i.journal_id,CONCAT('LEGACY-INV-TAX-',d.id),COALESCE(i.posted_at,i.created_at,NOW()) FROM invoice_details d JOIN invoices i ON i.id=d.invoice_id JOIN tax_codes t ON t.code='PPN' WHERE d.tax_amount>0 AND i.workflow_status='posted'");
|
||||
$this->db->query("INSERT IGNORE INTO tax_transactions(tax_date,tax_code_id,direction,source_type,source_id,source_line_id,counterparty_type,counterparty_id,tax_invoice_no,tax_base,tax_amount,status,journal_id,idempotency_key,created_at) SELECT s.invoice_date,t.id,'input','supplier_invoice',s.id,l.id,'supplier',s.supplier_id,s.supplier_invoice_no,(l.line_total-l.tax_amount),l.tax_amount,'posted',s.journal_id,CONCAT('LEGACY-BILL-TAX-',l.id),COALESCE(s.posted_at,s.created_at,NOW()) FROM supplier_invoice_lines l JOIN supplier_invoices s ON s.id=l.supplier_invoice_id JOIN tax_codes t ON t.code='PPN' WHERE l.tax_amount>0 AND s.status IN('posted','partial','paid')");
|
||||
$this->db->query('DROP TRIGGER IF EXISTS trg_closed_tax_period');$this->db->query("CREATE TRIGGER trg_closed_tax_period BEFORE INSERT ON tax_transactions FOR EACH ROW BEGIN DECLARE n INT; SELECT COUNT(*) INTO n FROM tax_periods WHERE period=DATE_FORMAT(NEW.tax_date,'%Y-%m') AND tax_type IN('all',(SELECT tax_type FROM tax_codes WHERE id=NEW.tax_code_id)) AND status IN('filed','closed'); IF n>0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Tax period is filed or closed'; END IF; END");
|
||||
}
|
||||
public function down(){throw new RuntimeException('Gunakan backup untuk rollback Tahap 11.');}
|
||||
}
|
||||
Reference in New Issue
Block a user