| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- """生产物料库 API"""
- from typing import List, Optional
- from datetime import datetime
- from decimal import Decimal
- from fastapi import APIRouter, Depends, HTTPException, Query
- from pydantic import BaseModel, ConfigDict, Field
- from sqlalchemy.orm import Session
- from app.core.database import get_db
- from app.core.menu_guard import require_any_menu_code, require_menu_code
- from app.core.response import error_response, success_response
- from app.core.utils import parse_datetime
- from app.crud import production_material as mat_svc
- from app.crud import production_material_stock_batch as batch_svc
- from app.crud import production_material_stock_check as stock_check_svc
- from app.crud import production_material_usage as mu_svc
- from app.crud import production_material_formula as formula_svc
- from app.crud import production_material_prep as prep_svc
- from app.api.sample_reception import _get_current_user_id, _get_tenant_no, _page_payload
- router = APIRouter(tags=["生产物料库"])
- # ---------- 生产物料库 ----------
- class ProductionMaterialBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- material_type: str = Field(..., alias="materialType")
- material_code: str = Field(..., alias="materialCode")
- material_name: str = Field(..., alias="materialName")
- spec_model: Optional[str] = Field(None, alias="specModel")
- unit: Optional[str] = None
- warning_qty: Optional[float] = Field(0, alias="warningQty")
- status: str = "1"
- remark: Optional[str] = None
- class ProductionMaterialUpdateBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- material_type: Optional[str] = Field(None, alias="materialType")
- material_code: Optional[str] = Field(None, alias="materialCode")
- material_name: Optional[str] = Field(None, alias="materialName")
- spec_model: Optional[str] = Field(None, alias="specModel")
- unit: Optional[str] = None
- warning_qty: Optional[float] = Field(None, alias="warningQty")
- status: Optional[str] = None
- remark: Optional[str] = None
- class StockInBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- stock_in_no: str = Field(..., alias="stockInNo")
- stock_in_date: str = Field(..., alias="stockInDate")
- expire_date: str = Field(..., alias="expireDate")
- production_date: Optional[str] = Field(None, alias="productionDate")
- catalog_no: str = Field(..., alias="catalogNo")
- batch_no: str = Field(..., alias="batchNo")
- manufacturer: str
- storage_location: Optional[str] = Field(None, alias="storageLocation")
- unit_price: float = Field(..., alias="unitPrice")
- amount: float
- qty: float = Field(..., gt=0)
- remark: Optional[str] = None
- stock_in_time: Optional[str] = Field(None, alias="stockInTime")
- class BatchStockInItem(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- material_id: str = Field(..., alias="materialId")
- stock_in_no: str = Field(..., alias="stockInNo")
- stock_in_date: str = Field(..., alias="stockInDate")
- expire_date: str = Field(..., alias="expireDate")
- production_date: Optional[str] = Field(None, alias="productionDate")
- catalog_no: str = Field(..., alias="catalogNo")
- batch_no: str = Field(..., alias="batchNo")
- manufacturer: str
- storage_location: Optional[str] = Field(None, alias="storageLocation")
- unit_price: float = Field(..., alias="unitPrice")
- amount: float
- qty: float = Field(..., gt=0)
- remark: Optional[str] = None
- class BatchStockInBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- items: List[BatchStockInItem] = Field(..., min_length=1)
- remark: Optional[str] = None
- stock_in_time: Optional[str] = Field(None, alias="stockInTime")
- class BatchStockOutItem(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- batch_id: str = Field(..., alias="batchId")
- qty: float = Field(..., gt=0)
- remark: Optional[str] = None
- class BatchStockOutBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- items: List[BatchStockOutItem] = Field(..., min_length=1)
- receiver: Optional[str] = None
- purpose: Optional[str] = None
- remark: Optional[str] = None
- stock_out_time: Optional[str] = Field(None, alias="stockOutTime")
- class BatchDiscardItem(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- batch_id: str = Field(..., alias="batchId")
- qty: float = Field(..., gt=0)
- usage_unit: Optional[str] = Field(None, alias="usageUnit")
- remark: Optional[str] = None
- class BatchDiscardBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- items: List[BatchDiscardItem] = Field(..., min_length=1)
- discard_reason: str = Field(..., alias="discardReason")
- remark: Optional[str] = None
- discard_time: Optional[str] = Field(None, alias="discardTime")
- class ProductionMaterialBatchUpdateBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- stock_in_no: Optional[str] = Field(None, alias="stockInNo")
- stock_in_date: Optional[str] = Field(None, alias="stockInDate")
- expire_date: Optional[str] = Field(None, alias="expireDate")
- production_date: Optional[str] = Field(None, alias="productionDate")
- catalog_no: Optional[str] = Field(None, alias="catalogNo")
- batch_no: Optional[str] = Field(None, alias="batchNo")
- manufacturer: Optional[str] = None
- storage_location: Optional[str] = Field(None, alias="storageLocation")
- unit_price: Optional[float] = Field(None, alias="unitPrice")
- amount: Optional[float] = None
- remark: Optional[str] = None
- material_code: Optional[str] = Field(None, alias="materialCode")
- material_name: Optional[str] = Field(None, alias="materialName")
- spec_model: Optional[str] = Field(None, alias="specModel")
- unit: Optional[str] = None
- warning_qty: Optional[float] = Field(None, alias="warningQty")
- status: Optional[str] = None
- @router.get("/production-material-batches")
- async def list_production_material_batches(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- materialId: Optional[str] = None,
- materialType: Optional[str] = None,
- status: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = batch_svc.list_batches(
- db,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- material_id=materialId,
- material_type=materialType,
- status=status,
- tenant_no=tenant_no,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-batches/options")
- async def production_material_batch_options(
- keyword: Optional[str] = None,
- materialId: Optional[str] = None,
- materialType: Optional[str] = None,
- onlyAvailable: bool = Query(True, alias="onlyAvailable"),
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- return success_response(
- batch_svc.batch_options(
- db,
- tenant_no,
- keyword=keyword,
- material_id=materialId,
- material_type=materialType,
- only_available=onlyAvailable,
- )
- )
- except Exception as e:
- return error_response(400, str(e))
- @router.put("/production-material-batches/{batch_id}")
- async def update_production_material_batch(
- batch_id: str,
- body: ProductionMaterialBatchUpdateBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- data = body.model_dump(exclude_none=True)
- obj = batch_svc.update_batch(
- db,
- batch_id,
- tenant_no=tenant_no,
- user_id=user_id,
- **data,
- )
- material = mat_svc.get_material(db, obj.material_id, tenant_no=tenant_no)
- return success_response(
- batch_svc._batch_row_to_dict(obj, material),
- message="更新成功",
- )
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.delete("/production-material-batches/{batch_id}")
- async def delete_production_material_batch(
- batch_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- batch_svc.delete_batch(db, batch_id, tenant_no=tenant_no, user_id=user_id)
- return success_response(None, message="删除成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-materials")
- async def list_production_materials(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- materialType: Optional[str] = None,
- status: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mat_svc.list_materials(
- db,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- material_type=materialType,
- status=status,
- tenant_no=tenant_no,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-materials/options")
- async def production_material_options(
- materialType: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- return success_response(
- mat_svc.options_list(db, tenant_no=tenant_no, material_type=materialType)
- )
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-materials")
- async def create_production_material(
- body: ProductionMaterialBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- obj = mat_svc.create_material(
- db,
- material_type=body.material_type,
- material_code=body.material_code,
- material_name=body.material_name,
- spec_model=body.spec_model,
- unit=body.unit,
- warning_qty=Decimal(str(body.warning_qty))
- if body.warning_qty is not None
- else None,
- status=mat_svc._norm_status(body.status),
- remark=body.remark,
- tenant_no=tenant_no,
- user_id=user_id,
- )
- return success_response(mat_svc._row_to_dict(obj), message="创建成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.put("/production-materials/{material_id}")
- async def update_production_material(
- material_id: str,
- body: ProductionMaterialUpdateBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- data = body.model_dump(exclude_none=True)
- if "warning_qty" in data:
- data["warning_qty"] = Decimal(str(data["warning_qty"]))
- if "status" in data:
- data["status"] = mat_svc._norm_status(data["status"])
- obj = mat_svc.update_material(
- db,
- material_id,
- tenant_no=tenant_no,
- user_id=user_id,
- **data,
- )
- return success_response(mat_svc._row_to_dict(obj), message="更新成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.delete("/production-materials/{material_id}")
- async def delete_production_material(
- material_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- mat_svc.soft_delete_material(db, material_id, tenant_no=tenant_no)
- return success_response(None, message="删除成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-materials/batch-stock-in")
- async def batch_stock_in_production_material(
- body: BatchStockInBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- operate_time = datetime.now()
- if body.stock_in_time:
- rt = parse_datetime(body.stock_in_time)
- if not rt:
- return error_response(400, "入库时间格式无效")
- operate_time = rt
- items = [
- {
- "material_id": x.material_id,
- "materialId": x.material_id,
- "stock_in_no": x.stock_in_no,
- "stockInNo": x.stock_in_no,
- "stock_in_date": x.stock_in_date,
- "stockInDate": x.stock_in_date,
- "expire_date": x.expire_date,
- "expireDate": x.expire_date,
- "catalog_no": x.catalog_no,
- "catalogNo": x.catalog_no,
- "batch_no": x.batch_no,
- "batchNo": x.batch_no,
- "manufacturer": x.manufacturer,
- "storage_location": x.storage_location,
- "storageLocation": x.storage_location,
- "unit_price": x.unit_price,
- "unitPrice": x.unit_price,
- "amount": x.amount,
- "qty": x.qty,
- "remark": x.remark,
- }
- for x in body.items
- ]
- count = batch_svc.batch_stock_in(
- db,
- items=items,
- remark=body.remark,
- tenant_no=tenant_no,
- user_id=user_id,
- operate_time=operate_time,
- )
- return success_response({"count": count}, message=f"批量入库成功,共 {count} 项")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-materials/batch-stock-out")
- async def batch_stock_out_production_material(
- body: BatchStockOutBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- operate_time = datetime.now()
- if body.stock_out_time:
- rt = parse_datetime(body.stock_out_time)
- if not rt:
- return error_response(400, "出库时间格式无效")
- operate_time = rt
- items = [
- {"batch_id": x.batch_id, "batchId": x.batch_id, "qty": x.qty, "remark": x.remark}
- for x in body.items
- ]
- count = batch_svc.batch_stock_out(
- db,
- items=items,
- receiver=body.receiver,
- purpose=body.purpose,
- remark=body.remark,
- tenant_no=tenant_no,
- user_id=user_id,
- operate_time=operate_time,
- )
- return success_response({"count": count}, message=f"批量出库成功,共 {count} 项")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-materials/batch-discard")
- async def batch_discard_production_material(
- body: BatchDiscardBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- operate_time = datetime.now()
- if body.discard_time:
- rt = parse_datetime(body.discard_time)
- if not rt:
- return error_response(400, "废弃时间格式无效")
- operate_time = rt
- items = [
- {
- "batch_id": x.batch_id,
- "batchId": x.batch_id,
- "qty": x.qty,
- "usageUnit": x.usage_unit,
- "usage_unit": x.usage_unit,
- "remark": x.remark,
- }
- for x in body.items
- ]
- count = batch_svc.batch_discard(
- db,
- items=items,
- discard_reason=body.discard_reason,
- remark=body.remark,
- tenant_no=tenant_no,
- user_id=user_id,
- operate_time=operate_time,
- )
- return success_response({"count": count}, message=f"批量废弃成功,共 {count} 项")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-materials/{material_id}/stock-in")
- async def stock_in_production_material(
- material_id: str,
- body: StockInBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- operate_time = datetime.now()
- if body.stock_in_time:
- rt = parse_datetime(body.stock_in_time)
- if not rt:
- return error_response(400, "入库时间格式无效")
- operate_time = rt
- from decimal import Decimal
- obj = batch_svc.stock_in_one(
- db,
- material_id,
- stock_in_no=body.stock_in_no,
- stock_in_date=body.stock_in_date,
- expire_date=body.expire_date,
- catalog_no=body.catalog_no,
- batch_no=body.batch_no,
- manufacturer=body.manufacturer,
- storage_location=body.storage_location,
- unit_price=body.unit_price,
- amount=body.amount,
- qty=Decimal(str(body.qty)),
- remark=body.remark,
- production_date=body.production_date,
- tenant_no=tenant_no,
- user_id=user_id,
- operate_time=operate_time,
- )
- return success_response(batch_svc._batch_row_to_dict(obj), message="入库成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-batches/batch-stock-in")
- async def batch_stock_in_production_material_batches(
- body: BatchStockInBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """批量入库(与 /production-materials/batch-stock-in 等价)。"""
- return await batch_stock_in_production_material(body, db, user)
- @router.post("/production-material-batches/batch-stock-out")
- async def batch_stock_out_production_material_batches(
- body: BatchStockOutBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """批量出库(与 /production-materials/batch-stock-out 等价)。"""
- return await batch_stock_out_production_material(body, db, user)
- @router.post("/production-material-batches/batch-discard")
- async def batch_discard_production_material_batches(
- body: BatchDiscardBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """批量废弃(与 /production-materials/batch-discard 等价)。"""
- return await batch_discard_production_material(body, db, user)
- # ---------- 物料变动记录 ----------
- @router.get("/production-material-requisition-options")
- async def list_production_material_requisition_options(
- materialType: Optional[str] = None,
- keyword: Optional[str] = None,
- limit: int = Query(50, ge=1, le=100),
- db: Session = Depends(get_db),
- user: dict = Depends(
- require_any_menu_code(
- "production:work-order:list",
- "production:my-task:list",
- "production:material:list",
- )
- ),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items = mu_svc.list_requisition_options(
- db,
- tenant_no,
- keyword=keyword,
- material_type=materialType,
- limit=limit,
- user_info=user,
- )
- return success_response(items)
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-usages")
- async def list_production_material_usages(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- materialId: Optional[str] = None,
- materialType: Optional[str] = None,
- changeType: Optional[str] = None,
- startTime: Optional[str] = None,
- endTime: Optional[str] = None,
- keyword: Optional[str] = None,
- stockInNo: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mu_svc.list_usages(
- db, tenant_no, page=page, page_size=pageSize,
- material_id=materialId, material_type=materialType, change_type=changeType,
- start_time=startTime, end_time=endTime,
- keyword=keyword, stock_in_no=stockInNo,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-usages/{usage_id}/mark-fully-used")
- async def mark_production_material_usage_fully_used(
- usage_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """标记领用记录已使用完(盘点登记 / 变动记录)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- mu_svc.mark_requisition_fully_used(
- db,
- usage_id,
- tenant_no=tenant_no,
- user_id=user_id,
- user_info=user,
- )
- return success_response(None, message="已标记为使用完")
- except PermissionError as e:
- return error_response(403, str(e))
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/my-production-material-usages")
- async def list_my_production_material_usages(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- materialId: Optional[str] = None,
- materialType: Optional[str] = None,
- changeType: Optional[str] = None,
- startTime: Optional[str] = None,
- endTime: Optional[str] = None,
- keyword: Optional[str] = None,
- stockInNo: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:my-material-usage:list")),
- ):
- """当前登录人的物料领用记录(手动出库领用人 / 任务使用填报人)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mu_svc.list_my_usages(
- db,
- tenant_no,
- user_id=_get_current_user_id(user),
- nick_name=(user.get("real_name") or "").strip(),
- page=page,
- page_size=pageSize,
- material_id=materialId,
- material_type=materialType,
- change_type=changeType,
- start_time=startTime,
- end_time=endTime,
- keyword=keyword,
- stock_in_no=stockInNo,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-usages/{usage_id}/linked-step-usages")
- async def list_production_material_linked_step_usages(
- usage_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(
- require_any_menu_code("production:material:list", "production:my-material-usage:list")
- ),
- ):
- """领用记录下关联的消耗明细(生产任务 step_save / 配制 prep_consume)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items = mu_svc.list_linked_step_usages(
- db, usage_id, tenant_no, user_info=user
- )
- return success_response(items)
- except HTTPException as e:
- detail = e.detail
- msg = detail if isinstance(detail, str) else str(detail)
- return error_response(e.status_code, msg)
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- # ---------- 库存盘点 ----------
- class StockCheckCreateBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- title: str
- check_time: Optional[str] = Field(None, alias="checkTime")
- remark: Optional[str] = None
- class StockCheckUnusedLineItem(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- usage_id: Optional[str] = Field(None, alias="usageId")
- batch_id: Optional[str] = Field(None, alias="batchId")
- material_code: Optional[str] = Field(None, alias="materialCode")
- material_name: str = Field(..., alias="materialName")
- spec_model: Optional[str] = Field(None, alias="specModel")
- unit: Optional[str] = None
- stock_in_no: Optional[str] = Field(None, alias="stockInNo")
- stock_in_date: Optional[str] = Field(None, alias="stockInDate")
- expire_date: Optional[str] = Field(None, alias="expireDate")
- catalog_no: Optional[str] = Field(None, alias="catalogNo")
- batch_no: Optional[str] = Field(None, alias="batchNo")
- manufacturer: Optional[str] = None
- unit_price: Optional[float] = Field(None, alias="unitPrice")
- holder: Optional[str] = None
- unused_qty: float = Field(..., alias="unusedQty", gt=0)
- remark: Optional[str] = None
- class StockCheckUnusedLinesBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- items: List[StockCheckUnusedLineItem] = Field(default_factory=list)
- @router.get("/production-material-stock-check-requisitions")
- async def list_production_material_stock_check_requisitions(
- page: int = Query(1, ge=1),
- pageSize: int = Query(50, ge=1, le=200),
- keyword: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """盘点登记:待处理领用记录(未标记已使用完)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mu_svc.list_stock_check_requisitions(
- db,
- tenant_no,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-stock-checks")
- async def list_production_material_stock_checks(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- status: Optional[str] = None,
- startTime: Optional[str] = None,
- endTime: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = stock_check_svc.list_checks(
- db,
- tenant_no,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- status=status,
- start_time=startTime,
- end_time=endTime,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except PermissionError as e:
- return error_response(403, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-stock-checks")
- async def create_production_material_stock_check(
- body: StockCheckCreateBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- res = stock_check_svc.create_check(
- db,
- title=body.title,
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- check_time=body.check_time,
- remark=body.remark,
- )
- return success_response(res, message="盘点单创建成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-stock-checks/{check_id}")
- async def get_production_material_stock_check(
- check_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- data = stock_check_svc.get_check_detail(db, check_id, tenant_no)
- return success_response(data)
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.put("/production-material-stock-checks/{check_id}/unused-lines")
- async def save_production_material_stock_check_unused_lines(
- check_id: str,
- body: StockCheckUnusedLinesBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- count = stock_check_svc.save_unused_lines(
- db,
- check_id,
- items=[x.model_dump(by_alias=False) for x in body.items],
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response({"count": count}, message="登记保存成功")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-stock-checks/{check_id}/complete")
- async def complete_production_material_stock_check(
- check_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- stock_check_svc.complete_check(db, check_id, tenant_no=tenant_no, user_id=user_id)
- return success_response(None, message="盘点已完成")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-stock-checks/{check_id}/cancel")
- async def cancel_production_material_stock_check(
- check_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- stock_check_svc.cancel_check(db, check_id, tenant_no=tenant_no, user_id=user_id)
- return success_response(None, message="盘点已作废")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- # ---------- 配置试剂配方 ----------
- class FormulaLineBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- component_material_id: str = Field(..., alias="componentMaterialId")
- component_material_type: str = Field(..., alias="componentMaterialType")
- standard_qty: float = Field(..., gt=0, alias="standardQty")
- usage_unit: Optional[str] = Field(None, alias="usageUnit")
- remark: Optional[str] = None
- class FormulaReplaceBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- lines: List[FormulaLineBody] = Field(default_factory=list)
- @router.get("/production-materials/{material_id}/formula")
- async def get_production_material_formula(
- material_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items = formula_svc.get_formula(db, material_id, tenant_no)
- return success_response(items)
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.put("/production-materials/{material_id}/formula")
- async def replace_production_material_formula(
- material_id: str,
- body: FormulaReplaceBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- lines = [ln.model_dump(by_alias=True) for ln in body.lines]
- items = formula_svc.replace_formula(
- db,
- material_id,
- lines=lines,
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response(items, message="配方已保存")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- # ---------- 配置试剂配制单 ----------
- class PrepLineBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- component_material_id: str = Field(..., alias="componentMaterialId")
- component_material_type: str = Field(..., alias="componentMaterialType")
- requisition_usage_id: str = Field(..., alias="requisitionUsageId")
- actual_qty: float = Field(..., gt=0, alias="actualQty")
- usage_unit: Optional[str] = Field(None, alias="usageUnit")
- remark: Optional[str] = None
- class PrepBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- configured_material_id: str = Field(..., alias="configuredMaterialId")
- prep_time: Optional[str] = Field(None, alias="prepTime")
- output_qty: float = Field(..., gt=0, alias="outputQty")
- output_unit: Optional[str] = Field(None, alias="outputUnit")
- batch_no: str = Field(..., alias="batchNo")
- storage_location: Optional[str] = Field(None, alias="storageLocation")
- expire_date: str = Field(..., alias="expireDate")
- remark: Optional[str] = None
- lines: List[PrepLineBody] = Field(..., min_length=1)
- class PrepUpdateBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- prep_time: Optional[str] = Field(None, alias="prepTime")
- output_qty: Optional[float] = Field(None, gt=0, alias="outputQty")
- output_unit: Optional[str] = Field(None, alias="outputUnit")
- batch_no: Optional[str] = Field(None, alias="batchNo")
- storage_location: Optional[str] = Field(None, alias="storageLocation")
- expire_date: Optional[str] = Field(None, alias="expireDate")
- remark: Optional[str] = None
- lines: Optional[List[PrepLineBody]] = None
- @router.get("/production-material-preps")
- async def list_production_material_preps(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- status: Optional[str] = None,
- configuredMaterialId: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = prep_svc.list_preps(
- db,
- tenant_no,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- status=status,
- configured_material_id=configuredMaterialId,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except Exception as e:
- return error_response(400, str(e))
- class AvailableMaterialBatch(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- requisitionUsageId: str = Field(..., alias="requisitionUsageId")
- batchId: str = Field(..., alias="batchId")
- materialId: str = Field(..., alias="materialId")
- materialType: str = Field(..., alias="materialType")
- materialCode: str = Field(..., alias="materialCode")
- materialName: str = Field(..., alias="materialName")
- specModel: Optional[str] = Field(None, alias="specModel")
- unit: Optional[str] = None
- stockInNo: str = Field(..., alias="stockInNo")
- stockInDate: Optional[str] = Field(None, alias="stockInDate")
- expireDate: Optional[str] = Field(None, alias="expireDate")
- catalogNo: Optional[str] = Field(None, alias="catalogNo")
- batchNo: str = Field(..., alias="batchNo")
- manufacturer: str
- storageLocation: Optional[str] = Field(None, alias="storageLocation")
- requisitionQty: float = Field(..., alias="requisitionQty")
- unitPrice: Optional[float] = Field(None, alias="unitPrice")
- receiver: Optional[str] = None
- receiverName: Optional[str] = Field(None, alias="receiverName")
- useTime: Optional[str] = Field(None, alias="useTime")
- @router.get("/production-material-preps/available-materials")
- async def list_available_materials_for_prep(
- page: int = Query(1, ge=1),
- pageSize: int = Query(50, ge=1, le=200),
- keyword: Optional[str] = None,
- materialType: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """获取可用于配制的领用记录(有剩余、未标记已用完,须注册在 {prep_id} 路由之前)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = prep_svc.list_available_material_batches(
- db,
- tenant_no,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- material_type=materialType,
- user_info=user,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-preps/{prep_id}")
- async def get_production_material_prep(
- prep_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- data = prep_svc.get_prep_detail(db, prep_id, tenant_no)
- return success_response(data)
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-preps")
- async def create_production_material_prep(
- body: PrepBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- data = prep_svc.create_prep(
- db,
- data=body.model_dump(by_alias=True),
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response(data, message="配制单已创建")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.put("/production-material-preps/{prep_id}")
- async def update_production_material_prep(
- prep_id: str,
- body: PrepUpdateBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- data = prep_svc.update_prep(
- db,
- prep_id,
- data=body.model_dump(by_alias=True, exclude_none=True),
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response(data, message="配制单已更新")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-preps/{prep_id}/complete")
- async def complete_production_material_prep(
- prep_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- data = prep_svc.complete_prep(
- db,
- prep_id,
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response(data, message="配制已完成")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.post("/production-material-preps/{prep_id}/cancel")
- async def cancel_production_material_prep(
- prep_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- prep_svc.cancel_prep(db, prep_id, tenant_no=tenant_no, user_id=user_id)
- return success_response(None, message="配制单已作废")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.delete("/production-material-preps/{prep_id}")
- async def delete_production_material_prep(
- prep_id: str,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- prep_svc.delete_prep(db, prep_id, tenant_no=tenant_no, user_id=user_id)
- return success_response(None, message="配制单已删除")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- # ---------- 一次性配制(简化流程)----------
- class QuickPrepLineBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- requisitionUsageId: str = Field(..., alias="requisitionUsageId")
- componentMaterialId: str = Field(..., alias="componentMaterialId")
- componentMaterialType: str = Field(..., alias="componentMaterialType")
- batchId: Optional[str] = Field(None, alias="batchId")
- consumptionQty: float = Field(..., gt=0, alias="consumptionQty")
- usageUnit: Optional[str] = Field(None, alias="usageUnit")
- remark: Optional[str] = None
- class QuickPrepBody(BaseModel):
- model_config = ConfigDict(populate_by_name=True)
- configuredMaterialName: str = Field(..., max_length=128, alias="configuredMaterialName")
- outputQty: float = Field(..., gt=0, alias="outputQty")
- lines: List[QuickPrepLineBody] = Field(..., min_length=1)
- specModel: Optional[str] = Field(None, alias="specModel")
- outputUnit: Optional[str] = Field(None, alias="outputUnit")
- stockInNo: Optional[str] = Field(None, alias="stockInNo")
- stockInDate: Optional[str] = Field(None, alias="stockInDate")
- catalogNo: Optional[str] = Field(None, alias="catalogNo")
- batchNo: Optional[str] = Field(None, alias="batchNo")
- manufacturer: Optional[str] = Field(None, alias="manufacturer")
- storageLocation: Optional[str] = Field(None, alias="storageLocation")
- expireDate: Optional[str] = Field(None, alias="expireDate")
- remark: Optional[str] = None
- @router.post("/production-material-preps/quick")
- async def quick_production_material_prep(
- body: QuickPrepBody,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """一次性配制:提交即完成(子物料扣减 → 配置试剂入库 → 自动出库)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- user_id = _get_current_user_id(user)
- dept_id = user.get("deptId") or user.get("dept_id") or ""
- data = prep_svc.create_quick_prep(
- db,
- data=body.model_dump(by_alias=True),
- tenant_no=tenant_no,
- user_id=user_id,
- dept_id=dept_id,
- )
- return success_response(data, message="配置试剂配制完成")
- except ValueError as e:
- return error_response(400, str(e))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/production-material-consumptions")
- async def list_production_material_consumptions(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- materialType: Optional[str] = None,
- startTime: Optional[str] = None,
- endTime: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:material:list")),
- ):
- """查询配制消耗明细(子物料消耗记录)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mu_svc.list_prep_consumptions(
- db,
- tenant_no,
- page=page,
- page_size=pageSize,
- keyword=keyword,
- material_type=materialType,
- start_time=startTime,
- end_time=endTime,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except Exception as e:
- return error_response(400, str(e))
- @router.get("/my-production-material-consumptions")
- async def list_my_production_material_consumptions(
- page: int = Query(1, ge=1),
- pageSize: int = Query(10, ge=1, le=100),
- keyword: Optional[str] = None,
- materialType: Optional[str] = None,
- startTime: Optional[str] = None,
- endTime: Optional[str] = None,
- db: Session = Depends(get_db),
- user: dict = Depends(require_menu_code("production:my-material-usage:list")),
- ):
- """当前登录人的配制消耗明细(我的物料领用记录 - 消耗明细 Tab)。"""
- try:
- tenant_no = _get_tenant_no(db, user)
- items, total = mu_svc.list_my_prep_consumptions(
- db,
- tenant_no,
- user_id=_get_current_user_id(user),
- nick_name=(user.get("real_name") or "").strip(),
- page=page,
- page_size=pageSize,
- keyword=keyword,
- material_type=materialType,
- start_time=startTime,
- end_time=endTime,
- )
- return success_response(_page_payload(items, total, page, pageSize))
- except Exception as e:
- return error_response(400, str(e))
|