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 (v15) #93

Merged
merged 2 commits into from
Jul 24, 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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ repos:
args: [--ignore-missing-imports]

- repo: https://github.com/agritheory/test_utils
rev: v0.13.1
rev: v0.14.0
hooks:
- id: update_pre_commit_config
- id: validate_copyright
Expand Down
4 changes: 2 additions & 2 deletions inventory_tools/inventory_tools/custom/operation.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
"insert_after": "workstation",
"is_system_generated": 0,
"is_virtual": 0,
"label": "Alternative Workstations",
"label": "Alternative Workstation",
"length": 0,
"modified": "2024-07-13 08:52:49.184527",
"modified_by": "Administrator",
"module": "Inventory Tools",
"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 @@ -21,7 +21,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
4 changes: 2 additions & 2 deletions inventory_tools/inventory_tools/overrides/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,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"))
7 changes: 4 additions & 3 deletions inventory_tools/inventory_tools/overrides/stock_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class InventoryToolsStockEntry(StockEntry):
def check_if_operations_completed(self):
"""
HASH: b5a2e5a375b39edf5f22a4d2276faeb3a4f85ced
HASH: 54791e938bd56eb81f7d8d923381a006998919fe
REPO: https://github.com/frappe/erpnext/
PATH: erpnext/stock/doctype/stock_entry/stock_entry.py
METHOD: check_if_operations_completed
Expand Down Expand Up @@ -54,7 +54,7 @@ def check_if_operations_completed(self):

def validate_finished_goods(self):
"""
HASH: b5a2e5a375b39edf5f22a4d2276faeb3a4f85ced
HASH: 54791e938bd56eb81f7d8d923381a006998919fe
REPO: https://github.com/frappe/erpnext/
PATH: erpnext/stock/doctype/stock_entry/stock_entry.py
METHOD: validate_finished_goods
Expand Down Expand Up @@ -120,7 +120,7 @@ def validate_finished_goods(self):

def get_pending_raw_materials(self, backflush_based_on=None):
"""
HASH: b5a2e5a375b39edf5f22a4d2276faeb3a4f85ced
HASH: 54791e938bd56eb81f7d8d923381a006998919fe
REPO: https://github.com/frappe/erpnext/
PATH: erpnext/stock/doctype/stock_entry/stock_entry.py
METHOD: get_pending_raw_materials
Expand Down Expand Up @@ -175,6 +175,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
66 changes: 42 additions & 24 deletions inventory_tools/inventory_tools/overrides/workstation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# For license information, please see license.txt

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 @@ -26,38 +28,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",
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 @@ -17,7 +17,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 @@ -27,6 +27,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 @@ -491,6 +491,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
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"
68 changes: 46 additions & 22 deletions inventory_tools/tests/test_uom.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
# Copyright (c) 2024, AgriTheory and contributors
# For license information, please see license.txt

import frappe
import pytest
from frappe.exceptions import ValidationError


@pytest.mark.order(40)
def test_uom_enforcement_validation():
_so = frappe.get_last_doc("Sales Order")
inventory_tools_settings = frappe.get_doc("Inventory Tools Settings", _so.company)
inventory_tools_settings.enforce_uoms = True
inventory_tools_settings.save()

so = frappe.copy_doc(_so)
assert so.items[0].uom == "Nos"
so.items[0].uom = "Box"
with pytest.raises(ValidationError) as exc_info:
so.save()

assert "Invalid UOM" in exc_info.value.args[0]
# Copyright (c) 2024, AgriTheory and contributors
# For license information, please see license.txt

import frappe
import pytest
from frappe.exceptions import ValidationError


@pytest.mark.order(40)
def test_uom_enforcement_validation():
_so = frappe.get_last_doc("Sales Order")
inventory_tools_settings = frappe.get_doc("Inventory Tools Settings", _so.company)
inventory_tools_settings.enforce_uoms = True
inventory_tools_settings.save()

so = frappe.copy_doc(_so)
assert so.items[0].uom == "Nos"
so.items[0].uom = "Box"
with pytest.raises(ValidationError) as exc_info:
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