Skip to content

Commit

Permalink
Merge pull request #629 from frappe/version-14-hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchamahabal committed Jun 26, 2023
2 parents c00adaa + ae0a54f commit 1da7ad3
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 48 deletions.
5 changes: 4 additions & 1 deletion hrms/hr/doctype/expense_claim/expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def get_gl_entries(self):
"against_voucher_type": self.doctype,
"against_voucher": self.name,
"cost_center": self.cost_center,
"project": self.project,
},
item=self,
)
Expand All @@ -175,6 +176,7 @@ def get_gl_entries(self):
"debit_in_account_currency": data.sanctioned_amount,
"against": self.employee,
"cost_center": data.cost_center or self.cost_center,
"project": data.project or self.project,
},
item=data,
)
Expand Down Expand Up @@ -241,7 +243,8 @@ def add_tax_gl_entries(self, gl_entries):
"debit": tax.tax_amount,
"debit_in_account_currency": tax.tax_amount,
"against": self.employee,
"cost_center": self.cost_center,
"cost_center": tax.cost_center or self.cost_center,
"project": tax.project or self.project,
"against_voucher_type": self.doctype,
"against_voucher": self.name,
},
Expand Down
88 changes: 71 additions & 17 deletions hrms/hr/doctype/expense_claim/test_expense_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
class TestExpenseClaim(FrappeTestCase):
def setUp(self):
if not frappe.db.get_value("Cost Center", {"company": company_name}):
frappe.get_doc(
cost_center = frappe.new_doc("Cost Center")
cost_center.update(
{
"doctype": "Cost Center",
"cost_center_name": "_Test Cost Center 3",
Expand All @@ -33,39 +34,41 @@ def setUp(self):
}
).insert()

frappe.db.set_value("Company", company_name, "default_cost_center", cost_center)

def test_total_expense_claim_for_project(self):
frappe.db.sql("""delete from `tabTask`""")
frappe.db.sql("""delete from `tabProject`""")
frappe.db.delete("Task")
frappe.db.delete("Project")
frappe.db.sql("update `tabExpense Claim` set project = '', task = ''")

project = frappe.get_doc({"project_name": "_Test Project 1", "doctype": "Project"})
project.save()
project = create_project("_Test Project 1")

task = frappe.get_doc(
dict(doctype="Task", subject="_Test Project Task 1", status="Open", project=project.name)
task = frappe.new_doc("Task")
task.update(
dict(doctype="Task", subject="_Test Project Task 1", status="Open", project=project)
).insert()
task = task.name

task_name = task.name
payable_account = get_payable_account(company_name)

make_expense_claim(
payable_account, 300, 200, company_name, "Travel Expenses - _TC3", project.name, task_name
payable_account, 300, 200, company_name, "Travel Expenses - _TC3", project, task
)

self.assertEqual(frappe.db.get_value("Task", task_name, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Project", project.name, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Task", task, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Project", project, "total_expense_claim"), 200)

expense_claim2 = make_expense_claim(
payable_account, 600, 500, company_name, "Travel Expenses - _TC3", project.name, task_name
payable_account, 600, 500, company_name, "Travel Expenses - _TC3", project, task
)

self.assertEqual(frappe.db.get_value("Task", task_name, "total_expense_claim"), 700)
self.assertEqual(frappe.db.get_value("Project", project.name, "total_expense_claim"), 700)
self.assertEqual(frappe.db.get_value("Task", task, "total_expense_claim"), 700)
self.assertEqual(frappe.db.get_value("Project", project, "total_expense_claim"), 700)

expense_claim2.cancel()

self.assertEqual(frappe.db.get_value("Task", task_name, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Project", project.name, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Task", task, "total_expense_claim"), 200)
self.assertEqual(frappe.db.get_value("Project", project, "total_expense_claim"), 200)

def test_expense_claim_status_as_payment_from_journal_entry(self):
# Via Journal Entry
Expand Down Expand Up @@ -427,6 +430,36 @@ def test_journal_entry_against_expense_claim(self):

self.assertEqual(je.accounts[0].debit_in_account_currency, expense_claim.grand_total)

def test_accounting_dimension_mapping(self):
project = create_project("_Test Expense Project")
payable_account = get_payable_account(company_name)

expense_claim = make_expense_claim(
payable_account,
300,
200,
company_name,
"Travel Expenses - _TC3",
do_not_submit=True,
)

expense_claim.expenses[0].project = project
expense_claim.submit()

dimensions = frappe.db.get_value(
"GL Entry",
{
"voucher_type": "Expense Claim",
"voucher_no": expense_claim.name,
"account": "Travel Expenses - _TC3",
},
["cost_center", "project"],
as_dict=1,
)

self.assertEqual(dimensions.project, project)
self.assertEqual(dimensions.cost_center, expense_claim.cost_center)


def get_payable_account(company):
return frappe.get_cached_value("Company", company, "default_payable_account")
Expand All @@ -443,9 +476,19 @@ def generate_taxes(company=None):
account_type="Tax",
parent_account=parent_account,
)

cost_center = frappe.db.get_value("Company", company, "cost_center")

return {
"taxes": [
{"account_head": account, "rate": 9, "description": "CGST", "tax_amount": 10, "total": 210}
{
"account_head": account,
"cost_center": cost_center,
"rate": 9,
"description": "CGST",
"tax_amount": 10,
"total": 210,
}
]
}

Expand Down Expand Up @@ -579,3 +622,14 @@ def allocate_using_payment_reconciliation(expense_claim, employee, journal_entry

pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments}))
pr.reconcile()


def create_project(project_name):
project = frappe.db.exists("Project", {"project_name": project_name})
if project:
return project

doc = frappe.new_doc("Project")
doc.project_name = project_name
doc.insert()
return doc.name
14 changes: 11 additions & 3 deletions hrms/hr/doctype/expense_claim_detail/expense_claim_detail.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"sanctioned_amount",
"accounting_dimensions_section",
"cost_center",
"dimension_col_break"
"dimension_col_break",
"project"
],
"fields": [
{
Expand Down Expand Up @@ -114,17 +115,24 @@
{
"fieldname": "dimension_col_break",
"fieldtype": "Column Break"
},
{
"fieldname": "project",
"fieldtype": "Link",
"label": "Project",
"options": "Project"
}
],
"idx": 1,
"istable": 1,
"links": [],
"modified": "2021-11-26 14:23:45.539922",
"modified": "2023-06-24 00:56:08.900677",
"modified_by": "Administrator",
"module": "HR",
"name": "Expense Claim Detail",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC"
"sort_order": "DESC",
"states": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"total",
"accounting_dimensions_section",
"cost_center",
"dimension_col_break"
"dimension_col_break",
"project"
],
"fields": [
{
Expand Down Expand Up @@ -96,17 +97,25 @@
{
"fieldname": "dimension_col_break",
"fieldtype": "Column Break"
},
{
"fieldname": "project",
"fieldtype": "Link",
"label": "Project",
"options": "Project"
}
],
"istable": 1,
"links": [],
"modified": "2021-10-26 20:27:36.027728",
"modified": "2023-06-26 13:06:28.555076",
"modified_by": "Administrator",
"module": "HR",
"name": "Expense Taxes and Charges",
"naming_rule": "Random",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "ASC",
"states": [],
"track_changes": 1
}
}
18 changes: 9 additions & 9 deletions hrms/hr/doctype/shift_assignment/shift_assignment.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"fieldtype": "Link",
"label": "Employee",
"options": "Employee",
"reqd": 1
"reqd": 1,
"search_index": 1
},
{
"fetch_from": "employee.employee_name",
Expand All @@ -48,7 +49,8 @@
"in_list_view": 1,
"label": "Shift Type",
"options": "Shift Type",
"reqd": 1
"reqd": 1,
"search_index": 1
},
{
"fieldname": "column_break_3",
Expand Down Expand Up @@ -88,27 +90,24 @@
"allow_on_submit": 1,
"fieldname": "end_date",
"fieldtype": "Date",
"label": "End Date",
"show_days": 1,
"show_seconds": 1
"label": "End Date"
},
{
"allow_on_submit": 1,
"default": "Active",
"fieldname": "status",
"fieldtype": "Select",
"label": "Status",
"options": "Active\nInactive",
"show_days": 1,
"show_seconds": 1
"options": "Active\nInactive"
}
],
"is_submittable": 1,
"links": [],
"modified": "2020-06-15 14:27:54.310773",
"modified": "2023-06-23 12:08:23.247882",
"modified_by": "Administrator",
"module": "HR",
"name": "Shift Assignment",
"naming_rule": "Expression (old style)",
"owner": "Administrator",
"permissions": [
{
Expand Down Expand Up @@ -150,6 +149,7 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "employee_name",
"track_changes": 1
}
19 changes: 10 additions & 9 deletions hrms/hr/doctype/shift_assignment/shift_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.query_builder import Criterion, Interval
from frappe.query_builder.functions import Date
from frappe.utils import cstr, get_link_to_form, get_time, getdate, now_datetime
from frappe.query_builder import Criterion
from frappe.utils import add_days, cstr, get_link_to_form, get_time, getdate, now_datetime

from hrms.hr.utils import validate_active_employee

Expand Down Expand Up @@ -258,24 +257,26 @@ def _adjust_overlapping_shifts(shifts: dict):

def get_shifts_for_date(employee: str, for_timestamp: datetime) -> List[Dict[str, str]]:
"""Returns list of shifts with details for given date"""
assignment = frappe.qb.DocType("Shift Assignment")
for_date = for_timestamp.date()
prev_day = add_days(for_date, -1)

assignment = frappe.qb.DocType("Shift Assignment")
return (
frappe.qb.from_(assignment)
.select(assignment.name, assignment.shift_type, assignment.start_date, assignment.end_date)
.where(
(assignment.employee == employee)
& (assignment.docstatus == 1)
& (assignment.status == "Active")
& (assignment.start_date <= getdate(for_timestamp.date()))
& (assignment.start_date <= for_date)
& (
Criterion.any(
[
assignment.end_date.isnull(),
(
assignment.end_date.isnotnull()
# for midnight shifts, valid assignments are upto 1 day prior
& (Date(for_timestamp) - Interval(days=1) <= assignment.end_date)
& (prev_day <= assignment.end_date)
),
]
)
Expand Down Expand Up @@ -463,7 +464,7 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D
:param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime
"""
if not shift_type_name:
return {}
return frappe._dict()

if for_timestamp is None:
for_timestamp = now_datetime()
Expand All @@ -489,13 +490,13 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D
if get_time(for_timestamp.time()) >= get_time(shift_actual_start):
# if for_timestamp is greater than start time, it's within the first day
start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time
for_timestamp = for_timestamp + timedelta(days=1)
for_timestamp += timedelta(days=1)
end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time

elif get_time(for_timestamp.time()) < get_time(shift_actual_start):
# if for_timestamp is less than start time, it's within the second day
end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time
for_timestamp = for_timestamp + timedelta(days=-1)
for_timestamp += timedelta(days=-1)
start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time
else:
# start and end timings fall on the same day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ def get_columns(filters):
columns = [
{
"label": _("Employee"),
"options": "Employee",
"fieldname": "employee",
"fieldtype": "Link",
"options": "Employee",
"width": 200,
},
{
"label": _("Employee Name"),
"options": "Employee",
"fieldname": "employee_name",
"fieldtype": "Link",
"width": 160,
},
{"label": _("Amount"), "fieldname": "amount", "fieldtype": "Currency", "width": 140},
Expand Down
Loading

0 comments on commit 1da7ad3

Please sign in to comment.