Files
accounting_dev_v2/application/migrations/20260903000700_professional_purchase_workflow.php
2026-09-11 16:03:00 +07:00

25 lines
4.1 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Professional_purchase_workflow extends CI_Migration
{
public function up(){
$columns=array(
'procurement_method'=>"ENUM('online','direct','supplier') NOT NULL DEFAULT 'supplier' AFTER expected_date",
'payment_timing'=>"ENUM('prepaid','on_receipt','installment','credit') NOT NULL DEFAULT 'credit' AFTER procurement_method",
'marketplace'=>"VARCHAR(80) NULL AFTER payment_timing",'external_order_no'=>"VARCHAR(120) NULL AFTER marketplace",'courier'=>"VARCHAR(80) NULL AFTER external_order_no",'tracking_no'=>"VARCHAR(120) NULL AFTER courier",
'shipping_cost'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER total",'insurance_cost'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER shipping_cost",'service_fee'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER insurance_cost",'discount_amount'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER service_fee",'cashback_amount'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER discount_amount",
'payment_status'=>"ENUM('unpaid','partial','paid','refunded') NOT NULL DEFAULT 'unpaid' AFTER cashback_amount",'paid_amount'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER payment_status",'transit_cleared_amount'=>"DECIMAL(18,2) NOT NULL DEFAULT 0 AFTER paid_amount"
);foreach($columns as$n=>$definition)if(!$this->db->field_exists($n,'purchase_orders'))$this->db->query("ALTER TABLE purchase_orders ADD `$n` $definition");
if(!$this->db->field_exists('purchase_order_id','supplier_payments'))$this->db->query('ALTER TABLE supplier_payments ADD purchase_order_id BIGINT UNSIGNED NULL AFTER supplier_id');
if(!$this->db->field_exists('payment_kind','supplier_payments'))$this->db->query("ALTER TABLE supplier_payments ADD payment_kind ENUM('invoice','prepayment','refund') NOT NULL DEFAULT 'invoice' AFTER purchase_order_id");
if(!$this->db->field_exists('advance_journal_id','goods_receipt_lines'))$this->db->query('ALTER TABLE goods_receipt_lines ADD advance_journal_id INT NULL AFTER stock_log_id');
$this->db->query("CREATE TABLE IF NOT EXISTS purchase_payment_sources(id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,supplier_payment_id BIGINT UNSIGNED NOT NULL,account_id INT NOT NULL,amount DECIMAL(18,2) NOT NULL,created_at DATETIME NOT NULL,KEY idx_purchase_payment_source(supplier_payment_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$this->db->query("CREATE TABLE IF NOT EXISTS purchase_installment_schedules(id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,purchase_order_id BIGINT UNSIGNED NOT NULL,installment_no INT NOT NULL,due_date DATE NOT NULL,amount DECIMAL(18,2) NOT NULL,paid_amount DECIMAL(18,2) NOT NULL DEFAULT 0,status ENUM('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid',created_at DATETIME NOT NULL,UNIQUE KEY uq_po_installment(purchase_order_id,installment_no),KEY idx_installment_due(status,due_date)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$this->db->query("CREATE TABLE IF NOT EXISTS purchase_refunds(id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,refund_no VARCHAR(60) NOT NULL,purchase_order_id BIGINT UNSIGNED NOT NULL,supplier_payment_id BIGINT UNSIGNED NULL,refund_date DATE NOT NULL,account_id INT NOT NULL,amount DECIMAL(18,2) NOT NULL,reason TEXT NOT NULL,status ENUM('posted','reversed') NOT NULL DEFAULT 'posted',journal_id INT NOT NULL,created_by INT NULL,created_at DATETIME NOT NULL,UNIQUE KEY uq_purchase_refund_no(refund_no)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$account=$this->db->get_where('accounts',array('kode_akun'=>'1193'))->row();if(!$account){$this->db->insert('accounts',array('kode_akun'=>'1193','nama_akun'=>'Barang Dalam Perjalanan','tipe'=>'asset','posisi'=>'debit','kategori'=>'neraca','is_active'=>1,'is_header'=>0,'allow_posting'=>1));$accountId=$this->db->insert_id();}else$accountId=$account->id;
if(!$this->db->where('mapping_key','goods_in_transit')->count_all_results('system_account_mappings'))$this->db->insert('system_account_mappings',array('mapping_key'=>'goods_in_transit','account_id'=>$accountId,'description'=>'Pembayaran barang sebelum diterima gudang','is_active'=>1,'created_at'=>date('Y-m-d H:i:s')));
}
public function down(){throw new RuntimeException('Gunakan backup untuk rollback workflow pembelian profesional.');}
}