<commit_before>{% extends 'layout/page.html' %}
{% import 'macros.twig' as scat %}

{% block title %}
  {{ txn.friendly_type }} {{ txn.formatted_number }}
{% endblock %}

{% block extra_head %}
  <script src="/extern/mousetrap-1.6.2/mousetrap.min.js"></script>
  <script src="/extern/mousetrap-1.6.2/plugins/global-bind/mousetrap-global-bind.min.js"></script>
{% endblock %}

{% block content %}
  <style>
    [data-action~="editable"] {
      border-bottom: 1px solid rgba(255, 0, 0, 0.2);
    }
    [data-action] {
      cursor: pointer;
    }
  </style>
  <div class="row">
    <div class="col-sm-9">
      {{ block('search') }}
      <br>
      <div id="search-results"></div>
      <div data-reload="invoice">
        {{ block('invoice') }}
      </div>
    </div>
    <div data-reload="sidebar" id="sidebar" class="col-md-3">
      {{ block('sidebar') }}
    </div>
  </div>
{% endblock %}

{% block search %}
  <form class="form form-inline" data-action="search">
    <div class="input-group">
      <span class="input-group-addon"><i class="fa fa-fw fa-barcode"></i></span>
      <input type="hidden" name="scope" value="items">
      <input type="hidden" name="limit" value="50">
      <input type="text" class="form-control autofocus"
             name="q"
             autocomplete="off" autocorrect="off" autocapitalize="off"
             spellcheck="false"
             placeholder="Scan item or enter search terms"
             value="" size="200">
      <span class="input-group-btn">
        <input type="submit" class="btn btn-default" value="Find Items">
        <div class="btn-group">
          <button type="button" class="btn btn-default dropdown-toggle"
                  data-toggle="dropdown" aria-haspopup="true"
                  aria-expanded="false">
            Custom <span class="caret"></span>
          </button>
          <ul class="dropdown-menu">
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-GIFT">
                Gift Card
              </a>
            </li>
            <li role="separator" class="divider"></li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-FLOAT">
                Floater Frame
              </a>
            </li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-CANVAS">
                Canvas
              </a>
            </li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-PANEL">
                Panel
              </a>
            </li>
          </ul>
        </div>
      </span>
    </div>
  </form>
{% endblock %}

{% block invoice %}
  {% import 'macros.twig' as scat %}
  <div class="panel panel-default">
    <style>
      #txn th:last-child, #txn td:last-child {
        padding-right: 1rem;
      }
      .no-backorder, .no-backorder a {
        color: #500;
      }
    </style>
    {% if not txn.stocked() %}
      <div class="panel-header">
        <div class="alert alert-danger">
          This order includes items that are not in stock (<i class="fa fa-fw fa-exclamation-triangle"></i>).
        </div>
      </div>
    {% endif %}
    <table id="txn" class="table table-striped table-condensed table-hover">
      <thead>
        <tr>
          <th></th>
          <th>Qty</th>
          <th>Code</th>
          <th width="50%">Name</th>
          <th style="text-align: right">Price</th>
          <th style="text-align: right">Ext</th>
        </tr>
      </thead>
      <tbody>
        {% for line in txn.items.find_many %}
          <tr data-id="{{ line.id }}"
              data-editable-base="/sale/{{ txn.id }}/item/{{ line.id }}"
              class="{{ line.item().no_backorder or not line.item.active ? 'no-backorder' }}"
              >
            <td>
              {% if not line.kit_id %}
                <a data-action="remove-item" class="btn btn-link btn-xs">
                  <i class="fa fa-fw fa-trash-o"></i>
                </a>
              {% endif %}
            </td>
            <td>
              <div data-action="editable" data-name="quantity">
                {{ (txn.type == 'vendor' ? 1 : -1) * line.ordered }}
              </div>
            </td>
            <td>
              <a data-action="show-item-info" data-code="{{ line.code }}">
                {{ line.code }}
              </a>
            </td>
            <td style="{{ not line.item.active ? 'text-decoration: line-through' }}">
              {% if line.item.code == 'ZZ-GIFTCARD' and txn.paid %}
                {% if line.data().card %}
                  <a data-action="print-gift-card"
                     data-card="{{ line.data().card }}">
                    <i class="fa fa-print"></i>
                  </a>
                  <a data-action="email-gift-card"
                     data-card="{{ line.data().card }}">
                    <i class="fa fa-envelope-o"></i>
                  </a>
                {% else %}
                  <a data-action="create-gift-card"
                     data-value="{{ line.sale_price }}">
                    <i class="fa fa-barcode"></i>
                  </a>
                {% endif %}
              {% endif %}
              {% if line.item.no_backorder %}
                <span class="pull-right"
                      data-toggle="tooltip"
                      title="This item is not available for backorder!">
                  <i class="fa fa-fw fa-exclamation-circle"></i>
                </span>
              {% endif %}
              {% if line.item.tic == '00000' and line.item.stock(txn.created) < -line.ordered %}
                <span class="pull-right"
                      data-toggle="tooltip"
                      title="There is not enough stock!">
                  <i class="fa fa-fw fa-exclamation-triangle"></i>
                </span>
              {% endif %}
              {% if (-line.ordered % max(1,line.item.purchase_quantity)) != 0 %}
                <span class="pull-right text-danger"
                      data-toggle="tooltip"
                      title="Not ordering purchase quantity!">
                  <i class="fa fa-fw fa-warning"></i>
                </span>
              {% endif %}
              <span data-action="editable" data-name="override_name">
                {{- line.name -}}
              </span>
              <div><small>{{ line.pricing_detail }}</small></div>
            </td>
            <td style="text-align: right">
              {% if not line.kit_id %}
                <div data-action="editable" data-name="sale_price">
                  {{- scat.amount(line.sale_price) -}}
                </div>
              {% endif %}
            </td>
            <td style="text-align: right">
              {% if not line.kit_id %}
                {{ scat.amount((txn.type == 'vendor' ? 1 : -1) * line.ext_price) }}
              {% endif %}
            </td>
          </tr>
        {% endfor %}
      </tbody>
      <tfoot>
        <tr>
          <th colspan="5" style="text-align: right">
            Subtotal:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.subtotal) }}</td>
        </tr>
        <tr>
          <th colspan="5" style="text-align: right">
            Tax:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.tax) }}</td>
        </tr>
        <tr>
          <th colspan="5" style="text-align: right">
            Total:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.total) }}</td>
        </tr>
        {% for payment in txn.payments.find_many %}
          <tr>
            <th colspan="5" style="text-align: right">
            {%- if payment.method == 'gift' -%}
              <a href="/gift-card/{{ payment.data().card }}">
            {%- endif -%}
              {{- payment.pretty_method -}}:
            {%- if payment.method == 'gift' -%}
              </a>
            {%- endif -%}
            </th>
            <td style="text-align: right">
              {{ scat.amount(-1 * payment.amount) }}
            </td>
          </tr>
        {% endfor %}
        <tr>
          <th colspan="5" style="text-align: right">
            Due:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.due) }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
{% endblock %}

{% block sidebar %}
  {% import 'macros.twig' as scat %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <div class="pull-right">
        <button class="btn btn-sm btn-default"
                data-action="show-notes" data-kind="txn"
                data-attach-id="{{ txn.id }}">
          <i class="fa fa-fw fa-sticky-note-o"></i>
          <span class="label label-{{ txn.notes.where('todo',1).count ? 'danger' : 'default' }}">
            {{- txn.notes.count ?: '' -}}
          </span>
        </button>
      </div>
      <button class="btn btn-link btn-sm pull-right" data-action="report">
        <i class="fa fa-fw fa-line-chart"></i>
      </button>
      <h1 class="panel-title">
        {{ block('title') }}
        {% if txn.online_sale_id %}
          ({{ "%07d"|format(txn.online_sale_id) }})
        {% endif %}
      </h1>
      <small data-toggle="tooltip" title="{{ (txn.filled ? ('Filled: ' ~ txn.filled|date("F j Y g:ia"))) ~ "\n" ~ (txn.paid ? ('Paid: ' ~ txn.paid|date("F j Y g:ia"))) }}">
        {{ txn.created|date("l, F j Y g:ia") }}
      </small>
    </div>
    <div class="panel-body">
      <style>
        .list-group-item.disabled {
          opacity: 40%;
        }
      </style>
      {% if txn.person_id %}
        {% set person= txn.person %}

        {% if txn.person.notes %}
          <div class="alert alert-danger">
            {{- txn.person.notes -}}
          </div>
        {% endif %}
        {% if not txn.paid %}
          {% if person.exemption_certificate_id %}
            {% if txn.tax_rate > 0 %}
              <button class="btn btn-primary btn-block"
                      data-action="tax-exempt" data-value="1">
                <i class="fa fa-certificate fa-fw"></i>
                Apply Tax Exemption
              </button>
            {% else %}
              <button class="btn btn-danger btn-block"
                      data-action="tax-exempt" data-value="0">
                <i class="fa fa-certificate fa-fw"></i>
                Remove Tax Exemption
              </button>
            {% endif %}
            <br><br>
          {% endif %}

          {% if txn.used_loyalty_reward %}
            <button class="btn btn-default btn-block"
                    data-action="clear-loyalty-reward">
              Clear Loyalty Reward
            </button>
          {% else %}
            <h4>
              <div class="pull-right">
                <button class="btn btn-xs btn-default" data-no-rewards="{{ txn.no_rewards }}" data-action="toggle-no-rewards">
                  <i class="fa fa-fw {{ txn.no_rewards ? 'fa-square-o' : 'fa-check-square-o' }}"></i>
                  Eligible
                </button>
              </div>
              Rewards
            </h4>
            <ul class="list-group">
              {% for reward in txn.person.available_loyalty_rewards.find_many() %}
                <a class="list-group-item
                        {{ (-reward.item.retail_price > txn.due) ? 'disabled' }}"
                    data-reward="{{ reward.id }}"
                    data-action="use-loyalty-reward">
                  <span class="badge">{{ reward.cost }}</span>
                  {{ reward.item.name }}
                </a>
              {% endfor %}
            </ul>
          {% endif %}
        {% endif %}
      {% else %}
        <button class="btn btn-lg btn-block btn-primary"
                data-action="change-person">
          <i class="fa fa-user-o"></i> Rewards Check-In
        </button>
      {% endif %}
      <div class="btn-group btn-block">
        <button type="button"
                class="btn btn-default btn-block dropdown-toggle"
                data-toggle="dropdown" aria-expanded="false">
          {{ scat.format_txn_status(txn.status) }}
          <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
          <li>
            {% set options= ['new','filled','paid','processing','waitingforitems','readyforpickup','shipping','shipped','complete','template'] %}
            {% for opt in options %}
              <a data-action="set-status" data-status="{{ opt }}">
                {{ scat.format_txn_status(opt) }}
              </a>
            {% endfor %}
          </li>
        </ul>
      </div>
      <div class="well">
        <h1 class="text-center {{ txn.due < 0 ? 'text-danger' }}"
            style="margin: 0px; padding: 0px">
          {{ scat.amount(txn.due) }}
        </h1>
        {% if txn.change %}
          <div class="alert alert-success" style="font-size: larger; font-weight: bold; text-align: center; margin-top: 0.75em">
            Change: {{ scat.amount(-txn.change) }}
          </div>
        {% endif %}
      </div>

      <div class="text-center">
        <div class="btn-group btn-group-lg">
         <button type="button" class="btn btn-default" data-action="print-receipt">
           <u>P</u>rint
         </button>
         <button type="button" class="btn btn-default dropdown-toggle" 
                 data-toggle="dropdown" aria-expanded="false">
          <span class="caret"></span>
           <span class="sr-only">Toggle Dropdown</span>
          </button>
         <ul class="dropdown-menu" role="menu">
          <li><a data-action="print-invoice">Invoice</a></li>
          <li><a data-action="print-receipt">Receipt</a></li>
          <li>
            <a data-action="print-receipt" data-variation="gift">
              Gift Receipt
            </a>
          </li>
          <li>
            <a data-action="print-invoice" data-variation="packing-slip">
              Packing Slip
            </a>
          </li>
          <li>
            <a data-action="print-invoice" data-download="1">
              Download
            </a>
          </li>
          <li>
            <a data-action="email-invoice">
              Email
            </a>
          </li>
         </ul>
        </div>

        {% if txn.paid %}
          <button data-action="return-sale" class="btn btn-lg btn-default">
            Return
          </button>
        {% else %}
          {% if txn.items.count %}
            <button data-action="pay-sale" class="btn btn-lg btn-default">
              Pa<u>y</u>
            </button>
          {% else %}
            <button data-action="delete-sale" class="btn btn-lg btn-default">
              Delete
            </button>
          {% endif %}
        {% endif %}
      </div>

      {% if txn.returned_from_id %}
        <h4>Returned From</h4>
        <ul class="list-group">
          <a class="list-group-item" href="/sale/{{ txn.returned_from_id }}">
            {{ txn.returned_from.friendly_type }}
            {{ txn.returned_from.formatted_number }}
          </a>
        </ul>

        {% if txn.status in [ 'paid', 'complete' ] %}
          <a data-action="send-canned-message" data-message="refunded"
              class="btn btn-block btn-primary">
            <i class="fa fa-envelope-o"></i>
            Refund Processed
          </a>
        {% endif %}

      {% endif %}

      {% set returns= txn.returns.find_many() %}
      {% if returns|length %}
        <h4>Returns</h4>
        <ul class="list-group">
          {% for return in returns %}
            <a class="list-group-item" href="/sale/{{ return.id }}">
              {{ return.friendly_type ~ " " ~ return.formatted_number }}
            </a>
          {% endfor %}
        </ul>
      {% endif %}


      <h3>
        <a data-action="change-person"
           class="btn btn-default btn-sm pull-right">
          <i class="fa fa-user-o"></i>
        </a>
        {{ txn.type == 'customer' ? 'Customer' : 'Vendor' }}
      </h3>

      <a data-action="edit-person" data-id="{{ txn.person_id }}">
        {{ txn.person.friendly_name ?: 'Anonymous' }}
        {% if txn.person and txn.person.store_credit.balance > 0 %}
          <span class="label label-success">
            {{ scat.amount(txn.person.store_credit.balance) }}
          </span>
        {% endif %}
      </a>

      {% if person.id %}
        <div>{{ person.email }}</div>
        <div>{{ person.pretty_phone }}</div>
        <a data-action="show-loyalty">
          <i class="fa fa-star"></i>
          {{ person.points_available }}
        </a>
        {% if person.points_pending %}
          + <i class="fa fa-star"></i> {{ person.points_pending }}
          = {{ person.points_available + person.points_pending }}
        {% endif %}
        {% if txn.due > 0 %}
          {# subtract points being used #}
          {% if not txn.no_rewards %}
            + <i class="fa fa-star"></i> {{ txn.points_earned }}
            = {{ person.points_available + person.points_pending + txn.points_earned }}
          {% endif %}
        {% endif %}

        {% if person.vendor_rebate > 0 and not txn.paid %}
          <br><br>
          {% if not txn.used_discount %}
            <button class="btn btn-primary btn-block"
                    data-action="apply-discount"
                    data-discount="{{ person.vendor_rebate }}%">
              <i class="fa fa-percent fa-fw"></i>
              Apply {{ '%g'|format(person.vendor_rebate ) }}% Discount
            </button>
          {% else %}
            <button class="btn btn-primary btn-block"
                    data-action="remove-discount">
              <i class="fa fa-percent fa-fw"></i>
              Remove Discount
            </button>
          {% endif %}
        {% endif %}
      {% endif %}

      <div class="clearfix"></div>

      <h3>
        <div class="btn-group pull-right">
         <button type="button" class="btn btn-default btn-sm dropdown-toggle"
                 data-toggle="dropdown" aria-expanded="false">
          <i class="fa fa-truck"></i>
          <span class="caret"></span>
         </button>
         <ul class="dropdown-menu" role="menu">
          <li>
            <a data-action="edit-shipping-address">
              {{ txn.shipping_address_id > 1 ? 'Edit' : 'Add' }}
              Shipping Address
            </a>
            {% if txn.shipping_address_id != 1 %}
              <a data-action="set-in-store-pickup">
                Set for In-Store Pickup
              </a>
            {% endif %}
            <a data-action="create-delivery">Create Delivery</a>
            <a data-action="create-dropship">Create Drop Shipment</a>
            <a data-action="create-shipment">Create Shipment</a>
            <a data-action="add-tracker">Add Tracker</a>
          </li>
         </ul>
        </div>

        Shipping
      </h3>

      {% if txn.shipping_address_id == 1 %}
        <h4>Curbside Pickup</h4>
      {% elseif txn.shipping_address_id %}
        <h4>{{ txn.is_bike_delivery ? 'Deliver to' : 'Ship to' }}:</h4>
        {% set shipping_address= txn.shipping_address() %}
        <address>
          <div>{{ shipping_address.name }}</div>
          <div>{{ shipping_address.company }}</div>
          <div>{{ shipping_address.email }}</div>
          {% if shipping_address.phone %}
            <div>{{ shipping_address.phone | phone_number_format }}</div>
          {% endif %}
          <div>{{ shipping_address.street1 }}</div>
          <div>{{ shipping_address.street2 }}</div>
          <div>
            {% if shipping_address.city %}
              {{ shipping_address.city }},
            {% endif %}
            {{ shipping_address.state }}
            {{ shipping_address.zip }}
          </div>
        </address>
      {% endif %}

      {% if txn.status in [ 'paid' ] %}
        <a data-action="print-packing-slip-and-process"
            class="btn btn-block btn-success">
          <i class="fa fa-print"></i>
          Print Packing Slip
        </a>
      {% endif %}

      {% if txn.status in [ 'readyforpickup' ] %}
        <a data-action="send-canned-message" data-message="pickup-reminder"
            class="btn btn-block btn-info">
          <i class="fa fa-envelope-o"></i>
          Pickup Reminder
        </a>
      {% endif %}

      {% if txn.status in [ 'paid', 'processing', 'waitingforitems' ] %}
        {% if txn.shipping_address_id > 1 %}
          {% if txn.is_local_delivery %}
            <a data-action="create-delivery"
                class="btn btn-block btn-primary">
              <i class="fa fa-truck"></i>
              Create Delivery
            </a>
          {% else %}
            <a data-action="create-shipment"
                class="btn btn-block btn-primary">
              <i class="fa fa-truck"></i>
              Create Shipment
            </a>
          {% endif %}

          <a data-action="send-canned-message" data-message="ship-delay"
              class="btn btn-block btn-warning">
            <i class="fa fa-envelope-o"></i>
            Shipping Delay
          </a>
        {% endif %}

        {% if txn.shipping_address_id == 1 %}
          <a data-action="send-canned-message"
              data-message="{{ txn.total > 100 ? 'pickup-over100' : 'pickup' }}"
              class="btn btn-block btn-primary">
            <i class="fa fa-envelope-o"></i>
            Ready for Pickup
          </a>
        {% endif %}

        {% if txn.shipping_address_id == 1 %}
          <a data-action="send-canned-message"
              data-message="{{ txn.total > 100 ? 'pickupmost-over100' : 'pickupmost' }}"
              class="btn btn-block btn-warning">
            <i class="fa fa-envelope-o"></i>
            Ready for Pickup (Most)
          </a>
        {% endif %}
      {% endif %}

      {% if txn.status in [ 'paid', 'processing', 'waitingforitems', 'readyforpickup' ] and (txn.shipping_address_id == 1 or txn.is_local_delivery) %}
        <a data-action="set-status" data-status="complete"
            class="btn btn-block btn-primary">
          <i class="fa fa-check-square-o"></i>
          Complete
        </a>
      {% endif %}

      {% if txn.shipments.count %}
        <h4>Shipments</h4>
        <ul id="shipments" class="list-group">
          {% for shipment in txn.shipments.find_many() %}
            <li class="list-group-item"
                data-action="{{ shipment.status == 'pending' ? 'finalize-shipment' : 'show-shipment' }}" data-id="{{ shipment.id }}">
              <span class="pull-right">
                {{ scat.format_shipping_status(shipment.status) }}
              </span>
              {{ shipment.created|date("D F j") }}
              <div class="clearfix"></div>
            </li>
          {% endfor %}
        </ul>
      {% endif %}

    </div><commit_msg>Change 'Most' to 'Partial'<commit_after>{% extends 'layout/page.html' %}
{% import 'macros.twig' as scat %}

{% block title %}
  {{ txn.friendly_type }} {{ txn.formatted_number }}
{% endblock %}

{% block extra_head %}
  <script src="/extern/mousetrap-1.6.2/mousetrap.min.js"></script>
  <script src="/extern/mousetrap-1.6.2/plugins/global-bind/mousetrap-global-bind.min.js"></script>
{% endblock %}

{% block content %}
  <style>
    [data-action~="editable"] {
      border-bottom: 1px solid rgba(255, 0, 0, 0.2);
    }
    [data-action] {
      cursor: pointer;
    }
  </style>
  <div class="row">
    <div class="col-sm-9">
      {{ block('search') }}
      <br>
      <div id="search-results"></div>
      <div data-reload="invoice">
        {{ block('invoice') }}
      </div>
    </div>
    <div data-reload="sidebar" id="sidebar" class="col-md-3">
      {{ block('sidebar') }}
    </div>
  </div>
{% endblock %}

{% block search %}
  <form class="form form-inline" data-action="search">
    <div class="input-group">
      <span class="input-group-addon"><i class="fa fa-fw fa-barcode"></i></span>
      <input type="hidden" name="scope" value="items">
      <input type="hidden" name="limit" value="50">
      <input type="text" class="form-control autofocus"
             name="q"
             autocomplete="off" autocorrect="off" autocapitalize="off"
             spellcheck="false"
             placeholder="Scan item or enter search terms"
             value="" size="200">
      <span class="input-group-btn">
        <input type="submit" class="btn btn-default" value="Find Items">
        <div class="btn-group">
          <button type="button" class="btn btn-default dropdown-toggle"
                  data-toggle="dropdown" aria-haspopup="true"
                  aria-expanded="false">
            Custom <span class="caret"></span>
          </button>
          <ul class="dropdown-menu">
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-GIFT">
                Gift Card
              </a>
            </li>
            <li role="separator" class="divider"></li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-FLOAT">
                Floater Frame
              </a>
            </li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-CANVAS">
                Canvas
              </a>
            </li>
            <li>
              <a data-action="add-item-by-code" data-code="ZZ-PANEL">
                Panel
              </a>
            </li>
          </ul>
        </div>
      </span>
    </div>
  </form>
{% endblock %}

{% block invoice %}
  {% import 'macros.twig' as scat %}
  <div class="panel panel-default">
    <style>
      #txn th:last-child, #txn td:last-child {
        padding-right: 1rem;
      }
      .no-backorder, .no-backorder a {
        color: #500;
      }
    </style>
    {% if not txn.stocked() %}
      <div class="panel-header">
        <div class="alert alert-danger">
          This order includes items that are not in stock (<i class="fa fa-fw fa-exclamation-triangle"></i>).
        </div>
      </div>
    {% endif %}
    <table id="txn" class="table table-striped table-condensed table-hover">
      <thead>
        <tr>
          <th></th>
          <th>Qty</th>
          <th>Code</th>
          <th width="50%">Name</th>
          <th style="text-align: right">Price</th>
          <th style="text-align: right">Ext</th>
        </tr>
      </thead>
      <tbody>
        {% for line in txn.items.find_many %}
          <tr data-id="{{ line.id }}"
              data-editable-base="/sale/{{ txn.id }}/item/{{ line.id }}"
              class="{{ line.item().no_backorder or not line.item.active ? 'no-backorder' }}"
              >
            <td>
              {% if not line.kit_id %}
                <a data-action="remove-item" class="btn btn-link btn-xs">
                  <i class="fa fa-fw fa-trash-o"></i>
                </a>
              {% endif %}
            </td>
            <td>
              <div data-action="editable" data-name="quantity">
                {{ (txn.type == 'vendor' ? 1 : -1) * line.ordered }}
              </div>
            </td>
            <td>
              <a data-action="show-item-info" data-code="{{ line.code }}">
                {{ line.code }}
              </a>
            </td>
            <td style="{{ not line.item.active ? 'text-decoration: line-through' }}">
              {% if line.item.code == 'ZZ-GIFTCARD' and txn.paid %}
                {% if line.data().card %}
                  <a data-action="print-gift-card"
                     data-card="{{ line.data().card }}">
                    <i class="fa fa-print"></i>
                  </a>
                  <a data-action="email-gift-card"
                     data-card="{{ line.data().card }}">
                    <i class="fa fa-envelope-o"></i>
                  </a>
                {% else %}
                  <a data-action="create-gift-card"
                     data-value="{{ line.sale_price }}">
                    <i class="fa fa-barcode"></i>
                  </a>
                {% endif %}
              {% endif %}
              {% if line.item.no_backorder %}
                <span class="pull-right"
                      data-toggle="tooltip"
                      title="This item is not available for backorder!">
                  <i class="fa fa-fw fa-exclamation-circle"></i>
                </span>
              {% endif %}
              {% if line.item.tic == '00000' and line.item.stock(txn.created) < -line.ordered %}
                <span class="pull-right"
                      data-toggle="tooltip"
                      title="There is not enough stock!">
                  <i class="fa fa-fw fa-exclamation-triangle"></i>
                </span>
              {% endif %}
              {% if (-line.ordered % max(1,line.item.purchase_quantity)) != 0 %}
                <span class="pull-right text-danger"
                      data-toggle="tooltip"
                      title="Not ordering purchase quantity!">
                  <i class="fa fa-fw fa-warning"></i>
                </span>
              {% endif %}
              <span data-action="editable" data-name="override_name">
                {{- line.name -}}
              </span>
              <div><small>{{ line.pricing_detail }}</small></div>
            </td>
            <td style="text-align: right">
              {% if not line.kit_id %}
                <div data-action="editable" data-name="sale_price">
                  {{- scat.amount(line.sale_price) -}}
                </div>
              {% endif %}
            </td>
            <td style="text-align: right">
              {% if not line.kit_id %}
                {{ scat.amount((txn.type == 'vendor' ? 1 : -1) * line.ext_price) }}
              {% endif %}
            </td>
          </tr>
        {% endfor %}
      </tbody>
      <tfoot>
        <tr>
          <th colspan="5" style="text-align: right">
            Subtotal:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.subtotal) }}</td>
        </tr>
        <tr>
          <th colspan="5" style="text-align: right">
            Tax:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.tax) }}</td>
        </tr>
        <tr>
          <th colspan="5" style="text-align: right">
            Total:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.total) }}</td>
        </tr>
        {% for payment in txn.payments.find_many %}
          <tr>
            <th colspan="5" style="text-align: right">
            {%- if payment.method == 'gift' -%}
              <a href="/gift-card/{{ payment.data().card }}">
            {%- endif -%}
              {{- payment.pretty_method -}}:
            {%- if payment.method == 'gift' -%}
              </a>
            {%- endif -%}
            </th>
            <td style="text-align: right">
              {{ scat.amount(-1 * payment.amount) }}
            </td>
          </tr>
        {% endfor %}
        <tr>
          <th colspan="5" style="text-align: right">
            Due:
          </th>
          <td style="text-align: right">{{ scat.amount(txn.due) }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
{% endblock %}

{% block sidebar %}
  {% import 'macros.twig' as scat %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <div class="pull-right">
        <button class="btn btn-sm btn-default"
                data-action="show-notes" data-kind="txn"
                data-attach-id="{{ txn.id }}">
          <i class="fa fa-fw fa-sticky-note-o"></i>
          <span class="label label-{{ txn.notes.where('todo',1).count ? 'danger' : 'default' }}">
            {{- txn.notes.count ?: '' -}}
          </span>
        </button>
      </div>
      <button class="btn btn-link btn-sm pull-right" data-action="report">
        <i class="fa fa-fw fa-line-chart"></i>
      </button>
      <h1 class="panel-title">
        {{ block('title') }}
        {% if txn.online_sale_id %}
          ({{ "%07d"|format(txn.online_sale_id) }})
        {% endif %}
      </h1>
      <small data-toggle="tooltip" title="{{ (txn.filled ? ('Filled: ' ~ txn.filled|date("F j Y g:ia"))) ~ "\n" ~ (txn.paid ? ('Paid: ' ~ txn.paid|date("F j Y g:ia"))) }}">
        {{ txn.created|date("l, F j Y g:ia") }}
      </small>
    </div>
    <div class="panel-body">
      <style>
        .list-group-item.disabled {
          opacity: 40%;
        }
      </style>
      {% if txn.person_id %}
        {% set person= txn.person %}

        {% if txn.person.notes %}
          <div class="alert alert-danger">
            {{- txn.person.notes -}}
          </div>
        {% endif %}
        {% if not txn.paid %}
          {% if person.exemption_certificate_id %}
            {% if txn.tax_rate > 0 %}
              <button class="btn btn-primary btn-block"
                      data-action="tax-exempt" data-value="1">
                <i class="fa fa-certificate fa-fw"></i>
                Apply Tax Exemption
              </button>
            {% else %}
              <button class="btn btn-danger btn-block"
                      data-action="tax-exempt" data-value="0">
                <i class="fa fa-certificate fa-fw"></i>
                Remove Tax Exemption
              </button>
            {% endif %}
            <br><br>
          {% endif %}

          {% if txn.used_loyalty_reward %}
            <button class="btn btn-default btn-block"
                    data-action="clear-loyalty-reward">
              Clear Loyalty Reward
            </button>
          {% else %}
            <h4>
              <div class="pull-right">
                <button class="btn btn-xs btn-default" data-no-rewards="{{ txn.no_rewards }}" data-action="toggle-no-rewards">
                  <i class="fa fa-fw {{ txn.no_rewards ? 'fa-square-o' : 'fa-check-square-o' }}"></i>
                  Eligible
                </button>
              </div>
              Rewards
            </h4>
            <ul class="list-group">
              {% for reward in txn.person.available_loyalty_rewards.find_many() %}
                <a class="list-group-item
                        {{ (-reward.item.retail_price > txn.due) ? 'disabled' }}"
                    data-reward="{{ reward.id }}"
                    data-action="use-loyalty-reward">
                  <span class="badge">{{ reward.cost }}</span>
                  {{ reward.item.name }}
                </a>
              {% endfor %}
            </ul>
          {% endif %}
        {% endif %}
      {% else %}
        <button class="btn btn-lg btn-block btn-primary"
                data-action="change-person">
          <i class="fa fa-user-o"></i> Rewards Check-In
        </button>
      {% endif %}
      <div class="btn-group btn-block">
        <button type="button"
                class="btn btn-default btn-block dropdown-toggle"
                data-toggle="dropdown" aria-expanded="false">
          {{ scat.format_txn_status(txn.status) }}
          <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
          <li>
            {% set options= ['new','filled','paid','processing','waitingforitems','readyforpickup','shipping','shipped','complete','template'] %}
            {% for opt in options %}
              <a data-action="set-status" data-status="{{ opt }}">
                {{ scat.format_txn_status(opt) }}
              </a>
            {% endfor %}
          </li>
        </ul>
      </div>
      <div class="well">
        <h1 class="text-center {{ txn.due < 0 ? 'text-danger' }}"
            style="margin: 0px; padding: 0px">
          {{ scat.amount(txn.due) }}
        </h1>
        {% if txn.change %}
          <div class="alert alert-success" style="font-size: larger; font-weight: bold; text-align: center; margin-top: 0.75em">
            Change: {{ scat.amount(-txn.change) }}
          </div>
        {% endif %}
      </div>

      <div class="text-center">
        <div class="btn-group btn-group-lg">
         <button type="button" class="btn btn-default" data-action="print-receipt">
           <u>P</u>rint
         </button>
         <button type="button" class="btn btn-default dropdown-toggle" 
                 data-toggle="dropdown" aria-expanded="false">
          <span class="caret"></span>
           <span class="sr-only">Toggle Dropdown</span>
          </button>
         <ul class="dropdown-menu" role="menu">
          <li><a data-action="print-invoice">Invoice</a></li>
          <li><a data-action="print-receipt">Receipt</a></li>
          <li>
            <a data-action="print-receipt" data-variation="gift">
              Gift Receipt
            </a>
          </li>
          <li>
            <a data-action="print-invoice" data-variation="packing-slip">
              Packing Slip
            </a>
          </li>
          <li>
            <a data-action="print-invoice" data-download="1">
              Download
            </a>
          </li>
          <li>
            <a data-action="email-invoice">
              Email
            </a>
          </li>
         </ul>
        </div>

        {% if txn.paid %}
          <button data-action="return-sale" class="btn btn-lg btn-default">
            Return
          </button>
        {% else %}
          {% if txn.items.count %}
            <button data-action="pay-sale" class="btn btn-lg btn-default">
              Pa<u>y</u>
            </button>
          {% else %}
            <button data-action="delete-sale" class="btn btn-lg btn-default">
              Delete
            </button>
          {% endif %}
        {% endif %}
      </div>

      {% if txn.returned_from_id %}
        <h4>Returned From</h4>
        <ul class="list-group">
          <a class="list-group-item" href="/sale/{{ txn.returned_from_id }}">
            {{ txn.returned_from.friendly_type }}
            {{ txn.returned_from.formatted_number }}
          </a>
        </ul>

        {% if txn.status in [ 'paid', 'complete' ] %}
          <a data-action="send-canned-message" data-message="refunded"
              class="btn btn-block btn-primary">
            <i class="fa fa-envelope-o"></i>
            Refund Processed
          </a>
        {% endif %}

      {% endif %}

      {% set returns= txn.returns.find_many() %}
      {% if returns|length %}
        <h4>Returns</h4>
        <ul class="list-group">
          {% for return in returns %}
            <a class="list-group-item" href="/sale/{{ return.id }}">
              {{ return.friendly_type ~ " " ~ return.formatted_number }}
            </a>
          {% endfor %}
        </ul>
      {% endif %}


      <h3>
        <a data-action="change-person"
           class="btn btn-default btn-sm pull-right">
          <i class="fa fa-user-o"></i>
        </a>
        {{ txn.type == 'customer' ? 'Customer' : 'Vendor' }}
      </h3>

      <a data-action="edit-person" data-id="{{ txn.person_id }}">
        {{ txn.person.friendly_name ?: 'Anonymous' }}
        {% if txn.person and txn.person.store_credit.balance > 0 %}
          <span class="label label-success">
            {{ scat.amount(txn.person.store_credit.balance) }}
          </span>
        {% endif %}
      </a>

      {% if person.id %}
        <div>{{ person.email }}</div>
        <div>{{ person.pretty_phone }}</div>
        <a data-action="show-loyalty">
          <i class="fa fa-star"></i>
          {{ person.points_available }}
        </a>
        {% if person.points_pending %}
          + <i class="fa fa-star"></i> {{ person.points_pending }}
          = {{ person.points_available + person.points_pending }}
        {% endif %}
        {% if txn.due > 0 %}
          {# subtract points being used #}
          {% if not txn.no_rewards %}
            + <i class="fa fa-star"></i> {{ txn.points_earned }}
            = {{ person.points_available + person.points_pending + txn.points_earned }}
          {% endif %}
        {% endif %}

        {% if person.vendor_rebate > 0 and not txn.paid %}
          <br><br>
          {% if not txn.used_discount %}
            <button class="btn btn-primary btn-block"
                    data-action="apply-discount"
                    data-discount="{{ person.vendor_rebate }}%">
              <i class="fa fa-percent fa-fw"></i>
              Apply {{ '%g'|format(person.vendor_rebate ) }}% Discount
            </button>
          {% else %}
            <button class="btn btn-primary btn-block"
                    data-action="remove-discount">
              <i class="fa fa-percent fa-fw"></i>
              Remove Discount
            </button>
          {% endif %}
        {% endif %}
      {% endif %}

      <div class="clearfix"></div>

      <h3>
        <div class="btn-group pull-right">
         <button type="button" class="btn btn-default btn-sm dropdown-toggle"
                 data-toggle="dropdown" aria-expanded="false">
          <i class="fa fa-truck"></i>
          <span class="caret"></span>
         </button>
         <ul class="dropdown-menu" role="menu">
          <li>
            <a data-action="edit-shipping-address">
              {{ txn.shipping_address_id > 1 ? 'Edit' : 'Add' }}
              Shipping Address
            </a>
            {% if txn.shipping_address_id != 1 %}
              <a data-action="set-in-store-pickup">
                Set for In-Store Pickup
              </a>
            {% endif %}
            <a data-action="create-delivery">Create Delivery</a>
            <a data-action="create-dropship">Create Drop Shipment</a>
            <a data-action="create-shipment">Create Shipment</a>
            <a data-action="add-tracker">Add Tracker</a>
          </li>
         </ul>
        </div>

        Shipping
      </h3>

      {% if txn.shipping_address_id == 1 %}
        <h4>Curbside Pickup</h4>
      {% elseif txn.shipping_address_id %}
        <h4>{{ txn.is_bike_delivery ? 'Deliver to' : 'Ship to' }}:</h4>
        {% set shipping_address= txn.shipping_address() %}
        <address>
          <div>{{ shipping_address.name }}</div>
          <div>{{ shipping_address.company }}</div>
          <div>{{ shipping_address.email }}</div>
          {% if shipping_address.phone %}
            <div>{{ shipping_address.phone | phone_number_format }}</div>
          {% endif %}
          <div>{{ shipping_address.street1 }}</div>
          <div>{{ shipping_address.street2 }}</div>
          <div>
            {% if shipping_address.city %}
              {{ shipping_address.city }},
            {% endif %}
            {{ shipping_address.state }}
            {{ shipping_address.zip }}
          </div>
        </address>
      {% endif %}

      {% if txn.status in [ 'paid' ] %}
        <a data-action="print-packing-slip-and-process"
            class="btn btn-block btn-success">
          <i class="fa fa-print"></i>
          Print Packing Slip
        </a>
      {% endif %}

      {% if txn.status in [ 'readyforpickup' ] %}
        <a data-action="send-canned-message" data-message="pickup-reminder"
            class="btn btn-block btn-info">
          <i class="fa fa-envelope-o"></i>
          Pickup Reminder
        </a>
      {% endif %}

      {% if txn.status in [ 'paid', 'processing', 'waitingforitems' ] %}
        {% if txn.shipping_address_id > 1 %}
          {% if txn.is_local_delivery %}
            <a data-action="create-delivery"
                class="btn btn-block btn-primary">
              <i class="fa fa-truck"></i>
              Create Delivery
            </a>
          {% else %}
            <a data-action="create-shipment"
                class="btn btn-block btn-primary">
              <i class="fa fa-truck"></i>
              Create Shipment
            </a>
          {% endif %}

          <a data-action="send-canned-message" data-message="ship-delay"
              class="btn btn-block btn-warning">
            <i class="fa fa-envelope-o"></i>
            Shipping Delay
          </a>
        {% endif %}

        {% if txn.shipping_address_id == 1 %}
          <a data-action="send-canned-message"
              data-message="{{ txn.total > 100 ? 'pickup-over100' : 'pickup' }}"
              class="btn btn-block btn-primary">
            <i class="fa fa-envelope-o"></i>
            Ready for Pickup
          </a>
        {% endif %}

        {% if txn.shipping_address_id == 1 %}
          <a data-action="send-canned-message"
              data-message="{{ txn.total > 100 ? 'pickupmost-over100' : 'pickupmost' }}"
              class="btn btn-block btn-warning">
            <i class="fa fa-envelope-o"></i>
            Ready for Pickup (Partial)
          </a>
        {% endif %}
      {% endif %}

      {% if txn.status in [ 'paid', 'processing', 'waitingforitems', 'readyforpickup' ] and (txn.shipping_address_id == 1 or txn.is_local_delivery) %}
        <a data-action="set-status" data-status="complete"
            class="btn btn-block btn-primary">
          <i class="fa fa-check-square-o"></i>
          Complete
        </a>
      {% endif %}

      {% if txn.shipments.count %}
        <h4>Shipments</h4>
        <ul id="shipments" class="list-group">
          {% for shipment in txn.shipments.find_many() %}
            <li class="list-group-item"
                data-action="{{ shipment.status == 'pending' ? 'finalize-shipment' : 'show-shipment' }}" data-id="{{ shipment.id }}">
              <span class="pull-right">
                {{ scat.format_shipping_status(shipment.status) }}
              </span>
              {{ shipment.created|date("D F j") }}
              <div class="clearfix"></div>
            </li>
          {% endfor %}
        </ul>
      {% endif %}

    </div>
{'fraction_non_alphanumeric': 0.16993435448577682, 'fraction_numerical': 0.0045076586433260395, 'mean_word_length': 1.501696956426538, 'pattern_counts': {'":': 0, '<': 875, '<?xml version=': 0, '>': 889, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5681320', 'n_tokens_mistral': 14747, 'n_tokens_neox': 13688, 'n_words': 2672}