Server side onload functionality in multiple docs
diff --git a/erpnext/accounts/doctype/account/account.js b/erpnext/accounts/doctype/account/account.js
index 40a2d3c..f085c8b 100644
--- a/erpnext/accounts/doctype/account/account.js
+++ b/erpnext/accounts/doctype/account/account.js
@@ -19,7 +19,7 @@
cur_frm.toggle_enable(['account_name', 'group_or_ledger', 'company'], false);
if(doc.group_or_ledger=='Ledger') {
- cur_frm.toggle_display('freeze_account', doc.can_freeze_account);
+ cur_frm.toggle_display('freeze_account', doc.__onload && doc.__onload.can_freeze_account);
}
// read-only for root accounts
diff --git a/erpnext/accounts/doctype/account/account.py b/erpnext/accounts/doctype/account/account.py
index 3c73d1f..ad588b5 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -12,16 +12,9 @@
def onload(self):
frozen_accounts_modifier = frappe.db.get_value("Accounts Settings", "Accounts Settings", "frozen_accounts_modifier")
- print frozen_accounts_modifier
if frozen_accounts_modifier in frappe.user.get_roles():
- self.can_freeze_account = True
+ self.get("__onload").can_freeze_account = True
- def as_dict(self, no_nulls=False):
- doc = super(Account, self).as_dict(no_nulls)
- if self.get("can_freeze_account"):
- doc["can_freeze_account"] = self.can_freeze_account
-
- return doc
def autoname(self):
self.name = self.account_name.strip() + ' - ' + \
diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
index 3431ff4..158dec2 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -110,6 +110,12 @@
entries_add: function(doc, cdt, cdn) {
var row = frappe.get_doc(cdt, cdn);
this.frm.script_manager.copy_from_first_row("entries", row, ["expense_account", "cost_center"]);
+ },
+
+ on_submit: function() {
+ $.each(this.frm.doc["entries"], function(i, row) {
+ if(row.purchase_receipt) frappe.model.clear_doc("Purchase Receipt", row.purchase_receipt)
+ })
}
});
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
index ed2a1d4..a5707bb 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -387,6 +387,10 @@
if(cint(frappe.boot.notification_settings.sales_invoice)) {
cur_frm.email_doc(frappe.boot.notification_settings.sales_invoice_message);
}
+
+ $.each(doc["entries"], function(i, row) {
+ if(row.delivery_note) frappe.model.clear_doc("Delivery Note", row.delivery_note)
+ })
}
cur_frm.cscript.convert_into_recurring_invoice = function(doc, dt, dn) {
diff --git a/erpnext/hr/doctype/employee/employee.js b/erpnext/hr/doctype/employee/employee.js
index fabdfb8..96aca00 100644
--- a/erpnext/hr/doctype/employee/employee.js
+++ b/erpnext/hr/doctype/employee/employee.js
@@ -18,9 +18,10 @@
refresh: function() {
var me = this;
erpnext.toggle_naming_series();
- if(!this.frm.doc.__islocal && !this.frm.doc.salary_structure_exists) {
- cur_frm.add_custom_button(__('Make Salary Structure'), function() {
- me.make_salary_structure(this); });
+ if(!this.frm.doc.__islocal && this.frm.doc.__onload &&
+ !this.frm.doc.__onload.salary_structure_exists) {
+ cur_frm.add_custom_button(__('Make Salary Structure'), function() {
+ me.make_salary_structure(this); });
}
},
diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py
index 2ae4a2a..9840df7 100644
--- a/erpnext/hr/doctype/employee/employee.py
+++ b/erpnext/hr/doctype/employee/employee.py
@@ -14,17 +14,9 @@
class Employee(Document):
def onload(self):
- self.salary_structure_exists = frappe.db.get_value("Salary Structure",
+ self.get("__onload").salary_structure_exists = frappe.db.get_value("Salary Structure",
{"employee": self.name, "is_active": "Yes", "docstatus": ["!=", 2]})
- def as_dict(self, no_nulls=False):
- doc = super(Employee, self).as_dict(no_nulls)
-
- if hasattr(self, "salary_structure_exists"):
- doc["salary_structure_exists"] = self.salary_structure_exists
-
- return doc
-
def autoname(self):
naming_method = frappe.db.get_value("HR Settings", None, "emp_created_by")
if not naming_method:
diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.js b/erpnext/hr/doctype/salary_structure/salary_structure.js
index 8ee67a5..ee42d0c 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.js
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.js
@@ -57,6 +57,7 @@
cur_frm.cscript.validate = function(doc, cdt, cdn) {
calculate_totals(doc, cdt, cdn);
+ if(doc.employee && doc.is_active == "Yes") frappe.model.clear_doc("Employee", doc.employee);
}
cur_frm.fields_dict.employee.get_query = function(doc,cdt,cdn) {
diff --git a/erpnext/selling/doctype/customer/customer.js b/erpnext/selling/doctype/customer/customer.js
index 94b8f66..e840758 100644
--- a/erpnext/selling/doctype/customer/customer.js
+++ b/erpnext/selling/doctype/customer/customer.js
@@ -42,6 +42,10 @@
}
}
+cur_frm.cscript.validate = function(doc, dt, dn) {
+ if(doc.lead_name) frappe.model.clear_doc("Lead", doc.lead_name);
+}
+
cur_frm.cscript.setup_dashboard = function(doc) {
cur_frm.dashboard.reset(doc);
if(doc.__islocal)
diff --git a/erpnext/selling/doctype/lead/lead.js b/erpnext/selling/doctype/lead/lead.js
index 66b5270..6172b1e 100644
--- a/erpnext/selling/doctype/lead/lead.js
+++ b/erpnext/selling/doctype/lead/lead.js
@@ -10,7 +10,7 @@
this.frm.fields_dict.customer.get_query = function(doc, cdt, cdn) {
return { query: "erpnext.controllers.queries.customer_query" } }
},
-
+
onload: function() {
if(cur_frm.fields_dict.lead_owner.df.options.match(/^User/)) {
cur_frm.fields_dict.lead_owner.get_query = function(doc, cdt, cdn) {
@@ -27,31 +27,30 @@
<span class="help">'+__('Automatically extract Leads from a mail box e.g.')+' "sales@example.com"</span></p>';
}
},
-
+
refresh: function() {
var doc = this.frm.doc;
erpnext.toggle_naming_series();
this.frm.clear_custom_buttons();
- this.frm.__is_customer = this.frm.__is_customer || this.frm.doc.__is_customer;
- if(!this.frm.doc.__islocal && !this.frm.doc.__is_customer) {
+ if(!this.frm.doc.__islocal && this.frm.doc.__onload && !this.frm.doc.__onload.is_customer) {
this.frm.add_custom_button(__("Create Customer"), this.create_customer);
this.frm.add_custom_button(__("Create Opportunity"), this.create_opportunity);
this.frm.appframe.add_button(__("Send SMS"), this.frm.cscript.send_sms, "icon-mobile-phone");
}
-
+
cur_frm.communication_view = new frappe.views.CommunicationList({
list: frappe.get_list("Communication", {"parenttype": "Lead", "parent":this.frm.doc.name}),
parent: this.frm.fields_dict.communication_html.wrapper,
doc: this.frm.doc,
recipients: this.frm.doc.email_id
});
-
+
if(!this.frm.doc.__islocal) {
this.make_address_list();
}
},
-
+
make_address_list: function() {
var me = this;
if(!this.frm.address_list) {
@@ -73,15 +72,15 @@
// note: render_address_row is defined in contact_control.js
}
this.frm.address_list.run();
- },
-
+ },
+
create_customer: function() {
frappe.model.open_mapped_doc({
method: "erpnext.selling.doctype.lead.lead.make_customer",
frm: cur_frm
})
- },
-
+ },
+
create_opportunity: function() {
frappe.model.open_mapped_doc({
method: "erpnext.selling.doctype.lead.lead.make_opportunity",
diff --git a/erpnext/selling/doctype/lead/lead.py b/erpnext/selling/doctype/lead/lead.py
index b40132e..6b44e57 100644
--- a/erpnext/selling/doctype/lead/lead.py
+++ b/erpnext/selling/doctype/lead/lead.py
@@ -13,8 +13,8 @@
class Lead(SellingController):
def onload(self):
customer = frappe.db.get_value("Customer", {"lead_name": self.name})
- if customer:
- self.set("__is_customer", customer)
+ self.get("__onload").is_customer = customer
+ print "server", self.get("__onload").is_customer
def validate(self):
self._prev = frappe._dict({
diff --git a/erpnext/setup/doctype/company/company.js b/erpnext/setup/doctype/company/company.js
index 41b63c3..ee7d66a 100644
--- a/erpnext/setup/doctype/company/company.js
+++ b/erpnext/setup/doctype/company/company.js
@@ -9,7 +9,8 @@
}
if(!doc.__islocal) {
- cur_frm.toggle_enable("default_currency", !cur_frm.doc.__transactions_exist);
+ cur_frm.toggle_enable("default_currency", (cur_frm.doc.__onload &&
+ !cur_frm.doc.__onload.transactions_exist));
}
}
diff --git a/erpnext/setup/doctype/company/company.py b/erpnext/setup/doctype/company/company.py
index ae6d641..6ea4fc2 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -13,7 +13,7 @@
class Company(Document):
def onload(self):
- self.set("__transactions_exist", self.check_if_transactions_exist())
+ self.get("__onload").transactions_exist = self.check_if_transactions_exist()
def check_if_transactions_exist(self):
exists = False
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.js b/erpnext/stock/doctype/delivery_note/delivery_note.js
index c0586b3..ff551f7 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -17,7 +17,7 @@
refresh: function(doc, dt, dn) {
this._super();
- if(!doc.__billing_complete && doc.docstatus==1) {
+ if(doc.__onload && !doc.__onload.billing_complete && doc.docstatus==1) {
// show Make Invoice button only if Delivery Note is not created from Sales Invoice
var from_sales_invoice = false;
from_sales_invoice = cur_frm.doc.delivery_note_details.some(function(item) {
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 5ea5b6b..da7dd7a 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -37,7 +37,7 @@
where docstatus=1 and delivery_note=%s""", self.name)
if billed_qty:
total_qty = sum((item.qty for item in self.get("delivery_note_details")))
- self.set("__billing_complete", billed_qty[0][0] == total_qty)
+ self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
def get_portal_page(self):
return "shipment" if self.docstatus==1 else None
diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js
index 15c8793..319d67d 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -25,7 +25,7 @@
if (!doc.__islocal && doc.is_stock_item == 'Yes') {
cur_frm.toggle_enable(['has_serial_no', 'is_stock_item', 'valuation_method'],
- doc.__sle_exists=="exists" ? false : true);
+ (doc.__onload && doc.__onload.sle_exists=="exists") ? false : true);
}
erpnext.item.toggle_reqd(cur_frm);
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 9b2d862..642a429 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -13,7 +13,7 @@
class Item(WebsiteGenerator):
def onload(self):
- self.set("__sle_exists", self.check_if_sle_exists())
+ self.get("__onload").sle_exists = self.check_if_sle_exists()
def autoname(self):
if frappe.db.get_default("item_naming_by")=="Naming Series":
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
index 2dc99ef..3d1a216 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -16,9 +16,8 @@
this._super();
if(this.frm.doc.docstatus == 1) {
- if(!this.frm.doc.__billing_complete) {
- cur_frm.add_custom_button(__('Make Purchase Invoice'),
- this.make_purchase_invoice);
+ if(this.frm.doc.__onload && !this.frm.doc.__onload.billing_complete) {
+ cur_frm.add_custom_button(__('Make Purchase Invoice'), this.make_purchase_invoice);
}
cur_frm.add_custom_button('Send SMS', cur_frm.cscript.send_sms);
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index 90161f5..13bb193 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -34,7 +34,7 @@
where purchase_receipt=%s""", self.name)
if billed_qty:
total_qty = sum((item.qty for item in self.get("purchase_receipt_details")))
- self.set("__billing_complete", billed_qty[0][0] == total_qty)
+ self.get("__onload").billing_complete = (billed_qty[0][0] == total_qty)
def validate(self):
super(PurchaseReceipt, self).validate()
diff --git a/erpnext/support/doctype/newsletter/newsletter.js b/erpnext/support/doctype/newsletter/newsletter.js
index 6ea4926..c514a21 100644
--- a/erpnext/support/doctype/newsletter/newsletter.js
+++ b/erpnext/support/doctype/newsletter/newsletter.js
@@ -22,25 +22,25 @@
});
})
}
-
+
cur_frm.cscript.setup_dashboard();
if(doc.__islocal && !doc.send_from) {
- cur_frm.set_value("send_from",
+ cur_frm.set_value("send_from",
repl("%(fullname)s <%(email)s>", frappe.user_info(doc.owner)));
}
}
cur_frm.cscript.setup_dashboard = function() {
cur_frm.dashboard.reset();
- if(!cur_frm.doc.__islocal && cint(cur_frm.doc.email_sent) && cur_frm.doc.__status_count) {
- var stat = cur_frm.doc.__status_count;
+ if(!cur_frm.doc.__islocal && cint(cur_frm.doc.email_sent) && cur_frm.doc.__onload && cur_frm.doc.__onload.status_count) {
+ var stat = cur_frm.doc.__onload.status_count;
var total = frappe.utils.sum($.map(stat, function(v) { return v; }));
if(total) {
$.each(stat, function(k, v) {
stat[k] = flt(v * 100 / total, 2);
});
-
+
cur_frm.dashboard.add_progress("Status", [
{
title: stat["Sent"] + "% Sent",
@@ -60,4 +60,4 @@
]);
}
}
-}
\ No newline at end of file
+}
diff --git a/erpnext/support/doctype/newsletter/newsletter.py b/erpnext/support/doctype/newsletter/newsletter.py
index 4ac9f8a..a6dd8da 100644
--- a/erpnext/support/doctype/newsletter/newsletter.py
+++ b/erpnext/support/doctype/newsletter/newsletter.py
@@ -12,16 +12,9 @@
class Newsletter(Document):
def onload(self):
if self.email_sent:
- self.set("__status_count", dict(frappe.db.sql("""select status, count(*)
+ self.get("__onload").status_count = dict(frappe.db.sql("""select status, count(*)
from `tabBulk Email` where ref_doctype=%s and ref_docname=%s
- group by status""", (self.doctype, self.name))) or None)
-
- def as_dict(self, no_nulls=False):
- doc = super(Newsletter, self).as_dict(no_nulls)
- if self.get("__status_count"):
- doc["__status_count"] = self.get("__status_count")
-
- return doc
+ group by status""", (self.doctype, self.name))) or None
def test_send(self, doctype="Lead"):
self.recipients = self.test_email_id.split(",")