Household Dynamic Mailing Labels - Table (Run as HTML)
Imports: 3

This report dynamically generates mailing labels based on the results from a list. Only the names of people on the list will be included on the mailing label. If both members of a married couple are included on the list, they will print as either "John & Jane Smith" or "John Smith & Jane Doe," based on whether they share the same last name. If two unmarried adults (who are members of the same household) are included, they will show as "John Smith, Jane Doe." In all cases, the primary contact will be listed first, and all names will include a prefix (if set). If only children in a household are included, the household name will be printed instead. This version generates as a downloadable table when run as an HTML report.

Preview

Report Preview


<!-- 

This report dynamically generates mailing labels based on the results from a list. 
Only the names of people on the list will be included on the mailing label. If 
both members of a married couple are included on the list, they will print as 
either "John & Jane Smith" or "John Smith & Jane Doe," based on whether they share
the same last name. If two unmarried adults (who are members of the same 
household) are included, they will show as "John Smith, Jane Doe." In all cases,
the primary contact will be listed first, and all names will include a prefix (if
set). If only children in a household are included, the household name will be
printed instead.

This version generates as a downloadable table when run as an HTML report.

-->


<html lang="en">
  <head>
    <meta charset="utf-8">
    
    <link href="https://unpkg.com/bootstrap@4.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    
    <script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
    <script src="https://unpkg.com/bootstrap@4.3.1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/bootstrap-table.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/extensions/export/bootstrap-table-export.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/extensions/multiple-sort/bootstrap-table-multiple-sort.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.15.5/dist/extensions/filter-control/bootstrap-table-filter-control.min.js"></script>

    <style>
        #toolbar { margin: 0; }
      	tr.household > td { 
          padding: 0.25em; }
      	tr:nth-child(even) { background-color: #edf2f7; }
      	thead { 
          border-bottom: 1px solid #bec9d3; }
				th { padding: .5em; }
      	.avatar { 
          width: 50px;
        	height: 50px;
					border-radius: 25px 25px 25px 25px;
          -moz-border-radius: 25px 25px 25px 25px;
          -webkit-border-radius: 25px 25px 25px 25px;
        }
      	.container {
          width: 100%;
          padding-top: 15px;
        }
    </style>
  </head>
  <body>
    <div class="container">
      <h2>
        {{ list.name }}
        <br>
        <small>
          {{ organization.name }}
        </small>
      </h2>
      <div class="clearfix"></div>
      
      <div id="toolbar" class="select" style="display: none;">
        <select class="form-control">
          <option value="">Export Basic</option>
          <option value="all">Export All</option>
          <option value="selected">Export Selected</option>
        </select>
      </div>
      
      <table id="table"
        style="width: 100%"
        data-show-export="true"
        data-search="true"
        data-show-multi-sort="true"
        data-show-columns="true"
        data-filter-control="true"
        data-show-search-clear-button="true"
        data-show-toggle="true">
      <thead>
        <tr>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">Addressee</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">Street 1</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">Street 2</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">City</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">State</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">Zip</th>
          <th style="text-align: left" data-sortable="true" data-filter-control="input">Location</th>
        </tr>
      </thead>
        <tbody>
          
      <!-- Add IDs of all people on list to array -->
      {% assign inclusionArray = '' | split: '$' %}
      
      {% for person in people %}
        {% assign addArray = person.id | split: '$' %}
        {% assign inclusionArray = inclusionArray | concat: addArray %}
      {% endfor %}
      
      
      
      {% for household in households %}
      
        {% assign lastNameArray = '' | split: '$' %}
        {% assign marriedAdults = 0 %}
        {% assign includedAdults = 0 %}
        {% assign sameLast = false %}

        <!-- Count number of married adults, and total adults on the list in this household -->
        {% for person in household.adults %}
          <!-- Convert Person ID from number to string -->
          {% assign personID = person.id | downcase %}
          {% if inclusionArray contains personID %}
            {% assign includedAdults = includedAdults | plus: 1 %}
            {% if person.marital_status == 'Married' %}
              {% assign marriedAdults = marriedAdults | plus: 1 %}
            {% endif %}
            <!-- Add last names to array -->
            {% if person.marital_status == 'Married' %}
              {% assign addArray = person.last_name | split: '$' %}
              {% assign lastNameArray = lastNameArray | concat: addArray %}
            {% endif %}
          {% endif %}
        {% endfor %}
        <!-- Filter for unique last names -->
        {% assign lastNameArray = lastNameArray | uniq %}
        
        
        <!-- Generate addressee string using included people -->
        
        {% assign nameArray = '' | split: '$' %}
        {% assign nameString = '' | split: '$'  %}
        
      
        <!-- Children Only -->
        {% if includedAdults == 0 %}
          {% assign nameString = household.name %}
          
          <!-- Married couple, same name -->
          {% elsif lastNameArray.size == 1 and marriedAdults > 1 %}
          {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id == person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.first_name}}{% endcapture%}
                {% assign addArray = addArray | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id != person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.first_name}}{% endcapture%}
                {% assign addArray = addArray | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% assign nameString = nameArray | join: ' & ' | append: ' ' | append: household.primary_contact.last_name %}
      
          <!-- Married couple, different names -->
          {% elsif lastNameArray.size == 1 and marriedAdults > 1 %}
          {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id == person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.name}}{% endcapture%}
                {% assign addArray = addArray | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id != person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.name}}{% endcapture%}
                {% assign addArray = person.name_prefix | append: person.name | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% assign nameString = nameArray | join: ' & ' %}
          
          <!-- Single or unmarried adults -->
          {% elsif marriedAdults < 2 %}
          {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id == person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.name}}{% endcapture%}
                {% assign addArray = addArray | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% for person in household.adults %}
              {% assign personID = person.id | downcase %}
              {% if inclusionArray contains personID and household.primary_contact.id != person.id %}
                {% capture addArray %}{{person.name_prefix}} {{person.name}}{% endcapture%}
                {% assign addArray = person.name_prefix | append: person.name | split: '$' %}
                {% assign nameArray = nameArray | concat: addArray %}
              {% endif %}
            {% endfor %}
            {% assign nameString = nameArray | join: ', ' %}
                                    
            
          <!-- All other cases -->
          {% else %}
            {% assign nameString = household.name %}
        
        {% endif %}
        
        <!-- Generate Table -->
        <tr class="household">
          <td>{{ nameString }}</td>
          <td>{{ household.primary_address.street_line_1 }}</td>
          <td>{{ household.primary_address.street_line_2 }}</td>
          <td>{{ household.primary_address.city }}</td>
          <td>{{ household.primary_address.state }}</td>
          <td>{{ household.primary_address.zip }}</td>
          <td>{{ household.primary_address.location }}</td>
        </tr>
      
      {% endfor %}
          
          
        </tbody>
    </table>
    </div>
  </body>
  
  <script>
    var $table = $('#table')

    $(function() {
      $('#toolbar').find('select').change(function () {
        $table.bootstrapTable('destroy').bootstrapTable({
          exportDataType: $(this).val(),
          exportTypes: ['csv', 'excel', 'txt', 'xml', 'json', ]
        })
      }).trigger('change')
    })
  </script>
  
  
</html>