{%- import "macros.jinja2" as macros %}
<Orders>
    {{ macros.emit_user_info(user_info) }}
    <NewOrder>
        <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>
                    <AccountNumber>{{ user_info.account_number or '' }}</AccountNumber>
                    <LastName>{{ user_info.last_name or '' }}</LastName>
                    <FirstName>{{ user_info.first_name or '' }}</FirstName>
                    <MiddleInitial>{{ user_info.middle_initial or '' }}</MiddleInitial>
                </Account>
                {%- endif %}
                {%- if fire_time %}
                <AdvanceOrderDate>{{ fire_date }}</AdvanceOrderDate>
                <AdvanceOrderTime>{{ fire_time }}</AdvanceOrderTime>
                {%- 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 }}</Memo>
            </ItemDetail>
            {%- endif %}
            {%- for item in items %}
            <ItemDetail>
                {{ macros.id_node(item.ordering_id) }}
                <Quantity>{{ item.quantity or 1 }}</Quantity>
                {%- if item.price_per_unit %}
                <Price>{{ item.price_per_unit }}</Price>
                {%- endif %}
            </ItemDetail>
            {%- for modifier in item.modifiers %}
                <ItemDetail>
                    {{ macros.id_node(modifier.ordering_id) }}
                    <Quantity>{{ modifier.quantity }}</Quantity>
                    {%- if modifier.item_comment %}
                    <Memo>{{ modifier.item_comment }}</Memo>
                    {%- endif %}
                </ItemDetail>
                {%- if modifier.comment %}
                <ItemDetail>
                    {{ macros.id_node(comment_id) }}
                    <Memo>{{ modifier.comment }}</Memo>
                </ItemDetail>
                {%- endif %}
            {%- endfor %}
            {%- for discount in item.discounts %}
                {%- if discount.is_true_open_discount %}
                    {%- if discount.type == 'dollar' %}
                    <OpenDollarDiscItem>
                        {{ macros.id_node(discount.ordering_id) }}
                        <OpenDollarDiscAmt>{{ discount.value }}</OpenDollarDiscAmt>
                    </OpenDollarDiscItem>
                    {%- else %}
                    <OpenPercentDiscItem>
                        {{ macros.id_node(discount.ordering_id) }}
                        <OpenPercentDiscPct>{{ discount.value }}</OpenPercentDiscPct>
                    </OpenPercentDiscItem>
                    {%- endif %}
                {%- else %}
                <ItemDetail>
                    {%- if discount.ordering_id %}
                        {{ macros.id_node(discount.ordering_id) }}
                    {%- else %}
                        {{ macros.id_node(discount.id) }}
                    {%- endif %}
                    {%- 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.ordering_id) }}
                        <OpenDollarDiscAmt>{{ discount.value }}</OpenDollarDiscAmt>
                    </OpenDollarDiscItem>
                    {%- else %}
                    <OpenPercentDiscItem>
                        {{ macros.id_node(discount.ordering_id) }}
                        <OpenPercentDiscPct>{{ discount.value }}</OpenPercentDiscPct>
                    </OpenPercentDiscItem>
                    {%- endif %}
                {%- else %}
                <ItemDetail>
                    {{ macros.id_node(discount.ordering_id) }}
                    <Quantity>{{ discount.quantity or 1 }}</Quantity>
                    {%- if discount.value %}
                    <Price>{{ discount.value }}</Price>
                    {%- endif %}
                </ItemDetail>
                {%- endif %}
                {%- if not discount.is_true_open_discount %}
                    {%- if discount.comment %}
                        <ItemDetail>
                            {{ macros.id_node(comment_id) }}
                            <Memo>{{ discount.comment }}</Memo>
                        </ItemDetail>
                    {%- endif %}
                    {%- for modifier in discount.modifiers %}
                        {%- if modifier.item_comment %}
                        <ItemDetail>
                            {{ macros.id_node(modifier.ordering_id) }}
                            <Quantity>{{ modifier.quantity }}</Quantity>
                            <Memo>{{ modifier.item_comment }}</Memo>
                        </ItemDetail>
                        {%- endif %}
                        {%- if modifier.comment %}
                        <ItemDetail>
                            {{ macros.id_node(comment_id) }}
                            <Memo>{{ modifier.comment }}</Memo>
                        </ItemDetail>
                        {%- endif %}
                    {%- 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 }}</CardholderName>
                {%- endif %}
            </CreditCardAuth>
            {%- endfor %}
    </NewOrder>
</Orders>