| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Http\Livewire\Traits;
- use App\Models\ReportRichiesta;
- use Illuminate\Support\Facades\Validator;
- trait HasRichieste{
- public $richiestaId = 0;
- public $richiestaAnagrafica = 0;
- public $richiestaData = null;
- public $richiestaConsegna = null;
- public $richiestaText = '';
- public $richieste;
- protected function getRichiestaRules(){
- return [
- 'richiestaAnagrafica' => 'required|min:1',
- 'richiestaData' => 'required|date',
- 'richiestaConsegna' => 'required|date',
- ];
- }
- protected function getRichiestaMessages(){
- return [
- 'richiestaData.required' => 'Il campo data richiesta è obbligatorio.',
- 'richiestaData.date' => 'Il campo data richiesta deve essere una data valida.',
- 'richiestaConsegna.required' => 'Il campo data consegna è obbligatorio.',
- 'richiestaConsegna.date' => 'Il campo data consegna deve essere una data valida.',
- ];
- }
- public function saveRichiesta(){
- $this->validate($this->getRichiestaRules(), $this->getRichiestaMessages());
- if ($this->richiestaId > 0) {
- $this->updateExistingRichiesta();
- } else {
- $this->createNewRichiesta();
- }
- $this->resetRichiestaFields();
- $this->refreshRichieste();
- }
- protected function updateExistingRichiesta(){
- ReportRichiesta::where('id', $this->richiestaId)->update([
- 'anagrafica_id' => $this->richiestaAnagrafica,
- 'data_richiesta' => $this->richiestaData,
- 'consegna_richiesta' => $this->richiestaConsegna,
- 'description' => $this->richiestaText,
- 'state' => 0
- ]);
- }
- protected function createNewRichiesta(){
- ReportRichiesta::create([
- 'report_id' => $this->dataId,
- 'anagrafica_id' => $this->richiestaAnagrafica,
- 'data_richiesta' => $this->richiestaData,
- 'consegna_richiesta' => $this->richiestaConsegna,
- 'description' => $this->richiestaText,
- 'state' => 0,
- 'created' => date("Y-m-d H:i:s"),
- 'created_by' => 0
- ]);
- }
- protected function resetRichiestaFields(){
- $this->richiestaId = 0;
- $this->richiestaAnagrafica = 0;
- $this->richiestaData = null;
- $this->richiestaConsegna = null;
- $this->richiestaText = '';
- }
- protected function refreshRichieste(){
- $this->richieste = ReportRichiesta::where('report_id', $this->dataId)->get();
- }
- public function editRichiesta($id){
- $richiesta = ReportRichiesta::where('id', $id)->first();
- if ($richiesta) {
- $this->richiestaId = $id;
- $this->richiestaAnagrafica = $richiesta->anagrafica_id;
- $this->richiestaData = $richiesta->data_richiesta;
- $this->richiestaConsegna = $richiesta->consegna_richiesta;
- $this->richiestaText = $richiesta->description;
- }
- }
- public function removeRichiesta($id){
- ReportRichiesta::findOrFail($id)->delete();
- $this->refreshRichieste();
- }
- }
|