<odoo>

    <menuitem
        id="rental_menu_root"
        name="Rental"
        web_icon="odex25_sale_renting,static/description/icon.png"
        groups="sales_team.group_sale_salesman"
    />

    <menuitem id="menu_rental_config" name="Configuration" parent="rental_menu_root" sequence="10"/>

    <record id="rental_product_template_action" model="ir.actions.act_window">
      <field name="name">Products</field>
      <field name="res_model">product.template</field>
      <field name="view_mode">kanban,tree,form</field>
      <field name="domain"></field>
      <field name="context">{
        'default_sale_ok': False,
        'default_rent_ok': True,
        'default_type': 'consu',
        'default_purchase_ok': False,
        'search_default_filter_to_rent': True,
        }
      </field>
      <field name="help" type="html">
        <p class="o_view_nocontent_smiling_face">
          Create a new rental product!
        </p><p>
          You can also create additional products or services to sell by checking *Can be Sold* in the product form (e.g. insurance).
        </p>
      </field>
    </record>

    <menuitem id="menu_rental_products" name="Products" parent="rental_menu_root" sequence="4" action="rental_product_template_action"/>

    <record id="product_template_rental_kanban_view" model="ir.ui.view">
        <field name="name">product.template.product.kanban.inherit.rental</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_kanban_view"/>
        <field name="mode">primary</field>
        <field name="arch" type="xml">
            <field name="activity_state" position="after">
                <field name="rental_pricing_ids"/>
            </field>
            <field name="lst_price" position="replace">
                <t t-if="record.rental_pricing_ids.value">
                  <field name="display_price"/>
                </t>
                <t t-else="">
                    <field name="lst_price" widget="monetary" options="{'currency_field': 'currency_id', 'field_digits': True}"/>
                </t>
            </field>
        </field>
    </record>

    <record id="product_template_rental_tree_view" model="ir.ui.view">
        <field name="name">product.template.product.tree.inherit.rental</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_tree_view"/>
        <field name="mode">primary</field>
        <field name="arch" type="xml">
            <field name="list_price" position="after">
                <field name="display_price"/>
            </field>
            <field name="list_price" position="attributes">
                <attribute name="optional">hide</attribute>
            </field>
            <field name="standard_price" position="attributes">
                <attribute name="optional">hide</attribute>
            </field>
        </field>
    </record>

    <record id="rental_product_template_kanban" model="ir.actions.act_window.view">
        <field name="view_mode">kanban</field>
        <field name="view_id" ref="product_template_rental_kanban_view"/>
        <field name="act_window_id" ref="rental_product_template_action"/>
    </record>

    <record id="rental_product_template_tree" model="ir.actions.act_window.view">
        <field name="view_mode">tree</field>
        <field name="view_id" ref="product_template_rental_tree_view"/>
        <field name="act_window_id" ref="rental_product_template_action"/>
    </record>

    <!-- Rental orders access -->

    <record id="rental_order_primary_form_view" model="ir.ui.view">
        <field name="name">rental.order.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="odex25_sale_renting.rental_order_form_view"/>
        <field name="mode">primary</field>
        <field name="priority">999</field>
        <field name="arch" type="xml">
          <!-- options key for rental app knowledge in the rental configurator -->
          <xpath expr="//notebook/page[@name='order_lines']/field/tree/field[@name='product_id']" position='attributes'>
            <attribute name="context">{
                      'partner_id': parent.partner_id,
                      'quantity': product_uom_qty,
                      'pricelist': parent.pricelist_id,
                      'uom':product_uom,
                      'company_id': parent.company_id,
                      'default_lst_price': price_unit,
                      'default_description_sale': name,
                      'default_rent_ok': 1,
                      'rental_products': True,
                  }
            </attribute>
            <attribute name="domain">['|', ('sale_ok', '=', True), ('rent_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]</attribute>
            <attribute name="options">{
              'no_open': True,
              'rent': True
            }</attribute>
          </xpath>
          <xpath expr="//notebook/page[@name='order_lines']/field/tree/field[@name='product_template_id']" position='attributes'>
            <attribute name="context">{
                    'partner_id': parent.partner_id,
                    'quantity': product_uom_qty,
                    'pricelist': parent.pricelist_id,
                    'uom':product_uom,
                    'company_id': parent.company_id,
                    'default_list_price': price_unit,
                    'default_description_sale': name,
                    'default_rent_ok': 1,
                    'rental_products': True,
                }
            </attribute>
            <attribute name="domain">['|', ('sale_ok', '=', True), ('rent_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]</attribute>
            <attribute name="options">{
              'no_open': True,
              'rent': True
            }</attribute>
          </xpath>
          <xpath expr="//field[@name='order_line']//form//field[@name='product_id']" position='attributes'>
            <attribute name="domain">['|', ('sale_ok', '=', True), ('rent_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]</attribute>
          </xpath>
        </field>
      </record>

    <record id="rental_order_view_tree" model="ir.ui.view">
      <field name="name">rental.order.tree</field>
      <field name="model">sale.order</field>
      <field name="arch" type="xml">
        <tree multi_edit="1" sample="1">
          <field name="has_late_lines" invisible="1"/>
          <field name="name" readonly="1" decoration-bf="1"/>
          <field name="date_order" widget='date'/>
          <field name="next_action_date" widget='remaining_days' attrs="{'invisible': ['|', ('rental_status','=','pickup'), ('rental_status','=','returned')]}"/>
          <field name="partner_id" readonly="1"/>
          <field name="user_id" optional="show" widget='many2one_avatar_user'/>
          <field name="team_id" optional="hide"/>
          <field name="amount_total" sum="Total Tax Included" widget="monetary" decoration-bf="1"/>
          <field name="rental_status" widget='badge' decoration-info="rental_status == 'draft' or rental_status == 'sent'" decoration-warning="rental_status == 'pickup'" decoration-success="rental_status == 'return'"/>
          <field name="state" invisible="1"/>
          <field name="currency_id" invisible="1"/>
        </tree>
      </field>
    </record>

    <record id="rental_order_view_kanban" model="ir.ui.view">
      <field name="name">rental.order.kanban</field>
      <field name="model">sale.order</field>
      <field name="inherit_id" ref="sale.view_sale_order_kanban"/>
      <field name="mode">primary</field>
      <field name="priority">100</field>
      <field name="arch" type="xml">
        <xpath expr="//kanban" position="attributes">
          <attribute name="quick_create">false</attribute>
          <attribute name="class">o_kanban_mobile align-content-start</attribute>
        </xpath>
        <field name="state" position="after">
          <field name="next_action_date"/>
          <field name="has_late_lines"/>
        </field>
        <xpath expr="//div[hasclass('oe_kanban_bottom_left')]" position="replace">
          <div class="oe_kanban_bottom_left text-muted">
            <span>
              <t t-esc="record.name.value"/>
              <span t-if="record.next_action_date.value" t-attf-class="#{record.has_late_lines.raw_value and record.rental_status.raw_value=='return' ? 'text-danger' : 'text-muted' } ">
                <br/>
                <t t-if="record.has_late_lines.raw_value">
                  Late
                </t>
                <t t-if="record.rental_status.raw_value=='pickup'">
                  Pickup:
                </t>
                <t t-elif="record.rental_status.raw_value=='return'">
                  Return:
                </t>
              </span>
              <t t-esc="record.next_action_date.value"/>
            </span>
          </div>
        </xpath>
        <xpath expr="//div[hasclass('oe_kanban_bottom_right')]/field[@name='state']" position="attributes">
          <attribute name="attrs">
            {'invisible': [('state', '=', 'sale'), ('rental_status', '!=', False)]}
          </attribute>
        </xpath>
        <xpath expr="//div[hasclass('oe_kanban_bottom_right')]/field[@name='state']" position="after">
          <field name="rental_status" widget="label_selection"
            attrs="{'invisible': ['|', ('state', '!=', 'sale'), ('rental_status', '=', False)]}"
            options="{'classes': {'pickup': 'info', 'return': 'warning', 'returned': 'success'}}"/>
        </xpath>
      </field>
    </record>

    <record id="rental_order_view_search" model="ir.ui.view">
      <field name="name">rental.order.search</field>
      <field name="model">sale.order</field>
      <field name="priority">100</field>
      <field name="arch" type="xml">
        <search>
          <field name="name" string="Order" filter_domain="['|', '|', ('name', 'ilike', self), ('client_order_ref', 'ilike', self), ('partner_id', 'child_of', self)]"/>
          <field name="partner_id" operator="child_of"/>
          <field name="user_id"/>
          <field name="team_id" string="Sales Team"/>
          <field name="order_line" string="Product" filter_domain="[('order_line.product_id', 'ilike', self)]"/>
          <field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
          <filter string="My Orders" domain="[('user_id','=',uid)]" name="my_rental_orders"/>
          <separator/>
          <filter string="Rentals" domain="[('is_rental_order', '=', True)]" name="from_rental"/>
          <separator/>
            <filter name="filter_today" string="To Do Today"
              domain="[('next_action_date','&gt;=', datetime.datetime.combine(context_today(), datetime.time(0,0,0))),
                       ('next_action_date','&lt;=', datetime.datetime.combine(context_today(), datetime.time(23,59,59)))]"/>
            <filter string="Late" name="rental_late_orders"
              domain="[('rental_status', 'in', ['pickup', 'return']), ('next_action_date', '&lt;', (datetime.datetime.now() + relativedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S'))]"/>
          <separator/>
            <filter string="Pickup Date" name="filter_pickup" date="next_action_date" domain="[('rental_status', '=', 'pickup')]"/>
            <filter string="Return Date" name="filter_return" date="next_action_date" domain="[('rental_status', '=', 'return')]"/>
          <separator/>
          <group expand="0" string="Group By">
              <filter string="Status" name="groupby_status" context="{'group_by': 'rental_status'}"/>
              <separator/>
              <filter string="Salesperson" name="salesperson" domain="[]" context="{'group_by': 'user_id'}"/>
              <filter string="Customer" name="customer" domain="[]" context="{'group_by': 'partner_id'}"/>
          </group>
          <searchpanel>
              <field name="rental_status" icon="fa-retweet" enable_counters="1"/>
              <field name="invoice_status" icon="fa-retweet" enable_counters="1"/>
          </searchpanel>
        </search>
      </field>
    </record>

    <record id="rental_order_view_search_without_searchpanel" model="ir.ui.view">
      <field name="name">rental.order.search.bis</field>
      <field name="model">sale.order</field>
      <field name="inherit_id" ref="rental_order_view_search"/>
      <field name="mode">primary</field>
      <field name="arch" type="xml">
        <xpath expr="//filter[@name='rental_late_orders']" position="after">
            <separator/>
            <filter string="To Pickup" name="filter_to_pickup" domain="[('rental_status', '=', 'pickup')]"/>
            <filter string="To Return" name="filter_to_return" domain="[('rental_status', '=', 'return')]"/>
        </xpath>
      </field>
    </record>

    <record id="rental_order_action" model="ir.actions.act_window">
        <field name="name">Rental Orders</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order</field>
        <field name="view_mode">tree,kanban,form,calendar,pivot,graph,activity</field>
        <field name="search_view_id" ref="rental_order_view_search"/>
        <field name="context">{'default_is_rental_order': 1, 'search_default_from_rental': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create a new quotation, the first step of a new rental!
            </p><p>
                Once the quotation is confirmed, it becomes a rental order.<br/> You will be able to create an invoice and collect the payment.
            </p>
        </field>
    </record>
    <record id="rental_order_kanban" model="ir.actions.act_window.view">
      <field name="sequence" eval="1"/>
      <field name="view_mode">kanban</field>
      <field name="view_id" ref="rental_order_view_kanban"/>
      <field name="act_window_id" ref="rental_order_action"/>
    </record>
    <record id="rental_order_tree" model="ir.actions.act_window.view">
        <field name="sequence" eval="2"/>
        <field name="view_mode">tree</field>
        <field name="view_id" ref="rental_order_view_tree"/>
        <field name="act_window_id" ref="rental_order_action"/>
    </record>
    <record id="rental_order_form" model="ir.actions.act_window.view">
        <field name="sequence" eval="3"/>
        <field name="view_mode">form</field>
        <field name="view_id" ref="rental_order_primary_form_view"/>
        <field name="act_window_id" ref="rental_order_action"/>
    </record>

    <record id="rental_order_today_pickup_action" model="ir.actions.act_window">
        <field name="name">Rental Orders</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order</field>
        <field name="view_mode">tree,kanban,form,calendar,pivot,graph,activity</field>
        <field name="search_view_id" ref="rental_order_view_search_without_searchpanel"/>
        <field name="context">{'default_is_rental_order': 1, 'search_default_filter_today': 1, 'search_default_filter_to_pickup': 1}</field>
        <field name="domain">[('is_rental_order', '=', True)]</field>
        <field name="help" type="html">
          <p class="o_view_nocontent_smiling_face">
              There isn't any scheduled pickup or return.
          </p><p>
              You can search on a larger period using the filters here above
              <br/>
              or create a new rental order.
          </p>
        </field>
    </record>
    <record id="rental_order_today_pickup_kanban" model="ir.actions.act_window.view">
      <field name="sequence" eval="1"/>
      <field name="view_mode">kanban</field>
      <field name="view_id" ref="rental_order_view_kanban"/>
      <field name="act_window_id" ref="rental_order_today_pickup_action"/>
    </record>
    <record id="rental_order_today_pickup_tree" model="ir.actions.act_window.view">
        <field name="sequence" eval="2"/>
        <field name="view_mode">tree</field>
        <field name="view_id" ref="rental_order_view_tree"/>
        <field name="act_window_id" ref="rental_order_today_pickup_action"/>
    </record>
    <record id="rental_order_today_pickup_form" model="ir.actions.act_window.view">
        <field name="sequence" eval="3"/>
        <field name="view_mode">form</field>
        <field name="view_id" ref="rental_order_primary_form_view"/>
        <field name="act_window_id" ref="rental_order_today_pickup_action"/>
    </record>

    <record id="rental_order_today_return_action" model="ir.actions.act_window">
        <field name="name">Rental Orders</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order</field>
        <field name="view_mode">tree,kanban,form,calendar,pivot,graph,activity</field>
        <field name="search_view_id" ref="rental_order_view_search_without_searchpanel"/>
        <field name="context">{'default_is_rental_order': 1, 'search_default_filter_today': 1, 'search_default_filter_to_return': 1}</field>
        <field name="domain">[('is_rental_order', '=', True)]</field>
        <field name="help" type="html">
          <p class="o_view_nocontent_smiling_face">
              There isn't any scheduled pickup or return.
          </p><p>
              You can search on a larger period using the filters here above
              <br/>
              or create a new rental order.
          </p>
        </field>
    </record>
    <record id="rental_order_today_return_kanban" model="ir.actions.act_window.view">
      <field name="sequence" eval="1"/>
      <field name="view_mode">kanban</field>
      <field name="view_id" ref="rental_order_view_kanban"/>
      <field name="act_window_id" ref="rental_order_today_return_action"/>
    </record>
    <record id="rental_order_today_return_tree" model="ir.actions.act_window.view">
        <field name="sequence" eval="2"/>
        <field name="view_mode">tree</field>
        <field name="view_id" ref="rental_order_view_tree"/>
        <field name="act_window_id" ref="rental_order_today_return_action"/>
    </record>
    <record id="rental_order_today_return_form" model="ir.actions.act_window.view">
        <field name="sequence" eval="3"/>
        <field name="view_mode">form</field>
        <field name="view_id" ref="rental_order_primary_form_view"/>
        <field name="act_window_id" ref="rental_order_today_return_action"/>
    </record>

    <menuitem id="rental_order_menu" name="Orders" parent="rental_menu_root" sequence="2"/>
    <menuitem id="rental_orders_all" name="Orders" parent="rental_order_menu" sequence="1" action="rental_order_action"/>
    <menuitem id="menu_orders_customers" name="Customers"
        action="account.res_partner_action_customer"
        parent="rental_order_menu" sequence="2"/>
    <menuitem id="rental_orders_today" name="To Do Today" parent="rental_order_menu" sequence="3"/>
    <menuitem id="rental_orders_pickup" name="Pickup" parent="rental_orders_today" action="rental_order_today_pickup_action" sequence="1"/>
    <menuitem id="rental_orders_return" name="Return" parent="rental_orders_today" action="rental_order_today_return_action" sequence="2"/>

</odoo>
