<?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright 2024-2025 Nexa Systems Inc.
    License OPL-1 (Odoo Proprietary License v1.0)
    Part of the Fusion Claim Assistant product family.
-->
<odoo>
    <!-- ===================================================================== -->
    <!-- INVOICE FORM: Header Fields -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_header" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.header</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            <!-- Add hidden detection flags -->
            <xpath expr="//field[@name='move_type']" position="after">
                <field name="x_fc_is_adp_invoice" invisible="1"/>
                <field name="x_fc_show_authorizer" invisible="1"/>
                <field name="x_fc_show_authorizer_question" invisible="1"/>
            </xpath>
            
            <!-- Add Portion badge at the top (header area) for split invoices -->
            <xpath expr="//div[hasclass('oe_title')]" position="before">
                <div class="float-end" invisible="move_type not in ['out_invoice', 'out_refund'] or x_fc_adp_invoice_portion == 'full'">
                    <field name="x_fc_adp_invoice_portion" widget="badge" readonly="1"
                           decoration-info="x_fc_adp_invoice_portion == 'client'"
                           decoration-warning="x_fc_adp_invoice_portion == 'adp'"/>
                </div>
            </xpath>
            
            <!-- Add Invoice Type, Authorizer Required?, Authorizer, Client Type after Journal div -->
            <xpath expr="//div[@name='journal_div']" position="after">
                <field name="x_fc_invoice_type" string="Invoice Type"
                       invisible="move_type not in ['out_invoice', 'out_refund']"/>
                       
                <!-- Authorizer Required? - only for odsp, direct_private, insurance, other, rental -->
                <field name="x_fc_authorizer_required" string="Authorizer Required?"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_show_authorizer_question"/>
                
                <!-- Authorizer - shown based on invoice type and authorizer_required -->
                <field name="x_fc_authorizer_id" string="Authorizer"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_show_authorizer"
                       options="{'no_create': False, 'no_quick_create': False}"/>
                       
                <field name="x_fc_client_type" string="Client Type"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: Export Button, Sync Button, and Device Verification -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_button" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.button</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">52</field>
        <field name="arch" type="xml">
            <!-- Add Export and Verify buttons to header -->
            <xpath expr="//button[@name='button_draft']" position="after">
                <field name="x_fc_is_adp_invoice" invisible="1"/>
                <field name="x_fc_needs_device_verification" invisible="1"/>
                <field name="x_fc_adp_invoice_portion" invisible="1"/>
                <!-- Verify Device Approval button - only on client invoices that need verification -->
                <button name="action_open_device_approval_wizard" 
                        string="Verify Device Approval" 
                        type="object"
                        class="btn-warning"
                        icon="fa-check-square-o"
                        invisible="not x_fc_needs_device_verification"
                        help="Complete device verification to enable ADP invoice creation"/>
                <button name="action_export_adp_claim" 
                        string="Export ADP" 
                        type="object"
                        class="btn-secondary"
                        icon="fa-file-text-o"
                        invisible="move_type not in ['out_invoice', 'out_refund'] or state != 'posted' or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'adp'"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: ADP Billing Status Statusbar -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_billing_status" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.billing.status</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">49</field>
        <field name="arch" type="xml">
            <!-- Add ADP Billing Status statusbar after the main header -->
            <xpath expr="//header" position="inside">
                <field name="x_fc_is_adp_invoice" invisible="1"/>
                <field name="x_fc_is_mod_invoice" invisible="1"/>
                <field name="x_fc_adp_billing_status" widget="statusbar" nolabel="1"
                       statusbar_visible="waiting,submitted,payment_issued"
                       clickable="1"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'adp'"/>
                <!-- Send to MOD button for March of Dimes invoices -->
                <button name="action_mod_send_invoice" type="object"
                        string="Send to Case Worker" class="btn-primary" icon="fa-paper-plane"
                        invisible="move_type != 'out_invoice' or not x_fc_is_mod_invoice"
                        help="Send this invoice to the March of Dimes case worker"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: Device Verification Alert Banner -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_verification_alert" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.verification.alert</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">51</field>
        <field name="arch" type="xml">
            <!-- Add verification alert banner at the top of the form -->
            <xpath expr="//sheet" position="before">
                <field name="x_fc_needs_device_verification" invisible="1"/>
                <field name="x_fc_adp_invoice_portion" invisible="1"/>
                <!-- Alert for client invoices needing device verification -->
                <div class="alert alert-warning d-flex align-items-center" role="alert"
                     invisible="not x_fc_needs_device_verification">
                    <i class="fa fa-exclamation-triangle fa-2x me-3" title="Verification Pending"/>
                    <div>
                        <strong>Device Verification Pending</strong>
                        <p class="mb-0">
                            ADP funding approval verification is required before creating the ADP invoice.
                            Click <strong>Verify Device Approval</strong> to complete verification.
                        </p>
                    </div>
                </div>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: ADP Case Details Tab -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_adp_tab" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.adp.tab</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">55</field>
        <field name="arch" type="xml">
            <!-- Add ADP Case Details tab in notebook -->
            <xpath expr="//notebook" position="inside">
                <!-- Use computed x_fc_is_adp_invoice which checks BOTH Studio and module fields -->
                <field name="x_fc_is_adp_invoice" invisible="1"/>
                <page string="ADP Case Details" name="adp_case_details"
                      invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice">
                    <group>
                        <group string="Claim Information">
                            <field name="x_fc_claim_number"/>
                            <field name="x_fc_client_ref_1"/>
                            <field name="x_fc_client_ref_2"/>
                        </group>
                        <group string="Dates">
                            <field name="x_fc_adp_delivery_date"/>
                            <field name="x_fc_service_start_date"/>
                            <field name="x_fc_service_end_date"/>
                        </group>
                    </group>
                    <group>
                        <group string="Export Status">
                            <field name="adp_exported" readonly="1"/>
                            <field name="adp_export_date" readonly="1" invisible="not adp_exported"/>
                            <field name="adp_export_count" readonly="1" invisible="not adp_exported"/>
                        </group>
                    </group>
                    <group invisible="x_fc_adp_invoice_portion != 'adp'">
                        <group string="ADP Billing Status">
                            <field name="x_fc_adp_billing_status"/>
                        </group>
                    </group>
                    
                </page>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: ADP Summary Tab -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_tab" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.tab</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">60</field>
        <field name="arch" type="xml">
            <!-- Add ADP Summary tab in notebook -->
            <xpath expr="//notebook" position="inside">
                <!-- Use computed x_fc_is_adp_invoice which checks BOTH Studio and module fields -->
                <page string="ADP Summary" name="adp_summary" 
                      invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice">
                    
                    <!-- DEDUCTION ALERT - Only show when there are deductions -->
                    <field name="x_fc_has_deductions" invisible="1"/>
                    <field name="x_fc_total_deduction_amount" invisible="1"/>
                    
                    <div class="alert alert-warning mb-3" role="alert" invisible="not x_fc_has_deductions">
                        <strong><i class="fa fa-minus-circle"/> Deductions Applied</strong>
                        <p class="mb-0">
                            ADP deductions have been applied to this invoice. 
                            Total deduction impact: <strong><field name="x_fc_total_deduction_amount" widget="monetary" class="oe_inline"/></strong>
                        </p>
                    </div>
                    
                    <group>
                        <group string="ADP Portion Breakdown">
                            <field name="x_fc_adp_portion_total" string="Total ADP Portion" widget="monetary"/>
                            <field name="x_fc_client_portion_total" string="Total Client Portion" widget="monetary"/>
                        </group>
                        <group string="Invoice Information">
                            <field name="x_fc_invoice_type" string="Invoice Type" readonly="1"/>
                            <field name="x_fc_client_type" string="Client Type" readonly="1"/>
                        </group>
                    </group>
                    
                    <!-- Invoice line details for ADP Summary - filtered product lines with conditional deduction columns -->
                    <field name="x_fc_product_lines" mode="list" readonly="1" nolabel="1" class="w-100"
                           context="{'show_deductions': x_fc_has_deductions}">
                        <list string="Line Details" create="0" delete="0" limit="50"
                              decoration-success="x_fc_adp_approved"
                              decoration-danger="not x_fc_adp_approved and x_fc_adp_device_type"
                              decoration-warning="x_fc_deduction_type and x_fc_deduction_type != 'none'">
                            <field name="x_fc_adp_approved" string="✓" widget="boolean" width="40px"
                                   help="Device approved by ADP"/>
                            <field name="name" string="Description" class="text-wrap"/>
                            <field name="x_fc_adp_device_type" string="Device Type" optional="show" width="150px"/>
                            <field name="x_fc_serial_number" string="S/N" optional="show" width="90px"/>
                            <field name="quantity" string="Qty" width="60px"/>
                            <field name="price_unit" string="Unit $" width="100px"/>
                            <field name="x_fc_deduction_type" string="Ded." width="70px"
                                   column_invisible="not parent.x_fc_has_deductions"/>
                            <field name="x_fc_deduction_value" string="Ded.$" width="80px"
                                   column_invisible="not parent.x_fc_has_deductions"/>
                            <field name="x_fc_adp_portion" string="ADP $" width="100px"/>
                            <field name="x_fc_client_portion" string="Client $" width="100px"/>
                            <field name="price_subtotal" string="Total" width="100px"/>
                            <field name="x_fc_device_placement" string="Plcmt" optional="hide" width="70px"/>
                        </list>
                    </field>
                    <div class="alert alert-info" role="alert">
                        <strong>Calculation Rules:</strong>
                        <ul>
                            <li><strong>REG Client:</strong> ADP Portion = 75%, Client Portion = 25%</li>
                            <li><strong>ODS/OWP/ACS/LTC/SEN/CCA:</strong> ADP Portion = 100%, Client Portion = 0%</li>
                            <li><strong>PCT Deduction:</strong> ADP pays X% of their normal portion</li>
                            <li><strong>AMT Deduction:</strong> Fixed $ amount deducted from ADP portion</li>
                        </ul>
                        <button name="action_recalculate_adp_portions" type="object" 
                                string="Recalculate Portions" class="btn-primary"
                                help="Manually recalculate ADP and Client portions"/>
                    </div>
                </page>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE LINE: Add S/N, ADP Portion, Client Portion columns -->
    <!-- ===================================================================== -->
    <record id="view_move_form_line_fusion_claims" model="ir.ui.view">
        <field name="name">account.move.form.line.fusion.central</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">65</field>
        <field name="arch" type="xml">
            <!-- Add serial number column right after product name/description - always visible for invoices -->
            <xpath expr="//field[@name='invoice_line_ids']//field[@name='name']" position="after">
                <field name="x_fc_serial_number" string="S/N" width="100px"
                       column_invisible="parent.move_type not in ['out_invoice', 'out_refund']"/>
            </xpath>
            
            <!-- Add ADP Portion and Client Portion columns after taxes - only for ADP invoices -->
            <xpath expr="//field[@name='invoice_line_ids']//field[@name='tax_ids']" position="after">
                <field name="x_fc_adp_portion" string="ADP" widget="monetary" optional="show"
                       column_invisible="parent.move_type not in ['out_invoice', 'out_refund'] or not parent.x_fc_is_adp_invoice"/>
                <field name="x_fc_client_portion" string="Client" widget="monetary" optional="show"
                       column_invisible="parent.move_type not in ['out_invoice', 'out_refund'] or not parent.x_fc_is_adp_invoice"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE FORM: ADP Totals in Summary Footer -->
    <!-- ===================================================================== -->
    <record id="view_move_form_fusion_claims_totals" model="ir.ui.view">
        <field name="name">account.move.form.fusion.central.totals</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_move_form"/>
        <field name="priority">70</field>
        <field name="arch" type="xml">
            <!-- Add ADP portion totals inside the subtotal footer group -->
            <xpath expr="//group[hasclass('oe_subtotal_footer')]" position="inside">
                <field name="x_fc_is_adp_invoice" invisible="1"/>
                <field name="x_fc_adp_invoice_portion" invisible="1"/>
                
                <!-- ADP Portion Breakdown - always show for ADP invoices -->
                <div colspan="2" invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice">
                    <hr class="mt-2 mb-2"/>
                </div>
                
                <!-- Total ADP Portion (sum of line ADP portions) -->
                <label for="x_fc_adp_portion_total" string="Total ADP Portion:" 
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice"/>
                <field name="x_fc_adp_portion_total" widget="monetary" nolabel="1"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice"/>
                
                <!-- Total Client Portion (sum of line client portions) -->
                <label for="x_fc_client_portion_total" string="Total Client Portion:" 
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice"/>
                <field name="x_fc_client_portion_total" widget="monetary" nolabel="1"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice"/>
                
                <!-- Sibling Invoice Totals -->
                <div colspan="2" invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion == 'full'">
                    <hr class="mt-1 mb-1"/>
                </div>
                <!-- On Client invoice (25%), show the ADP invoice total -->
                <label for="x_fc_sibling_adp_total" string="ADP Invoice Total:" 
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'client'"/>
                <field name="x_fc_sibling_adp_total" widget="monetary" nolabel="1"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'client'"/>
                <!-- On ADP invoice (75%), show the Client invoice total -->
                <label for="x_fc_sibling_client_total" string="Client Invoice Total:" 
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'adp'"/>
                <field name="x_fc_sibling_client_total" widget="monetary" nolabel="1"
                       invisible="move_type not in ['out_invoice', 'out_refund'] or not x_fc_is_adp_invoice or x_fc_adp_invoice_portion != 'adp'"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE LIST: Custom Columns -->
    <!-- ===================================================================== -->
    <record id="view_out_invoice_tree_fusion_claims" model="ir.ui.view">
        <field name="name">account.move.list.fusion.central</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_invoice_tree"/>
        <field name="priority">80</field>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='amount_untaxed_in_currency_signed']" position="before">
                <field name="x_fc_invoice_type" string="Invoice Type" optional="hide"/>
                <field name="x_fc_client_type" string="Client Type" optional="hide"/>
                <field name="x_fc_claim_number" string="Claim #" optional="hide"/>
                <field name="x_fc_client_ref_1" string="Client Ref 1" optional="hide"/>
                <field name="x_fc_client_ref_2" string="Client Ref 2" optional="hide"/>
                <field name="partner_shipping_id" string="Delivery Address" optional="hide"/>
                <field name="x_fc_adp_invoice_portion" string="Portion" optional="hide" widget="badge"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- INVOICE SEARCH: Filters -->
    <!-- ===================================================================== -->
    <record id="view_account_invoice_filter_fusion_claims" model="ir.ui.view">
        <field name="name">account.move.search.fusion.central</field>
        <field name="model">account.move</field>
        <field name="inherit_id" ref="account.view_account_invoice_filter"/>
        <field name="arch" type="xml">
            <xpath expr="//search" position="inside">
                <!-- Search Fields -->
                <field name="partner_shipping_id" string="Delivery Address"/>
                <field name="x_fc_claim_number" string="Claim Number"/>
                <field name="x_fc_client_ref_1" string="Client Reference 1"/>
                <field name="x_fc_client_ref_2" string="Client Reference 2"/>
                <field name="x_fc_invoice_type" string="Invoice Type"/>
                <field name="x_fc_client_type" string="Client Type"/>
                <separator/>
                <!-- Sale Type Filters -->
                <filter string="ADP" name="type_adp"
                        domain="[('x_fc_invoice_type', 'in', ['adp', 'adp_odsp'])]"/>
                <filter string="ADP Client" name="type_adp_client"
                        domain="[('x_fc_invoice_type', '=', 'adp_client')]"/>
                <filter string="ODSP" name="type_odsp"
                        domain="[('x_fc_invoice_type', 'in', ['odsp', 'adp_odsp'])]"/>
                <filter string="MOD" name="type_mod"
                        domain="[('x_fc_invoice_type', '=', 'march_of_dimes')]"/>
                <filter string="WSIB" name="type_wsib"
                        domain="[('x_fc_invoice_type', '=', 'wsib')]"/>
                <filter string="Insurance" name="type_insurance"
                        domain="[('x_fc_invoice_type', '=', 'insurance')]"/>
                <filter string="Direct/Private" name="type_direct_private"
                        domain="[('x_fc_invoice_type', '=', 'direct_private')]"/>
                <filter string="Hardship" name="type_hardship"
                        domain="[('x_fc_invoice_type', '=', 'hardship')]"/>
                <filter string="Rentals" name="type_rental"
                        domain="[('x_fc_invoice_type', '=', 'rental')]"/>
                <filter string="Muscular Dystrophy" name="type_muscular_dystrophy"
                        domain="[('x_fc_invoice_type', '=', 'muscular_dystrophy')]"/>
                <filter string="Others" name="type_other"
                        domain="[('x_fc_invoice_type', '=', 'other')]"/>
                <filter string="Regular" name="type_regular"
                        domain="[('x_fc_invoice_type', '=', 'regular')]"/>
                <separator/>
                <!-- ADP Export Filters -->
                <filter string="ADP Exported" name="adp_exported" 
                        domain="[('adp_exported', '=', True)]"/>
                <filter string="Not ADP Exported" name="not_adp_exported" 
                        domain="[('adp_exported', '=', False), ('x_fc_invoice_type', 'in', ['adp', 'adp_odsp']), ('move_type', 'in', ['out_invoice', 'out_refund'])]"/>
                <separator/>
                <!-- Client Type Filters -->
                <filter string="REG Clients" name="reg_clients" 
                        domain="[('x_fc_client_type', '=', 'REG')]"/>
                <filter string="ODS/OWP/ACS" name="full_funding" 
                        domain="[('x_fc_client_type', 'in', ['ODS', 'OWP', 'ACS'])]"/>
                <separator/>
                <!-- Invoice Portion Filters -->
                <filter string="Client Invoices (25%)" name="client_invoices" 
                        domain="[('x_fc_adp_invoice_portion', '=', 'client')]"/>
                <filter string="ADP Invoices (75%)" name="adp_portion_invoices" 
                        domain="[('x_fc_adp_invoice_portion', '=', 'adp')]"/>
                <separator/>
                <!-- ADP Billing Status Filters -->
                <filter string="Billing: Waiting" name="billing_waiting" 
                        domain="[('x_fc_adp_billing_status', '=', 'waiting')]"/>
                <filter string="Billing: Submitted" name="billing_submitted" 
                        domain="[('x_fc_adp_billing_status', '=', 'submitted')]"/>
                <filter string="Billing: Need Correction" name="billing_need_correction" 
                        domain="[('x_fc_adp_billing_status', '=', 'need_correction')]"/>
                <filter string="Billing: Payment Issued" name="billing_payment_issued" 
                        domain="[('x_fc_adp_billing_status', '=', 'payment_issued')]"/>
                <separator/>
                <!-- Group By -->
                <filter string="Invoice Type" name="group_invoice_type"
                        context="{'group_by': 'x_fc_invoice_type'}"/>
                <filter string="Client Type" name="group_client_type"
                        context="{'group_by': 'x_fc_client_type'}"/>
                <filter string="Invoice Portion" name="group_invoice_portion"
                        context="{'group_by': 'x_fc_adp_invoice_portion'}"/>
                <filter string="Billing Status" name="group_billing_status"
                        context="{'group_by': 'x_fc_adp_billing_status'}"/>
            </xpath>
        </field>
    </record>

    <!-- ===================================================================== -->
    <!-- SERVER ACTION: Bulk Export -->
    <!-- ===================================================================== -->
    <record id="action_adp_export_wizard" model="ir.actions.act_window">
        <field name="name">Export ADP Claims</field>
        <field name="res_model">fusion_claims.export.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="binding_model_id" ref="account.model_account_move"/>
        <field name="binding_view_types">list</field>
    </record>
</odoo>
