Record.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. class Record extends Component
  10. {
  11. public $records, $dataId, $totals;
  12. public $in;
  13. public $out;
  14. public $payments = [];
  15. public $fromDate;
  16. public $toDate;
  17. public $filterCausals = null;
  18. public $filterMember = null;
  19. public array $recordDatas = [];
  20. public array $labels = [];
  21. public array $causals = [];
  22. public $members = array();
  23. public function hydrate()
  24. {
  25. $this->emit('load-select');
  26. }
  27. public function mount()
  28. {
  29. $this->fromDate = date("Y-m-d");
  30. $this->toDate = date("Y-m-d");
  31. $this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
  32. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  33. $this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
  34. }
  35. public function getCausals($records, $indentation)
  36. {
  37. foreach ($records as $record) {
  38. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  39. if (count($record->childs))
  40. $this->getCausals($record->childs, $indentation + 1);
  41. }
  42. }
  43. public function getMonth($m)
  44. {
  45. $ret = '';
  46. switch ($m) {
  47. case 1:
  48. $ret = 'Gennaio';
  49. break;
  50. case 2:
  51. $ret = 'Febbraio';
  52. break;
  53. case 3:
  54. $ret = 'Marzo';
  55. break;
  56. case 4:
  57. $ret = 'Aprile';
  58. break;
  59. case 5:
  60. $ret = 'Maggio';
  61. break;
  62. case 6:
  63. $ret = 'Giugno';
  64. break;
  65. case 7:
  66. $ret = 'Luglio';
  67. break;
  68. case 8:
  69. $ret = 'Agosto';
  70. break;
  71. case 9:
  72. $ret = 'Settembre';
  73. break;
  74. case 10:
  75. $ret = 'Ottobre';
  76. break;
  77. case 11:
  78. $ret = 'Novembre';
  79. break;
  80. case 12:
  81. $ret = 'Dicembre';
  82. break;
  83. default:
  84. $ret = '';
  85. break;
  86. }
  87. return $ret;
  88. }
  89. public function render()
  90. {
  91. $month = 0;
  92. $year = 0;
  93. $this->records = array();
  94. $this->totals = array();
  95. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  96. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  97. ->select('records.*', 'records_rows.*') // Ensure all columns are selected
  98. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  99. ->whereBetween('date', [$this->fromDate, $this->toDate])
  100. ->where(function ($query) {
  101. $query->where('type', 'OUT')
  102. ->orWhere(function ($query) {
  103. $query->where('records.corrispettivo_fiscale', true)
  104. ->orWhere('records.commercial', false);
  105. });
  106. })
  107. ->where(function ($query) use ($exclude_from_records) {
  108. $query->where('type', 'OUT')
  109. ->orWhere(function ($subquery) use ($exclude_from_records) {
  110. $subquery->whereNotIn('member_id', $exclude_from_records);
  111. });
  112. });
  113. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  114. $causals = array();
  115. foreach ($this->filterCausals as $z) {
  116. $causals[] = $z;
  117. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  118. foreach ($childs as $c) {
  119. $causals[] = $c->id;
  120. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  121. foreach ($childsX as $cX) {
  122. $causals[] = $cX->id;
  123. }
  124. }
  125. }
  126. $datas->whereIn('causal_id', $causals);
  127. }
  128. if ($this->filterMember != null && $this->filterMember > 0) {
  129. $datas->where('member_id', $this->filterMember);
  130. }
  131. $datas = $datas->orderBy('date', 'ASC')
  132. ->orderBy('records.created_at', 'ASC')
  133. ->orderBy('records_rows.id', 'ASC') // Important to maintain row order
  134. ->get();
  135. foreach ($datas as $idx => $data) {
  136. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  137. $paymentCheck = $data->payment_method->money;
  138. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
  139. if (!$data->deleted) {
  140. $amount = $data->amount;
  141. $amount += getVatValue($amount, $data->vat_id);
  142. } else {
  143. $amount = $data->amount;
  144. }
  145. $prefix = '';
  146. if (!$data->commercial)
  147. $prefix = $idx . "$";
  148. // aggiungere il nome * * *
  149. $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" .
  150. ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") :
  151. $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '') .
  152. "§" . $data->numero_linea;
  153. if (!isset($this->records[$causal][$data->payment_method->name][$data->type])) {
  154. $this->records[$causal][$data->payment_method->name][$data->type] = 0;
  155. }
  156. // Add to the records array
  157. $this->records[$causal][$data->payment_method->name][$data->type] += $amount;
  158. // Initialize totals if needed
  159. if (!isset($this->totals[$data->payment_method->name])) {
  160. $this->totals[$data->payment_method->name]["IN"] = 0;
  161. $this->totals[$data->payment_method->name]["OUT"] = 0;
  162. }
  163. // Update totals if not deleted
  164. if (!$data->deleted)
  165. $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
  166. }
  167. }
  168. return view('livewire.records');
  169. }
  170. private function getLabels($fromDate, $toDate)
  171. {
  172. $begin = new DateTime($fromDate);
  173. $end = new DateTime($toDate);
  174. $interval = DateInterval::createFromDateString('1 day');
  175. $date_range = new DatePeriod($begin, $interval, $end);
  176. foreach ($date_range as $date) {
  177. $labels[] = $date->format('d/M');
  178. }
  179. return $labels;
  180. }
  181. private function getRecordData($type, $fromDate, $toDate)
  182. {
  183. $data = [];
  184. $begin = new DateTime($fromDate);
  185. $end = new DateTime($toDate);
  186. $interval = DateInterval::createFromDateString('1 day');
  187. $date_range = new DatePeriod($begin, $interval, $end);
  188. foreach ($date_range as $date) {
  189. if ($type == 'IN') {
  190. $found = false;
  191. foreach ($this->in as $in) {
  192. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d')) {
  193. $data[] = number_format($in->total, 0, "", "");
  194. $found = true;
  195. }
  196. }
  197. if (!$found)
  198. $data[] = 0;
  199. }
  200. if ($type == 'OUT') {
  201. $found = false;
  202. foreach ($this->out as $out) {
  203. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d')) {
  204. $data[] = number_format($out->total, 0, "", "");
  205. $found = true;
  206. }
  207. }
  208. if (!$found)
  209. $data[] = 0;
  210. }
  211. }
  212. return $data;
  213. }
  214. public function export()
  215. {
  216. $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
  217. $spreadsheet = new Spreadsheet();
  218. $activeWorksheet = $spreadsheet->getActiveSheet();
  219. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  220. $activeWorksheet->setCellValue('A1', "Data");
  221. $activeWorksheet->setCellValue('B1', "Causale");
  222. $activeWorksheet->setCellValue('C1', "Dettaglio");
  223. $activeWorksheet->setCellValue('D1', "Nominativo");
  224. $activeWorksheet->setCellValue('E1', "Stato");
  225. $idx = 0;
  226. foreach ($this->payments as $p) {
  227. if ($idx >= count($letters)) {
  228. break;
  229. }
  230. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  231. $idx++;
  232. if ($idx >= count($letters)) {
  233. break;
  234. }
  235. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  236. $idx++;
  237. }
  238. $idx = 0;
  239. $activeWorksheet->setCellValue('A2', "");
  240. $activeWorksheet->setCellValue('B2', "");
  241. $activeWorksheet->setCellValue('C2', "");
  242. $activeWorksheet->setCellValue('D2', "");
  243. $activeWorksheet->setCellValue('E2', "");
  244. foreach ($this->payments as $p) {
  245. if ($p->type == 'ALL') {
  246. if ($idx >= count($letters)) {
  247. break;
  248. }
  249. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  250. $idx++;
  251. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  252. $idx++;
  253. } elseif ($p->type == 'IN') {
  254. if ($idx >= count($letters)) {
  255. break;
  256. }
  257. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  258. $idx++;
  259. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  260. $idx++;
  261. } elseif ($p->type == 'OUT') {
  262. if ($idx >= count($letters)) {
  263. break;
  264. }
  265. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  266. $idx++;
  267. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  268. $idx++;
  269. }
  270. }
  271. $activeWorksheet->getStyle('A1:Q1')->getFont()->setBold(true);
  272. $activeWorksheet->getStyle('A2:Q2')->getFont()->setBold(true);
  273. // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
  274. $count = 3;
  275. $totals = [];
  276. foreach ($this->records as $causal => $record) {
  277. $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
  278. list($d, $c, $j, $k, $deleted, $numeroLinea) = explode("§", $check);
  279. $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
  280. $activeWorksheet->setCellValue('B' . $count, $c);
  281. $activeWorksheet->setCellValue('C' . $count, $k);
  282. $activeWorksheet->setCellValue('D' . $count, $j);
  283. $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
  284. $activeWorksheet->setCellValue('E' . $count, $stato);
  285. if ($stato === 'ANNULLATA') {
  286. $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
  287. }
  288. $idx = 0;
  289. foreach ($this->payments as $p) {
  290. if (isset($record[$p->name])) {
  291. if (isset($record[$p->name]["IN"])) {
  292. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
  293. } else {
  294. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  295. }
  296. $idx++;
  297. if (isset($record[$p->name]["OUT"])) {
  298. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
  299. } else {
  300. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  301. }
  302. $idx++;
  303. } else {
  304. if ($idx >= count($letters)) {
  305. break;
  306. }
  307. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  308. $idx++;
  309. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  310. $idx++;
  311. }
  312. }
  313. $count += 1;
  314. }
  315. $count += 1;
  316. $idx = 0;
  317. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  318. $activeWorksheet->setCellValue('B' . $count, '');
  319. $activeWorksheet->setCellValue('C' . $count, '');
  320. $activeWorksheet->setCellValue('D' . $count, '');
  321. $activeWorksheet->setCellValue('E' . $count, '');
  322. foreach ($this->payments as $p) {
  323. if (isset($this->totals[$p->name])) {
  324. if ($p->type == 'ALL') {
  325. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  326. $idx++;
  327. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  328. $idx++;
  329. } elseif ($p->type == 'IN') {
  330. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
  331. $idx++;
  332. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  333. $idx++;
  334. } elseif ($p->type == 'OUT') {
  335. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  336. $idx++;
  337. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
  338. $idx++;
  339. }
  340. } else {
  341. if ($p->type == 'ALL') {
  342. if ($idx >= count($letters)) {
  343. break;
  344. }
  345. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  346. $idx++;
  347. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  348. $idx++;
  349. } elseif ($p->type == 'IN') {
  350. if ($idx >= count($letters)) {
  351. break;
  352. }
  353. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  354. $idx++;
  355. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  356. $idx++;
  357. } elseif ($p->type == 'OUT') {
  358. if ($idx >= count($letters)) {
  359. break;
  360. }
  361. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  362. $idx++;
  363. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  364. $idx++;
  365. }
  366. }
  367. }
  368. $activeWorksheet->getStyle('A' . $count . ':Q' . $count)->getFont()->setBold(true);
  369. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  370. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  371. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  372. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  373. $activeWorksheet->getColumnDimension('E')->setWidth(20);
  374. foreach ($letters as $l)
  375. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  376. $writer = new Xlsx($spreadsheet);
  377. $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
  378. return response()->download($path)->deleteFileAfterSend();
  379. }
  380. }