Spaces:
Runtime error
Runtime error
Commit ·
6730276
1
Parent(s): d37b170
PO decimal issue
Browse files- app/sales/orders/services/service.py +14 -15
- app/utils/mongodb.py +31 -0
app/sales/orders/services/service.py
CHANGED
|
@@ -10,6 +10,7 @@ import secrets
|
|
| 10 |
|
| 11 |
from app.nosql import get_database
|
| 12 |
from app.constants.collections import SCM_SALES_ORDERS_COLLECTION
|
|
|
|
| 13 |
from app.sales.orders.schemas.schema import (
|
| 14 |
SalesOrderCreate,
|
| 15 |
SalesOrderUpdate,
|
|
@@ -150,13 +151,11 @@ class SalesOrderService:
|
|
| 150 |
item.tax_percent,
|
| 151 |
item.discount_percent
|
| 152 |
)
|
| 153 |
-
# Convert Decimal to float for MongoDB
|
| 154 |
-
item_dict["unit_price"] = float(item_dict["unit_price"])
|
| 155 |
-
item_dict["tax_percent"] = float(item_dict["tax_percent"])
|
| 156 |
-
item_dict["discount_percent"] = float(item_dict["discount_percent"])
|
| 157 |
-
item_dict["line_total"] = float(item_dict["line_total"])
|
| 158 |
items_data.append(item_dict)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
| 160 |
# Calculate summary
|
| 161 |
summary = calculate_order_summary(items_data)
|
| 162 |
|
|
@@ -188,20 +187,20 @@ class SalesOrderService:
|
|
| 188 |
"items": items_data,
|
| 189 |
|
| 190 |
# Financial summary
|
| 191 |
-
"summary":
|
| 192 |
|
| 193 |
# Payment
|
| 194 |
-
"payment": {
|
| 195 |
"payment_type": payload.payment.payment_type.value,
|
| 196 |
"payment_status": payment_status,
|
| 197 |
"payment_method": payload.payment.payment_method,
|
| 198 |
"payment_date": payload.payment.payment_date.isoformat() if payload.payment.payment_date else None,
|
| 199 |
"payment_reference": payload.payment.payment_reference,
|
| 200 |
-
"amount_paid":
|
| 201 |
-
"amount_due":
|
| 202 |
"credit_terms": payload.payment.credit_terms,
|
| 203 |
-
"credit_limit":
|
| 204 |
-
},
|
| 205 |
|
| 206 |
# Fulfillment
|
| 207 |
"fulfillment_status": FulfillmentStatus.PENDING.value,
|
|
@@ -378,17 +377,17 @@ class SalesOrderService:
|
|
| 378 |
for item in update_data["items"]:
|
| 379 |
item_dict = item if isinstance(item, dict) else item.dict()
|
| 380 |
item_dict["id"] = item_dict.get("id") or f"item_{secrets.token_urlsafe(8)}"
|
| 381 |
-
item_dict["line_total"] =
|
| 382 |
item_dict["quantity"],
|
| 383 |
Decimal(str(item_dict["unit_price"])),
|
| 384 |
Decimal(str(item_dict.get("tax_percent", 0))),
|
| 385 |
Decimal(str(item_dict.get("discount_percent", 0)))
|
| 386 |
-
)
|
| 387 |
items_data.append(item_dict)
|
| 388 |
|
| 389 |
-
update_data["items"] = items_data
|
| 390 |
summary = calculate_order_summary(items_data)
|
| 391 |
-
update_data["summary"] =
|
| 392 |
|
| 393 |
update_data["updated_at"] = datetime.utcnow().isoformat()
|
| 394 |
|
|
|
|
| 10 |
|
| 11 |
from app.nosql import get_database
|
| 12 |
from app.constants.collections import SCM_SALES_ORDERS_COLLECTION
|
| 13 |
+
from app.utils.mongodb import convert_decimals_to_float
|
| 14 |
from app.sales.orders.schemas.schema import (
|
| 15 |
SalesOrderCreate,
|
| 16 |
SalesOrderUpdate,
|
|
|
|
| 151 |
item.tax_percent,
|
| 152 |
item.discount_percent
|
| 153 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
items_data.append(item_dict)
|
| 155 |
|
| 156 |
+
# Convert all Decimal objects to float for MongoDB compatibility
|
| 157 |
+
items_data = convert_decimals_to_float(items_data)
|
| 158 |
+
|
| 159 |
# Calculate summary
|
| 160 |
summary = calculate_order_summary(items_data)
|
| 161 |
|
|
|
|
| 187 |
"items": items_data,
|
| 188 |
|
| 189 |
# Financial summary
|
| 190 |
+
"summary": convert_decimals_to_float(summary),
|
| 191 |
|
| 192 |
# Payment
|
| 193 |
+
"payment": convert_decimals_to_float({
|
| 194 |
"payment_type": payload.payment.payment_type.value,
|
| 195 |
"payment_status": payment_status,
|
| 196 |
"payment_method": payload.payment.payment_method,
|
| 197 |
"payment_date": payload.payment.payment_date.isoformat() if payload.payment.payment_date else None,
|
| 198 |
"payment_reference": payload.payment.payment_reference,
|
| 199 |
+
"amount_paid": amount_paid,
|
| 200 |
+
"amount_due": amount_due,
|
| 201 |
"credit_terms": payload.payment.credit_terms,
|
| 202 |
+
"credit_limit": payload.payment.credit_limit
|
| 203 |
+
}),
|
| 204 |
|
| 205 |
# Fulfillment
|
| 206 |
"fulfillment_status": FulfillmentStatus.PENDING.value,
|
|
|
|
| 377 |
for item in update_data["items"]:
|
| 378 |
item_dict = item if isinstance(item, dict) else item.dict()
|
| 379 |
item_dict["id"] = item_dict.get("id") or f"item_{secrets.token_urlsafe(8)}"
|
| 380 |
+
item_dict["line_total"] = calculate_line_total(
|
| 381 |
item_dict["quantity"],
|
| 382 |
Decimal(str(item_dict["unit_price"])),
|
| 383 |
Decimal(str(item_dict.get("tax_percent", 0))),
|
| 384 |
Decimal(str(item_dict.get("discount_percent", 0)))
|
| 385 |
+
)
|
| 386 |
items_data.append(item_dict)
|
| 387 |
|
| 388 |
+
update_data["items"] = convert_decimals_to_float(items_data)
|
| 389 |
summary = calculate_order_summary(items_data)
|
| 390 |
+
update_data["summary"] = convert_decimals_to_float(summary)
|
| 391 |
|
| 392 |
update_data["updated_at"] = datetime.utcnow().isoformat()
|
| 393 |
|
app/utils/mongodb.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MongoDB utility functions for handling data type conversions.
|
| 3 |
+
"""
|
| 4 |
+
from typing import Any
|
| 5 |
+
from decimal import Decimal
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def convert_decimals_to_float(obj: Any) -> Any:
|
| 9 |
+
"""
|
| 10 |
+
Recursively convert Decimal objects to float for MongoDB compatibility.
|
| 11 |
+
|
| 12 |
+
MongoDB/BSON cannot encode Python Decimal objects directly.
|
| 13 |
+
This function recursively traverses data structures and converts
|
| 14 |
+
all Decimal instances to float while preserving the structure.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
obj: Any object that may contain Decimal instances
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
Object with all Decimal instances converted to float
|
| 21 |
+
"""
|
| 22 |
+
if isinstance(obj, Decimal):
|
| 23 |
+
return float(obj)
|
| 24 |
+
elif isinstance(obj, dict):
|
| 25 |
+
return {key: convert_decimals_to_float(value) for key, value in obj.items()}
|
| 26 |
+
elif isinstance(obj, list):
|
| 27 |
+
return [convert_decimals_to_float(item) for item in obj]
|
| 28 |
+
elif isinstance(obj, tuple):
|
| 29 |
+
return tuple(convert_decimals_to_float(item) for item in obj)
|
| 30 |
+
else:
|
| 31 |
+
return obj
|