feat(db): add datetime column dual-caret sort on inventory and weighing lists.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7579c36fc6
commit
100b0880f0
11 files changed
+42746
-40718
No files matched your search
@@ -15,6 +15,7 @@ from app.core.tenant_context import get_tenant_no
|
||||
from app.services.db_data_scope import apply_db_scope, assert_db_row_in_scope
|
||||
from app.services.db.inventory_id import next_inbound_outbound_id
|
||||
from app.services.db.inventory_list_filters import build_inventory_column_filters
|
||||
from app.services.db.list_sort import order_inventory, parse_sort_params
|
||||
from app.models.db_models import DbInboundOutbound, DbPurchasePlan, DbWeighingSlip
|
||||
from app.schemas.db.inventory import (
|
||||
CreateLinkForPlanPayload,
|
||||
@@ -226,10 +227,7 @@ def _fetch_inventory(
|
||||
db: Session, user: DbUserContext, params: dict
|
||||
) -> list[InboundOutboundRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(
|
||||
DbInboundOutbound.date.desc(),
|
||||
DbInboundOutbound.id.desc(),
|
||||
)
|
||||
query = _base_query(db, tenant_no)
|
||||
|
||||
q = _param_get(params, "q")
|
||||
if q:
|
||||
@@ -249,6 +247,11 @@ def _fetch_inventory(
|
||||
query = query.filter(*column_filters)
|
||||
|
||||
query = apply_db_scope(query, DbInboundOutbound, user, db, tenant_no)
|
||||
sf, so = parse_sort_params(params)
|
||||
query = query.order_by(
|
||||
order_inventory(sf, so, default=DbInboundOutbound.date.desc()),
|
||||
DbInboundOutbound.id.desc(),
|
||||
)
|
||||
inventories = query.all()
|
||||
inventory_ids = [inv.id for inv in inventories]
|
||||
weighing_map: dict[str, DbWeighingSlip] = {}
|
||||
@@ -355,6 +358,8 @@ class InventoryExportBody(BaseModel):
|
||||
date_to: Optional[str] = Field(default=None, alias="dateTo")
|
||||
status: Optional[str] = None
|
||||
business_type: Optional[str] = Field(default=None, alias="businessType")
|
||||
sort_field: Optional[str] = Field(default=None, alias="sortField")
|
||||
sort_order: Optional[str] = Field(default=None, alias="sortOrder")
|
||||
ids: Optional[List[str]] = None
|
||||
columns: Optional[List[str]] = None
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
@@ -25,6 +25,11 @@ from app.services.db.mappers import (
|
||||
plan_to_read,
|
||||
weighing_to_read,
|
||||
)
|
||||
from app.services.db.list_sort import (
|
||||
order_weighing,
|
||||
parse_sort_params,
|
||||
weighing_needs_inventory_join,
|
||||
)
|
||||
from app.services.db.weighing_list_filters import build_weighing_column_filters
|
||||
from app.services.db.weighing_logic import process_weighing_business_logic
|
||||
from app.services.db.product_helpers import (
|
||||
@@ -107,7 +112,7 @@ def _fetch_weighing(
|
||||
db: Session, user: DbUserContext, params: dict
|
||||
) -> list[WeighingSlipRead]:
|
||||
tenant_no = get_tenant_no(db, user.to_tenant_user_dict())
|
||||
query = _base_query(db, tenant_no).order_by(DbWeighingSlip.gross_weight_time.desc())
|
||||
query = _base_query(db, tenant_no)
|
||||
|
||||
q = _param_get(params, "q")
|
||||
if q:
|
||||
@@ -128,6 +133,19 @@ def _fetch_weighing(
|
||||
query = query.filter(*column_filters)
|
||||
|
||||
query = apply_db_scope(query, DbWeighingSlip, user, db, tenant_no)
|
||||
sf, so = parse_sort_params(params)
|
||||
if weighing_needs_inventory_join(sf, so):
|
||||
query = query.outerjoin(
|
||||
DbInboundOutbound,
|
||||
and_(
|
||||
DbWeighingSlip.inbound_outbound_id == DbInboundOutbound.id,
|
||||
DbInboundOutbound.del_flag == 0,
|
||||
),
|
||||
)
|
||||
query = query.order_by(
|
||||
order_weighing(sf, so, default=DbWeighingSlip.gross_weight_time.desc()),
|
||||
DbWeighingSlip.id.desc(),
|
||||
)
|
||||
weighings = query.all()
|
||||
inventory_ids = [w.inbound_outbound_id for w in weighings if w.inbound_outbound_id]
|
||||
inventory_date_map: dict[str, str] = {}
|
||||
@@ -277,6 +295,8 @@ class WeighingExportBody(BaseModel):
|
||||
supplier: Optional[str] = None
|
||||
consignee: Optional[str] = None
|
||||
business_type: Optional[str] = Field(default=None, alias="businessType")
|
||||
sort_field: Optional[str] = Field(default=None, alias="sortField")
|
||||
sort_order: Optional[str] = Field(default=None, alias="sortOrder")
|
||||
ids: Optional[List[str]] = None
|
||||
columns: Optional[List[str]] = None
|
||||
|
||||
|
||||
Reference in new issue
Block a user