MemberArchive.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Illuminate\Support\Facades\Log;
  5. use DateTime;
  6. class MemberArchive extends Component
  7. {
  8. public $categories = [];
  9. public function mount()
  10. {
  11. $this->categories = [];
  12. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->orderBy('name')->get(), 0);
  13. }
  14. public function render()
  15. {
  16. return view('livewire.member_archive');
  17. }
  18. public function getCategories($records, $indentation)
  19. {
  20. foreach ($records as $record) {
  21. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  22. if (count($record->childs))
  23. $this->getCategories($record->childs, $indentation + 1);
  24. }
  25. }
  26. public function restoreMultiple($ids) {
  27. $success = true;
  28. foreach ($ids as $id) {
  29. $success = $this->restore($id, true);
  30. if (!$success) break;
  31. }
  32. if ($success) {
  33. session()->flash('success', 'Membri ripristinati con successo');
  34. $this->emit('reload');
  35. }
  36. }
  37. public function restore($id, $bulk = false)
  38. {
  39. try {
  40. $member = \App\Models\Member::findOrFail($id);
  41. $member->update([
  42. 'is_archived' => false,
  43. 'archived_date' => null,
  44. 'status' => '',
  45. 'enabled' => true
  46. ]);
  47. updateMemberData($id);
  48. if (!$bulk) {
  49. session()->flash('success', 'Membro ripristinato con successo');
  50. $this->emit('reload');
  51. }
  52. } catch (\Exception $e) {
  53. session()->flash('error', 'Errore durante il ripristino: ' . $e->getMessage());
  54. Log::error('Restore member error', [
  55. 'member_id' => $id,
  56. 'error' => $e->getMessage(),
  57. 'trace' => $e->getTraceAsString()
  58. ]);
  59. if ($bulk) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. public function archiveMultiple($ids) {
  66. $success = true;
  67. foreach ($ids as $id) {
  68. $success = $this->archive($id, true);
  69. if (!$success) break;
  70. }
  71. if ($success) {
  72. session()->flash('success', 'Membri archiviati con successo');
  73. $this->emit('reload');
  74. }
  75. }
  76. public function archive($id, $bulk = false)
  77. {
  78. try {
  79. $member = \App\Models\Member::findOrFail($id);
  80. $member->update([
  81. 'is_deleted' => true,
  82. 'deleted_date' => now(),
  83. 'first_name' => 'Nome Archiviato',
  84. 'last_name' => 'Cognome Archiviato',
  85. 'fiscal_code' => null,
  86. 'email' => null,
  87. 'phone' => null,
  88. 'phone2' => null,
  89. 'phone3' => null,
  90. 'address' => null,
  91. 'zip_code' => null,
  92. 'birth_place' => null,
  93. 'birth_date' => null,
  94. 'birth_city_id' => null,
  95. 'birth_province_id' => null,
  96. 'birth_nation_id' => null,
  97. 'nation_id' => null,
  98. 'province_id' => null,
  99. 'city_id' => null,
  100. 'document_type' => null,
  101. 'document_number' => null,
  102. 'document_from' => null,
  103. 'document_expire_date' => null,
  104. 'document_files' => null,
  105. 'father_name' => null,
  106. 'mother_name' => null,
  107. 'father_email' => null,
  108. 'mother_email' => null,
  109. 'father_phone' => null,
  110. 'mother_phone' => null,
  111. 'father_fiscal_code' => null,
  112. 'mother_fiscal_code' => null,
  113. 'father_doc_number' => null,
  114. 'father_doc_type' => null,
  115. 'mother_doc_number' => null,
  116. 'mother_doc_type' => null,
  117. 'father_document_files' => null,
  118. 'mother_document_files' => null,
  119. 'image' => null,
  120. 'gender' => null,
  121. 'status' => 'archived',
  122. 'exclude_from_records' => true,
  123. 'enabled' => false
  124. ]);
  125. updateMemberData($id);
  126. if (!$bulk) {
  127. session()->flash('success', 'Membro archiviato con successo');
  128. $this->emit('reload');
  129. }
  130. } catch (\Exception $e) {
  131. session()->flash('error', 'Errore durante l\'archiviazione: ' . $e->getMessage());
  132. Log::error('Archive member error', [
  133. 'member_id' => $id,
  134. 'error' => $e->getMessage(),
  135. 'trace' => $e->getTraceAsString()
  136. ]);
  137. if ($bulk) {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. }