diff --git a/smart_service/transactions/doctype/publish/publish.py b/smart_service/transactions/doctype/publish/publish.py index e931577..5458efc 100644 --- a/smart_service/transactions/doctype/publish/publish.py +++ b/smart_service/transactions/doctype/publish/publish.py @@ -1,270 +1,313 @@ # Copyright (c) 2021, Hard n Soft Technologies Pvt Ltd and contributors # For license information, please see license.txt +import time +import sys +from html import unescape import frappe from frappe.model.document import Document -import json +import json current_db_name = frappe.conf.get("db_name") -from html import unescape -import sys -sys.tracebacklimit=0 +sys.tracebacklimit = 0 + class Publish(Document): - def validate(self): - ###Selected variant and kilometer mapping details list - variant = [] - for vm in self.get('publish_documents'): - if vm.variant_mapping in variant: - pass - else: - variant.append(vm.variant_mapping) - - self.variant_mapping_details = '' - self.kilometer_mapping_details = '' - for v in variant: - self.variant_mapping_details = self.variant_mapping_details + str(v) + '\n' - km_mapping_record = frappe.db.sql("""select name from {0}.`tabKilometer Mapping` where variant_mapping = '{1}' and language = '{2}'""".format(current_db_name,str(v),self.language),as_dict=True) - for km_mapping in km_mapping_record: - self.kilometer_mapping_details = self.kilometer_mapping_details + str(km_mapping.name) + '\n' - - def onload(self): - current_db_name = frappe.conf.get("db_name") - ###To validate updated procedure status - procedure_status = frappe.db.sql("""select p.name,p.update_procedure_status,pro.name,p.variant_mapping,p.system,p.sub_system,p.asset_category,p.item_category,p.variant,p.procedure_link,p.procedure_status,pro.name,pro.workflow_state + def validate(self): + # Selected variant and kilometer mapping details list + variant = [] + for vm in self.get('publish_documents'): + if vm.variant_mapping in variant: + pass + else: + variant.append(vm.variant_mapping) + + self.variant_mapping_details = '' + self.kilometer_mapping_details = '' + for v in variant: + self.variant_mapping_details = self.variant_mapping_details + \ + str(v) + '\n' + km_mapping_record = frappe.db.sql("""select name from {0}.`tabKilometer Mapping` where variant_mapping = '{1}' and language = '{2}'""".format( + current_db_name, str(v), self.language), as_dict=True) + for km_mapping in km_mapping_record: + self.kilometer_mapping_details = self.kilometer_mapping_details + \ + str(km_mapping.name) + '\n' + + def onload(self): + current_db_name = frappe.conf.get("db_name") + # To validate updated procedure status + procedure_status = frappe.db.sql("""select p.name,p.update_procedure_status,pro.name,p.variant_mapping,p.system,p.sub_system,p.asset_category,p.item_category,p.variant,p.procedure_link,p.procedure_status,pro.name,pro.workflow_state from {0}.`tabPublish_Docs` as p,{0}.`tabProcedure` as pro - where p.parent = '{1}' and p.procedure_link = pro.name and p.procedure_link is not null""".format(current_db_name,self.name),as_dict=True) - l = len(procedure_status) - if procedure_status and self.publish_status == "Published": - for x in procedure_status: - for y in self.get("publish_documents"): - if y.procedure_link: - if x.procedure_link == y.procedure_link and y.update_procedure_status != y.procedure_status: - y.update_procedure_status = x.workflow_state - frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where procedure_link = %s""",(x.workflow_state,x.procedure_link)) - frappe.db.commit() - if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status: - y.update_procedure_status = x.workflow_state - frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where procedure_link = %s""",(x.workflow_state,x.procedure_link)) - frappe.db.commit() - if y.procedure_status == "Publish Ready": - if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and not y.excluded_global: - y.excluded_global = "1" - if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and x.workflow_state == "Draft" and y.procedure_status != "Publish Ready": - y.excluded_internal = "1" - y.save() - frappe.db.commit() - - ###Update system and variant mapping ststus - if self.publish_status == "Published" and self.publish_type == "Global": - global_publish = frappe.db.sql("""select name from {0}.`tabPublish` where global_publish = '{1}'""".format(current_db_name,self.name),as_dict=True) - for global_name in global_publish: - update_mapping(self.variant_mapping,global_name.name) - - u_id = frappe.get_all("User",filters={"name":frappe.session.user},fields=["username"]) - if u_id: - for i in u_id: - name = i.username - if not self.published_by and self.publish_status == "Published": - self.published_by = str(name) - frappe.db.set_value("Publish",self.name,"published_by",str(name)) - frappe.db.commit() - - def on_cancel(self): - ###Published document should not allow to cancel - if self.publish_status == "Published": - frappe.throw('You can not cancel a Published Document!') - -def update_procedure(vehicle,lang,publish_type,doc): - ###To validate updated procedure status - doc = frappe.get_doc("Publish",doc) - current_db_name = frappe.conf.get("db_name") - procedure_status = frappe.db.sql("""select p.name,p.update_procedure_status,pro.name,p.variant_mapping,p.system,p.sub_system,p.asset_category,p.item_category,p.variant,p.procedure_link,p.procedure_status,pro.name,pro.workflow_state + where p.parent = '{1}' and p.procedure_link = pro.name and p.procedure_link is not null""".format(current_db_name, self.name), as_dict=True) + l = len(procedure_status) + if procedure_status and self.publish_status == "Published": + for x in procedure_status: + for y in self.get("publish_documents"): + if y.procedure_link: + if x.procedure_link == y.procedure_link and y.update_procedure_status != y.procedure_status: + y.update_procedure_status = x.workflow_state + frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where parent = %s and procedure_link = %s ; """, ( + x.workflow_state, self.name, x.procedure_link)) + frappe.db.commit() + if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status: + y.update_procedure_status = x.workflow_state + frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where parent = %s and procedure_link = %s""", ( + x.workflow_state, self.name, x.procedure_link)) + frappe.db.commit() + if y.procedure_status == "Publish Ready": + if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and not y.excluded_global: + y.excluded_global = "1" + if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and x.workflow_state == "Draft" and y.procedure_status != "Publish Ready": + y.excluded_internal = "1" + y.save() + frappe.db.commit() + + # Update system and variant mapping ststus + if self.publish_status == "Published" and self.publish_type == "Global": + global_publish = frappe.db.sql("""select name from {0}.`tabPublish` where global_publish = '{1}'""".format( + current_db_name, self.name), as_dict=True) + for global_name in global_publish: + update_mapping(self.variant_mapping, global_name.name) + + u_id = frappe.get_all( + "User", filters={"name": frappe.session.user}, fields=["username"]) + if u_id: + for i in u_id: + name = i.username + if not self.published_by and self.publish_status == "Published": + self.published_by = str(name) + frappe.db.set_value( + "Publish", self.name, "published_by", str(name)) + frappe.db.commit() + + def on_cancel(self): + # Published document should not allow to cancel + if self.publish_status == "Published": + frappe.throw('You can not cancel a Published Document!') + + +def update_procedure(vehicle, lang, publish_type, doc): + # To validate updated procedure status + doc = frappe.get_doc("Publish", doc) + current_db_name = frappe.conf.get("db_name") + procedure_status = frappe.db.sql("""select p.name,p.update_procedure_status,pro.name,p.variant_mapping,p.system,p.sub_system,p.asset_category,p.item_category,p.variant,p.procedure_link,p.procedure_status,pro.name,pro.workflow_state from {0}.`tabPublish_Docs` as p,{0}.`tabProcedure` as pro - where p.parent = '{1}' and p.procedure_link = pro.name and pro.workflow_state != p.update_procedure_status and p.procedure_link is not null""".format(current_db_name,doc.name),as_dict=True) - l = len(procedure_status) - if procedure_status: - for x in procedure_status: - for y in doc.get("publish_documents"): - if y.procedure_link: - if x.procedure_link == y.procedure_link and x.workflow_state != y.update_procedure_status: - y.update_procedure_status = x.workflow_state - frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where procedure_link = %s""",(x.workflow_state,x.procedure_link)) - frappe.db.commit() - if y.procedure_status == "Publish Ready": - if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and not y.excluded_global: - y.excluded_global = "1" - if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and x.workflow_state == "Draft" and y.procedure_status != "Publish Ready": - y.excluded_internal = "1" - - y.save() - frappe.db.commit() - if l > 0: - frappe.msgprint(str(l) + ' Procedure Status to be updated, Please check the Publish Documents table.') - draft_procedure_status = frappe.db.sql("""select name from {0}.`tabPublish_Docs` where parent = '{1}' and update_procedure_status = 'Draft' and procedure_link is not null""".format(current_db_name,doc.name),as_dict=True) - publish_table = doc.publish_documents - table_len = len(publish_table) - l1 = len(draft_procedure_status) - if str(l1) == str(table_len): - frappe.throw("There is no item for internal publish") - - if doc.publish_type == "Global": - publish_table = doc.publish_documents - table_len = len(publish_table) - publish_record = frappe.db.sql("""select name from {0}.`tabPublish_Docs` where update_procedure_status != procedure_status and update_procedure_status != 'Publish Ready' and parent = '{1}'""".format(current_db_name,doc.name),as_dict=True) - if publish_record: - len_publish = len(publish_record) - if str(table_len) == str(len_publish): - frappe.throw("There is no item for global publish") - -###Generate Publish versions + where p.parent = '{1}' and p.procedure_link = pro.name and pro.workflow_state != p.update_procedure_status and p.procedure_link is not null""".format(current_db_name, doc.name), as_dict=True) + l = len(procedure_status) + if procedure_status: + for x in procedure_status: + for y in doc.get("publish_documents"): + if y.procedure_link: + if x.procedure_link == y.procedure_link and x.workflow_state != y.update_procedure_status: + y.update_procedure_status = x.workflow_state + frappe.db.sql("""update `tabPublish_Docs` set update_procedure_status = %s where procedure_link = %s""", ( + x.workflow_state, x.procedure_link)) + frappe.db.commit() + if y.procedure_status == "Publish Ready": + if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and not y.excluded_global: + y.excluded_global = "1" + if x.procedure_link == y.procedure_link and x.workflow_state != y.procedure_status and x.workflow_state == "Draft" and y.procedure_status != "Publish Ready": + y.excluded_internal = "1" + + y.save() + frappe.db.commit() + if l > 0: + frappe.msgprint(str( + l) + ' Procedure Status to be updated, Please check the Publish Documents table.') + draft_procedure_status = frappe.db.sql( + """select name from {0}.`tabPublish_Docs` where parent = '{1}' and update_procedure_status = 'Draft' and procedure_link is not null""".format(current_db_name, doc.name), as_dict=True) + publish_table = doc.publish_documents + table_len = len(publish_table) + l1 = len(draft_procedure_status) + if str(l1) == str(table_len): + frappe.throw("There is no item for internal publish") + + if doc.publish_type == "Global": + publish_table = doc.publish_documents + table_len = len(publish_table) + publish_record = frappe.db.sql("""select name from {0}.`tabPublish_Docs` where update_procedure_status != procedure_status and update_procedure_status != 'Publish Ready' and parent = '{1}'""".format( + current_db_name, doc.name), as_dict=True) + if publish_record: + len_publish = len(publish_record) + if str(table_len) == str(len_publish): + frappe.throw("There is no item for global publish") + +# Generate Publish versions + + @frappe.whitelist() -def cal_ver(vehicle,lang,publish_type,doc): - update_procedure(vehicle,lang,publish_type,doc) - current_db_name = frappe.conf.get("db_name") - doc = frappe.get_doc("Publish",doc) - ver = frappe.db.sql("""select max(version),publish_type from {current_db_name}.tabPublish where vehicle = '{vehi}' and language = '{lang}';""" - .format(current_db_name=current_db_name,vehi=vehicle,lang=lang)) - v = 0.0 - if publish_type == 'Global': - if ver[0][0] == None: - v = 1.0 - else: - v= int(float(ver[0][0])) + 1.0 - - elif publish_type == 'Internal': - if ver[0][0] == None: - v = 0.01 - else: - v= float(ver[0][0]) + 0.01 - v = "{:.2f}".format(v) - doc.version = v - frappe.db.sql("""update {0}.`tabPublish` set version = '{1}' where name = '{2}'""".format(current_db_name,v,doc.name)) - frappe.db.commit() - return v - -###Create global publish document for publish ready procedures +def cal_ver(vehicle, lang, publish_type, doc): + update_procedure(vehicle, lang, publish_type, doc) + current_db_name = frappe.conf.get("db_name") + doc = frappe.get_doc("Publish", doc) + ver = frappe.db.sql("""select max(version),publish_type from {current_db_name}.tabPublish where vehicle = '{vehi}' and language = '{lang}';""" + .format(current_db_name=current_db_name, vehi=vehicle, lang=lang)) + v = 0.0 + if publish_type == 'Global': + if ver[0][0] == None: + v = 1.0 + else: + v = int(float(ver[0][0])) + 1.0 + + elif publish_type == 'Internal': + if ver[0][0] == None: + v = 0.01 + else: + v = float(ver[0][0]) + 0.01 + v = "{:.2f}".format(v) + doc.version = v + frappe.db.sql("""update {0}.`tabPublish` set version = '{1}' where name = '{2}'""".format( + current_db_name, v, doc.name)) + frappe.db.commit() + return v + +# Create global publish document for publish ready procedures + + @frappe.whitelist() def global_publish(doc): - doc = frappe.get_doc("Publish",doc) - current_db_name = frappe.conf.get("db_name") - procedure = frappe.db.sql("""select parent,name from {0}.`tabPublish_Docs` where parent = '{1}' and update_procedure_status='Publish Ready' and procedure_link is not null""".format(current_db_name,doc.name)) - asset = frappe.db.sql("""select parent,name from {0}.`tabPublish_Docs` where parent = '{1}' and variant_mapping is not null and procedure_link is null""".format(current_db_name,doc.name)) - if not procedure and not asset: - frappe.throw("There is no item for global publish") - - publish_type = "Global" - publish_record = frappe.new_doc("Publish") - publish_record.language = doc.language - publish_record.publish_type = "Global" - publish_record.release_description = doc.release_description - publish_record.vehicle = doc.vehicle - publish_record.vehicle_id = doc.vehicle_id - publish_record.variant_mapping = doc.variant_mapping - publish_record.variant_mapping_status = doc.variant_mapping_status - publish_record.kilometer_mapping = doc.kilometer_mapping - publish_record.km_active_status = doc.km_active_status - publish_record.variant = doc.variant - for table in doc.get('publish_documents'): - if table.update_procedure_status == "Publish Ready": - publish_table = publish_record.append('publish_documents',{}) - publish_table.variant_mapping = table.variant_mapping - publish_table.system = table.system - publish_table.sub_system = table.sub_system - publish_table.procedure_link = table.procedure_link - publish_table.procedure_status = table.procedure_status - publish_table.update_procedure_status = table.procedure_status - publish_table.asset_category = table.asset_category - publish_table.item_category = table.item_category - publish_table.variant = table.variant - if table.variant_mapping and not table.procedure_link: - publish_table = publish_record.append('publish_documents',{}) - publish_table.variant_mapping = table.variant_mapping - publish_table.asset_category = table.asset_category - publish_table.item_category = table.item_category - publish_table.variant = table.variant - - publish_record.save() - doc.global_publish = publish_record.name - doc.save() - frappe.msgprint('Global Publish Created for ' + publish_record.name) + doc = frappe.get_doc("Publish", doc) + current_db_name = frappe.conf.get("db_name") + procedure = frappe.db.sql( + """select parent,name from {0}.`tabPublish_Docs` where parent = '{1}' and update_procedure_status='Publish Ready' and procedure_link is not null""".format(current_db_name, doc.name)) + asset = frappe.db.sql("""select parent,name from {0}.`tabPublish_Docs` where parent = '{1}' and variant_mapping is not null and procedure_link is null""".format( + current_db_name, doc.name)) + if not procedure and not asset: + frappe.throw("There is no item for global publish") + + publish_type = "Global" + publish_record = frappe.new_doc("Publish") + publish_record.language = doc.language + publish_record.publish_type = "Global" + publish_record.release_description = doc.release_description + publish_record.vehicle = doc.vehicle + publish_record.vehicle_id = doc.vehicle_id + publish_record.variant_mapping = doc.variant_mapping + publish_record.variant_mapping_status = doc.variant_mapping_status + publish_record.kilometer_mapping = doc.kilometer_mapping + publish_record.km_active_status = doc.km_active_status + publish_record.variant = doc.variant + for table in doc.get('publish_documents'): + if table.update_procedure_status == "Publish Ready": + publish_table = publish_record.append('publish_documents', {}) + publish_table.variant_mapping = table.variant_mapping + publish_table.system = table.system + publish_table.sub_system = table.sub_system + publish_table.procedure_link = table.procedure_link + publish_table.procedure_status = table.procedure_status + publish_table.update_procedure_status = table.procedure_status + publish_table.asset_category = table.asset_category + publish_table.item_category = table.item_category + publish_table.variant = table.variant + if table.variant_mapping and not table.procedure_link: + publish_table = publish_record.append('publish_documents', {}) + publish_table.variant_mapping = table.variant_mapping + publish_table.asset_category = table.asset_category + publish_table.item_category = table.item_category + publish_table.variant = table.variant + + publish_record.save() + doc.global_publish = publish_record.name + doc.save() + frappe.msgprint('Global Publish Created for ' + publish_record.name) + @frappe.whitelist() -def old_publish(vehicle,language): - current_db_name = frappe.conf.get("db_name") - publish_record = frappe.db.sql("""select name,publish_type from {0}.`tabPublish` where vehicle = '{1}' and language = '{2}' and publish_status='To Publish' and publish_type = 'Internal' and docstatus != '2'""".format(current_db_name,vehicle,language),as_dict=True) - if publish_record: - return publish_record - global_publish_record = frappe.db.sql("""select name,publish_type from {0}.`tabPublish` where vehicle = '{1}' and language = '{2}' and publish_status='To Publish' and publish_type = 'Global' and docstatus != '2'""".format(current_db_name,vehicle,language),as_dict=True) - if global_publish_record: - return global_publish_record - -###Update System and Variant Mapping and its called from publish client script +def old_publish(vehicle, language): + current_db_name = frappe.conf.get("db_name") + publish_record = frappe.db.sql("""select name,publish_type from {0}.`tabPublish` where vehicle = '{1}' and language = '{2}' and publish_status='To Publish' and publish_type = 'Internal' and docstatus != '2'""".format( + current_db_name, vehicle, language), as_dict=True) + if publish_record: + return publish_record + global_publish_record = frappe.db.sql("""select name,publish_type from {0}.`tabPublish` where vehicle = '{1}' and language = '{2}' and publish_status='To Publish' and publish_type = 'Global' and docstatus != '2'""".format( + current_db_name, vehicle, language), as_dict=True) + if global_publish_record: + return global_publish_record + +# Update System and Variant Mapping and its called from publish client script + + @frappe.whitelist() -def update_mapping(variant_mapping,doc): - doc = frappe.get_doc("Publish",doc) - current_db_name = frappe.conf.get("db_name") - if doc.publish_documents: - for variant in doc.get('publish_documents'): - if variant.item_category == "Variant Mapping Assets": - vm = frappe.get_doc("Variant Mapping",variant.variant_mapping) - for v in vm.get('assets'): - if v.category == variant.asset_category: - v.published = '1' - frappe.db.sql("""update {0}.`tabVariant Mapping_Assets` set published = '1' where parent = '{1}' and category = '{2}'""".format(current_db_name,variant.variant_mapping,variant.asset_category)) - frappe.db.commit() - - if variant.item_category == "System Mapping": - system_mapping = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping = '{1}' and language = '{2}'""".format(current_db_name,variant.variant_mapping,doc.language),as_dict=True) - if system_mapping: - for system_map in system_mapping: - sm = frappe.get_doc("System Mapping",system_map.name) - for sys_asset in sm.get('systems_assets'): - if sys_asset.systems == variant.system: - sys_asset.published = '1' - frappe.db.sql("""update {0}.`tabSystem Mapping_System Assets` set published = '1' where parent = '{1}' and systems = '{2}'""".format(current_db_name,sm.name,variant.system)) - frappe.db.commit() - - for sm_item in sm.get("system_sub_systems"): - if sm_item.systems == variant.system and sm_item.sub_systems == variant.sub_system and sm_item.procedure == variant.procedure_link: - sm_item.procedure_is_published = '1' - frappe.db.sql("""update {0}.`tabSystem Mapping_Sub System` set procedure_is_published = '1' where parent = '{1}' and systems = '{2}' and sub_systems = '{3}'""".format(current_db_name,sm.name,variant.system,variant.sub_system)) - frappe.db.commit() - -###Set filter for latest modified document +def update_mapping(variant_mapping, doc): + doc = frappe.get_doc("Publish", doc) + current_db_name = frappe.conf.get("db_name") + if doc.publish_documents: + for variant in doc.get('publish_documents'): + if variant.item_category == "Variant Mapping Assets": + vm = frappe.get_doc("Variant Mapping", variant.variant_mapping) + for v in vm.get('assets'): + if v.category == variant.asset_category: + v.published = '1' + frappe.db.sql("""update {0}.`tabVariant Mapping_Assets` set published = '1' where parent = '{1}' and category = '{2}'""".format( + current_db_name, variant.variant_mapping, variant.asset_category)) + frappe.db.commit() + + if variant.item_category == "System Mapping": + system_mapping = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping = '{1}' and language = '{2}'""".format( + current_db_name, variant.variant_mapping, doc.language), as_dict=True) + if system_mapping: + for system_map in system_mapping: + sm = frappe.get_doc("System Mapping", system_map.name) + for sys_asset in sm.get('systems_assets'): + if sys_asset.systems == variant.system: + sys_asset.published = '1' + frappe.db.sql("""update {0}.`tabSystem Mapping_System Assets` set published = '1' where parent = '{1}' and systems = '{2}'""".format( + current_db_name, sm.name, variant.system)) + frappe.db.commit() + + for sm_item in sm.get("system_sub_systems"): + if sm_item.systems == variant.system and sm_item.sub_systems == variant.sub_system and sm_item.procedure == variant.procedure_link: + sm_item.procedure_is_published = '1' + frappe.db.sql("""update {0}.`tabSystem Mapping_Sub System` set procedure_is_published = '1' where parent = '{1}' and systems = '{2}' and sub_systems = '{3}'""".format( + current_db_name, sm.name, variant.system, variant.sub_system)) + frappe.db.commit() + +# Set filter for latest modified document + + @frappe.whitelist() def get_vm_asset(doc): - doc = frappe.get_doc("Variant Mapping",doc) - vm_assets = frappe.db.sql("""select category,language,attach_file,active_status from {0}.`tabVariant Mapping_Assets` where parent = '{1}' and published = '0'""".format(current_db_name,doc.name),as_dict=True) - return vm_assets + doc = frappe.get_doc("Variant Mapping", doc) + vm_assets = frappe.db.sql("""select category,language,attach_file,active_status from {0}.`tabVariant Mapping_Assets` where parent = '{1}' and published = '0'""".format( + current_db_name, doc.name), as_dict=True) + return vm_assets + @frappe.whitelist() -def get_sm_asset(doc,name): - system_map = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping='{1}' and name = '{2}'""".format(current_db_name,doc,name),as_dict=True) - if system_map: - for s in system_map: - doc = frappe.get_doc("System Mapping",s.name) - system_asset = frappe.db.sql("""select systems,language,active_status,system_asset from {0}.`tabSystem Mapping_System Assets` where parent = '{1}' and published = '0'""".format(current_db_name,s.name),as_dict=True) - return system_asset - +def get_sm_asset(doc, name): + system_map = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping='{1}' and name = '{2}'""".format( + current_db_name, doc, name), as_dict=True) + if system_map: + for s in system_map: + doc = frappe.get_doc("System Mapping", s.name) + system_asset = frappe.db.sql("""select systems,language,active_status,system_asset from {0}.`tabSystem Mapping_System Assets` where parent = '{1}' and published = '0'""".format( + current_db_name, s.name), as_dict=True) + return system_asset + + @frappe.whitelist() -def get_system(doc,name): - system_map = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping='{1}' and name = '{2}'""".format(current_db_name,doc,name),as_dict=True) - if system_map: - for s in system_map: - doc = frappe.get_doc("System Mapping",s.name) - system = frappe.db.sql("""select systems,sub_systems,active_status,symptom,component,estimate_time,rts,component,mat,procedure_status,`procedure` from {0}.`tabSystem Mapping_Sub System` where parent = '{1}' and procedure_is_published = '0'""".format(current_db_name,s.name),as_dict=True) - return system +def get_system(doc, name): + system_map = frappe.db.sql("""select name from {0}.`tabSystem Mapping` where variant_mapping='{1}' and name = '{2}'""".format( + current_db_name, doc, name), as_dict=True) + if system_map: + for s in system_map: + doc = frappe.get_doc("System Mapping", s.name) + system = frappe.db.sql("""select systems,sub_systems,active_status,symptom,component,estimate_time,rts,component,mat,procedure_status,`procedure` from {0}.`tabSystem Mapping_Sub System` where parent = '{1}' and procedure_is_published = '0'""".format( + current_db_name, s.name), as_dict=True) + return system + @frappe.whitelist() def max_publish(doc): - doc = frappe.get_doc("Publish",doc) - ver = frappe.db.sql("""select max(version) from {0}.tabPublish where vehicle = '{1}' and language = '{2}';""".format(current_db_name,doc.vehicle,doc.language)) - if ver: - return ver + doc = frappe.get_doc("Publish", doc) + ver = frappe.db.sql("""select max(version) from {0}.tabPublish where vehicle = '{1}' and language = '{2}';""".format( + current_db_name, doc.vehicle, doc.language)) + if ver: + return ver + @frappe.whitelist() def variant(doctype, txt, searchfield, start, page_len, filters): - vehicle = filters.get('vehicle') - return frappe.db.sql(""" select name,family_code,fuel,transmission,vehicle_segment,active_status + vehicle = filters.get('vehicle') + return frappe.db.sql(""" select name,family_code,fuel,transmission,vehicle_segment,active_status FROM {0}.`tabVariant Mapping` where vehicle = '{1}' and - (active_status like "%%%(txt)s%%" or vehicle_segment like "%%%(txt)s%%" or name like "%%%(txt)s%%" or variant like "%%%(txt)s%%" or family_code like "%%%(txt)s%%" or fuel like "%%%(txt)s%%" or transmission like "%%%(txt)s%%") order by modified desc limit %(start)s, %(page_len)s """.format(current_db_name,vehicle)%{'txt': txt, 'start': start, 'page_len': page_len}) + (active_status like "%%%(txt)s%%" or vehicle_segment like "%%%(txt)s%%" or name like "%%%(txt)s%%" or variant like "%%%(txt)s%%" or family_code like "%%%(txt)s%%" or fuel like "%%%(txt)s%%" or transmission like "%%%(txt)s%%") order by modified desc limit %(start)s, %(page_len)s """.format(current_db_name, vehicle) % {'txt': txt, 'start': start, 'page_len': page_len})