From 08f99f102e3a3198b16ebc5cfc1f47c7eb0b901e Mon Sep 17 00:00:00 2001 From: hreyes Date: Tue, 2 Jun 2026 07:59:12 -0600 Subject: [PATCH] fix/uso-de-catalogo-sectores-empresa --- ...0a256_company_prosec_boolean_to_integer.py | 37 +++++++++++++ .../a76/general_catalogs/company/models.py | 2 +- .../edit/[[id]]/+page.svelte | 53 +++++++++++++++++-- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 backend/alembic/versions/506969b0a256_company_prosec_boolean_to_integer.py diff --git a/backend/alembic/versions/506969b0a256_company_prosec_boolean_to_integer.py b/backend/alembic/versions/506969b0a256_company_prosec_boolean_to_integer.py new file mode 100644 index 00000000..61760536 --- /dev/null +++ b/backend/alembic/versions/506969b0a256_company_prosec_boolean_to_integer.py @@ -0,0 +1,37 @@ +"""company prosec boolean to integer + +Revision ID: 506969b0a256 +Revises: a7b8c9d0e1f2 +Create Date: 2026-06-02 00:00:00.000000 + +""" +from typing import Sequence, Union + +from alembic import op + +revision: str = "506969b0a256" +down_revision: Union[str, None] = "a7b8c9d0e1f2" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # Convierte prosec de boolean a integer para almacenar el ID del sector PROSEC. + # El default 'false' debe eliminarse antes de cambiar el tipo. + op.execute("ALTER TABLE a76.company ALTER COLUMN prosec DROP DEFAULT") + op.execute("ALTER TABLE a76.company ALTER COLUMN prosec DROP NOT NULL") + op.execute( + "ALTER TABLE a76.company ALTER COLUMN prosec TYPE integer USING prosec::int" + ) + + +def downgrade() -> None: + op.execute( + """ + ALTER TABLE a76.company + ALTER COLUMN prosec TYPE boolean + USING CASE WHEN prosec IS NULL OR prosec = 0 THEN false ELSE true END + """ + ) + op.execute("ALTER TABLE a76.company ALTER COLUMN prosec SET DEFAULT false") + op.execute("ALTER TABLE a76.company ALTER COLUMN prosec SET NOT NULL") diff --git a/backend/api/v1/modules/a76/general_catalogs/company/models.py b/backend/api/v1/modules/a76/general_catalogs/company/models.py index 90c41010..68ec0597 100644 --- a/backend/api/v1/modules/a76/general_catalogs/company/models.py +++ b/backend/api/v1/modules/a76/general_catalogs/company/models.py @@ -39,7 +39,7 @@ class Company(Base, TimestampMixin): # Programa program: Mapped[Optional[str]] = mapped_column(String(7)) program_number: Mapped[Optional[str]] = mapped_column(String(40)) - prosec: Mapped[bool] = mapped_column(Boolean, default=False, server_default="false") + prosec: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, default=None) prosec_authorization: Mapped[Optional[str]] = mapped_column(String(20)) # Sectores diff --git a/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte b/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte index be094a8f..07897dd5 100644 --- a/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte +++ b/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte @@ -11,6 +11,7 @@ import * as Card from '$lib/components/ui/card'; import CountrySelectorDialog from '$lib/components/dashboard/goods/modales/country-selector-dialog.svelte'; import StateSelectorDialog from '$lib/components/dashboard/shared/modals/state-selector-dialog.svelte'; + import SectorSelectorDialog from '$lib/components/dashboard/shared/modals/sector-selector-dialog.svelte'; import { createCompany, updateCompany, @@ -19,6 +20,7 @@ uploadCompanyCertificate, type Company } from '$lib/api/dashboard/a76/general_catalogs/company'; + import { sectorsApi } from '$lib/api/dashboard/reference_data/sectors'; import { companyStore } from '$lib/stores/company.svelte'; import { getBackendAssetUrl, getFileDisplayName } from '$lib/utils'; import { toDbDate, toInputDate } from '$lib/utils/date'; @@ -37,7 +39,8 @@ Key, ShieldCheck, FileSignature, - Ban + Ban, + Factory } from 'lucide-svelte'; import { useShortcuts } from '$lib/hooks/use-shortcuts'; @@ -64,6 +67,8 @@ let activeCountryField = $state<'main_country' | 'ind1_country' | 'ind2_country'>('main_country'); let stateModalOpen = $state(false); let activeStateField = $state<'main_state' | 'ind1_state' | 'ind2_state'>('main_state'); + let sectorModalOpen = $state(false); + let prosecDisplay = $state(''); let logoFile = $state(null); let logoPreview = $state(null); @@ -408,6 +413,21 @@ cancel_key_exp: item.cancel_key_exp || 0 }; + // Resolver el sector PROSEC contra el catálogo para mostrar KEY - Descripción + if (item.prosec) { + const companyId = companyStore.activeCompany?.id; + if (companyId) { + const sectorResp = await sectorsApi.get(item.prosec, companyId); + prosecDisplay = sectorResp.data + ? `${sectorResp.data.key} - ${sectorResp.data.description}` + : String(item.prosec); // fallback al ID si el sector no se encontró + } else { + prosecDisplay = String(item.prosec); + } + } else { + prosecDisplay = ''; + } + // Guardar la URL del logo actual si existe if (item.logo) { currentLogo = item.logo; @@ -447,6 +467,11 @@ formData[relatedCountryField] = state.m3_key; } + function handleSectorSelect(sector: { id: number; key: string; description: string }) { + formData.prosec = sector.id; + prosecDisplay = `${sector.key} - ${sector.description}`; + } + function handleLogoChange(event: Event) { const target = event.target as HTMLInputElement; const file = target.files?.[0]; @@ -902,8 +927,25 @@
- - + +
+ + +
@@ -2044,4 +2086,9 @@ + { formData.prosec = 0; prosecDisplay = ''; }} +/> {/if}