<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <mapped-superclass name="Sylius\Component\Core\Model\Order" table="sylius_order">
        <field name="currencyCode" column="currency_code" length="3" />

        <field type="string" name="localeCode" column="locale_code" />
        <field type="string" name="checkoutState" column="checkout_state" />
        <field type="string" name="paymentState" column="payment_state" />
        <field type="string" name="shippingState" column="shipping_state" />
        <field type="boolean" name="createdByGuest" column="created_by_guest" >
            <options>
                <option name="default">1</option>
            </options>
        </field>

        <many-to-one field="channel" target-entity="Sylius\Component\Channel\Model\ChannelInterface">
            <join-column name="channel_id" referenced-column-name="id" nullable="true" />
        </many-to-one>

        <one-to-one field="shippingAddress" target-entity="Sylius\Component\Addressing\Model\AddressInterface">
            <cascade>
                <cascade-persist/>
                <cascade-remove/>
            </cascade>
            <join-column name="shipping_address_id" referenced-column-name="id" nullable="true" />
        </one-to-one>

        <one-to-one field="billingAddress" target-entity="Sylius\Component\Addressing\Model\AddressInterface">
            <cascade>
                <cascade-persist/>
                <cascade-remove/>
            </cascade>
            <join-column name="billing_address_id" referenced-column-name="id" nullable="true" />
        </one-to-one>

        <many-to-one field="promotionCoupon" target-entity="Sylius\Component\Promotion\Model\PromotionCouponInterface">
            <join-column name="promotion_coupon_id" />
            <cascade>
                <cascade-persist />
            </cascade>
        </many-to-one>

        <one-to-many field="payments" target-entity="Sylius\Component\Payment\Model\PaymentInterface" mapped-by="order" orphan-removal="true">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-many>

        <one-to-many field="shipments" target-entity="Sylius\Component\Shipping\Model\ShipmentInterface" mapped-by="order" orphan-removal="true">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <cascade>
                <cascade-all/>
            </cascade>
        </one-to-many>

        <many-to-one field="customer" target-entity="Sylius\Component\Customer\Model\CustomerInterface" inversed-by="orders">
            <join-column name="customer_id" referenced-column-name="id" nullable="true" />
            <cascade>
                <cascade-persist />
            </cascade>
        </many-to-one>

        <many-to-many field="promotions" target-entity="Sylius\Component\Promotion\Model\PromotionInterface">
            <order-by>
                <order-by-field name="id" />
            </order-by>

            <join-table name="sylius_promotion_order">
                <join-columns>
                    <join-column name="order_id" referenced-column-name="id" on-delete="CASCADE" />
                </join-columns>
                <inverse-join-columns>
                    <join-column name="promotion_id" referenced-column-name="id" />
                </inverse-join-columns>
            </join-table>
            <cascade>
                <cascade-persist />
            </cascade>
        </many-to-many>

        <field type="string" name="tokenValue" column="token_value" nullable="true" unique="true" />
        <field type="string" name="customerIp" column="customer_ip" nullable="true" />
    </mapped-superclass>

</doctrine-mapping>
