feat: Relocate units of measure and historical tariff fractions seeding to company creation and add Celery schedule files to gitignore.
This commit is contained in:
@@ -12,7 +12,10 @@ from sqlalchemy.orm import Session
|
||||
from .dto import CompanyCreateDTO, CompanyResponseDTO, CompanyUpdateDTO
|
||||
from .models import Company
|
||||
from ...audit_log.services.service import AuditService
|
||||
from ..units_of_measure.seed import seed as units_of_measure_seed
|
||||
from ..fractions.historical_tariff_fractions.seed import seed as historical_tariff_fractions_seed
|
||||
from core.context import get_user_context
|
||||
from sqlalchemy import text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -381,7 +384,10 @@ class CompanyService:
|
||||
if addr_ind2:
|
||||
self.db.add(CompanyAddress(**addr_ind2, address_type='industrial2', company_id=db_company.id))
|
||||
|
||||
# 7. Commit
|
||||
# 7. Seed company data (tenant/company dependent)
|
||||
self._seed_company_data(self.db, tenant_id, db_company.id)
|
||||
|
||||
# 8. Commit
|
||||
self.db.commit()
|
||||
|
||||
self.db.refresh(db_company)
|
||||
@@ -698,6 +704,63 @@ class CompanyService:
|
||||
|
||||
|
||||
# Custom methods
|
||||
def _seed_company_data(self, db: Session, tenant_id: int, company_id: int):
|
||||
"""Seeds tenant/company dependent data for a new company"""
|
||||
def format_value(val):
|
||||
if val is None or str(val).strip() == "" or str(val).upper() == "NONE":
|
||||
return "NULL"
|
||||
return f"'{str(val).replace(chr(39), chr(39)*2)}'"
|
||||
|
||||
# 1. Units of Measure
|
||||
val_uom = ", ".join(
|
||||
[
|
||||
f"({format_value(code)}, {format_value(desc)}, {format_value(desc_en)}, "
|
||||
f"{format_value(customs)}, {format_value(american)}, {format_value(ace)}, {format_value(oma)}, {tenant_id}, {company_id})"
|
||||
for code, desc, desc_en, customs, american, ace, oma in units_of_measure_seed
|
||||
]
|
||||
)
|
||||
|
||||
db.execute(text("ALTER TABLE a76.units_of_measure DISABLE TRIGGER ALL;"))
|
||||
db.execute(text(f"INSERT INTO a76.units_of_measure (code, description, description_en, customs_code, american_code, ace_code, oma_code, tenant_id, company_id) VALUES {val_uom} ON CONFLICT (code, tenant_id, company_id) DO NOTHING;"))
|
||||
db.execute(text("ALTER TABLE a76.units_of_measure ENABLE TRIGGER ALL;"))
|
||||
|
||||
# 2. Historical Tariff Fractions
|
||||
def format_bool(val):
|
||||
if val is None or str(val).strip() == "" or str(val).upper() == "NONE":
|
||||
return "NULL"
|
||||
return "TRUE" if str(val).upper() == "TRUE" else "FALSE"
|
||||
|
||||
def format_timestamp(val):
|
||||
if val is None or str(val).strip() == "" or str(val).upper() == "NONE":
|
||||
return "NULL"
|
||||
return f"'{str(val)}'"
|
||||
|
||||
values_historical = ", ".join(
|
||||
[
|
||||
f"({format_value(historical_fraction)}, {format_value(nico)}, {format_value(unit_measure)}, {format_value(country)}, "
|
||||
f"{format_value(fraction_type)}, {format_value(sector)}, {format_value(import_tax)}, "
|
||||
f"{format_value(export_tax)}, {format_timestamp(pub_date)}, {format_bool(is_immex)}, "
|
||||
f"{format_bool(normal_temp)}, {format_bool(services_temp)}, {format_bool(certified_temp)}, "
|
||||
f"{format_bool(by_log)}, {format_timestamp(end_date)}, "
|
||||
f"{tenant_id}, {company_id})"
|
||||
for (historical_fraction, nico, unit_measure, country, fraction_type, sector, import_tax,
|
||||
export_tax, pub_date, is_immex, normal_temp, services_temp, certified_temp, by_log, end_date) in historical_tariff_fractions_seed
|
||||
]
|
||||
)
|
||||
|
||||
if values_historical:
|
||||
db.execute(text("SET session_replication_role = replica;"))
|
||||
db.execute(text(f"""
|
||||
INSERT INTO a76.historical_tariff_fractions
|
||||
(historical_fraction, nico, unit_of_measure_code, country, fraction_type, sector,
|
||||
import_tax_rate, export_tax_rate, publication_date, is_immex,
|
||||
normal_temporality, services_temporality, certified_temporality, by_log, end_date,
|
||||
tenant_id, company_id)
|
||||
VALUES {values_historical}
|
||||
ON CONFLICT DO NOTHING;
|
||||
"""))
|
||||
db.execute(text("SET session_replication_role = DEFAULT;"))
|
||||
|
||||
def get_companies_by_tenant(self, tenant_id: int) -> List[Company]:
|
||||
"""Get all companies for a tenant"""
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user