index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // Icons from https://icon-icons.com/fr/pack/BigMug-Line-icons/935
  3. global $account;
  4. $period = Period::LoadPendingPeriod();
  5. $events = [];
  6. if ($period)
  7. $events = Event::LoadForPeriod($period);
  8. class Total {
  9. public function Total() {
  10. }
  11. public function add($event) {
  12. $amount = $event->getAmount();
  13. $beneficiaryId = $event->getBeneficiary()->getId();
  14. if (!isset($this->beneficiaries[$beneficiaryId]))
  15. $this->beneficiaries[$beneficiaryId] = $amount;
  16. else
  17. $this->beneficiaries[$beneficiaryId] += $amount;
  18. foreach ($event->getContext() as $ctx) {
  19. if (!isset($this->contexts[$ctx->getId()]))
  20. $this->contexts[$ctx->getId()] = $amount;
  21. else
  22. $this->contexts[$ctx->getId()] += $amount;
  23. }
  24. $this->total += $amount;
  25. }
  26. public function getTotal() {
  27. return $this->total;
  28. }
  29. public function getBeneficiaries() {
  30. $result = array();
  31. foreach ($this->beneficiaries as $i => $amount)
  32. array_push($result, array(Beneficiary::Get($i), $amount));
  33. usort($result, function($a, $b) { return $b[1] -$a[1]; });
  34. return $result;
  35. }
  36. public function getContexts() {
  37. $result = array();
  38. foreach ($this->contexts as $i => $amount) {
  39. array_push($result, array(Context::Get($i), $amount));
  40. }
  41. usort($result, function($a, $b) { return $b[1] -$a[1]; });
  42. return $result;
  43. }
  44. private $beneficiaries = array();
  45. private $contexts = array();
  46. private $total = 0;
  47. }
  48. function printContext($ctx) {
  49. $result = "<ul style='padding: 0;'>";
  50. usort($ctx, function($a, $b) { return strcasecmp($a->getLabel(), $b->getLabel()); });
  51. foreach ($ctx as $i) {
  52. $label = $i->getLabel();
  53. $result .= "<li class='badge badge-info' style='margin-left: 5px;'>${label}</li>";
  54. }
  55. $result .= "</ul>";
  56. return $result;
  57. }
  58. function printEvent($e, $totalAmount) {
  59. $id = $e->getId();
  60. $amount = $e->getAmount();
  61. $date = $e->getPaymentDate()->format("d/m/Y");
  62. $label = $e->getLabel();
  63. $beneficiary = $e->getBeneficiary()->getLabel();
  64. $context = printContext($e->getContext());
  65. echo "<tr><td>${date}</td>";
  66. echo "<td>${label}</td>";
  67. echo "<td>${beneficiary}</td>";
  68. echo "<td>${context}</td>";
  69. echo "<td>${amount}</td>";
  70. echo "<td>
  71. <a href='event/edit?id=${id}'><img alt='Edit' class='icon' src='public/img/note-outlined-symbol_icon-icons.com_73198.svg'/></a>
  72. <a href='event/add?from=${id}'><img alt='Duplicate' class='icon' src='public/img/copy-two-paper-sheets-interface-symbol_icon-icons.com_73283.svg'/></a>
  73. <a href='event/delete?id=${id}'><img alt='Delete' class='icon' src='public/img/recycling-bin_icon-icons.com_73179.svg'/></a>
  74. </td></tr>";
  75. $totalAmount->add($e);
  76. }
  77. $totalAmount = new Total();
  78. ?><!DOCTYPE html5><html><head>
  79. <link rel="stylesheet" href="public/css/bootstrap.min.css" />
  80. <link rel="stylesheet" href="public/css/style.css" />
  81. </head><body>
  82. <div class="container">
  83. <table class="table table-striped table-hover"><tr><th>Date</th><th>Label</th><th>Beneficiary</th><th>Context</th><th>Amount</th><th>Action</th></tr>
  84. <?php foreach ($events as $i) {
  85. printEvent($i, $totalAmount);
  86. } ?>
  87. <tr><td colspan=4>Total</td><td><?php echo $totalAmount->getTotal(); ?></td><td></td></tr>
  88. </table>
  89. <button onclick="javascript:document.location.href='event/add'" class="btn btn-primary">Add</button></div>
  90. <div class="container"><div class="row"><div class="col-sm">
  91. <table class="table table-striped table-hover"><tr><th>Beneficiary</th><th>Amount</th></tr>
  92. <?php
  93. foreach ($totalAmount->getBeneficiaries() as $i) {
  94. echo "<tr><td>" .$i[0]->getLabel() ."</td><td>" .$i[1] ."</td></tr>";
  95. }
  96. ?>
  97. </table></div><div class="col-sm">
  98. <table class="table table-striped table-hover"><tr><th>Context</th><th>Amount</th></tr>
  99. <?php
  100. foreach ($totalAmount->getContexts() as $i) {
  101. echo "<tr><td>" .$i[0]->getLabel() ."</td><td>" .$i[1] ."</td></tr>";
  102. }
  103. ?>
  104. </table></div></div></div>
  105. <script src="public/js/jquery-3.3.1.slim.min.js"></script>
  106. <script src="public/js/popper.min.js"></script>
  107. <script src="public/js/bootstrap.min.js"></script>
  108. </body></html>