|
|
@@ -59,7 +59,7 @@ class WorkRecordService:
|
|
|
|
|
|
@staticmethod
|
|
|
def get_all(person_id=None, start_date=None, end_date=None, year=None, month=None, is_settled=None,
|
|
|
- page=1, page_size=20):
|
|
|
+ supplier_id=None, page=1, page_size=20):
|
|
|
"""Get work records with pagination and filters.
|
|
|
|
|
|
All filters are applied as intersection (AND logic).
|
|
|
@@ -71,6 +71,7 @@ class WorkRecordService:
|
|
|
year: Filter by year (optional, used with month)
|
|
|
month: Filter by month 1-12 (optional, used with year)
|
|
|
is_settled: Filter by settlement status (optional, True/False)
|
|
|
+ supplier_id: Filter by supplier ID (optional), use "none" to filter items without supplier
|
|
|
page: Page number, starting from 1 (default: 1)
|
|
|
page_size: Number of records per page (default: 20, max: 100)
|
|
|
|
|
|
@@ -97,6 +98,15 @@ class WorkRecordService:
|
|
|
if is_settled is not None:
|
|
|
query = query.filter(WorkRecord.is_settled == is_settled)
|
|
|
|
|
|
+ # Apply supplier filter
|
|
|
+ if supplier_id is not None:
|
|
|
+ query = query.join(Item)
|
|
|
+ if supplier_id == 'none':
|
|
|
+ # Filter items without supplier
|
|
|
+ query = query.filter(Item.supplier_id.is_(None))
|
|
|
+ else:
|
|
|
+ query = query.filter(Item.supplier_id == supplier_id)
|
|
|
+
|
|
|
# Apply month filter if both year and month are provided
|
|
|
if year is not None and month is not None:
|
|
|
month_start = date(year, month, 1)
|
|
|
@@ -485,6 +495,31 @@ class WorkRecordService:
|
|
|
reverse=True
|
|
|
)
|
|
|
|
|
|
+ # Group by person and item for unsettled breakdown only
|
|
|
+ unsettled_item_breakdown_dict = {}
|
|
|
+ for wr in work_records:
|
|
|
+ if not wr.is_settled:
|
|
|
+ key = (wr.person_id, wr.item_id)
|
|
|
+ if key not in unsettled_item_breakdown_dict:
|
|
|
+ supplier_name = wr.item.supplier.name if wr.item.supplier else ''
|
|
|
+ unsettled_item_breakdown_dict[key] = {
|
|
|
+ 'person_id': wr.person_id,
|
|
|
+ 'person_name': wr.person.name,
|
|
|
+ 'item_id': wr.item_id,
|
|
|
+ 'item_name': wr.item.name,
|
|
|
+ 'supplier_name': supplier_name,
|
|
|
+ 'quantity': 0,
|
|
|
+ 'earnings': 0.0
|
|
|
+ }
|
|
|
+ unsettled_item_breakdown_dict[key]['quantity'] += wr.quantity
|
|
|
+ unsettled_item_breakdown_dict[key]['earnings'] += wr.total_price
|
|
|
+
|
|
|
+ # Sort by person_name, then item_name
|
|
|
+ unsettled_item_breakdown = sorted(
|
|
|
+ unsettled_item_breakdown_dict.values(),
|
|
|
+ key=lambda x: (x['person_name'], x['item_name'])
|
|
|
+ )
|
|
|
+
|
|
|
# Group by person, supplier, and settlement status for supplier_breakdown
|
|
|
person_supplier_earnings = {}
|
|
|
for wr in work_records:
|
|
|
@@ -518,6 +553,7 @@ class WorkRecordService:
|
|
|
'unsettled_earnings': unsettled_earnings,
|
|
|
'top_performers': top_performers,
|
|
|
'item_breakdown': item_breakdown,
|
|
|
+ 'unsettled_item_breakdown': unsettled_item_breakdown,
|
|
|
'supplier_breakdown': supplier_breakdown
|
|
|
}
|
|
|
|