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


    <!-- Payroll Settings Form View -->
    <record id="payroll_config_settings_view_form" model="ir.ui.view">
        <field name="name">payroll.config.settings.form</field>
        <field name="model">payroll.config.settings</field>
        <field name="arch" type="xml">
            <form string="Payroll Settings">
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <field name="company_id" widget="res_company" options="{'no_create': True, 'no_open': True}"/>
                    </div>
                    <notebook>
                        
                        <!-- General Tax Info -->
                        <page string="General Tax Info" name="general_tax">
                            <div class="alert alert-info" role="alert">
                                <p>Tell us about your business so we can report your payroll wages and taxes. 
                                This is the info you used when you applied for a business number with the CRA.</p>
                            </div>
                            <group>
                                <group string="Company Legal Name">
                                    <field name="company_legal_name" placeholder="e.g., Westin Healthcare Inc"/>
                                    <div class="text-muted mt-2">
                                        <small>This may be different than a trade or doing business as (DBA) name.</small>
                                    </div>
                                </group>
                            </group>
                            <group string="Company Legal Address">
                                <group>
                                    <field name="company_legal_street" placeholder="Street address"/>
                                    <field name="company_legal_street2" placeholder="Apt, Suite, Unit (optional)"/>
                                    <field name="company_legal_city" placeholder="City"/>
                                </group>
                                <group>
                                    <field name="company_legal_country_id" placeholder="Country"/>
                                    <field name="company_legal_state_id" placeholder="Province"/>
                                    <field name="company_legal_zip" placeholder="Postal code"/>
                                </group>
                            </group>
                        </page>

                        <!-- Contact Information -->
                        <page string="Contact Information" name="contact_info">
                            <div class="alert alert-info" role="alert">
                                <p>Who's your payroll contact?</p>
                                <p>This information is required for payroll forms such as the Record of Employment (ROE) or the T4 Summary (T4SUM). 
                                <a href="#" target="_blank">Learn more about different roles in payroll</a>.</p>
                            </div>
                            <group>
                                <group>
                                    <field name="payroll_contact_first_name" placeholder="First name"/>
                                    <field name="payroll_contact_last_name" placeholder="Last name"/>
                                </group>
                                <group>
                                    <field name="payroll_contact_phone" placeholder="Business phone"/>
                                    <field name="payroll_contact_email" placeholder="Email address"/>
                                </group>
                            </group>
                        </page>

                        <!-- Federal Tax Info -->
                        <page string="Federal Tax Info" name="federal_tax">
                            <div class="alert alert-info" role="alert">
                                <p>Once you have your tax info, you can correctly pay your federal taxes. 
                                You can find what you need in letters and tax notices you have received from the CRA.</p>
                            </div>
                            <group string="CRA Payroll Number">
                                <group>
                                    <label for="cra_business_number" string="CRA payroll number"/>
                                    <div class="d-flex align-items-center gap-2">
                                        <field name="cra_business_number" placeholder="Business Number"/>
                                        <span class="text-muted">RP</span>
                                        <field name="cra_reference_number" placeholder="Reference Number"/>
                                    </div>
                                    <div class="mt-2">
                                        <a href="#" target="_blank">Don't have it?</a>
                                    </div>
                                </group>
                            </group>
                            <group string="Social Insurance Numbers">
                                <group>
                                    <field name="cra_owner1_sin" placeholder="SIN: Owner 1"/>
                                    <field name="cra_owner2_sin" placeholder="SIN: Owner 2 (optional)"/>
                                    <field name="cra_representative_rac" placeholder="Representative Identifier (RAC) - optional"/>
                                </group>
                            </group>
                            <group string="Tax Payment Frequency">
                                <group>
                                    <label for="federal_tax_display" string="How often do you pay your taxes?"/>
                                    <field name="federal_tax_display" readonly="1"/>
                                </group>
                                <group>
                                    <field name="federal_tax_form_type" placeholder="e.g., Form PD7A"/>
                                    <field name="federal_tax_payment_frequency"/>
                                    <field name="federal_tax_effective_date"/>
                                </group>
                            </group>
                        </page>

                        <!-- Provincial Tax Info -->
                        <page string="Provincial Tax Info" name="provincial_tax">
                            <div class="alert alert-info" role="alert">
                                <p>We'll use this info to calculate and track your workers' compensation premiums based on where your employees work. 
                                You can find it in the letters or emails you receive from the province. 
                                <a href="#" target="_blank">Learn more</a>.</p>
                            </div>
                            <group>
                                <field name="provincial_tax_schedule_ids" nolabel="1" context="{'default_config_id': id}"/>
                            </group>
                        </page>

                        <!-- Work Locations -->
                        <page string="Work Locations" name="work_locations">
                            <group>
                                <field name="work_location_ids" nolabel="1"/>
                            </group>
                        </page>

                        <!-- Email Notifications -->
                        <page string="Email Notifications" name="email_notifications">
                            <group>
                                <group string="Primary Notification Recipients">
                                    <field name="notification_email_primary_ids" widget="many2many_tags" options="{'no_create': True}"/>
                                </group>
                            </group>
                            <group string="Notification Preferences">
                                <group>
                                    <field name="notification_setup_send_to" string="Setup notifications"/>
                                    <field name="notification_form_filing_send_to" string="Form filing notifications"/>
                                    <field name="notification_payday_send_to" string="Payday notifications"/>
                                </group>
                                <group>
                                    <field name="notification_tax_send_to" string="Tax notifications"/>
                                    <field name="notification_payday_reminders_send_to" string="Payday reminders"/>
                                    <field name="notification_tax_setup_reminders_send_to" string="Tax setup reminders"/>
                                </group>
                            </group>
                        </page>

                        <!-- Bank Accounts -->
                        <page string="Bank Accounts" name="bank_accounts">
                            <group>
                                <group>
                                    <field name="payroll_bank_account_id" 
                                           options="{'no_create': True, 'no_open': True}"
                                           domain="[('company_id', '=', company_id)]"/>
                                    <div class="mt-2">
                                        <a href="#" target="_blank">Manage payroll principal officer information</a>
                                    </div>
                                </group>
                                <group>
                                    <field name="payroll_principal_officer_info" widget="text"/>
                                </group>
                            </group>
                        </page>

                        <!-- Printing -->
                        <page string="Printing" name="printing">
                            <div class="alert alert-info" role="alert">
                                <p>Choose your default printing preferences:</p>
                            </div>
                            <group>
                                <group>
                                    <field name="print_preference" widget="radio"/>
                                    <field name="paystub_layout" 
                                           invisible="print_preference != 'paycheques_and_stubs'"
                                           widget="radio"/>
                                    <div class="mt-2" invisible="print_preference != 'paycheques_and_stubs'">
                                        <a href="#" target="_blank">Order cheques</a>
                                    </div>
                                </group>
                            </group>
                            <group string="Vacation Display Options">
                                <group>
                                    <field name="show_accrued_vacation_hours"/>
                                    <field name="show_accrued_vacation_balance"/>
                                </group>
                            </group>
                            <div class="mt-3">
                                <button name="action_align_printer" string="Align my printer" type="object" class="btn-secondary"/>
                            </div>
                            <div class="mt-3">
                                <p>Need help? <a href="#" target="_blank">Check out guidance</a></p>
                            </div>
                        </page>

                        <!-- Direct Deposit -->
                        <page string="Direct Deposit" name="direct_deposit" invisible="1">
                            <group>
                                <group>
                                    <field name="direct_deposit_funding_time" string="Funding time"/>
                                    <field name="direct_deposit_funding_limit" string="Funding limit"/>
                                    <field name="direct_deposit_funding_period_days" string="Funding period (days)"/>
                                </group>
                            </group>
                        </page>

                        <!-- Workers' Compensation -->
                        <page string="Workers' Compensation" name="workers_comp">
                            <div class="alert alert-info" role="alert">
                                <p>Add the details of your workers' comp insurance coverage.</p>
                            </div>
                            <group>
                                <group>
                                    <field name="workers_comp_province"/>
                                    <field name="workers_comp_class" placeholder="Workers' Comp Class"/>
                                    <field name="workers_comp_account_number" placeholder="Account Number"/>
                                </group>
                            </group>
                        </page>

                        <!-- Accounting Preferences -->
                        <page string="Accounting" name="accounting" groups="account.group_account_readonly">
                            <div class="alert alert-info" role="alert">
                                <p>Choose how payroll transactions are mapped to your chart of accounts.</p>
                            </div>
                            <group>
                                <group string="Paycheque and Payroll Tax Payments">
                                    <field name="account_bank_account_id" 
                                           options="{'no_create': True}"/>
                                    <field name="account_wage_expense_id" 
                                           options="{'no_create': True}"/>
                                    <field name="account_employer_tax_expense_id" 
                                           options="{'no_create': True}"
                                           domain="[('account_type', 'in', ('expense', 'liability_current')), ('company_ids', 'parent_of', company_id)]"/>
                                </group>
                                <group string="Tax Liability Accounts">
                                    <field name="account_federal_tax_liability_id" 
                                           options="{'no_create': True}"
                                           domain="[('account_type', 'in', ('expense', 'liability_current')), ('company_ids', 'parent_of', company_id)]"/>
                                    <field name="account_ontario_tax_liability_id" 
                                           options="{'no_create': True}"
                                           domain="[('account_type', 'in', ('expense', 'liability_current')), ('company_ids', 'parent_of', company_id)]"/>
                                </group>
                                <group string="Other Liability and Assets">
                                    <field name="account_vacation_pay_liability_id" 
                                           options="{'no_create': True}"
                                           domain="[('account_type', 'in', ('expense', 'liability_current')), ('company_ids', 'parent_of', company_id)]"/>
                                </group>
                            </group>
                            <group string="Class Tracking">
                                <div class="alert alert-info" role="alert">
                                    <p><strong>How do you want to track classes for payroll transactions in QuickBooks?</strong></p>
                                </div>
                                <field name="account_class_tracking" widget="radio" options="{'horizontal': true}"/>
                            </group>
                            <group string="Update Transactions" col="12">
                                <div class="alert alert-warning" role="alert" style="padding: 15px 20px; font-size: 14px; line-height: 1.5; width: 100%;">
                                    <p style="margin: 0;">Update accounting preferences for past transactions according to current preference settings (we'll update all accounts except the Bank Account section).</p>
                                </div>
                            </group>
                        </page>

                        <!-- Auto Payroll -->
                        <page string="Auto Payroll" name="auto_payroll" invisible="1">
                            <div class="alert alert-warning" role="alert">
                                <p><strong>Currently ineligible.</strong></p>
                                <field name="auto_payroll_ineligibility_reason" readonly="1" nolabel="1"/>
                                <div class="mt-2">
                                    <a href="#" target="_blank">Why am I ineligible?</a>
                                </div>
                            </div>
                            <group>
                                <field name="auto_payroll_enabled" readonly="1"/>
                            </group>
                        </page>

                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Payroll Settings List View -->
    <record id="payroll_config_settings_view_tree" model="ir.ui.view">
        <field name="name">payroll.config.settings.list</field>
        <field name="model">payroll.config.settings</field>
        <field name="arch" type="xml">
            <list string="Payroll Settings">
                <field name="company_id"/>
                <field name="company_legal_name"/>
                <field name="payroll_contact_email"/>
            </list>
        </field>
    </record>

    <!-- Payroll Settings Action - Server Action to get/create settings -->
    <record id="action_payroll_config_settings" model="ir.actions.server">
        <field name="name">Payroll Settings</field>
        <field name="model_id" ref="model_payroll_config_settings"/>
        <field name="state">code</field>
        <field name="code">
settings = model.get_settings()
action = {
    'type': 'ir.actions.act_window',
    'name': 'Payroll Settings',
    'res_model': 'payroll.config.settings',
    'res_id': settings.id,
    'view_mode': 'form',
    'target': 'current',
}
        </field>
    </record>

</odoo>
