def calculate_new_data(data, project, project_id):
    inactive_data = cal.get_inactive_sku(data)
    data, active_list = cal.get_active_sku(data)

    original_data = cal.get_data(project_id)
    original_inactive_data = cal.get_inactive_sku(original_data)
    original_inactive_list = cal.get_inactive_list(original_inactive_data)
    original_data, original_active_list = cal.get_active_sku(original_data)

    is_sku_introduced = cal.is_sku_introduced(original_data, data)
    # print("is_sku_introduced: {}".format(is_sku_introduced))

    if is_sku_introduced:
        zero_price_level = cal.get_zero_price(original_active_list)
        sku, price, price_list = cal.get_hbu_data(project.hbu_filename,
                                                  project.start_price,
                                                  original_active_list)
        df_util = cal.get_utility_table(price, price_list, zero_price_level,
                                        data, project_id)
        sop = cal.calculate_sop_after_utility(df_util, sku)

        cal.update_sop(data, sop)
        cal.remove_inactive_sop(data, original_inactive_list)
        cal.update_sop_after_wd_without_rebase(data)

        cal.update_volume_share_using_base(data)
        cal.remove_inactive_volume_share(data, original_inactive_list)
        cal.rebase_volume_share_using_base(data)

        cal.update_old_sop(data, original_inactive_list)
        cal.update_old_volume_share(data, original_inactive_list)

    else:
        cal.default_old_sop(data)
        cal.default_old_volume_share(data)

    zero_price_level = cal.get_zero_price(active_list)
    sku, price, price_list = cal.get_hbu_data(project.hbu_filename,
                                              project.start_price, active_list)
    df_util = cal.get_utility_table(price, price_list, zero_price_level, data,
                                    project_id)
    sop = cal.calculate_sop_after_utility(df_util, sku)
    cal.update_sop(data, sop)
    cal.update_sop_after_wd_without_rebase(data)

    cal.update_volume_share(data)
    cal.rebase_volume_share(data)

    cal.calculate_value_part(data, project.msu, project.vsu)

    data = cal.join_active_inactive_sku(data, inactive_data)

    new_data = cal.convert_to_json(data)

    return new_data
def get_project2(project_id):
    user_projects = flask_praetorian.current_user().projects
    if any(project.id == project_id for project in user_projects):
        # User has access to project
        # start = datetime.datetime.utcnow()

        project = Project.query.filter_by(id=project_id).first()
        data = cal.get_data(project_id)

        # Old method, does not calculate sop
        # ----------------------------------------
        # inactive_data = cal.get_inactive_sku(data)
        # data, active_list = cal.get_active_sku(data)
        # cal.calculate_value_part(data,project.msu,project.vsu)
        # data = cal.join_active_inactive_sku(data,inactive_data)
        # data_json = cal.convert_to_json(data)

        inactive_data = cal.get_inactive_sku(data)
        # time = datetime.datetime.utcnow() - start
        # print('1',time)
        data, active_list = cal.get_active_sku(data)
        # time = datetime.datetime.utcnow() - start
        # print('2',time)
        zero_price_level = cal.get_zero_price(active_list)
        # time = datetime.datetime.utcnow() - start
        # print('3',time)
        sku, price, price_list = cal.get_hbu_data(project.hbu_filename,
                                                  project.start_price,
                                                  active_list)
        # time = datetime.datetime.utcnow() - start
        # print('4',time)
        df_util = cal.get_init_utility_table(price, price_list,
                                             zero_price_level, data,
                                             project_id)
        # print('df_util',df_util)
        # time = datetime.datetime.utcnow() - start
        # print('5',time)
        sop = cal.calculate_sop_after_utility(df_util, sku)
        # time = datetime.datetime.utcnow() - start
        # print('6',time)
        cal.update_sop(data, sop)
        # time = datetime.datetime.utcnow() - start
        # print('7',time)

        # cal.update_volume_share(data)
        # cal.rebase_volume_share(data)

        cal.init_sop(data)
        # time = datetime.datetime.utcnow() - start
        # ('print8',time)

        cal.calculate_value_part(data, project.msu, project.vsu)
        # time = datetime.datetime.utcnow() - start
        # print('9',time)

        cal.calculate_financials(data)
        cal.calculate_financials_zero(inactive_data)

        data = cal.join_active_inactive_sku(data, inactive_data)

        # time = datetime.datetime.utcnow() - start
        # print('10',time)

        data_json = cal.convert_to_json(data)

        return flask.jsonify(data_json)
    else:
        # User does not have access to project
        return {'message': 'Unauthorized to view this project'}, 401
def introduction(project_id):
    user_projects = flask_praetorian.current_user().projects
    if any(project.id == project_id for project in user_projects):
        # User has access to project
        project = Project.query.filter_by(id=project_id).first()

        req = flask.request.get_json(force=True)
        sku_id = req.get("sku-id", None)
        is_active = req.get("is_active", None)
        data_json = req.get("data", None)

        data = cal.df_load_json(data_json)

        # is_price_changed = cal.is_price_changed(data)
        # print("is_price_changed: ", is_price_changed)
        # is_wd_changed = cal.is_wd_changed(data)
        # print("is_wd_changed: ", is_wd_changed)

        cal.activate_sku(data, sku_id, is_active)
        if not is_active:
            cal.remove_sop(data, sku_id)

        inactive_data = cal.get_inactive_sku(data)
        data, active_list = cal.get_active_sku(data)

        original_data = cal.get_data(project_id)
        original_inactive_data = cal.get_inactive_sku(original_data)
        original_inactive_list = cal.get_inactive_list(original_inactive_data)
        original_data, original_active_list = cal.get_active_sku(original_data)

        is_sku_introduced = cal.is_sku_introduced(original_data, data)
        # print("is_sku_introduced: {}".format(is_sku_introduced))

        if is_sku_introduced:
            zero_price_level = cal.get_zero_price(original_active_list)
            sku, price, price_list = cal.get_hbu_data(project.hbu_filename,
                                                      project.start_price,
                                                      original_active_list)
            df_util = cal.get_utility_table(price, price_list,
                                            zero_price_level, data, project_id)
            sop = cal.calculate_sop_after_utility(df_util, sku)

            cal.update_sop(data, sop)
            cal.remove_inactive_sop(data, original_inactive_list)
            cal.update_sop_after_wd(data)

            cal.update_volume_share_using_base(data)
            cal.remove_inactive_volume_share(data, original_inactive_list)
            cal.rebase_volume_share_using_base(data)

            cal.update_old_sop(data, original_inactive_list)
            cal.update_old_volume_share(data, original_inactive_list)

        else:
            cal.default_old_sop(data)
            cal.default_old_volume_share(data)

        zero_price_level = cal.get_zero_price(active_list)
        sku, price, price_list = cal.get_hbu_data(project.hbu_filename,
                                                  project.start_price,
                                                  active_list)
        df_util = cal.get_utility_table(price, price_list, zero_price_level,
                                        data, project_id)
        sop = cal.calculate_sop_after_utility(df_util, sku)
        cal.update_sop(data, sop)
        cal.update_sop_after_wd_without_rebase(data)

        cal.update_volume_share(data)
        cal.rebase_volume_share(data, original_active_list)

        cal.calculate_value_part(data, project.msu, project.vsu)

        cal.calculate_financials(data)
        cal.calculate_financials_zero(inactive_data)

        data = cal.join_active_inactive_sku(data, inactive_data)

        new_data = cal.convert_to_json(data)

        return flask.jsonify(new_data)
    else:
        # User does not have access to project
        return {'message': 'Unauthorized to view this project'}, 401