Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added percentage-based expense split and validation #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions batwara/batwara/doctype/expense/expense.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"fieldname": "split_method",
"fieldtype": "Select",
"label": "Split Method",
"options": "Equally\nManually",
"options": "Equally\nManually\nPercentage",
"reqd": 1
},
{
Expand All @@ -118,7 +118,7 @@
"link_fieldname": "expense"
}
],
"modified": "2024-07-28 16:20:39.494671",
"modified": "2024-09-23 15:52:18.839671",
"modified_by": "Administrator",
"module": "Batwara",
"name": "Expense",
Expand Down
17 changes: 14 additions & 3 deletions batwara/batwara/doctype/expense/expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class Expense(Document):
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from frappe.types import DF

from batwara.batwara.doctype.expense_split.expense_split import ExpenseSplit
from frappe.types import DF

amended_from: DF.Link | None
amount: DF.Currency
Expand All @@ -23,7 +22,7 @@ class Expense(Document):
description: DF.Data
notes: DF.SmallText | None
paid_by: DF.Link
split_method: DF.Literal["Equally", "Manually"]
split_method: DF.Literal["Equally", "Manually", "Percentage"]
splits: DF.Table[ExpenseSplit]
# end: auto-generated types

Expand All @@ -33,6 +32,9 @@ def before_save(self):
def apply_split(self):
if self.split_method == "Equally":
self.calculate_equal_splits()
elif self.split_method == "Percentage":
self.validate_percentage_splits()
self.calculate_percentage_splits()
else:
# manually
self.validate_manual_splits()
Expand All @@ -49,6 +51,15 @@ def calculate_equal_splits(self):

for s in self.splits:
s.amount = split_amount

def validate_percentage_splits(self):
sum_of_percentages = sum(x.percentage for x in self.splits)
if sum_of_percentages != 100:
frappe.throw("Sum of percentages must equal 100%!")

def calculate_percentage_splits(self):
for s in self.splits:
s.amount = (s.percentage / 100) * self.amount

def before_submit(self):
self.create_ledger_entries()
Expand Down
8 changes: 7 additions & 1 deletion batwara/batwara/doctype/expense_split/expense_split.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"engine": "InnoDB",
"field_order": [
"user",
"percentage",
"column_break_lnxs",
"amount",
"currency"
Expand Down Expand Up @@ -40,12 +41,17 @@
"label": "Currency",
"options": "Currency",
"reqd": 1
},
{
"fieldname": "percentage",
"fieldtype": "Float",
"label": "Percentage"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-07-27 08:43:35.299074",
"modified": "2024-09-23 15:52:55.986929",
"modified_by": "Administrator",
"module": "Batwara",
"name": "Expense Split",
Expand Down
16 changes: 16 additions & 0 deletions batwara/batwara/doctype/expense_split/expense_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@


class ExpenseSplit(Document):
# begin: auto-generated types
# This code is auto-generated. Do not modify anything in this block.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from frappe.types import DF

amount: DF.Currency
currency: DF.Link
parent: DF.Data
parentfield: DF.Data
parenttype: DF.Data
percentage: DF.Float
user: DF.Link
# end: auto-generated types
pass
21 changes: 21 additions & 0 deletions frontend/src/pages/CreateNewExpense.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
expenseDetails.split_method = 'Manually'
},
},
{
label: 'Percentage',
onClick: () => {
expenseDetails.split_method = 'Percentage'
},
},
]"
:button="{
label: expenseDetails.split_method,
Expand Down Expand Up @@ -75,6 +81,20 @@
</li>
</ol>
</div>

<div v-if="expenseDetails.split_method === 'Percentage'">
<ol class="space-y-3">
<li v-for="friend in expenseDetails.selected_friends">
<FormControl
v-model.number="friend.percentage"
type="number"
:label="friend.label"
placeholder="0%"
/>
</li>
</ol>
</div>

</div>

<div>
Expand Down Expand Up @@ -186,6 +206,7 @@ function saveExpense() {
splits: expenseDetails.selected_friends.map((f) => ({
user: f.value,
amount: f.amount || 0.0,
percentage: f.percentage || 0.0
})),
},
{
Expand Down