Record.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. class Record extends Component
  8. {
  9. public $records, $dataId, $totals;
  10. public $in;
  11. public $out;
  12. public $payments = [];
  13. public $period = [];
  14. public array $recordDatas = [];
  15. public array $labels = [];
  16. public $selectedFilter ;
  17. public function mount()
  18. {
  19. foreach(range(date("Y"), date("Y") - 1) as $year)
  20. {
  21. foreach(range(date("m"), 1) as $month)
  22. {
  23. $this->period[] = array('value' => $month . "-" . $year, 'text' => $this->getMonth($month) . " " . $year);
  24. }
  25. }
  26. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->get();
  27. }
  28. public function getMonth($m)
  29. {
  30. $ret = '';
  31. switch ($m) {
  32. case 1:
  33. $ret = 'Gennaio';
  34. break;
  35. case 2:
  36. $ret = 'Febbraio';
  37. break;
  38. case 3:
  39. $ret = 'Marzo';
  40. break;
  41. case 4:
  42. $ret = 'Aprile';
  43. break;
  44. case 5:
  45. $ret = 'Maggio';
  46. break;
  47. case 6:
  48. $ret = 'Giugno';
  49. break;
  50. case 7:
  51. $ret = 'Luglio';
  52. break;
  53. case 8:
  54. $ret = 'Agosto';
  55. break;
  56. case 9:
  57. $ret = 'Settembre';
  58. break;
  59. case 10:
  60. $ret = 'Ottobre';
  61. break;
  62. case 11:
  63. $ret = 'Novembre';
  64. break;
  65. case 12:
  66. $ret = 'Dicembre';
  67. break;
  68. default:
  69. $ret = '';
  70. break;
  71. }
  72. return $ret;
  73. }
  74. public function render()
  75. {
  76. $month = 0;
  77. $year = 0;
  78. if ($this->selectedFilter == '')
  79. {
  80. $month = date("m");
  81. $year = date("Y");
  82. }
  83. else
  84. {
  85. list($month, $year) = explode("-", $this->selectedFilter);
  86. }
  87. /*
  88. $fromDate = date("Y-m-01");
  89. $toDate = date("Y-m-t 23:59:59");
  90. if ($this->selectedFilter == 1)
  91. {
  92. $month = date("m");
  93. $year = date("m");
  94. $fromDate = date("Y-01-01");
  95. $toDate = date("Y-12-31 23:59:59");
  96. }
  97. if ($this->selectedFilter == 2)
  98. {
  99. $fromDate = date("Y-01-01");
  100. $toDate = date("Y-12-31 23:59:59");
  101. }
  102. */
  103. $this->records = array();
  104. $this->totals = array();
  105. // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  106. $datas = \App\Models\Record::where('month', $month)->where('year', $year)->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  107. foreach($datas as $idx => $data)
  108. {
  109. if ($data->causal->no_first == null || !$data->causal->no_first)
  110. {
  111. $amount = $data->amount;
  112. $prefix = '';
  113. if (!$data->commercial)
  114. $prefix = $idx . "$";
  115. $causal = $prefix . $data->date . "§" . $data->causal->getTree();
  116. if (isset($this->records[$causal]))
  117. {
  118. if (isset($this->records[$causal][$data->payment_method->name]))
  119. {
  120. if ($data->commercial)
  121. {
  122. if ($this->records[$causal][$data->payment_method->name][$data->type])
  123. $amount += $this->records[$causal][$data->payment_method->name][$data->type];
  124. }
  125. }
  126. }
  127. if (!isset($this->totals[$data->payment_method->name]))
  128. {
  129. $this->totals[$data->payment_method->name]["IN"] = 0;
  130. $this->totals[$data->payment_method->name]["OUT"] = 0;
  131. }
  132. $this->records[$causal][$data->payment_method->name][$data->type] = $amount;
  133. $this->totals[$data->payment_method->name][$data->type] += $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  134. }
  135. }
  136. // asort($this->records);
  137. /*
  138. $this->in = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  139. $this->out = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->select(\DB::raw("SUM(amount) as total, date"))->groupBy('date')->get();
  140. $this->labels = $this->getLabels($fromDate, $toDate);
  141. $this->recordDatas = [
  142. [
  143. 'label' => 'Entrate',
  144. 'backgroundColor' => 'green',
  145. 'borderColor' => 'green',
  146. // 'data' => $this->getRandomData(),
  147. 'data' => $this->getRecordData('IN', $fromDate, $toDate),
  148. ],
  149. [
  150. 'label' => 'Uscite',
  151. 'backgroundColor' => 'red',
  152. 'borderColor' => 'red',
  153. 'data' => $this->getRecordData('OUT', $fromDate, $toDate),
  154. ]
  155. ];
  156. */
  157. return view('livewire.records');
  158. }
  159. private function getLabels($fromDate, $toDate)
  160. {
  161. $begin = new DateTime($fromDate);
  162. $end = new DateTime($toDate);
  163. $interval = DateInterval::createFromDateString('1 day');
  164. $date_range = new DatePeriod($begin, $interval, $end);
  165. foreach ($date_range as $date)
  166. {
  167. $labels[] = $date->format('d/M');
  168. }
  169. return $labels;
  170. }
  171. private function getRecordData($type, $fromDate, $toDate)
  172. {
  173. $data = [];
  174. $begin = new DateTime($fromDate);
  175. $end = new DateTime($toDate);
  176. $interval = DateInterval::createFromDateString('1 day');
  177. $date_range = new DatePeriod($begin, $interval, $end);
  178. foreach ($date_range as $date)
  179. {
  180. if ($type == 'IN')
  181. {
  182. $found = false;
  183. foreach($this->in as $in)
  184. {
  185. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  186. {
  187. $data[] = number_format($in->total, 0, "", "");
  188. $found = true;
  189. }
  190. }
  191. if (!$found)
  192. $data[] = 0;
  193. }
  194. if ($type == 'OUT')
  195. {
  196. $found = false;
  197. foreach($this->out as $out)
  198. {
  199. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  200. {
  201. $data[] = number_format($out->total, 0, "", "");
  202. $found = true;
  203. }
  204. }
  205. if (!$found)
  206. $data[] = 0;
  207. }
  208. }
  209. return $data;
  210. }
  211. }