1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Sales</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript" src="jquery-jtemplates.js"></script>
  <link rel="stylesheet" href="style.css">
 
  <script type="text/javascript">
  <!--
    //global variables
    Items = null;
    Customers = null;
    Sales = null;
    
    //update selection of Customers and refresh entries
    function UpdateCustomer(elem, index) {
      Customers[index].selected = elem.checked;
      UpdatEntries();
    }
    
    //update selection of Items and refresh entries
    function UpdateItem(elem, index) {
      Items[index].selected = elem.checked;
      UpdatEntries();
    }
    
    //refresh entries
    function UpdatEntries() {
      //local key cache
      var CustCacheMap = {};
      var ItemCacheMap = {};
      
      //build key cache
      for(var i=0; i<Customers.length; ++i) {
        CustCacheMap[Customers[i].ID] = Customers[i];
      }
      for(var i=0; i<Items.length; ++i) {
        ItemCacheMap[Items[i].ID] = Items[i];
      }
      
      //create resultset from all Sales entries
      var entries = $.map(Sales, function(e) {
        //find customer and item
        var cust = CustCacheMap[e.CustomerID];
        var item = ItemCacheMap[e.ItemID];
        
        //if customer of item not found skip record
        if(cust == null || item == null) {
          return null;
        }
        
        //if both customer and item are selected then put record
        if(cust.selected && item.selected) {
          return [{
            "CustName": cust.FirstName + ' ' + cust.LastName,
            "ItemName": item.Name,
            "Price": e.SalesPrice,
            "Cost": e.UnitCost
          }];
        } else {
          return null;
        }
      });
      
      //process template
      $("#Entries").processTemplate(entries);
    }
    
    $(document).ready(function() {
      //load data
      $.getJSON('data.json', function(data) {
        Items = data.Items;
        Customers = data.Customers;
        Sales = data.SalesEntry;
        
        //setup templates
        $("#Customers").setTemplateElement("Template-ListCustomers").processTemplate(Customers);
        $("#Items").setTemplateElement("Template-ListItems").processTemplate(Items);
        
        $("#Entries").setTemplateElement("Template-Entries");
        $("#Entries").processTemplate(null); //process empty data to show header
      });
    });
  -->
  </script>
</head>
 
<body>
 
  <!-- Templates -->
  <p style="display:none"><textarea id="Template-ListCustomers" rows="0" cols="0"><!--
    <div class="title">Customers</div>
    <table>
      <tr>
        <td class="header">Choose</td>
        <td class="header">Name</td>
      </tr>
      {#foreach $T as Row}
        <tr class="{#cycle values=['bcEED','bcDEE']}">
          <td><input type="checkbox" value="{$T.Row.ID}" onclick="UpdateCustomer(this,{$T.Row$index})") /></td>
          <td>{$T.Row.FirstName} {$T.Row.LastName}</td>
        </tr>
      {#/for}
    </table>
  --></textarea></p>
    
  <p style="display:none"><textarea id="Template-ListItems" rows="0" cols="0"><!--
    <div class="title">Items</div>
    <table>
      <tr>
        <td class="header">Choose</td>
        <td class="header">Name</td>
      </tr>
      {#foreach $T as Row}
        <tr class="{#cycle values=['bcEED','bcDEE']}">
          <td><input type="checkbox" value="{$T.Row.ID}" onclick="UpdateItem(this,{$T.Row$index})") /></td>
          <td>{$T.Row.Name}</td>
        </tr>
      {#/for}
    </table>
  --></textarea></p>
  
  <p style="display:none"><textarea id="Template-Entries" rows="0" cols="0"><!--
    <div class="title">Entries</div>
    <table>
      <tr>
        <td class="header">Customer</td>
        <td class="header">Item</td>
        <td class="header">Price</td>
        <td class="header">Cost</td>
        <td class="header">Profit</td>
      </tr>
      {#param name=ProfitTotal value=0}
      {#foreach $T as Row}
        <tr class="{#cycle values=['bcEED','bcDEE']}">
          <td>{$T.Row.CustName}</td>
          <td>{$T.Row.ItemName}</td>
          <td>{$T.Row.Price}</td>
          <td>{$T.Row.Cost}</td>
          <td>{$T.Row.Price - $T.Row.Cost}</td>
        </tr>
      {#param name=ProfitTotal value=$P.ProfitTotal + ($T.Row.Price - $T.Row.Cost)}
      {#/for}
      <tr>
        <td class="header"></td>
        <td class="header"></td>
        <td class="header"></td>
        <td class="header">Total:</td>
        <td class="header">{$P.ProfitTotal}</td>
      </tr>
    </table>
  --></textarea></p>
 
  <!-- Output elements -->
  <div id="Customers" class="Content"></div>
  <div id="Items" class="Content"></div>
  <div id="Entries" class="Content"></div>
  
</body>
</html>