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

    <!-- Pay Stub Report Action -->
    <record id="action_report_paystub_canada" model="ir.actions.report">
        <field name="name">Pay Stub (Canada)</field>
        <field name="model">hr.payslip</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">fusion_payroll.report_paystub_canada_document</field>
        <field name="report_file">fusion_payroll.report_paystub_canada_document</field>
        <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
        <field name="binding_type">report</field>
    </record>

    <!-- Pay Stub Template -->
    <template id="report_paystub_canada_document">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <t t-call="web.external_layout">
                    <div class="page">
                        <!-- Header Section -->
                        <div class="row mb-4">
                            <div class="col-6">
                                <h5 class="text-muted">EMPLOYER</h5>
                                <strong><t t-out="o.company_id.name"/></strong><br/>
                                <t t-if="o.company_id.street"><t t-out="o.company_id.street"/><br/></t>
                                <t t-if="o.company_id.city">
                                    <t t-out="o.company_id.city"/>,
                                    <t t-out="o.company_id.state_id.code or ''"/>
                                    <t t-out="o.company_id.zip or ''"/>
                                </t>
                            </div>
                            <div class="col-6 text-end">
                                <h3>Pay Stub</h3>
                                <table class="table table-sm table-borderless text-end" style="margin-left: auto;">
                                    <tr>
                                        <td class="text-muted">Pay Date:</td>
                                        <td><strong><t t-out="o.date_to" t-options="{'widget': 'date'}"/></strong></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Cheque #:</td>
                                        <td><t t-out="o.cheque_number or '-'"/></td>
                                    </tr>
                                    <tr>
                                        <td class="text-muted">Net Pay:</td>
                                        <td class="fs-4 text-success"><strong><t t-out="o.net_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></strong></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <!-- Employee Info -->
                        <div class="row mb-4">
                            <div class="col-6">
                                <h5 class="text-muted">EMPLOYEE</h5>
                                <strong><t t-out="o.employee_id.name"/></strong><br/>
                                <t t-if="o.employee_id.home_street"><t t-out="o.employee_id.home_street"/><br/></t>
                                <t t-if="o.employee_id.home_city">
                                    <t t-out="o.employee_id.home_city"/>,
                                    <t t-out="o.employee_id.home_province or ''"/>
                                    <t t-out="o.employee_id.home_postal_code or ''"/>
                                </t>
                            </div>
                            <div class="col-6">
                                <h5 class="text-muted">PAY PERIOD</h5>
                                <table class="table table-sm table-borderless">
                                    <tr>
                                        <td>Period Beginning:</td>
                                        <td><t t-out="o.date_from" t-options="{'widget': 'date'}"/></td>
                                    </tr>
                                    <tr>
                                        <td>Period Ending:</td>
                                        <td><t t-out="o.date_to" t-options="{'widget': 'date'}"/></td>
                                    </tr>
                                    <tr>
                                        <td>Pay Date:</td>
                                        <td><t t-out="o.date_to" t-options="{'widget': 'date'}"/></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <!-- Earnings Section -->
                        <h5 class="bg-light p-2">EARNINGS</h5>
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Description</th>
                                    <th class="text-end">Hours</th>
                                    <th class="text-end">Rate</th>
                                    <th class="text-end">Current</th>
                                    <th class="text-end">YTD</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="o.line_ids.filtered(lambda l: l.category_id.code in ['BASIC', 'ALW'] and l.total != 0)" t-as="line">
                                    <tr>
                                        <td><t t-out="line.name"/></td>
                                        <td class="text-end"><t t-out="line.quantity" t-options="{'widget': 'float', 'precision': 2}"/></td>
                                        <td class="text-end"><t t-out="line.rate" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                        <td class="text-end"><t t-out="line.total" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                        <td class="text-end text-muted">-</td>
                                    </tr>
                                </t>
                            </tbody>
                            <tfoot>
                                <tr class="fw-bold">
                                    <td colspan="3">Total Earnings</td>
                                    <td class="text-end"><t t-out="o.gross_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td class="text-end text-muted"><t t-out="o.ytd_gross" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                </tr>
                            </tfoot>
                        </table>

                        <!-- Taxes Section -->
                        <h5 class="bg-light p-2">TAXES</h5>
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Description</th>
                                    <th class="text-end">Current</th>
                                    <th class="text-end">YTD</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>Income Tax</td>
                                    <td class="text-end"><t t-out="o.employee_income_tax" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td class="text-end text-muted"><t t-out="o.ytd_income_tax" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                </tr>
                                <tr>
                                    <td>Employment Insurance</td>
                                    <td class="text-end"><t t-out="o.employee_ei" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td class="text-end text-muted"><t t-out="o.ytd_ei" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                </tr>
                                <tr>
                                    <td>Canada Pension Plan</td>
                                    <td class="text-end"><t t-out="o.employee_cpp" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td class="text-end text-muted"><t t-out="o.ytd_cpp" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                </tr>
                                <tr t-if="o.employee_cpp2">
                                    <td>Second Canada Pension Plan</td>
                                    <td class="text-end"><t t-out="o.employee_cpp2" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td class="text-end text-muted"><t t-out="o.ytd_cpp2" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr class="fw-bold">
                                    <td>Total Taxes</td>
                                    <td class="text-end"><t t-out="o.total_employee_deductions" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        </table>

                        <!-- Summary Section -->
                        <div class="row mt-4">
                            <div class="col-6 offset-6">
                                <h5 class="bg-dark text-white p-2">SUMMARY</h5>
                                <table class="table table-sm">
                                    <tr>
                                        <td>Total Pay</td>
                                        <td class="text-end"><t t-out="o.gross_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                        <td class="text-end text-muted"><t t-out="o.ytd_gross" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    </tr>
                                    <tr>
                                        <td>Taxes</td>
                                        <td class="text-end text-danger">-<t t-out="o.total_employee_deductions" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Deductions</td>
                                        <td class="text-end">$0.00</td>
                                        <td></td>
                                    </tr>
                                    <tr class="fw-bold fs-5">
                                        <td>Net Pay</td>
                                        <td class="text-end text-success"><t t-out="o.net_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                        <td class="text-end text-muted"><t t-out="o.ytd_net" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <!-- Footer -->
                        <div class="row mt-4 pt-4 border-top">
                            <div class="col-12 text-center text-muted small">
                                <p>This is a computer-generated document. Please retain for your records.</p>
                            </div>
                        </div>

                    </div>
                </t>
            </t>
        </t>
    </template>

    <!-- Cheque-Style Pay Stub (3-part format) -->
    <record id="action_report_paycheque_canada" model="ir.actions.report">
        <field name="name">Paycheque (Canada)</field>
        <field name="model">hr.payslip</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">fusion_payroll.report_paycheque_canada_document</field>
        <field name="report_file">fusion_payroll.report_paycheque_canada_document</field>
        <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
        <field name="binding_type">report</field>
        <field name="paperformat_id" ref="base.paperformat_us"/>
    </record>

    <template id="report_paycheque_canada_document">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <div class="page" style="page-break-after: always;">
                    
                    <!-- TOP STUB - Employee Copy -->
                    <div style="height: 30%; border-bottom: 1px dashed #ccc; padding-bottom: 10px; margin-bottom: 10px;">
                        <div class="row">
                            <div class="col-6">
                                <small class="text-muted">EMPLOYEE COPY</small>
                                <h5><t t-out="o.employee_id.name"/></h5>
                                <t t-if="o.employee_id.home_street"><small><t t-out="o.employee_id.home_street"/></small><br/></t>
                                <t t-if="o.employee_id.home_city">
                                    <small><t t-out="o.employee_id.home_city"/>, <t t-out="o.employee_id.home_province or ''"/> <t t-out="o.employee_id.home_postal_code or ''"/></small>
                                </t>
                            </div>
                            <div class="col-6 text-end">
                                <table class="table table-sm table-borderless" style="font-size: 0.8em;">
                                    <tr><td>Pay Period:</td><td><t t-out="o.date_from"/> - <t t-out="o.date_to"/></td></tr>
                                    <tr><td>Pay Date:</td><td><t t-out="o.date_to"/></td></tr>
                                </table>
                            </div>
                        </div>
                        <div class="row" style="font-size: 0.75em;">
                            <div class="col-4">
                                <strong>PAY</strong>
                                <table class="table table-sm">
                                    <t t-foreach="o.line_ids.filtered(lambda l: l.category_id.code in ['BASIC', 'ALW'] and l.total != 0)" t-as="line">
                                        <tr><td><t t-out="line.name"/></td><td class="text-end"><t t-out="line.total" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    </t>
                                </table>
                            </div>
                            <div class="col-4">
                                <strong>TAXES</strong>
                                <table class="table table-sm">
                                    <tr><td>Income Tax</td><td class="text-end"><t t-out="o.employee_income_tax" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>EI</td><td class="text-end"><t t-out="o.employee_ei" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>CPP</td><td class="text-end"><t t-out="o.employee_cpp" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                </table>
                            </div>
                            <div class="col-4">
                                <strong>SUMMARY</strong>
                                <table class="table table-sm">
                                    <tr><td>Total Pay</td><td class="text-end"><t t-out="o.gross_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>Taxes</td><td class="text-end"><t t-out="o.total_employee_deductions" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr class="fw-bold"><td>NET PAY</td><td class="text-end"><t t-out="o.net_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- MIDDLE - Cheque -->
                    <div style="height: 35%; border-bottom: 1px dashed #ccc; padding: 20px 0;">
                        <div class="row">
                            <div class="col-8">
                                <strong><t t-out="o.company_id.name"/></strong><br/>
                                <small>
                                    <t t-if="o.company_id.street"><t t-out="o.company_id.street"/><br/></t>
                                    <t t-if="o.company_id.city"><t t-out="o.company_id.city"/>, <t t-out="o.company_id.state_id.code or ''"/> <t t-out="o.company_id.zip or ''"/></t>
                                </small>
                            </div>
                            <div class="col-4 text-end">
                                <small class="text-muted">DATE</small>
                                <h5><t t-out="o.date_to" t-options="{'widget': 'date'}"/></h5>
                            </div>
                        </div>
                        <div class="row mt-4">
                            <div class="col-12">
                                <p class="mb-1">Pay to the order of:</p>
                                <h4 class="border-bottom pb-2"><t t-out="o.employee_id.name"/></h4>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-8">
                                <p><strong>**<t t-out="o.net_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/>**</strong></p>
                            </div>
                            <div class="col-4 text-end">
                                <small class="text-muted">Cheque #</small><br/>
                                <strong><t t-out="o.cheque_number or '______'"/></strong>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-12">
                                <small class="text-muted">MEMO: Pay Period <t t-out="o.date_from"/> - <t t-out="o.date_to"/></small>
                            </div>
                        </div>
                    </div>

                    <!-- BOTTOM STUB - Employer Copy -->
                    <div style="height: 30%; padding-top: 10px;">
                        <div class="row">
                            <div class="col-6">
                                <small class="text-muted">EMPLOYER COPY</small>
                                <h5><t t-out="o.employee_id.name"/></h5>
                            </div>
                            <div class="col-6 text-end">
                                <table class="table table-sm table-borderless" style="font-size: 0.8em;">
                                    <tr><td>Pay Period:</td><td><t t-out="o.date_from"/> - <t t-out="o.date_to"/></td></tr>
                                    <tr><td>Pay Date:</td><td><t t-out="o.date_to"/></td></tr>
                                </table>
                            </div>
                        </div>
                        <div class="row" style="font-size: 0.75em;">
                            <div class="col-4">
                                <strong>PAY</strong>
                                <table class="table table-sm">
                                    <t t-foreach="o.line_ids.filtered(lambda l: l.category_id.code in ['BASIC', 'ALW'] and l.total != 0)" t-as="line">
                                        <tr><td><t t-out="line.name"/></td><td class="text-end"><t t-out="line.total" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    </t>
                                </table>
                            </div>
                            <div class="col-4">
                                <strong>TAXES</strong>
                                <table class="table table-sm">
                                    <tr><td>Income Tax</td><td class="text-end"><t t-out="o.employee_income_tax" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>EI</td><td class="text-end"><t t-out="o.employee_ei" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>CPP</td><td class="text-end"><t t-out="o.employee_cpp" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                </table>
                            </div>
                            <div class="col-4">
                                <strong>SUMMARY</strong>
                                <table class="table table-sm">
                                    <tr><td>Total Pay</td><td class="text-end"><t t-out="o.gross_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr><td>Taxes</td><td class="text-end"><t t-out="o.total_employee_deductions" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                    <tr class="fw-bold"><td>NET PAY</td><td class="text-end"><t t-out="o.net_wage" t-options="{'widget': 'monetary', 'display_currency': o.currency_id}"/></td></tr>
                                </table>
                            </div>
                        </div>
                    </div>

                </div>
            </t>
        </t>
    </template>

</odoo>
