Project.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Validate;
  5. class Project extends Component
  6. {
  7. public $projects;
  8. #[Validate('required')]
  9. public $name = '';
  10. public $code = '';
  11. public $customer_id = null;
  12. public $company_service_id = null;
  13. public $description = '';
  14. public $start_date = null;
  15. public $end_date = null;
  16. public $project_manager_id = null;
  17. public $project_value = 0;
  18. public $hours = 0;
  19. public $status = 0;
  20. public $billable = 0;
  21. public $enabled;
  22. public $current_project = null;
  23. public $is_edit = false;
  24. public function resetFields(){
  25. $this->name = '';
  26. $this->code = '';
  27. $this->customer_id = null;
  28. $this->company_service_id = null;
  29. $this->description = '';
  30. $this->start_date = null;
  31. $this->end_date = null;
  32. $this->project_manager_id = null;
  33. $this->project_value = 0;
  34. $this->hours = 0;
  35. $this->status = 0;
  36. $this->billable = 0;
  37. $this->enabled = true;
  38. }
  39. public function render()
  40. {
  41. // Get Customers
  42. $this->projects = \App\Models\Project::all();
  43. return view('livewire.project');
  44. }
  45. public function add()
  46. {
  47. $this->resetFields();
  48. $this->is_edit = true;
  49. $this->current_project = null;
  50. }
  51. public function edit($id)
  52. {
  53. $this->resetFields();
  54. $this->is_edit = true;
  55. $this->current_project = \App\Models\Project::findOrFail($id);
  56. $this->name = $this->current_project->name;
  57. $this->code = $this->current_project->code;
  58. $this->customer_id = $this->current_project->customer_id;
  59. $this->company_service_id = $this->current_project->company_service_id;
  60. $this->description = $this->current_project->description;
  61. $this->start_date = $this->current_project->start_date;
  62. $this->end_date = $this->current_project->end_date;
  63. $this->project_manager_id = $this->current_project->project_manager_id;
  64. $this->project_value = $this->current_project->project_value;
  65. $this->hours = $this->current_project->hours;
  66. $this->status = $this->current_project->status;
  67. $this->billable = $this->current_project->billable;
  68. $this->enabled = $this->current_customer->enabled;
  69. }
  70. public function save()
  71. {
  72. $this->validate();
  73. try
  74. {
  75. if ($this->current_project == null)
  76. {
  77. \App\Models\Project::create([
  78. 'name' => $this->name,
  79. 'code' => $this->code,
  80. 'customer_id' => $this->customer_id,
  81. 'company_service_id' => $this->company_service_id,
  82. 'description' => $this->description,
  83. 'start_date' => $this->start_date,
  84. 'end_date' => $this->end_date,
  85. 'project_manager_id' => $this->project_manager_id,
  86. 'project_value' => $this->project_value,
  87. 'hours' => $this->hours,
  88. 'status' => $this->status,
  89. 'billable' => $this->billable,
  90. 'enabled' => $this->enabled
  91. ]);
  92. }
  93. else
  94. {
  95. $this->current_project->update([
  96. 'name' => $this->name,
  97. 'code' => $this->code,
  98. 'customer_id' => $this->customer_id,
  99. 'company_service_id' => $this->company_service_id,
  100. 'description' => $this->description,
  101. 'start_date' => $this->start_date,
  102. 'end_date' => $this->end_date,
  103. 'project_manager_id' => $this->project_manager_id,
  104. 'project_value' => $this->project_value,
  105. 'hours' => $this->hours,
  106. 'status' => $this->status,
  107. 'billable' => $this->billable,
  108. 'enabled' => $this->enabled,
  109. ]);
  110. }
  111. session()->flash('success','Dati salvati con successo');
  112. $this->resetFields();
  113. $this->is_edit = false;
  114. } catch (\Exception $ex) {
  115. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  116. }
  117. }
  118. public function cancel()
  119. {
  120. $this->resetFields();
  121. $this->is_edit = false;
  122. $this->current_company = null;
  123. }
  124. }