File size: 1,568 Bytes
f39814a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
from typing import Any, Union

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def clean_value(value: Union[str, float, int, None]) -> float:
	"""
	Clean and convert a value to float.
	Removes commas from strings and strips whitespace.
	Returns 0.0 if conversion fails.
	"""
	try:
		if isinstance(value, str):
			value = value.replace(',', '').strip()
		return float(value) if value else 0.0
	except (ValueError, TypeError):
		logger.debug(f"Could not clean value: {value}")
		return 0.0

def to_lakhs(value: Union[float, int, str]) -> float:
	"""
	Convert a numeric value to lakhs (divide by 100,000 and round to 2 decimals).
	Accepts int, float, or numeric string.
	"""
	try:
		if isinstance(value, str):
			value = float(value.replace(',', '').strip())
		return round(float(value) / 100000, 2)
	except (ValueError, TypeError):
		logger.debug(f"Could not convert to lakhs: {value}")
		return 0.0

def convert_note_json_to_lakhs(note_json: Any) -> Any:
	"""
	Recursively convert all numeric values in a note JSON to lakhs.
	Returns the converted object.
	"""
	def convert(obj: Any) -> Any:
		if isinstance(obj, dict):
			for k, v in obj.items():
				if isinstance(v, (int, float)):
					obj[k] = to_lakhs(v)
				elif isinstance(v, str):
					try:
						obj[k] = to_lakhs(float(v.replace(',', '')))
					except Exception:
						obj[k] = v
				else:
					obj[k] = convert(v)
		elif isinstance(obj, list):
			for i in range(len(obj)):
				obj[i] = convert(obj[i])
		return obj

	return convert(note_json)