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

Test alternative workstation #80

Merged
merged 7 commits into from
May 29, 2024
Merged
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
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ignore =
W391,
W503,
W504,
W604,
E711,
E129,
F841,
Expand Down
2 changes: 1 addition & 1 deletion inventory_tools/inventory_tools/custom/operation.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"name": "Operation-alternative_workstations",
"no_copy": 0,
"non_negative": 0,
"options": "Alternative Workstations",
"options": "Alternative Workstation",
"owner": "Administrator",
"permlevel": 0,
"precision": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"modified": "2024-02-01 07:04:30.059469",
"modified_by": "Administrator",
"module": "Inventory Tools",
"name": "Alternative Workstations",
"name": "Alternative Workstation",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) 2024, AgriTheory and contributors
# For license information, please see license.txt

# import frappe
import frappe
from frappe.model.document import Document


class AlternativeWorkstations(Document):
class AlternativeWorkstation(Document):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"overproduction_percentage_for_work_order",
"section_break_0",
"update_warehouse_path",
"section_break_gzcbr",
"column_break_ddssn",
"allow_alternative_workstations",
"uoms_section",
"enforce_uoms"
],
Expand Down Expand Up @@ -79,19 +80,15 @@
},
{
"fieldname": "section_break_0",
"fieldtype": "Section Break"
"fieldtype": "Section Break",
"label": "Warehouses and Workstations"
},
{
"default": "0",
"fieldname": "update_warehouse_path",
"fieldtype": "Check",
"label": "Update Warehouse Path"
},
{
"fieldname": "section_break_gzcbr",
"fieldtype": "Section Break",
"label": "Warehouses"
},
{
"fieldname": "uoms_section",
"fieldtype": "Section Break",
Expand Down Expand Up @@ -146,11 +143,21 @@
"fieldtype": "Link",
"label": "Aggregated Sales Warehouse",
"options": "Warehouse"
},
{
"fieldname": "column_break_ddssn",
"fieldtype": "Column Break"
},
{
"default": "0",
"fieldname": "allow_alternative_workstations",
"fieldtype": "Check",
"label": "Allow Alternative Workstations"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-04-12 10:02:32.688310",
"modified": "2024-05-13 13:19:00.665445",
"modified_by": "Administrator",
"module": "Inventory Tools",
"name": "Inventory Tools Settings",
Expand Down
4 changes: 2 additions & 2 deletions inventory_tools/inventory_tools/overrides/operation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import frappe


def validate_alternative_workstation(self, method):
def validate_alternative_workstation(self, method=None):
if self.workstation:
for row in self.alternative_workstations:
if row.workstation == self.workstation:
frappe.throw("Default Workstation should not be selected as alternative workstation")
frappe.throw(frappe._("Default Workstation should not be selected as alternative workstation"))
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class InventoryToolsPurchaseInvoice(PurchaseInvoice):
def validate_with_previous_doc(self):
"""
HASH: 82d206b709ada758c277bc5a266dbc6ec10d00c8
HASH: 014486de39dd7da6fe79bf803adcf1b66d890876
REPO: https://github.com/frappe/erpnext/
PATH: erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
METHOD: validate_with_previous_doc
Expand Down
1 change: 1 addition & 0 deletions inventory_tools/inventory_tools/overrides/stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def get_pending_raw_materials(self, backflush_based_on=None):


@frappe.whitelist()
@frappe.read_only()
def get_production_item_if_work_orders_for_required_item_exists(stock_entry_name: str) -> str:
stock_entry = frappe.get_doc("Stock Entry", stock_entry_name)

Expand Down
68 changes: 42 additions & 26 deletions inventory_tools/inventory_tools/overrides/workstation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

import frappe
from frappe.desk.reportview import execute
from frappe.desk.search import search_link

"""
This function fetch workstation of the document operation.
Expand All @@ -25,38 +25,54 @@


@frappe.whitelist()
@frappe.read_only()
@frappe.validate_and_sanitize_search_inputs
def get_alternative_workstations(doctype, txt, searchfield, start, page_len, filters):
company = filters.get("company") or frappe.defaults.get_defaults().get("company")
if not frappe.get_cached_value(
"Inventory Tools Settings", company, "allow_alternative_workstations"
):
filters.pop("operation") if "operation" in filters else True
filters.pop("company") if "company" in filters else True
return execute(
"Workstation",
HKuz marked this conversation as resolved.
Show resolved Hide resolved
filters=filters,
fields=[searchfield],
limit_start=start,
limit_page_length=page_len,
as_list=True,
)

operation = filters.get("operation")
if not operation:
frappe.throw("Please select a Operation first.")

if txt:
searchfields = frappe.get_meta(doctype).get_search_fields()
searchfields = " or ".join(["ws." + field + f" LIKE '%{txt}%'" for field in searchfields])

conditions = ""
if txt and searchfields:
conditions = f"and ({searchfields})"
searchfields = list(reversed(frappe.get_meta(doctype).get_search_fields()))
select = ",\n".join([f"`tabWorkstation`.{field}" for field in searchfields])
search_text = "AND `tabAlternative Workstation`.workstation LIKE %(txt)s" if txt else ""

workstation = frappe.db.sql(
"""
Select aw.workstation, ws.workstation_type, ws.description
From `tabOperation` as op
Left Join `tabAlternative Workstations` as aw ON aw.parent = op.name
Left Join `tabWorkstation` as ws ON ws.name = aw.workstation
Where op.name = '{operation}' {conditions}
""".format(
conditions=conditions, operation=operation
)
f"""
SELECT DISTINCT {select}
FROM `tabOperation`, `tabWorkstation`, `tabAlternative Workstation`
WHERE `tabWorkstation`.name = `tabAlternative Workstation`.workstation
AND `tabAlternative Workstation`.parent = %(operation)s
{search_text}
""",
{"operation": operation, "txt": f"%{txt}%"},
as_list=True,
)

default_workstation = frappe.db.get_value("Operation", operation, "workstation")
flag = True
for row in workstation:
if row[0] == None:
workstation = ((default_workstation,),)
flag = False
if flag:
workstation += ((default_workstation,),)
default_workstation_name = frappe.db.get_value("Operation", operation, "workstation")
default_workstation_fields = frappe.db.get_values(
"Workstation", default_workstation_name, searchfields, as_dict=True
)
if default_workstation_name not in [row[0] for row in workstation]:
_default = tuple(
[
default_workstation_fields[0].name,
f"{frappe.bold('Default')} - {','.join([v for k, v in default_workstation_fields[0].items() if k != 'name'])}",
]
)
workstation.insert(0, _default)
return workstation
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def create(company, filters, rows):
rows = [frappe._dict(r) for r in json.loads(rows)] if isinstance(rows, str) else rows
if not rows:
return
print(rows)
counter = 0
settings = frappe.get_doc("Inventory Tools Settings", company)
requesting_companies = list({row.company for row in rows})
Expand Down
1 change: 1 addition & 0 deletions inventory_tools/patches.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inventory_tools.patches.rename_alternative_workstation # Tyler Matteson 5/13/24
11 changes: 11 additions & 0 deletions inventory_tools/patches/rename_alternative_workstation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import frappe
from frappe.model.rename_doc import rename_doc


def execute():
if frappe.db.exists("DocType", "Alternative Workstations"):
rename_doc(
"DocType", "Alternative Workstations", "Alternative Workstation", ignore_if_exists=True
)

frappe.reload_doc("inventory_tools", "doctype", "alternative_workstation", force=True)
3 changes: 2 additions & 1 deletion inventory_tools/public/js/job_card_custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function set_workstation_query(frm) {
return {
query: 'inventory_tools.inventory_tools.overrides.workstation.get_alternative_workstations',
filters: {
operation: doc.operation,
operation: frm.doc.operation,
company: frm.doc.company,
},
}
})
Expand Down
1 change: 1 addition & 0 deletions inventory_tools/public/js/work_order_custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function get_workstations(frm) {
query: 'inventory_tools.inventory_tools.overrides.workstation.get_alternative_workstations',
filters: {
operation: d.operation,
company: frm.doc.company,
},
}
})
Expand Down
1 change: 1 addition & 0 deletions inventory_tools/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
"item_price": 0.02,
"default_warehouse": "Storeroom - APC",
"supplier": ["Freedom Provisions", "Unity Bakery Supply"],
"uom_conversion_detail": {"Box": 100},
},
{
"item_code": "Salt",
Expand Down
2 changes: 1 addition & 1 deletion inventory_tools/tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def create_production_plan(settings, prod_plan_from_doc):
job_card = frappe.get_doc("Job Card", job_card)
job_card.time_logs[0].completed_qty = wo.qty
job_card.save()
job_card.submit()
job_card.submit() # don't submit to test alternative workstations


def create_fruit_material_request(settings):
Expand Down
37 changes: 37 additions & 0 deletions inventory_tools/tests/test_alternative_workstation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import frappe
import pytest


@pytest.mark.order(45)
def test_alternative_workstation_query():
# test default settings
frappe.call(
"frappe.desk.search.search_link",
**{
"doctype": "Workstation",
"txt": "",
"reference_doctype": "Job Card",
},
)
assert len(frappe.response.results) == 16 # all workstations

# test with inventory tools settings
inventory_tools_settings = frappe.get_doc(
"Inventory Tools Settings", frappe.defaults.get_defaults().get("company")
)
inventory_tools_settings.allow_alternative_workstations = True
inventory_tools_settings.save()
frappe.call(
"frappe.desk.search.search_link",
**{
"doctype": "Workstation",
"txt": "",
"query": "inventory_tools.inventory_tools.overrides.workstation.get_alternative_workstations",
"filters": {"operation": "Gather Pie Filling Ingredients"},
"reference_doctype": "Job Card",
},
)
assert len(frappe.response.results) == 2
assert frappe.response.results[0].get("value") == "Food Prep Table 1" # default returns first
assert "Default" in frappe.response.results[0].get("description")
assert frappe.response.results[1].get("value") == "Food Prep Table 2"
24 changes: 24 additions & 0 deletions inventory_tools/tests/test_uom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ def test_uom_enforcement_validation():
so.save()

assert "Invalid UOM" in exc_info.value.args[0]


@pytest.mark.order(41)
def test_uom_enforcement_query():
inventory_tools_settings = frappe.get_doc(
"Inventory Tools Settings", frappe.defaults.get_defaults().get("company")
)
inventory_tools_settings.enforce_uoms = True
inventory_tools_settings.save()
frappe.call(
"frappe.desk.search.search_link",
**{
"doctype": "UOM",
"txt": "",
"query": "inventory_tools.inventory_tools.overrides.uom.uom_restricted_query",
"filters": {"parent": "Parchment Paper"},
"reference_doctype": "Purchase Order Item",
},
)
assert len(frappe.response.results) == 2
assert frappe.response.results[0].get("value") == "Nos"
assert frappe.response.results[0].get("description") == "1.0"
assert frappe.response.results[1].get("value") == "Box"
assert frappe.response.results[1].get("description") == "100.0"
Loading