{%- import "macros.jinja2" as macros %}
<Orders>
    {%- if user_info and account_maintenance %}
    <AccountMaintenance>
        <AccountDetail>
            {{ macros.emit_user_info(user_info) }}
        </AccountDetail>
    </AccountMaintenance>
    {%- endif %}
    <NewOrder>
        {%- if has_account_number and default_payment_type %}
        <DefaultPaymentType>{{ default_payment_type }}</DefaultPaymentType>
        {%- endif %}
        <Function>{{ function_value }}</Function>
        <ErrorLevel>2</ErrorLevel>
        <ReferenceNumber>{{ reference_number }}</ReferenceNumber>
        <Check>
            <CheckHeader>
                <TableNumber>{{ table_number }}</TableNumber>
                <CostCenter>{{ revenue_center }}</CostCenter>
                <ServerNumber>{{ server_number }}</ServerNumber>
                <NumberInParty>{{ number_in_party }}</NumberInParty>
                {%- if terminal_number %}
                <TerminalNumber>{{ terminal_number }}</TerminalNumber>
                {%- endif %}
                {%- if menu %}
                {#- Increment the menu number prior to sending in XML.  According to POSitouch documentation, #}
                {#- "Menu numbers submitted should be increased by 1.  For instance, if a site wants menu #1 #}
                {#- to be applied, the Menu tag must have the value 2." #}
                <Menu>{{ menu|int + 1 }}</Menu>
                {%- endif %}
                {%- if user_info %}
                <Account>
                    {{ macros.emit_user_info(user_info) }}
                </Account>
                {%- endif %}
                {%- if ready_time %}
                <AdvanceOrderDate>{{ ready_date }}</AdvanceOrderDate>
                <AdvanceOrderTime>{{ ready_time }}</AdvanceOrderTime>
                {%- if prep_time_minutes %}
                <AdvanceOrderOffset>{{ prep_time_minutes }}</AdvanceOrderOffset>
                {%- endif %}
                {%- endif %}
            </CheckHeader>
            {%- if ticket_name and ticket_name_item and ticket_name_modifier %}
            <ItemDetail>
                {{ macros.id_node(ticket_name_item) }}
                <Quantity>1</Quantity>
                <ItemName>NAME</ItemName>
            </ItemDetail>
            <ItemDetail>
                {{ macros.id_node(ticket_name_modifier) }}
                <Quantity>1</Quantity>
                <ItemName>NAME</ItemName>
                <Memo>{{ ticket_name|e }}</Memo>
            </ItemDetail>
            {%- endif %}
            {%- for item in items %}
            <ItemDetail>
                {{ macros.id_node(item) }}
                <Quantity>{{ item.quantity or 1 }}</Quantity>
                {%- if item.price %}
                <Price>{{ item.price }}</Price>
                {%- endif %}
            </ItemDetail>
            {%- for modifier in item.modifiers %}
                <ItemDetail>
                    {{ macros.id_node(modifier) }}
                    <Quantity>{{ modifier.quantity }}</Quantity>
                    {%- if modifier.item_comment %}
                    <Memo>{{ modifier.item_comment|e }}</Memo>
                    {%- endif %}
                </ItemDetail>
            {%- endfor %}
            {%- for discount in item.discounts %}
                {%- if discount.is_true_open_discount %}
                    {%- if discount.type == 'dollar' %}
                    <OpenDollarDiscItem>
                        {{ macros.id_node(discount) }}
                        <OpenDollarDiscAmt>{{ discount.value }}</OpenDollarDiscAmt>
                    </OpenDollarDiscItem>
                    {%- else %}
                    <OpenPercentDiscItem>
                        {{ macros.id_node(discount) }}
                        <OpenPercentDiscPct>{{ discount.value }}</OpenPercentDiscPct>
                    </OpenPercentDiscItem>
                    {%- endif %}
                {%- else %}
                <ItemDetail>
                    {{ macros.id_node(discount) }}
                    {%- if discount.value %}
                    <Price>{{ discount.value }}</Price>
                    {%- endif %}
                </ItemDetail>
                {%- endif %}
            {%- endfor %}
            {%- endfor %}
            {%- for discount in discounts %}
                {%- if discount.is_true_open_discount %}
                    {%- if discount.type == 'dollar' %}
                    <OpenDollarDiscItem>
                        {{ macros.id_node(discount) }}
                        <OpenDollarDiscAmt>{{ discount.value }}</OpenDollarDiscAmt>
                    </OpenDollarDiscItem>
                    {%- else %}
                    <OpenPercentDiscItem>
                        {{ macros.id_node(discount) }}
                        <OpenPercentDiscPct>{{ discount.value }}</OpenPercentDiscPct>
                    </OpenPercentDiscItem>
                    {%- endif %}
                {%- else %}
                <ItemDetail>
                    {{ macros.id_node(discount) }}
                    <Quantity>{{ discount.quantity or 1 }}</Quantity>
                    {%- if discount.value %}
                    <Price>{{ discount.value }}</Price>
                    {%- endif %}
                </ItemDetail>
                {%- endif %}
                {%- if not discount.is_true_open_discount %}
                    {%- for modifier in discount.modifiers %}
                    <ItemDetail>
                        {{ macros.id_node(modifier) }}
                        <Quantity>{{ modifier.quantity }}</Quantity>
                        {%- if modifier.item_comment %}
                        <Memo>{{ modifier.item_comment|e }}</Memo>
                        {%- endif %}
                    </ItemDetail>
                    {%- endfor %}
                {%- endif %}
            {%- endfor %}
        </Check>
            {%- for payment in payments %}
            <CreditCardAuth>
                <CardNumber>{{ payment.masked_card_number or payment.card_number }}</CardNumber>
                <ExpirationDate>{{ payment.expiration_date }}</ExpirationDate>
                <ReferenceNumber>{{ payment.reference_number }}</ReferenceNumber>
                <ApprovalCode>{{ payment.approval_code }}</ApprovalCode>
                <Amount>{{ payment.converted_payment }}</Amount>
                {%- if payment.converted_gift_card_balance %}
                <GCRemainingBalance>{{ payment.converted_gift_card_balance }}</GCRemainingBalance>
                {%- endif %}
                {%- if payment.type == '3rd_party' %}
                <DebitProcessing>Y</DebitProcessing>
                {%- endif %}
                <Gratuity>{{ payment.converted_tip }}</Gratuity>
                <SlipCopies>{{ slip_copies }}</SlipCopies>
                <Debit>N</Debit>
                {%- if payment.full_name %}
                <CardholderName>{{ payment.full_name|e }}</CardholderName>
                {%- endif %}
            </CreditCardAuth>
            {%- endfor %}
    </NewOrder>
</Orders>