2025-03-05 20:43:35 -06:00

191 lines
5.7 KiB
PHP

<?php
namespace Koneko\VuexyStoreManager\Livewire\Stores;
use Illuminate\Support\Facades\DB;
use Koneko\VuexyWarehouse\Models\Warehouse;
use Livewire\Component;
class StoreForm extends Component
{
public $form_title;
public $mode = 'create';
public $warehouseId,
$store_id,
$workcenter_id,
$code,
$name,
$description,
$is_active = true,
$is_default,
$confirm_delete;
public $store_options = [],
$workcenter_options = [];
protected $listeners = [
'editWarehouse' => 'loadWarehouse',
'confirmDeleteWarehouse' => 'loadWarehouseForDeletion',
];
public function mount()
{
$this->loadOptions();
$this->resetForm();
}
private function loadOptions()
{
$this->store_options = DB::table('stores')
->select('id', 'name')
->orderBy('name')
->pluck('name', 'id');
$this->workcenter_options = DB::table('store_work_centers')
->select('id', 'name')
->orderBy('name')
->pluck('name', 'id');
}
public function loadWarehouse($id)
{
$warehouse = Warehouse::find($id);
if ($warehouse) {
$this->fill($warehouse->only(['id', 'store_id', 'workcenter_id', 'code', 'name', 'description', 'is_active', 'is_default']));
$this->form_title = "Editar: $warehouse->name";
$this->mode = 'edit';
$this->dispatch('on-edit-warehouse-modal');
}
}
public function loadWarehouseForDeletion($id)
{
$warehouse = Warehouse::find($id);
if ($warehouse) {
$this->fill($warehouse->only(['id', 'store_id', 'workcenter_id', 'code', 'name', 'description', 'is_active', 'is_default']));
$this->form_title = "Eliminar: $warehouse->name";
$this->mode = 'delete';
$this->dispatch('on-delete-warehouse-modal');
}
}
public function editWarehouse($id)
{
$warehouse = Warehouse::find($id);
if ($warehouse) {
$this->form_title = 'Editar: ' . $warehouse->name;
$this->mode = 'edit';
$this->warehouseId = $warehouse->id;
$this->store_id = $warehouse->store_id;
$this->workcenter_id = $warehouse->workcenter_id;
$this->code = $warehouse->code;
$this->name = $warehouse->name;
$this->description = $warehouse->description;
$this->is_active = $warehouse->is_active;
$this->is_default = $warehouse->is_default;
$this->dispatch('on-edit-warehouse-modal');
}
}
public function confirmDeleteWarehouse($id)
{
$warehouse = Warehouse::find($id);
if ($warehouse) {
$this->form_title = 'Eliminar: ' . $warehouse->name;
$this->mode = 'delete';
$this->warehouseId = $warehouse->id;
$this->store_id = $warehouse->store_id;
$this->workcenter_id = $warehouse->workcenter_id;
$this->code = $warehouse->code;
$this->name = $warehouse->name;
$this->description = $warehouse->description;
$this->is_active = $warehouse->is_active;
$this->is_default = $warehouse->is_default;
$this->dispatch('on-delete-warehouse-modal');
}
}
public function onSubmit()
{
if ($this->mode === 'delete') {
return $this->delete();
}
return $this->save();
}
private function save()
{
try {
$validatedData = $this->validate([
'store_id' => 'required',
'code' => 'required|string|max:16',
'name' => 'required|string|max:96',
'description' => 'nullable|string|max:1024',
]);
} catch (\Illuminate\Validation\ValidationException $e) {
$this->dispatch('on-failed-validation-warehouse-modal');
$this->dispatch('warehouse-message', ['type' => 'danger', 'message' => 'Error en la validación']);
throw $e;
}
Warehouse::updateOrCreate(
['id' => $this->warehouseId],
[
'store_id' => $validatedData['store_id'],
'workcenter_id' => $this->workcenter_id,
'code' => $validatedData['code'],
'name' => $validatedData['name'],
'description' => $validatedData['description'] ?? null,
'is_active' => (bool) $this->is_active,
'is_default' => (bool) $this->is_default,
]
);
$this->dispatch('warehouse-message', ['type' => 'success', 'message' => 'Almacén guardado correctamente']);
$this->dispatch('reload-warehouse-table');
$this->dispatch('close-warehouse-modal');
$this->resetForm();
}
public function delete()
{
if ($this->warehouseId) {
Warehouse::find($this->warehouseId)->delete();
$this->dispatch('warehouse-message', ['type' => 'warning', 'message' => 'Almacén eliminado']);
$this->dispatch('reload-warehouse-table');
$this->dispatch('close-warehouse-modal');
$this->resetForm();
}
}
public function resetForm()
{
$this->reset(['warehouseId', 'store_id', 'workcenter_id', 'code', 'name', 'description', 'is_default', 'confirm_delete']);
$this->form_title = 'Agregar almacén';
$this->mode = 'create';
$this->is_active = true;
}
public function render()
{
return view('vuexy-warehouse::livewire.stores.form');
}
}