HasRichieste.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Http\Livewire\Traits;
  3. use App\Models\ReportRichiesta;
  4. use Illuminate\Support\Facades\Validator;
  5. trait HasRichieste{
  6. public $richiestaId = 0;
  7. public $richiestaAnagrafica = 0;
  8. public $richiestaData = null;
  9. public $richiestaConsegna = null;
  10. public $richiestaText = '';
  11. public $richieste;
  12. protected function getRichiestaRules(){
  13. return [
  14. 'richiestaAnagrafica' => 'required|min:1',
  15. 'richiestaData' => 'required|date',
  16. 'richiestaConsegna' => 'required|date',
  17. ];
  18. }
  19. protected function getRichiestaMessages(){
  20. return [
  21. 'richiestaData.required' => 'Il campo data richiesta è obbligatorio.',
  22. 'richiestaData.date' => 'Il campo data richiesta deve essere una data valida.',
  23. 'richiestaConsegna.required' => 'Il campo data consegna è obbligatorio.',
  24. 'richiestaConsegna.date' => 'Il campo data consegna deve essere una data valida.',
  25. ];
  26. }
  27. public function saveRichiesta(){
  28. $this->validate($this->getRichiestaRules(), $this->getRichiestaMessages());
  29. if ($this->richiestaId > 0) {
  30. $this->updateExistingRichiesta();
  31. } else {
  32. $this->createNewRichiesta();
  33. }
  34. $this->resetRichiestaFields();
  35. $this->refreshRichieste();
  36. }
  37. protected function updateExistingRichiesta(){
  38. ReportRichiesta::where('id', $this->richiestaId)->update([
  39. 'anagrafica_id' => $this->richiestaAnagrafica,
  40. 'data_richiesta' => $this->richiestaData,
  41. 'consegna_richiesta' => $this->richiestaConsegna,
  42. 'description' => $this->richiestaText,
  43. 'state' => 0
  44. ]);
  45. }
  46. protected function createNewRichiesta(){
  47. ReportRichiesta::create([
  48. 'report_id' => $this->dataId,
  49. 'anagrafica_id' => $this->richiestaAnagrafica,
  50. 'data_richiesta' => $this->richiestaData,
  51. 'consegna_richiesta' => $this->richiestaConsegna,
  52. 'description' => $this->richiestaText,
  53. 'state' => 0,
  54. 'created' => date("Y-m-d H:i:s"),
  55. 'created_by' => 0
  56. ]);
  57. }
  58. protected function resetRichiestaFields(){
  59. $this->richiestaId = 0;
  60. $this->richiestaAnagrafica = 0;
  61. $this->richiestaData = null;
  62. $this->richiestaConsegna = null;
  63. $this->richiestaText = '';
  64. }
  65. protected function refreshRichieste(){
  66. $this->richieste = ReportRichiesta::where('report_id', $this->dataId)->get();
  67. }
  68. public function editRichiesta($id){
  69. $richiesta = ReportRichiesta::where('id', $id)->first();
  70. if ($richiesta) {
  71. $this->richiestaId = $id;
  72. $this->richiestaAnagrafica = $richiesta->anagrafica_id;
  73. $this->richiestaData = $richiesta->data_richiesta;
  74. $this->richiestaConsegna = $richiesta->consegna_richiesta;
  75. $this->richiestaText = $richiesta->description;
  76. }
  77. }
  78. public function removeRichiesta($id){
  79. ReportRichiesta::findOrFail($id)->delete();
  80. $this->refreshRichieste();
  81. }
  82. }