Custom Script - Lock child table fields using array

Hey guys,

I’m having a bit of trouble making this work.

frappe.ui.form.on("Purchase Order", "onload", function(frm, cdt, cdn) {
    console.log('child table function loaded');
    if (frm.doc.workflow_state === "Approved") {
        console.log('child table function triggered');
        var j = 0;
        var df = frappe.meta.get_docfield("Purchase Order Item",
            poi_fields[j], cur_frm.doc.name);
        for (j = 0; j < poi_fields.length; j++) {
            df.read_only = 1;
        }
    }
});

It’s supposed to lock all of the fields in the poi_fields array

var poi_fields = ["item_code", "supplier_part_no", "item_name", "schedule_date",
    "description", "image", "image_view", "qty", "stock_uom", "uom",
    "conversion_factor", "price_list_rate", "discount_percent",
    "base_price_list_rate", "rate", "amount", "base_rate", "base_amount",
    "pricing_rule", "net_rate", "net_amount", "base_net_rate",
    "base_net_amount", "warehouse", "project", "prevdoc_doctype",
    "prevdoc_docname", "prevdoc_detail_docname", "supplier_quotation",
    "supplier_quotation_item", "delivered_by_supplier", "item_group",
    "brand", "bom", "stock_qty", "received_qty", "returned_qty",
    "billed_amt", "item_tax_rate"
];

I have another function set up for the purchase order itself, the next post will be the entire custom script.

//po_fields is an array of all of the fieldnames in the PO, except child tables.    Note, this array does not include section breaks or column breaks.
var po_fiel//po_fields is an array of all of the fieldnames in the PO, except child tables.    Note, this array does not include section breaks or column breaks.

var po_fields = ["title", "naming_series", "read_only", "supplier", "read_only",

    "get_items_from_open_material_requests", "is_subcontracted",

    "supplier_name", "column_break1", "transaction_date", "amended_from",

    "company", "customer", "customer_name", "column_break_19",

    "customer_contact_person", "customer_contact_display",

    "customer_contact_mobile", "customer_contact_email", "supplier_address",

    "contact_person", "address_display", "contact_display",

    "contact_mobile", "contact_email", "col_break_address",

    "shipping_address", "shipping_address_display", "currency",

    "conversion_rate", "cb_price_list", "buying_price_list",

    "price_list_currency", "plc_conversion_rate", "ignore_pricing_rule",

    "items_section", "items", "quality_requirements_table",

    "get_last_purchase_rate", "base_total", "base_net_total",

    "column_break_26", "total", "net_total", "taxes_section",

    "taxes_and_charges", "taxes", "other_charges_calculation",

    "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",

    "base_total_taxes_and_charges", "column_break_39",

    "taxes_and_charges_added", "taxes_and_charges_deducted",

    "total_taxes_and_charges", "apply_discount_on", "base_discount_amount",

    "column_break_45", "additional_discount_percentage", "discount_amount",

    "base_grand_total", "base_in_words", "base_rounded_total",

    "column_break4", "grand_total", "in_words", "advance_paid", "tc_name",

    "terms", "Status", "ref_sq", "party_account_currency",

    "column_break_74", "per_received", "per_billed", "column_break5",

    "letter_head", "select_print_heading", "language",

    "raw_material_details", "supplied_items", "recurring_order",

    "column_break", "is_recurring", "recurring_id", "recurring_type",

    "repeat_on_day_of_month", "end_date", "submit_on_creation",

    "notify_by_email", "notification_email_address",

    "recurring_print_format", "column_break83", "from_date", "to_date",

    "next_date"

];ds = ["title", "naming_series", "read_only", "supplier", "read_only",
    "get_items_from_open_material_requests", "is_subcontracted",
    "supplier_name", "column_break1", "transaction_date", "amended_from",
    "company", "customer", "customer_name", "column_break_19",
    "customer_contact_person", "customer_contact_display",
    "customer_contact_mobile", "customer_contact_email", "supplier_address",
    "contact_person", "address_display", "contact_display",
    "contact_mobile", "contact_email", "col_break_address",
    "shipping_address", "shipping_address_display", "currency",
    "conversion_rate", "cb_price_list", "buying_price_list",
    "price_list_currency", "plc_conversion_rate", "ignore_pricing_rule",
    "items_section", "items", "quality_requirements_table",
    "get_last_purchase_rate", "base_total", "base_net_total",
    "column_break_26", "total", "net_total", "taxes_section",
    "taxes_and_charges", "taxes", "other_charges_calculation",
    "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
    "base_total_taxes_and_charges", "column_break_39",
    "taxes_and_charges_added", "taxes_and_charges_deducted",
    "total_taxes_and_charges", "apply_discount_on", "base_discount_amount",
    "column_break_45", "additional_discount_percentage", "discount_amount",
    "base_grand_total", "base_in_words", "base_rounded_total",
    "column_break4", "grand_total", "in_words", "advance_paid", "tc_name",
    "terms", "Status", "ref_sq", "party_account_currency",
    "column_break_74", "per_received", "per_billed", "column_break5",
    "letter_head", "select_print_heading", "language",
    "raw_material_details", "supplied_items", "recurring_order",
    "column_break", "is_recurring", "recurring_id", "recurring_type",
    "repeat_on_day_of_month", "end_date", "submit_on_creation",
    "notify_by_email", "notification_email_address",
    "recurring_print_format", "column_break83", "from_date", "to_date",
    "next_date"
];
//poi_fields is an array of all of the fieldnames in the Purchase Order Item child table.  Note, this array does not include section breaks or column breaks.
var poi_fields = ["item_code", "supplier_part_no", "item_name", "schedule_date",
    "description", "image", "image_view", "qty", "stock_uom", "uom",
    "conversion_factor", "price_list_rate", "discount_percent",
    "base_price_list_rate", "rate", "amount", "base_rate", "base_amount",
    "pricing_rule", "net_rate", "net_amount", "base_net_rate",
    "base_net_amount", "warehouse", "project", "prevdoc_doctype",
    "prevdoc_docname", "prevdoc_detail_docname", "supplier_quotation",
    "supplier_quotation_item", "delivered_by_supplier", "item_group",
    "brand", "bom", "stock_qty", "received_qty", "returned_qty",
    "billed_amt", "item_tax_rate"
];
//qar_fields is an array of all of the fieldnames in the Quality Codes child table.
var qar_fields = ["code", "description"];
//pr_fields is an array of all of the fieldnames in the Purchasing Requirements child table.
var pr_fields = ["pc", "pcd"];
//Creates Console Verification Message
console.log("PurchaseOrder.js Loaded Successfully");
//Fetches description for Quality Code Table
cur_frm.add_fetch("code", "description", "description");
//Fetches description for Purchasing Code Table
cur_frm.add_fetch("pc", "purchase_code_description", "pcd");
//Locks the Purchase Order for purchase user, for PM Approval
frappe.ui.form.on("Purchase Order", "onload", function(frm) {
    if (user_roles.indexOf("Purchase User") !== 0 && frm.doc.workflow_state ===
        "PM Approval") {
        msgprint(
            "<body bgcolor=\"00FFFB\">\r\n<p style=\"text-align: center;\"><strong><span style=\"font-size:20px;\">Locked for Approval<\/span><\/strong><\/p>\r\n\r\n<p style=\"text-align: center;\"><img alt=\"Sea-Fire Marine\" src=\"http:\/\/www.sea-fire.com\/wp-content\/uploads\/2012\/07\/copy-seafireLogo.png\" style=\"width: 207px; height: 100px;\" \/><\/p>\r\n\r\n<p style=\"text-align: center;\"><span style=\"color:#FF0000;\">THE CREATOR OF A PURCHASE ORDER CANNOT APPROVE IT.  YOU WILL NOT BE ABLE TO SAVE CHANGES TO THIS DOCUMENT.<\/span><\/p>\r\n\r\n<p style=\"text-align: center;\">IF YOU BELIEVE YOU SHOULD HAVE ACCESS TO THIS DOCUMENT, PLEASE RELOAD THE PAGE.<\/p>\r\n\r\n<p style=\"text-align: center;\">If you believe you have encountered this message in error, please contact the ERP System Administrator. &nbsp;<\/p>\r\n\r\n<p style=\"text-align: center;\">MESSAGE ID:001-S-001<\/p>\r\n<\/body>"
        );
        frm.disable_save();
    }
    if (user_roles.indexOf("Purchase User") !== 0 && frm.doc.workflow_state ===
        "QA Approval") {
        msgprint(
            "<body bgcolor=\"00FFFB\">\r\n<p style=\"text-align: center;\"><strong><span style=\"font-size:20px;\">Locked for Approval<\/span><\/strong><\/p>\r\n\r\n<p style=\"text-align: center;\"><img alt=\"Sea-Fire Marine\" src=\"http:\/\/www.sea-fire.com\/wp-content\/uploads\/2012\/07\/copy-seafireLogo.png\" style=\"width: 207px; height: 100px;\" \/><\/p>\r\n\r\n<p style=\"text-align: center;\"><span style=\"color:#FF0000;\">THE CREATOR OF A PURCHASE ORDER CANNOT APPROVE IT.  YOU WILL NOT BE ABLE TO SAVE CHANGES TO THIS DOCUMENT.<\/span><\/p>\r\n\r\n<p style=\"text-align: center;\">IF YOU BELIEVE YOU SHOULD HAVE ACCESS TO THIS DOCUMENT, PLEASE RELOAD THE PAGE.<\/p>\r\n\r\n<p style=\"text-align: center;\">If you believe you have encountered this message in error, please contact the ERP System Administrator. &nbsp;<\/p>\r\n\r\n<p style=\"text-align: center;\">MESSAGE ID:001-S-001<\/p>\r\n<\/body>"
        );
        frm.disable_save();
    }
    if (user_roles.indexOf("Purchase Manager") !== 0 && frm.doc.workflow_state ===
        "QA Approval") {
        msgprint(
            "<body bgcolor=\"00FFFB\">\r\n<p style=\"text-align: center;\"><strong><span style=\"font-size:20px;\">Locked for Approval<\/span><\/strong><\/p>\r\n\r\n<p style=\"text-align: center;\"><img alt=\"Sea-Fire Marine\" src=\"http:\/\/www.sea-fire.com\/wp-content\/uploads\/2012\/07\/copy-seafireLogo.png\" style=\"width: 207px; height: 100px;\" \/><\/p>\r\n\r\n<p style=\"text-align: center;\"><span style=\"color:#FF0000;\">THE CREATOR OF A PURCHASE ORDER CANNOT APPROVE IT.  YOU WILL NOT BE ABLE TO SAVE CHANGES TO THIS DOCUMENT.<\/span><\/p>\r\n\r\n<p style=\"text-align: center;\">IF YOU BELIEVE YOU SHOULD HAVE ACCESS TO THIS DOCUMENT, PLEASE RELOAD THE PAGE.<\/p>\r\n\r\n<p style=\"text-align: center;\">If you believe you have encountered this message in error, please contact the ERP System Administrator. &nbsp;<\/p>\r\n\r\n<p style=\"text-align: center;\">MESSAGE ID:001-S-001<\/p>\r\n<\/body>"
        );
        frm.disable_save();
        //The following locks all of the fields in the PO
    }
    if (frm.doc.workflow_state === "Approved") {
        var i = 0;
        for (i = 0; i < po_fields.length; i++) {
            frm.set_df_property(po_fields[i], "read_only", frm.doc.__islocal ?
                0 : 1);
        }
    }
});
frappe.ui.form.on("Purchase Order", "onload", function(frm, cdt, cdn) {
    console.log('child table function loaded');
    if (frm.doc.workflow_state === "Approved") {
        console.log('child table function triggered');
        var j = 0;
        var df = frappe.meta.get_docfield("Purchase Order Item",
            poi_fields[j], cur_frm.doc.name);
        for (j = 0; j < poi_fields.length; j++) {
            df.read_only = 1;
        }
    }
});