def get_warehouse_account(warehouse, warehouse_account=None): account = warehouse.account if not account and warehouse.parent_warehouse: if warehouse_account: if warehouse_account.get(warehouse.parent_warehouse): account = warehouse_account.get( warehouse.parent_warehouse).account else: from dataent.utils.nestedset import rebuild_tree rebuild_tree("Warehouse", "parent_warehouse") else: account = dataent.db.sql( """ select account from `tabWarehouse` where lft <= %s and rgt >= %s and company = %s and account is not null and ifnull(account, '') !='' order by lft desc limit 1""", (warehouse.lft, warehouse.rgt, warehouse.company), as_list=1) account = account[0][0] if account else None if not account and warehouse.company: account = get_company_default_inventory_account(warehouse.company) if not account and warehouse.company: dataent.throw( _("Please set Account in Warehouse {0} or Default Inventory Account in Company {1}" ).format(warehouse.name, warehouse.company)) return account
def set_parent_to_warehouse(warehouse_group, company=None): dataent.db.sql( """ update tabWarehouse set parent_warehouse = %s, is_group = 0 where (is_group = 0 or is_group is null or is_group = '') and ifnull(company, '') = %s """, (warehouse_group, company.name if company else "")) rebuild_tree("Warehouse", "parent_warehouse")
def execute(): from dataent.core.doctype.file.file import make_home_folder if not dataent.db.exists("DocType", "File"): dataent.rename_doc("DocType", "File Data", "File") dataent.reload_doctype("File") if not dataent.db.exists("File", {"is_home_folder": 1}): make_home_folder() # make missing folders and set parent folder for file in dataent.get_all("File", filters={"is_folder": 0}): file = dataent.get_doc("File", file.name) file.flags.ignore_folder_validate = True file.flags.ignore_file_validate = True file.flags.ignore_duplicate_entry_error = True file.flags.ignore_links = True file.set_folder_name() try: file.save() except: print(dataent.get_traceback()) raise from dataent.utils.nestedset import rebuild_tree rebuild_tree("File", "folder") # reset file size for folder in dataent.db.sql("""select name from tabFile f1 where is_folder = 1 and (select count(*) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""): folder = dataent.get_doc("File", folder[0]) folder.save()
def set_parent_to_warehouse_account(company): dataent.db.sql( """ update tabAccount set parent_account = %s where is_group = 0 and account_type = "Warehouse" and (warehouse is not null or warehouse != '') and company = %s """, ("{0} - {1}".format(_("All Warehouses"), company.abbr), company.name)) rebuild_tree("Account", "parent_account")
def build_tree(): dataent.db.sql( """update `tabSupplier Group` set parent_supplier_group = '{0}' where is_group = 0""".format(_('All Supplier Groups'))) if not dataent.db.exists("Supplier Group", _('All Supplier Groups')): dataent.get_doc({ 'doctype': 'Supplier Group', 'supplier_group_name': _('All Supplier Groups'), 'is_group': 1, 'parent_supplier_group': '' }).insert(ignore_permissions=True) rebuild_tree("Supplier Group", "parent_supplier_group")
def execute(): """ assign lft and rgt appropriately """ dataent.reload_doc("hr", "doctype", "department") if not dataent.db.exists("Department", _('All Departments')): dataent.get_doc({ 'doctype': 'Department', 'department_name': _('All Departments'), 'is_group': 1 }).insert(ignore_permissions=True, ignore_mandatory=True) dataent.db.sql("""update `tabDepartment` set parent_department = '{0}' where is_group = 0""".format(_('All Departments'))) rebuild_tree("Department", "parent_department")
def execute(): dataent.local.lang = dataent.db.get_default("lang") or 'en' for doctype in [ 'department', 'leave_period', 'staffing_plan', 'job_opening', 'payroll_entry' ]: dataent.reload_doc("hr", "doctype", doctype) companies = dataent.db.get_all("Company", fields=["name", "abbr"]) departments = dataent.db.get_all("Department") comp_dict = {} # create a blank list for each company for company in companies: comp_dict[company.name] = {} for department in departments: # skip root node if _(department.name) == _("All Departments"): continue # for each company, create a copy of the doc department_doc = dataent.get_doc("Department", department) for company in companies: copy_doc = dataent.copy_doc(department_doc) copy_doc.update({"company": company.name}) try: copy_doc.insert() except dataent.DuplicateEntryError: pass # append list of new department for each company comp_dict[company.name][department.name] = copy_doc.name rebuild_tree('Department', 'parent_department') doctypes = [ "Asset", "Employee", "Payroll Entry", "Staffing Plan", "Job Opening" ] for d in doctypes: update_records(d, comp_dict) update_instructors(comp_dict) dataent.local.lang = 'en'
def execute(): if not dataent.db.get_value('Asset', {'docstatus': ('<', 2)}, 'name'): return dataent.reload_doc('assets', 'doctype', 'location') dataent.reload_doc('stock', 'doctype', 'warehouse') for d in dataent.get_all( 'Warehouse', fields=['warehouse_name', 'is_group', 'parent_warehouse'], order_by="lft asc"): try: loc = dataent.new_doc('Location') loc.location_name = d.warehouse_name loc.is_group = d.is_group loc.flags.ignore_mandatory = True if d.parent_warehouse: loc.parent_location = get_parent_warehouse_name( d.parent_warehouse) loc.save(ignore_permissions=True) except dataent.DuplicateEntryError: continue rebuild_tree("Location", "parent_location")
def create_charts(company, chart_template=None, existing_company=None): chart = get_chart(chart_template, existing_company) if chart: accounts = [] def _import_accounts(children, parent, root_type, root_account=False): for account_name, child in iteritems(children): if root_account: root_type = child.get("root_type") if account_name not in [ "account_number", "account_type", "root_type", "is_group", "tax_rate" ]: account_number = cstr(child.get("account_number")).strip() account_name, account_name_in_db = add_suffix_if_duplicate( account_name, account_number, accounts) is_group = identify_is_group(child) report_type = "Balance Sheet" if root_type in ["Asset", "Liability", "Equity"] \ else "Profit and Loss" account = dataent.get_doc({ "doctype": "Account", "account_name": account_name, "company": company, "parent_account": parent, "is_group": is_group, "root_type": root_type, "report_type": report_type, "account_number": account_number, "account_type": child.get("account_type"), "account_currency": dataent.db.get_value('Company', company, "default_currency"), "tax_rate": child.get("tax_rate") }) if root_account or dataent.local.flags.allow_unverified_charts: account.flags.ignore_mandatory = True account.flags.ignore_permissions = True account.insert() accounts.append(account_name_in_db) _import_accounts(child, account.name, root_type) # Rebuild NestedSet HSM tree for Account Doctype # after all accounts are already inserted. dataent.local.flags.ignore_on_update = True _import_accounts(chart, None, None, root_account=True) rebuild_tree("Account", "parent_account") dataent.local.flags.ignore_on_update = False
def test_rebuild_tree(self): rebuild_tree("Item Group", "parent_item_group") self.test_basic_tree()
def execute(): """ assign lft and rgt appropriately """ dataent.reload_doc("hr", "doctype", "employee") rebuild_tree("Employee", "reports_to")
def execute(): dataent.reload_doc("setup", "doctype", "company") rebuild_tree('Company', 'parent_company')