fix sort order for contract recap
All checks were successful
Deploy Amap / deploy (push) Successful in 15s
All checks were successful
Deploy Amap / deploy (push) Successful in 15s
This commit is contained in:
@@ -268,8 +268,11 @@ def get_contract_recap(
|
||||
form = form_service.get_one(session, form_id=form_id)
|
||||
contracts = service.get_all(session, user, forms=[form.name])
|
||||
filename = f'{form.name}_recapitulatif_contrats.ods'
|
||||
recap = generate_recap(contracts, form)
|
||||
if recap is None:
|
||||
raise HTTPException(status_code=404)
|
||||
return StreamingResponse(
|
||||
io.BytesIO(generate_recap(contracts, form)),
|
||||
io.BytesIO(recap),
|
||||
media_type='application/vnd.oasis.opendocument.spreadsheet',
|
||||
headers={
|
||||
'Content-Disposition': (
|
||||
|
||||
@@ -412,20 +412,34 @@ def generate_recap(
|
||||
'2': 'Kg',
|
||||
'3': 'Piece'
|
||||
}
|
||||
if len(contracts) <= 0:
|
||||
# TODO: raise correct exception
|
||||
return None
|
||||
first_contract = contracts[0]
|
||||
reccurents_sorted = sorted(
|
||||
[
|
||||
product for product in first_contract.products
|
||||
if product.product.type == models.ProductType.RECCURENT
|
||||
],
|
||||
key=lambda x: x.product.name
|
||||
)
|
||||
recurrents = [
|
||||
f'{pr.name}{f' - {pr.quantity}{pr.quantity_unit}'
|
||||
if pr.quantity else ''} ({product_unit_map[pr.unit]})'
|
||||
for pr in form.productor.products
|
||||
if pr.type == models.ProductType.RECCURENT
|
||||
for pr in reccurents_sorted
|
||||
]
|
||||
recurrents.sort()
|
||||
occasionnals_sorted = sorted(
|
||||
[
|
||||
product for product in first_contract.products
|
||||
if product.product.type == models.ProductType.OCCASIONAL
|
||||
],
|
||||
key=lambda x: (x.shipment.name, x.product.name)
|
||||
)
|
||||
occasionnals = [
|
||||
f'{pr.name}{f' - {pr.quantity}{pr.quantity_unit}'
|
||||
if pr.quantity else ''} ({product_unit_map[pr.unit]})'
|
||||
for pr in form.productor.products
|
||||
if pr.type == models.ProductType.OCCASIONAL
|
||||
for pr in occasionnals_sorted
|
||||
]
|
||||
occasionnals.sort()
|
||||
shipments = form.shipments
|
||||
occasionnals_header = [
|
||||
occ for shipment in shipments for occ in occasionnals
|
||||
|
||||
Reference in New Issue
Block a user