Skip to content

Commit

Permalink
/
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu committed Jan 17, 2025
1 parent 9fabfb2 commit 42d2f5e
Show file tree
Hide file tree
Showing 22 changed files with 1,465 additions and 13 deletions.
84 changes: 72 additions & 12 deletions lpp_co/custom/quality_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from frappe import _
from frappe.utils import get_link_to_form

QC_TEMPLATE_FIELDS = [
"custom_visual_inspection",
"custom_functional_testing",
"custom_specification_inspection"
]

class QualityInspectionLPP(QualityInspection):
def get_formula_evaluation_data(self, reading):
Expand All @@ -25,15 +30,70 @@ def get_formula_evaluation_data(self, reading):
return data

def validate_inspection_required(self):
doc = frappe.get_doc(self.reference_type, self.reference_name)
if self.reference_type == "Purchase Receipt" and doc.get("docstatus", 0) == 1:
if not frappe.get_cached_value(
"Item", self.item_code, "custom_inspection_required_after_purchase_receipt"
):
frappe.throw(
_(
"'Inspection Required after Purchase Receipt' has disabled for the item {0}, no need to create the QI"
).format(get_link_to_form("Item", self.item_code))
)
else:
super().validate_inspection_required()
# Still not sure if we need to check?
# ------------------------------------
# doc = frappe.get_doc(self.reference_type, self.reference_name)
# if self.reference_type == "Purchase Receipt" and doc.get("docstatus", 0) == 1:
# if not frappe.get_cached_value(
# "Item", self.item_code, "custom_inspection_required_after_purchase_receipt"
# ):
# frappe.throw(
# _(
# "'Inspection Required after Purchase Receipt' has disabled for the item {0}, no need to create the QI"
# ).format(get_link_to_form("Item", self.item_code))
# )
# else:
# super().validate_inspection_required()
return

def on_update(self):
self.reset_quality_inspection_results()

def reset_quality_inspection_results(self):
# Check if reset is necessary
prev_doc = self.get_doc_before_save()
if not self._should_reset_quality_inspection_results(prev_doc):
return

# Delete existing Quality Inspection Results
frappe.db.delete("Quality Inspection Result", {"quality_inspection": self.name})

# Recreate Quality Inspection Results
self._create_quality_inspection_results()

def _should_reset_quality_inspection_results(self, prev_doc):
"""Check if Quality Inspection Results need to be reset."""
if not prev_doc:
return True # Reset if there is no previous document

# Compare template fields in the current and previous document
for tmpl_field in QC_TEMPLATE_FIELDS:
if prev_doc.get(tmpl_field) != self.get(tmpl_field):
return True

# Compare sample quantity
if prev_doc.sample_size != self.sample_size:
return True

return False

def _create_quality_inspection_results(self):
"""Create Quality Inspection Results based on templates."""
for tmpl_field in QC_TEMPLATE_FIELDS:
template_name = self.get(tmpl_field)
if not template_name:
continue

# Fetch the template and create results for each parameter
doc_template = frappe.get_doc("Quality Inspection Template", template_name)
for row in doc_template.item_quality_inspection_parameter:
result = frappe.get_doc({
"doctype": "Quality Inspection Result",
"quality_inspection": self.name,
"quality_inspection_template": doc_template.name,
"parameter": row.specification
})
for i in range(self.sample_size):
result.append("readings", {}) # Add bland row
result.insert(ignore_permissions=True)

Loading

0 comments on commit 42d2f5e

Please sign in to comment.