# Buggy code
def get_charts():
    company = frappe.get_doc("Company", get_company_for_dashboards())
    income_account = company.default_income_account or get_account(
        "Income Account", company.name
    )
    expense_account = company.default_expense_account or get_account(
        "Expense Account", company.name
    )
    bank_account = company.default_bank_account or get_account("Bank", company.name)

    return [
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Income",
            "chart_name": "Income",
            "timespan": "Last Year",
            "color": None,
            "filters_json": json.dumps(
                {"company": company.name, "account": income_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Expenses",
            "chart_name": "Expenses",
            "timespan": "Last Year",
            "color": None,
            "filters_json": json.dumps(
                {"company": company.name, "account": expense_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Bank Balance",
            "chart_name": "Bank Balance",
            "timespan": "Last Year",
            "color": "#ffb868",
            "filters_json": json.dumps(
                {"company": company.name, "account": bank_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Monthly",
            "name": "Incoming Bills (Purchase Invoice)",
            "chart_name": "Incoming Bills (Purchase Invoice)",
            "timespan": "Last Year",
            "color": "#a83333",
            "value_based_on": "base_grand_total",
            "filters_json": json.dumps({}),
            "chart_type": "Sum",
            "timeseries": 1,
            "based_on": "posting_date",
            "owner": "Administrator",
            "document_type": "Purchase Invoice",
            "type": "Bar",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Monthly",
            "name": "Outgoing Bills (Sales Invoice)",
            "chart_name": "Outgoing Bills (Sales Invoice)",
            "timespan": "Last Year",
            "color": "#7b933d",
            "value_based_on": "base_grand_total",
            "filters_json": json.dumps({}),
            "chart_type": "Sum",
            "timeseries": 1,
            "based_on": "posting_date",
            "owner": "Administrator",
            "document_type": "Sales Invoice",
            "type": "Bar",
        },
    ]


# Traceback (frappe.exceptions.DoesNotExistError)
Migrating test.localhost
Executing execute:frappe.reload_doc('core', 'doctype', 'doctype_action', force=True) #2019-09-23 in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.248s
Executing execute:frappe.reload_doc('core', 'doctype', 'doctype_link', force=True) #2019-09-23 in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.107s
Executing frappe.patches.v12_0.change_existing_dashboard_chart_filters in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.047s
Executing execute:frappe.delete_doc("Test Runner") in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.017s
Executing execute:frappe.db.set_default('desktop:home_page', 'workspace') in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.081s
Executing frappe.patches.v13_0.website_theme_custom_scss in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.364s
Executing frappe.patches.v13_0.set_existing_dashboard_charts_as_public in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.366s
Executing frappe.patches.v13_0.set_path_for_homepage_in_web_page_view in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.155s
Executing frappe.patches.v13_0.migrate_translation_column_data in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.181s
Executing frappe.patches.v13_0.set_read_times in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.286s
Executing frappe.patches.v13_0.remove_web_view in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.013s
Executing erpnext.patches.v12_0.update_is_cancelled_field in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.017s
Executing erpnext.patches.v11_0.rename_duplicate_item_code_values in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.012s
Executing execute:frappe.delete_doc_if_exists("Report", "Loan Repayment") in test.localhost (_fcadc07dbdbc21db)
Success: Done in 1.58s
Executing erpnext.patches.v12_0.set_automatically_process_deferred_accounting_in_accounts_settings in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.268s
Executing erpnext.patches.v12_0.set_production_capacity_in_workstation in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.242s
Executing erpnext.patches.v12_0.set_employee_preferred_emails in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.072s
Executing erpnext.patches.v12_0.set_against_blanket_order_in_sales_and_purchase_order in test.localhost (_fcadc07dbdbc21db)
Success: Done in 1.073s
Executing erpnext.patches.v12_0.set_cost_center_in_child_table_of_expense_claim in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.189s
Executing erpnext.patches.v12_0.add_eway_bill_in_delivery_note in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.088s
Executing erpnext.patches.v12_0.set_lead_title_field in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.581s
Executing erpnext.patches.v12_0.set_published_in_hub_tracked_item in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.374s
Executing erpnext.patches.v12_0.set_job_offer_applicant_email in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.258s
Executing erpnext.patches.v12_0.move_bank_account_swift_number_to_bank in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.594s
Executing erpnext.patches.v12_0.rename_bank_reconciliation in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.057s
Executing erpnext.patches.v12_0.set_purchase_receipt_delivery_note_detail in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.919s
Executing erpnext.patches.v12_0.add_permission_in_lower_deduction in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.016s
Executing erpnext.patches.v12_0.rename_account_type_doctype in test.localhost (_fcadc07dbdbc21db)
Success: Done in 1.138s
Executing erpnext.patches.v12_0.update_healthcare_refactored_changes in test.localhost (_fcadc07dbdbc21db)
Success: Done in 3.16s
Executing erpnext.patches.v12_0.set_total_batch_quantity in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.251s
Executing erpnext.patches.v12_0.set_updated_purpose_in_pick_list in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.264s
Executing erpnext.patches.v12_0.set_default_payroll_based_on in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.205s
Executing erpnext.patches.v12_0.update_end_date_and_status_in_email_campaign in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.019s
Executing erpnext.patches.v13_0.move_tax_slabs_from_payroll_period_to_income_tax_slab #123 in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.725s
Executing erpnext.patches.v12_0.fix_quotation_expired_status in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.016s
Executing erpnext.patches.v12_0.update_appointment_reminder_scheduler_entry in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.013s
Executing erpnext.patches.v12_0.retain_permission_rules_for_video_doctype in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.388s
Executing erpnext.patches.v13_0.patch_to_fix_reverse_linking_in_additional_salary_encashment_and_incentive in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.881s
Executing execute:frappe.delete_doc_if_exists("Page", "appointment-analytic") in test.localhost (_fcadc07dbdbc21db)
Success: Done in 0.165s
Updating DocTypes for frappe        : [========================================] 100%
Updating DocTypes for erpnext       : [========================================] 100%
Updating Dashboard for frappe
Updating Dashboard for erpnext
Traceback (most recent call last):
File "/home/frappe/frappe-bench/commands/auto_migrate.py", line 115, in <module>
main()
File "/home/frappe/frappe-bench/commands/auto_migrate.py", line 109, in main
migrate_sites(maintenance_mode=True)
File "/home/frappe/frappe-bench/commands/migrate.py", line 41, in migrate_sites
migrate()
File "/home/frappe/frappe-bench/apps/frappe/frappe/migrate.py", line 58, in migrate
sync_dashboards()
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/dashboard.py", line 86, in sync_dashboards
config = get_config(app_name, module_name)
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/dashboard.py", line 110, in get_config
return frappe._dict(module_dashboards.get_data())
File "/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/dashboard_fixtures.py", line 11, in get_data
"charts": get_charts(),
File "/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/dashboard_fixtures.py", line 28, in get_charts
company = frappe.get_doc("Company", get_company_for_dashboards())
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 772, in get_doc
doc = frappe.model.document.get_doc(*args, **kwargs)
File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 71, in get_doc
return controller(*args, **kwargs)
File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 106, in __init__
self.load_from_db()
File "/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 149, in load_from_db
frappe.throw(_("{0} {1} not found").format(_(self.doctype), self.name), frappe.DoesNotExistError)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 397, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 376, in msgprint
_raise_exception()
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 327, in _raise_exception
raise raise_exception(msg)
frappe.exceptions.DoesNotExistError: Company None not found

# Fixed code
def get_charts(company):
    income_account = company.default_income_account or get_account(
        "Income Account", company.name
    )
    expense_account = company.default_expense_account or get_account(
        "Expense Account", company.name
    )
    bank_account = company.default_bank_account or get_account("Bank", company.name)

    return [
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Income",
            "chart_name": "Income",
            "timespan": "Last Year",
            "color": None,
            "filters_json": json.dumps(
                {"company": company.name, "account": income_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Expenses",
            "chart_name": "Expenses",
            "timespan": "Last Year",
            "color": None,
            "filters_json": json.dumps(
                {"company": company.name, "account": expense_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Quarterly",
            "name": "Bank Balance",
            "chart_name": "Bank Balance",
            "timespan": "Last Year",
            "color": "#ffb868",
            "filters_json": json.dumps(
                {"company": company.name, "account": bank_account}
            ),
            "source": "Account Balance Timeline",
            "chart_type": "Custom",
            "timeseries": 1,
            "owner": "Administrator",
            "type": "Line",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Monthly",
            "name": "Incoming Bills (Purchase Invoice)",
            "chart_name": "Incoming Bills (Purchase Invoice)",
            "timespan": "Last Year",
            "color": "#a83333",
            "value_based_on": "base_grand_total",
            "filters_json": json.dumps({}),
            "chart_type": "Sum",
            "timeseries": 1,
            "based_on": "posting_date",
            "owner": "Administrator",
            "document_type": "Purchase Invoice",
            "type": "Bar",
        },
        {
            "doctype": "Dashboard Chart",
            "time_interval": "Monthly",
            "name": "Outgoing Bills (Sales Invoice)",
            "chart_name": "Outgoing Bills (Sales Invoice)",
            "timespan": "Last Year",
            "color": "#7b933d",
            "value_based_on": "base_grand_total",
            "filters_json": json.dumps({}),
            "chart_type": "Sum",
            "timeseries": 1,
            "based_on": "posting_date",
            "owner": "Administrator",
            "document_type": "Sales Invoice",
            "type": "Bar",
        },
    ]