Receipt.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Barryvdh\DomPDF\Facade\Pdf;
  5. use Illuminate\Support\Facades\Log;
  6. use SimpleXMLElement;
  7. class Receipt extends Component
  8. {
  9. public $records;
  10. public $filterStatus = '';
  11. public $hasFilter = false;
  12. public $filterFrom = '', $filterTo = '';
  13. public function updatedFilterFrom()
  14. {
  15. $this->emit('destroy-data-table');
  16. }
  17. public function updatedFilterTo()
  18. {
  19. $this->emit('destroy-data-table');
  20. }
  21. public function render()
  22. {
  23. if ($this->hasFilter)
  24. {
  25. $r = \App\Models\Receipt::with('member');
  26. if ($this->filterStatus != '')
  27. $r = $r->where('status', $this->filterStatus);
  28. if ($this->filterFrom != '')
  29. $r = $r->where('date', '>=', $this->filterFrom);
  30. if ($this->filterTo != '')
  31. $r = $r->where('date', '<=', $this->filterTo);
  32. $this->records = $r->get();
  33. }
  34. else
  35. {
  36. $this->records = \App\Models\Receipt::with('member')->get();
  37. }
  38. $this->emit('load-data-table');
  39. return view('livewire.receipt');
  40. }
  41. public function printReceipt($id)
  42. {
  43. $this->emit('load-data-table');
  44. $receipt = \App\Models\Receipt::findOrFail($id);
  45. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  46. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));//->output();
  47. return $pdf->stream('aaa.pdf');
  48. }
  49. public function importReceipts()
  50. {
  51. $this->validate([
  52. 'receiptFile' => 'required|mimes:xml|max:2048',
  53. ]);
  54. try {
  55. $xmlString = file_get_contents($this->receiptFile->getRealPath());
  56. $xml = new SimpleXMLElement($xmlString);
  57. Log::info('XML Data: ' . print_r($xml, true));
  58. // Extract data from XML and create receipts
  59. foreach ($xml->receipt as $receiptData) {
  60. Log::info('Receipt Data: ' . print_r($receiptData, true));
  61. $member = \App\Models\Member::where('fiscal_code', (string)$receiptData->fiscal_code)->first();
  62. if ($member) {
  63. $receipt = new \App\Models\Receipt();
  64. $receipt->member_id = $member->id;
  65. $receipt->date = (string)$receiptData->date;
  66. $receipt->amount = (float)$receiptData->amount;
  67. $receipt->number = (string)$receiptData->number;
  68. $receipt->year = (int)$receiptData->year;
  69. $receipt->save();
  70. }
  71. }
  72. session()->flash('message', 'Ricevute importate con successo.');
  73. } catch (\Exception $e) {
  74. session()->flash('error', 'Errore durante l\'importazione del file XML: ' . $e->getMessage());
  75. }
  76. $this->reset('receiptFile');
  77. $this->emit('load-data-table');
  78. }
  79. public function search()
  80. {
  81. $this->hasFilter = true;
  82. }
  83. public function disableSearch()
  84. {
  85. $this->filterStatus = "";
  86. $this->filterTo = '';
  87. $this->filterFrom = '';
  88. $this->hasFilter = false;
  89. }
  90. }