# -*- coding: utf-8 -*-
# Copyright (c) 2019, <NAME> and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class CustomerRequirement(Document):
	def validate(self):
		# Set product shortcode
		plpf = frappe.get_doc("Product Line Process Flow", self.product)
		self.product_short = plpf.description_short

		# Set characteristic type
		self.type = self.type_long[0:2]

		# Set missing fields and warn if duplicate
		found = []
		for control in self.controls:
			control_detail = get_control_details(control.control)
			if not control.step:
				control.step = control_detail["step"]

			if not control.step_description:
				control.step_description = control_detail["step_description"]

			# if not control.workstation:
			# 	control.workstation = control_detail["workstation"]

			if control.control in found:
				frappe.throw(_("Control {0} entered twice").format(control.control))

			found.append(control.control)

		# Set title on save
		self.title = self.product_short + " - " + self.code + " (" + self.description + ")"

@frappe.whitelist()
def get_control_details(control):

	control_doc = frappe.get_doc("Step Characteristic", control)
	step = frappe.get_doc("Step", control_doc.parent)
	return {
		"control": control_doc.name,
		"step": control_doc.parent,
		"step_description": step.description,
		"workstation": ""
	}

# -*- coding: utf-8 -*-
# Copyright (c) 2019, <NAME> and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class CustomerRequirement(Document):
	def validate(self):
		# Set product shortcode
		plpf = frappe.get_doc("Product Line Process Flow", self.product)
		self.product_short = plpf.description_short

		# Set characteristic type
		self.type = self.type_long[0:2]

		# Set missing fields and warn if duplicate
		found = []
		for control in self.controls:
			control_detail = get_control_details(control.control)
			if not control.step:
				control.step = control_detail["step"]

			if not control.step_description:
				control.step_description = control_detail["step_description"]

			# if not control.workstation:
			# 	control.workstation = control_detail["workstation"]

			if control.control in found:
				frappe.throw(_("Control {0} entered twice").format(control.control))

			found.append(control.control)

		# Set title on save
		self.title = self.product_short + " - " + self.code + " (" + self.description + ")"

@frappe.whitelist()
def get_control_details(control):

	control_doc = frappe.get_doc("Step Characteristic", control)
	step = frappe.get_doc("Step", control_doc.parent)
	return {
		"control": control_doc.name,
		"step": control_doc.parent,
		"step_description": step.description,
		"workstation": ""
	}

# -*- coding: utf-8 -*- # Copyright (c) 2019, <NAME> and contributors # For license information, please see license.txt # Set product shortcode # Set characteristic type # Set missing fields and warn if duplicate # if not control.workstation: # 	control.workstation = control_detail["workstation"] # Set title on save