|
@@ -5,6 +5,7 @@ namespace App\Http\Livewire;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Livewire\Component;
|
|
use Livewire\Component;
|
|
|
use App\Http\Middleware\TenantMiddleware;
|
|
use App\Http\Middleware\TenantMiddleware;
|
|
|
|
|
+
|
|
|
class Causal extends Component
|
|
class Causal extends Component
|
|
|
{
|
|
{
|
|
|
public $recordsIn, $recordsOut, $parent_id, $name, $enabled, $corrispettivo_fiscale, $no_receipt, $money, $user_status, $no_first, $no_records, $type, $dataId, $update = false, $add = false;
|
|
public $recordsIn, $recordsOut, $parent_id, $name, $enabled, $corrispettivo_fiscale, $no_receipt, $money, $user_status, $no_first, $no_records, $type, $dataId, $update = false, $add = false;
|
|
@@ -12,6 +13,7 @@ class Causal extends Component
|
|
|
public $corrispettivo_causal_id = 0;
|
|
public $corrispettivo_causal_id = 0;
|
|
|
|
|
|
|
|
public $parent = '';
|
|
public $parent = '';
|
|
|
|
|
+ public $showHidden = false;
|
|
|
|
|
|
|
|
protected $rules = [
|
|
protected $rules = [
|
|
|
'name' => 'required',
|
|
'name' => 'required',
|
|
@@ -21,22 +23,92 @@ class Causal extends Component
|
|
|
protected $messages = [
|
|
protected $messages = [
|
|
|
'name.required' => 'Il nome è obbligatorio'
|
|
'name.required' => 'Il nome è obbligatorio'
|
|
|
];
|
|
];
|
|
|
|
|
+
|
|
|
public function boot()
|
|
public function boot()
|
|
|
{
|
|
{
|
|
|
app(TenantMiddleware::class)->setupTenantConnection();
|
|
app(TenantMiddleware::class)->setupTenantConnection();
|
|
|
}
|
|
}
|
|
|
- public function mount(){
|
|
|
|
|
|
|
|
|
|
- if(Auth::user()->level != env('LEVEL_ADMIN', 0))
|
|
|
|
|
- return redirect()->to('/dashboard');
|
|
|
|
|
|
|
+ public function mount()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->loadRecords();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
|
|
|
|
|
- if ($fisc)
|
|
|
|
|
- $this->corrispettivo_causal_id = $fisc->id;
|
|
|
|
|
|
|
+ public function hide($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ \App\Models\Causal::whereId($id)->update(['hidden' => true]);
|
|
|
|
|
+ session()->flash('success', 'Causale nascosta');
|
|
|
|
|
+ $this->loadRecords(); // Refresh the data
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ public function show($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ \App\Models\Causal::whereId($id)->update(['hidden' => false]);
|
|
|
|
|
+ session()->flash('success', 'Causale ripristinata');
|
|
|
|
|
+ $this->loadRecords(); // Refresh the data
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function resetFields(){
|
|
|
|
|
|
|
+ public function toggleHidden()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->showHidden = !$this->showHidden;
|
|
|
|
|
+ $this->loadRecords();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function loadRecords()
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($this->showHidden) {
|
|
|
|
|
+ // Show all records including hidden ones
|
|
|
|
|
+ $this->recordsIn = \App\Models\Causal::where('type', 'IN')
|
|
|
|
|
+ ->where('parent_id', null)
|
|
|
|
|
+ ->with(['childs'])
|
|
|
|
|
+ ->orderBy('name')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ $this->recordsOut = \App\Models\Causal::where('type', 'OUT')
|
|
|
|
|
+ ->where('parent_id', null)
|
|
|
|
|
+ ->with(['childs'])
|
|
|
|
|
+ ->orderBy('name')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Show only non-hidden records
|
|
|
|
|
+ $this->recordsIn = \App\Models\Causal::where('type', 'IN')
|
|
|
|
|
+ ->where('parent_id', null)
|
|
|
|
|
+ ->where(function ($query) {
|
|
|
|
|
+ $query->where('hidden', false)->orWhereNull('hidden');
|
|
|
|
|
+ })
|
|
|
|
|
+ ->with(['childs' => function ($query) {
|
|
|
|
|
+ $query->where(function ($q) {
|
|
|
|
|
+ $q->where('hidden', false)->orWhereNull('hidden');
|
|
|
|
|
+ });
|
|
|
|
|
+ }])
|
|
|
|
|
+ ->orderBy('name')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ $this->recordsOut = \App\Models\Causal::where('type', 'OUT')
|
|
|
|
|
+ ->where('parent_id', null)
|
|
|
|
|
+ ->where(function ($query) {
|
|
|
|
|
+ $query->where('hidden', false)->orWhereNull('hidden');
|
|
|
|
|
+ })
|
|
|
|
|
+ ->with(['childs' => function ($query) {
|
|
|
|
|
+ $query->where(function ($q) {
|
|
|
|
|
+ $q->where('hidden', false)->orWhereNull('hidden');
|
|
|
|
|
+ });
|
|
|
|
|
+ }])
|
|
|
|
|
+ ->orderBy('name')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function resetFields()
|
|
|
|
|
+ {
|
|
|
$this->name = '';
|
|
$this->name = '';
|
|
|
$this->parent_id = null;
|
|
$this->parent_id = null;
|
|
|
$this->parent = '';
|
|
$this->parent = '';
|
|
@@ -52,8 +124,12 @@ class Causal extends Component
|
|
|
|
|
|
|
|
public function render()
|
|
public function render()
|
|
|
{
|
|
{
|
|
|
- $this->recordsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->get();
|
|
|
|
|
- $this->recordsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->get();
|
|
|
|
|
|
|
+ // Remove the duplicate queries - loadRecords() already handles this
|
|
|
|
|
+ // and make sure it respects the showHidden state
|
|
|
|
|
+ if (!isset($this->recordsIn) || !isset($this->recordsOut)) {
|
|
|
|
|
+ $this->loadRecords();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return view('livewire.causal');
|
|
return view('livewire.causal');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -91,19 +167,21 @@ class Causal extends Component
|
|
|
'no_records' => $this->no_records,
|
|
'no_records' => $this->no_records,
|
|
|
'enabled' => $this->enabled
|
|
'enabled' => $this->enabled
|
|
|
]);
|
|
]);
|
|
|
- session()->flash('success','Causale creata');
|
|
|
|
|
|
|
+ session()->flash('success', 'Causale creata');
|
|
|
$this->resetFields();
|
|
$this->resetFields();
|
|
|
$this->add = false;
|
|
$this->add = false;
|
|
|
|
|
+ $this->loadRecords();
|
|
|
} catch (\Exception $ex) {
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
|
|
+ session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function edit($id){
|
|
|
|
|
|
|
+ public function edit($id)
|
|
|
|
|
+ {
|
|
|
try {
|
|
try {
|
|
|
$causal = \App\Models\Causal::findOrFail($id);
|
|
$causal = \App\Models\Causal::findOrFail($id);
|
|
|
- if( !$causal) {
|
|
|
|
|
- session()->flash('error','Causale non trovata');
|
|
|
|
|
|
|
+ if (!$causal) {
|
|
|
|
|
+ session()->flash('error', 'Causale non trovata');
|
|
|
} else {
|
|
} else {
|
|
|
$this->name = $causal->name;
|
|
$this->name = $causal->name;
|
|
|
$this->money = $causal->money;
|
|
$this->money = $causal->money;
|
|
@@ -112,6 +190,7 @@ class Causal extends Component
|
|
|
$this->no_first = $causal->no_first;
|
|
$this->no_first = $causal->no_first;
|
|
|
$this->no_records = $causal->no_records;
|
|
$this->no_records = $causal->no_records;
|
|
|
$this->enabled = $causal->enabled;
|
|
$this->enabled = $causal->enabled;
|
|
|
|
|
+ $this->corrispettivo_fiscale = $causal->corrispettivo_fiscale;
|
|
|
$this->type = $causal->type;
|
|
$this->type = $causal->type;
|
|
|
$this->parent_id = $causal->parent_id;
|
|
$this->parent_id = $causal->parent_id;
|
|
|
$this->dataId = $causal->id;
|
|
$this->dataId = $causal->id;
|
|
@@ -119,7 +198,7 @@ class Causal extends Component
|
|
|
$this->add = false;
|
|
$this->add = false;
|
|
|
}
|
|
}
|
|
|
} catch (\Exception $ex) {
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
|
|
+ session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -139,11 +218,12 @@ class Causal extends Component
|
|
|
'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
|
|
'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
|
|
|
'enabled' => $this->enabled
|
|
'enabled' => $this->enabled
|
|
|
]);
|
|
]);
|
|
|
- session()->flash('success','Tessera aggiornata');
|
|
|
|
|
|
|
+ session()->flash('success', 'Causale aggiornata');
|
|
|
$this->resetFields();
|
|
$this->resetFields();
|
|
|
$this->update = false;
|
|
$this->update = false;
|
|
|
|
|
+ $this->loadRecords();
|
|
|
} catch (\Exception $ex) {
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
|
|
+ session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -154,17 +234,6 @@ class Causal extends Component
|
|
|
$this->resetFields();
|
|
$this->resetFields();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function delete($id)
|
|
|
|
|
- {
|
|
|
|
|
- try{
|
|
|
|
|
- \App\Models\Causal::find($id)->delete();
|
|
|
|
|
- session()->flash('success',"Tessera eliminata");
|
|
|
|
|
- return redirect(request()->header('Referer'));
|
|
|
|
|
- }catch(\Exception $e){
|
|
|
|
|
- session()->flash('error','Errore (' . $e->getMessage() . ')');
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public function duplicate($id)
|
|
public function duplicate($id)
|
|
|
{
|
|
{
|
|
|
$old = \App\Models\Causal::find($id);
|
|
$old = \App\Models\Causal::find($id);
|
|
@@ -173,25 +242,23 @@ class Causal extends Component
|
|
|
$new->save();
|
|
$new->save();
|
|
|
|
|
|
|
|
$this->duplicateRecursive($old, $new);
|
|
$this->duplicateRecursive($old, $new);
|
|
|
|
|
+ $this->loadRecords(); // Refresh after duplicate
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function duplicateRecursive($old, $new)
|
|
public function duplicateRecursive($old, $new)
|
|
|
{
|
|
{
|
|
|
- foreach($old->childs as $c)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ foreach ($old->childs as $c) {
|
|
|
$old1 = \App\Models\Causal::find($c->id);
|
|
$old1 = \App\Models\Causal::find($c->id);
|
|
|
$new1 = $old1->replicate();
|
|
$new1 = $old1->replicate();
|
|
|
$new1->parent_id = $new->id;
|
|
$new1->parent_id = $new->id;
|
|
|
$new1->save();
|
|
$new1->save();
|
|
|
|
|
|
|
|
$this->duplicateRecursive($old1, $new1);
|
|
$this->duplicateRecursive($old1, $new1);
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function reorder()
|
|
public function reorder()
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
|
|
+ $this->loadRecords(); // Refresh after reorder
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|