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

    <!-- Search View -->
    <record id="view_fusion_cp_shipment_search" model="ir.ui.view">
        <field name="name">fusion.cp.shipment.search</field>
        <field name="model">fusion.cp.shipment</field>
        <field name="arch" type="xml">
            <search string="Search Shipments">
                <field name="name"/>
                <field name="tracking_number"/>
                <field name="shipment_id"/>
                <field name="sale_order_id"/>
                <field name="picking_id"/>
                <field name="recipient_name"/>
                <filter name="filter_confirmed" string="Confirmed"
                        domain="[('status','=','confirmed')]"/>
                <filter name="filter_shipped" string="Shipped"
                        domain="[('status','=','shipped')]"/>
                <filter name="filter_delivered" string="Delivered"
                        domain="[('status','=','delivered')]"/>
                <filter name="filter_returned" string="Returned"
                        domain="[('status','=','returned')]"/>
                <filter name="filter_cancelled" string="Cancelled"
                        domain="[('status','=','cancelled')]"/>
                <group>
                    <filter name="group_status" string="Status"
                            context="{'group_by': 'status'}"/>
                    <filter name="group_service" string="Service Type"
                            context="{'group_by': 'service_type'}"/>
                    <filter name="group_date" string="Shipment Date"
                            context="{'group_by': 'shipment_date:month'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- List View -->
    <record id="view_fusion_cp_shipment_list" model="ir.ui.view">
        <field name="name">fusion.cp.shipment.list</field>
        <field name="model">fusion.cp.shipment</field>
        <field name="arch" type="xml">
            <list string="Canada Post Shipments" default_order="shipment_date desc">
                <field name="name" decoration-bf="1"/>
                <field name="tracking_number"/>
                <field name="recipient_name"/>
                <field name="sale_order_id" optional="show"/>
                <field name="picking_id" optional="show"/>
                <field name="service_type"/>
                <field name="currency_id" column_invisible="1"/>
                <field name="shipping_cost" sum="Total Cost"/>
                <field name="weight" optional="hide"/>
                <field name="package_name" optional="hide"/>
                <field name="shipment_date"/>
                <field name="status"
                       decoration-info="status == 'draft'"
                       decoration-success="status in ('confirmed','shipped')"
                       decoration-bf="status == 'delivered'"
                       decoration-warning="status == 'returned'"
                       decoration-danger="status == 'cancelled'"
                       widget="badge"/>
            </list>
        </field>
    </record>

    <!-- Form View -->
    <record id="view_fusion_cp_shipment_form" model="ir.ui.view">
        <field name="name">fusion.cp.shipment.form</field>
        <field name="model">fusion.cp.shipment</field>
        <field name="arch" type="xml">
            <form string="Canada Post Shipment">
                <header>
                    <button name="action_refresh_tracking" type="object"
                            string="Refresh Tracking"
                            class="btn-primary"
                            icon="fa-refresh"
                            invisible="not tracking_number or status == 'cancelled'"/>
                    <button name="action_track_on_canada_post" type="object"
                            string="Track on Canada Post"
                            class="btn-secondary"
                            icon="fa-external-link"
                            invisible="not tracking_number"/>
                    <button name="action_create_return_label" type="object"
                            string="Create Return Label"
                            class="btn-warning"
                            icon="fa-mail-reply"
                            invisible="status not in ('delivered', 'shipped') or return_tracking_number"
                            confirm="Return labels are a paid service (bill-on-scan). The cost will be charged to your Canada Post account when the customer uses the label. Continue?"/>
                    <button name="action_void_shipment" type="object"
                            string="Void Shipment"
                            class="btn-danger"
                            icon="fa-ban"
                            invisible="status in ('cancelled', 'delivered', 'returned')"
                            confirm="Are you sure you want to void this shipment? This cannot be undone."/>
                    <button name="action_reissue_shipment" type="object"
                            string="Reissue Shipment"
                            class="btn-warning"
                            icon="fa-repeat"
                            invisible="status != 'cancelled'"/>
                    <field name="status" widget="statusbar"
                           statusbar_visible="confirmed,shipped,delivered"/>
                </header>
                <sheet>
                    <div name="button_box" class="oe_button_box">
                        <button name="action_open_sale_order" type="object"
                                class="oe_stat_button" icon="fa-shopping-cart"
                                invisible="not sale_order_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Sale Order</span>
                            </div>
                        </button>
                        <button name="action_open_picking" type="object"
                                class="oe_stat_button" icon="fa-truck"
                                invisible="not picking_id">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_text">Transfer</span>
                            </div>
                        </button>
                        <button name="action_refresh_tracking" type="object"
                                class="oe_stat_button" icon="fa-map-marker"
                                invisible="not tracking_number">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="tracking_event_count"/>
                                </span>
                                <span class="o_stat_text">Events</span>
                            </div>
                        </button>
                    </div>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1><field name="name" readonly="1"/></h1>
                    </div>
                    <group>
                        <group string="Shipment Details">
                            <field name="tracking_number"/>
                            <field name="shipment_id"/>
                            <field name="carrier_id"/>
                            <field name="service_type"/>
                            <field name="shipment_date"/>
                            <field name="shipping_cost"/>
                            <field name="weight"/>
                            <field name="package_name"
                                   invisible="not package_name"/>
                            <field name="return_tracking_number"
                                   invisible="not return_tracking_number"/>
                        </group>
                        <group string="Links">
                            <field name="sale_order_id"/>
                            <field name="picking_id"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                    </group>
                    <group>
                        <group string="Sender">
                            <field name="sender_name"/>
                            <field name="sender_address"/>
                        </group>
                        <group string="Recipient">
                            <field name="recipient_name"/>
                            <field name="recipient_address"/>
                        </group>
                    </group>
                    <notebook>
                        <page string="Labels &amp; Documents" name="labels">
                            <field name="label_attachment_id" invisible="1"/>
                            <field name="full_label_attachment_id" invisible="1"/>
                            <field name="receipt_attachment_id" invisible="1"/>
                            <field name="commercial_invoice_attachment_id" invisible="1"/>
                            <field name="return_label_attachment_id" invisible="1"/>
                            <group string="Labels">
                                <span class="o_form_label" invisible="not label_attachment_id">Printable Label (4x6)</span>
                                <button name="action_view_label" type="object"
                                        class="btn btn-link p-0"
                                        icon="fa-file-pdf-o"
                                        string="Open"
                                        invisible="not label_attachment_id"/>
                                <span class="o_form_label" invisible="not full_label_attachment_id">Full Label (8.5x11)</span>
                                <button name="action_view_full_label" type="object"
                                        class="btn btn-link p-0"
                                        icon="fa-file-pdf-o"
                                        string="Open"
                                        invisible="not full_label_attachment_id"/>
                                <span class="o_form_label" invisible="not return_label_attachment_id">Return Label</span>
                                <button name="action_view_return_label" type="object"
                                        class="btn btn-link p-0"
                                        icon="fa-file-pdf-o"
                                        string="Open"
                                        invisible="not return_label_attachment_id"/>
                            </group>
                            <group string="Documents">
                                <span class="o_form_label" invisible="not receipt_attachment_id">Receipt</span>
                                <button name="action_view_receipt" type="object"
                                        class="btn btn-link p-0"
                                        icon="fa-file-pdf-o"
                                        string="Open"
                                        invisible="not receipt_attachment_id"/>
                                <span class="o_form_label" invisible="not commercial_invoice_attachment_id">Commercial Invoice</span>
                                <button name="action_view_commercial_invoice" type="object"
                                        class="btn btn-link p-0"
                                        icon="fa-file-pdf-o"
                                        string="Open"
                                        invisible="not commercial_invoice_attachment_id"/>
                            </group>
                        </page>
                        <page string="Tracking History" name="tracking_history">
                            <group>
                                <group>
                                    <field name="last_tracking_update"/>
                                </group>
                                <group>
                                    <field name="delivery_date"
                                           invisible="status not in ('delivered', 'returned')"/>
                                </group>
                            </group>
                            <field name="tracking_event_ids" readonly="1" nolabel="1">
                                <list string="Tracking Events"
                                      default_order="event_datetime desc">
                                    <field name="event_datetime" string="Date/Time"/>
                                    <field name="event_description"/>
                                    <field name="event_site" string="Location"/>
                                    <field name="event_province"/>
                                    <field name="signatory_name" optional="hide"/>
                                    <field name="event_type" optional="hide"/>
                                </list>
                            </field>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- Kanban View -->
    <record id="view_fusion_cp_shipment_kanban" model="ir.ui.view">
        <field name="name">fusion.cp.shipment.kanban</field>
        <field name="model">fusion.cp.shipment</field>
        <field name="arch" type="xml">
            <kanban default_group_by="status" class="o_kanban_small_column">
                <field name="name"/>
                <field name="tracking_number"/>
                <field name="status"/>
                <field name="shipping_cost"/>
                <field name="shipment_date"/>
                <field name="recipient_name"/>
                <field name="service_type"/>
                <templates>
                    <t t-name="card">
                        <div class="oe_kanban_details">
                            <strong><field name="name"/></strong>
                            <div><field name="recipient_name"/></div>
                            <div class="text-muted">
                                <i class="fa fa-barcode"/> <field name="tracking_number"/>
                            </div>
                            <div class="text-muted">
                                <field name="service_type"/> |
                                <field name="shipment_date" widget="date"/>
                            </div>
                            <div>
                                <strong>$<field name="shipping_cost"/></strong>
                            </div>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- Window Actions -->
    <record id="action_fusion_cp_shipment" model="ir.actions.act_window">
        <field name="name">All Shipments</field>
        <field name="res_model">fusion.cp.shipment</field>
        <field name="view_mode">list,form,kanban</field>
        <field name="search_view_id" ref="view_fusion_cp_shipment_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No shipments yet
            </p>
            <p>Shipments are created automatically when you validate a delivery
            order with a Canada Post carrier.</p>
        </field>
    </record>

    <record id="action_fusion_cp_shipment_confirmed" model="ir.actions.act_window">
        <field name="name">Confirmed</field>
        <field name="res_model">fusion.cp.shipment</field>
        <field name="view_mode">list,form,kanban</field>
        <field name="domain">[('status', '=', 'confirmed')]</field>
        <field name="search_view_id" ref="view_fusion_cp_shipment_search"/>
    </record>

    <record id="action_fusion_cp_shipment_shipped" model="ir.actions.act_window">
        <field name="name">Shipped</field>
        <field name="res_model">fusion.cp.shipment</field>
        <field name="view_mode">list,form,kanban</field>
        <field name="domain">[('status', '=', 'shipped')]</field>
        <field name="search_view_id" ref="view_fusion_cp_shipment_search"/>
    </record>

    <record id="action_fusion_cp_shipment_delivered" model="ir.actions.act_window">
        <field name="name">Delivered</field>
        <field name="res_model">fusion.cp.shipment</field>
        <field name="view_mode">list,form,kanban</field>
        <field name="domain">[('status', '=', 'delivered')]</field>
        <field name="search_view_id" ref="view_fusion_cp_shipment_search"/>
    </record>

    <record id="action_fusion_cp_shipment_returned" model="ir.actions.act_window">
        <field name="name">Returned</field>
        <field name="res_model">fusion.cp.shipment</field>
        <field name="view_mode">list,form,kanban</field>
        <field name="domain">[('status', '=', 'returned')]</field>
        <field name="search_view_id" ref="view_fusion_cp_shipment_search"/>
    </record>

</odoo>
