270 lines
10 KiB
PHP
270 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Koneko\VuexyStoreManager\Livewire\Company;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Koneko\SatCatalogs\Models\Colonia;
|
|
use Koneko\SatCatalogs\Models\Estado;
|
|
use Koneko\SatCatalogs\Models\Localidad;
|
|
use Koneko\SatCatalogs\Models\Municipio;
|
|
use Koneko\SatCatalogs\Models\Pais;
|
|
use Koneko\SatCatalogs\Models\RegimenFiscal;
|
|
use Koneko\VuexyStoreManager\Models\Store;
|
|
use Livewire\Component;
|
|
|
|
class CompanyIndex extends Component
|
|
{
|
|
public $btnSubmitText;
|
|
public $mode;
|
|
public $storeId;
|
|
|
|
public $code,
|
|
$name,
|
|
$description,
|
|
$manager_id,
|
|
$rfc,
|
|
$nombre_fiscal,
|
|
$c_regimen_fiscal,
|
|
$domicilio_fiscal,
|
|
$serie_ingresos,
|
|
$serie_egresos,
|
|
$serie_pagos,
|
|
$c_codigo_postal,
|
|
$c_pais,
|
|
$c_estado,
|
|
$c_localidad,
|
|
$c_municipio,
|
|
$c_colonia,
|
|
$direccion,
|
|
$num_ext,
|
|
$num_int,
|
|
$email,
|
|
$tel,
|
|
$tel2,
|
|
$lat,
|
|
$lng,
|
|
$status,
|
|
$show_on_website,
|
|
$enable_ecommerce;
|
|
|
|
public $manager_id_options = [],
|
|
$c_regimen_fiscal_options = [],
|
|
$c_pais_options = [],
|
|
$c_estado_options = [],
|
|
$c_localidad_options = [],
|
|
$c_municipio_options = [],
|
|
$c_colonia_options = [];
|
|
|
|
protected $listeners = [
|
|
'editStore' => 'loadStore',
|
|
'confirmDeletionStore' => 'loadStoreForDeletion',
|
|
];
|
|
|
|
|
|
public function mount(String $mode = 'create', Store $store = null)
|
|
{
|
|
$this->mode = $mode;
|
|
|
|
$this->loadData($store);
|
|
$this->loadOptions();
|
|
}
|
|
|
|
private function loadData(Store $store)
|
|
{
|
|
switch($this->mode){
|
|
case 'create':
|
|
$this->btnSubmitText = 'Crear sucursal';
|
|
|
|
$this->c_pais = 'MEX';
|
|
$this->status = true;
|
|
break;
|
|
|
|
case 'edit':
|
|
|
|
$this->btnSubmitText = 'Guardar cambios';
|
|
break;
|
|
|
|
case 'delete':
|
|
$this->btnSubmitText = 'Eliminar sucursal';
|
|
break;
|
|
}
|
|
|
|
if($store){
|
|
$this->storeId = $store->id;
|
|
|
|
$this->code = $store->code;
|
|
$this->name = $store->name;
|
|
$this->description = $store->description;
|
|
$this->manager_id = $store->manager_id;
|
|
$this->rfc = $store->rfc;
|
|
$this->nombre_fiscal = $store->nombre_fiscal;
|
|
$this->c_regimen_fiscal = $store->c_regimen_fiscal;
|
|
$this->domicilio_fiscal = $store->domicilio_fiscal;
|
|
$this->serie_ingresos = $store->serie_ingresos;
|
|
$this->serie_egresos = $store->serie_egresos;
|
|
$this->serie_pagos = $store->serie_pagos;
|
|
$this->c_codigo_postal = $store->c_codigo_postal;
|
|
$this->c_pais = $store->c_pais;
|
|
$this->c_estado = $store->c_estado;
|
|
$this->c_localidad = $store->c_localidad;
|
|
$this->c_municipio = $store->c_municipio;
|
|
$this->c_colonia = $store->c_colonia;
|
|
$this->direccion = $store->direccion;
|
|
$this->num_ext = $store->num_ext;
|
|
$this->num_int = $store->num_int;
|
|
$this->email = $store->email;
|
|
$this->tel = $store->tel;
|
|
$this->tel2 = $store->tel2;
|
|
$this->lat = $store->lat;
|
|
$this->lng = $store->lng;
|
|
$this->status = $store->status;
|
|
$this->show_on_website = $store->show_on_website;
|
|
$this->enable_ecommerce = $store->enable_ecommerce;
|
|
}
|
|
}
|
|
|
|
private function loadOptions()
|
|
{
|
|
$this->manager_id_options = DB::table('users')
|
|
->select('id', DB::raw("CONCAT(CONCAT_WS(' ', name, last_name), ' - ', email) as full_name"))
|
|
//->where('is_user', 1)
|
|
->orderBy('full_name')
|
|
->pluck('full_name', 'id');
|
|
|
|
$this->c_regimen_fiscal_options = RegimenFiscal::selectList();
|
|
$this->c_pais_options = Pais::selectList();
|
|
|
|
if($this->mode !== 'create'){
|
|
$this->c_estado_options = ['' => 'Seleccione el estado'] + Estado::selectList($this->c_pais)->toArray();
|
|
$this->c_localidad_options = ['' => 'Seleccione la localidad'] + Localidad::selectList($this->c_estado)->toArray();
|
|
$this->c_municipio_options = ['' => 'Seleccione el municipio'] + Municipio::selectList($this->c_estado, $this->c_municipio)->toArray();
|
|
$this->c_colonia_options = ['' => 'Seleccione la colonia'] + Colonia::selectList($this->c_codigo_postal, $this->c_colonia)->toArray();
|
|
}
|
|
}
|
|
|
|
public function onSubmit()
|
|
{
|
|
if ($this->mode === 'delete') {
|
|
return $this->delete();
|
|
}
|
|
|
|
return $this->save();
|
|
}
|
|
|
|
private function save()
|
|
{
|
|
$validatedData = $this->validate([
|
|
'code' => 'required|string|max:16',
|
|
'name' => 'required|string|max:96',
|
|
'description' => 'nullable|string|max:1024',
|
|
|
|
'manager_id' => 'nullable|exists:users,id',
|
|
'rfc' => 'nullable|string|max:13',
|
|
'nombre_fiscal' => 'nullable|string|max:255',
|
|
'c_regimen_fiscal' => 'nullable|integer',
|
|
'domicilio_fiscal' => 'nullable|integer',
|
|
|
|
'c_pais' => 'nullable|string|max:3',
|
|
'c_estado' => 'nullable|string|max:3',
|
|
'c_municipio' => 'nullable|integer',
|
|
'c_localidad' => 'nullable|integer',
|
|
'c_codigo_postal' => 'nullable|integer',
|
|
'c_colonia' => 'nullable|integer',
|
|
'direccion' => 'nullable|string|max:255',
|
|
'num_ext' => 'nullable|string|max:50',
|
|
'num_int' => 'nullable|string|max:50',
|
|
'lat' => 'nullable|numeric',
|
|
'lng' => 'nullable|numeric',
|
|
|
|
'email' => 'nullable|email|max:96',
|
|
'tel' => 'nullable|string|max:15',
|
|
'tel2' => 'nullable|string|max:15',
|
|
|
|
'status' => 'nullable|boolean',
|
|
'show_on_website' => 'nullable|boolean',
|
|
'enable_ecommerce' => 'nullable|boolean',
|
|
]);
|
|
|
|
try {
|
|
$store = Store::updateOrCreate(
|
|
[ 'id' => $this->storeId ], // Si $this->storeId es null, creará un nuevo registro
|
|
[
|
|
'code' => $validatedData['code'],
|
|
'name' => $validatedData['name'],
|
|
'description' => $validatedData['description'] ?? null,
|
|
|
|
'manager_id' => $validatedData['manager_id'] ?? null,
|
|
'rfc' => $validatedData['rfc'] ?? null,
|
|
'nombre_fiscal' => $validatedData['nombre_fiscal'] ?? null,
|
|
'c_regimen_fiscal' => $validatedData['c_regimen_fiscal'] ?? null,
|
|
'domicilio_fiscal' => $validatedData['domicilio_fiscal'] ?? null,
|
|
|
|
'c_pais' => $validatedData['c_pais'] ?? null,
|
|
'c_estado' => $validatedData['c_estado'] ?? null,
|
|
'c_municipio' => $validatedData['c_municipio'] ?? null,
|
|
'c_localidad' => $validatedData['c_localidad'] ?? null,
|
|
'c_codigo_postal' => $validatedData['c_codigo_postal'] ?? null,
|
|
'c_colonia' => $validatedData['c_colonia'] ?? null,
|
|
'direccion' => $validatedData['direccion'] ?? null,
|
|
'num_ext' => $validatedData['num_ext'] ?? null,
|
|
'num_int' => $validatedData['num_int'] ?? null,
|
|
'lat' => $validatedData['lat'] ?? null,
|
|
'lng' => $validatedData['lng'] ?? null,
|
|
|
|
'email' => $validatedData['email'] ?? null,
|
|
'tel' => $validatedData['tel'] ?? null,
|
|
'tel2' => $validatedData['tel2'] ?? null,
|
|
|
|
'show_on_website' => (bool) $validatedData['show_on_website'],
|
|
'enable_ecommerce' => (bool) $validatedData['enable_ecommerce'],
|
|
'status' => (bool) $validatedData['status'],
|
|
]
|
|
);
|
|
|
|
session()->flash('success', 'Sucursal guardada correctamente.');
|
|
|
|
return redirect()->route('admin.store-manager.stores.index');
|
|
|
|
} catch (QueryException $e) {
|
|
// Manejar un error específico de SQL/DB, por ejemplo duplicados, FK violation, etc.
|
|
// O podrías capturar \Exception para cualquier error genérico
|
|
session()->flash('error', 'Ocurrió un error al guardar la sucursal.');
|
|
// Opcionalmente: loguear el error para depuración:
|
|
\Log::error($e->getMessage());
|
|
|
|
// Si no haces return, el método continuará, podrías redirigir o quedarte en la misma página
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
if ($this->storeId) {
|
|
try {
|
|
Store::find($this->storeId)->delete();
|
|
|
|
session()->flash('warning', 'Sucursal eliminada correctamente.');
|
|
|
|
return redirect()->route('admin.store-manager.stores.index');
|
|
|
|
} catch (QueryException $e) {
|
|
// Manejar un error específico de SQL/DB, por ejemplo duplicados, FK violation, etc.
|
|
// O podrías capturar \Exception para cualquier error genérico
|
|
session()->flash('error', 'Ocurrió un error al eliminar la sucursal.');
|
|
// Opcionalmente: loguear el error para depuración:
|
|
\Log::error($e->getMessage());
|
|
|
|
// Si no haces return, el método continuará, podrías redirigir o quedarte en la misma página
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('vuexy-store-manager::livewire.company.index');
|
|
}
|
|
|
|
}
|