[docs] merged kb and cleanup. warning: don't see the commit

This commit is contained in:
Rushabh Mehta
2015-12-03 17:52:46 +05:30
parent ca4c663e07
commit ab7021ce64
1353 changed files with 6077 additions and 1999 deletions

View File

@@ -0,0 +1,23 @@
# Add a Custom Button
frappe.ui.form.on("Event", "refresh", function(frm) {
frm.add_custom_button(__("Do Something"), function() {
// When this button is clicked, do this
var subject = frm.doc.subject;
var event_type = frm.doc.event_type;
// do something with these values, like an ajax request
// or call a server side frappe function using frappe.call
$.ajax({
url: "http://example.com/just-do-it",
data: {
"subject": subject,
"event_type": event_type
}
// read more about $.ajax syntax at http://api.jquery.com/jquery.ajax/
});
});
});

View File

@@ -0,0 +1,25 @@
To pull a value of a link on selection, use the `add_fetch` method.
add_fetch(link_fieldname, source_fieldname, target_fieldname)
### Example
You create Custom Field **VAT ID** (`vat_id`) in **Customer** and **Sales
Invoice** and want to make sure this value gets updated every time you select
a Customer in a Sales Invoice.
Then in the Sales Invoice Custom Script, add this line:
cur_frm.add_fetch('customer','vat_id','vat_id')
* * *
See: [How to create a custom script]({{docs_base_url}}/user/guides/customize-erpnext/custom-scripts.html)
{next}

View File

@@ -0,0 +1,11 @@
# Date Validation
frappe.ui.form.on("Event", "validate", function(frm) {
if (frm.doc.from_date < get_today()) {
msgprint(__("You can not select past date in From Date"));
throw "past date selected"
}
});
{next}

View File

@@ -0,0 +1,39 @@
Add this in the Custom Script of **Item**, so that the new Item Code is
generated just before the a new Item is saved.
(Thanks to Aditya Duggal)
cur_frm.cscript.custom_validate = function(doc) {
// clear item_code (name is from item_code)
doc.item_code = "";
// first 2 characters based on item_group
switch(doc.item_group) {
case "Test A":
doc.item_code = "TA";
break;
case "Test B":
doc.item_code = "TB";
break;
default:
doc.item_code = "XX";
}
// add next 2 characters based on brand
switch(doc.brand) {
case "Brand A":
doc.item_code += "BA";
break;
case "Brand B":
doc.item_code += "BB";
break;
default:
doc.item_code += "BX";
}
}
{next}

View File

@@ -0,0 +1,18 @@
### How to Create a Custom Script
Create a Custom Script (you must have System Manager role for this):
1. Got to: Setup > Custom Script > New Custom Script
2. Select the DocType in which you want to add the Custom Script
* * *
### Notes
1. Server Custom Scripts are only available for the Administrator.
2. Client Custom Scripts are in Javascript and Server Custom Scripts are in Python.
3. For testing, make sure to go to Tools > Clear Cache and refresh after updating a Custom Script.
### Topics
{index}

View File

@@ -0,0 +1,10 @@
custom-script-fetch-values-from-master
date-validation
generate-item-code-based-on-custom-logic
make-read-only-after-saving
restrict-cancel-rights
restrict-purpose-of-stock-entry
restrict-user-based-on-child-record
sales-invoice-id-based-on-sales-order-id
update-date-field-based-on-value-in-other-date-field
custom-button

View File

@@ -0,0 +1,12 @@
Use the method `cur_frm.set_df_property` to update the field's display.
In this script we also use the `__islocal` property of the doc to check if the
document has been saved atleast once or is never saved. If `__islocal` is `1`,
then the document has never been saved.
frappe.ui.form.on("MyDocType", "refresh", function(frm) {
// use the __islocal value of doc, to check if the doc is saved or not
frm.set_df_property("myfield", "read_only", frm.doc.__islocal ? 0 : 1);
}
{next}

View File

@@ -0,0 +1,17 @@
Add a handler to `custom_before_cancel` event:
cur_frm.cscript.custom_before_cancel = function(doc) {
if (user_roles.indexOf("Accounts User")!=-1 && user_roles.indexOf("Accounts Manager")==-1
&& user_roles.indexOf("System Manager")==-1) {
if (flt(doc.grand_total) > 10000) {
msgprint("You can not cancel this transaction, because grand total \
is greater than 10000");
validated = false;
}
}
}
{next}

View File

@@ -0,0 +1,10 @@
frappe.ui.form.on("Material Request", "validate", function(frm) {
if(user=="user1@example.com" && frm.doc.purpose!="Material Receipt") {
msgprint("You are only allowed Material Receipt");
throw "Not allowed";
}
}
{next}

View File

@@ -0,0 +1,20 @@
// restrict certain warehouse to Material Manager
cur_frm.cscript.custom_validate = function(doc) {
if(user_roles.indexOf("Material Manager")==-1) {
var restricted_in_source = wn.model.get("Stock Entry Detail",
{parent:cur_frm.doc.name, s_warehouse:"Restricted"});
var restricted_in_target = wn.model.get("Stock Entry Detail",
{parent:cur_frm.doc.name, t_warehouse:"Restricted"})
if(restricted_in_source.length || restricted_in_target.length) {
msgprint("Only Material Manager can make entry in Restricted Warehouse");
validated = false;
}
}
}
{next}

View File

@@ -0,0 +1,19 @@
Below script allows you to get naming series in Sales Invoice, same as of corresponding Sales Order.
Invoice uses a prefix M- but the number duplicates the SO doc name (number).
Example: If Sales Order id is SO-12345, then corresponding Sales Invoice id will be set as M-12345.
frappe.ui.form.on("Sales Invoice", "refresh", function(frm){
var sales_order = frm.doc.items[0].sales_order.replace("M", "M-");
if (!frm.doc.__islocal && sales_order && frm.doc.name!==sales_order){
frappe.call({
method: 'frappe.model.rename_doc.rename_doc',
args: {
doctype: frm.doctype,
old: frm.docname,
"new": sales_order,
"merge": false
},
});
}
});

View File

@@ -0,0 +1,7 @@
Below script would auto-set value for the date field, based on the value in another date field.
Example: Production Due Date must be set as two days before Delivery Date. If you have Production Due Date field already, with field type as Date, as per the below given script, date will be auto-updated in it, two days prior Deliver Date.
cur_frm.cscript.custom_delivery_date = function(doc, cdt, cd){
cur_frm.set_value("production_due_date", frappe.datetime.add_days(doc.delivery_date, -2));
}