diff --git a/backend/api/v1/modules/a76/general_catalogs/packages/models.py b/backend/api/v1/modules/a76/general_catalogs/packages/models.py index f191fa94..92b07647 100644 --- a/backend/api/v1/modules/a76/general_catalogs/packages/models.py +++ b/backend/api/v1/modules/a76/general_catalogs/packages/models.py @@ -19,7 +19,7 @@ class Package(Base, TenantScopedMixin, TimestampMixin): __table_args__ = ( PrimaryKeyConstraint("id", name="packages_pkey"), UniqueConstraint("tenant_id", "company_id", "key", name="packages_key_ukey"), - {"schema": "a76"}, + {"schema": "a76", "extend_existing": True}, ) id: Mapped[int] = mapped_column(Integer, primary_key=True) diff --git a/backend/api/v1/modules/a76/items/line_quantities/models.py b/backend/api/v1/modules/a76/items/line_quantities/models.py index c7a60558..cc588e1e 100644 --- a/backend/api/v1/modules/a76/items/line_quantities/models.py +++ b/backend/api/v1/modules/a76/items/line_quantities/models.py @@ -4,6 +4,8 @@ from sqlalchemy import String, Integer, Numeric, SmallInteger, ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from core.database import Base +from api.v1.modules.a76.general_catalogs.packages.models import Package + if TYPE_CHECKING: from ..line_items.models import LineItem @@ -38,13 +40,14 @@ class LineQuantity(Base): net_weight: Mapped[Optional[Decimal]] = mapped_column(Numeric(19, 8)) # PESONETO gross_weight: Mapped[Optional[Decimal]] = mapped_column(Numeric(19, 8)) # PESOBRUTO + # Packaging - package_key: Mapped[Optional[str]] = mapped_column(String(5)) # CLAVEBULTOS + package_id: Mapped[Optional[int]] = mapped_column(ForeignKey("a76.packages.id")) package_quantity: Mapped[Optional[int]] = mapped_column(Integer) # CANTBULTOS - package_description: Mapped[Optional[str]] = mapped_column(String(40)) # DESCBULTOS container_quantity: Mapped[Optional[int]] = mapped_column(SmallInteger) # CANTBULCONT container_description: Mapped[Optional[str]] = mapped_column(String(40)) # DESCCONTENEDOR box_count: Mapped[Optional[str]] = mapped_column(String(30)) # NOCAJAS # Relationship (one-to-one) - line: Mapped["LineItem"] = relationship(back_populates="quantity") \ No newline at end of file + line: Mapped["LineItem"] = relationship(back_populates="quantity") + package_info: Mapped[Optional["Package"]] = relationship(Package) \ No newline at end of file diff --git a/backend/api/v1/modules/a76/reports/importacion/packing_list/service.py b/backend/api/v1/modules/a76/reports/importacion/packing_list/service.py index 43f0036d..a6163592 100644 --- a/backend/api/v1/modules/a76/reports/importacion/packing_list/service.py +++ b/backend/api/v1/modules/a76/reports/importacion/packing_list/service.py @@ -313,7 +313,7 @@ class PackingListService: cantidad_importacion=qty.quantity if qty else 0, unidad_medida=uom_comercial, # Commercial UOM (PCS, EA) cantidad_bultos=int(qty.package_quantity) if qty and qty.package_quantity else 0, - clave_bultos=(qty.package_key or "") if qty else "", + clave_bultos=(qty.package_info.key if qty and qty.package_info else "") if qty else "", peso_neto=self.formatear_numero(peso_neto_kg), peso_bruto=self.formatear_numero(peso_bruto_kg), peso_neto_lbs=self.formatear_numero(peso_neto_lb), diff --git a/drop_column.sql b/drop_column.sql new file mode 100644 index 00000000..c0845c83 --- /dev/null +++ b/drop_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE a76.item_line_quantities +DROP COLUMN IF EXISTS package_key; \ No newline at end of file diff --git a/drop_desc_column.sql b/drop_desc_column.sql new file mode 100644 index 00000000..e8e26aa2 --- /dev/null +++ b/drop_desc_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE a76.item_line_quantities +DROP COLUMN IF EXISTS package_description; \ No newline at end of file diff --git a/fix_data.sql b/fix_data.sql new file mode 100644 index 00000000..b3003427 --- /dev/null +++ b/fix_data.sql @@ -0,0 +1,10 @@ +-- Actualizar el nuevo campo package_id usando el valor numérico guardado erróneamente en package_key +UPDATE a76.item_line_quantities +SET + package_id = CAST(package_key AS INTEGER) +WHERE + package_key ~ '^\d+$' + AND package_id IS NULL; + +-- Opcional: Limpiar el campo package_key si ya se migró (para evitar confusión futura, pero mejor dejarlo por seguridad) +-- UPDATE a76.item_line_quantities SET package_key = NULL WHERE package_id IS NOT NULL; \ No newline at end of file diff --git a/fix_db_column.py b/fix_db_column.py new file mode 100644 index 00000000..7145b2f4 --- /dev/null +++ b/fix_db_column.py @@ -0,0 +1,18 @@ +from sqlalchemy import text +from core.database import SessionLocal + +def add_column(): + db = SessionLocal() + try: + sql = text("ALTER TABLE a76.item_line_quantities ADD COLUMN IF NOT EXISTS package_id INTEGER REFERENCES a76.packages(id);") + db.execute(sql) + db.commit() + print("Successfully added package_id column.") + except Exception as e: + print(f"Error: {e}") + db.rollback() + finally: + db.close() + +if __name__ == "__main__": + add_column() diff --git a/fix_schema.sql b/fix_schema.sql new file mode 100644 index 00000000..7106b3c8 --- /dev/null +++ b/fix_schema.sql @@ -0,0 +1,2 @@ +ALTER TABLE a76.item_line_quantities +ADD COLUMN IF NOT EXISTS package_id INTEGER REFERENCES a76.packages (id); \ No newline at end of file