From 2e0620e370abaf3eb12e254e75504a4cd1bcbc10 Mon Sep 17 00:00:00 2001
From: Nabin Hait
Date: Mon, 19 May 2014 18:15:05 +0530
Subject: [PATCH 1/3] Server side onload functionality in multiple docs
---
erpnext/accounts/doctype/account/account.js | 2 +-
erpnext/accounts/doctype/account/account.py | 9 +-------
.../purchase_invoice/purchase_invoice.js | 6 ++++++
.../doctype/sales_invoice/sales_invoice.js | 4 ++++
erpnext/hr/doctype/employee/employee.js | 7 ++++---
erpnext/hr/doctype/employee/employee.py | 10 +--------
.../salary_structure/salary_structure.js | 1 +
erpnext/selling/doctype/customer/customer.js | 4 ++++
erpnext/selling/doctype/lead/lead.js | 21 +++++++++----------
erpnext/selling/doctype/lead/lead.py | 4 ++--
erpnext/setup/doctype/company/company.js | 3 ++-
erpnext/setup/doctype/company/company.py | 2 +-
.../doctype/delivery_note/delivery_note.js | 2 +-
.../doctype/delivery_note/delivery_note.py | 2 +-
erpnext/stock/doctype/item/item.js | 2 +-
erpnext/stock/doctype/item/item.py | 2 +-
.../purchase_receipt/purchase_receipt.js | 5 ++---
.../purchase_receipt/purchase_receipt.py | 2 +-
.../support/doctype/newsletter/newsletter.js | 12 +++++------
.../support/doctype/newsletter/newsletter.py | 11 ++--------
20 files changed, 52 insertions(+), 59 deletions(-)
diff --git a/erpnext/accounts/doctype/account/account.js b/erpnext/accounts/doctype/account/account.js
index 40a2d3cb69..f085c8bb6b 100644
--- a/erpnext/accounts/doctype/account/account.js
+++ b/erpnext/accounts/doctype/account/account.js
@@ -19,7 +19,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) {
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 3c73d1f2fc..ad588b5291 100644
--- a/erpnext/accounts/doctype/account/account.py
+++ b/erpnext/accounts/doctype/account/account.py
@@ -12,16 +12,9 @@ class Account(Document):
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 3431ff45f1..158dec2046 100644
--- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -110,6 +110,12 @@ erpnext.accounts.PurchaseInvoice = erpnext.buying.BuyingController.extend({
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 ed2a1d4f0f..a5707bb222 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js
@@ -387,6 +387,10 @@ cur_frm.cscript.on_submit = function(doc, cdt, cdn) {
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 fabdfb840e..96aca00532 100644
--- a/erpnext/hr/doctype/employee/employee.js
+++ b/erpnext/hr/doctype/employee/employee.js
@@ -18,9 +18,10 @@ erpnext.hr.EmployeeController = frappe.ui.form.Controller.extend({
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 2ae4a2a9d3..9840df7bcf 100644
--- a/erpnext/hr/doctype/employee/employee.py
+++ b/erpnext/hr/doctype/employee/employee.py
@@ -14,17 +14,9 @@ from frappe.model.mapper import get_mapped_doc
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 8ee67a5be3..ee42d0cf8b 100644
--- a/erpnext/hr/doctype/salary_structure/salary_structure.js
+++ b/erpnext/hr/doctype/salary_structure/salary_structure.js
@@ -57,6 +57,7 @@ var calculate_totals = function(doc, cdt, cdn) {
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 94b8f66da6..e8407581d1 100644
--- a/erpnext/selling/doctype/customer/customer.js
+++ b/erpnext/selling/doctype/customer/customer.js
@@ -42,6 +42,10 @@ cur_frm.cscript.refresh = function(doc, dt, dn) {
}
}
+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 66b52700af..6172b1e6ab 100644
--- a/erpnext/selling/doctype/lead/lead.js
+++ b/erpnext/selling/doctype/lead/lead.js
@@ -10,7 +10,7 @@ erpnext.LeadController = frappe.ui.form.Controller.extend({
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 @@ erpnext.LeadController = frappe.ui.form.Controller.extend({
'+__('Automatically extract Leads from a mail box e.g.')+' "sales@example.com"
';
}
},
-
+
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 @@ erpnext.LeadController = frappe.ui.form.Controller.extend({
// 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 b40132ecb9..6b44e578d3 100644
--- a/erpnext/selling/doctype/lead/lead.py
+++ b/erpnext/selling/doctype/lead/lead.py
@@ -13,8 +13,8 @@ from erpnext.controllers.selling_controller import SellingController
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 41b63c37ab..ee7d66a769 100644
--- a/erpnext/setup/doctype/company/company.js
+++ b/erpnext/setup/doctype/company/company.js
@@ -9,7 +9,8 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) {
}
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 ae6d641755..6ea4fc2724 100644
--- a/erpnext/setup/doctype/company/company.py
+++ b/erpnext/setup/doctype/company/company.py
@@ -13,7 +13,7 @@ from frappe.model.document import Document
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 c0586b3185..ff551f7720 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.js
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.js
@@ -17,7 +17,7 @@ erpnext.stock.DeliveryNoteController = erpnext.selling.SellingController.extend(
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 5ea5b6bcda..da7dd7af56 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -37,7 +37,7 @@ class DeliveryNote(SellingController):
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 15c87932c7..319d67dc90 100644
--- a/erpnext/stock/doctype/item/item.js
+++ b/erpnext/stock/doctype/item/item.js
@@ -25,7 +25,7 @@ cur_frm.cscript.refresh = function(doc) {
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 9b2d8623b5..642a4293a5 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -13,7 +13,7 @@ class WarehouseNotSet(frappe.ValidationError): pass
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 2dc99ef606..3d1a2162a2 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.js
@@ -16,9 +16,8 @@ erpnext.stock.PurchaseReceiptController = erpnext.buying.BuyingController.extend
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 90161f5456..13bb193f4b 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -34,7 +34,7 @@ class PurchaseReceipt(BuyingController):
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 6ea49262fe..c514a214ec 100644
--- a/erpnext/support/doctype/newsletter/newsletter.js
+++ b/erpnext/support/doctype/newsletter/newsletter.js
@@ -22,25 +22,25 @@ cur_frm.cscript.refresh = function(doc) {
});
})
}
-
+
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 @@ cur_frm.cscript.setup_dashboard = function() {
]);
}
}
-}
\ No newline at end of file
+}
diff --git a/erpnext/support/doctype/newsletter/newsletter.py b/erpnext/support/doctype/newsletter/newsletter.py
index 4ac9f8ae85..a6dd8da7c2 100644
--- a/erpnext/support/doctype/newsletter/newsletter.py
+++ b/erpnext/support/doctype/newsletter/newsletter.py
@@ -12,16 +12,9 @@ from frappe.model.document import Document
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(",")
From 85786f307116a985b15fdfccc21ce73bdf506352 Mon Sep 17 00:00:00 2001
From: Nabin Hait
Date: Mon, 19 May 2014 19:31:40 +0530
Subject: [PATCH 2/3] Support ticket / lead status based on communication.
Fixes #1645
---
erpnext/controllers/status_updater.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/erpnext/controllers/status_updater.py b/erpnext/controllers/status_updater.py
index 2f655bae9b..6330508680 100644
--- a/erpnext/controllers/status_updater.py
+++ b/erpnext/controllers/status_updater.py
@@ -88,6 +88,7 @@ class StatusUpdater(Document):
def on_communication(self):
self.communication_set = True
+ self.get("communications").sort(key=lambda d: d.creation)
self.set_status(update=True)
del self.communication_set
From 956d7868b58d4b4a89d14e76627e03cbf8ad4a75 Mon Sep 17 00:00:00 2001
From: Nabin Hait
Date: Tue, 20 May 2014 10:36:14 +0530
Subject: [PATCH 3/3] removed print statement
---
erpnext/selling/doctype/lead/lead.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/erpnext/selling/doctype/lead/lead.py b/erpnext/selling/doctype/lead/lead.py
index 6b44e578d3..3c345efc47 100644
--- a/erpnext/selling/doctype/lead/lead.py
+++ b/erpnext/selling/doctype/lead/lead.py
@@ -14,7 +14,6 @@ class Lead(SellingController):
def onload(self):
customer = frappe.db.get_value("Customer", {"lead_name": self.name})
self.get("__onload").is_customer = customer
- print "server", self.get("__onload").is_customer
def validate(self):
self._prev = frappe._dict({