set search_path = budget_app, public;

insert into companies (code, name, business_number)
values ('JH001', '장헌건설', '123-45-67890')
on conflict (code) do nothing;

insert into fiscal_years (company_id, fiscal_year, start_date, end_date, is_closed)
select c.id, 2026, date '2026-01-01', date '2026-12-31', false
from companies c
where c.code = 'JH001'
on conflict (company_id, fiscal_year) do nothing;

insert into departments (company_id, parent_department_id, code, name, manager_name)
select c.id, null, x.code, x.name, x.manager_name
from companies c
cross join (
    values
        ('HQ', '본사', '김대표'),
        ('SALES', '영업본부', '박영업'),
        ('EXEC', '실행본부', '이실행'),
        ('FIN', '재무팀', '최재무')
) as x(code, name, manager_name)
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into employees (company_id, department_id, employee_no, name, title, email)
select
    c.id,
    d.id,
    x.employee_no,
    x.name,
    x.title,
    x.email
from companies c
join departments d on d.company_id = c.id
join (
    values
        ('E001', '김대표', '대표', 'ceo@jh.local', 'HQ'),
        ('E002', '박영업', '영업이사', 'sales@jh.local', 'SALES'),
        ('E003', '이실행', '실행이사', 'exec@jh.local', 'EXEC'),
        ('E004', '최재무', '재무팀장', 'finance@jh.local', 'FIN'),
        ('E005', '정현장', '현장소장', 'site1@jh.local', 'EXEC')
) as x(employee_no, name, title, email, dept_code)
    on d.code = x.dept_code
where c.code = 'JH001'
on conflict (company_id, employee_no) do nothing;

insert into business_units (company_id, code, name)
select c.id, x.code, x.name
from companies c
cross join (
    values
        ('BU001', '건축사업부'),
        ('BU002', '토목사업부')
) as x(code, name)
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into clients (company_id, code, name, business_number, contact_name, phone, email)
select c.id, x.code, x.name, x.business_number, x.contact_name, x.phone, x.email
from companies c
cross join (
    values
        ('CL001', '한맥개발', '210-81-11111', '김발주', '02-1111-1111', 'client1@hmac.local'),
        ('CL002', '동해산업', '210-81-22222', '이발주', '02-2222-2222', 'client2@donghae.local')
) as x(code, name, business_number, contact_name, phone, email)
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into vendors (company_id, code, name, business_number, contact_name, phone, email)
select c.id, x.code, x.name, x.business_number, x.contact_name, x.phone, x.email
from companies c
cross join (
    values
        ('VD001', '성우외주', '301-86-11111', '오외주', '031-111-1111', 'vendor1@sw.local'),
        ('VD002', '대한자재', '301-86-22222', '문자재', '031-222-2222', 'vendor2@dh.local'),
        ('VD003', '정우장비', '301-86-33333', '최장비', '031-333-3333', 'vendor3@jw.local')
) as x(code, name, business_number, contact_name, phone, email)
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into account_categories (company_id, code, name, category_type, sort_order)
select c.id, x.code, x.name, x.category_type, x.sort_order
from companies c
cross join (
    values
        ('REV', '매출', 'revenue', 1),
        ('COST', '공사원가', 'cost', 2),
        ('EXP', '판관비', 'expense', 3)
) as x(code, name, category_type, sort_order)
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into accounts (company_id, category_id, code, name, account_type)
select
    c.id,
    ac.id,
    x.code,
    x.name,
    x.account_type
from companies c
join account_categories ac
    on ac.company_id = c.id
join (
    values
        ('REV001', '기성매출', 'revenue', 'REV'),
        ('REV002', '추가공사매출', 'revenue', 'REV'),
        ('COST001', '외주비', 'cost', 'COST'),
        ('COST002', '자재비', 'cost', 'COST'),
        ('COST003', '장비비', 'cost', 'COST'),
        ('EXP001', '현장관리비', 'expense', 'EXP'),
        ('EXP002', '본사관리비', 'expense', 'EXP')
) as x(code, name, account_type, category_code)
    on ac.code = x.category_code
where c.code = 'JH001'
on conflict (company_id, code) do nothing;

insert into projects (
    company_id,
    business_unit_id,
    department_id,
    client_id,
    project_code,
    project_name,
    project_type,
    status,
    contract_amount,
    start_date,
    end_date,
    manager_employee_id,
    description
)
select
    c.id,
    bu.id,
    d.id,
    cl.id,
    x.project_code,
    x.project_name,
    x.project_type,
    x.status,
    x.contract_amount,
    x.start_date,
    x.end_date,
    e.id,
    x.description
from companies c
join business_units bu on bu.company_id = c.id
join departments d on d.company_id = c.id
join clients cl on cl.company_id = c.id
join employees e on e.company_id = c.id
join (
    values
        (
            'PJT-2026-001',
            '장헌 오피스 신축공사',
            '건축',
            'active',
            1500000000.00,
            date '2026-01-10',
            date '2026-12-20',
            'BU001',
            'EXEC',
            'CL001',
            'E005',
            '오피스 신축 메인 프로젝트'
        ),
        (
            'PJT-2026-002',
            '동해 물류창고 증축공사',
            '건축',
            'active',
            950000000.00,
            date '2026-02-01',
            date '2026-10-30',
            'BU001',
            'EXEC',
            'CL002',
            'E003',
            '물류창고 증축 프로젝트'
        )
) as x(
    project_code,
    project_name,
    project_type,
    status,
    contract_amount,
    start_date,
    end_date,
    bu_code,
    dept_code,
    client_code,
    employee_no,
    description
)
    on bu.code = x.bu_code
   and d.code = x.dept_code
   and cl.code = x.client_code
   and e.employee_no = x.employee_no
where c.code = 'JH001'
on conflict (company_id, project_code) do nothing;

insert into budget_versions (
    company_id,
    fiscal_year_id,
    version_name,
    version_no,
    status,
    created_by,
    approved_by,
    approved_at,
    notes
)
select
    c.id,
    fy.id,
    '2026 본예산',
    1,
    'approved',
    e1.id,
    e2.id,
    now(),
    '초기 승인 예산'
from companies c
join fiscal_years fy on fy.company_id = c.id and fy.fiscal_year = 2026
join employees e1 on e1.company_id = c.id and e1.employee_no = 'E004'
join employees e2 on e2.company_id = c.id and e2.employee_no = 'E001'
where c.code = 'JH001'
on conflict (company_id, fiscal_year_id, version_no) do nothing;

insert into budget_items (
    company_id,
    budget_version_id,
    project_id,
    department_id,
    account_id,
    month_no,
    planned_amount,
    memo
)
select
    c.id,
    bv.id,
    p.id,
    d.id,
    a.id,
    x.month_no,
    x.planned_amount,
    x.memo
from companies c
join budget_versions bv on bv.company_id = c.id and bv.version_no = 1
join projects p on p.company_id = c.id
join departments d on d.id = p.department_id
join accounts a on a.company_id = c.id
join (
    values
        ('PJT-2026-001', 'REV001', 1, 120000000.00, '1월 기성매출'),
        ('PJT-2026-001', 'REV001', 2, 130000000.00, '2월 기성매출'),
        ('PJT-2026-001', 'REV001', 3, 150000000.00, '3월 기성매출'),
        ('PJT-2026-001', 'COST001', 1, 40000000.00, '1월 외주비'),
        ('PJT-2026-001', 'COST001', 2, 45000000.00, '2월 외주비'),
        ('PJT-2026-001', 'COST001', 3, 50000000.00, '3월 외주비'),
        ('PJT-2026-001', 'COST002', 1, 25000000.00, '1월 자재비'),
        ('PJT-2026-001', 'COST002', 2, 28000000.00, '2월 자재비'),
        ('PJT-2026-001', 'EXP001', 1, 8000000.00, '1월 현장관리비'),
        ('PJT-2026-001', 'EXP001', 2, 8000000.00, '2월 현장관리비'),
        ('PJT-2026-002', 'REV001', 2, 90000000.00, '2월 기성매출'),
        ('PJT-2026-002', 'REV001', 3, 110000000.00, '3월 기성매출'),
        ('PJT-2026-002', 'COST001', 2, 30000000.00, '2월 외주비'),
        ('PJT-2026-002', 'COST002', 2, 22000000.00, '2월 자재비'),
        ('PJT-2026-002', 'EXP001', 2, 6000000.00, '2월 현장관리비')
) as x(project_code, account_code, month_no, planned_amount, memo)
    on p.project_code = x.project_code
   and a.code = x.account_code
where c.code = 'JH001'
on conflict (budget_version_id, project_id, department_id, account_id, month_no) do nothing;

insert into purchase_requests (
    company_id,
    project_id,
    department_id,
    vendor_id,
    requester_employee_id,
    request_no,
    request_date,
    status,
    description,
    total_amount
)
select
    c.id,
    p.id,
    p.department_id,
    v.id,
    e.id,
    x.request_no,
    x.request_date,
    x.status,
    x.description,
    x.total_amount
from companies c
join projects p on p.company_id = c.id
join vendors v on v.company_id = c.id
join employees e on e.company_id = c.id
join (
    values
        ('PR-2026-0001', date '2026-01-12', 'approved', '1차 외주 발주 요청', 38000000.00, 'PJT-2026-001', 'VD001', 'E005'),
        ('PR-2026-0002', date '2026-02-08', 'ordered', '자재 구매 요청', 21000000.00, 'PJT-2026-002', 'VD002', 'E003')
) as x(request_no, request_date, status, description, total_amount, project_code, vendor_code, employee_no)
    on p.project_code = x.project_code
   and v.code = x.vendor_code
   and e.employee_no = x.employee_no
where c.code = 'JH001'
on conflict (company_id, request_no) do nothing;

insert into purchase_request_items (
    purchase_request_id,
    account_id,
    item_name,
    quantity,
    unit_price,
    amount,
    needed_date
)
select
    pr.id,
    a.id,
    x.item_name,
    x.quantity,
    x.unit_price,
    x.amount,
    x.needed_date
from purchase_requests pr
join companies c on c.id = pr.company_id
join accounts a on a.company_id = c.id
join (
    values
        ('PR-2026-0001', 'COST001', '철근 가공 외주', 1.0, 38000000.00, 38000000.00, date '2026-01-20'),
        ('PR-2026-0002', 'COST002', '철골 자재 구매', 1.0, 21000000.00, 21000000.00, date '2026-02-15')
) as x(request_no, account_code, item_name, quantity, unit_price, amount, needed_date)
    on pr.request_no = x.request_no
   and a.code = x.account_code
where c.code = 'JH001';

insert into purchase_orders (
    company_id,
    purchase_request_id,
    project_id,
    department_id,
    vendor_id,
    order_no,
    order_date,
    status,
    total_amount
)
select
    c.id,
    pr.id,
    p.id,
    p.department_id,
    v.id,
    x.order_no,
    x.order_date,
    x.status,
    x.total_amount
from companies c
join purchase_requests pr on pr.company_id = c.id
join projects p on p.id = pr.project_id
join vendors v on v.id = pr.vendor_id
join (
    values
        ('PO-2026-0001', date '2026-01-15', 'issued', 38000000.00, 'PR-2026-0001'),
        ('PO-2026-0002', date '2026-02-10', 'received', 21000000.00, 'PR-2026-0002')
) as x(order_no, order_date, status, total_amount, request_no)
    on pr.request_no = x.request_no
where c.code = 'JH001'
on conflict (company_id, order_no) do nothing;

insert into purchase_order_items (
    purchase_order_id,
    account_id,
    item_name,
    quantity,
    unit_price,
    amount
)
select
    po.id,
    a.id,
    x.item_name,
    x.quantity,
    x.unit_price,
    x.amount
from purchase_orders po
join companies c on c.id = po.company_id
join accounts a on a.company_id = c.id
join (
    values
        ('PO-2026-0001', 'COST001', '철근 가공 외주', 1.0, 38000000.00, 38000000.00),
        ('PO-2026-0002', 'COST002', '철골 자재 구매', 1.0, 21000000.00, 21000000.00)
) as x(order_no, account_code, item_name, quantity, unit_price, amount)
    on po.order_no = x.order_no
   and a.code = x.account_code
where c.code = 'JH001';

insert into invoices (
    company_id,
    project_id,
    department_id,
    vendor_id,
    client_id,
    invoice_no,
    invoice_type,
    issue_date,
    due_date,
    supply_amount,
    tax_amount,
    total_amount,
    status
)
select
    c.id,
    p.id,
    p.department_id,
    v.id,
    cl.id,
    x.invoice_no,
    x.invoice_type,
    x.issue_date,
    x.due_date,
    x.supply_amount,
    x.tax_amount,
    x.total_amount,
    x.status
from companies c
join projects p on p.company_id = c.id
left join vendors v on v.company_id = c.id
left join clients cl on cl.company_id = c.id
join (
    values
        ('INV-S-2026-0001', 'sales', date '2026-01-31', date '2026-02-28', 120000000.00, 12000000.00, 132000000.00, 'issued', 'PJT-2026-001', null, 'CL001'),
        ('INV-P-2026-0001', 'purchase', date '2026-01-25', date '2026-02-15', 38000000.00, 3800000.00, 41800000.00, 'paid', 'PJT-2026-001', 'VD001', null),
        ('INV-P-2026-0002', 'purchase', date '2026-02-18', date '2026-03-10', 21000000.00, 2100000.00, 23100000.00, 'paid', 'PJT-2026-002', 'VD002', null)
) as x(
    invoice_no,
    invoice_type,
    issue_date,
    due_date,
    supply_amount,
    tax_amount,
    total_amount,
    status,
    project_code,
    vendor_code,
    client_code
)
    on p.project_code = x.project_code
   and (v.code = x.vendor_code or x.vendor_code is null)
   and (cl.code = x.client_code or x.client_code is null)
where c.code = 'JH001'
on conflict (company_id, invoice_no) do nothing;

insert into actual_transactions (
    company_id,
    fiscal_year_id,
    project_id,
    department_id,
    account_id,
    vendor_id,
    client_id,
    employee_id,
    source_type,
    source_id,
    transaction_date,
    month_no,
    transaction_type,
    amount,
    description
)
select
    c.id,
    fy.id,
    p.id,
    p.department_id,
    a.id,
    v.id,
    cl.id,
    e.id,
    x.source_type,
    null,
    x.transaction_date,
    extract(month from x.transaction_date)::integer,
    x.transaction_type,
    x.amount,
    x.description
from companies c
join fiscal_years fy on fy.company_id = c.id and fy.fiscal_year = 2026
join projects p on p.company_id = c.id
join accounts a on a.company_id = c.id
left join vendors v on v.company_id = c.id
left join clients cl on cl.company_id = c.id
left join employees e on e.company_id = c.id
join (
    values
        ('PJT-2026-001', 'REV001', date '2026-01-31', 'revenue', 118000000.00, 'invoice', '1월 기성매출 실적', null, 'CL001', 'E002'),
        ('PJT-2026-001', 'COST001', date '2026-01-25', 'cost', 38000000.00, 'purchase_order', '1월 외주비 실적', 'VD001', null, 'E005'),
        ('PJT-2026-001', 'COST002', date '2026-01-27', 'cost', 24000000.00, 'manual', '1월 자재비 실적', 'VD002', null, 'E005'),
        ('PJT-2026-001', 'EXP001', date '2026-01-31', 'expense', 7500000.00, 'manual', '1월 현장관리비', null, null, 'E005'),
        ('PJT-2026-001', 'REV001', date '2026-02-28', 'revenue', 126000000.00, 'invoice', '2월 기성매출 실적', null, 'CL001', 'E002'),
        ('PJT-2026-001', 'COST001', date '2026-02-26', 'cost', 47000000.00, 'manual', '2월 외주비 실적', 'VD001', null, 'E005'),
        ('PJT-2026-001', 'EXP001', date '2026-02-28', 'expense', 8100000.00, 'manual', '2월 현장관리비', null, null, 'E005'),
        ('PJT-2026-002', 'REV001', date '2026-02-28', 'revenue', 92000000.00, 'invoice', '2월 기성매출 실적', null, 'CL002', 'E002'),
        ('PJT-2026-002', 'COST001', date '2026-02-18', 'cost', 29500000.00, 'purchase_order', '2월 외주비 실적', 'VD001', null, 'E003'),
        ('PJT-2026-002', 'COST002', date '2026-02-20', 'cost', 21000000.00, 'invoice', '2월 자재비 실적', 'VD002', null, 'E003'),
        ('PJT-2026-002', 'EXP001', date '2026-02-28', 'expense', 5900000.00, 'manual', '2월 현장관리비', null, null, 'E003')
) as x(
    project_code,
    account_code,
    transaction_date,
    transaction_type,
    amount,
    source_type,
    description,
    vendor_code,
    client_code,
    employee_no
)
    on p.project_code = x.project_code
   and a.code = x.account_code
   and (v.code = x.vendor_code or x.vendor_code is null)
   and (cl.code = x.client_code or x.client_code is null)
   and (e.employee_no = x.employee_no or x.employee_no is null)
where c.code = 'JH001';

insert into cashflow_transactions (
    company_id,
    project_id,
    department_id,
    transaction_date,
    cashflow_type,
    amount,
    description
)
select
    c.id,
    p.id,
    p.department_id,
    x.transaction_date,
    x.cashflow_type,
    x.amount,
    x.description
from companies c
join projects p on p.company_id = c.id
join (
    values
        ('PJT-2026-001', date '2026-02-05', 'inflow', 132000000.00, '1월 매출 입금'),
        ('PJT-2026-001', date '2026-02-15', 'outflow', 41800000.00, '외주비 지급'),
        ('PJT-2026-002', date '2026-03-12', 'outflow', 23100000.00, '자재비 지급')
) as x(project_code, transaction_date, cashflow_type, amount, description)
    on p.project_code = x.project_code
where c.code = 'JH001';

insert into file_import_logs (
    company_id,
    import_type,
    file_name,
    row_count,
    success_count,
    failure_count,
    imported_by,
    notes
)
select
    c.id,
    'budget',
    'budget_2026_sample.xlsx',
    15,
    15,
    0,
    e.id,
    '샘플 예산 데이터 적재'
from companies c
join employees e on e.company_id = c.id and e.employee_no = 'E004'
where c.code = 'JH001';
