[docs] custom script examples, [build] move CMS make to app via event_handlers.on_build

This commit is contained in:
Rushabh Mehta
2013-09-03 17:04:05 +05:30
parent 4263344cd2
commit d50d924ee5
12 changed files with 190 additions and 11 deletions

View File

@@ -0,0 +1,24 @@
---
{
"_label": "Calculate Incentive for Sales Team"
}
---
Can be used in any Sales Transaction with **Sales Team** Table:
cur_frm.cscript.custom_validate = function(doc) {
// calculate incentives for each person on the deal
total_incentive = 0
$.each(wn.model.get("Sales Team", {parent:doc.name}), function(i, d) {
// calculate incentive
var incentive_percent = 2;
if(doc.grand_total > 400) incentive_percent = 4;
// actual incentive
d.incentives = flt(doc.grand_total) * incentive_percent / 100;
total_incentive += flt(d.incentives)
});
doc.total_incentive = total_incentive;
}

View File

@@ -0,0 +1,20 @@
---
{
"_label": "Custom Script: Fetch Values from Master"
}
---
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.dev.custom_script.html)

View File

@@ -0,0 +1,35 @@
---
{
"_label": "Generate Item Code based on Custom Logic"
}
---
Add this in the Custom Script of **Item**, so that the new Item Code is generated just before the a new Item is saved.
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";
}
}

View File

@@ -0,0 +1,13 @@
---
{
"_label": "Make an Item read-only after Saving"
}
---
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.
cur_frm.cscript.custom_refresh = function(doc) {
// use the __islocal value of doc, to check if the doc is saved or not
cur_frm.set_df_property("myfield", "read_only", doc.__islocal ? 0 : 1);
}

View File

@@ -0,0 +1,12 @@
---
{
"_label": "Date Validation: Do not allow past dates in a date field"
}
---
cur_frm.cscript.custom_validate = function(doc) {
if (doc.from_date < get_today()) {
msgprint("You can not select past date in From Date");
validated = false;
}
}

View File

@@ -0,0 +1,12 @@
---
{
"_label": "Restrict Purpose of Stock Entry"
}
---
cur_frm.cscript.custom_validate = function(doc) {
if(user=="user1@example.com" && doc.purpose!="Material Receipt") {
msgprint("You are only allowed Material Receipt");
validated = false;
}
}

View File

@@ -0,0 +1,22 @@
---
{
"_label": "Restrict User Based on Child Record (Warehouse)"
}
---
// 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;
}
}
}

View File

@@ -0,0 +1,17 @@
---
{
"_label": "Restrict Cancel Rights based on Certain Order Value"
}
---
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;
}
}
}