def load_from_db(self, dt=None, dn=None): """ Load doclist from dt """ from frappe.model.doc import getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn) # get all children types tablefields = frappe.model.meta.get_table_fields(dt) # load chilren doclist = frappe.doclist([ doc, ]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt) self.set_doclist(doclist) if dt == dn: self.convert_type(self.doc)
def post(self): """ Save diff between Customize Form Bean and DocType Bean as property setter entries """ if self.doc.doc_type: from frappe.model import doc from frappe.core.doctype.doctype.doctype import validate_fields_for_doctype this_doclist = frappe.doclist([self.doc] + self.doclist) ref_doclist = self.get_ref_doclist() dt_doclist = doc.get('DocType', self.doc.doc_type) # get a list of property setter docs self.idx_dirty = False diff_list = self.diff(this_doclist, ref_doclist, dt_doclist) if self.idx_dirty: self.make_idx_property_setter(this_doclist, diff_list) self.set_properties(diff_list) validate_fields_for_doctype(self.doc.doc_type) frappe.clear_cache(doctype=self.doc.doc_type) frappe.msgprint("Updated")
def set_doclist(self, doclist): for i, d in enumerate(doclist): if isinstance(d, dict): doclist[i] = Document(fielddata=d) self.doclist = frappe.doclist(doclist) self.doc = self.doclist[0] if self.obj: self.obj.doclist = self.doclist self.obj.doc = self.doc
def get_ref_doclist(self): """ * Gets doclist of type self.doc.doc_type * Applies property setter properties on the doclist * returns the modified doclist """ from frappe.model.doctype import get ref_doclist = get(self.doc.doc_type) ref_doclist = frappe.doclist([ref_doclist[0]] + ref_doclist.get({"parent": self.doc.doc_type})) return ref_doclist
def cleanup_packing_list(obj, parent_items): """Remove all those child items which are no longer present in main item table""" delete_list = [] for d in obj.doclist.get({"parentfield": "packing_details"}): if [d.parent_item, d.parent_detail_docname] not in parent_items: # mark for deletion from doclist delete_list.append([d.parent_item, d.parent_detail_docname]) if not delete_list: return obj.doclist # delete from doclist obj.doclist = frappe.doclist(filter(lambda d: [d.parent_item, d.parent_detail_docname] not in delete_list, obj.doclist)) return obj.doclist
def make_purchase_order_based_on_supplier(source_name, target_doclist=None): from frappe.model.mapper import get_mapped_doclist if target_doclist: if isinstance(target_doclist, basestring): import json target_doclist = frappe.doclist(json.loads(target_doclist)) target_doclist = target_doclist.get( {"parentfield": ["!=", "po_details"]}) material_requests, supplier_items = get_material_requests_based_on_supplier( source_name) def postprocess(source, target_doclist): target_doclist[0].supplier = source_name set_missing_values(source, target_doclist) po_items = target_doclist.get({"parentfield": "po_details"}) target_doclist = target_doclist.get({"parentfield": ["!=", "po_details"]}) + \ [d for d in po_items if d.fields.get("item_code") in supplier_items and d.fields.get("qty") > 0] return target_doclist for mr in material_requests: target_doclist = get_mapped_doclist( "Material Request", mr, { "Material Request": { "doctype": "Purchase Order", }, "Material Request Item": { "doctype": "Purchase Order Item", "field_map": [["name", "prevdoc_detail_docname"], ["parent", "prevdoc_docname"], ["parenttype", "prevdoc_doctype"], ["uom", "stock_uom"], ["uom", "uom"]], "postprocess": update_item } }, target_doclist, postprocess) return [d.fields for d in target_doclist]
def clear_table(self, doclist, tablefield, save=0): """ Clears the child records from the given `doclist` for a particular `tablefield` """ from frappe.model.utils import getlist table_list = getlist(doclist, tablefield) delete_list = [d.name for d in table_list] if delete_list: #filter doclist doclist = filter(lambda d: d.name not in delete_list, doclist) # delete from db frappe.db.sql( """\ delete from `tab%s` where parent=%s and parenttype=%s""" % (table_list[0].doctype, '%s', '%s'), (self.name, self.doctype)) self.fields['__unsaved'] = 1 return frappe.doclist(doclist)
def get_parent_doclist(self): return frappe.doclist([self[0]] + self.get({"parent": self[0].name}))
def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=None, postprocess=None, ignore_permissions=False): if target_doclist is None: target_doclist = [] if isinstance(target_doclist, basestring): target_doclist = json.loads(target_doclist) source = frappe.bean(from_doctype, from_docname) if not ignore_permissions and not frappe.has_permission(from_doctype, "read", source.doc): frappe.msgprint("No Permission", raise_exception=frappe.PermissionError) source_meta = frappe.get_doctype(from_doctype) target_meta = frappe.get_doctype(table_maps[from_doctype]["doctype"]) # main if target_doclist: if isinstance(target_doclist[0], dict): target_doc = frappe.doc(fielddata=target_doclist[0]) else: target_doc = target_doclist[0] else: target_doc = frappe.new_doc(table_maps[from_doctype]["doctype"]) map_doc(source.doc, target_doc, table_maps[source.doc.doctype], source_meta, target_meta) if target_doclist: target_doclist[0] = target_doc else: target_doclist = [target_doc] target_doclist = frappe.doclist(target_doclist) row_exists_for_parentfield = {} # children for source_d in source.doclist[1:]: table_map = table_maps.get(source_d.doctype) if table_map: if "condition" in table_map: if not table_map["condition"](source_d): continue target_doctype = table_map["doctype"] parentfield = target_meta.get({ "parent": target_doc.doctype, "doctype": "DocField", "fieldtype": "Table", "options": target_doctype })[0].fieldname # does row exist for a parentfield? if parentfield not in row_exists_for_parentfield: row_exists_for_parentfield[parentfield] = True if \ frappe.doclist(target_doclist).get({"parentfield": parentfield}) else False if table_map.get("add_if_empty") and row_exists_for_parentfield.get(parentfield): continue target_d = frappe.new_doc(target_doctype, target_doc, parentfield) map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0]) target_d.idx = None target_doclist.append(target_d) target_doclist = frappe.doclist(target_doclist) if postprocess: new_target_doclist = postprocess(source, target_doclist) if new_target_doclist: target_doclist = new_target_doclist return target_doclist