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

	<record id="sale_order_to_purchase_order_view_form" model="ir.ui.view">
		<field name="name">sale.order.to.purchase.order.form</field>
		<field name="model">sale.order</field>
		<field name="inherit_id" ref="sale.view_order_form"/>
		<field name="arch" type="xml">
			<xpath expr="//form//header//button[@name='action_quotation_send']" position="after">
				<button name="%(purchase_order_wizard_action)d" class="btn-primary" type="action" string="Create PO"/>
			</xpath>
			<xpath expr="//button[@name='action_view_invoice']" position="before">
				<button type="object" name="action_view_custom_purchase" class="oe_stat_button" invisible ="custom_purchase_count == 0" icon="fa-file">
					<field name="custom_purchase_count" widget="statinfo" string="Purchase"/>
				</button>
			</xpath>
		</field>
	</record>

	<record id="purchase_order_sale_ord_view_form" model="ir.ui.view">
		<field name="name">purchase.order.sale.ord.form</field>
		<field name="model">purchase.order</field>
		<field name="inherit_id" ref="purchase.purchase_order_form"/>
		<field name="arch" type="xml">
			<!-- Add Match SO button in header -->
			<xpath expr="//header" position="inside">
				<button name="action_match_sale_order" type="object" string="Match Sale Order" 
					class="btn-secondary" invisible="sale_ord_id"
					confirm="This will try to match this PO to a Sale Order based on the 'Marked For' field. Continue?"/>
			</xpath>
			<!-- Add stat button for linked Sale Order -->
			<xpath expr="//div[@name='button_box']" position="inside">
				<button type="object" name="action_view_linked_sale_order" class="oe_stat_button" 
					invisible="custom_sale_count == 0" icon="fa-shopping-cart">
					<field name="custom_sale_count" widget="statinfo" string="Sale Order"/>
				</button>
			</xpath>
			<!-- Add Sale Order and Marked For fields -->
			<xpath expr="//field[@name='date_order']" position="before">
				<field name="sale_ord_id" string="Sale Order"/>
				<field name="marked_for" string="Marked For"/>
			</xpath>
		</field>
	</record>

	</data>
</odoo>
