From 1872312cc622c61f05c269a87ecfd983e3025886 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 25 Apr 2014 18:36:39 +0530 Subject: [PATCH] Deprecated client side mapper: frappe.model.map. Fixes #1530 --- .../sales_invoice/sales_invoice_map.js | 19 ------ erpnext/hr/doctype/employee/employee.js | 35 ++--------- erpnext/hr/doctype/employee/employee.py | 26 +++++++++ .../salary_structure/salary_structure.js | 58 +++++++++---------- .../doctype/time_log_batch/time_log_batch.js | 12 ++-- .../doctype/time_log_batch/time_log_batch.py | 22 +++++++ 6 files changed, 88 insertions(+), 84 deletions(-) delete mode 100644 erpnext/accounts/doctype/sales_invoice/sales_invoice_map.js diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice_map.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice_map.js deleted file mode 100644 index 79036f9747e..00000000000 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice_map.js +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors -// License: GNU General Public License v3. See license.txt - -frappe.model.map_info["Sales Invoice"] = { - "Time Log Batch": { - table_map: { - "Sales Invoice Item": "Time Log Batch", - }, - field_map: { - "Sales Invoice Item": { - "base_rate": "rate", - "time_log_batch": "name", - "qty": "total_hours", - "stock_uom": "=Hour", - "description": "=via Time Logs" - } - }, - } -} \ No newline at end of file diff --git a/erpnext/hr/doctype/employee/employee.js b/erpnext/hr/doctype/employee/employee.js index 5d365d07445..39e0c702989 100644 --- a/erpnext/hr/doctype/employee/employee.js +++ b/erpnext/hr/doctype/employee/employee.js @@ -20,7 +20,7 @@ erpnext.hr.EmployeeController = frappe.ui.form.Controller.extend({ refresh: function() { var me = this; erpnext.toggle_naming_series(); - if(!this.frm.doc.__islocal) { + 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); }); } @@ -58,35 +58,10 @@ erpnext.hr.EmployeeController = frappe.ui.form.Controller.extend({ }, make_salary_structure: function(btn) { - var me = this; - this.validate_salary_structure(btn, function(r) { - if(r.message) { - msgprint(__("Active Salary Sructure already exists for Employee {0}", [me.frm.doc.name])); - } else if(!r.exc) { - frappe.model.map({ - source: me.frm.doc, - target: "Salary Structure" - }); - } + frappe.model.open_mapped_doc({ + method: "erpnext.hr.doctype.employee.employee.make_salary_structure", + source_name: cur_frm.doc.name }); - }, - - validate_salary_structure: function(btn, callback) { - var me = this; - return this.frm.call({ - btn: btn, - method: "frappe.client.get_value", - args: { - doctype: "Salary Structure", - fieldname: "name", - filters: { - employee: me.frm.doc.name, - is_active: "Yes", - docstatus: ["!=", 2] - }, - }, - callback: callback - }); - }, + } }); cur_frm.cscript = new erpnext.hr.EmployeeController({frm: cur_frm}); diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py index e1a060b7f42..44c1752139f 100644 --- a/erpnext/hr/doctype/employee/employee.py +++ b/erpnext/hr/doctype/employee/employee.py @@ -10,8 +10,21 @@ from frappe import throw, _ import frappe.permissions from frappe.defaults import get_restrictions from frappe.model.document import Document +from frappe.model.mapper import get_mapped_doc class Employee(Document): + def onload(self): + self.salary_structure_exists = frappe.db.get_value("Salary Structure", + {"employee": self.name, "is_active": "Yes", "docstatus": ["!=", 2]}) + + def as_dict(self): + doc = super(Employee, self).as_dict() + + 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: @@ -203,3 +216,16 @@ def get_retirement_date(date_of_birth=None): dt = getdate(date_of_birth) + datetime.timedelta(21915) ret = {'date_of_retirement': dt.strftime('%Y-%m-%d')} return ret + +@frappe.whitelist() +def make_salary_structure(source_name, target=None): + target = get_mapped_doc("Employee", source_name, { + "Employee": { + "doctype": "Salary Structure", + "field_map": { + "name": "employee" + } + } + }) + target.make_earn_ded_table() + return target diff --git a/erpnext/hr/doctype/salary_structure/salary_structure.js b/erpnext/hr/doctype/salary_structure/salary_structure.js index e4235eac227..e50a99c7ba2 100644 --- a/erpnext/hr/doctype/salary_structure/salary_structure.js +++ b/erpnext/hr/doctype/salary_structure/salary_structure.js @@ -4,18 +4,18 @@ cur_frm.add_fetch('employee', 'company', 'company'); cur_frm.cscript.onload = function(doc, dt, dn){ - e_tbl = doc.earning_details || []; - d_tbl = doc.deduction_details || []; - if (e_tbl.length == 0 && d_tbl.length == 0) - return $c_obj(doc,'make_earn_ded_table','', function(r, rt) { refresh_many(['earning_details', 'deduction_details']);}); + e_tbl = doc.earning_details || []; + d_tbl = doc.deduction_details || []; + if (e_tbl.length == 0 && d_tbl.length == 0) + return $c_obj(doc,'make_earn_ded_table','', function(r, rt) { refresh_many(['earning_details', 'deduction_details']);}); } cur_frm.cscript.refresh = function(doc, dt, dn){ - if((!doc.__islocal) && (doc.is_active == 'Yes')){ - cur_frm.add_custom_button(__('Make Salary Slip'), cur_frm.cscript['Make Salary Slip']); - } + if((!doc.__islocal) && (doc.is_active == 'Yes')){ + cur_frm.add_custom_button(__('Make Salary Slip'), cur_frm.cscript['Make Salary Slip']); + } - cur_frm.toggle_enable('employee', doc.__islocal); + cur_frm.toggle_enable('employee', doc.__islocal); } cur_frm.cscript['Make Salary Slip'] = function() { @@ -26,39 +26,39 @@ cur_frm.cscript['Make Salary Slip'] = function() { } cur_frm.cscript.employee = function(doc, dt, dn){ - if (doc.employee) - return get_server_fields('get_employee_details','','',doc,dt,dn); + if (doc.employee) + return get_server_fields('get_employee_details','','',doc,dt,dn); } cur_frm.cscript.modified_value = function(doc, cdt, cdn){ - calculate_totals(doc, cdt, cdn); + calculate_totals(doc, cdt, cdn); } cur_frm.cscript.d_modified_amt = function(doc, cdt, cdn){ - calculate_totals(doc, cdt, cdn); + calculate_totals(doc, cdt, cdn); } var calculate_totals = function(doc, cdt, cdn) { - var tbl1 = doc.earning_details || []; - var tbl2 = doc.deduction_details || []; - - var total_earn = 0; var total_ded = 0; - for(var i = 0; i < tbl1.length; i++){ - total_earn += flt(tbl1[i].modified_value); - } - for(var j = 0; j < tbl2.length; j++){ - total_ded += flt(tbl2[j].d_modified_amt); - } - doc.total_earning = total_earn; - doc.total_deduction = total_ded; - doc.net_pay = flt(total_earn) - flt(total_ded); - refresh_many(['total_earning', 'total_deduction', 'net_pay']); + var tbl1 = doc.earning_details || []; + var tbl2 = doc.deduction_details || []; + + var total_earn = 0; var total_ded = 0; + for(var i = 0; i < tbl1.length; i++){ + total_earn += flt(tbl1[i].modified_value); + } + for(var j = 0; j < tbl2.length; j++){ + total_ded += flt(tbl2[j].d_modified_amt); + } + doc.total_earning = total_earn; + doc.total_deduction = total_ded; + doc.net_pay = flt(total_earn) - flt(total_ded); + refresh_many(['total_earning', 'total_deduction', 'net_pay']); } cur_frm.cscript.validate = function(doc, cdt, cdn) { - calculate_totals(doc, cdt, cdn); + calculate_totals(doc, cdt, cdn); } cur_frm.fields_dict.employee.get_query = function(doc,cdt,cdn) { - return{ query: "erpnext.controllers.queries.employee_query" } -} \ No newline at end of file + return{ query: "erpnext.controllers.queries.employee_query" } +} diff --git a/erpnext/projects/doctype/time_log_batch/time_log_batch.js b/erpnext/projects/doctype/time_log_batch/time_log_batch.js index 3065f334c17..c2d353531d9 100644 --- a/erpnext/projects/doctype/time_log_batch/time_log_batch.js +++ b/erpnext/projects/doctype/time_log_batch/time_log_batch.js @@ -23,16 +23,16 @@ $.extend(cur_frm.cscript, { "Billed": __("This Time Log Batch has been billed."), "Cancelled": __("This Time Log Batch has been cancelled.") }[doc.status]); - + if(doc.status=="Submitted") { - cur_frm.add_custom_button(__("Make Sales Invoice"), function() { cur_frm.cscript.make_invoice() }, + cur_frm.add_custom_button(__("Make Sales Invoice"), function() { cur_frm.cscript.make_invoice() }, "icon-file-alt"); } }, make_invoice: function() { - frappe.model.map({ - source: cur_frm.doc, - target: "Sales Invoice" + frappe.model.open_mapped_doc({ + method: "erpnext.projects.doctype.time_log_batch.time_log_batch.make_sales_invoice", + source_name: cur_frm.doc.name }); } -}); \ No newline at end of file +}); diff --git a/erpnext/projects/doctype/time_log_batch/time_log_batch.py b/erpnext/projects/doctype/time_log_batch/time_log_batch.py index 6d9ce14fbf7..0724bc77b3e 100644 --- a/erpnext/projects/doctype/time_log_batch/time_log_batch.py +++ b/erpnext/projects/doctype/time_log_batch/time_log_batch.py @@ -8,6 +8,7 @@ import frappe from frappe import _ from frappe.model.document import Document +from frappe.model.mapper import get_mapped_doc class TimeLogBatch(Document): @@ -58,3 +59,24 @@ class TimeLogBatch(Document): tl.sales_invoice = self.sales_invoice tl.ignore_validate_update_after_submit = True tl.save() + +@frappe.whitelist() +def make_sales_invoice(source_name, target=None): + def update_item(source_doc, target_doc, source_parent): + target_doc.stock_uom = "Hour" + target_doc.description = "via Time Logs" + + target = frappe.new_doc("Sales Invoice") + target.append("entries", get_mapped_doc("Time Log Batch", source_name, { + "Time Log Batch": { + "doctype": "Sales Invoice Item", + "field_map": { + "rate": "base_rate", + "name": "time_log_batch", + "total_hours": "qty", + }, + "postprocess": update_item + } + })) + + return target