{%- import "macros.jinja2" as macros %}
<Orders>
    {%- if user_info and account_maintenance and not house_account_number %}
    <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 house_account_number %}
                <Account>
                    <AccountNumber>{{ house_account_number }}</AccountNumber>
                </Account>
                {%- elif 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 %}
                {%- for service_charge in service_charges %}
                {%- if loop.index == 1 %}
                <ServiceCharge1>{{ service_charge.service_charge_id }}</ServiceCharge1>
                {%- endif %}
                {%- endfor %}
            </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 %}
                {%- if item.seat %}
                    <Seat>{{ item.seat }}</Seat>
                {%- 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 %}
                    {%- if modifier.price %}
                    <Price>{{ modifier.price }}</Price>
                    {%- 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 %}
        {%- if payment.positouch_type == 'CARD' %}
        <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' or (uses_posipay and payment.converted_tip > 0) %}
            <DebitProcessing>Y</DebitProcessing>
            {%- endif %}
            <Gratuity>{{ payment.converted_tip }}</Gratuity>
            {%- if payment.converted_tip > 0 %}
            <RestrictEdit>Y</RestrictEdit>
            {%- endif %}
            <SlipCopies>{{ slip_copies }}</SlipCopies>
            <Debit>N</Debit>
            {%- if payment.full_name %}
            <CardholderName>{{ payment.full_name|e }}</CardholderName>
            {%- endif %}
        </CreditCardAuth>
        {%- endif %}
        {%- endfor %}
    </NewOrder>
</Orders>