| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\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 $corrispettivo_causal_id = 0;
- protected $rules = [
- 'name' => 'required',
- 'type' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio'
- ];
- public function mount()
- {
- $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
- if ($fisc)
- $this->corrispettivo_causal_id = $fisc->id;
- }
- public function resetFields(){
- $this->name = '';
- $this->parent_id = null;
- $this->type = null;
- $this->money = false;
- $this->corrispettivo_fiscale = false;
- $this->no_receipt = false;
- $this->user_status = false;
- $this->no_first = false;
- $this->no_records = false;
- $this->enabled = true;
- }
- 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();
- return view('livewire.causal');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function addLevel($parent_id)
- {
- $this->resetFields();
- $this->parent_id = $parent_id;
- $this->type = \App\Models\Causal::findOrFail($parent_id)->type;
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- try {
- \App\Models\Causal::create([
- 'name' => $this->name,
- 'type' => $this->type,
- 'parent_id' => $this->parent_id,
- 'money' => $this->money,
- 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
- 'no_receipt' => $this->no_receipt,
- 'user_status' => $this->user_status,
- 'no_first' => $this->no_first,
- 'no_records' => $this->no_records,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Causale creata');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- try {
- $causal = \App\Models\Causal::findOrFail($id);
- if( !$causal) {
- session()->flash('error','Causale non trovata');
- } else {
- $this->name = $causal->name;
- $this->money = $causal->money;
- $this->no_receipt = $causal->no_receipt;
- $this->user_status = $causal->user_status;
- $this->no_first = $causal->no_first;
- $this->no_records = $causal->no_records;
- $this->enabled = $causal->enabled;
- $this->type = $causal->type;
- $this->parent_id = $causal->parent_id;
- $this->dataId = $causal->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\Causal::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'type' => $this->type,
- 'parent_id' => $this->parent_id,
- 'user_status' => $this->user_status,
- 'no_first' => $this->no_first,
- 'no_records' => $this->no_records,
- 'money' => $this->money,
- 'no_receipt' => $this->no_receipt,
- 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Tessera aggiornata');
- $this->resetFields();
- $this->update = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->resetFields();
- }
- public function delete($id)
- {
- try{
- \App\Models\Causal::find($id)->delete();
- session()->flash('success',"Tessera eliminata");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function duplicate($id)
- {
- $old = \App\Models\Causal::find($id);
- $new = $old->replicate();
- $new->name = $new->name . ' - COPIA';
- $new->save();
- $this->duplicateRecursive($old, $new);
- }
- public function duplicateRecursive($old, $new)
- {
- foreach($old->childs as $c)
- {
- $old1 = \App\Models\Causal::find($c->id);
- $new1 = $old1->replicate();
- $new1->parent_id = $new->id;
- $new1->save();
- $this->duplicateRecursive($old1, $new1);
- }
- }
- public function reorder()
- {
- }
- }
|