ResourceContract.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Livewire;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use Livewire\Attributes\Validate;
  6. use Livewire\Component;
  7. use Livewire\Attributes\Layout;
  8. class ResourceContract extends Component
  9. {
  10. public $resource;
  11. public $current_resource_contract = null;
  12. public $resource_contracts = [];
  13. public $contract_types = [];
  14. // #[Validate('required')]
  15. public $contract_type_id = null;
  16. public $contract_date = null;
  17. public $contract_number = '';
  18. public $position = '';
  19. public $role = '';
  20. public $start_date = null;
  21. public $end_date = null;
  22. public $notes = '';
  23. public $enabled = 1;
  24. public $is_edit = false;
  25. public function resetFields(){
  26. $this->contract_type_id = null;
  27. $this->contract_date = null;
  28. $this->contract_number = '';
  29. $this->position = '';
  30. $this->role = '';
  31. $this->start_date = null;
  32. $this->end_date = null;
  33. $this->notes = '';
  34. $this->enabled = 1;
  35. }
  36. public function mount()
  37. {
  38. $this->contract_types = \App\Models\ContractType::all();
  39. }
  40. public function render()
  41. {
  42. $this->resource_contracts = $this->resource->contracts ?? [];
  43. return view('livewire.resource_contract');
  44. }
  45. public function add()
  46. {
  47. $this->resetFields();
  48. $this->is_edit = true;
  49. $this->current_resource_contract = null;
  50. }
  51. public function edit($id)
  52. {
  53. $this->resetFields();
  54. $this->is_edit = true;
  55. $this->current_resource_contract = \App\Models\ResourceContract::findOrFail($id);
  56. $this->contract_type_id = $this->current_resource_contract->contract_type_id;
  57. $this->contract_date = $this->current_resource_contract->contract_date;
  58. $this->contract_number = $this->current_resource_contract->contract_number;
  59. $this->position = $this->current_resource_contract->position;
  60. $this->role = $this->current_resource_contract->role;
  61. $this->start_date = $this->current_resource_contract->start_date;
  62. $this->end_date = $this->current_resource_contract->end_date;
  63. $this->notes = $this->current_resource_contract->notes;
  64. $this->enabled = $this->current_resource_contract->enabled == 1;
  65. }
  66. public function save()
  67. {
  68. $this->validate();
  69. try
  70. {
  71. if ($this->current_resource_contract == null)
  72. {
  73. \App\Models\ResourceContract::create([
  74. 'resource_id' => $this->resource->id,
  75. 'contract_type_id' => $this->contract_type_id,
  76. 'contract_date' => $this->contract_date,
  77. 'contract_number' => $this->contract_number,
  78. 'position' => $this->position,
  79. 'role' => $this->role,
  80. 'start_date' => $this->start_date,
  81. 'end_date' => $this->end_date,
  82. 'notes' => $this->notes,
  83. 'enabled' => $this->enabled
  84. ]);
  85. }
  86. else
  87. {
  88. $this->current_resource_contract->update([
  89. 'contract_type_id' => $this->contract_type_id,
  90. 'contract_date' => $this->contract_date,
  91. 'contract_number' => $this->contract_number,
  92. 'position' => $this->position,
  93. 'role' => $this->role,
  94. 'start_date' => $this->start_date,
  95. 'end_date' => $this->end_date,
  96. 'notes' => $this->notes,
  97. 'enabled' => $this->enabled ? 1 : 0,
  98. ]);
  99. }
  100. session()->flash('success','Dati salvati con successo');
  101. $this->resetFields();
  102. $this->is_edit = false;
  103. } catch (\Exception $ex) {
  104. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  105. }
  106. }
  107. public function cancel()
  108. {
  109. $this->resetFields();
  110. $this->is_edit = false;
  111. $this->current_resource_contract = null;
  112. }
  113. }