Track items sold and graph profit for player shop inventory.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.9 KiB

6 years ago
  1. {% extends "layout.html" %}
  2. {% block left_top_bar %}
  3. <button class="uk-button uk-button-default uk-button-small uk-border-rounded"
  4. uk-toggle="target: #charts-container">Show/Hide Charts</button>
  5. {% endblock %}
  6. {% block center_top_bar %}
  7. Total Funds Earned:
  8. <span id="total_funds_earned" class="uk-text-primary"></span>
  9. {% endblock %}
  10. {% block content %}
  11. <div id="charts-container" uk-grid hidden>
  12. <div class="uk-width-1-2">
  13. <canvas id="sales-chart"></canvas>
  14. </div>
  15. <div class="uk-width-1-2">
  16. <canvas id="stock-chart"></canvas>
  17. </div>
  18. </div>
  19. <table id="products" class="uk-table uk-table-small uk-table-striped uk-table-hover">
  20. <thead>
  21. <tr>
  22. <th class="uk-table-expand">Product Name</th>
  23. <th>Current Stock</th>
  24. <th>Total Stock</th>
  25. <th>Total Sold</th>
  26. <th>Total Earned</th>
  27. <th>Latest Price</th>
  28. <th>Last Update</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. {% for product in products %}
  33. {% if product.latest_entry != None %}
  34. <tr>
  35. <td class="uk-table-link">
  36. <a href="/shop/product/{{ product.id }}">
  37. {{ product.name }}
  38. </a>
  39. </td>
  40. <td>{{ product.latest_entry.stock }}</td>
  41. <td>{{ product.total_stocked }}</td>
  42. <td>{{ product.total_sold }}</td>
  43. <td>A${{ product.total_earned }}</td>
  44. <td>
  45. <span title="{{ product.latest_entry.raw_price}}">
  46. A${{ product.latest_entry.price }}
  47. </span>
  48. </td>
  49. <td class="uk-text-nowrap">{{ product.latest_entry.date.isoformat() }}</td>
  50. </tr>
  51. {% endif %}
  52. {% endfor %}
  53. </tbody>
  54. </table>
  55. {% endblock %}
  56. {% block pagescripts %}
  57. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
  58. <script>
  59. function highlight_empty_stock() {
  60. var rows = document.querySelectorAll('table#products > tbody > tr > td:nth-child(2)');
  61. for(var row of rows) {
  62. if(parseInt(row.textContent) == 0) {
  63. row.parentElement.classList.add('uk-text-danger');
  64. }
  65. }
  66. }
  67. function calculate_total_earnings() {
  68. var total_earnings = 0.00;
  69. var earnings = document.querySelectorAll('table#products > tbody > tr > td:nth-child(5)');
  70. for(var earning of earnings) {
  71. total_earnings += parseFloat(earning.textContent.substring(2))
  72. }
  73. document.getElementById('total_funds_earned').textContent = "A$" + total_earnings.toFixed(2);
  74. }
  75. /* Chart.js */
  76. var item_labels = [
  77. {% for product in products %}
  78. "{{ product.name }}",
  79. {% endfor %}
  80. ];
  81. var sale_data = [
  82. {% for product in products %}
  83. {{ product.total_sold }},
  84. {% endfor %}
  85. ];
  86. var earnings_data = [
  87. {% for product in products %}
  88. {{ product.total_earned }},
  89. {% endfor %}
  90. ];
  91. var total_stock_data = [
  92. {% for product in products %}
  93. {{ product.total_stocked }},
  94. {% endfor %}
  95. ];
  96. var current_stock_data = [
  97. {% for product in products %}
  98. {{ product.latest_entry.stock }},
  99. {% endfor %}
  100. ];
  101. function init_sales_chart(labels, sold_data, earned_data) {
  102. var ctx = document.getElementById("sales-chart").getContext('2d');
  103. var chart = new Chart(ctx, {
  104. type: 'horizontalBar',
  105. data: {
  106. labels: labels,
  107. datasets: [
  108. {
  109. label: 'Sold',
  110. backgroundColor: 'rgb(93, 173, 226)',
  111. borderColor: 'rgb(89, 131, 227)',
  112. data: sold_data,
  113. xAxisID: 'quantity'
  114. },
  115. {
  116. label: 'Earned',
  117. backgroundColor: 'rgb(173, 93, 226)',
  118. borderColor: 'rgb(131, 89, 227)',
  119. data: earned_data,
  120. xAxisID: 'currency'
  121. }
  122. ],
  123. },
  124. options: {
  125. elements: { rectangle: { borderWidth: 2, } },
  126. responsive: true,
  127. legend: { position: 'bottom', },
  128. title: { display: true, text: 'Sales & Earnings'},
  129. scales: {
  130. xAxes: [
  131. {
  132. id: 'quantity',
  133. type: 'linear',
  134. position: 'top'
  135. },
  136. {
  137. id: 'currency',
  138. type: 'linear',
  139. position: 'bottom'
  140. }
  141. ]
  142. }
  143. }
  144. });
  145. }
  146. function init_stock_chart(labels, total_data, current_data) {
  147. var ctx = document.getElementById("stock-chart").getContext('2d');
  148. var chart = new Chart(ctx, {
  149. type: 'horizontalBar',
  150. data: {
  151. labels: labels,
  152. datasets: [
  153. {
  154. label: 'Total',
  155. backgroundColor: 'rgb(54, 84, 126)',
  156. data: total_data,
  157. xAxisID: 'quantity'
  158. },
  159. {
  160. label: 'Current',
  161. backgroundColor: 'rgb(73, 133, 226)',
  162. data: current_data,
  163. xAxisID: 'big_quantity'
  164. }
  165. ],
  166. },
  167. options: {
  168. responsive: true,
  169. legend: { position: 'bottom', },
  170. title: { display: true, text: 'Total/Current Stock'},
  171. scales: {
  172. xAxes: [
  173. {
  174. id: 'quantity',
  175. type: 'linear',
  176. position: 'top'
  177. },
  178. {
  179. id: 'big_quantity',
  180. type: 'linear',
  181. position: 'bottom'
  182. }
  183. ]
  184. }
  185. }
  186. });
  187. }
  188. /* On Page Ready */
  189. document.addEventListener("DOMContentLoaded", function(event) {
  190. highlight_empty_stock();
  191. calculate_total_earnings();
  192. init_sales_chart(item_labels, sale_data, earnings_data);
  193. init_stock_chart(item_labels, total_stock_data, current_stock_data);
  194. });
  195. </script>
  196. {% endblock %}