<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="event_sale_report_view_graph" model="ir.ui.view">
        <field name="name">event.sale.report.view.graph</field>
        <field name="model">event.sale.report</field>
        <field name="arch" type="xml">
            <graph string="Revenues" sample="1" type="line">
                <field name="sale_price" type="measure"/>
                <field name="event_registration_create_date" interval="day"/>
                <field name="event_ticket_price" type="measure" invisible="True"/>
            </graph>
        </field>
    </record>

    <record id="event_sale_report_view_form" model="ir.ui.view">
        <field name="name">event.sale.report.view.form</field>
        <field name="model">event.sale.report</field>
        <field name="arch" type="xml">
            <form string="Registration revenues" edit="false" create="false">
                <sheet>
                    <group col="2">
                        <group string="Event">
                            <field name="event_type_id"/>
                            <field name="event_id"/>
                            <field name="event_date_begin"/>
                        </group>
                        <group string="Registration">
                            <field name="event_registration_id"/>
                            <field name="event_registration_name"/>
                            <field name="event_registration_create_date"/>
                            <field name="event_ticket_id"/>
                            <field name="event_registration_state"/>
                        </group>
                    </group>
                    <group col="2">
                        <group string="Sale Order">
                            <field name="sale_order_partner_id"/>
                            <field name="sale_order_id"/>
                            <field name="product_id"/>
                        </group>
                        <group string="Revenues">
                            <field name="event_ticket_price"/>
                            <field name="sale_price_untaxed"/>
                            <field name="sale_price"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="event_sale_report_view_pivot" model="ir.ui.view">
        <field name="name">event.sale.report.view.pivot</field>
        <field name="model">event.sale.report</field>
        <field name="arch" type="xml">
            <pivot string="Revenues" sample="1">
                <field name="sale_price_untaxed" type="measure"/>
                <field name="sale_price" type="measure"/>
                <field name="event_id" type="row"/>
                <field name="product_id" type="row"/>
                <field name="event_ticket_price" invisible="True"/>
            </pivot>
        </field>
    </record>

    <record id="event_sale_report_view_tree" model="ir.ui.view">
        <field name="name">event.sale.report.view.tree</field>
        <field name="model">event.sale.report</field>
        <field name="arch" type="xml">
            <tree string="Revenues" edit="false" create="false">
                <field name="event_id"/>
                <field name="event_ticket_id"/>
                <field name="product_id" optional="hide"/>
                <field name="event_ticket_price"/>
                <field name="sale_price_untaxed" optional="hide"/>
                <field name="sale_price" optional="hide"/>
                <field name="event_registration_state" optional="hide"/>
                <field name="sale_order_partner_id"/>
                <field name="invoice_partner_id" optional="hide"/>
                <field name="event_registration_name" optional="hide"/>
                <field name="sale_order_state" widget="badge"
                       decoration-success="sale_order_state == 'sale' or sale_order_state == 'done'"
                       decoration-info="sale_order_state == 'draft' or sale_order_state == 'sent'"/>
            </tree>
        </field>
    </record>

    <record id="event_sale_report_view_search" model="ir.ui.view">
        <field name="name">event.sale.report.view.search</field>
        <field name="model">event.sale.report</field>
        <field name="arch" type="xml">
            <search string="Event Sales Analysis">
                <field name="event_id"/>
                <field name="event_registration_name" string="Participant"/>
                <field name="sale_order_partner_id" string="Booked by"/>
                <field name="company_id"/>
                <filter string="Non-free tickets" name="priced_tickets" domain="[('event_ticket_price', '!=', 0)]"/>
                <separator/>
                <filter string="Free" name="free" domain="[('payment_status', '=', 'free')]"/>
                <filter string="Pending payment" name="payment_pending" domain="[('payment_status', '=', 'to_pay')]"/>
                <filter string="Paid" name="is_paid" domain="[('payment_status', '=', 'paid')]"/>
                <separator/>
                <filter string="Registration Date" name="event_registration_create_date" date="event_registration_create_date" default_period="this_year"/>
                <separator/>
                <filter string="Upcoming/Running" name="upcoming" help="Upcoming events from today"
                        domain="[('event_date_end', '&gt;=', datetime.datetime.combine(context_today(), datetime.time(0,0,0)))]"/>
                <filter string="Past Events" name="past" help="Events that have ended"
                        domain="[('event_date_end', '&lt;', datetime.datetime.combine(context_today(), datetime.time(0,0,0)))]"/>
                <filter string="Event Start Date" name="event_date_start" date="event_date_begin" default_period="this_year"/>
                <filter string="Event End Date" name="event_date_end" date="event_date_end"/>
                <group expand="0" string="Group By">
                    <filter string="Event Type" name="group_by_event_type_id" context="{'group_by': 'event_type_id' }"/>
                    <filter string="Event" name="group_by_event_id" context="{'group_by': 'event_id' }"/>
                    <separator/>
                    <filter string="Product" name="group_by_product_id" context="{'group_by': 'product_id'}"/>
                    <filter string="Ticket" name="group_by_ticket_id" context="{'group_by': 'event_ticket_id'}"/>
                    <separator/>
                    <filter string="Registration Status" name="group_by_registration_state"
                            context="{'group_by': 'event_registration_state'}"/>
                    <filter string="Sale Order Status" name="group_by_sale_order_state"
                            context="{'group_by': 'sale_order_state'}"/>
                    <filter string="Customer" name="group_by_customer" context="{'group_by': 'sale_order_partner_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <record id="event_sale_report_action" model="ir.actions.act_window">
        <field name="name">Revenues</field>
        <field name="res_model">event.sale.report</field>
        <field name="view_mode">graph,pivot</field>
        <field name="context">{
            'search_default_priced_tickets': 1,
            'search_default_event_date_start': 1,
            'pivot_measures': ['__count__', 'sale_price_untaxed', 'sale_price'],
        }</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No Event Revenues yet!
            </p><p>
                Come back once tickets have been sold to overview your sales income.
            </p>
        </field>
    </record>

    <menuitem name="Revenues"
        id="menu_action_show_revenues"
        action="event_sale_report_action"
        sequence="5"
        parent="event.menu_reporting_events"
        groups="event.group_event_user"/>
</odoo>
