<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="view_order_batch_tree" model="ir.ui.view">
        <field name="name">sale.order.batch.tree</field>
        <field name="model">sale.order.batch</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <field name="date_order"/>
                <field name="company_id" groups="base.group_multi_company" optional="show" readonly="1"/>
                <field name="company_id" groups="!base.group_multi_company" invisible="1"/>
                <field name="sale_order_ids" widget="many2many_tags"/>
                <field
                    name="invoice_status"
                    decoration-success="invoice_status == 'invoiced'"
                    decoration-info="invoice_status == 'to invoice'"
                    decoration-warning="invoice_status == 'upselling'"
                    widget="badge"
                    optional="show"
                />
                <field name="state"/>
            </tree>
        </field>
    </record>

    <record id="view_order_batch_form" model="ir.ui.view">
        <field name="name">sale.order.batch.form</field>
        <field name="model">sale.order.batch</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button
                        name="action_in_progress"
                        id="action_in_progress"
                        data-hotkey="g"
                        string="In Progress"
                        class="btn-primary"
                        type="object"
                        attrs="{'invisible': [('state', 'not in',['open'])]}"
                    />
                    <button
                        name="action_confirm"
                        id="action_confirm"
                        data-hotkey="v"
                        string="Confirm"
                        class="btn-secondary"
                        type="object"
                        attrs="{'invisible': [('state', 'not in',['open'])]}"
                    />
                    <button
                        name="action_confirm"
                        id="action_confirm"
                        data-hotkey="v"
                        string="Confirm"
                        class="btn-primary"
                        type="object"
                        attrs="{'invisible': [('state', 'not in',['in_progress'])]}"
                    />
                    <button
                        id="create_invoices"
                        name="%(sale.action_view_sale_advance_payment_inv)d"
                        string="Create Invoices"
                        type="action"
                        class="btn-primary"
                        context="{
                        'default_sale_order_ids': sale_order_ids,
                        }"
                        data-hotkey="q"
                        attrs="{'invisible': [('state', 'not in','closed')]}"
                    />
                    <button
                        name="action_cancel"
                        id="action_cancel"
                        data-hotkey="z"
                        string="Cancel"
                        class="btn-secondary"
                        type="object"
                        attrs="{'invisible': [('state', 'not in',['open', 'in_progress','closed'])]}"
                    />
                    <field name="state" widget="statusbar"/>
                </header>
                <div
                    class="alert alert-warning mb-0"
                    role="alert"
                    attrs="{'invisible': [('partner_credit_warning', '=', '')]}"
                >
                    <field name="partner_credit_warning"/>
                </div>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button
                            class="oe_stat_button"
                            type="object"
                            name="action_view_source_sale_orders"
                            icon="fa-pencil-square-o"
                            attrs="{'invisible': [('sale_order_count', '=', 0)]}"
                        >
                            <field string="Sale Orders" name="sale_order_count" widget="statinfo"/>
                        </button>
                        <button
                            class="oe_stat_button"
                            type="object"
                            name="action_view_products"
                            icon="fa-cart-plus"
                            attrs="{'invisible': [('product_count', '=', 0)]}"
                        >
                            <field string="Products" name="product_count" widget="statinfo"/>
                        </button>
                        <button
                            name="action_view_invoice"
                            type="object"
                            class="oe_stat_button"
                            icon="fa-pencil-square-o"
                            attrs="{'invisible': [('invoice_count', '=', 0)]}"
                        >
                            <field name="invoice_count" widget="statinfo" string="Invoices"/>
                        </button>
                    </div>
                    <h1>
                        <field name="name"/>
                    </h1>
                    <group>
                        <group>
                           <field name="amount_total" colspan="2" readonly="1"/>
                        </group>
                        <group>
                            <field name="date_order"/>
                            <field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
                            <field name="company_id" invisible="1" groups="!base.group_multi_company"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Sale Orders" name="orders">
                            <field name="sale_order_ids">
                                <tree delete="0" editable="0" create="0">
                                    <field name="name"/>
                                    <field name="partner_id" readonly="1"/>
                                    <field name="date_order" readonly="1"/>
                                    <field name="amount_untaxed"/>
                                    <field name="amount_total"/>
                                    <field name="company_id" invisible="1"/>
                                    <field
                                        name="invoice_status"
                                        decoration-success="invoice_status == 'invoiced'"
                                        decoration-info="invoice_status == 'to invoice'"
                                        decoration-warning="invoice_status == 'upselling'"
                                        widget="badge"
                                        optional="show"
                                    />
                                </tree>
                            </field>
                        </page>
                        <page string="Sale Order Lines" name="order_line">
                            <field name="sale_order_line_ids" widget="section_and_note_one2many">
                                <tree create="0" editable="bottom">
                                    <field name="sequence" widget="handle"/>
                                    <field name="order_id"/>
                                    <field name="order_partner_id"/>
                                    <field name="product_template_id" optional="hide"/>
                                    <field name="product_id" optional="hide"/>
                                    <field name="name"/>
                                    <field name="product_uom_qty"/>
                                    <field name="company_id" invisible="1"/>
                                    <field name="price_unit"/>
                                    <field name="price_subtotal"/>
                                </tree>
                            </field>
                        </page>
                        <page string="Products" name="products">
                            <field name="product_ids">
                                <tree create="0" delete="0">
                                    <field name="product_template_id" optional="hide"/>
                                    <field name="product_id"/>
                                    <field name="product_uom_qty"/>
                                    <field name="product_packaging_qty" groups="product.group_stock_packaging"/>
                                    <field name="product_packaging_id" groups="product.group_stock_packaging"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </sheet>
                <div class="oe_chatter">
                    <field name="message_follower_ids" widget="mail_followers"/>
                    <field name="message_ids" widget="mail_thread"/>
                </div>
            </form>
        </field>
    </record>

       <record id="sale_order_batch_view_search" model="ir.ui.view">
        <field name="name">sale.order.batch.view.search</field>
        <field name="model">sale.order.batch</field>
        <field name="arch" type="xml">
            <search>
                <filter string="Active" name="filter_batch_open" domain="[('state', 'in', ['open', 'in_progress'])]"/>
                <filter string="Finished" name="filter_batch_finished" domain="[('state', '=', 'closed')]"/>
                <filter string="Cancelled" name="filter_batch_cancel" domain="[('state', '=', 'cancel')]"/>
            </search>
        </field>
    </record>

    <record id="action_quotation_batch" model="ir.actions.act_window">
        <field name="name">Quotation Batches</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order.batch</field>
        <field name="view_mode">tree,form,activity</field>
        <field name="context">{'search_default_filter_batch_open': 1}</field>
    </record>

    <record id="action_order_batch" model="ir.actions.act_window">
        <field name="name">Order Batches</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order.batch</field>
        <field name="view_mode">tree,form,activity</field>
        <field name="context">{'search_default_filter_batch_finished': 1}</field>
    </record>

    <record id="action_batch_to_invoice" model="ir.actions.act_window">
        <field name="name">Batches to Invoice</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">sale.order.batch</field>
        <field name="view_mode">tree,form,activity</field>
        <field name="context">{'create': False}</field>
        <field name="domain">[('invoice_status','=','to invoice')]</field>
        <field name="search_view_id" ref="sale_order_batch_view_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
            No batches to invoice found
            </p>
        </field>
    </record>
</odoo>
