mirror of
https://github.com/frappe/erpnext.git
synced 2026-09-15 01:43:10 +00:00
refactor: simple utility for growth value computation for custom FS report
This commit is contained in:
committed by
Smit Vora
parent
c179460c98
commit
698876672d
@@ -1860,7 +1860,7 @@ class GrowthViewTransformer:
|
|||||||
self.formatted_rows = context.raw_data.get("formatted_data", [])
|
self.formatted_rows = context.raw_data.get("formatted_data", [])
|
||||||
self.period_list = context.period_list
|
self.period_list = context.period_list
|
||||||
|
|
||||||
def transform(self) -> None:
|
def transform(self):
|
||||||
for row_data in self.formatted_rows:
|
for row_data in self.formatted_rows:
|
||||||
if row_data.get("is_blank_line"):
|
if row_data.get("is_blank_line"):
|
||||||
continue
|
continue
|
||||||
@@ -1870,50 +1870,41 @@ class GrowthViewTransformer:
|
|||||||
else:
|
else:
|
||||||
self._transform_single_row(row_data)
|
self._transform_single_row(row_data)
|
||||||
|
|
||||||
def _transform_single_row(self, row_data: dict) -> None:
|
def _compute_growth_values(self, source: dict) -> dict:
|
||||||
transformed_values = {}
|
transformed = {}
|
||||||
|
|
||||||
for i, period in enumerate(self.period_list):
|
for i, period in enumerate(self.period_list):
|
||||||
current_period = period["key"]
|
current_period = period["key"]
|
||||||
current_value = row_data.get(current_period)
|
current_value = source.get(current_period)
|
||||||
|
|
||||||
if not isinstance(current_value, int | float):
|
if current_value in (None, ""):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if i == 0:
|
if i == 0:
|
||||||
transformed_values[current_period] = current_value
|
transformed[current_period] = current_value
|
||||||
else:
|
else:
|
||||||
previous_period = self.period_list[i - 1]["key"]
|
previous_period = self.period_list[i - 1]["key"]
|
||||||
previous_value = row_data.get(previous_period) or 0
|
previous_value = source.get(previous_period) or 0
|
||||||
transformed_values[current_period] = self._calculate_growth(previous_value, current_value)
|
transformed[current_period] = self._calculate_growth(previous_value, current_value)
|
||||||
|
|
||||||
row_data.update(transformed_values)
|
return transformed
|
||||||
|
|
||||||
def _transform_segmented_row(self, row_data: dict) -> None:
|
def _transform_single_row(self, row_data: dict):
|
||||||
|
row_data.update(self._compute_growth_values(row_data))
|
||||||
|
|
||||||
|
def _transform_segmented_row(self, row_data: dict):
|
||||||
for seg_id, seg_data in row_data.get("segment_values", {}).items():
|
for seg_id, seg_data in row_data.get("segment_values", {}).items():
|
||||||
if seg_data.get("is_blank_line"):
|
if seg_data.get("is_blank_line"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
transformed = {}
|
transformed = self._compute_growth_values(seg_data)
|
||||||
for i, period in enumerate(self.period_list):
|
|
||||||
current_period = period["key"]
|
|
||||||
current_value = seg_data.get(current_period)
|
|
||||||
|
|
||||||
if not isinstance(current_value, int | float):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if i == 0:
|
|
||||||
transformed[current_period] = current_value
|
|
||||||
else:
|
|
||||||
previous_period = self.period_list[i - 1]["key"]
|
|
||||||
previous_value = seg_data.get(previous_period) or 0
|
|
||||||
transformed[current_period] = self._calculate_growth(previous_value, current_value)
|
|
||||||
|
|
||||||
row_data[f"{seg_id}_{current_period}"] = transformed[current_period]
|
|
||||||
|
|
||||||
seg_data.update(transformed)
|
seg_data.update(transformed)
|
||||||
|
|
||||||
|
for period_key, value in transformed.items():
|
||||||
|
row_data[f"{seg_id}_{period_key}"] = value
|
||||||
|
|
||||||
def _calculate_growth(self, previous_value: float, current_value: float) -> float | None:
|
def _calculate_growth(self, previous_value: float, current_value: float) -> float | None:
|
||||||
if current_value is None or current_value == "":
|
if current_value in (None, ""):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if previous_value == 0 and current_value > 0:
|
if previous_value == 0 and current_value > 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user