Partidas por tipo de factura en importacion
This commit is contained in:
@@ -40,6 +40,7 @@ from .models import LineItem
|
||||
from .series.models import Serie
|
||||
from api.v1.modules.a76.invoices.models import InvoiceHeader
|
||||
from api.v1.modules.a76.parts.models import Part
|
||||
from api.v1.modules.a76.general_catalogs.identifiers.models import IdentifierDetail
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -195,6 +196,26 @@ class ItemService:
|
||||
serie_dict["row"] = 1
|
||||
db.add(Serie(**serie_dict))
|
||||
|
||||
# Identifier Detail data
|
||||
if hasattr(line_data, "identifiers") and line_data.identifiers:
|
||||
id_list = (
|
||||
line_data.identifiers
|
||||
if isinstance(line_data.identifiers, list)
|
||||
else [line_data.identifiers]
|
||||
)
|
||||
for d in id_list:
|
||||
id_dict = (
|
||||
d.model_dump(exclude_unset=True)
|
||||
if hasattr(d, "model_dump")
|
||||
else (dict(d) if isinstance(d, dict) else {})
|
||||
)
|
||||
if not id_dict:
|
||||
continue
|
||||
id_dict["item_line_id"] = line.id
|
||||
id_dict["tenant_id"] = tenant_id
|
||||
id_dict["company_id"] = company_id
|
||||
db.add(IdentifierDetail(**id_dict))
|
||||
|
||||
@staticmethod
|
||||
def _attach_series(db: Session, item: LineItem) -> None:
|
||||
"""Query and attach all Serie rows for this item as a list."""
|
||||
@@ -206,6 +227,16 @@ class ItemService:
|
||||
)
|
||||
item.series = list(series)
|
||||
|
||||
@staticmethod
|
||||
def _attach_identifiers(db: Session, item: LineItem) -> None:
|
||||
"""Query and attach all IdentifierDetail rows for this item."""
|
||||
identifiers = (
|
||||
db.query(IdentifierDetail)
|
||||
.filter(IdentifierDetail.item_line_id == item.id)
|
||||
.all()
|
||||
)
|
||||
item.identifiers = list(identifiers)
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(
|
||||
db: Session, item_id: int, tenant_id: int, company_id: int
|
||||
@@ -232,6 +263,7 @@ class ItemService:
|
||||
)
|
||||
if result:
|
||||
ItemService._attach_series(db, result)
|
||||
ItemService._attach_identifiers(db, result)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
@@ -285,6 +317,7 @@ class ItemService:
|
||||
items = query.offset(skip).limit(limit).all()
|
||||
for item in items:
|
||||
ItemService._attach_series(db, item)
|
||||
ItemService._attach_identifiers(db, item)
|
||||
return items, total
|
||||
|
||||
@staticmethod
|
||||
@@ -318,6 +351,7 @@ class ItemService:
|
||||
items = query.offset(skip).limit(limit).all()
|
||||
for item in items:
|
||||
ItemService._attach_series(db, item)
|
||||
ItemService._attach_identifiers(db, item)
|
||||
return items, total
|
||||
|
||||
@staticmethod
|
||||
@@ -443,6 +477,7 @@ class ItemService:
|
||||
db.commit()
|
||||
db.refresh(db_item)
|
||||
ItemService._attach_series(db, db_item)
|
||||
ItemService._attach_identifiers(db, db_item)
|
||||
return db_item
|
||||
|
||||
except IntegrityError as e:
|
||||
@@ -591,6 +626,7 @@ class ItemService:
|
||||
).delete()
|
||||
db.query(FaLineItem).filter(FaLineItem.id == db_item.id).delete()
|
||||
db.query(Serie).filter(Serie.line_item_id == db_item.id).delete()
|
||||
db.query(IdentifierDetail).filter(IdentifierDetail.item_line_id == db_item.id).delete()
|
||||
db.flush()
|
||||
|
||||
# Create new nested data
|
||||
@@ -604,6 +640,7 @@ class ItemService:
|
||||
db.commit()
|
||||
db.refresh(db_item)
|
||||
ItemService._attach_series(db, db_item)
|
||||
ItemService._attach_identifiers(db, db_item)
|
||||
return db_item
|
||||
|
||||
except HTTPException:
|
||||
|
||||
Reference in New Issue
Block a user