mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-24 07:29:22 +00:00
fix: better manual budget distribution on update
(cherry picked from commit 1c82f42fa8)
This commit is contained in:
@@ -60,8 +60,6 @@ frappe.ui.form.on("Budget", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
distribute_equally: function (frm) {
|
distribute_equally: function (frm) {
|
||||||
console.log("here");
|
|
||||||
|
|
||||||
toggle_distribution_fields(frm);
|
toggle_distribution_fields(frm);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -235,23 +235,43 @@ class Budget(Document):
|
|||||||
self.validate_distribution_totals()
|
self.validate_distribution_totals()
|
||||||
|
|
||||||
def allocate_budget(self):
|
def allocate_budget(self):
|
||||||
if self.revision_of:
|
if self._should_skip_allocation():
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._should_recalculate_manual_distribution():
|
||||||
|
self._recalculate_manual_distribution()
|
||||||
return
|
return
|
||||||
|
|
||||||
if not self.should_regenerate_budget_distribution():
|
if not self.should_regenerate_budget_distribution():
|
||||||
return
|
return
|
||||||
|
|
||||||
self.set("budget_distribution", [])
|
self._regenerate_distribution()
|
||||||
|
|
||||||
periods = self.get_budget_periods()
|
def _should_skip_allocation(self):
|
||||||
total_periods = len(periods)
|
return self.revision_of and not self.distribute_equally
|
||||||
row_percent = 100 / total_periods if total_periods else 0
|
|
||||||
|
|
||||||
for start_date, end_date in periods:
|
def _should_recalculate_manual_distribution(self):
|
||||||
row = self.append("budget_distribution", {})
|
return (
|
||||||
row.start_date = start_date
|
not self.distribute_equally
|
||||||
row.end_date = end_date
|
and bool(self.budget_distribution)
|
||||||
self.add_allocated_amount(row, row_percent)
|
and self._is_only_budget_amount_changed()
|
||||||
|
)
|
||||||
|
|
||||||
|
def _is_only_budget_amount_changed(self):
|
||||||
|
old = self.get_doc_before_save()
|
||||||
|
if not old:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return (
|
||||||
|
old.budget_amount != self.budget_amount
|
||||||
|
and old.distribution_frequency == self.distribution_frequency
|
||||||
|
and old.budget_start_date == self.budget_start_date
|
||||||
|
and old.budget_end_date == self.budget_end_date
|
||||||
|
)
|
||||||
|
|
||||||
|
def _recalculate_manual_distribution(self):
|
||||||
|
for row in self.budget_distribution:
|
||||||
|
row.amount = flt((row.percent / 100) * self.budget_amount, 3)
|
||||||
|
|
||||||
def should_regenerate_budget_distribution(self):
|
def should_regenerate_budget_distribution(self):
|
||||||
"""Check whether budget distribution should be recalculated."""
|
"""Check whether budget distribution should be recalculated."""
|
||||||
@@ -265,7 +285,6 @@ class Budget(Document):
|
|||||||
"to_fiscal_year",
|
"to_fiscal_year",
|
||||||
"budget_amount",
|
"budget_amount",
|
||||||
"distribution_frequency",
|
"distribution_frequency",
|
||||||
"distribute_equally",
|
|
||||||
]
|
]
|
||||||
for field in changed_fields:
|
for field in changed_fields:
|
||||||
if old_doc.get(field) != self.get(field):
|
if old_doc.get(field) != self.get(field):
|
||||||
@@ -273,6 +292,19 @@ class Budget(Document):
|
|||||||
|
|
||||||
return bool(self.distribute_equally)
|
return bool(self.distribute_equally)
|
||||||
|
|
||||||
|
def _regenerate_distribution(self):
|
||||||
|
self.set("budget_distribution", [])
|
||||||
|
|
||||||
|
periods = self.get_budget_periods()
|
||||||
|
total_periods = len(periods)
|
||||||
|
row_percent = 100 / total_periods if total_periods else 0
|
||||||
|
|
||||||
|
for start_date, end_date in periods:
|
||||||
|
row = self.append("budget_distribution", {})
|
||||||
|
row.start_date = start_date
|
||||||
|
row.end_date = end_date
|
||||||
|
self.add_allocated_amount(row, row_percent)
|
||||||
|
|
||||||
def get_budget_periods(self):
|
def get_budget_periods(self):
|
||||||
"""Return list of (start_date, end_date) tuples based on frequency."""
|
"""Return list of (start_date, end_date) tuples based on frequency."""
|
||||||
frequency = self.distribution_frequency
|
frequency = self.distribution_frequency
|
||||||
@@ -312,12 +344,8 @@ class Budget(Document):
|
|||||||
}.get(frequency, 1)
|
}.get(frequency, 1)
|
||||||
|
|
||||||
def add_allocated_amount(self, row, row_percent):
|
def add_allocated_amount(self, row, row_percent):
|
||||||
if not self.distribute_equally:
|
row.amount = flt(self.budget_amount * row_percent / 100, 3)
|
||||||
row.amount = 0
|
row.percent = flt(row_percent, 3)
|
||||||
row.percent = 0
|
|
||||||
else:
|
|
||||||
row.amount = flt(self.budget_amount * row_percent / 100, 3)
|
|
||||||
row.percent = flt(row_percent, 3)
|
|
||||||
|
|
||||||
def validate_distribution_totals(self):
|
def validate_distribution_totals(self):
|
||||||
if self.should_regenerate_budget_distribution():
|
if self.should_regenerate_budget_distribution():
|
||||||
|
|||||||
Reference in New Issue
Block a user