PageRenderTime 19ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://github.com/nylen/wesabe-balances
PHP | 136 lines | 116 code | 20 blank | 0 comment | 0 complexity | d0cc0529f923eacb66cf17800c62d68f MD5 | raw file
  1. <?php
  2. $time = microtime(true);
  3. require_once 'settings.php';
  4. $link = mysql_connect($db_server, $db_username, $db_password)
  5. or die(mysql_error());
  6. mysql_select_db($db_database) or die(mysql_error());
  7. $result = mysql_query(<<<SQL
  8. select a.name, a.type, a.id,
  9. coalesce(b.balance, i.avail_cash + p.market_value) balance,
  10. coalesce(b.updated_at, greatest(i.created_at, p.updated_at)) updated_at
  11. from accounts a
  12. left join (
  13. select b.*
  14. from account_balances b
  15. inner join (
  16. select account_id, max(updated_at) updated_at
  17. from account_balances
  18. group by 1
  19. ) m
  20. on m.account_id = b.account_id
  21. and m.updated_at = b.updated_at
  22. ) b
  23. on a.id = b.account_id
  24. left join (
  25. select b.*
  26. from investment_balances b
  27. inner join (
  28. select account_id, max(created_at) created_at
  29. from investment_balances
  30. group by 1
  31. ) m
  32. on m.account_id = b.account_id
  33. and m.created_at = b.created_at
  34. ) i
  35. on a.id = i.account_id
  36. left join (
  37. select p.account_id,
  38. sum(p.market_value) market_value,
  39. max(p.updated_at) updated_at
  40. from investment_positions p
  41. inner join (
  42. select account_id, investment_security_id, max(updated_at) updated_at
  43. from investment_positions
  44. group by 1,2
  45. ) m
  46. on m.account_id = p.account_id
  47. and m.investment_security_id = p.investment_security_id
  48. and m.updated_at = p.updated_at
  49. group by 1
  50. ) p
  51. on a.id = p.account_id
  52. order by a.type, a.name
  53. SQL
  54. ) or die($mysql_error());
  55. ?>
  56. <!DOCTYPE html>
  57. <html>
  58. <head>
  59. <meta name="viewport" content="width=device-width" />
  60. <title>Account balances</title>
  61. <style type="text/css">
  62. body {
  63. font-family: Verdana, Arial, sans-serif;
  64. }
  65. .account {
  66. padding-bottom: 8px;
  67. margin-bottom: 8px;
  68. border-bottom: 2px solid #ccc;
  69. }
  70. .description {
  71. font-weight: bold;
  72. padding-right: 4px;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <?php
  78. $account_types = array(
  79. 'Account' => 'Regular',
  80. 'InvestmentAccount' => 'Investment'
  81. );
  82. $first = true;
  83. while($row = mysql_fetch_assoc($result)) {
  84. $type = $account_types[$row['type']];
  85. $balance = number_format($row['balance'], 2);
  86. $updated_at = date('n/d/Y g:i:s a', strtotime("$row[updated_at] UTC"));
  87. echo <<<HTML
  88. <div class="account">
  89. <table>
  90. <tr>
  91. <td class="description">Account:</td>
  92. <td class="content">$row[name]</td>
  93. </tr>
  94. <tr>
  95. <td class="description">Type:</td>
  96. <td class="content">$type</td>
  97. </tr>
  98. <tr>
  99. <td class="description">Balance:</td>
  100. <td class="content">\$$balance</td>
  101. </tr>
  102. <tr>
  103. <td class="description">As of:</td>
  104. <td class="content">$updated_at</td>
  105. </tr>
  106. </table>
  107. </div>
  108. HTML;
  109. }
  110. $time = number_format(microtime(true) - $time, 4);
  111. echo <<<HTML
  112. Page generated in ${time}s
  113. HTML;
  114. ?>
  115. </body>
  116. </html>