<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <!-- =============================================================
         Fiscal Category – List View
         ============================================================= -->
    <record id="fusion_fiscal_category_view_list" model="ir.ui.view">
        <field name="name">fusion.fiscal.category.list</field>
        <field name="model">fusion.fiscal.category</field>
        <field name="arch" type="xml">
            <list string="Fiscal Categories" editable="bottom"
                  decoration-muted="not active">
                <field name="code"/>
                <field name="name"/>
                <field name="category_type"/>
                <field name="account_count" string="# Accounts"/>
                <field name="parent_id" optional="show"/>
                <field name="company_id" groups="base.group_multi_company"/>
                <field name="active" column_invisible="True"/>
            </list>
        </field>
    </record>

    <!-- =============================================================
         Fiscal Category – Form View
         ============================================================= -->
    <record id="fusion_fiscal_category_view_form" model="ir.ui.view">
        <field name="name">fusion.fiscal.category.form</field>
        <field name="model">fusion.fiscal.category</field>
        <field name="arch" type="xml">
            <form string="Fiscal Category">
                <sheet>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" placeholder="e.g. Operating Revenue"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Classification">
                            <field name="code"/>
                            <field name="category_type"/>
                            <field name="parent_id"/>
                            <field name="active"/>
                        </group>
                        <group string="Company">
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="account_count"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Accounts" name="accounts">
                            <field name="account_ids">
                                <list>
                                    <field name="code"/>
                                    <field name="name"/>
                                    <field name="account_type"/>
                                </list>
                            </field>
                        </page>
                        <page string="Sub-categories" name="children">
                            <field name="child_ids">
                                <list>
                                    <field name="code"/>
                                    <field name="name"/>
                                    <field name="category_type"/>
                                    <field name="account_count"/>
                                </list>
                            </field>
                        </page>
                        <page string="Notes" name="notes">
                            <field name="description"
                                   placeholder="Internal notes about this fiscal category..."/>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- =============================================================
         Fiscal Category – Search View
         ============================================================= -->
    <record id="fusion_fiscal_category_view_search" model="ir.ui.view">
        <field name="name">fusion.fiscal.category.search</field>
        <field name="model">fusion.fiscal.category</field>
        <field name="arch" type="xml">
            <search string="Search Fiscal Categories">
                <field name="name" string="Name / Code"
                       filter_domain="['|', ('name', 'ilike', self), ('code', 'ilike', self)]"/>
                <separator/>
                <filter name="filter_income" string="Income"
                        domain="[('category_type', '=', 'income')]"/>
                <filter name="filter_expense" string="Expense"
                        domain="[('category_type', '=', 'expense')]"/>
                <filter name="filter_asset" string="Asset"
                        domain="[('category_type', '=', 'asset')]"/>
                <filter name="filter_liability" string="Liability"
                        domain="[('category_type', '=', 'liability')]"/>
                <separator/>
                <filter name="filter_archived" string="Archived"
                        domain="[('active', '=', False)]"/>
                <group>
                    <filter name="group_type" string="Type"
                            context="{'group_by': 'category_type'}"/>
                    <filter name="group_parent" string="Parent"
                            context="{'group_by': 'parent_id'}"/>
                    <filter name="group_company" string="Company"
                            context="{'group_by': 'company_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- =============================================================
         Fiscal Category – Action
         ============================================================= -->
    <record id="action_fusion_fiscal_category" model="ir.actions.act_window">
        <field name="name">Fiscal Categories</field>
        <field name="res_model">fusion.fiscal.category</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="fusion_fiscal_category_view_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first fiscal category
            </p>
            <p>
                Fiscal categories group general-ledger accounts for
                structured fiscal reporting and SAF-T exports.
            </p>
        </field>
    </record>


    <!-- =============================================================
         SAF-T Export Wizard – Form View
         ============================================================= -->
    <record id="fusion_saft_export_view_form" model="ir.ui.view">
        <field name="name">fusion.saft.export.form</field>
        <field name="model">fusion.saft.export</field>
        <field name="arch" type="xml">
            <form string="SAF-T Export">
                <group invisible="state != 'error'">
                    <div class="alert alert-danger" role="alert">
                        <field name="error_message"/>
                    </div>
                </group>
                <group invisible="state != 'done'">
                    <div class="alert alert-success" role="alert">
                        SAF-T file generated successfully. The download
                        should start automatically.
                    </div>
                </group>
                <group>
                    <group string="Period">
                        <field name="date_from"/>
                        <field name="date_to"/>
                    </group>
                    <group string="Settings">
                        <field name="company_id" groups="base.group_multi_company"/>
                        <field name="saft_namespace" groups="base.group_no_one"/>
                        <field name="state" invisible="1"/>
                    </group>
                </group>
                <group invisible="state != 'done'">
                    <field name="attachment_id" filename="saft_filename"/>
                </group>
                <footer>
                    <button name="action_generate_saft"
                            string="Generate SAF-T"
                            type="object"
                            class="btn-primary"
                            invisible="state == 'done'"/>
                    <button string="Close" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- SAF-T Export – Action -->
    <record id="action_fusion_saft_export" model="ir.actions.act_window">
        <field name="name">SAF-T Export</field>
        <field name="res_model">fusion.saft.export</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>


    <!-- =============================================================
         SAF-T Import Wizard – Form View
         ============================================================= -->
    <record id="fusion_saft_import_view_form" model="ir.ui.view">
        <field name="name">fusion.saft.import.form</field>
        <field name="model">fusion.saft.import</field>
        <field name="arch" type="xml">
            <form string="SAF-T Import">
                <group invisible="state == 'done'">
                    <group string="File">
                        <field name="saft_file" filename="saft_filename"/>
                        <field name="saft_filename" invisible="1"/>
                    </group>
                    <group string="Settings">
                        <field name="company_id" groups="base.group_multi_company"/>
                        <field name="state" invisible="1"/>
                    </group>
                </group>
                <group invisible="state != 'done'">
                    <div class="alert alert-success" role="alert">
                        <strong>Import complete.</strong>
                    </div>
                    <group string="Summary">
                        <field name="accounts_created"/>
                        <field name="partners_created"/>
                        <field name="moves_created"/>
                    </group>
                </group>
                <group invisible="not import_log">
                    <field name="import_log" readonly="1" nolabel="1"
                           widget="text" colspan="2"/>
                </group>
                <footer>
                    <button name="action_preview"
                            string="Preview"
                            type="object"
                            class="btn-secondary"
                            invisible="state != 'upload'"/>
                    <button name="action_import"
                            string="Import"
                            type="object"
                            class="btn-primary"
                            invisible="state == 'done'"
                            confirm="This will create accounting records. Continue?"/>
                    <button string="Close" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- SAF-T Import – Action -->
    <record id="action_fusion_saft_import" model="ir.actions.act_window">
        <field name="name">SAF-T Import</field>
        <field name="res_model">fusion.saft.import</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>


    <!-- =============================================================
         Intrastat Commodity Codes – List / Form
         ============================================================= -->
    <record id="fusion_intrastat_code_view_list" model="ir.ui.view">
        <field name="name">fusion.intrastat.code.list</field>
        <field name="model">fusion.intrastat.code</field>
        <field name="arch" type="xml">
            <list string="Intrastat Codes" editable="bottom">
                <field name="code"/>
                <field name="name"/>
                <field name="supplementary_unit" optional="show"/>
                <field name="active" column_invisible="True"/>
            </list>
        </field>
    </record>

    <record id="fusion_intrastat_code_view_form" model="ir.ui.view">
        <field name="name">fusion.intrastat.code.form</field>
        <field name="model">fusion.intrastat.code</field>
        <field name="arch" type="xml">
            <form string="Intrastat Code">
                <sheet>
                    <group>
                        <field name="code"/>
                        <field name="name"/>
                        <field name="supplementary_unit"/>
                        <field name="active"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="action_fusion_intrastat_code" model="ir.actions.act_window">
        <field name="name">Commodity Codes</field>
        <field name="res_model">fusion.intrastat.code</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Add an Intrastat commodity code
            </p>
            <p>
                Commodity codes classify products under the Combined
                Nomenclature (CN8) for EU Intrastat declarations.
            </p>
        </field>
    </record>


    <!-- =============================================================
         Intrastat Report Wizard – Form View
         ============================================================= -->
    <record id="fusion_intrastat_report_view_form" model="ir.ui.view">
        <field name="name">fusion.intrastat.report.form</field>
        <field name="model">fusion.intrastat.report</field>
        <field name="arch" type="xml">
            <form string="Intrastat Declaration">
                <group>
                    <group string="Period">
                        <field name="date_from"/>
                        <field name="date_to"/>
                    </group>
                    <group string="Parameters">
                        <field name="flow_type"/>
                        <field name="company_id" groups="base.group_multi_company"/>
                        <field name="state" invisible="1"/>
                    </group>
                </group>
                <notebook invisible="state != 'done'">
                    <page string="Declaration Lines" name="lines">
                        <field name="line_ids" nolabel="1">
                            <list string="Intrastat Lines">
                                <field name="intrastat_code_id"/>
                                <field name="country_id"/>
                                <field name="transaction_code"/>
                                <field name="total_value" sum="Total Value"/>
                                <field name="total_weight" sum="Total Weight"/>
                                <field name="supplementary_qty" sum="Total Qty"/>
                            </list>
                        </field>
                    </page>
                </notebook>
                <footer>
                    <button name="action_compute"
                            string="Compute"
                            type="object"
                            class="btn-primary"
                            invisible="state == 'done'"/>
                    <button name="action_compute"
                            string="Recompute"
                            type="object"
                            class="btn-secondary"
                            invisible="state != 'done'"/>
                    <button string="Close" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- Intrastat Report – Action -->
    <record id="action_fusion_intrastat_report" model="ir.actions.act_window">
        <field name="name">Intrastat Declaration</field>
        <field name="res_model">fusion.intrastat.report</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>


    <!-- =============================================================
         Product Template – Inherit to show Intrastat fields
         ============================================================= -->
    <record id="fusion_product_template_intrastat_form" model="ir.ui.view">
        <field name="name">product.template.form.intrastat</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='general_information']" position="after">
                <page string="Intrastat" name="intrastat"
                      groups="account.group_account_user">
                    <group>
                        <group string="Trade Classification">
                            <field name="intrastat_code_id"/>
                            <field name="origin_country_id"/>
                        </group>
                        <group string="Physical Properties">
                            <field name="intrastat_weight"/>
                        </group>
                    </group>
                </page>
            </xpath>
        </field>
    </record>


    <!-- =============================================================
         Menu Structure: Accounting > Reports > Fiscal Compliance
         ============================================================= -->
    <menuitem id="menu_fiscal_compliance_root"
              name="Fiscal Compliance"
              parent="account.menu_finance_reports"
              sequence="90"
              groups="account.group_account_user"/>

    <menuitem id="menu_fiscal_category"
              name="Fiscal Categories"
              parent="menu_fiscal_compliance_root"
              action="action_fusion_fiscal_category"
              sequence="10"/>

    <menuitem id="menu_saft_export"
              name="SAF-T Export"
              parent="menu_fiscal_compliance_root"
              action="action_fusion_saft_export"
              sequence="20"/>

    <menuitem id="menu_saft_import"
              name="SAF-T Import"
              parent="menu_fiscal_compliance_root"
              action="action_fusion_saft_import"
              sequence="30"/>

    <menuitem id="menu_intrastat_report"
              name="Intrastat Declaration"
              parent="menu_fiscal_compliance_root"
              action="action_fusion_intrastat_report"
              sequence="40"/>

    <!-- Commodity Codes under Configuration -->
    <menuitem id="menu_intrastat_codes"
              name="Intrastat Codes"
              parent="account.menu_finance_configuration"
              action="action_fusion_intrastat_code"
              sequence="95"
              groups="account.group_account_manager"/>

</odoo>
