Monday, 6 April 2015

ERPNext: Create custom button and call custom


ERPNext: Create custom button and call custom method from app

cur_frm.cscript.custom_refresh = function(doc) {
cur_frm.add_custom_button(__('Generate Document'),
cur_frm.cscript['generate_document'], "icon-exclamation", "btn-default");
};

cur_frm.cscript.generate_document = function(doc) {
    cur_frm.call({
    "method": 'ef_app.ef_doc.ef_doc.generate_document',
     "args": {
           "doc": doc.docs_required
        },
     callback: function(r) {
           if(!r.exc) {
           }
           }
})
};


above script will create custom button on doctype.
after click, generate_document custom script will be called.
from custom script custom python method is called.

this example explains how to call custom event onclick.



Method II:

frappe.ui.form.on("Sales Order", "generate_document", function(frm, doctype, name) {
    msgprint("hi")
    frappe.model.open_mapped_doc({
            method: "ef_fruits.ef_doc.ef_doc.
generate_document",
            frm: cur_frm
        });
});  


@frappe.whitelist()
def
generate_document(source_name, target=None):
    doc_m = frappe.get_doc("Sales Order", source_name)
    doc_m.submit();

    frappe.msgprint(doc_m.customer)    

Method III: to change customer name
@frappe.whitelist()
def make_sales_invoice(source_name, target=None):
    doc_m = frappe.get_doc("Sales Order", source_name)
    doc_m.customer = "
Sambhaji"
    doc_m.set("customer","Sambhaji");
    doc_m.save();

    frappe.msgprint(doc_m.customer)   

#doc_m.save() this saves the document.
#doc_m.set(key, value)  this saves set the values

Method IV: this will insert new document "Bank Set Of Documents"

@frappe.whitelist()
def make_bank_document(source_name, target=None):
    doc_m = frappe.get_doc("Sales Order", source_name)
    doc_m.customer = "Satish"
    frappe.msgprint(doc_m.name)
    bank=frappe.new_doc("Bank Set Of Documents")
    bank.sales_order = doc_m.name
    bank.rounded_total = doc_m.rounded_total,
    bank.customer_name = doc_m.customer_name,
    bank.payment_terms = doc_m.payment_terms
    bank.insert() 


12 comments:

  1. Can you explain how and from where to invoke the custom scripts? am a newbie here, other than the sample scripts, I couldn't find clear documentation on how to invoke them. tia

    ReplyDelete
  2. You can access custom script from, Setup>>Customize>>Customize Script
    https://localhost:8000/desk#Form/Custom Script

    In Custom Script, Select DocType on which you want to write custom script.
    Write code into Script block, Never forgot to reload/clear cache to test custom script.

    ReplyDelete
  3. Correct,but am not clear about where this script will be saved and how it will be invoked!

    ReplyDelete
  4. script will be saved in database,
    For advance detail you can refer core code.
    https://github.com/frappe/frappe/tree/develop/frappe/custom/doctype/custom_script
    https://github.com/frappe/frappe/tree/develop/frappe/custom/doctype/customize_form

    ReplyDelete
  5. Thanks. I understand that the custom scripts are handled in the core code based on the doctype that is selected. All that needs to be done is just write the script and test them. For now, I can do with this level of understanding.

    ReplyDelete


  6. I am trying to make a similar table as Tax and Other Charges in Sales Invoice
    I have written 2 functions , one in sales_invoice.js and corresponding function
    in sales_invoice.py file
    I have created a button which when clicked calls the .js code but I am getting the following error:

    Traceback (innermost last):
    File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 51, in application
    response = frappe.handler.handle()
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 66, in handle
    execute_cmd(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 77, in execute_cmd
    method = get_attr(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 98, in get_attr
    method = frappe.get_attr(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 520, in get_attr
    return getattr(get_module(modulename), methodname)
    AttributeError: 'module' object has no attribute 'get_tax_and_other_charges'

    Below is the .js code and .py code.

    //My function to get all other charges in JS
    frappe.ui.form.on("Sales Invoice", "get_other_charges", function(frm) {

    frm.set_value("sales_taxes_and_charges", []);
    alert("The value entered was \n" + frm.doc.sales_taxes_and_charges_master_title);

    return frappe.call({
    method: 'erpnext.accounts.doctype.sales_invoice.sales_invoice.get_tax_and_other_charges',
    args: {
    args: {
    "sales_taxes_and_charges_master_title": frm.doc.sales_taxes_and_charges_master_title,
    }
    },
    callback: function(r, rt) {
    if(r.message) {
    frm.fields_dict.get_other_charges.$input.addClass("btn-primary");
    alert("The value entered was 1 \n" + frm.doc.sales_taxes_and_charges_master_title);
    frappe.model.clear_table(frm.doc, "sales_taxes_and_charges");
    alert("The value entered was 2 \n" + frm.doc.sales_taxes_and_charges_master_title);

    $.each(r.message, function(i, d) {
    var tax_detail = frappe.model.add_child(frm.doc, "Sales Taxes and Charges", "sales_taxes_and_charges");
    tax_detail.charge_type = d.charge_type;
    tax_detail.description = d.description;
    tax_detail.rate = d.rate;
    tax_detail.tax_amount = d.tax_amount;

    });
    }
    refresh_field("sales_taxes_and_charges");
    }
    })

    })
    //My function to get all other charges in PY
    @frappe.whitelist()
    def get_tax_and_other_chargs(sales_taxes_and_charges_master_title):
    flat_other_charges=[]
    other_charges_list=frappe.db.sql("""
    select
    SC.charge_type,SC.description,SC.rate,SC.tax_amount
    from
    tabSales Taxes and Charges SC join tabSales Taxes and Charges Master SCM on SC.parent=SCM.name where SCM.name=%s""")
    for d in other_charges_list:
    flat_other_charges({
    'charge_ype':d.charge_type,
    'description':d.description,
    'rate':d.rate,
    'tax_amount':d.tax_amount
    })
    return flat_other_charges

    ReplyDelete
    Replies
    1. @Dn Gupta

      Why there is 2 args
      args: {
      args: {
      "sales_taxes_and_charges_master_title": frm.doc.sales_taxes_and_charges_master_title,
      }
      },


      Also, Your error says, your function doesn't exit in ...py file,
      This is because you have typing mistake.
      see: 'erpnext.accounts.doctype.sales_invoice.sales_invoice.get_tax_and_other_charges',

      @frappe.whitelist()
      def get_tax_and_other_chargs(sales_taxes_and_charges_master_title):

      in function defination you have written "_chargs" instead of "charges"

      Please feel free to skype me, my skype id is kolate.sambhaji

      Delete
  7. Yes i have correct it but still i not getting my result..some error is in JS file bcoz my page is not showing the field...i am sending my codes(JS & PY) please check and reply me as soon as possible
    Codes are Below
    Sales_Invoice.JS


    cur_frm.cscript.custom_refresh = function(doc) {
    cur_frm.add_custom_button(__('Get Tax And Other Charges'),
    cur_frm.cscript['get_tax_and_other_charges'], "icon-exclamation", "btn-default");
    };//This is the button by which i want to fill all the records of Sales Taxes and Charges in a table(Like same as in Payment Tool "Get Outstanding Vouchers")

    cur_frm.cscript.get_tax_and_other_charges= function(doc) {
    cur_frm.call({
    "method": 'erpnext.accounts.doctype.sales_invoice.sales_invoice.get_tax_and_other_charges',
    "args": {
    "doc": doc.sales_taxes_and_charges_master_title,
    },
    callback: function(r) {
    $.each(r.message, function(i, d) {
    var sales_tax_and_other_charges = frappe.model.add_child(frm.doc, "Sales Taxes and Charges", "sales_taxes_and_charges");
    sales_tax_and_other_charges.charge_type=d.charge_type
    sales_tax_and_other_charges.description=d.description
    sales_tax_and_other_charges.rate=d.tax_amount
    }
    }
    })
    };

    Sales_Invoice.PY

    @frappe.whitelist()
    def get_tax_and_other_charges(sales_taxes_and_charges_master_title):
    flat_other_charges=[]
    other_charges_list = frappe.db.sql("""
    select
    SC.charge_type,SC.description,SC.rate,SC.tax_amount
    from
    `tabSales Taxes and Charges` SC join `tabSales Taxes and Charges Master` SCM on
    SCM.name=SC.parent where SCM.name=%s""".format(sales_taxes_and_charges_master_title))

    for d in other_charges_list:
    flat_other_charges({
    'charge type':d.charge_type,
    'description':d.description,
    'rate':d.rate,
    'tax_amount':d.tax_amount
    })
    return flat_other_charges

    ReplyDelete
    Replies
    1. What is console error log?

      you can refer this post for fetching data into table.
      http://sbkolate.blogspot.in/2015/03/in-erpnext-fetch-table-field-from.html
      http://sbkolate.blogspot.in/2015/03/erpnext-call-custom-app-methon-using.html
      http://sbkolate.blogspot.in/2015/03/erpnext-call-custom-app-methon-custom.html

      Delete
  8. I am getting the same error as earlier. Below is the console error log.


    Traceback (innermost last):
    File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 51, in application
    response = frappe.handler.handle()
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 66, in handle
    execute_cmd(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 77, in execute_cmd
    method = get_attr(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 98, in get_attr
    method = frappe.get_attr(cmd)
    File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 520, in get_attr
    return getattr(get_module(modulename), methodname)
    AttributeError: 'module' object has no attribute 'get_tax_and_other_charges'

    ReplyDelete
  9. Hello Sambhaji Kolate,

    I have just started learning the erpnext so i am newbie here.

    I want to add a custom button like 'confirm sale' in selling module in quotation form while its in draft status...

    so user can directly 'confirm sale' or 'cancel sale' without submitting the quotation.
    i have already added the buttons, just need to write custom script just like 'Make sale order' after submitting quotation.

    Can you please walk me through this.

    ReplyDelete
    Replies
    1. What action you want on confirm sale?
      also pls drop me email at kolate.sambhaji@gmail.com
      I will be happy to help you

      Delete