Record.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 $months = [];
  14. public $years = [];
  15. public array $recordDatas = [];
  16. public array $labels = [];
  17. public $selectedFilterFromMonth;
  18. public $selectedFilterFromYear;
  19. public $selectedFilterToMonth;
  20. public $selectedFilterToYear;
  21. public function mount()
  22. {
  23. $this->selectedFilterFromMonth = date("n");
  24. $this->selectedFilterToMonth = date("n");
  25. $this->selectedFilterFromYear = date("Y");
  26. $this->selectedFilterToYear = date("Y");
  27. for($month = 1; $month <= 12; $month++)
  28. {
  29. $this->months[] = array('value' => $month, 'text' => $this->getMonth($month));
  30. }
  31. for($year = date("Y"); $year >= 2020; $year--)
  32. {
  33. $this->years[] = array('value' => $year, 'text' => $year);
  34. }
  35. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->get();
  36. }
  37. public function getMonth($m)
  38. {
  39. $ret = '';
  40. switch ($m) {
  41. case 1:
  42. $ret = 'Gennaio';
  43. break;
  44. case 2:
  45. $ret = 'Febbraio';
  46. break;
  47. case 3:
  48. $ret = 'Marzo';
  49. break;
  50. case 4:
  51. $ret = 'Aprile';
  52. break;
  53. case 5:
  54. $ret = 'Maggio';
  55. break;
  56. case 6:
  57. $ret = 'Giugno';
  58. break;
  59. case 7:
  60. $ret = 'Luglio';
  61. break;
  62. case 8:
  63. $ret = 'Agosto';
  64. break;
  65. case 9:
  66. $ret = 'Settembre';
  67. break;
  68. case 10:
  69. $ret = 'Ottobre';
  70. break;
  71. case 11:
  72. $ret = 'Novembre';
  73. break;
  74. case 12:
  75. $ret = 'Dicembre';
  76. break;
  77. default:
  78. $ret = '';
  79. break;
  80. }
  81. return $ret;
  82. }
  83. public function render()
  84. {
  85. $month = 0;
  86. $year = 0;
  87. $this->records = array();
  88. $this->totals = array();
  89. $fromDate = '';
  90. $toDate = '';
  91. if ($this->selectedFilterFromMonth != '' && $this->selectedFilterFromYear != '')
  92. {
  93. $fromDate = date($this->selectedFilterFromYear . "-" . $this->selectedFilterFromMonth . "-01");
  94. }
  95. if ($this->selectedFilterToMonth != '' && $this->selectedFilterToYear != '')
  96. {
  97. $toDate = date($this->selectedFilterToYear . "-" . $this->selectedFilterToMonth . "-t 23:59:59");
  98. }
  99. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  100. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  101. //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
  102. ->whereBetween('date', [$fromDate, $toDate])
  103. ->get();
  104. /*
  105. if ($this->selectedFilter == '')
  106. {
  107. $month = date("m");
  108. $year = date("Y");
  109. $fromDate = date("Y-m-01");
  110. $toDate = date("Y-m-t 23:59:59");
  111. // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  112. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  113. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  114. //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
  115. ->whereBetween('date', [$fromDate, $toDate])
  116. ->get();
  117. }
  118. else
  119. {
  120. if ($this->selectedFilter == '0')
  121. {
  122. $fromDate = date("Y-m-01");
  123. $toDate = date("Y-m-t 23:59:59");
  124. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  125. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  126. ->whereBetween('date', [$fromDate, $toDate])
  127. //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
  128. ->get();
  129. // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  130. }
  131. else if ($this->selectedFilter == '1')
  132. {
  133. $fromDate = date("Y-m-01", strtotime("-1 month"));
  134. $toDate = date("Y-m-t 23:59:59", strtotime("-1 month"));
  135. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  136. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  137. ->whereBetween('date', [$fromDate, $toDate])
  138. //->where('records_rows.when', 'like', '%' . date("m-Y", strtotime("-1 month")) . '%')
  139. ->get();
  140. // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  141. }
  142. else if ($this->selectedFilter == '2')
  143. {
  144. $fromDate = date("Y-01-01");
  145. $toDate = date("Y-12-31 23:59:59");
  146. // $datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  147. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  148. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  149. ->whereBetween('date', [$fromDate, $toDate])
  150. //->where('records_rows.when', 'like', '%' . date("m-Y", strtotime("-1 month")) . '%')
  151. ->get();
  152. }
  153. else
  154. {
  155. list($month, $year) = explode("-", $this->selectedFilter);
  156. $fromDate = date($year . "-" . $month . "-01");
  157. $toDate = date($year . "-" . $month . "-t 23:59:59");
  158. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  159. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  160. ->whereBetween('date', [$fromDate, $toDate])
  161. //->where('records_rows.when', 'like', '%' . $this->selectedFilter . '%')
  162. ->get();
  163. //$datas = \App\Models\Record::whereBetween('date', [$fromDate, $toDate])->with('member', 'supplier', 'causal', 'payment_method')->orderBy('date', 'DESC')->get();
  164. }
  165. }
  166. */
  167. /*
  168. $fromDate = date("Y-m-01");
  169. $toDate = date("Y-m-t 23:59:59");
  170. if ($this->selectedFilter == 1)
  171. {
  172. $month = date("m");
  173. $year = date("m");
  174. $fromDate = date("Y-01-01");
  175. $toDate = date("Y-12-31 23:59:59");
  176. }
  177. if ($this->selectedFilter == 2)
  178. {
  179. $fromDate = date("Y-01-01");
  180. $toDate = date("Y-12-31 23:59:59");
  181. }
  182. */
  183. // $
  184. foreach($datas as $idx => $data)
  185. {
  186. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  187. $paymentCheck = $data->payment_method->money;
  188. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
  189. {
  190. $amount = $data->amount;
  191. $amount += getVatValue($amount, $data->vat_id);
  192. /*$when = sizeof(json_decode($data->when));
  193. if ($when > 1)
  194. {
  195. $amount = $amount / $when;
  196. }*/
  197. $prefix = '';
  198. if (!$data->commercial)
  199. $prefix = $idx . "$";
  200. $causal = $prefix . $data->date . "§" . $causalCheck->getTree();
  201. if (isset($this->records[$causal]))
  202. {
  203. if (isset($this->records[$causal][$data->payment_method->name]))
  204. {
  205. if ($data->commercial)
  206. {
  207. if ($this->records[$causal][$data->payment_method->name][$data->type])
  208. $amount += $this->records[$causal][$data->payment_method->name][$data->type];
  209. }
  210. }
  211. }
  212. if (!isset($this->totals[$data->payment_method->name]))
  213. {
  214. $this->totals[$data->payment_method->name]["IN"] = 0;
  215. $this->totals[$data->payment_method->name]["OUT"] = 0;
  216. }
  217. $this->records[$causal][$data->payment_method->name][$data->type] = $amount;
  218. $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  219. }
  220. /*
  221. if ($data->causal->no_first == null || !$data->causal->no_first)
  222. {
  223. $amount = $data->amount;
  224. $prefix = '';
  225. if (!$data->commercial)
  226. $prefix = $idx . "$";
  227. $causal = $prefix . $data->date . "§" . $data->causal->getTree();
  228. if (isset($this->records[$causal]))
  229. {
  230. if (isset($this->records[$causal][$data->payment_method->name]))
  231. {
  232. if ($data->commercial)
  233. {
  234. if ($this->records[$causal][$data->payment_method->name][$data->type])
  235. $amount += $this->records[$causal][$data->payment_method->name][$data->type];
  236. }
  237. }
  238. }
  239. if (!isset($this->totals[$data->payment_method->name]))
  240. {
  241. $this->totals[$data->payment_method->name]["IN"] = 0;
  242. $this->totals[$data->payment_method->name]["OUT"] = 0;
  243. }
  244. $this->records[$causal][$data->payment_method->name][$data->type] = $amount;
  245. $this->totals[$data->payment_method->name][$data->type] += $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  246. }
  247. */
  248. }
  249. return view('livewire.records');
  250. }
  251. private function getLabels($fromDate, $toDate)
  252. {
  253. $begin = new DateTime($fromDate);
  254. $end = new DateTime($toDate);
  255. $interval = DateInterval::createFromDateString('1 day');
  256. $date_range = new DatePeriod($begin, $interval, $end);
  257. foreach ($date_range as $date)
  258. {
  259. $labels[] = $date->format('d/M');
  260. }
  261. return $labels;
  262. }
  263. private function getRecordData($type, $fromDate, $toDate)
  264. {
  265. $data = [];
  266. $begin = new DateTime($fromDate);
  267. $end = new DateTime($toDate);
  268. $interval = DateInterval::createFromDateString('1 day');
  269. $date_range = new DatePeriod($begin, $interval, $end);
  270. foreach ($date_range as $date)
  271. {
  272. if ($type == 'IN')
  273. {
  274. $found = false;
  275. foreach($this->in as $in)
  276. {
  277. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
  278. {
  279. $data[] = number_format($in->total, 0, "", "");
  280. $found = true;
  281. }
  282. }
  283. if (!$found)
  284. $data[] = 0;
  285. }
  286. if ($type == 'OUT')
  287. {
  288. $found = false;
  289. foreach($this->out as $out)
  290. {
  291. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
  292. {
  293. $data[] = number_format($out->total, 0, "", "");
  294. $found = true;
  295. }
  296. }
  297. if (!$found)
  298. $data[] = 0;
  299. }
  300. }
  301. return $data;
  302. }
  303. }